bool GDBGeometryToOGRGeometry(bool forceMulti, FileGDBAPI::ShapeBuffer* pGdbGeometry, OGRSpatialReference* pOGRSR, OGRGeometry** ppOutGeometry) { OGRGeometry* pOGRGeometry = NULL; OGRErr eErr = OGRCreateFromShapeBin( pGdbGeometry->shapeBuffer, &pOGRGeometry, pGdbGeometry->inUseLength); //OGRErr eErr = OGRGeometryFactory::createFromWkb(pGdbGeometry->shapeBuffer, pOGRSR, &pOGRGeometry, pGdbGeometry->inUseLength ); if (eErr != OGRERR_NONE) { CPLError( CE_Failure, CPLE_AppDefined, "Failed attempting to import GDB WKB Geometry. OGRGeometryFactory err:%d", eErr); return false; } if( pOGRGeometry != NULL ) { // force geometries to multi if requested // If it is a polygon, force to MultiPolygon since we always produce multipolygons if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbPolygon) { pOGRGeometry = OGRGeometryFactory::forceToMultiPolygon(pOGRGeometry); } else if (forceMulti) { if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbLineString) { pOGRGeometry = OGRGeometryFactory::forceToMultiLineString(pOGRGeometry); } else if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbPoint) { pOGRGeometry = OGRGeometryFactory::forceToMultiPoint(pOGRGeometry); } } if (pOGRGeometry) pOGRGeometry->assignSpatialReference( pOGRSR ); } *ppOutGeometry = pOGRGeometry; return true; }
bool GDBGeometryToOGRGeometry(bool forceMulti, FileGDBAPI::ShapeBuffer* pGdbGeometry, OGRSpatialReference* pOGRSR, OGRGeometry** ppOutGeometry) { OGRGeometry* pOGRGeometry = nullptr; OGRErr eErr = OGRCreateFromShapeBin( pGdbGeometry->shapeBuffer, &pOGRGeometry, static_cast<int>(pGdbGeometry->inUseLength)); //OGRErr eErr = OGRGeometryFactory::createFromWkb(pGdbGeometry->shapeBuffer, pOGRSR, &pOGRGeometry, pGdbGeometry->inUseLength ); if (eErr != OGRERR_NONE) { CPLError( CE_Failure, CPLE_AppDefined, "Failed attempting to import GDB WKB Geometry. OGRGeometryFactory err:%d", eErr); return false; } if( pOGRGeometry != nullptr ) { // force geometries to multi if requested // If it is a polygon, force to MultiPolygon since we always produce multipolygons OGRwkbGeometryType eFlattenType = wkbFlatten(pOGRGeometry->getGeometryType()); if (eFlattenType == wkbPolygon) { pOGRGeometry = OGRGeometryFactory::forceToMultiPolygon(pOGRGeometry); } else if (eFlattenType == wkbCurvePolygon) { OGRMultiSurface* poMS = new OGRMultiSurface(); poMS->addGeometryDirectly( pOGRGeometry ); pOGRGeometry = poMS; } else if (forceMulti) { if (eFlattenType == wkbLineString) { pOGRGeometry = OGRGeometryFactory::forceToMultiLineString(pOGRGeometry); } else if (eFlattenType == wkbCompoundCurve) { OGRMultiCurve* poMC = new OGRMultiCurve(); poMC->addGeometryDirectly( pOGRGeometry ); pOGRGeometry = poMC; } else if (eFlattenType == wkbPoint) { pOGRGeometry = OGRGeometryFactory::forceToMultiPoint(pOGRGeometry); } } if (pOGRGeometry) pOGRGeometry->assignSpatialReference( pOGRSR ); } *ppOutGeometry = pOGRGeometry; return true; }
/** @brief Extract bounding box from shape * * get bounding box as OGREnvelope from polygon shape in OGRFeature */ OGREnvelope* getBBoxOfShape(OGRFeature* poFeature) { OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); cout <<"extracting shape of " << poFeature->GetFieldAsString(1) << endl; if (poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon) { OGRPolygon *poPoly = (OGRPolygon *) poGeometry; OGREnvelope *poEnv = new OGREnvelope(); poPoly->getEnvelope(poEnv); return poEnv; // ofstream outfile ("outfile.kml"); // double s = poEnv->MaxY; // double n = poEnv->MinY; // double e = poEnv->MaxX; // double w = poEnv->MinX; // // cout<< s << ", " << n << " to " << e << ", " << w << endl; // outfile.close(); // cout << poPoint->getX() <<", "<<poPoint->getY()<<endl; } else { cout << "No point Geometry"<<endl; return NULL; } }
osg::Geometry* multiPointToDrawable(OGRMultiPoint* mpoint) const { osg::Geometry* geom = new osg::Geometry; osg::Geometry* pointGeom = new osg::Geometry(); osg::Vec3Array* vertices = new osg::Vec3Array(); vertices->reserve(mpoint->getNumGeometries()); for (int i = 0; i < mpoint->getNumGeometries(); i++ ) { OGRGeometry* ogrGeom = mpoint->getGeometryRef(i); OGRwkbGeometryType ogrGeomType = ogrGeom->getGeometryType(); if (wkbPoint != ogrGeomType && wkbPoint25D != ogrGeomType) continue; // skip OGRPoint* points = static_cast<OGRPoint*>(ogrGeom); vertices->push_back(osg::Vec3(points->getX(), points->getY(), points->getZ())); } pointGeom->setVertexArray(vertices); pointGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size())); if (pointGeom->getVertexArray()) { OSG_INFO << "osgOgrFeature::multiPointToDrawable " << geom->getVertexArray()->getNumElements() << " vertexes"<< std::endl; } return pointGeom; }
void OGRGeoJSONLayer::DetectGeometryType() { if (poFeatureDefn_->GetGeomType() != wkbUnknown) return; OGRwkbGeometryType featType = wkbUnknown; OGRGeometry* poGeometry = NULL; FeaturesSeq::const_iterator it = seqFeatures_.begin(); FeaturesSeq::const_iterator end = seqFeatures_.end(); if( it != end ) { poGeometry = (*it)->GetGeometryRef(); if( NULL != poGeometry ) { featType = poGeometry->getGeometryType(); if( featType != poFeatureDefn_->GetGeomType() ) { poFeatureDefn_->SetGeomType( featType ); } } ++it; } while( it != end ) { poGeometry = (*it)->GetGeometryRef(); if( NULL != poGeometry ) { featType = poGeometry->getGeometryType(); if( featType != poFeatureDefn_->GetGeomType() ) { CPLDebug( "GeoJSON", "Detected layer of mixed-geometry type features." ); poFeatureDefn_->SetGeomType( DefaultGeometryType ); break; } } ++it; } }
osg::Geometry* multiPolygonToDrawable(OGRMultiPolygon* mpolygon) const { osg::Geometry* geom = new osg::Geometry; for (int i = 0; i < mpolygon->getNumGeometries(); i++ ) { OGRGeometry* ogrGeom = mpolygon->getGeometryRef(i); OGRwkbGeometryType ogrGeomType = ogrGeom->getGeometryType(); if (wkbPolygon != ogrGeomType && wkbPolygon25D != ogrGeomType) continue; // skip OGRPolygon* polygon = static_cast<OGRPolygon*>(ogrGeom); osg::ref_ptr<osg::Drawable> drw = polygonToDrawable(polygon); osg::ref_ptr<osg::Geometry> geometry = drw->asGeometry(); if (geometry.valid() && geometry->getVertexArray() && geometry->getVertexArray()->getNumElements() && geometry->getNumPrimitiveSets() && geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType ) { if (!geom->getVertexArray()) { // no yet data we put the first in geom->setVertexArray(geometry->getVertexArray()); geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); } else { // already a polygon then append int size = geom->getVertexArray()->getNumElements(); osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); osg::ref_ptr<osg::Vec3Array> triangulized = triangulizeGeometry(geometry.get()); if (triangulized.valid()) { arrayDst->insert(arrayDst->end(), triangulized->begin(), triangulized->end()); // shift index geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, size, triangulized->size())); } } } else { OSG_WARN << "Warning something wrong with a polygon in a multi polygon" << std::endl; } } if (geom->getVertexArray()) { OSG_INFO << "osgOgrFeature::multiPolygonToDrawable " << geom->getVertexArray()->getNumElements() << " vertexes"<< std::endl; } return geom; }
void GDALMergeFaces::run() { OGRGeometry * geo; leadingView.resetReading(); OGRFeature * f; std::set<int> indizes; while (f = leadingView.getNextFeature()) { indizes.insert(f->GetFieldAsInteger(this->attriubteName.c_str())); } //int cluster_id = 1; int counter = 1; foreach (int cluster_id, indizes) { //while(geo = joinCluster(cluster_id)) { //cluster_id++; geo = joinCluster(cluster_id); if (counter % 100 == 0) { DM::Logger(DM::Standard) << "merged " << counter << "/" << indizes.size(); } counter++; if (!geo) continue; if (wkbMultiPolygon == geo->getGeometryType()){ geo = geo->UnionCascaded(); OGRMultiPolygon * mgeo = (OGRMultiPolygon*) geo; if (mgeo->getNumGeometries() == 0) { continue; } geo = mgeo->getGeometryRef(0); int n = mgeo->getNumGeometries(); for (int i = 0; i < n; i++) { OGRFeature * f = combinedView.createFeature(); f->SetGeometry(mgeo->getGeometryRef(i)); f->SetField("test_id", counter); } continue; } OGRFeature * f = combinedView.createFeature(); f->SetGeometry(geo); }
int OGRPolygon::PointOnSurface( OGRPoint *poPoint ) const { if( poPoint == NULL ) return OGRERR_FAILURE; #ifndef HAVE_GEOS return OGRERR_FAILURE; #else GEOSGeom hThisGeosGeom = NULL; GEOSGeom hOtherGeosGeom = NULL; hThisGeosGeom = exportToGEOS(); if( hThisGeosGeom != NULL ) { hOtherGeosGeom = GEOSPointOnSurface( hThisGeosGeom ); GEOSGeom_destroy( hThisGeosGeom ); if( hOtherGeosGeom == NULL ) return OGRERR_FAILURE; OGRGeometry *poInsidePointGeom = (OGRGeometry *) OGRGeometryFactory::createFromGEOS( hOtherGeosGeom ); GEOSGeom_destroy( hOtherGeosGeom ); if (poInsidePointGeom == NULL) return OGRERR_FAILURE; if (wkbFlatten(poInsidePointGeom->getGeometryType()) != wkbPoint) { delete poInsidePointGeom; return OGRERR_FAILURE; } OGRPoint *poInsidePoint = (OGRPoint *) poInsidePointGeom; poPoint->setX( poInsidePoint->getX() ); poPoint->setY( poInsidePoint->getY() ); delete poInsidePointGeom; return OGRERR_NONE; } else { return OGRERR_FAILURE; } #endif /* HAVE_GEOS */ }
double OGRGeometryCollection::get_Length() const { double dfLength = 0.0; for( int iGeom = 0; iGeom < nGeomCount; iGeom++ ) { OGRGeometry* geom = papoGeoms[iGeom]; OGRwkbGeometryType eType = wkbFlatten(geom->getGeometryType()); if( OGR_GT_IsCurve(eType) ) { dfLength += ((OGRCurve *) geom)->get_Length(); } else if( OGR_GT_IsSubClassOf(eType, wkbMultiCurve) || eType == wkbGeometryCollection ) { dfLength += ((OGRGeometryCollection *) geom)->get_Length(); } } return dfLength; }
/** * 複数のポリゴンから成るオブジェクトを読み込む。 * 今の実装は、正確には対応していない。オブジェクトのpartsに順次格納していくだけ。 * そのため、アプリケーション側でparts[0]しか使わない場合、まずい。 */ void Shape::readMultiPolygon(OGRMultiPolygon* poMultiPolygon, ShapeObject& shapeObject) { int numGeometries = poMultiPolygon->getNumGeometries(); int partsIndex = 0; for (int i = 0; i < numGeometries; ++i) { OGRGeometry* geo = poMultiPolygon->getGeometryRef(i); if (wkbFlatten(geo->getGeometryType()) == wkbPolygon) { OGRPolygon* poPolygon = (OGRPolygon*)geo; int nInteriorRings = poPolygon->getNumInteriorRings(); shapeObject.parts.resize(shapeObject.parts.size() + nInteriorRings + 1); OGRLinearRing* ring = poPolygon->getExteriorRing(); readRing(ring, shapeObject.parts[partsIndex++]); for (int j = 0; j < nInteriorRings; ++j) { OGRLinearRing* ring = poPolygon->getInteriorRing(j); readRing(ring, shapeObject.parts[partsIndex++]); } } } }
osg::Geometry* multiLineStringToDrawable(OGRMultiLineString* mlineString) const { osg::Geometry* geom = new osg::Geometry; for (int i = 0; i < mlineString->getNumGeometries(); i++ ) { OGRGeometry* ogrGeom = mlineString->getGeometryRef(i); OGRwkbGeometryType ogrGeomType = ogrGeom->getGeometryType(); if (wkbLineString != ogrGeomType && wkbLineString25D != ogrGeomType) continue; // skip OGRLineString* lineString = static_cast<OGRLineString*>(ogrGeom); osg::ref_ptr<osg::Geometry> geometry = lineStringToDrawable(lineString); if (geometry.valid() && geometry->getVertexArray() && geometry->getNumPrimitiveSets() && geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType) { if (!geom->getVertexArray()) { geom->setVertexArray(geometry->getVertexArray()); geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); } else { int size = geom->getVertexArray()->getNumElements(); osg::Vec3Array* arraySrc = static_cast<osg::Vec3Array*>(geometry->getVertexArray()); osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); arrayDst->insert(arrayDst->end(), arraySrc->begin(), arraySrc->end()); // shift index geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, size, arraySrc->size())); } } } return geom; }
double OGRGeometryCollection::get_Area() const { double dfArea = 0.0; for( int iGeom = 0; iGeom < nGeomCount; iGeom++ ) { OGRGeometry* geom = papoGeoms[iGeom]; switch( wkbFlatten(geom->getGeometryType()) ) { case wkbPolygon: dfArea += ((OGRPolygon *) geom)->get_Area(); break; case wkbMultiPolygon: dfArea += ((OGRMultiPolygon *) geom)->get_Area(); break; case wkbLinearRing: case wkbLineString: /* This test below is required to filter out wkbLineString geometries * not being of type of wkbLinearRing. */ if( EQUAL( ((OGRGeometry*) geom)->getGeometryName(), "LINEARRING" ) ) { dfArea += ((OGRLinearRing *) geom)->get_Area(); } break; case wkbGeometryCollection: dfArea +=((OGRGeometryCollection *) geom)->get_Area(); break; default: break; } } return dfArea; }
void line2end_points(std::vector<unsigned char> &wkb, OGRPoint *firstlast){ OGRGeometryFactory gf; OGRGeometry *gg; int ret = gf.createFromWkb( (unsigned char *)&wkb[0], NULL, &gg); if (ret != OGRERR_NONE ){ std::cout << "Invalid geometry"; return; } if (gg->getGeometryType() != wkbLineString ){ OGRGeometryFactory::destroyGeometry(gg); std::cout << "Geometry must be a LINESTRING" << std::endl; return; } OGRLineString *ls = (OGRLineString *)gg; ls->StartPoint(firstlast); ls->EndPoint(firstlast+1); //free allocated memory OGRGeometryFactory::destroyGeometry(gg); }
double OGRGeometryCollection::get_Length() const { double dfLength = 0.0; for( int iGeom = 0; iGeom < nGeomCount; iGeom++ ) { OGRGeometry* geom = papoGeoms[iGeom]; switch( wkbFlatten(geom->getGeometryType()) ) { case wkbLinearRing: case wkbLineString: dfLength += ((OGRCurve *) geom)->get_Length(); break; case wkbGeometryCollection: dfLength +=((OGRGeometryCollection *) geom)->get_Length(); break; default: break; } } return dfLength; }
OGRErr OGRPCIDSKLayer::ISetFeature( OGRFeature *poFeature ) { PCIDSK::ShapeId id = (PCIDSK::ShapeId) poFeature->GetFID(); /* -------------------------------------------------------------------- */ /* Translate attribute fields. */ /* -------------------------------------------------------------------- */ try { int iPCI; std::vector<PCIDSK::ShapeField> aoPCIFields; aoPCIFields.resize(poVecSeg->GetFieldCount()); for( iPCI = 0; iPCI < poVecSeg->GetFieldCount(); iPCI++ ) { int iOGR; iOGR = poFeatureDefn->GetFieldIndex( poVecSeg->GetFieldName(iPCI).c_str() ); if( iOGR == -1 ) continue; switch( poVecSeg->GetFieldType(iPCI) ) { case PCIDSK::FieldTypeInteger: aoPCIFields[iPCI].SetValue( poFeature->GetFieldAsInteger( iOGR ) ); break; case PCIDSK::FieldTypeFloat: aoPCIFields[iPCI].SetValue( (float) poFeature->GetFieldAsDouble( iOGR ) ); break; case PCIDSK::FieldTypeDouble: aoPCIFields[iPCI].SetValue( (double) poFeature->GetFieldAsDouble( iOGR ) ); break; case PCIDSK::FieldTypeString: aoPCIFields[iPCI].SetValue( poFeature->GetFieldAsString( iOGR ) ); break; case PCIDSK::FieldTypeCountedInt: { int nCount; const int *panList = poFeature->GetFieldAsIntegerList( iOGR, &nCount ); std::vector<PCIDSK::int32> anList; anList.resize( nCount ); memcpy( &(anList[0]), panList, 4 * anList.size() ); aoPCIFields[iPCI].SetValue( anList ); } break; default: CPLAssert( FALSE ); break; } } if( poVecSeg->GetFieldCount() > 0 ) poVecSeg->SetFields( id, aoPCIFields ); /* -------------------------------------------------------------------- */ /* Translate the geometry. */ /* -------------------------------------------------------------------- */ std::vector<PCIDSK::ShapeVertex> aoVertices; OGRGeometry *poGeometry = poFeature->GetGeometryRef(); if( poGeometry == NULL ) { } else if( wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ) { OGRPoint *poPoint = (OGRPoint *) poGeometry; aoVertices.resize(1); aoVertices[0].x = poPoint->getX(); aoVertices[0].y = poPoint->getY(); aoVertices[0].z = poPoint->getZ(); } else if( wkbFlatten(poGeometry->getGeometryType()) == wkbLineString ) { OGRLineString *poLS = (OGRLineString *) poGeometry; unsigned int i; aoVertices.resize(poLS->getNumPoints()); for( i = 0; i < aoVertices.size(); i++ ) { aoVertices[i].x = poLS->getX(i); aoVertices[i].y = poLS->getY(i); aoVertices[i].z = poLS->getZ(i); } } else { CPLDebug( "PCIDSK", "Unsupported geometry type in SetFeature(): %s", poGeometry->getGeometryName() ); } poVecSeg->SetVertices( id, aoVertices ); } /* try */ /* -------------------------------------------------------------------- */ /* Trap exceptions and report as CPL errors. */ /* -------------------------------------------------------------------- */ catch( PCIDSK::PCIDSKException ex ) { CPLError( CE_Failure, CPLE_AppDefined, "%s", ex.what() ); return OGRERR_FAILURE; } catch(...) { CPLError( CE_Failure, CPLE_AppDefined, "Non-PCIDSK exception trapped." ); return OGRERR_FAILURE; } return OGRERR_NONE; }
static void OGR2SQLITE_ogr_geocode_reverse(sqlite3_context* pContext, int argc, sqlite3_value** argv) { OGRSQLiteExtensionData* poModule = (OGRSQLiteExtensionData*) sqlite3_user_data(pContext); double dfLon = 0.0, dfLat = 0.0; int iAfterGeomIdx = 0; int bGotLon = FALSE, bGotLat = FALSE; if( argc >= 2 ) { dfLon = OGR2SQLITE_GetValAsDouble(argv[0], &bGotLon); dfLat = OGR2SQLITE_GetValAsDouble(argv[1], &bGotLat); } if( argc >= 3 && bGotLon && bGotLat && sqlite3_value_type (argv[2]) == SQLITE_TEXT ) { iAfterGeomIdx = 2; } else if( argc >= 2 && sqlite3_value_type (argv[0]) == SQLITE_BLOB && sqlite3_value_type (argv[1]) == SQLITE_TEXT ) { OGRGeometry* poGeom = OGR2SQLITE_GetGeom(pContext, argc, argv, NULL); if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbPoint ) { OGRPoint* poPoint = (OGRPoint*) poGeom; dfLon = poPoint->getX(); dfLat = poPoint->getY(); delete poGeom; } else { delete poGeom; sqlite3_result_null (pContext); return; } iAfterGeomIdx = 1; } else { sqlite3_result_null (pContext); return; } const char* pszField = (const char*)sqlite3_value_text(argv[iAfterGeomIdx]); int i; char** papszOptions = NULL; for(i = iAfterGeomIdx + 1; i < argc; i++) { if( sqlite3_value_type (argv[i]) == SQLITE_TEXT ) { papszOptions = CSLAddString(papszOptions, (const char*)sqlite3_value_text(argv[i])); } } OGRGeocodingSessionH hSession = poModule->GetGeocodingSession(); if( hSession == NULL ) { hSession = OGRGeocodeCreateSession(papszOptions); if( hSession == NULL ) { sqlite3_result_null (pContext); CSLDestroy(papszOptions); return; } poModule->SetGeocodingSession(hSession); } if( strcmp(pszField, "raw") == 0 ) papszOptions = CSLAddString(papszOptions, "RAW_FEATURE=YES"); OGRLayerH hLayer = OGRGeocodeReverse(hSession, dfLon, dfLat, papszOptions); OGR2SQLITE_ogr_geocode_set_result(pContext, hLayer, pszField); CSLDestroy(papszOptions); return; }
OGRErr OGRILI1Layer::CreateFeature( OGRFeature *poFeature ) { static long tid = -1; //system generated TID (must be unique within table) VSIFPrintf( poDS->GetTransferFile(), "OBJE" ); if ( !EQUAL(poFeatureDefn->GetFieldDefn(0)->GetNameRef(), "TID") ) { //Input is not generated from an Interlis 1 source if (poFeature->GetFID() != OGRNullFID) tid = poFeature->GetFID(); else ++tid; VSIFPrintf( poDS->GetTransferFile(), " %ld", tid ); //Embedded geometry if( poFeature->GetGeometryRef() != NULL ) { OGRGeometry *poGeometry = poFeature->GetGeometryRef(); // 2D Point if( poGeometry->getGeometryType() == wkbPoint ) { OGRPoint *poPoint = (OGRPoint *) poGeometry; VSIFPrintf( poDS->GetTransferFile(), " %s", d2str(poPoint->getX()) ); VSIFPrintf( poDS->GetTransferFile(), " %s", d2str(poPoint->getY()) ); } // 3D Point else if( poGeometry->getGeometryType() == wkbPoint25D ) { OGRPoint *poPoint = (OGRPoint *) poGeometry; VSIFPrintf( poDS->GetTransferFile(), " %s", d2str(poPoint->getX()) ); VSIFPrintf( poDS->GetTransferFile(), " %s", d2str(poPoint->getY()) ); VSIFPrintf( poDS->GetTransferFile(), " %s", d2str(poPoint->getZ()) ); } } } // Write all fields. for(int iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ ) { if ( !EQUAL(poFeatureDefn->GetFieldDefn(iField)->GetNameRef(), "ILI_Geometry") ) { if ( poFeature->IsFieldSet( iField ) ) { const char *pszRaw = poFeature->GetFieldAsString( iField ); VSIFPrintf( poDS->GetTransferFile(), " %s", pszRaw ); } else { VSIFPrintf( poDS->GetTransferFile(), " @" ); } } } VSIFPrintf( poDS->GetTransferFile(), "\n" ); // Write out Geometry if( poFeature->GetGeometryRef() != NULL ) { if (EQUAL(poFeatureDefn->GetFieldDefn(poFeatureDefn->GetFieldCount()-1)->GetNameRef(), "ILI_Geometry")) { //Write original ILI geometry VSIFPrintf( poDS->GetTransferFile(), "%s", poFeature->GetFieldAsString( poFeatureDefn->GetFieldCount()-1 ) ); } else { //Convert to ILI geometry GeometryAppend(poFeature->GetGeometryRef()); } } return OGRERR_NONE; }
OGRErr OGRCSVLayer::CreateFeature( OGRFeature *poNewFeature ) { int iField; if( !bInWriteMode ) { CPLError( CE_Failure, CPLE_AppDefined, "The CreateFeature() operation is not permitted on a read-only CSV." ); return OGRERR_FAILURE; } /* If we need rewind, it means that we have just written a feature before */ /* so there's no point seeking to the end of the file, as we're already */ /* at the end */ int bNeedSeekEnd = !bNeedRewindBeforeRead; bNeedRewindBeforeRead = TRUE; /* -------------------------------------------------------------------- */ /* Write field names if we haven't written them yet. */ /* Write .csvt file if needed */ /* -------------------------------------------------------------------- */ if( bNew ) { OGRErr eErr = WriteHeader(); if (eErr != OGRERR_NONE) return eErr; bNeedSeekEnd = FALSE; } if (fpCSV == NULL) return OGRERR_FAILURE; /* -------------------------------------------------------------------- */ /* Make sure we are at the end of the file. */ /* -------------------------------------------------------------------- */ if (bNeedSeekEnd) { if (bFirstFeatureAppendedDuringSession) { /* Add a newline character to the end of the file if necessary */ bFirstFeatureAppendedDuringSession = FALSE; VSIFSeekL( fpCSV, 0, SEEK_END ); VSIFSeekL( fpCSV, VSIFTellL(fpCSV) - 1, SEEK_SET); char chLast; VSIFReadL( &chLast, 1, 1, fpCSV ); VSIFSeekL( fpCSV, 0, SEEK_END ); if (chLast != '\n') { if( bUseCRLF ) VSIFPutcL( 13, fpCSV ); VSIFPutcL( '\n', fpCSV ); } } else { VSIFSeekL( fpCSV, 0, SEEK_END ); } } /* -------------------------------------------------------------------- */ /* Write out the geometry */ /* -------------------------------------------------------------------- */ if (eGeometryFormat == OGR_CSV_GEOM_AS_WKT) { OGRGeometry *poGeom = poNewFeature->GetGeometryRef(); char* pszWKT = NULL; if (poGeom && poGeom->exportToWkt(&pszWKT) == OGRERR_NONE) { VSIFPrintfL( fpCSV, "\"%s\"", pszWKT); } else { VSIFPrintfL( fpCSV, "\"\""); } CPLFree(pszWKT); if (poFeatureDefn->GetFieldCount() > 0) VSIFPrintfL( fpCSV, "%c", chDelimiter); } else if (eGeometryFormat == OGR_CSV_GEOM_AS_XYZ || eGeometryFormat == OGR_CSV_GEOM_AS_XY || eGeometryFormat == OGR_CSV_GEOM_AS_YX) { OGRGeometry *poGeom = poNewFeature->GetGeometryRef(); if (poGeom && wkbFlatten(poGeom->getGeometryType()) == wkbPoint) { OGRPoint* poPoint = (OGRPoint*) poGeom; char szBuffer[75]; if (eGeometryFormat == OGR_CSV_GEOM_AS_XYZ ) OGRMakeWktCoordinate(szBuffer, poPoint->getX(), poPoint->getY(), poPoint->getZ(), 3); else if (eGeometryFormat == OGR_CSV_GEOM_AS_XY ) OGRMakeWktCoordinate(szBuffer, poPoint->getX(), poPoint->getY(), 0, 2); else OGRMakeWktCoordinate(szBuffer, poPoint->getY(), poPoint->getX(), 0, 2); char* pc = szBuffer; while(*pc != '\0') { if (*pc == ' ') *pc = chDelimiter; pc ++; } VSIFPrintfL( fpCSV, "%s", szBuffer ); } else { VSIFPrintfL( fpCSV, "%c", chDelimiter ); if (eGeometryFormat == OGR_CSV_GEOM_AS_XYZ) VSIFPrintfL( fpCSV, "%c", chDelimiter ); } if (poFeatureDefn->GetFieldCount() > 0) VSIFPrintfL( fpCSV, "%c", chDelimiter ); } /* -------------------------------------------------------------------- */ /* Write out all the field values. */ /* -------------------------------------------------------------------- */ int bNonEmptyLine = FALSE; for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ ) { char *pszEscaped; if( iField > 0 ) VSIFPrintfL( fpCSV, "%c", chDelimiter ); if (poFeatureDefn->GetFieldDefn(iField)->GetType() == OFTReal) { pszEscaped = CPLStrdup(poNewFeature->GetFieldAsString(iField)); /* Use point as decimal separator */ char* pszComma = strchr(pszEscaped, ','); if (pszComma) *pszComma = '.'; } else { pszEscaped = CPLEscapeString( poNewFeature->GetFieldAsString(iField), -1, CPLES_CSV ); } int nLen = (int)strlen(pszEscaped); bNonEmptyLine |= (nLen != 0); VSIFWriteL( pszEscaped, 1, nLen, fpCSV ); CPLFree( pszEscaped ); } if( poFeatureDefn->GetFieldCount() == 1 && !bNonEmptyLine ) VSIFPrintfL( fpCSV, "%c", chDelimiter ); if( bUseCRLF ) VSIFPutcL( 13, fpCSV ); VSIFPutcL( '\n', fpCSV ); return OGRERR_NONE; }
OGRErr OGRBNALayer::CreateFeature( OGRFeature *poFeature ) { int i,j,k,n; OGRGeometry *poGeom = poFeature->GetGeometryRef(); char eol[3]; const char* partialEol = (poDS->GetMultiLine()) ? eol : poDS->GetCoordinateSeparator(); if (poGeom == NULL || poGeom->IsEmpty() ) { CPLError(CE_Failure, CPLE_AppDefined, "OGR BNA driver cannot write features with empty geometries."); return OGRERR_FAILURE; } if (poDS->GetUseCRLF()) { eol[0] = 13; eol[1] = 10; eol[2] = 0; } else { eol[0] = 10; eol[1] = 0; } if ( ! bWriter ) { return OGRERR_FAILURE; } if( poFeature->GetFID() == OGRNullFID ) poFeature->SetFID( nFeatures++ ); VSILFILE* fp = poDS->GetOutputFP(); int nbPairPerLine = poDS->GetNbPairPerLine(); switch( poGeom->getGeometryType() ) { case wkbPoint: case wkbPoint25D: { OGRPoint* point = (OGRPoint*)poGeom; WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "1"); VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, point->getX(), point->getY()); VSIFPrintfL( fp, "%s", eol); break; } case wkbPolygon: case wkbPolygon25D: { OGRPolygon* polygon = (OGRPolygon*)poGeom; OGRLinearRing* ring = polygon->getExteriorRing(); if (ring == NULL) { return OGRERR_FAILURE; } double firstX = ring->getX(0); double firstY = ring->getY(0); int nBNAPoints = ring->getNumPoints(); int is_ellipse = FALSE; /* This code tries to detect an ellipse in a polygon geometry */ /* This will only work presumably on ellipses already read from a BNA file */ /* Mostly a BNA to BNA feature... */ if (poDS->GetEllipsesAsEllipses() && polygon->getNumInteriorRings() == 0 && nBNAPoints == 361) { double oppositeX = ring->getX(180); double oppositeY = ring->getY(180); double quarterX = ring->getX(90); double quarterY = ring->getY(90); double antiquarterX = ring->getX(270); double antiquarterY = ring->getY(270); double center1X = 0.5*(firstX + oppositeX); double center1Y = 0.5*(firstY + oppositeY); double center2X = 0.5*(quarterX + antiquarterX); double center2Y = 0.5*(quarterY + antiquarterY); if (fabs(center1X - center2X) < 1e-5 && fabs(center1Y - center2Y) < 1e-5 && fabs(oppositeY - firstY) < 1e-5 && fabs(quarterX - antiquarterX) < 1e-5) { double major_radius = fabs(firstX - center1X); double minor_radius = fabs(quarterY - center1Y); is_ellipse = TRUE; for(i=0;i<360;i++) { if (!(fabs(center1X + major_radius * cos(i * (M_PI / 180)) - ring->getX(i)) < 1e-5 && fabs(center1Y + minor_radius * sin(i * (M_PI / 180)) - ring->getY(i)) < 1e-5)) { is_ellipse = FALSE; break; } } if ( is_ellipse == TRUE ) { WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "2"); VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, center1X, center1Y); VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, major_radius, minor_radius); VSIFPrintfL( fp, "%s", eol); } } } if ( is_ellipse == FALSE) { int nInteriorRings = polygon->getNumInteriorRings(); for(i=0;i<nInteriorRings;i++) { nBNAPoints += polygon->getInteriorRing(i)->getNumPoints() + 1; } if (nBNAPoints <= 3) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid geometry" ); return OGRERR_FAILURE; } WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "%d", nBNAPoints); n = ring->getNumPoints(); int nbPair = 0; for(i=0;i<n;i++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(i), ring->getY(i)); nbPair++; } for(i=0;i<nInteriorRings;i++) { ring = polygon->getInteriorRing(i); n = ring->getNumPoints(); for(j=0;j<n;j++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(j), ring->getY(j)); nbPair++; } VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, firstX, firstY); nbPair++; } VSIFPrintfL( fp, "%s", eol); } break; } case wkbMultiPolygon: case wkbMultiPolygon25D: { OGRMultiPolygon* multipolygon = (OGRMultiPolygon*)poGeom; int N = multipolygon->getNumGeometries(); int nBNAPoints = 0; double firstX = 0, firstY = 0; for(i=0;i<N;i++) { OGRPolygon* polygon = (OGRPolygon*)multipolygon->getGeometryRef(i); OGRLinearRing* ring = polygon->getExteriorRing(); if (ring == NULL) continue; if (nBNAPoints) nBNAPoints ++; else { firstX = ring->getX(0); firstY = ring->getY(0); } nBNAPoints += ring->getNumPoints(); int nInteriorRings = polygon->getNumInteriorRings(); for(j=0;j<nInteriorRings;j++) { nBNAPoints += polygon->getInteriorRing(j)->getNumPoints() + 1; } } if (nBNAPoints <= 3) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid geometry" ); return OGRERR_FAILURE; } WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "%d", nBNAPoints); int nbPair = 0; for(i=0;i<N;i++) { OGRPolygon* polygon = (OGRPolygon*)multipolygon->getGeometryRef(i); OGRLinearRing* ring = polygon->getExteriorRing(); if (ring == NULL) continue; n = ring->getNumPoints(); int nInteriorRings = polygon->getNumInteriorRings(); for(j=0;j<n;j++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(j), ring->getY(j)); nbPair++; } if (i != 0) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, firstX, firstY); nbPair++; } for(j=0;j<nInteriorRings;j++) { ring = polygon->getInteriorRing(j); n = ring->getNumPoints(); for(k=0;k<n;k++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(k), ring->getY(k)); nbPair++; } VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, firstX, firstY); nbPair++; } } VSIFPrintfL( fp, "%s", eol); break; } case wkbLineString: case wkbLineString25D: { OGRLineString* line = (OGRLineString*)poGeom; int n = line->getNumPoints(); int i; if (n < 2) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid geometry" ); return OGRERR_FAILURE; } WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "-%d", n); int nbPair = 0; for(i=0;i<n;i++) { VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, line->getX(i), line->getY(i)); nbPair++; } VSIFPrintfL( fp, "%s", eol); break; } default: { CPLError( CE_Failure, CPLE_AppDefined, "Unsupported geometry type : %s.", poGeom->getGeometryName() ); return OGRERR_UNSUPPORTED_GEOMETRY_TYPE; } } return OGRERR_NONE; }
OGRErr GTMTrackLayer::CreateFeature (OGRFeature *poFeature) { FILE* fpTmpTrackpoints = poDS->getTmpTrackpointsFP(); if (fpTmpTrackpoints == NULL) return CE_Failure; FILE* fpTmpTracks = poDS->getTmpTracksFP(); if (fpTmpTracks == NULL) return CE_Failure; OGRGeometry *poGeom = poFeature->GetGeometryRef(); if ( poGeom == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Features without geometry not supported by GTM writer in track layer." ); return OGRERR_FAILURE; } if (NULL != poCT) { poGeom = poGeom->clone(); poGeom->transform( poCT ); } switch( poGeom->getGeometryType() ) { case wkbLineString: case wkbLineString25D: { WriteFeatureAttributes(poFeature); OGRLineString* line = (OGRLineString*)poGeom; for(int i = 0; i < line->getNumPoints(); ++i) { double lat = line->getY(i); double lon = line->getX(i); float altitude = 0; CheckAndFixCoordinatesValidity(lat, lon); poDS->checkBounds((float)lat, (float)lon); if (line->getGeometryType() == wkbLineString25D) altitude = (float)line->getZ(i); WriteTrackpoint( lat, lon, altitude, i==0 ); } break; } case wkbMultiLineString: case wkbMultiLineString25D: { int nGeometries = ((OGRGeometryCollection*)poGeom)->getNumGeometries (); for(int j = 0; j < nGeometries; ++j) { WriteFeatureAttributes(poFeature); OGRLineString* line = (OGRLineString*) ( ((OGRGeometryCollection*)poGeom)->getGeometryRef(j) ); int n = (line) ? line->getNumPoints() : 0; for(int i = 0; i < n; ++i) { double lat = line->getY(i); double lon = line->getX(i); float altitude = 0; CheckAndFixCoordinatesValidity(lat, lon); if (line->getGeometryType() == wkbLineString25D) altitude = (float) line->getZ(i); WriteTrackpoint( lat, lon, altitude, i==0 ); } } break; } default: { CPLError( CE_Failure, CPLE_NotSupported, "Geometry type of `%s' not supported for 'track' element.\n", OGRGeometryTypeToName(poGeom->getGeometryType()) ); if (NULL != poCT) delete poGeom; return OGRERR_FAILURE; } } if (NULL != poCT) delete poGeom; return OGRERR_NONE; }
OGRFeature* OGRPLScenesLayer::GetNextRawFeature() { if( bEOF || (!bFilterMustBeClientSideEvaluated && nFeatureCount >= 0 && nNextFID > nFeatureCount) ) return NULL; if( poGeoJSONLayer == NULL ) { if( !GetNextPage() ) return NULL; } #ifdef notdef if( CSLTestBoolean(CPLGetConfigOption("OGR_LIMIT_TOO_MANY_FEATURES", "FALSE")) && nFeatureCount > nPageSize ) { bEOF = TRUE; OGRFeature* poFeature = new OGRFeature(poFeatureDefn); OGRGeometry* poGeom; const char* pszWKT = "MULTIPOLYGON(((-180 90,180 90,180 -90,-180 -90,-180 90)))"; OGRGeometryFactory::createFromWkt((char**)&pszWKT, poSRS, &poGeom); poFeature->SetGeometryDirectly(poGeom); return poFeature; } #endif OGRFeature* poGeoJSONFeature = poGeoJSONLayer->GetNextFeature(); if( poGeoJSONFeature == NULL ) { osRequestURL = osNextURL; bStillInFirstPage = FALSE; if( !GetNextPage() ) return NULL; poGeoJSONFeature = poGeoJSONLayer->GetNextFeature(); if( poGeoJSONFeature == NULL ) { bEOF = TRUE; return NULL; } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetFID(nNextFID++); OGRGeometry* poGeom = poGeoJSONFeature->StealGeometry(); if( poGeom != NULL ) { if( poGeom->getGeometryType() == wkbPolygon ) { OGRMultiPolygon* poMP = new OGRMultiPolygon(); poMP->addGeometryDirectly(poGeom); poGeom = poMP; } poGeom->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poGeom); } for(int i=0;i<poFeatureDefn->GetFieldCount();i++) { OGRFieldDefn* poFieldDefn = poFeatureDefn->GetFieldDefn(i); OGRFieldType eType = poFieldDefn->GetType(); int iSrcField = poGeoJSONFeature->GetFieldIndex(poFieldDefn->GetNameRef()); if( iSrcField >= 0 && poGeoJSONFeature->IsFieldSet(iSrcField) ) { if( eType == OFTInteger ) poFeature->SetField(i, poGeoJSONFeature->GetFieldAsInteger(iSrcField)); else if( eType == OFTReal ) poFeature->SetField(i, poGeoJSONFeature->GetFieldAsDouble(iSrcField)); else poFeature->SetField(i, poGeoJSONFeature->GetFieldAsString(iSrcField)); } } delete poGeoJSONFeature; return poFeature; }
OGRGeometry* GML_BuildOGRGeometryFromList(const CPLXMLNode* const * papsGeometry, int bTryToMakeMultipolygons, int bInvertAxisOrderIfLatLong, const char* pszDefaultSRSName, int bConsiderEPSGAsURN, int bGetSecondaryGeometryOption, void* hCacheSRS, int bFaceHoleNegative) { OGRGeometry* poGeom = NULL; int i; OGRGeometryCollection* poCollection = NULL; for(i=0;papsGeometry[i] != NULL;i++) { OGRGeometry* poSubGeom = GML2OGRGeometry_XMLNode( papsGeometry[i], bGetSecondaryGeometryOption, 0, 0, FALSE, TRUE, bFaceHoleNegative ); if (poSubGeom) { if (poGeom == NULL) poGeom = poSubGeom; else { if (poCollection == NULL) { if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbPolygon) { OGRGeometryCollection* poGeomColl = new OGRMultiPolygon(); poGeomColl->addGeometryDirectly(poGeom); poGeomColl->addGeometryDirectly(poSubGeom); poGeom = poGeomColl; } else if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbPolygon) { OGRGeometryCollection* poGeomColl = (OGRGeometryCollection* )poGeom; poGeomColl->addGeometryDirectly(poSubGeom); } else if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbMultiPolygon) { OGRGeometryCollection* poGeomColl = (OGRGeometryCollection* )poGeom; OGRGeometryCollection* poGeomColl2 = (OGRGeometryCollection* )poSubGeom; int nCount = poGeomColl2->getNumGeometries(); int i; for(i=0;i<nCount;i++) { poGeomColl->addGeometry(poGeomColl2->getGeometryRef(i)); } delete poSubGeom; } else if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon) { delete poGeom; delete poSubGeom; return GML_BuildOGRGeometryFromList(papsGeometry, FALSE, bInvertAxisOrderIfLatLong, pszDefaultSRSName, bConsiderEPSGAsURN, bGetSecondaryGeometryOption, hCacheSRS); } else { poCollection = new OGRGeometryCollection(); poCollection->addGeometryDirectly(poGeom); poGeom = poCollection; } } if (poCollection != NULL) { poCollection->addGeometryDirectly(poSubGeom); } } } } if( poGeom == NULL ) return NULL; std::string osWork; const char* pszSRSName = GML_ExtractSrsNameFromGeometry(papsGeometry, osWork, bConsiderEPSGAsURN); const char* pszNameLookup = pszSRSName; if( pszNameLookup == NULL ) pszNameLookup = pszDefaultSRSName; if (pszNameLookup != NULL) { SRSCache* poSRSCache = (SRSCache*)hCacheSRS; SRSDesc& oSRSDesc = poSRSCache->Get(pszNameLookup); poGeom->assignSpatialReference(oSRSDesc.poSRS); if (oSRSDesc.bAxisInvert && bInvertAxisOrderIfLatLong) poGeom->swapXY(); } return poGeom; }
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; }
bool AOGeometryToOGRGeometry(bool forceMulti, esriGeometry::IGeometry* pInAOGeo, OGRSpatialReference* pOGRSR, unsigned char* & pInOutWorkingBuffer, long & inOutBufferSize, OGRGeometry** ppOutGeometry) { HRESULT hr; esriGeometry::IWkbPtr ipWkb = pInAOGeo; long reqSize = 0; if (FAILED(hr = ipWkb->get_WkbSize(&reqSize))) { AOErr(hr, "Error getting Wkb buffer size"); return false; } if (reqSize > inOutBufferSize) { // resize working buffer delete [] pInOutWorkingBuffer; pInOutWorkingBuffer = new unsigned char[reqSize]; inOutBufferSize = reqSize; } if (FAILED(hr = ipWkb->ExportToWkb(&reqSize, pInOutWorkingBuffer))) { AOErr(hr, "Error exporting to WKB buffer"); return false; } OGRGeometry* pOGRGeometry = NULL; OGRErr eErr = OGRGeometryFactory::createFromWkb(pInOutWorkingBuffer, pOGRSR, &pOGRGeometry, reqSize); if (eErr != OGRERR_NONE) { CPLError( CE_Failure, CPLE_AppDefined, "Failed attempting to import ArcGIS WKB Geometry. OGRGeometryFactory err:%d", eErr); return false; } // force geometries to multi if requested // If it is a polygon, force to MultiPolygon since we always produce multipolygons if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbPolygon) { pOGRGeometry = OGRGeometryFactory::forceToMultiPolygon(pOGRGeometry); } else if (forceMulti) { if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbLineString) { pOGRGeometry = OGRGeometryFactory::forceToMultiLineString(pOGRGeometry); } else if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbPoint) { pOGRGeometry = OGRGeometryFactory::forceToMultiPoint(pOGRGeometry); } } *ppOutGeometry = pOGRGeometry; return true; }
OGRErr OGRShapeLayer::CreateFeature( OGRFeature *poFeature ) { OGRErr eErr; if( !bUpdateAccess ) { CPLError( CE_Failure, CPLE_AppDefined, "The CreateFeature() operation is not permitted on a read-only shapefile." ); return OGRERR_FAILURE; } bHeaderDirty = TRUE; if( CheckForQIX() ) DropSpatialIndex(); poFeature->SetFID( OGRNullFID ); if( nTotalShapeCount == 0 && eRequestedGeomType == wkbUnknown && poFeature->GetGeometryRef() != NULL ) { OGRGeometry *poGeom = poFeature->GetGeometryRef(); int nShapeType; switch( poGeom->getGeometryType() ) { case wkbPoint: nShapeType = SHPT_POINT; eRequestedGeomType = wkbPoint; break; case wkbPoint25D: nShapeType = SHPT_POINTZ; eRequestedGeomType = wkbPoint25D; break; case wkbMultiPoint: nShapeType = SHPT_MULTIPOINT; eRequestedGeomType = wkbMultiPoint; break; case wkbMultiPoint25D: nShapeType = SHPT_MULTIPOINTZ; eRequestedGeomType = wkbMultiPoint25D; break; case wkbLineString: case wkbMultiLineString: nShapeType = SHPT_ARC; eRequestedGeomType = wkbLineString; break; case wkbLineString25D: case wkbMultiLineString25D: nShapeType = SHPT_ARCZ; eRequestedGeomType = wkbLineString25D; break; case wkbPolygon: case wkbMultiPolygon: nShapeType = SHPT_POLYGON; eRequestedGeomType = wkbPolygon; break; case wkbPolygon25D: case wkbMultiPolygon25D: nShapeType = SHPT_POLYGONZ; eRequestedGeomType = wkbPolygon25D; break; default: nShapeType = -1; break; } if( nShapeType != -1 ) { ResetGeomType( nShapeType ); } } eErr = SHPWriteOGRFeature( hSHP, hDBF, poFeatureDefn, poFeature ); if( hSHP != NULL ) nTotalShapeCount = hSHP->nRecords; else nTotalShapeCount = hDBF->nRecords; return eErr; }
OGRErr OGRCSVEditableLayerSynchronizer::EditableSyncToDisk(OGRLayer* poEditableLayer, OGRLayer** ppoDecoratedLayer) { CPLAssert( m_poCSVLayer == *ppoDecoratedLayer ); CPLString osLayerName(m_poCSVLayer->GetName()); CPLString osFilename(m_poCSVLayer->GetFilename()); const bool bCreateCSVT = m_poCSVLayer->GetCreateCSVT(); CPLString osCSVTFilename(CPLResetExtension(osFilename, "csvt")); VSIStatBufL sStatBuf; const bool bHasCSVT = VSIStatL(osCSVTFilename, &sStatBuf) == 0; CPLString osTmpFilename(osFilename); CPLString osTmpCSVTFilename(osFilename); if( VSIStatL(osFilename, &sStatBuf) == 0 ) { osTmpFilename += "_ogr_tmp.csv"; osTmpCSVTFilename += "_ogr_tmp.csvt"; } const char chDelimiter = m_poCSVLayer->GetDelimiter(); OGRCSVLayer* poCSVTmpLayer = new OGRCSVLayer( osLayerName, NULL, osTmpFilename, true, true, chDelimiter ); poCSVTmpLayer->BuildFeatureDefn(NULL, NULL, m_papszOpenOptions); poCSVTmpLayer->SetCRLF( m_poCSVLayer->GetCRLF() ); poCSVTmpLayer->SetCreateCSVT( bCreateCSVT || bHasCSVT ); poCSVTmpLayer->SetWriteBOM( m_poCSVLayer->GetWriteBOM() ); if( m_poCSVLayer->GetGeometryFormat() == OGR_CSV_GEOM_AS_WKT ) poCSVTmpLayer->SetWriteGeometry( wkbNone, OGR_CSV_GEOM_AS_WKT, NULL ); OGRErr eErr = OGRERR_NONE; OGRFeatureDefn* poEditableFDefn = poEditableLayer->GetLayerDefn(); for( int i=0; eErr == OGRERR_NONE && i < poEditableFDefn->GetFieldCount(); i++ ) { OGRFieldDefn oFieldDefn(poEditableFDefn->GetFieldDefn(i)); int iGeomFieldIdx = 0; if( (EQUAL(oFieldDefn.GetNameRef(), "WKT") && (iGeomFieldIdx = poEditableFDefn->GetGeomFieldIndex("")) >= 0) || (iGeomFieldIdx = poEditableFDefn->GetGeomFieldIndex(oFieldDefn.GetNameRef())) >= 0 ) { OGRGeomFieldDefn oGeomFieldDefn( poEditableFDefn->GetGeomFieldDefn(iGeomFieldIdx) ); eErr = poCSVTmpLayer->CreateGeomField( &oGeomFieldDefn ); } else { eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } } const bool bHasXY = ( !m_poCSVLayer->GetXField().empty() && !m_poCSVLayer->GetYField().empty() ); const bool bHasZ = ( !m_poCSVLayer->GetZField().empty() ); if( bHasXY && !CPLFetchBool(m_papszOpenOptions, "KEEP_GEOM_COLUMNS", true) ) { if( poCSVTmpLayer->GetLayerDefn()->GetFieldIndex(m_poCSVLayer->GetXField()) < 0 ) { OGRFieldDefn oFieldDefn(m_poCSVLayer->GetXField(), OFTReal); if( eErr == OGRERR_NONE ) eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } if( poCSVTmpLayer->GetLayerDefn()->GetFieldIndex(m_poCSVLayer->GetYField()) < 0 ) { OGRFieldDefn oFieldDefn(m_poCSVLayer->GetYField(), OFTReal); if( eErr == OGRERR_NONE ) eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } if( bHasZ && poCSVTmpLayer->GetLayerDefn()->GetFieldIndex(m_poCSVLayer->GetZField()) < 0 ) { OGRFieldDefn oFieldDefn(m_poCSVLayer->GetZField(), OFTReal); if( eErr == OGRERR_NONE ) eErr = poCSVTmpLayer->CreateField( &oFieldDefn ); } } int nFirstGeomColIdx = 0; if( m_poCSVLayer->HasHiddenWKTColumn() ) { poCSVTmpLayer->SetWriteGeometry( poEditableFDefn->GetGeomFieldDefn(0)->GetType(), OGR_CSV_GEOM_AS_WKT, poEditableFDefn->GetGeomFieldDefn(0)->GetNameRef()); nFirstGeomColIdx = 1; } if( !(poEditableFDefn->GetGeomFieldCount() == 1 && bHasXY) ) { for( int i=nFirstGeomColIdx; eErr == OGRERR_NONE && i < poEditableFDefn->GetGeomFieldCount(); i++ ) { OGRGeomFieldDefn oGeomFieldDefn( poEditableFDefn->GetGeomFieldDefn(i) ); if( poCSVTmpLayer->GetLayerDefn()->GetGeomFieldIndex(oGeomFieldDefn.GetNameRef()) >= 0 ) continue; eErr = poCSVTmpLayer->CreateGeomField( &oGeomFieldDefn ); } } OGRFeature* poFeature = NULL; poEditableLayer->ResetReading(); while( eErr == OGRERR_NONE && (poFeature = poEditableLayer->GetNextFeature()) != NULL ) { OGRFeature* poNewFeature = new OGRFeature( poCSVTmpLayer->GetLayerDefn() ); poNewFeature->SetFrom(poFeature); if( bHasXY ) { OGRGeometry* poGeom = poFeature->GetGeometryRef(); if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbPoint ) { poNewFeature->SetField( m_poCSVLayer->GetXField(), static_cast<OGRPoint*>(poGeom)->getX()); poNewFeature->SetField( m_poCSVLayer->GetYField(), static_cast<OGRPoint*>(poGeom)->getY()); if( bHasZ ) { poNewFeature->SetField( m_poCSVLayer->GetZField(), static_cast<OGRPoint*>(poGeom)->getZ()); } } } eErr = poCSVTmpLayer->CreateFeature(poNewFeature); delete poFeature; delete poNewFeature; } delete poCSVTmpLayer; if( eErr != OGRERR_NONE ) { CPLError(CE_Failure, CPLE_AppDefined, "Error while creating %s", osTmpFilename.c_str()); VSIUnlink( osTmpFilename ); VSIUnlink( CPLResetExtension(osTmpFilename, "csvt") ); return eErr; } delete m_poCSVLayer; if( osFilename != osTmpFilename ) { CPLString osTmpOriFilename(osFilename + ".ogr_bak"); CPLString osTmpOriCSVTFilename(osCSVTFilename + ".ogr_bak"); if( VSIRename( osFilename, osTmpOriFilename ) != 0 || (bHasCSVT && VSIRename( osCSVTFilename, osTmpOriCSVTFilename ) != 0 ) || VSIRename( osTmpFilename, osFilename) != 0 || (bHasCSVT && VSIRename( osTmpCSVTFilename, osCSVTFilename ) != 0) ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename files"); *ppoDecoratedLayer = NULL; m_poCSVLayer = NULL; return OGRERR_FAILURE; } VSIUnlink( osTmpOriFilename ); if( bHasCSVT ) VSIUnlink( osTmpOriCSVTFilename ); } VSILFILE* fp = VSIFOpenL( osFilename, "rb+" ); if( fp == NULL ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot reopen updated %s", osFilename.c_str()); *ppoDecoratedLayer = NULL; m_poCSVLayer = NULL; return OGRERR_FAILURE; } m_poCSVLayer = new OGRCSVLayer( osLayerName, fp, osFilename, false, /* new */ true, /* update */ chDelimiter ); m_poCSVLayer->BuildFeatureDefn(NULL, NULL, m_papszOpenOptions); *ppoDecoratedLayer = m_poCSVLayer; return OGRERR_NONE; }
/********************************************************************** * IMapInfoFile::CreateFeature() * * Standard OGR CreateFeature implementation. This method is used * to create a new feature in current dataset **********************************************************************/ OGRErr IMapInfoFile::CreateFeature(OGRFeature *poFeature) { TABFeature *poTABFeature; OGRGeometry *poGeom; OGRwkbGeometryType eGType; OGRErr eErr; TABPoint *poTABPointFeature = NULL; TABRegion *poTABRegionFeature = NULL; TABPolyline *poTABPolylineFeature = NULL; /*----------------------------------------------------------------- * MITAB won't accept new features unless they are in a type derived * from TABFeature... so we have to do our best to map to the right * feature type based on the geometry type. *----------------------------------------------------------------*/ poGeom = poFeature->GetGeometryRef(); if( poGeom != NULL ) eGType = poGeom->getGeometryType(); else eGType = wkbNone; switch( wkbFlatten(eGType) ) { /*------------------------------------------------------------- * POINT *------------------------------------------------------------*/ case wkbPoint: poTABFeature = new TABPoint(poFeature->GetDefnRef()); if(poFeature->GetStyleString()) { poTABPointFeature = (TABPoint*)poTABFeature; poTABPointFeature->SetSymbolFromStyleString( poFeature->GetStyleString()); } break; /*------------------------------------------------------------- * REGION *------------------------------------------------------------*/ case wkbPolygon: case wkbMultiPolygon: poTABFeature = new TABRegion(poFeature->GetDefnRef()); if(poFeature->GetStyleString()) { poTABRegionFeature = (TABRegion*)poTABFeature; poTABRegionFeature->SetPenFromStyleString( poFeature->GetStyleString()); poTABRegionFeature->SetBrushFromStyleString( poFeature->GetStyleString()); } break; /*------------------------------------------------------------- * LINE/PLINE/MULTIPLINE *------------------------------------------------------------*/ case wkbLineString: case wkbMultiLineString: poTABFeature = new TABPolyline(poFeature->GetDefnRef()); if(poFeature->GetStyleString()) { poTABPolylineFeature = (TABPolyline*)poTABFeature; poTABPolylineFeature->SetPenFromStyleString( poFeature->GetStyleString()); } break; /*------------------------------------------------------------- * Collection types that are not directly supported... convert * to multiple features in output file through recursive calls. *------------------------------------------------------------*/ case wkbGeometryCollection: case wkbMultiPoint: { OGRErr eStatus = OGRERR_NONE; int i; OGRGeometryCollection *poColl = (OGRGeometryCollection*)poGeom; OGRFeature *poTmpFeature = poFeature->Clone(); for (i=0; eStatus==OGRERR_NONE && i<poColl->getNumGeometries(); i++) { poTmpFeature->SetGeometry(poColl->getGeometryRef(i)); eStatus = CreateFeature(poTmpFeature); } delete poTmpFeature; return eStatus; } break; /*------------------------------------------------------------- * Unsupported type.... convert to MapInfo geometry NONE *------------------------------------------------------------*/ case wkbUnknown: default: poTABFeature = new TABFeature(poFeature->GetDefnRef()); break; } if( poGeom != NULL ) poTABFeature->SetGeometryDirectly(poGeom->clone()); for (int i=0; i< poFeature->GetDefnRef()->GetFieldCount(); i++) { poTABFeature->SetField(i,poFeature->GetRawFieldRef( i )); } eErr = CreateFeature(poTABFeature); delete poTABFeature; return eErr; }
OGRGeometry* GML_BuildOGRGeometryFromList(const CPLXMLNode* const * papsGeometry, int bTryToMakeMultipolygons, int bInvertAxisOrderIfLatLong, const char* pszDefaultSRSName, int bConsiderEPSGAsURN, int bGetSecondaryGeometryOption, void* hCacheSRS) { OGRGeometry* poGeom = NULL; int i; OGRGeometryCollection* poCollection = NULL; for(i=0;papsGeometry[i] != NULL;i++) { OGRGeometry* poSubGeom = GML2OGRGeometry_XMLNode( papsGeometry[i], bGetSecondaryGeometryOption ); if (poSubGeom) { if (poGeom == NULL) poGeom = poSubGeom; else { if (poCollection == NULL) { if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbPolygon) { OGRGeometryCollection* poGeomColl = new OGRMultiPolygon(); poGeomColl->addGeometryDirectly(poGeom); poGeomColl->addGeometryDirectly(poSubGeom); poGeom = poGeomColl; } else if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbPolygon) { OGRGeometryCollection* poGeomColl = (OGRGeometryCollection* )poGeom; poGeomColl->addGeometryDirectly(poSubGeom); } else if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbMultiPolygon) { OGRGeometryCollection* poGeomColl = (OGRGeometryCollection* )poGeom; OGRGeometryCollection* poGeomColl2 = (OGRGeometryCollection* )poSubGeom; int nCount = poGeomColl2->getNumGeometries(); int i; for(i=0;i<nCount;i++) { poGeomColl->addGeometry(poGeomColl2->getGeometryRef(i)); } delete poSubGeom; } else if (bTryToMakeMultipolygons && wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon) { delete poGeom; delete poSubGeom; return GML_BuildOGRGeometryFromList(papsGeometry, FALSE, bInvertAxisOrderIfLatLong, pszDefaultSRSName, bConsiderEPSGAsURN, bGetSecondaryGeometryOption, hCacheSRS); } else { poCollection = new OGRGeometryCollection(); poCollection->addGeometryDirectly(poGeom); poGeom = poCollection; } } if (poCollection != NULL) { poCollection->addGeometryDirectly(poSubGeom); } } } } if ( poGeom != NULL && bInvertAxisOrderIfLatLong ) { std::string osWork; const char* pszSRSName = GML_ExtractSrsNameFromGeometry(papsGeometry, osWork, bConsiderEPSGAsURN); const char* pszNameLookup = pszSRSName ? pszSRSName : pszDefaultSRSName; if (pszNameLookup != NULL) { SRSCache* poSRSCache = (SRSCache*)hCacheSRS; int bSwap; if (strcmp(poSRSCache->osLastSRSName.c_str(), pszNameLookup) == 0) { bSwap = poSRSCache->bAxisInvertLastSRSName; } else { bSwap = GML_IsSRSLatLongOrder(pszNameLookup); poSRSCache->osLastSRSName = pszNameLookup; poSRSCache->bAxisInvertLastSRSName= bSwap; } if (bSwap) poGeom->swapXY(); } } return poGeom; }
OGRErr GTMTrackLayer::ICreateFeature (OGRFeature *poFeature) { VSILFILE* fpTmpTrackpoints = poDS->getTmpTrackpointsFP(); if (fpTmpTrackpoints == nullptr) return OGRERR_FAILURE; VSILFILE* fpTmpTracks = poDS->getTmpTracksFP(); if (fpTmpTracks == nullptr) return OGRERR_FAILURE; OGRGeometry *poGeom = poFeature->GetGeometryRef(); if ( poGeom == nullptr ) { CPLError( CE_Failure, CPLE_AppDefined, "Features without geometry not supported by GTM writer in " "track layer." ); return OGRERR_FAILURE; } if (nullptr != poCT) { poGeom = poGeom->clone(); poGeom->transform( poCT ); } switch( poGeom->getGeometryType() ) { case wkbLineString: case wkbLineString25D: { WriteFeatureAttributes(poFeature); OGRLineString* line = poGeom->toLineString(); for(int i = 0; i < line->getNumPoints(); ++i) { double lat = line->getY(i); double lon = line->getX(i); float altitude = 0; CheckAndFixCoordinatesValidity(lat, lon); poDS->checkBounds((float)lat, (float)lon); if (line->getGeometryType() == wkbLineString25D) altitude = static_cast<float>(line->getZ(i)); WriteTrackpoint( lat, lon, altitude, i==0 ); } break; } case wkbMultiLineString: case wkbMultiLineString25D: { for( auto&& line: poGeom->toMultiLineString() ) { WriteFeatureAttributes(poFeature); int n = line->getNumPoints(); for(int i = 0; i < n; ++i) { double lat = line->getY(i); double lon = line->getX(i); float altitude = 0; CheckAndFixCoordinatesValidity(lat, lon); if (line->getGeometryType() == wkbLineString25D) altitude = static_cast<float>(line->getZ(i)); WriteTrackpoint( lat, lon, altitude, i==0 ); } } break; } default: { CPLError( CE_Failure, CPLE_NotSupported, "Geometry type of `%s' not supported for 'track' element.\n", OGRGeometryTypeToName(poGeom->getGeometryType()) ); if (nullptr != poCT) delete poGeom; return OGRERR_FAILURE; } } if (nullptr != poCT) delete poGeom; return OGRERR_NONE; }
OGRErr OGRCreateFromGeomedia( GByte *pabyGeom, OGRGeometry **ppoGeom, int nBytes ) { *ppoGeom = NULL; if( nBytes < 16 ) return OGRERR_FAILURE; if( !(pabyGeom[1] == 0xFF && pabyGeom[2] == 0xD2 && pabyGeom[3] == 0x0F) ) return OGRERR_FAILURE; int nGeomType = pabyGeom[0]; pabyGeom += 16; nBytes -= 16; if( nGeomType == GEOMEDIA_POINT || nGeomType == GEOMEDIA_ORIENTED_POINT ) { if (nBytes < 3 * 8) return OGRERR_FAILURE; double dfX, dfY, dfZ; memcpy(&dfX, pabyGeom, 8); CPL_LSBPTR64(&dfX); memcpy(&dfY, pabyGeom + 8, 8); CPL_LSBPTR64(&dfY); memcpy(&dfZ, pabyGeom + 16, 8); CPL_LSBPTR64(&dfZ); *ppoGeom = new OGRPoint( dfX, dfY, dfZ ); return OGRERR_NONE; } else if ( nGeomType == GEOMEDIA_POLYLINE ) { if (nBytes < 4) return OGRERR_FAILURE; int nPoints; memcpy(&nPoints, pabyGeom, 4); CPL_LSBPTR32(&nPoints); pabyGeom += 4; nBytes -= 4; if (nPoints < 0 || nPoints > INT_MAX / 24 || nBytes < nPoints * 24) return OGRERR_FAILURE; OGRLineString* poLS = new OGRLineString(); poLS->setNumPoints(nPoints); int i; for(i=0;i<nPoints;i++) { double dfX, dfY, dfZ; memcpy(&dfX, pabyGeom, 8); CPL_LSBPTR64(&dfX); memcpy(&dfY, pabyGeom + 8, 8); CPL_LSBPTR64(&dfY); memcpy(&dfZ, pabyGeom + 16, 8); CPL_LSBPTR64(&dfZ); poLS->setPoint(i, dfX, dfY, dfZ); pabyGeom += 24; } *ppoGeom = poLS; return OGRERR_NONE; } else if ( nGeomType == GEOMEDIA_POLYGON ) { if (nBytes < 4) return OGRERR_FAILURE; int nPoints; memcpy(&nPoints, pabyGeom, 4); CPL_LSBPTR32(&nPoints); pabyGeom += 4; nBytes -= 4; if (nPoints < 0 || nPoints > INT_MAX / 24 || nBytes < nPoints * 24) return OGRERR_FAILURE; OGRLinearRing* poRing = new OGRLinearRing(); poRing->setNumPoints(nPoints); int i; for(i=0;i<nPoints;i++) { double dfX, dfY, dfZ; memcpy(&dfX, pabyGeom, 8); CPL_LSBPTR64(&dfX); memcpy(&dfY, pabyGeom + 8, 8); CPL_LSBPTR64(&dfY); memcpy(&dfZ, pabyGeom + 16, 8); CPL_LSBPTR64(&dfZ); poRing->setPoint(i, dfX, dfY, dfZ); pabyGeom += 24; } OGRPolygon* poPoly = new OGRPolygon(); poPoly->addRingDirectly(poRing); *ppoGeom = poPoly; return OGRERR_NONE; } else if ( nGeomType == GEOMEDIA_BOUNDARY ) { if (nBytes < 4) return OGRERR_FAILURE; int nExteriorSize; memcpy(&nExteriorSize, pabyGeom, 4); CPL_LSBPTR32(&nExteriorSize); pabyGeom += 4; nBytes -= 4; if (nBytes < nExteriorSize) return OGRERR_FAILURE; OGRGeometry* poExteriorGeom = NULL; if (OGRCreateFromGeomedia( pabyGeom, &poExteriorGeom, nExteriorSize ) != OGRERR_NONE) return OGRERR_FAILURE; if ( wkbFlatten( poExteriorGeom->getGeometryType() ) != wkbPolygon ) { delete poExteriorGeom; return OGRERR_FAILURE; } pabyGeom += nExteriorSize; nBytes -= nExteriorSize; if (nBytes < 4) { delete poExteriorGeom; return OGRERR_FAILURE; } int nInteriorSize; memcpy(&nInteriorSize, pabyGeom, 4); CPL_LSBPTR32(&nInteriorSize); pabyGeom += 4; nBytes -= 4; if (nBytes < nInteriorSize) { delete poExteriorGeom; return OGRERR_FAILURE; } OGRGeometry* poInteriorGeom = NULL; if (OGRCreateFromGeomedia( pabyGeom, &poInteriorGeom, nInteriorSize ) != OGRERR_NONE) { delete poExteriorGeom; return OGRERR_FAILURE; } OGRwkbGeometryType interiorGeomType = wkbFlatten( poInteriorGeom->getGeometryType() ); if ( interiorGeomType == wkbPolygon ) { ((OGRPolygon*)poExteriorGeom)->addRing(((OGRPolygon*)poInteriorGeom)->getExteriorRing()); } else if ( interiorGeomType == wkbMultiPolygon ) { int numGeom = ((OGRMultiPolygon*)poInteriorGeom)->getNumGeometries(); for ( int i = 0; i < numGeom; ++i ) { OGRPolygon* poInteriorPolygon = (OGRPolygon*)((OGRMultiPolygon*)poInteriorGeom)->getGeometryRef(i); ((OGRPolygon*)poExteriorGeom)->addRing( poInteriorPolygon->getExteriorRing() ); } } else { delete poExteriorGeom; delete poInteriorGeom; return OGRERR_FAILURE; } delete poInteriorGeom; *ppoGeom = poExteriorGeom; return OGRERR_NONE; } else if ( nGeomType == GEOMEDIA_COLLECTION || nGeomType == GEOMEDIA_MULTILINE || nGeomType == GEOMEDIA_MULTIPOLYGON ) { if (nBytes < 4) return OGRERR_FAILURE; int i; int nParts; memcpy(&nParts, pabyGeom, 4); CPL_LSBPTR32(&nParts); pabyGeom += 4; nBytes -= 4; if (nParts < 0 || nParts > INT_MAX / (4 + 16) || nBytes < nParts * (4 + 16)) return OGRERR_FAILURE; /* Can this collection be considered as a multipolyline or multipolygon ? */ if ( nGeomType == GEOMEDIA_COLLECTION ) { GByte* pabyGeomBackup = pabyGeom; int nBytesBackup = nBytes; int bAllPolyline = TRUE; int bAllPolygon = TRUE; for(i=0;i<nParts;i++) { if (nBytes < 4) return OGRERR_FAILURE; int nSubBytes; memcpy(&nSubBytes, pabyGeom, 4); CPL_LSBPTR32(&nSubBytes); if (nSubBytes < 0) { return OGRERR_FAILURE; } pabyGeom += 4; nBytes -= 4; if (nBytes < nSubBytes) { return OGRERR_FAILURE; } if( nSubBytes < 16 ) return OGRERR_FAILURE; if( !(pabyGeom[1] == 0xFF && pabyGeom[2] == 0xD2 && pabyGeom[3] == 0x0F) ) return OGRERR_FAILURE; int nSubGeomType = pabyGeom[0]; if ( nSubGeomType != GEOMEDIA_POLYLINE ) bAllPolyline = FALSE; if ( nSubGeomType != GEOMEDIA_POLYGON ) bAllPolygon = FALSE; pabyGeom += nSubBytes; nBytes -= nSubBytes; } pabyGeom = pabyGeomBackup; nBytes = nBytesBackup; if (bAllPolyline) nGeomType = GEOMEDIA_MULTILINE; else if (bAllPolygon) nGeomType = GEOMEDIA_MULTIPOLYGON; } OGRGeometryCollection* poColl = (nGeomType == GEOMEDIA_MULTILINE) ? new OGRMultiLineString() : (nGeomType == GEOMEDIA_MULTIPOLYGON) ? new OGRMultiPolygon() : new OGRGeometryCollection(); for(i=0;i<nParts;i++) { if (nBytes < 4) return OGRERR_FAILURE; int nSubBytes; memcpy(&nSubBytes, pabyGeom, 4); CPL_LSBPTR32(&nSubBytes); if (nSubBytes < 0) { delete poColl; return OGRERR_FAILURE; } pabyGeom += 4; nBytes -= 4; if (nBytes < nSubBytes) { delete poColl; return OGRERR_FAILURE; } OGRGeometry* poSubGeom = NULL; if (OGRCreateFromGeomedia( pabyGeom, &poSubGeom, nSubBytes ) == OGRERR_NONE) { if (wkbFlatten(poColl->getGeometryType()) == wkbMultiPolygon && wkbFlatten(poSubGeom->getGeometryType()) == wkbLineString) { OGRPolygon* poPoly = new OGRPolygon(); OGRLinearRing* poRing = new OGRLinearRing(); poRing->addSubLineString((OGRLineString*)poSubGeom); poPoly->addRingDirectly(poRing); delete poSubGeom; poSubGeom = poPoly; } if (poColl->addGeometryDirectly(poSubGeom) != OGRERR_NONE) { //printf("%d %d\n", poColl->getGeometryType() & ~wkb25DBit, poSubGeom->getGeometryType() & ~wkb25DBit); delete poSubGeom; } } pabyGeom += nSubBytes; nBytes -= nSubBytes; } *ppoGeom = poColl; return OGRERR_NONE; } else { CPLDebug("GEOMEDIA", "Unhandled type %d", nGeomType); } return OGRERR_FAILURE; }