读取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::pairpair; 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 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