[geos]Geometry基本的几何对象

读取shp中的点,读取shp中的线,

(1)读取shp中的多边形,修改属性字段的值。

类库版本:geos3.6.2,shapelib1.3

添加引用:

#include "geos.h"
using namespace geos;

定义类变量:

 GeometryFactory::unique_ptr global_factory;

构造中初始化

1
2
3
4
5
6
7
// Define a precision model using 0,0 as the reference origin
// and 2.0 as coordinates scale.
PrecisionModel *pm = new PrecisionModel(1.0, 0, 0);
 
// Initialize global factory with defined PrecisionModel
// and a SRID of -1 (undefined).
global_factory = GeometryFactory::create(pm, -1);

 

方法体中调用:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  std::string::size_type pos=pszShapeFile.find('.');
/*std::string ext=filename.substr(pos==string::npos? pszShapeFile.length():pos+1); */
 
std::string tmp=pszShapeFile;
std::string dbfname=tmp.replace(tmp.begin()+pos+1,tmp.end(),"dbf");
SHPHandle hShp= SHPOpen(pszShapeFile.c_str(), "r");
DBFHandle   hDBF = DBFOpen( dbfname.c_str(), "r+b" );
if( hDBF == NULL )
{
    return;
}
int idxCeiling=DBFGetFieldIndex(hDBF,"CeilingZ");
int idxFloor=DBFGetFieldIndex(hDBF,"FloorZ");
if (idxCeiling==-1)
{
    idxCeiling=DBFAddField(hDBF,"CeilingZ",FTDouble, 10, 4);
}
if (idxFloor==-1)
{
    idxFloor=DBFAddField(hDBF,"FloorZ",FTDouble, 10, 4);
}
int nShapeType, nVertices;
int nEntities = 0;
double* minB = new double[4];
double* maxB = new double[4];
SHPGetInfo(hShp, &nEntities, &nShapeType, minB, maxB);
printf("ShapeType:%d\n", nShapeType);
printf("Number of Rooms: %d\n", nEntities);
if (nShapeType==SHPT_POLYGON ||nShapeType==SHPT_POLYGONZ)
{
    geos::geom::CoordinateArraySequenceFactory csf;
 
    for (int idx = 0; idx < nEntities;idx++)
    {
        std::pair<int, int> pair;
        cell_residual.resize (num_of_hists, pair);
        int iShape = idx;
        SHPObject *obj = SHPReadObject(hShp, iShape);
 
        int parts = obj->nParts;
        int verts=obj->nVertices;
        printf("nParts:%d\n", parts);
        printf("nVertices:%d\n", verts);
        geos::geom::CoordinateSequence* cs1 = csf.create(verts,2);
        for (size_t j = 0; j < verts; j++)
        {
            double x = obj->padfX[j];
            double y = obj->padfY[j];
            cs1->setAt(Coordinate (x,y,0),j);
        }
        geos::geom::LinearRing* ring1 = global_factory->createLinearRing(cs1);
        geos::geom::Geometry* p1 = global_factory->createPolygon(ring1,NULL);
        //根据房间范围遍历每一个点
        for (int i=0;i<pcl_t_cloud->points.size();i++)
        {
            pcl::PointXYZ pt=pcl_t_cloud->points[i];
 
            geos::geom::Coordinate coord(pt.x,pt.y,0);
            geos::geom::Geometry* pt_g=global_factory->createPoint(coord);
            bool flag=p1->contains(pt_g);
            if (flag)
            {
                int indx=floor((pt.z-minPt.z)/interval);
                if (indx<num_of_hists)
                {
                    cell_residual[indx].first = indx;
                    int ptscount=cell_residual[indx].second;
                    cell_residual[indx].second = ptscount+1;
                }
            }
        }
        //排序高度数组
        std::sort (cell_residual.begin (), cell_residual.end (), comparePair2);
        //得到最大和最小值,统计数目最多的两个
        double minZ=cell_residual[num_of_hists-2].first*interval + minPt.z;
        double maxZ=cell_residual[num_of_hists-1].first*interval + minPt.z;
        //赋值2个属性
        DBFWriteDoubleAttribute(hDBF, iShape ,idxCeiling,std::min(minZ,maxZ) );
        DBFWriteDoubleAttribute(hDBF, iShape ,idxFloor,std::max(minZ,maxZ) );
        cell_residual.clear();
    }
     
}
DBFClose( hDBF );
SHPClose(hShp);

  

posted @   太一吾鱼水  阅读(2568)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示