gdal求矢量图形的形心

gdal求矢量图形的形心

#include "gdal_priv.h"
#include "ogrsf_frmts.h"

int main() {
    OGRRegisterAll();

    OGRPolygon* square_1 = new OGRPolygon();
    OGRLinearRing* ring_1 = new OGRLinearRing();

    // 添加 square_1 的点
    ring_1->addPoint(0, 0);
    ring_1->addPoint(3, 2);
    ring_1->addPoint(4, 5);
    ring_1->addPoint(1, 6);
    ring_1->addPoint(0, 0);  // 添加起始点以形成闭环
    square_1->addRingDirectly(ring_1);

    OGRPolygon* square_2 = new OGRPolygon();
    OGRLinearRing* ring_2 = new OGRLinearRing();

    // 添加 square_2 的点
    ring_2->addPoint(3, 0);
    ring_2->addPoint(7, 1);
    ring_2->addPoint(6, 4);
    ring_2->addPoint(3, 3);
    ring_2->addPoint(3, 0);  // 添加起始点以形成闭环
    square_2->addRingDirectly(ring_2);

    OGRPoint point_1;
    OGRPoint point_2;

    auto a = square_1->Centroid(&point_1);  // 计算square_1的形心
    auto b = square_2->Centroid(&point_2);  // 计算square_2的形心


    // 释放内存
    delete square_1;
    delete square_2;

    return 0;
}

在这里插入图片描述
在这里插入图片描述

你可能感兴趣的:(图片,C++,c++,形心,矢量图形,gdal)