shape2ogr.cpp
来自「GIS系统支持库Geospatial Data Abstraction Libr」· C++ 代码 · 共 1,155 行 · 第 1/3 页
CPP
1,155 行
nPointCount, padfX, padfY, padfZ, NULL); SHPWriteObject( hSHP, iShape, psShape ); SHPDestroyObject( psShape ); CPLFree( padfX ); CPLFree( padfY ); CPLFree( padfZ ); delete poML; }/* ==================================================================== *//* Polygons/MultiPolygons *//* ==================================================================== */ else if( hSHP->nShapeType == SHPT_POLYGON || hSHP->nShapeType == SHPT_POLYGONM || hSHP->nShapeType == SHPT_POLYGONZ ) { OGRPolygon *poPoly; OGRLinearRing *poRing, **papoRings=NULL; double *padfX=NULL, *padfY=NULL, *padfZ=NULL; int iPoint, iRing, nRings, nVertex=0, *panRingStart; SHPObject *psShape; /* Collect list of rings */ if( wkbFlatten(poGeom->getGeometryType()) == wkbPolygon ) { poPoly = (OGRPolygon *) poGeom; nRings = poPoly->getNumInteriorRings()+1; papoRings = (OGRLinearRing **) CPLMalloc(sizeof(void*)*nRings); for( iRing = 0; iRing < nRings; iRing++ ) { if( iRing == 0 ) papoRings[iRing] = poPoly->getExteriorRing(); else papoRings[iRing] = poPoly->getInteriorRing( iRing-1 ); } } else if( wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon || wkbFlatten(poGeom->getGeometryType()) == wkbGeometryCollection ) { OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeom; int iGeom; nRings = 0; for( iGeom=0; iGeom < poGC->getNumGeometries(); iGeom++ ) { poPoly = (OGRPolygon *) poGC->getGeometryRef( iGeom ); if( wkbFlatten(poPoly->getGeometryType()) != wkbPolygon ) { CPLFree( papoRings ); CPLError( CE_Failure, CPLE_AppDefined, "Attempt to write non-polygon (%s) geometry to " " type shapefile.", poGeom->getGeometryName() ); return OGRERR_UNSUPPORTED_GEOMETRY_TYPE; } papoRings = (OGRLinearRing **) CPLRealloc(papoRings, sizeof(void*) * (nRings+poPoly->getNumInteriorRings()+1)); for( iRing = 0; iRing < poPoly->getNumInteriorRings()+1; iRing++ ) { if( iRing == 0 ) papoRings[nRings+iRing] = poPoly->getExteriorRing(); else papoRings[nRings+iRing] = poPoly->getInteriorRing( iRing-1 ); } nRings += poPoly->getNumInteriorRings()+1; } } else { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to write non-polygon (%s) geometry to " " type shapefile.", poGeom->getGeometryName() ); return OGRERR_UNSUPPORTED_GEOMETRY_TYPE; } /* count vertices */ nVertex = 0; for( iRing = 0; iRing < nRings; iRing++ ) nVertex += papoRings[iRing]->getNumPoints(); panRingStart = (int *) CPLMalloc(sizeof(int) * nRings); padfX = (double *) CPLMalloc(sizeof(double)*nVertex); padfY = (double *) CPLMalloc(sizeof(double)*nVertex); padfZ = (double *) CPLMalloc(sizeof(double)*nVertex); /* collect vertices */ nVertex = 0; for( iRing = 0; iRing < nRings; iRing++ ) { poRing = papoRings[iRing]; panRingStart[iRing] = nVertex; for( iPoint = 0; iPoint < poRing->getNumPoints(); iPoint++ ) { padfX[nVertex] = poRing->getX( iPoint ); padfY[nVertex] = poRing->getY( iPoint ); padfZ[nVertex] = poRing->getZ( iPoint ); nVertex++; } } psShape = SHPCreateObject( hSHP->nShapeType, iShape, nRings, panRingStart, NULL, nVertex, padfX, padfY, padfZ, NULL ); SHPRewindObject( hSHP, psShape ); SHPWriteObject( hSHP, iShape, psShape ); SHPDestroyObject( psShape ); CPLFree( papoRings ); CPLFree( panRingStart ); CPLFree( padfX ); CPLFree( padfY ); CPLFree( padfZ ); } else { /* do nothing for multipatch */ return OGRERR_UNSUPPORTED_GEOMETRY_TYPE; } return OGRERR_NONE;}/************************************************************************//* SHPReadOGRFeatureDefn() *//************************************************************************/OGRFeatureDefn *SHPReadOGRFeatureDefn( const char * pszName, SHPHandle hSHP, DBFHandle hDBF ){ OGRFeatureDefn *poDefn = new OGRFeatureDefn( pszName ); int iField; for( iField = 0; hDBF != NULL && iField < DBFGetFieldCount( hDBF ); iField++ ) { char szFieldName[20]; int nWidth, nPrecision; DBFFieldType eDBFType; OGRFieldDefn oField("", OFTInteger); eDBFType = DBFGetFieldInfo( hDBF, iField, szFieldName, &nWidth, &nPrecision ); oField.SetName( szFieldName ); oField.SetWidth( nWidth ); oField.SetPrecision( nPrecision ); if( eDBFType == FTDouble ) oField.SetType( OFTReal ); else if( eDBFType == FTInteger ) oField.SetType( OFTInteger ); else oField.SetType( OFTString ); poDefn->AddFieldDefn( &oField ); } if( hSHP == NULL ) poDefn->SetGeomType( wkbNone ); else { switch( hSHP->nShapeType ) { case SHPT_POINT: case SHPT_POINTM: poDefn->SetGeomType( wkbPoint ); break; case SHPT_POINTZ: poDefn->SetGeomType( wkbPoint25D ); break; case SHPT_ARC: case SHPT_ARCM: poDefn->SetGeomType( wkbLineString ); break; case SHPT_ARCZ: poDefn->SetGeomType( wkbLineString25D ); break; case SHPT_MULTIPOINT: case SHPT_MULTIPOINTM: poDefn->SetGeomType( wkbMultiPoint ); break; case SHPT_MULTIPOINTZ: poDefn->SetGeomType( wkbMultiPoint25D ); break; case SHPT_POLYGON: case SHPT_POLYGONM: poDefn->SetGeomType( wkbPolygon ); break; case SHPT_POLYGONZ: poDefn->SetGeomType( wkbPolygon25D ); break; } } return poDefn;}/************************************************************************//* SHPReadOGRFeature() *//************************************************************************/OGRFeature *SHPReadOGRFeature( SHPHandle hSHP, DBFHandle hDBF, OGRFeatureDefn * poDefn, int iShape ){ if( iShape < 0 || (hSHP != NULL && iShape >= hSHP->nRecords) || (hDBF != NULL && iShape >= hDBF->nRecords) ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read shape with feature id (%d) out of available" " range.", iShape ); return NULL; } OGRFeature *poFeature = new OGRFeature( poDefn ); if( hSHP != NULL ) poFeature->SetGeometryDirectly( SHPReadOGRObject( hSHP, iShape ) ); for( int iField = 0; iField < poDefn->GetFieldCount(); iField++ ) { // Skip null fields. if( DBFIsAttributeNULL( hDBF, iShape, iField ) ) continue; switch( poDefn->GetFieldDefn(iField)->GetType() ) { case OFTString: poFeature->SetField( iField, DBFReadStringAttribute( hDBF, iShape, iField ) ); break; case OFTInteger: poFeature->SetField( iField, DBFReadIntegerAttribute( hDBF, iShape, iField ) ); break; case OFTReal: poFeature->SetField( iField, DBFReadDoubleAttribute( hDBF, iShape, iField ) ); break; default: CPLAssert( FALSE ); } } if( poFeature != NULL ) poFeature->SetFID( iShape ); return( poFeature );}/************************************************************************//* SHPWriteOGRFeature() *//* *//* Write to an existing feature in a shapefile, or create a new *//* feature. *//************************************************************************/OGRErr SHPWriteOGRFeature( SHPHandle hSHP, DBFHandle hDBF, OGRFeatureDefn * poDefn, OGRFeature * poFeature ){#ifdef notdef/* -------------------------------------------------------------------- *//* Don't write objects with missing geometry. *//* -------------------------------------------------------------------- */ if( poFeature->GetGeometryRef() == NULL && hSHP != NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to write feature without geometry not supported" " for shapefile driver." ); return OGRERR_UNSUPPORTED_GEOMETRY_TYPE; }#endif/* -------------------------------------------------------------------- *//* Write the geometry. *//* -------------------------------------------------------------------- */ OGRErr eErr; if( hSHP != NULL ) { eErr = SHPWriteOGRObject( hSHP, poFeature->GetFID(), poFeature->GetGeometryRef() ); if( eErr != OGRERR_NONE ) return eErr; } /* -------------------------------------------------------------------- *//* If this is a new feature, establish it's feature id. *//* -------------------------------------------------------------------- */ if( poFeature->GetFID() == OGRNullFID ) poFeature->SetFID( DBFGetRecordCount( hDBF ) );/* -------------------------------------------------------------------- *//* If this is the first feature to be written, verify that we *//* have at least one attribute in the DBF file. If not, create *//* a dummy FID attribute to satisfy the requirement that there *//* be at least one attribute. *//* -------------------------------------------------------------------- */ if( DBFGetRecordCount( hDBF ) == 0 && DBFGetFieldCount( hDBF ) == 0 ) { CPLDebug( "OGR", "Created dummy FID field for shapefile since schema is empty."); DBFAddField( hDBF, "FID", FTInteger, 11, 0 ); }/* -------------------------------------------------------------------- *//* Write out dummy field value if it exists. *//* -------------------------------------------------------------------- */ if( DBFGetFieldCount( hDBF ) == 1 && poDefn->GetFieldCount() == 0 ) { DBFWriteIntegerAttribute( hDBF, poFeature->GetFID(), 0, poFeature->GetFID() ); }/* -------------------------------------------------------------------- *//* Write all the fields. *//* -------------------------------------------------------------------- */ for( int iField = 0; iField < poDefn->GetFieldCount(); iField++ ) { if( !poFeature->IsFieldSet( iField ) ) { DBFWriteNULLAttribute( hDBF, poFeature->GetFID(), iField ); continue; } switch( poDefn->GetFieldDefn(iField)->GetType() ) { case OFTString: DBFWriteStringAttribute( hDBF, poFeature->GetFID(), iField, poFeature->GetFieldAsString( iField )); break; case OFTInteger: DBFWriteIntegerAttribute( hDBF, poFeature->GetFID(), iField, poFeature->GetFieldAsInteger(iField) ); break; case OFTReal: DBFWriteDoubleAttribute( hDBF, poFeature->GetFID(), iField, poFeature->GetFieldAsDouble(iField) ); break; default: CPLAssert( FALSE ); } } return OGRERR_NONE;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?