📄 ogrct.cpp
字号:
/** * Create transformation object. * * This is the same as the C function OCTNewCoordinateTransformation(). * * The delete operator, or OCTDestroyCoordinateTransformation() should * be used to destroy transformation objects. * * @param poSource source spatial reference system. * @param poTarget target spatial reference system. * @return NULL on failure or a ready to use transformation object. */OGRCoordinateTransformation*OGRCreateCoordinateTransformation( OGRSpatialReference *poSource, OGRSpatialReference *poTarget ){ OGRProj4CT *poCT; if( !LoadProjLibrary() ) { CPLError( CE_Failure, CPLE_NotSupported, "Unable to load PROJ.4 library (%s), creation of\n" "OGRCoordinateTransformation failed.", LIBNAME ); return NULL; } poCT = new OGRProj4CT(); if( !poCT->Initialize( poSource, poTarget ) ) { delete poCT; return NULL; } else { return poCT; }}/************************************************************************//* OCTNewCoordinateTransformation() *//************************************************************************/OGRCoordinateTransformationH OCTNewCoordinateTransformation( OGRSpatialReferenceH hSourceSRS, OGRSpatialReferenceH hTargetSRS ){ return (OGRCoordinateTransformationH) OGRCreateCoordinateTransformation( (OGRSpatialReference *) hSourceSRS, (OGRSpatialReference *) hTargetSRS );}/************************************************************************//* OGRProj4CT() *//************************************************************************/OGRProj4CT::OGRProj4CT(){ poSRSSource = NULL; poSRSTarget = NULL; psPJSource = NULL; psPJTarget = NULL; nErrorCount = 0;}/************************************************************************//* ~OGRProj4CT() *//************************************************************************/OGRProj4CT::~OGRProj4CT(){ if( poSRSSource != NULL ) delete poSRSSource; if( poSRSTarget != NULL ) delete poSRSTarget; if( psPJSource != NULL ) pfn_pj_free( psPJSource ); if( psPJTarget != NULL ) pfn_pj_free( psPJTarget );}/************************************************************************//* Initialize() *//************************************************************************/int OGRProj4CT::Initialize( OGRSpatialReference * poSourceIn, OGRSpatialReference * poTargetIn ){ poSRSSource = poSourceIn->Clone(); poSRSTarget = poTargetIn->Clone(); bSourceLatLong = poSRSSource->IsGeographic(); bTargetLatLong = poSRSTarget->IsGeographic();/* -------------------------------------------------------------------- *//* Setup source and target translations to radians for lat/long *//* systems. *//* -------------------------------------------------------------------- */ dfSourceToRadians = DEG_TO_RAD; dfSourceFromRadians = RAD_TO_DEG; if( bSourceLatLong ) { OGR_SRSNode *poUNITS = poSRSSource->GetAttrNode( "GEOGCS|UNIT" ); if( poUNITS && poUNITS->GetChildCount() >= 2 ) { dfSourceToRadians = atof(poUNITS->GetChild(1)->GetValue()); if( dfSourceToRadians == 0.0 ) dfSourceToRadians = DEG_TO_RAD; else dfSourceFromRadians = 1 / dfSourceToRadians; } } dfTargetToRadians = DEG_TO_RAD; dfTargetFromRadians = RAD_TO_DEG; if( bTargetLatLong ) { OGR_SRSNode *poUNITS = poSRSTarget->GetAttrNode( "GEOGCS|UNIT" ); if( poUNITS && poUNITS->GetChildCount() >= 2 ) { dfTargetToRadians = atof(poUNITS->GetChild(1)->GetValue()); if( dfTargetToRadians == 0.0 ) dfTargetToRadians = DEG_TO_RAD; else dfTargetFromRadians = 1 / dfTargetToRadians; } }/* -------------------------------------------------------------------- *//* Establish PROJ.4 handle for source if projection. *//* -------------------------------------------------------------------- */ char *pszProj4Defn; if( poSRSSource->exportToProj4( &pszProj4Defn ) != OGRERR_NONE ) return FALSE; psPJSource = pfn_pj_init_plus( pszProj4Defn ); if( psPJSource == NULL ) { if( pfn_pj_get_errno_ref != NULL && pfn_pj_strerrno != NULL ) { int *p_pj_errno = pfn_pj_get_errno_ref(); CPLError( CE_Failure, CPLE_NotSupported, "Failed to initialize PROJ.4 with `%s'.\n%s", pszProj4Defn, pfn_pj_strerrno(*p_pj_errno) ); } else { CPLError( CE_Failure, CPLE_NotSupported, "Failed to initialize PROJ.4 with `%s'.\n", pszProj4Defn ); } } CPLFree( pszProj4Defn ); if( psPJSource == NULL ) return FALSE;/* -------------------------------------------------------------------- *//* Establish PROJ.4 handle for target if projection. *//* -------------------------------------------------------------------- */ if( poSRSTarget->exportToProj4( &pszProj4Defn ) != OGRERR_NONE ) return FALSE; psPJTarget = pfn_pj_init_plus( pszProj4Defn ); if( psPJTarget == NULL ) CPLError( CE_Failure, CPLE_NotSupported, "Failed to initialize PROJ.4 with `%s'.", pszProj4Defn ); CPLFree( pszProj4Defn ); if( psPJTarget == NULL ) return FALSE; return TRUE;}/************************************************************************//* GetSourceCS() *//************************************************************************/OGRSpatialReference *OGRProj4CT::GetSourceCS(){ return poSRSSource;}/************************************************************************//* GetTargetCS() *//************************************************************************/OGRSpatialReference *OGRProj4CT::GetTargetCS(){ return poSRSTarget;}/************************************************************************//* Transform() *//************************************************************************/int OGRProj4CT::Transform( int nCount, double *x, double *y, double *z ){ int err, i;/* -------------------------------------------------------------------- *//* Potentially transform to radians. *//* -------------------------------------------------------------------- */ if( bSourceLatLong ) { for( i = 0; i < nCount; i++ ) { x[i] *= dfSourceToRadians; y[i] *= dfSourceToRadians; } }/* -------------------------------------------------------------------- *//* Do the transformation using PROJ.4. *//* -------------------------------------------------------------------- */ err = pfn_pj_transform( psPJSource, psPJTarget, nCount, 1, x, y, z );/* -------------------------------------------------------------------- *//* Try to report an error through CPL. Get proj.4 error string *//* if possible. Try to avoid reporting thousands of error *//* ... supress further error reporting on this OGRProj4CT if we *//* have already reported 20 errors. *//* -------------------------------------------------------------------- */ if( err != 0 ) { if( ++nErrorCount < 20 ) { const char *pszError = NULL; if( pfn_pj_strerrno != NULL ) pszError = pfn_pj_strerrno( err ); if( pszError == NULL ) CPLError( CE_Failure, CPLE_AppDefined, "Reprojection failed, err = %d", err ); else CPLError( CE_Failure, CPLE_AppDefined, "%s", pszError ); } else if( nErrorCount == 20 ) { CPLError( CE_Failure, CPLE_AppDefined, "Reprojection failed, err = %d, further errors will be supressed on the transform object.", err ); } return FALSE; }/* -------------------------------------------------------------------- *//* Potentially transform back to degrees. *//* -------------------------------------------------------------------- */ if( bTargetLatLong ) { for( i = 0; i < nCount; i++ ) { x[i] *= dfTargetFromRadians; y[i] *= dfTargetFromRadians; } } return TRUE;}/************************************************************************//* OCTTransform() *//************************************************************************/int OCTTransform( OGRCoordinateTransformationH hTransform, int nCount, double *x, double *y, double *z ){ return ((OGRCoordinateTransformation*) hTransform)-> Transform( nCount, x, y,z );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -