GDAL C++ API 学习之路 (6) OGRGeometry 几何类 OGRGeometry

Simplify

virtual OGRGeometry *Simplify(double dTolerance) const

简化几何图形

参数:

dTolerance -- 简化的距离容差。

返回: 简化的几何图形或 NULL(如果发生错误)

   // 创建一个多边形对象
    OGRLinearRing ring;
    ring.addPoint(0, 0);
    ring.addPoint(0, 4);
    ring.addPoint(2, 4);
    ring.addPoint(2, 2);
    ring.addPoint(4, 2);
    ring.addPoint(4, 0);
    ring.addPoint(0, 0);

    OGRPolygon polygon;
    polygon.addRing(&ring);

    // 输出原始多边形的点数
    std::cout << "Original polygon points count: " << polygon.getNumPoints() << std::endl;

    // 进行简化
    OGRGeometry* simplifiedGeometry = polygon.Simplify(1.0);

    // 输出简化后的多边形的点数
    cout << "Simplified polygon points count: " << simplifiedGeometry->getNumPoints() << endl;

SimplifyPreserveTopology

OGRGeometry *SimplifyPreserveTopology(double dTolerance) const

简化几何图形,同时保留拓扑

参数:

dTolerance -- 简化的距离容差。

返回: 简化的几何图形或 NULL(如果发生错误)

    // 创建一个多边形对象
    OGRLinearRing ring;
    ring.addPoint(0, 0);
    ring.addPoint(0, 4);
    ring.addPoint(2, 4);
    ring.addPoint(2, 2);
    ring.addPoint(4, 2);
    ring.addPoint(4, 0);
    ring.addPoint(0, 0);

    OGRPolygon polygon;
    polygon.addRing(&ring);

    // 输出原始多边形的点数
    std::cout << "Original polygon points count: " << polygon.getNumPoints() << std::endl;

    // 进行简化,并保持拓扑结构
    OGRGeometry* simplifiedGeometry = polygon.SimplifyPreserveTopology(1.0);

    // 输出简化后的多边形的点数
    std::cout << "Simplified polygon points count: " << simplifiedGeometry->getNumPoints() << std::endl;

    // 释放简化后的几何对象的内存
    delete simplifiedGeometry;

DelaunayTriangulation

virtual OGRGeometry *DelaunayTriangulation(double dfTolerance, int bOnlyEdges) const

返回几何顶点的 Delaunay 三角测量

参数:

  • dfTolerance -- 可选的捕捉容差,用于提高鲁棒性

  • bOnlyEdge——如果为 TRUE,将返回一个 MULTILINESTRING,否则它将返回一个包含三角形多边形的 GEOMETRYCOLLECTION。

返回: 如果发生错误,则由 Delaunay 三角测量或 NULL 生成的几何图形

 // 创建一组点
    OGRPoint points[4];
    points[0].setX(0);
    points[0].setY(0);

    points[1].setX(0);
    points[1].setY(10);

    points[2].setX(10);
    points[2].setY(10);

    points[3].setX(10);
    points[3].setY(0);

    // 创建一个几何对象集合并添加点
    OGRGeometryCollection collection;
    for (int i = 0; i < 4; i++) {
        collection.addGeometry(&points[i]);
    }

    // 对点进行 Delaunay 三角剖分
    OGRGeometry* triangulatedGeometry = collection.DelaunayTriangulation(0.0, 0);

    // 输出三角剖分结果的类型
    if (triangulatedGeometry != nullptr) {
        OGRwkbGeometryType geomType = triangulatedGeometry->getGeometryType();
        std::cout << "Triangulated geometry type: " << OGRGeometryTypeToName(geomType) << std::endl;

        // 释放三角剖分结果的内存
        delete triangulatedGeometry;
    } else {
        std::cout << "Failed to perform Delaunay triangulation." << std::endl;
    }

Polygonize

Virtual OGRGeometry *Polygonize() const

多边形化一组稀疏边        

将创建并返回一个新的几何对象,其中包含重新组装的多边形集合:如果输入集合不对应于多线字符串,或者由于拓扑不一致而无法将边重新组装为多边形时,将返回 NULL

返回: 现在由调用方拥有的新分配的几何图形,或失败时为 NULL

// 创建一组线要素
    OGRLineString lineStrings[3];
    lineStrings[0].addPoint(0, 0);
    lineStrings[0].addPoint(0, 10);

    lineStrings[1].addPoint(0, 10);
    lineStrings[1].addPoint(10, 10);

    lineStrings[2].addPoint(10, 10);
    lineStrings[2].addPoint(10, 0);

    // 创建一个几何对象集合并添加线要素
    OGRGeometryCollection collection;
    for (int i = 0; i < 3; i++) {
        collection.addGeometry(&lineStrings[i]);
    }

    // 对几何对象进行多边形化处理
    OGRGeometry* polygonizedGeometry = collection.Polygonize();

    // 输出多边形化处理结果的类型
    if (polygonizedGeometry != nullptr) {
        OGRwkbGeometryType geomType = polygonizedGeometry->getGeometryType();
        std::cout << "Polygonized geometry type: " << OGRGeometryTypeToName(geomType) << std::endl;

        // 释放多边形化处理结果的内存
        delete polygonizedGeometry;
    } else {
        std::cout << "Failed to polygonize the geometries." << std::endl;
    }

Distance3D

virtual double Distance3D(const OGRGeometry *poOtherGeom) const 

返回两个几何图形之间的 3D 距离

距离表示为与几何坐标相同的单位

返回:  两个几何形状之间的距离

// 创建两个点
OGRPoint point1(0, 0, 0);
OGRPoint point2(3, 4, 5);

// 计算两点之间的三维空间距离
double distance3D = point1.Distance3D(&point2);

cout << "3D Distance between point1 and point2: " << distance3D << endl;

swapXY

virtual void swapXY()

交换 x 和 y 坐标

// 创建一个点对象
OGRPoint point(10, 20);

// 打印交换前的坐标
std::cout << "Before swap: X=" << point.getX() << ", Y=" << point.getY() << std::endl;

// 调用 swapXY 函数交换 X 和 Y 坐标
point.swapXY();

// 打印交换后的坐标
std::cout << "After swap: X=" << point.getX() << ", Y=" << point.getY() << std::endl;

投影

inline OGRPoint *toPoint()

向下投射到 OGRPoint*

意味着事先检查 wkbFlatten(getGeometryType()) == wkbPoint

有很多种不同的投影,但总的是高纬转低维

猜你喜欢

转载自blog.csdn.net/qq_69574549/article/details/131924382