void wxGISSimpleCircleSymbol::Draw(const wxGISGeometry &Geometry, int nLevel) { if (!Geometry.IsOk() || !m_pDisplay) return; OGRwkbGeometryType eGeomType = wkbFlatten(Geometry.GetType()); if (eGeomType != wkbMultiPoint) return; OGREnvelope Env; OGRGeometry *pGeom = Geometry; OGRMultiPoint* pMPT = (OGRMultiPoint*)pGeom; OGRPoint* pCenterPt = (OGRPoint*)pMPT->getGeometryRef(0); OGRPoint* pOriginPt = (OGRPoint*)pMPT->getGeometryRef(1); double dfRadius = sqrt((pCenterPt->getX() - pOriginPt->getX())*(pCenterPt->getX() - pOriginPt->getX()) + (pCenterPt->getY() - pOriginPt->getY())*(pCenterPt->getY() - pOriginPt->getY())); Env.MaxX = pCenterPt->getX() + dfRadius; Env.MinX = pCenterPt->getX() - dfRadius; Env.MaxY = pCenterPt->getY() + dfRadius; Env.MinY = pCenterPt->getY() - dfRadius; if (!m_pDisplay->CanDraw(Env)) return; wxCriticalSectionLocker lock(m_pDisplay->GetLock()); if (!m_pDisplay->CheckDrawAsPoint(Env, m_pLineSymbol->GetWidth())) { if (!m_pDisplay->DrawCircle(pCenterPt->getX(), pCenterPt->getY(), 0, 0, dfRadius)) { return; } if (m_Color.Alpha() > 0) { switch (m_eFillRule) { case enumGISFillRuleWinding: m_pDisplay->SetFillRule(CAIRO_FILL_RULE_WINDING); break; case enumGISFillRuleOdd: m_pDisplay->SetFillRule(CAIRO_FILL_RULE_EVEN_ODD); break; } m_pDisplay->SetColor(m_Color); m_pDisplay->FillPreserve(); } } m_pLineSymbol->SetStyleToDisplay(); m_pDisplay->Stroke(); }
OGREnvelope wxGISShape::GetBounds() const { OGREnvelope oReturnBounds; switch (m_eType) { case enumGISShapeTypeRectangle: case enumGISShapeTypePolygon: case enumGISShapeTypeLine: case enumGISShapeTypeEllipse: case enumGISShapeTypeMarker: oReturnBounds = m_oGeom.GetEnvelope(); break; case enumGISShapeTypeCircle: { OGRGeometry *pGeom = m_oGeom; OGRMultiPoint* pMPT = (OGRMultiPoint*)pGeom; OGRPoint* pCenterPt = (OGRPoint*)pMPT->getGeometryRef(0); OGRPoint* pOriginPt = (OGRPoint*)pMPT->getGeometryRef(1); double dfRadius = sqrt((pCenterPt->getX() - pOriginPt->getX())*(pCenterPt->getX() - pOriginPt->getX()) + (pCenterPt->getY() - pOriginPt->getY())*(pCenterPt->getY() - pOriginPt->getY())); oReturnBounds.MaxX = pCenterPt->getX() + dfRadius; oReturnBounds.MinX = pCenterPt->getX() - dfRadius; oReturnBounds.MaxY = pCenterPt->getY() + dfRadius; oReturnBounds.MinY = pCenterPt->getY() - dfRadius; break; } #ifdef wxGIS_USE_SPLINE case enumGISShapeTypeCurve://TODO: case enumGISShapeTypeFreeHand: break; #endif //wxGIS_USE_SPLINE case enumGISShapeTypeFreeHand: oReturnBounds = m_oGeom.GetEnvelope(); break; }; return oReturnBounds; }
bool ShpReader::Open(std::wstring fullPath, StudyControllerPtr studyController, VectorMapControllerPtr vectorMapController, ProgressDlgPtr progressDlg) { // Registers all format drivers built into GDAL/OGR. OGRRegisterAll(); OGRDataSource *OGRDataset; // Open vector file path std::string tempStr( fullPath.begin(), fullPath.end() ); OGRDataset = OGRSFDriverRegistrar::Open( tempStr.c_str(), FALSE ); // Return if no vector files are found if( OGRDataset == NULL ) { Log::Inst().Warning("(Warning) Failed to open file at " + tempStr + "."); return false; } if ( App::Inst().GetLayerTreeController()->GetNumMapLayers() >0 ) MapControllerPtr mapController= App::Inst().GetLayerTreeController()->GetLayerTreeModel()->GetStudy(0)->GetMapLayer(0)->GetMapController(); // It appears that shapefiles (*.SHP) only support up to one layer per file // This will need to be further investigated for other vector filetypes (e.g., KML) // For now just grab layer at position 0 OGRLayer *poLayer = OGRDataset->GetLayer( 0 ); // Determine the XY boundaries for the entire vector dataset OGREnvelope psEnvelope; VectorMapModelPtr vectorMapModel = vectorMapController->GetVectorMapModel(); poLayer->GetExtent( &psEnvelope ); vectorMapModel->SetVectorBoundary(psEnvelope); if(!SetupVectorProjection(OGRDataset,studyController,poLayer,vectorMapController )) { OGRDataset->DestroyDataSource(OGRDataset); return false; } if(progressDlg) { if(!progressDlg->Update(0, _T("Reading Vector Map Information..."))) { OGRDataset->DestroyDataSource(OGRDataset); return false; } } GLdouble minX, minY, maxX, maxY; minX = minY = std::numeric_limits<float>::max(); maxX = maxY = -std::numeric_limits<float>::max(); // Retrieve features from the dataset OGRFeature *poFeature; poLayer->ResetReading(); int numFeatures = poLayer->GetFeatureCount(); int count=0; //Log::Inst().Write("Loading shapefile with the following meta data:"); while ( ( poFeature = poLayer->GetNextFeature() ) != NULL ) { ///////////////////////////////////////////////// // PHASE 1: Retrieve METADATA from the dataset // ///////////////////////////////////////////////// OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); //if( poFieldDefn->GetType() == OFTInteger ) // printf( "%d,", poFeature->GetFieldAsInteger( iField ) ); //else if( poFieldDefn->GetType() == OFTReal ) // printf( "%.3f,", poFeature->GetFieldAsDouble(iField) ); //else if( poFieldDefn->GetType() == OFTString ) // printf( "%s,", poFeature->GetFieldAsString(iField) ); //else // printf( "%s,", poFeature->GetFieldAsString(iField) ); //ofs << poFeature->GetFieldAsString(iField) << ","; std::string metaData = poFeature->GetFieldAsString(iField); // do something with the meta data... //Log::Inst().Write(metaData); } count++; if(progressDlg) { if(!progressDlg->Update(int(50 + (float(count)/numFeatures)*50))) return false; } /////////////////////////////////////////////////// // PHASE 2: Retrieve GEOMETRIES from the dataset // /////////////////////////////////////////////////// OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); // Move to the next feature in the set if no geometry present if( poGeometry == NULL ) { OGRFeature::DestroyFeature( poFeature ); continue; } OGRwkbGeometryType whatisit = poGeometry->getGeometryType(); // Handle POINTS if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbPoint ) { GeoVector* geoVector = new GeoVector(); OGRPoint *poPoint = (OGRPoint *) poGeometry; geoVector->SetGeometryType( wkbPoint ); geoVector->SetNumberOfPoints( 1 ); if(needProjection) { double x,y; x= poPoint->getX(); y= poPoint->getY(); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[0] = x; geoVector->pointY[0] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[0] = poPoint->getX(); geoVector->pointY[0] = poPoint->getY(); } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } //Handle MultiPoint else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbMultiPoint ) { OGRMultiPoint *poMultiPoint = (OGRMultiPoint *) poGeometry; for ( int currGeometry = 0; currGeometry < poMultiPoint->getNumGeometries(); currGeometry++ ) { GeoVector* geoVector = new GeoVector(); OGRPoint *poPoint = ( OGRPoint* )poMultiPoint->getGeometryRef( currGeometry ); geoVector->SetGeometryType( wkbPoint ); geoVector->SetNumberOfPoints( 1 ); if(needProjection) { double x,y; x= poPoint->getX(); y= poPoint->getY(); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[0] = x; geoVector->pointY[0] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[0] = poPoint->getX(); geoVector->pointY[0] = poPoint->getY(); } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } } //Handle Polylines else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbLineString ) { GeoVector* geoVector = new GeoVector(); OGRLineString *poLine = (OGRLineString *) poGeometry; geoVector->SetGeometryType( wkbLineString ); geoVector->SetNumberOfPoints( poLine->getNumPoints() ); // Convert and store the points for ( int currentPoint = 0; currentPoint < poLine->getNumPoints(); currentPoint++ ) { // Convert and store the points if(needProjection) { double x,y; x= poLine->getX( currentPoint ); y= poLine->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLine->getX( currentPoint ); geoVector->pointY[currentPoint] = poLine->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } // Handle MultiPolyLine else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbMultiLineString ) { OGRMultiLineString *poMultiLine = (OGRMultiLineString *) poGeometry; for ( int currGeometry = 0; currGeometry < poMultiLine->getNumGeometries(); currGeometry++ ) { GeoVector* geoVector = new GeoVector(); OGRLineString *poLine = ( OGRLineString* )poMultiLine->getGeometryRef( currGeometry ); geoVector->SetGeometryType( wkbLineString ); geoVector->SetNumberOfPoints( poLine->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLine ->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLine->getX( currentPoint ); y= poLine->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLine->getX( currentPoint ); geoVector->pointY[currentPoint] = poLine->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } } // Handle POLYGONS else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbPolygon ) { GeoVector* geoVector = new GeoVector(); OGRPolygon *poPolygon = ( OGRPolygon* )poGeometry; OGRLinearRing *poLinearRing = poPolygon->getExteriorRing(); geoVector->SetGeometryType( wkbLinearRing ); geoVector->SetNumberOfPoints( poLinearRing->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLinearRing->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLinearRing->getX( currentPoint ); y= poLinearRing->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLinearRing->getX( currentPoint ); geoVector->pointY[currentPoint] = poLinearRing->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } // Handle MULTIPOLYGONS else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbMultiPolygon ) { OGRMultiPolygon *poMultiPolygon = (OGRMultiPolygon *) poGeometry; for ( int currGeometry = 0; currGeometry < poMultiPolygon->getNumGeometries(); currGeometry++ ) { GeoVector* geoVector = new GeoVector(); // OGRPolygon http://www.gdal.org/ogr/classOGRPolygon.html OGRPolygon *poPolygon = ( OGRPolygon* )poMultiPolygon->getGeometryRef( currGeometry ); // Retrieve the EXTERNAL ring of the multipolygon OGRLinearRing *poLinearRing = poPolygon->getExteriorRing(); geoVector->SetGeometryType( wkbLinearRing ); geoVector->SetNumberOfPoints( poLinearRing->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLinearRing->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLinearRing->getX( currentPoint ); y= poLinearRing->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLinearRing->getX( currentPoint ); geoVector->pointY[currentPoint] = poLinearRing->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); // Retrieve all the INTERNAL rings of the multipolygon for ( int currentRing = 0; currentRing < poPolygon->getNumInteriorRings(); currentRing++ ) { GeoVector* geoVector2 = new GeoVector(); poLinearRing = poPolygon->getInteriorRing( currentRing ); geoVector2->SetGeometryType( wkbLinearRing ); geoVector2->SetNumberOfPoints( poLinearRing->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLinearRing->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLinearRing->getX( currentPoint ); y= poLinearRing->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector2->pointX[currentPoint] = x; geoVector2->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector2->pointX[currentPoint] = poLinearRing->getX( currentPoint ); geoVector2->pointY[currentPoint] = poLinearRing->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector2 ); } } } // Report a warning message for unhandled geometries else { Log::Inst().Warning("(Warning) Could not load vector data: unsupported geometry."); } OGRFeature::DestroyFeature( poFeature ); } if (float(minX) == float(maxX) && float(minY) == float(maxY)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } if(needProjection) { vectorMapModel->SetVectorBoundary_MinX(minX); vectorMapModel->SetVectorBoundary_MaxX(maxX); vectorMapModel->SetVectorBoundary_MinY(minY); vectorMapModel->SetVectorBoundary_MaxY(maxY); } if(!SetupVectorScaling(vectorMapModel,progressDlg)) { OGRDataset->DestroyDataSource(OGRDataset); return false; } VectorMetaDataInfo(OGRDataset, studyController, vectorMapController); OGRDataSource::DestroyDataSource( OGRDataset ); return true; }
void PCLoaderArcView::load(const std::string& file, OptionsCont& oc, PCPolyContainer& toFill, PCTypeMap&) { #ifdef HAVE_GDAL GeoConvHelper& geoConvHelper = GeoConvHelper::getProcessing(); // get defaults std::string prefix = oc.getString("prefix"); std::string type = oc.getString("type"); RGBColor color = RGBColor::parseColor(oc.getString("color")); int layer = oc.getInt("layer"); std::string idField = oc.getString("shapefile.id-column"); bool useRunningID = oc.getBool("shapefile.use-running-id"); // start parsing std::string shpName = file + ".shp"; OGRRegisterAll(); OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE); if (poDS == NULL) { throw ProcessError("Could not open shape description '" + shpName + "'."); } // begin file parsing OGRLayer* poLayer = poDS->GetLayer(0); poLayer->ResetReading(); // build coordinate transformation OGRSpatialReference* origTransf = poLayer->GetSpatialRef(); OGRSpatialReference destTransf; // use wgs84 as destination destTransf.SetWellKnownGeogCS("WGS84"); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf); if (poCT == NULL) { if (oc.isSet("shapefile.guess-projection")) { OGRSpatialReference origTransf2; origTransf2.SetWellKnownGeogCS("WGS84"); poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf); } if (poCT == 0) { WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed."); } } OGRFeature* poFeature; poLayer->ResetReading(); unsigned int runningID = 0; while ((poFeature = poLayer->GetNextFeature()) != NULL) { // read in edge attributes std::string id = useRunningID ? toString(runningID) : poFeature->GetFieldAsString(idField.c_str()); ++runningID; id = StringUtils::prune(id); if (id == "") { throw ProcessError("Missing id under '" + idField + "'"); } id = prefix + id; // read in the geometry OGRGeometry* poGeometry = poFeature->GetGeometryRef(); if (poGeometry == 0) { OGRFeature::DestroyFeature(poFeature); continue; } // try transform to wgs84 poGeometry->transform(poCT); OGRwkbGeometryType gtype = poGeometry->getGeometryType(); switch (gtype) { case wkbPoint: { OGRPoint* cgeom = (OGRPoint*) poGeometry; Position pos((SUMOReal) cgeom->getX(), (SUMOReal) cgeom->getY()); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for POI '" + id + "'."); } PointOfInterest* poi = new PointOfInterest(id, type, color, pos, (SUMOReal)layer); if (!toFill.insert(id, poi, layer)) { WRITE_ERROR("POI '" + id + "' could not be added."); delete poi; } } break; case wkbLineString: { OGRLineString* cgeom = (OGRLineString*) poGeometry; PositionVector shape; for (int j = 0; j < cgeom->getNumPoints(); j++) { Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(id, type, color, shape, false, (SUMOReal)layer); if (!toFill.insert(id, poly, layer)) { WRITE_ERROR("Polygon '" + id + "' could not be added."); delete poly; } } break; case wkbPolygon: { OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing(); PositionVector shape; for (int j = 0; j < cgeom->getNumPoints(); j++) { Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(id, type, color, shape, true, (SUMOReal)layer); if (!toFill.insert(id, poly, layer)) { WRITE_ERROR("Polygon '" + id + "' could not be added."); delete poly; } } break; case wkbMultiPoint: { OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry; for (int i = 0; i < cgeom->getNumGeometries(); ++i) { OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i); Position pos((SUMOReal) cgeom2->getX(), (SUMOReal) cgeom2->getY()); std::string tid = id + "#" + toString(i); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for POI '" + tid + "'."); } PointOfInterest* poi = new PointOfInterest(tid, type, color, pos, (SUMOReal)layer); if (!toFill.insert(tid, poi, layer)) { WRITE_ERROR("POI '" + tid + "' could not be added."); delete poi; } } } break; case wkbMultiLineString: { OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry; for (int i = 0; i < cgeom->getNumGeometries(); ++i) { OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i); PositionVector shape; std::string tid = id + "#" + toString(i); for (int j = 0; j < cgeom2->getNumPoints(); j++) { Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(tid, type, color, shape, false, (SUMOReal)layer); if (!toFill.insert(tid, poly, layer)) { WRITE_ERROR("Polygon '" + tid + "' could not be added."); delete poly; } } } break; case wkbMultiPolygon: { OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry; for (int i = 0; i < cgeom->getNumGeometries(); ++i) { OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing(); PositionVector shape; std::string tid = id + "#" + toString(i); for (int j = 0; j < cgeom2->getNumPoints(); j++) { Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(tid, type, color, shape, true, (SUMOReal)layer); if (!toFill.insert(tid, poly, layer)) { WRITE_ERROR("Polygon '" + tid + "' could not be added."); delete poly; } } } break; default: WRITE_WARNING("Unsupported shape type occured (id='" + id + "')."); break; } OGRFeature::DestroyFeature(poFeature); } PROGRESS_DONE_MESSAGE(); #else WRITE_ERROR("SUMO was compiled without GDAL support."); #endif }
OGRErr OGROCIWritableLayer::TranslateToSDOGeometry( OGRGeometry * poGeometry, int *pnGType ) { nOrdinalCount = 0; nElemInfoCount = 0; if( poGeometry == NULL ) return OGRERR_FAILURE; /* ==================================================================== */ /* Handle a point geometry. */ /* ==================================================================== */ if( wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ) { #ifdef notdef char szResult[1024]; OGRPoint *poPoint = (OGRPoint *) poGeometry; if( nDimension == 2 ) CPLsprintf( szResult, "%s(%d,%s,MDSYS.SDO_POINT_TYPE(%.16g,%.16g,0.0),NULL,NULL)", SDO_GEOMETRY, 2001, szSRID, poPoint->getX(), poPoint->getY() ); else CPLsprintf( szResult, "%s(%d,%s,MDSYS.SDO_POINT_TYPE(%.16g,%.16g,%.16g),NULL,NULL)", SDO_GEOMETRY, 3001, szSRID, poPoint->getX(), poPoint->getY(), poPoint->getZ() ); return CPLStrdup(szResult ); #endif } /* ==================================================================== */ /* Handle a line string geometry. */ /* ==================================================================== */ else if( wkbFlatten(poGeometry->getGeometryType()) == wkbLineString ) { *pnGType = nDimension * 1000 + 2; TranslateElementGroup( poGeometry ); return OGRERR_NONE; } /* ==================================================================== */ /* Handle a polygon geometry. */ /* ==================================================================== */ else if( wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon ) { *pnGType = nDimension == 2 ? 2003 : 3003; TranslateElementGroup( poGeometry ); return OGRERR_NONE; } /* ==================================================================== */ /* Handle a multi point geometry. */ /* ==================================================================== */ else if( wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPoint ) { OGRMultiPoint *poMP = (OGRMultiPoint *) poGeometry; int iVert; *pnGType = nDimension*1000 + 5; PushElemInfo( 1, 1, poMP->getNumGeometries() ); for( iVert = 0; iVert < poMP->getNumGeometries(); iVert++ ) { OGRPoint *poPoint = (OGRPoint *)poMP->getGeometryRef( iVert ); PushOrdinal( poPoint->getX() ); PushOrdinal( poPoint->getY() ); if( nDimension == 3 ) PushOrdinal( poPoint->getZ() ); } return OGRERR_NONE; } /* ==================================================================== */ /* Handle other geometry collections. */ /* ==================================================================== */ else { /* -------------------------------------------------------------------- */ /* Identify the GType. */ /* -------------------------------------------------------------------- */ if( wkbFlatten(poGeometry->getGeometryType()) == wkbMultiLineString ) *pnGType = nDimension * 1000 + 6; else if( wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon ) *pnGType = nDimension * 1000 + 7; else if( wkbFlatten(poGeometry->getGeometryType()) == wkbGeometryCollection ) *pnGType = nDimension * 1000 + 4; else { CPLError( CE_Failure, CPLE_AppDefined, "Unexpected geometry type (%d/%s) in " "OGROCIWritableLayer::TranslateToSDOGeometry()", poGeometry->getGeometryType(), poGeometry->getGeometryName() ); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Translate each child in turn. */ /* -------------------------------------------------------------------- */ OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeometry; int iChild; for( iChild = 0; iChild < poGC->getNumGeometries(); iChild++ ) TranslateElementGroup( poGC->getGeometryRef(iChild) ); return OGRERR_NONE; } return OGRERR_FAILURE; }
int main() { GDALAllRegister(); //register all the format drivers cout << "GDAL All Registed!" << endl; GDALDataset *poDS; //Data source poDS = (GDALDataset*) GDALOpenEx("./beijing/road/Nbeijing_point.shp", GDAL_OF_VECTOR, NULL, NULL, NULL); if(poDS == NULL) { cout << "Open shp file failed!" << endl; exit(1); } cout << "Data source open success!" << endl; int layerNum = poDS->GetLayerCount(); //a dataset may have many layers cout << "Layer number:" << layerNum << endl; OGRLayer *poLayer; poLayer = poDS->GetLayerByName("Nbeijing_point"); //feature is a geometry and a set of attributes OGRFeature *poFeature; poLayer->ResetReading(); //Start at the beginning of the layer cout << "Feature number:" << poLayer->GetFeatureCount() << endl; stringstream ss; map<string, pair<double, double> > mip; map<string, pair<double, double> >::iterator imip; ofstream ofile("beijingNode"); string id; int crossFlag; string stmp, stmp2; vector<string> vs; vector<string>::iterator ivs; while((poFeature = poLayer->GetNextFeature()) != NULL) { //Defn contains the definition of all the fields OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); // for(iField = 0; iField < poFDefn->GetFieldCount(); iField++) // { // OGRFieldDefn * poFieldDefn = poFDefn->GetFieldDefn(iField); id = poFeature->GetFieldAsString(1); ofile << id; crossFlag = poFeature->GetFieldAsInteger(4); if(crossFlag == 0) { ofile << "\t" << crossFlag << "\t" << 0 << "\t" << 0 << "\t" << 0; } else if(crossFlag == 1) { ofile << "\t" << crossFlag << "\t" << 0 << "\t" << poFeature->GetFieldAsInteger(7) << "\t" << 0; } else if(crossFlag == 2) { stmp = poFeature->GetFieldAsString(6); vs = split(stmp, "|"); ofile << "\t" << crossFlag << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); ofile << "\t" << poFeature->GetFieldAsInteger(7) << "\t" << 0; } else if(crossFlag == 3) { stmp = poFeature->GetFieldAsString(6); vs = split(stmp, "|"); ofile << "\t" << crossFlag << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); ofile << "\t" << poFeature->GetFieldAsInteger(7); stmp = poFeature->GetFieldAsString(8); stmp2 = poFeature->GetFieldAsString(9); if(stmp2 != "") stmp += "|" + stmp2; vs = split(stmp, "|"); ofile << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); } ofile << "\t" << poFeature->GetFieldAsString(11); stmp = poFeature->GetFieldAsString(12); vs = split(stmp, "|"); ofile << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); /* if(poFieldDefn->GetType() == OFTInteger) cout << poFeature->GetFieldAsInteger(iField) << ", "; else if(poFieldDefn->GetType() == OFTInteger64) cout << poFeature->GetFieldAsInteger64(iField) << ", "; else if(poFieldDefn->GetType() == OFTReal) cout << setprecision(15) << poFeature->GetFieldAsDouble(iField) << ", "; else if(poFieldDefn->GetType() == OFTString) cout << poFeature->GetFieldAsString(iField) << ", "; else cout << poFeature->GetFieldAsString(iField) << ", ";*/ // } OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); // cout << "Geometry Type:" << poGeometry->getGeometryType() << endl; if(poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPoint) { OGRMultiPoint *poMultiPoint = (OGRMultiPoint*)poGeometry; // cout << "Number in the MultiPoint:" << poMultiPoint->getNumGeometries() << endl; OGRGeometry *pG; pG = poMultiPoint->getGeometryRef(0); OGRPoint *pP = (OGRPoint*)pG; ofile << "\t" << pP->getY() << "\t" << pP->getX(); // pP->flattenTo2D(); // cout << setprecision(15) << pP->getY() << ", " << pP->getX() << endl; // mip[id] = make_pair(pP->getY(), pP->getX()); } else cout << "No Point Geometry" << endl; ofile << endl; OGRFeature::DestroyFeature(poFeature); } /* cout << "Writing nodes" << endl; for(imip = mip.begin(); imip != mip.end(); imip++) ofile << setprecision(15) << (*imip).first << "\t" << (*imip).second.first << "\t" << (*imip).second.second << endl;*/ GDALClose(poFeature); ofile.close(); return 0; }
int S57Writer::WritePrimitive( OGRFeature *poFeature ) { DDFRecord *poRec = MakeRecord(); DDFField *poField; OGRGeometry *poGeom = poFeature->GetGeometryRef(); /* -------------------------------------------------------------------- */ /* Add the VRID field. */ /* -------------------------------------------------------------------- */ poField = poRec->AddField( poModule->FindFieldDefn( "VRID" ) ); poRec->SetIntSubfield ( "VRID", 0, "RCNM", 0, poFeature->GetFieldAsInteger( "RCNM") ); poRec->SetIntSubfield ( "VRID", 0, "RCID", 0, poFeature->GetFieldAsInteger( "RCID") ); poRec->SetIntSubfield ( "VRID", 0, "RVER", 0, 1 ); poRec->SetIntSubfield ( "VRID", 0, "RUIN", 0, 1 ); /* -------------------------------------------------------------------- */ /* Handle simple point. */ /* -------------------------------------------------------------------- */ if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbPoint ) { double dfX, dfY, dfZ; OGRPoint *poPoint = (OGRPoint *) poGeom; CPLAssert( poFeature->GetFieldAsInteger( "RCNM") == RCNM_VI || poFeature->GetFieldAsInteger( "RCNM") == RCNM_VC ); dfX = poPoint->getX(); dfY = poPoint->getY(); dfZ = poPoint->getZ(); if( dfZ == 0.0 ) WriteGeometry( poRec, 1, &dfX, &dfY, NULL ); else WriteGeometry( poRec, 1, &dfX, &dfY, &dfZ ); } /* -------------------------------------------------------------------- */ /* For multipoints we assuming SOUNDG, and write out as SG3D. */ /* -------------------------------------------------------------------- */ else if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPoint ) { OGRMultiPoint *poMP = (OGRMultiPoint *) poGeom; int i, nVCount = poMP->getNumGeometries(); double *padfX, *padfY, *padfZ; CPLAssert( poFeature->GetFieldAsInteger( "RCNM") == RCNM_VI || poFeature->GetFieldAsInteger( "RCNM") == RCNM_VC ); padfX = (double *) CPLMalloc(sizeof(double) * nVCount); padfY = (double *) CPLMalloc(sizeof(double) * nVCount); padfZ = (double *) CPLMalloc(sizeof(double) * nVCount); for( i = 0; i < nVCount; i++ ) { OGRPoint *poPoint = (OGRPoint *) poMP->getGeometryRef( i ); padfX[i] = poPoint->getX(); padfY[i] = poPoint->getY(); padfZ[i] = poPoint->getZ(); } WriteGeometry( poRec, nVCount, padfX, padfY, padfZ ); CPLFree( padfX ); CPLFree( padfY ); CPLFree( padfZ ); } /* -------------------------------------------------------------------- */ /* Handle LINESTRINGs (edge) geometry. */ /* -------------------------------------------------------------------- */ else if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbLineString ) { OGRLineString *poLS = (OGRLineString *) poGeom; int i, nVCount = poLS->getNumPoints(); double *padfX, *padfY; CPLAssert( poFeature->GetFieldAsInteger( "RCNM") == RCNM_VE ); padfX = (double *) CPLMalloc(sizeof(double) * nVCount); padfY = (double *) CPLMalloc(sizeof(double) * nVCount); for( i = 0; i < nVCount; i++ ) { padfX[i] = poLS->getX(i); padfY[i] = poLS->getY(i); } WriteGeometry( poRec, nVCount, padfX, padfY, NULL ); CPLFree( padfX ); CPLFree( padfY ); } /* -------------------------------------------------------------------- */ /* edge node linkages. */ /* -------------------------------------------------------------------- */ if( poFeature->GetDefnRef()->GetFieldIndex( "NAME_RCNM_0" ) >= 0 ) { DDFField *poField; char szName[5]; int nRCID; CPLAssert( poFeature->GetFieldAsInteger( "NAME_RCNM_0") == RCNM_VC ); poField = poRec->AddField( poModule->FindFieldDefn( "VRPT" ) ); nRCID = poFeature->GetFieldAsInteger( "NAME_RCID_0"); szName[0] = RCNM_VC; szName[1] = nRCID & 0xff; szName[2] = (nRCID & 0xff00) >> 8; szName[3] = (nRCID & 0xff0000) >> 16; szName[4] = (nRCID & 0xff000000) >> 24; poRec->SetStringSubfield( "VRPT", 0, "NAME", 0, szName, 5 ); poRec->SetIntSubfield ( "VRPT", 0, "ORNT", 0, poFeature->GetFieldAsInteger( "ORNT_0") ); poRec->SetIntSubfield ( "VRPT", 0, "USAG", 0, poFeature->GetFieldAsInteger( "USAG_0") ); poRec->SetIntSubfield ( "VRPT", 0, "TOPI", 0, poFeature->GetFieldAsInteger( "TOPI_0") ); poRec->SetIntSubfield ( "VRPT", 0, "MASK", 0, poFeature->GetFieldAsInteger( "MASK_0") ); nRCID = poFeature->GetFieldAsInteger( "NAME_RCID_1"); szName[0] = RCNM_VC; szName[1] = nRCID & 0xff; szName[2] = (nRCID & 0xff00) >> 8; szName[3] = (nRCID & 0xff0000) >> 16; szName[4] = (nRCID & 0xff000000) >> 24; poRec->SetStringSubfield( "VRPT", 0, "NAME", 1, szName, 5 ); poRec->SetIntSubfield ( "VRPT", 0, "ORNT", 1, poFeature->GetFieldAsInteger( "ORNT_1") ); poRec->SetIntSubfield ( "VRPT", 0, "USAG", 1, poFeature->GetFieldAsInteger( "USAG_1") ); poRec->SetIntSubfield ( "VRPT", 0, "TOPI", 1, poFeature->GetFieldAsInteger( "TOPI_1") ); poRec->SetIntSubfield ( "VRPT", 0, "MASK", 1, poFeature->GetFieldAsInteger( "MASK_1") ); }