[geos]Geometry基本的几何对象

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

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值