OGRErr OGRGeoJSONSeqWriteLayer::ICreateFeature( OGRFeature* poFeature ) { VSILFILE* fp = m_poDS->GetOutputFile(); std::unique_ptr<OGRFeature> poFeatureToWrite; if( m_poCT != nullptr ) { poFeatureToWrite.reset(new OGRFeature(m_poFeatureDefn)); poFeatureToWrite->SetFrom( poFeature ); poFeatureToWrite->SetFID( poFeature->GetFID() ); OGRGeometry* poGeometry = poFeatureToWrite->GetGeometryRef(); if( poGeometry ) { const char* const apszOptions[] = { "WRAPDATELINE=YES", nullptr }; OGRGeometry* poNewGeom = OGRGeometryFactory::transformWithOptions( poGeometry, m_poCT, const_cast<char**>(apszOptions)); if( poNewGeom == nullptr ) { return OGRERR_FAILURE; } OGREnvelope sEnvelope; poNewGeom->getEnvelope(&sEnvelope); if( sEnvelope.MinX < -180.0 || sEnvelope.MaxX > 180.0 || sEnvelope.MinY < -90.0 || sEnvelope.MaxY > 90.0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Geometry extent outside of [-180.0,180.0]x[-90.0,90.0] bounds"); return OGRERR_FAILURE; } poFeatureToWrite->SetGeometryDirectly( poNewGeom ); } } json_object* poObj = OGRGeoJSONWriteFeature( poFeatureToWrite.get() ? poFeatureToWrite.get() : poFeature, m_oWriteOptions ); CPLAssert( nullptr != poObj ); if( m_bRS ) { VSIFPrintfL( fp, "%c", RS); } VSIFPrintfL( fp, "%s\n", json_object_to_json_string( poObj ) ); json_object_put( poObj ); return OGRERR_NONE; }
OGRErr OGRGeoJSONWriteLayer::ICreateFeature( OGRFeature* poFeature ) { VSILFILE* fp = poDS_->GetOutputFile(); if( NULL == poFeature ) { CPLDebug( "GeoJSON", "Feature is null" ); return OGRERR_INVALID_HANDLE; } json_object* poObj = OGRGeoJSONWriteFeature( poFeature, bWriteBBOX, nCoordPrecision ); CPLAssert( NULL != poObj ); if( nOutCounter_ > 0 ) { /* Separate "Feature" entries in "FeatureCollection" object. */ VSIFPrintfL( fp, ",\n" ); } VSIFPrintfL( fp, "%s", json_object_to_json_string( poObj ) ); json_object_put( poObj ); ++nOutCounter_; OGRGeometry* poGeometry = poFeature->GetGeometryRef(); if ( bWriteBBOX && !poGeometry->IsEmpty() ) { OGREnvelope3D sEnvelope; poGeometry->getEnvelope(&sEnvelope); if( poGeometry->getCoordinateDimension() == 3 ) bBBOX3D = TRUE; sEnvelopeLayer.Merge(sEnvelope); } return OGRERR_NONE; }
OGRErr OGRGeoJSONWriteLayer::ICreateFeature( OGRFeature* poFeature ) { VSILFILE* fp = poDS_->GetOutputFile(); OGRFeature* poFeatureToWrite; if( poCT_ != nullptr || bRFC7946_ ) { poFeatureToWrite = new OGRFeature(poFeatureDefn_); poFeatureToWrite->SetFrom( poFeature ); poFeatureToWrite->SetFID( poFeature->GetFID() ); OGRGeometry* poGeometry = poFeatureToWrite->GetGeometryRef(); if( poGeometry ) { const char* const apszOptions[] = { "WRAPDATELINE=YES", nullptr }; OGRGeometry* poNewGeom = OGRGeometryFactory::transformWithOptions( poGeometry, poCT_, const_cast<char**>(apszOptions), oTransformCache_); if( poNewGeom == nullptr ) { delete poFeatureToWrite; return OGRERR_FAILURE; } OGREnvelope sEnvelope; poNewGeom->getEnvelope(&sEnvelope); if( sEnvelope.MinX < -180.0 || sEnvelope.MaxX > 180.0 || sEnvelope.MinY < -90.0 || sEnvelope.MaxY > 90.0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Geometry extent outside of [-180.0,180.0]x[-90.0,90.0] bounds"); delete poFeatureToWrite; return OGRERR_FAILURE; } poFeatureToWrite->SetGeometryDirectly( poNewGeom ); } } else { poFeatureToWrite = poFeature; } json_object* poObj = OGRGeoJSONWriteFeature( poFeatureToWrite, oWriteOptions_ ); CPLAssert( nullptr != poObj ); if( nOutCounter_ > 0 ) { /* Separate "Feature" entries in "FeatureCollection" object. */ VSIFPrintfL( fp, ",\n" ); } VSIFPrintfL( fp, "%s", json_object_to_json_string( poObj ) ); json_object_put( poObj ); ++nOutCounter_; OGRGeometry* poGeometry = poFeatureToWrite->GetGeometryRef(); if( bWriteFC_BBOX && poGeometry != nullptr && !poGeometry->IsEmpty() ) { OGREnvelope3D sEnvelope = OGRGeoJSONGetBBox( poGeometry, oWriteOptions_ ); if( poGeometry->getCoordinateDimension() == 3 ) bBBOX3D = true; if( !sEnvelopeLayer.IsInit() ) { sEnvelopeLayer = sEnvelope; } else if( oWriteOptions_.bBBOXRFC7946 ) { const bool bEnvelopeCrossAM = ( sEnvelope.MinX > sEnvelope.MaxX ); const bool bEnvelopeLayerCrossAM = ( sEnvelopeLayer.MinX > sEnvelopeLayer.MaxX ); if( bEnvelopeCrossAM ) { if( bEnvelopeLayerCrossAM ) { sEnvelopeLayer.MinX = std::min(sEnvelopeLayer.MinX, sEnvelope.MinX); sEnvelopeLayer.MaxX = std::max(sEnvelopeLayer.MaxX, sEnvelope.MaxX); } else { if( sEnvelopeLayer.MinX > 0 ) { sEnvelopeLayer.MinX = std::min(sEnvelopeLayer.MinX, sEnvelope.MinX); sEnvelopeLayer.MaxX = sEnvelope.MaxX; } else if( sEnvelopeLayer.MaxX < 0 ) { sEnvelopeLayer.MaxX = std::max(sEnvelopeLayer.MaxX, sEnvelope.MaxX); sEnvelopeLayer.MinX = sEnvelope.MinX; } else { sEnvelopeLayer.MinX = -180.0; sEnvelopeLayer.MaxX = 180.0; } } } else if( bEnvelopeLayerCrossAM ) { if( sEnvelope.MinX > 0 ) { sEnvelopeLayer.MinX = std::min(sEnvelopeLayer.MinX, sEnvelope.MinX); } else if( sEnvelope.MaxX < 0 ) { sEnvelopeLayer.MaxX = std::max(sEnvelopeLayer.MaxX, sEnvelope.MaxX); } else { sEnvelopeLayer.MinX = -180.0; sEnvelopeLayer.MaxX = 180.0; } } else { sEnvelopeLayer.MinX = std::min(sEnvelopeLayer.MinX, sEnvelope.MinX); sEnvelopeLayer.MaxX = std::max(sEnvelopeLayer.MaxX, sEnvelope.MaxX); } sEnvelopeLayer.MinY = std::min(sEnvelopeLayer.MinY, sEnvelope.MinY); sEnvelopeLayer.MaxY = std::max(sEnvelopeLayer.MaxY, sEnvelope.MaxY); } else { sEnvelopeLayer.Merge(sEnvelope); } } if( poFeatureToWrite != poFeature ) delete poFeatureToWrite; return OGRERR_NONE; }