欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[geos]Geometry基本的几何对象

程序员文章站 2022-04-02 16:58:39
...

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

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

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

定义类变量:

 GeometryFactory::unique_ptr global_factory;

构造中初始化

// 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);

 

方法体中调用:  

      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);