//读取Shape图形 GEOMETRY::geom::Geometry* CShapefileFeatureClass::ReadShape(long index) { typedef vector<GEOMETRY::geom::Coordinate> CoordVect; SHPObject *psShape; Geometry *pGeometry = NULL; psShape = SHPReadObject( m_shpHandle, index ); if( psShape == NULL ) { return NULL; } Coordinate coord; /* -------------------------------------------------------------------- */ /* Point. */ /* -------------------------------------------------------------------- */ if( psShape->nSHPType == SHPT_POINT || psShape->nSHPType == SHPT_POINTM || psShape->nSHPType == SHPT_POINTZ ) { coord.x =psShape->padfX[0]; coord.y = psShape->padfY[0]; coord.z = psShape->padfZ[0]; coord.m =psShape->padfM[0]; pGeometry =(Geometry*)GeometryFactory::getDefaultInstance()->createPoint(coord); } /* -------------------------------------------------------------------- */ /* Multipoint. */ /* -------------------------------------------------------------------- */ else if( psShape->nSHPType == SHPT_MULTIPOINT || psShape->nSHPType == SHPT_MULTIPOINTM || psShape->nSHPType == SHPT_MULTIPOINTZ ) { if (psShape->nVertices == 0) { pGeometry = NULL; } else { CoordVect *pcoords =new CoordVect(psShape->nVertices); for(long i=0;i<psShape->nVertices;i++) { coord.x =psShape->padfX[i]; coord.y = psShape->padfY[i]; coord.z = psShape->padfZ[i]; coord.m =psShape->padfM[i]; (*pcoords)[i] =coord; } CoordinateSequence *coords = GeometryFactory::getDefaultInstance()->getCoordinateSequenceFactory()->create(pcoords); pGeometry =(Geometry*)GeometryFactory::getDefaultInstance()->createMultiPoint(*coords); delete coords; } } /* -------------------------------------------------------------------- */ /* Arc (LineString) */ /* */ /* */ /* -------------------------------------------------------------------- */ else if( psShape->nSHPType == SHPT_ARC || psShape->nSHPType == SHPT_ARCM || psShape->nSHPType == SHPT_ARCZ ) { if( psShape->nParts == 0 ) { pGeometry = NULL; } /*else if( psShape->nParts == 1 ) { CoordVect *pcoords =new CoordVect(psShape->nVertices); for(long i=0;i<psShape->nVertices;i++) { coord.x =psShape->padfX[i]; coord.y = psShape->padfY[i]; coord.z = psShape->padfZ[i]; coord.m =psShape->padfM[i]; (*pcoords)[i] =coord; } CoordinateSequence *coords = GeometryFactory::getDefaultInstance()->getCoordinateSequenceFactory()->create(pcoords); pGeometry =(Geometry*)GeometryFactory::getDefaultInstance()->createLineString(coords); }*/ else { int iRing; int nRingPoints; int nRingStart; CoordVect *pcoords; int i; //建立一个空的多线要素 pGeometry =(Geometry*)GeometryFactory::getDefaultInstance()->createMultiLineString(); for( iRing = 0; iRing < psShape->nParts; iRing++ ) { if( psShape->panPartStart == NULL ) { nRingPoints = psShape->nVertices; nRingStart = 0; } else { if( iRing == psShape->nParts - 1 ) nRingPoints =psShape->nVertices - psShape->panPartStart[iRing]; else nRingPoints = psShape->panPartStart[iRing+1]- psShape->panPartStart[iRing]; nRingStart = psShape->panPartStart[iRing]; } pcoords =new CoordVect(nRingPoints); for(i=0;i<nRingPoints;i++) { coord.x = *(psShape->padfX+nRingStart+i); coord.y = *(psShape->padfY+nRingStart+i); coord.z = *(psShape->padfZ+nRingStart+i); coord.m = *(psShape->padfM+nRingStart+i); (*pcoords)[i] =coord; } CoordinateSequence *coords = GeometryFactory::getDefaultInstance()->getCoordinateSequenceFactory()->create(pcoords); LineString *pline = GeometryFactory::getDefaultInstance()->createLineString(coords); ((MultiLineString*)pGeometry)->AddGeometry((Geometry*)pline); } } } /* -------------------------------------------------------------------- */ /* Polygon */ /* */ /* 现在没有判断是否是内环外环,需要改进 */ /* -------------------------------------------------------------------- */ else if( psShape->nSHPType == SHPT_POLYGON || psShape->nSHPType == SHPT_POLYGONM || psShape->nSHPType == SHPT_POLYGONZ ) { if ( psShape->nParts == 0 ) { pGeometry = NULL; } //建立一个空的多边形要素 pGeometry =(Geometry*)GeometryFactory::getDefaultInstance()->createPolygon(); if ( psShape->nParts == 1 ) { CoordVect *pcoords =new CoordVect(psShape->nVertices); for(int i=0;i<psShape->nVertices;i++) { coord.x =psShape->padfX[i]; coord.y = psShape->padfY[i]; coord.z = psShape->padfZ[i]; coord.m =psShape->padfM[i]; (*pcoords)[i] =coord; } CoordinateSequence *coords = GeometryFactory::getDefaultInstance()->getCoordinateSequenceFactory()->create(pcoords); LinearRing *pring = GeometryFactory::getDefaultInstance()->createLinearRing(coords); ((GEOMETRY::geom::Polygon*)pGeometry)->AddGeometry((Geometry*)pring); } else { int iRing; int nRingPoints; int nRingStart; int i; CoordVect *pcoords =NULL; for( iRing = 0; iRing < psShape->nParts; iRing++ ) { if( psShape->panPartStart == NULL ) { nRingPoints = psShape->nVertices; nRingStart = 0; } else { if( iRing == psShape->nParts - 1 ) nRingPoints =psShape->nVertices - psShape->panPartStart[iRing]; else nRingPoints = psShape->panPartStart[iRing+1]- psShape->panPartStart[iRing]; nRingStart = psShape->panPartStart[iRing]; } pcoords =new CoordVect(nRingPoints); for(i=0;i<nRingPoints;i++) { coord.x = *(psShape->padfX+nRingStart+i); coord.y = *(psShape->padfY+nRingStart+i); coord.z = *(psShape->padfZ+nRingStart+i); coord.m = *(psShape->padfM+nRingStart+i); (*pcoords)[i] =coord; } CoordinateSequence *coords = GeometryFactory::getDefaultInstance()->getCoordinateSequenceFactory()->create(pcoords); LinearRing *pring = GeometryFactory::getDefaultInstance()->createLinearRing(coords); ((GEOMETRY::geom::Polygon*)pGeometry)->AddGeometry((Geometry*)pring); } } } /* -------------------------------------------------------------------- */ /* Otherwise for now we just ignore the object. Eventually we */ /* should implement multipatch. */ /* -------------------------------------------------------------------- */ else { if( psShape->nSHPType != SHPT_NULL ) { } /* nothing returned */ } /* -------------------------------------------------------------------- */ /* Cleanup shape, and set feature id. */ /* -------------------------------------------------------------------- */ SHPDestroyObject( psShape ); if(pGeometry) { pGeometry->SetbZ(HasZ()); pGeometry->SetbM(HasM()); } return pGeometry; }