void CoordinateTransformation::Init(unsigned int nSourceEPSG, unsigned int nDestEPSG) { _nSourceEPSG = nSourceEPSG; _nDestEPSG = nDestEPSG; _bIdentity = false; if (_nSourceEPSG == 0 || _nDestEPSG == 0 || nSourceEPSG == _nDest2) _bIdentity = true; if (_pCT) { OCTDestroyCoordinateTransformation((OGRCoordinateTransformation*) _pCT); _pCT = 0; } if (_pCTBack) { OCTDestroyCoordinateTransformation((OGRCoordinateTransformation*) _pCTBack); _pCTBack = 0; } if (!_bIdentity) { OGRSpatialReference srcref; OGRSpatialReference dstref; OGRErr err; err = srcref.importFromEPSG(_nSourceEPSG); err = dstref.importFromEPSG(_nDestEPSG); _pCT = (void*)OGRCreateCoordinateTransformation(&srcref, &dstref); _pCTBack = (void*)OGRCreateCoordinateTransformation(&dstref, &srcref); } }
void OGRUnionLayer::AutoWarpLayerIfNecessary(int iLayer) { if( !pabCheckIfAutoWrap[iLayer] ) { pabCheckIfAutoWrap[iLayer] = TRUE; for(int i=0; i<GetLayerDefn()->GetGeomFieldCount();i++) { OGRSpatialReference* poSRS = GetLayerDefn()->GetGeomFieldDefn(i)->GetSpatialRef(); if( poSRS != NULL ) poSRS->Reference(); OGRFeatureDefn* poSrcFeatureDefn = papoSrcLayers[iLayer]->GetLayerDefn(); int iSrcGeomField = poSrcFeatureDefn->GetGeomFieldIndex( GetLayerDefn()->GetGeomFieldDefn(i)->GetNameRef()); if( iSrcGeomField >= 0 ) { OGRSpatialReference* poSRS2 = poSrcFeatureDefn->GetGeomFieldDefn(iSrcGeomField)->GetSpatialRef(); if( (poSRS == NULL && poSRS2 != NULL) || (poSRS != NULL && poSRS2 == NULL) ) { CPLError(CE_Warning, CPLE_AppDefined, "SRS of geometry field '%s' layer %s not consistent with UnionLayer SRS", GetLayerDefn()->GetGeomFieldDefn(i)->GetNameRef(), papoSrcLayers[iLayer]->GetName()); } else if (poSRS != NULL && poSRS2 != NULL && poSRS != poSRS2 && !poSRS->IsSame(poSRS2)) { CPLDebug("VRT", "SRS of geometry field '%s' layer %s not consistent with UnionLayer SRS. " "Trying auto warping", GetLayerDefn()->GetGeomFieldDefn(i)->GetNameRef(), papoSrcLayers[iLayer]->GetName()); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation( poSRS2, poSRS ); OGRCoordinateTransformation* poReversedCT = (poCT != NULL) ? OGRCreateCoordinateTransformation( poSRS, poSRS2 ) : NULL; if( poReversedCT != NULL ) papoSrcLayers[iLayer] = new OGRWarpedLayer( papoSrcLayers[iLayer], iSrcGeomField, TRUE, poCT, poReversedCT); else { CPLError(CE_Warning, CPLE_AppDefined, "AutoWarpLayerIfNecessary failed to create " "poCT or poReversedCT."); if ( poCT != NULL ) delete poCT; } } } if( poSRS != NULL ) poSRS->Release(); } } }
GeometryHelper() { m_wgs.SetWellKnownGeogCS("WGS84"); m_mercator.importFromEPSG(3857); m_wgs2mercator = OGRCreateCoordinateTransformation(&m_wgs, &m_mercator); if (m_wgs2mercator == NULL) { std::cerr << "ERROR: m_wgs2mercator is null" << std::endl; } m_mercator2wgs = OGRCreateCoordinateTransformation(&m_mercator, &m_wgs); if (m_mercator2wgs == NULL) { std::cerr << "ERROR: m_mercator2wgs is null" << std::endl; } }
bool V2vProj::Compute(const data::VectorBarral * barrel) { OGRDataSource * poSourceDs = VectorOpen(barrel->GetSrcDataSource().c_str(), GA_ReadOnly); ON_SCOPE_EXIT([&]() {OGRDataSource::DestroyDataSource(poSourceDs); }); OGRDataSource * poOutputDs = VectorOpen(barrel->GetDstDataSource().c_str(), GA_Update); ON_SCOPE_EXIT([&]() {OGRDataSource::DestroyDataSource(poOutputDs); }); OGRLayer * poSrcLayer = poSourceDs->GetLayerByName( barrel->GetSrcLayer().c_str()); OGRLayer * poDstLayer = poOutputDs->GetLayerByName( barrel->GetDstLayer().c_str()); OGRSpatialReference * poSourceSRS = poSrcLayer->GetSpatialRef(); OGRCoordinateTransformation * poCT = poCT = OGRCreateCoordinateTransformation( poSourceSRS, m_ogrSr); OGRFeatureDefn * poDstFeatureDefn = poDstLayer->GetLayerDefn(); auto features = barrel->GetFeatures(); std::for_each(begin(features), end(features) , [&](int fid) { poSrcLayer->GetFeature(fid); OGRFeature * poDstFeature = OGRFeature::CreateFeature(poDstFeatureDefn); ON_SCOPE_EXIT([&]() {OGRFeature::DestroyFeature(poDstFeature); }); poDstFeature->SetFrom(poSrcLayer->GetFeature(fid)); OGRGeometry * poDstGeometry = poDstFeature->GetGeometryRef(); OGRGeometry * poReprojectedGeom = OGRGeometryFactory::transformWithOptions( poDstGeometry, poCT, NULL); poDstFeature->SetGeometryDirectly(poReprojectedGeom); poDstLayer->CreateFeature(poDstFeature); }); return true; }
std::pair<double,double> Terrain::getGeoTransformFromDEMs(Terrain *large, Terrain *small) { QString proj = large->dataset->GetProjectionRef(); OGRSpatialReference sr1; auto t = proj.toLatin1(); char *test = t.data(); sr1.importFromWkt(&test); proj = small->dataset->GetProjectionRef(); t = proj.toLatin1(); OGRSpatialReference sr2; test = t.data(); sr2.importFromWkt(&test); OGRCoordinateTransformation* poTransform = OGRCreateCoordinateTransformation( &sr2, &sr1 ); large->geot.resize(6); small->geot.resize(6); large->dataset->GetGeoTransform(large->geot.data()); small->dataset->GetGeoTransform(small->geot.data()); // double x = small->geot[0]; double y = small->geot[3]; // DCEWsqrext.tif upperleft hand corner is convert to tl2p5_dem.tif coordinate system. poTransform->Transform (1, &x, &y); return std::pair<double,double>(x,y); }
bool GDALPointToLatLon( double &x, double &y, GDALDataset *poSrcDS, const char *datum ) { char* pszPrj = NULL; OGRSpatialReference oSourceSRS, oTargetSRS; OGRCoordinateTransformation *poCT; if( poSrcDS == NULL ) return false; if( poSrcDS->GetProjectionRef() == NULL ) return false; else pszPrj = (char*)poSrcDS->GetProjectionRef(); oSourceSRS.importFromWkt( &pszPrj ); oTargetSRS.SetWellKnownGeogCS( datum ); poCT = OGRCreateCoordinateTransformation( &oSourceSRS, &oTargetSRS ); if( poCT == NULL ) return false; if( !poCT->Transform( 1, &x, &y ) ) { OGRCoordinateTransformation::DestroyCT( poCT ); return false; } OGRCoordinateTransformation::DestroyCT( poCT ); return true; }
void WCTSIsTransformable( CPLXMLNode *psOperation ) { OGRSpatialReference *poSrcCRS, *poDstCRS; CPLXMLNode *psSrcXMLCRS, *psDstXMLCRS; /* -------------------------------------------------------------------- */ /* Translate the source CRS. */ /* -------------------------------------------------------------------- */ psSrcXMLCRS = CPLGetXMLNode( psOperation, "SourceCRS" ); if( psSrcXMLCRS == NULL ) WCTSEmitServiceException( "Unable to identify SourceCRS.CoordinateReferenceSystem" ); poSrcCRS = WCTSImportCoordinateReferenceSystem( psSrcXMLCRS ); /* -------------------------------------------------------------------- */ /* Translate the destination CRS. */ /* -------------------------------------------------------------------- */ psDstXMLCRS = CPLGetXMLNode( psOperation, "TargetCRS" ); if( psDstXMLCRS == NULL ) WCTSEmitServiceException( "Unable to identify DestinationCRS.CoordinateReferenceSystem" ); poDstCRS = WCTSImportCoordinateReferenceSystem( psDstXMLCRS ); /* -------------------------------------------------------------------- */ /* Create a transformation object between the coordinate */ /* systems as an added step of verification that they are */ /* supported. */ /* -------------------------------------------------------------------- */ OGRCoordinateTransformation *poCT; const char *pszResult; poCT = OGRCreateCoordinateTransformation( poSrcCRS, poDstCRS ); if( poCT == NULL ) pszResult = "false"; else { delete poCT; pszResult = "true"; } delete poSrcCRS; delete poDstCRS; /* -------------------------------------------------------------------- */ /* Return the answer. */ /* -------------------------------------------------------------------- */ printf( "Content-type: text/xml%c%c", 10, 10 ); printf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); printf( "<TransformableResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.deegree.org/xml/schemas/wcts/transformableResponse.xsd\" transformable=\"%s\"/>\n", pszResult ); exit( 0 ); }
OGRCoordinateTransformationH CPL_STDCALL OCTNewCoordinateTransformation( OGRSpatialReferenceH hSourceSRS, OGRSpatialReferenceH hTargetSRS ) { return (OGRCoordinateTransformationH) OGRCreateCoordinateTransformation( (OGRSpatialReference *) hSourceSRS, (OGRSpatialReference *) hTargetSRS ); }
void WebMapService::Layer::CreateTransform(OGRSpatialReference* src, OGRSpatialReference* dst) { SrcDestTransfromId transId; transId.src = src; transId.dst = dst; auto existingTransform = srsTransforms.find(transId); if (existingTransform != srsTransforms.end()) return; srsTransforms[transId] = (src == dst) ? NULL : OGRCreateCoordinateTransformation(src, dst); }
/** Fetch the longitude/latitude bounds of an image * @param poDS a pointer to a valid GDAL Dataset * @param boundsLonLat a pointer to a double[4] n, e, s, w order * @return true on valid population of boundsLonLat in n, e, s, w order */ bool GDALGetBounds( GDALDataset *poDS, double *boundsLonLat ) { char* pszPrj; double adfGeoTransform[6]; int xSize, ySize; double nLat, eLon, sLat, wLon; OGRSpatialReference oSourceSRS, oTargetSRS; OGRCoordinateTransformation *poCT; if( poDS == NULL ) return false; xSize = poDS->GetRasterXSize (); ySize = poDS->GetRasterYSize(); poDS->GetGeoTransform( adfGeoTransform ); if( poDS->GetProjectionRef() == NULL ) return false; else pszPrj = (char*)poDS->GetProjectionRef(); oSourceSRS.importFromWkt( &pszPrj ); oTargetSRS.SetWellKnownGeogCS( "WGS84" ); poCT = OGRCreateCoordinateTransformation( &oSourceSRS, &oTargetSRS ); if( poCT == NULL ) return false; nLat = adfGeoTransform[3] + adfGeoTransform[4] * 0 + adfGeoTransform[5] * 0; eLon = adfGeoTransform[0] + adfGeoTransform[1] * xSize + adfGeoTransform[2] * 0; sLat = adfGeoTransform[3] + adfGeoTransform[4] * 0 + adfGeoTransform[5] * ySize; wLon = adfGeoTransform[0] + adfGeoTransform[1] * 0 + adfGeoTransform[2] * 0; if( !poCT->Transform( 1, &eLon, &nLat ) ) { OGRCoordinateTransformation::DestroyCT( poCT ); return false; } boundsLonLat[0] = nLat; boundsLonLat[1] = eLon; if( !poCT->Transform( 1, &wLon, &sLat ) ) { OGRCoordinateTransformation::DestroyCT( poCT ); return false; } boundsLonLat[2] = sLat; boundsLonLat[3] = wLon; OGRCoordinateTransformation::DestroyCT( poCT ); return true; }
CoordinateTransformation::CoordinateTransformation( OGRSpatialReference* srcSRS, OGRSpatialReference* dstSRS) { if (nullptr != srcSRS && nullptr != dstSRS && !srcSRS->IsSame(dstSRS)) { m_oCT = static_cast<OGRCoordinateTransformation*>( OGRCreateCoordinateTransformation(srcSRS, dstSRS)); } else { m_oCT = nullptr; } }
CPLErr HDF5ImageDataset::CreateODIMH5Projection() { const char* const pszProj4String = GetMetadataItem("where_projdef"); const char* const pszLL_lon = GetMetadataItem("where_LL_lon"); const char* const pszLL_lat = GetMetadataItem("where_LL_lat"); const char* const pszUR_lon = GetMetadataItem("where_UR_lon"); const char* const pszUR_lat = GetMetadataItem("where_UR_lat"); if( pszProj4String == NULL || pszLL_lon == NULL || pszLL_lat == NULL || pszUR_lon == NULL || pszUR_lat == NULL ) return CE_Failure; if( oSRS.importFromProj4( pszProj4String ) != OGRERR_NONE ) return CE_Failure; OGRSpatialReference oSRSWGS84; oSRSWGS84.SetWellKnownGeogCS( "WGS84" ); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation( &oSRSWGS84, &oSRS ); if( poCT == NULL ) return CE_Failure; /* Reproject corners from long,lat WGS84 to the target SRS */ double dfLLX = CPLAtof(pszLL_lon); double dfLLY = CPLAtof(pszLL_lat); double dfURX = CPLAtof(pszUR_lon); double dfURY = CPLAtof(pszUR_lat); if( !poCT->Transform(1, &dfLLX, &dfLLY) || !poCT->Transform(1, &dfURX, &dfURY) ) { delete poCT; return CE_Failure; } delete poCT; /* Compute the geotransform now */ const double dfPixelX = (dfURX - dfLLX) / nRasterXSize; const double dfPixelY = (dfURY - dfLLY) / nRasterYSize; bHasGeoTransform = true; adfGeoTransform[0] = dfLLX; adfGeoTransform[1] = dfPixelX; adfGeoTransform[2] = 0; adfGeoTransform[3] = dfURY; adfGeoTransform[4] = 0; adfGeoTransform[5] = -dfPixelY; CPLFree( pszProjection ); oSRS.exportToWkt( &pszProjection ); return CE_None; }
/** Fetch the center of a domain. * Fetch the center of a domain from any valid GDAL dataset * @param poDS a pointer to a valid GDAL Dataset * @param centerLonLat a pointer to a double size of double * 2 * @return true on valid population of the double* */ bool GDALGetCenter( GDALDataset *poDS, double *centerLonLat ) { char* pszPrj; double adfGeoTransform[6]; int xSize, ySize; double xCenter, yCenter; double lon, lat; OGRSpatialReference oSourceSRS, oTargetSRS; OGRCoordinateTransformation *poCT; if( poDS == NULL ) return false; xSize = poDS->GetRasterXSize( ); ySize = poDS->GetRasterYSize( ); if( poDS->GetGeoTransform( adfGeoTransform ) != CE_None ) return false; if( poDS->GetProjectionRef( ) == NULL ) return false; else pszPrj = (char*)poDS->GetProjectionRef(); oSourceSRS.importFromWkt( &pszPrj ); oTargetSRS.SetWellKnownGeogCS( "WGS84" ); poCT = OGRCreateCoordinateTransformation( &oSourceSRS, &oTargetSRS ); if( poCT == NULL ) return false; xCenter = xSize / 2; yCenter = ySize / 2; lon = adfGeoTransform[0] + adfGeoTransform[1] * xCenter + adfGeoTransform[2] * yCenter; lat = adfGeoTransform[3] + adfGeoTransform[4] * xCenter + adfGeoTransform[5] * yCenter; if( !poCT->Transform( 1, &lon, &lat ) ) { OGRCoordinateTransformation::DestroyCT( poCT ); return false; } centerLonLat[0] = lon; centerLonLat[1] = lat; OGRCoordinateTransformation::DestroyCT( poCT ); return true; }
void OGRUnionLayer::AutoWarpLayerIfNecessary(int iLayer) { if( !pabCheckIfAutoWrap[iLayer] ) { pabCheckIfAutoWrap[iLayer] = TRUE; OGRSpatialReference* poSRS = GetSpatialRef(); if( poSRS != NULL ) poSRS->Reference(); OGRSpatialReference* poSRS2 = papoSrcLayers[iLayer]->GetSpatialRef(); if( (poSRS == NULL && poSRS2 != NULL) || (poSRS != NULL && poSRS2 == NULL) ) { CPLError(CE_Warning, CPLE_AppDefined, "SRS of layer %s not consistant with layer SRS", papoSrcLayers[iLayer]->GetName()); } else if (poSRS != NULL && poSRS2 != NULL && poSRS != poSRS2 && !poSRS->IsSame(poSRS2)) { CPLDebug("VRT", "SRS of layer %s not consistant with layer SRS. " "Trying auto warping", papoSrcLayers[iLayer]->GetName()); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation( poSRS2, poSRS ); OGRCoordinateTransformation* poReversedCT = (poCT != NULL) ? OGRCreateCoordinateTransformation( poSRS, poSRS2 ) : NULL; if( poCT != NULL && poReversedCT != NULL ) papoSrcLayers[iLayer] = new OGRWarpedLayer( papoSrcLayers[iLayer], TRUE, poCT, poReversedCT); } if( poSRS != NULL ) poSRS->Release(); } }
CDEHandlerPass2(const std::string& outfile, const stringv& fields_nodes, const stringv& fields_ways, stringv fields_areas, const transmap& tm) : m_fields_nodes(fields_nodes), m_fields_ways(fields_ways), m_fields_areas(fields_areas), m_translations(tm), m_srs_wgs84(), m_srs_out() { last_area_seen = 0; if (m_srs_wgs84.SetWellKnownGeogCS("WGS84") != OGRERR_NONE) { std::cerr << "Can't initalize WGS84 SRS\n"; exit(1); } if (m_srs_out.importFromEPSG(3857) != OGRERR_NONE) { // if configuration for EPSG:3857 is not found, fall back to hard-coded one if (m_srs_out.importFromProj4("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs <>") != OGRERR_NONE) { std::cerr << "Can't initialize output SRS\n"; exit(1); } } m_transformation = OGRCreateCoordinateTransformation(&m_srs_wgs84, &m_srs_out); if (!m_transformation) { std::cerr << "Can't create coordinate transformation\n"; exit(1); } init_ogr(outfile); m_highway2z["minor"] = 3; m_highway2z["road"] = 3; m_highway2z["unclassified"] = 3; m_highway2z["residential"] = 3; m_highway2z["tertiary_link"] = 4; m_highway2z["tertiary"] = 4; m_highway2z["secondary_link"] = 6; m_highway2z["secondary"] = 6; m_highway2z["primary_link"] = 7; m_highway2z["primary"] = 7; m_highway2z["trunk_link"] = 8; m_highway2z["trunk"] = 8; m_highway2z["motorway_link"] = 9; m_highway2z["motorway"] = 9; }
OGRLayer* OGRGeoJSONSeqDataSource::ICreateLayer( const char* pszNameIn, OGRSpatialReference* poSRS, OGRwkbGeometryType /*eGType*/, char** papszOptions ) { if( nullptr == m_fpOut ) { CPLError(CE_Failure, CPLE_NotSupported, "GeoJSONSeq driver doesn't support creating a layer " "on a read-only datasource"); return nullptr; } if( m_poLayer.get() ) { CPLError(CE_Failure, CPLE_NotSupported, "GeoJSONSeq driver doesn't support creating more than one layer"); return nullptr; } OGRCoordinateTransformation* poCT = nullptr; if( poSRS == nullptr ) { CPLError(CE_Warning, CPLE_AppDefined, "No SRS set on layer. Assuming it is long/lat on WGS84 ellipsoid"); } else { OGRSpatialReference oSRSWGS84; oSRSWGS84.SetWellKnownGeogCS( "WGS84" ); if( !poSRS->IsSame(&oSRSWGS84) ) { poCT = OGRCreateCoordinateTransformation( poSRS, &oSRSWGS84 ); if( poCT == nullptr ) { CPLError( CE_Warning, CPLE_AppDefined, "Failed to create coordinate transformation between the " "input coordinate system and WGS84." ); return nullptr; } } } m_poLayer.reset( new OGRGeoJSONSeqWriteLayer( this, pszNameIn, papszOptions, poCT )); return m_poLayer.get(); }
void MapReprojector::reproject(const shared_ptr<Geometry>& g, const shared_ptr<OGRSpatialReference>& srs1, const shared_ptr<OGRSpatialReference>& srs2) { OGRCoordinateTransformation* t(OGRCreateCoordinateTransformation(srs1.get(), srs2.get())); if (t == 0) { throw HootException(QString("Error creating transformation object: ") + CPLGetLastErrorMsg()); } ReprojectCoordinateFilter filter(t); g->apply_rw(&filter); OGRCoordinateTransformation::DestroyCT(t); }
void MapReprojector::reproject(shared_ptr<OsmMap> map, shared_ptr<OGRSpatialReference> ref) { shared_ptr<OGRSpatialReference> sourceSrs = map->getProjection(); OGRCoordinateTransformation* t(OGRCreateCoordinateTransformation(sourceSrs.get(), ref.get())); if (t == 0) { throw HootException(QString("Error creating transformation object: ") + CPLGetLastErrorMsg()); } ReprojectCoordinateFilter rcf(t); int count = 0; const NodeMap& nodes = map->getNodeMap(); for (NodeMap::const_iterator it = nodes.begin(); it != nodes.end(); ++it) { Node* n = it->second.get(); Coordinate c = n->toCoordinate(); try { rcf.project(&c); } catch(IllegalArgumentException& e) { LOG_WARN("Failure projecting node: " << n->toString()); throw e; } n->setX(c.x); n->setY(c.y); if (count % 1000 == 0 && Log::getInstance().isInfoEnabled()) { cout << "Reprojecting " << count << " / " << nodes.size() << " \r"; cout.flush(); } count++; } if (Log::getInstance().isInfoEnabled()) { cout << endl; } map->setProjection(ref); OGRCoordinateTransformation::DestroyCT(t); }
Coordinate MapReprojector::reproject(const Coordinate& c, shared_ptr<OGRSpatialReference> srs1, shared_ptr<OGRSpatialReference> srs2) { OGRCoordinateTransformation* t(OGRCreateCoordinateTransformation(srs1.get(), srs2.get())); if (t == 0) { throw HootException(QString("Error creating transformation object: ") + CPLGetLastErrorMsg()); } Coordinate result; result.x = c.x; result.y = c.y; ReprojectCoordinateFilter(t).project(&result); OGRCoordinateTransformation::DestroyCT(t); return result; }
OGRCoordinateTransformation* OGRSQLiteExtensionData::GetTransform(int nSrcSRSId, int nDstSRSId) { std::map< std::pair<int,int>, OGRCoordinateTransformation*>::iterator oIter = oCachedTransformsMap.find(std::pair<int,int>(nSrcSRSId, nDstSRSId)); if( oIter == oCachedTransformsMap.end() ) { OGRCoordinateTransformation* poCT = NULL; OGRSpatialReference oSrcSRS, oDstSRS; if (oSrcSRS.importFromEPSG(nSrcSRSId) == OGRERR_NONE && oDstSRS.importFromEPSG(nDstSRSId) == OGRERR_NONE ) { poCT = OGRCreateCoordinateTransformation( &oSrcSRS, &oDstSRS ); } oCachedTransformsMap[std::pair<int,int>(nSrcSRSId, nDstSRSId)] = poCT; return poCT; } else return oIter->second; }
void Convert_Geographic_To_UTM( const int& number_coordinates, const double* latitudes_degrees, const double* longitudes_degrees, const std::string& input_datum, const int& grid_zone, const bool& is_northern, double* eastings_meters, double* northings_meters, const std::string& output_datum ) { // Create the Spatial Reference Objects OGRSpatialReference sourceSRS, targetSRS; sourceSRS.SetWellKnownGeogCS( input_datum.c_str() ); targetSRS.SetWellKnownGeogCS( output_datum.c_str() ); // Configure the Projected Coordinate Components targetSRS.SetUTM( grid_zone, is_northern ); // Build the Transform Engine OGRCoordinateTransformation* transform; transform = OGRCreateCoordinateTransformation( &sourceSRS, &targetSRS ); double* elevations_meters = NULL; memcpy( eastings_meters, longitudes_degrees, sizeof(double)*number_coordinates ); memcpy( northings_meters, latitudes_degrees, sizeof(double)*number_coordinates ); if( !transform->Transform( number_coordinates, eastings_meters, northings_meters, NULL )) { throw std::runtime_error("Transformation Failed."); } // Destroy the Transform OCTDestroyCoordinateTransformation( transform ); }
/** * @param matchStatus If the element's status matches this status then it is checked for a match. */ PlacesPoiMatchVisitor(const ConstOsmMapPtr& map, vector<const Match*>& result, Meters worstCircularError, const Envelope& bounds, ConstMatchThresholdPtr threshold) : _map(map), _result(result), _bounds(bounds), _threshold(threshold) { _worstCircularError = worstCircularError; _neighborCountMax = -1; _neighborCountSum = 0; _elementsEvaluated = 0; _maxGroupSize = 0; _rejectScore = 0.9; _buildIndex(); // used for bounds checks. _transform.reset(OGRCreateCoordinateTransformation(_map->getProjection().get(), OsmMap::getWgs84().get())); }
void Convert_Geographic_To_UTM( const double& latitude_degrees, const double& longitude_degrees, const std::string& input_datum, const int& grid_zone, const bool& is_northern, double& easting_meters, double& northing_meters, const std::string& output_datum ) { // Create the Spatial Reference Objects OGRSpatialReference sourceSRS, targetSRS; sourceSRS.SetWellKnownGeogCS( input_datum.c_str() ); targetSRS.SetWellKnownGeogCS( output_datum.c_str() ); // Configure the Projected Coordinate Components targetSRS.SetUTM( grid_zone, is_northern ); // Build the Transform Engine OGRCoordinateTransformation* transform; transform = OGRCreateCoordinateTransformation( &sourceSRS, &targetSRS ); easting_meters = longitude_degrees; northing_meters = latitude_degrees; double output_elevation_meters = 0; if( !transform->Transform( 1, &easting_meters, &northing_meters, &output_elevation_meters ) ) { throw std::runtime_error("Transformation Failed."); } // Destroy the Transform OCTDestroyCoordinateTransformation( transform ); }
// [[Rcpp::export]] Rcpp::List CPL_transform(Rcpp::List sfc, Rcpp::CharacterVector proj4) { // import proj4string: OGRSpatialReference *dest = new OGRSpatialReference; handle_error(dest->importFromProj4((const char *) (proj4[0]))); // transform geometries: std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); if (g.size() == 0) { dest->Release(); // #nocov Rcpp::stop("CPL_transform: zero length geometry list"); // #nocov } OGRCoordinateTransformation *ct = OGRCreateCoordinateTransformation(g[0]->getSpatialReference(), dest); if (ct == NULL) { dest->Release(); // #nocov Rcpp::stop("OGRCreateCoordinateTransformation() returned NULL: PROJ.4 available?"); // #nocov } for (size_t i = 0; i < g.size(); i++) { CPLPushErrorHandler(CPLQuietErrorHandler); OGRErr err = 0; if (! g[i]->IsEmpty()) err = g[i]->transform(ct); CPLPopErrorHandler(); if (err == 1 || err == 6) { OGRwkbGeometryType geomType = g[i]->getGeometryType(); OGRGeometryFactory f; f.destroyGeometry(g[i]); g[i] = f.createGeometry(geomType); } else handle_error(err); } Rcpp::List ret = sfc_from_ogr(g, true); // destroys g; ct->DestroyCT(ct); dest->Release(); return ret; }
void setSourceSRS(const std::string& sourceURN) { if (m_transformation != nullptr) { OCTDestroyCoordinateTransformation(m_transformation); m_transformation = nullptr; } OGRSpatialReference sourceSRS; OGRErr err = sourceSRS.SetFromUserInput(sourceURN.c_str()); if (err != OGRERR_NONE) { CITYGML_LOG_ERROR(m_logger, "Could not create OGRSpatialReference for source SRS " << sourceURN << ". OGR error code: " << err << "."); return; } m_transformation = OGRCreateCoordinateTransformation(&sourceSRS, &m_destSRS); if (m_transformation == nullptr) { CITYGML_LOG_ERROR(m_logger, "Could not create transformation from source SRS " << sourceURN << " to destination SRS " << m_destSRSURN << "."); return; } m_sourceURN = sourceURN; }
OGRErr OGRGeometry::transformTo( OGRSpatialReference *poSR ) { #ifdef DISABLE_OGRGEOM_TRANSFORM return OGRERR_FAILURE; #else OGRCoordinateTransformation *poCT; OGRErr eErr; if( getSpatialReference() == NULL || poSR == NULL ) return OGRERR_FAILURE; poCT = OGRCreateCoordinateTransformation( getSpatialReference(), poSR ); if( poCT == NULL ) return OGRERR_FAILURE; eErr = transform( poCT ); delete poCT; return eErr; #endif }
bool OGRPointToLatLon(double &x, double &y, OGRDataSourceH hDS, const char *datum) { char *pszPrj = NULL; OGRSpatialReference *poSrcSRS; OGRSpatialReference oSourceSRS, oTargetSRS; OGRCoordinateTransformation *poCT; if (hDS == NULL) { return false; } OGRLayer *poLayer; poLayer = (OGRLayer *)OGR_DS_GetLayer(hDS, 0); poLayer->ResetReading(); poSrcSRS = poLayer->GetSpatialRef(); if (poSrcSRS == NULL) { return false; } oTargetSRS.SetWellKnownGeogCS(datum); poCT = OGRCreateCoordinateTransformation(poSrcSRS, &oTargetSRS); if (poCT == NULL) { return false; } if (!poCT->Transform(1, &x, &y)) { OGRCoordinateTransformation::DestroyCT(poCT); return false; } OGRCoordinateTransformation::DestroyCT(poCT); return true; }
OGREnvelope wxGISRasterRGBRenderer::TransformEnvelope(OGREnvelope* pEnvelope, OGRSpatialReference* pSrsSpatialReference, OGRSpatialReference* pDstSpatialReference) { //get new envelope - it may rotate OGRCoordinateTransformation *poCT = OGRCreateCoordinateTransformation( pSrsSpatialReference, pDstSpatialReference); double pointsx[4]; double pointsy[4]; pointsx[0] = pEnvelope->MaxX; pointsy[0] = pEnvelope->MaxY; pointsx[1] = pEnvelope->MinX; pointsy[1] = pEnvelope->MinY; pointsx[2] = pEnvelope->MaxX; pointsy[2] = pEnvelope->MinY; pointsx[3] = pEnvelope->MinX; pointsy[3] = pEnvelope->MaxY; //get real envelope poCT->Transform(4, pointsx, pointsy); OCTDestroyCoordinateTransformation(poCT); OGREnvelope out; out.MinX = MIN(pointsx[0], MIN(pointsx[1], MIN(pointsx[2], pointsx[3]))); out.MaxX = MAX(pointsx[0], MAX(pointsx[1], MAX(pointsx[2], pointsx[3]))); out.MinY = MIN(pointsy[0], MIN(pointsy[1], MIN(pointsy[2], pointsy[3]))); out.MaxY = MAX(pointsy[0], MAX(pointsy[1], MAX(pointsy[2], pointsy[3]))); return out; }
/** Test the spatial reference of an image. * @param poDS a pointer to a valid GDALDataset * @return true if the spatial reference is valid */ bool GDALTestSRS( GDALDataset *poDS ) { char* pszPrj; OGRSpatialReference oSourceSRS, oTargetSRS; OGRCoordinateTransformation *poCT; if( poDS == NULL ) return false; if( poDS->GetProjectionRef() == NULL ) return false; else pszPrj = (char*) poDS->GetProjectionRef(); oSourceSRS.importFromWkt( &pszPrj ); oTargetSRS.SetWellKnownGeogCS( "WGS84" ); poCT = OGRCreateCoordinateTransformation( &oSourceSRS, &oTargetSRS ); if( poCT == NULL ) return false; OGRCoordinateTransformation::DestroyCT( poCT ); return true; }
GTMTrackLayer::GTMTrackLayer( const char* pszNameIn, OGRSpatialReference *poSRSIn, int /* bWriterIn */, OGRGTMDataSource* poDSIn ) { poCT = nullptr; /* We are implementing just WGS84, although GTM supports other datum formats. */ if( poSRSIn != nullptr ) { poSRS = new OGRSpatialReference(nullptr); poSRS->SetWellKnownGeogCS( "WGS84" ); if (!poSRS->IsSame(poSRSIn)) { poCT = OGRCreateCoordinateTransformation( poSRSIn, poSRS ); if( poCT == nullptr && poDSIn->isFirstCTError() ) { /* If we can't create a transformation, issue a warning - but continue the transformation*/ char *pszWKT = nullptr; poSRSIn->exportToPrettyWkt( &pszWKT, FALSE ); CPLError( CE_Warning, CPLE_AppDefined, "Failed to create coordinate transformation between " "the\n" "input coordinate system and WGS84. This may be " "because they\n" "are not transformable, or because projection " "services\n" "(PROJ.4 DLL/.so) could not be loaded.\n" "This message will not be issued any more. \n" "\nSource:\n%s", pszWKT ); CPLFree( pszWKT ); poDSIn->issuedFirstCTError(); } } } else { poSRS = nullptr; } poDS = poDSIn; nNextFID = 0; nTotalFCount = poDS->getNTracks(); pszName = CPLStrdup(pszNameIn); poFeatureDefn = new OGRFeatureDefn( pszName ); SetDescription( poFeatureDefn->GetName() ); poFeatureDefn->Reference(); poFeatureDefn->SetGeomType ( wkbLineString ); poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(poSRS); /* We implement just name, type, and color for tracks, if others needed feel free to append more parameters and implement the code */ OGRFieldDefn oFieldName( "name", OFTString ); poFeatureDefn->AddFieldDefn( &oFieldName ); OGRFieldDefn oFieldTrackType( "type", OFTInteger ); poFeatureDefn->AddFieldDefn( &oFieldTrackType ); OGRFieldDefn oFieldColor( "color", OFTInteger ); poFeatureDefn->AddFieldDefn( &oFieldColor ); }