GDAL C++ API 学习之路 OGRGeometry 线类 OGRLineString

OGRLineString class "ogr_geometry.h"

OGRLineString 类是 OGR 库中的一个几何对象类,用于表示线段或折线。它由多个坐标点组成,并且在坐标点之间形成线段。OGRLineString 可以包含 2D、3D 或 3D+M 坐标点,其中 M 表示额外的度量值,例如时间或速度

Public Functions

OGRLineString()

创建一个空线串

OGRLineString(const OGRLineString &other**)**

复制构造函数

OGRLineString &operator=(const OGRLineString &other**)**

赋值运算符

clone

virtual OGRLineString *clone() const override

创建此对象的副本

复制代码
OGRLineString lineString;

// 添加两个坐标点构成线段
lineString.addPoint(0.0, 0.0);
lineString.addPoint(10.0, 10.0);

// 克隆 OGRLineString 对象
OGRLineString* clonedLineString = lineString.clone();

CurveToLine

virtual OGRLineString *CurveToLine(double dfMaxAngleStepSizeDegrees = 0, const char *const *papszOptions = nullptr) const override

从曲线几何返回线串

参数:

  • dfMaxAngleStepSizeDegrees -- 沿弧的最大步长(以度为单位),使用默认设置为零。

  • papszOptions -- 选项作为以 null 结尾的字符串列表或 NULL。

返回: 近似曲线的线串

复制代码
OGRLineString lineString;
    
// Add points to the lineString (this could be a curve)
lineString.addPoint(0, 0);
lineString.addPoint(1, 1);
lineString.addPoint(2, 0);
    
// Convert curve to line
OGRLineString* convertedLine = lineString.CurveToLine();
    
// Output the converted line's points
for (int i = 0; i < convertedLine->getNumPoints(); ++i) {
    double x, y;
    convertedLine->getPoint(i, &x, &y);
    std::cout << "Point " << i << ": (" << x << ", " << y << ")" << std::endl;
}
    
// Release memory
delete convertedLine;

getCurveGeometry

virtual OGRGeometry *getCurveGeometry(const char *const *papszOptions = nullptr) const override

返回此几何图形的曲线版本

参数:

papszOptions -- 选项作为以 null 结尾的字符串列表。暂时未使用。必须设置为 NULL。

返回: 新的几何图形

复制代码
// 创建一个线性曲线对象
    OGRLineString linearRing;
    linearRing.addPoint(0, 0);
    linearRing.addPoint(1, 0);
    linearRing.addPoint(1, 1);
    linearRing.addPoint(0, 1);
    linearRing.addPoint(0, 0);

    // 获取线性曲线的几何对象
    OGRGeometry* curveGeometry = linearRing.getCurveGeometry();

    // 输出几何对象的类型
    if (curveGeometry != nullptr) {
        OGRwkbGeometryType geomType = curveGeometry->getGeometryType();
        const char* typeName = OGRGeometryTypeToName(geomType);
        printf("Curve geometry type: %s\n", typeName);

        // 释放内存
        OGRGeometryFactory::destroyGeometry(curveGeometry);
    } else {
        printf("Failed to get curve geometry.\n");
    }

get_Area

virtual double get_Area() const override

获取(闭合)曲线的面积

返回: 要素的面积(以使用的空间参考系统的平方单位表示)

复制代码
// 创建一个多边形对象
    OGRLinearRing ring;
    ring.addPoint(0, 0);
    ring.addPoint(1, 0);
    ring.addPoint(1, 1);
    ring.addPoint(0, 1);
    ring.addPoint(0, 0);

    OGRPolygon polygon;
    polygon.addRing(&ring);

    // 获取多边形对象的面积
    double area = polygon.get_Area();

    // 输出面积
    printf("Polygon area: %f\n", area);

getGeometryType

virtual OGRwkbGeometryType getGeometryType() const override

返回:

几何类型类型

复制代码
    // 创建一个点对象
    OGRPoint point(10.0, 20.0);

    // 获取点对象的几何类型
    OGRwkbGeometryType geomType = point.getGeometryType();

    // 输出几何类型
    switch (geomType) {
        case wkbPoint:
            printf("Geometry Type: Point\n");
            break;
        case wkbLineString:
            printf("Geometry Type: LineString\n");
            break;
        case wkbPolygon:
            printf("Geometry Type: Polygon\n");
            break;
        // 更多几何类型的处理...
        default:
            printf("Unknown Geometry Type\n");
            break;
    }

getGeometryName

Virtual const char *getGeometryName() const override

获取几何类型的 WKT 名称

返回: 用于此几何类型的名称,采用众所周知的文本格式。返回的指针指向静态内部字符串,不应修改或释放

复制代码
// 创建一个点对象
    OGRPoint point(10.0, 20.0);

    // 获取几何对象的名称
    const char* geometryName = point.getGeometryName();

    // 输出几何对象的名称
    printf("Geometry Name: %s\n", geometryName);

isClockwise

virtual int isClockwise() const override

如果环具有顺时针绕组(或小于 2 磅),则返回 TRUE

返回: 如果顺时针为真,否则为假

复制代码
/ 创建一个多边形对象
    OGRLinearRing ring;
    ring.addPoint(0, 0);
    ring.addPoint(0, 1);
    ring.addPoint(1, 1);
    ring.addPoint(1, 0);
    ring.addPoint(0, 0);

    OGRPolygon polygon;
    polygon.addRing(&ring);

    // 判断多边形顶点排列方向
    int clockwise = polygon.isClockwise();

    // 输出判断结果
    if (clockwise > 0) {
        printf("Polygon is clockwise.\n");
    } else if (clockwise < 0) {
        printf("Polygon is counterclockwise.\n");
    } else {
        printf("Vertices are collinear or not a valid polygon.\n");
    }
相关推荐
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码5 天前
嵌入式学习路线
学习
毛小茛5 天前
计算机系统概论——校验码
学习
babe小鑫5 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms5 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下5 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。5 天前
2026.2.25监控学习
学习
im_AMBER5 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J5 天前
从“Hello World“ 开始 C++
c语言·c++·学习