📄 ogr_srs_xml.cpp
字号:
poUnits->AddChild( new OGR_SRSNode( SRS_UA_DEGREE_CONV ) ); } else { poUnits->AddChild( new OGR_SRSNode( SRS_UL_METER ) ); poUnits->AddChild( new OGR_SRSNode( "1.0" ) ); } poNode->AddChild( poUnits );}#endif/************************************************************************//* importXMLAuthority() *//************************************************************************/static void importXMLAuthority( CPLXMLNode *psSrcXML, OGRSpatialReference *poSRS, const char *pszSourceKey, const char *pszTargetKey ){ CPLXMLNode *psIDNode = CPLGetXMLNode( psSrcXML, pszSourceKey ); if( psIDNode == NULL ) return; if( CPLGetXMLNode( psIDNode, "code" ) == NULL || CPLGetXMLNode( psIDNode, "codeSpace" ) == NULL ) return; poSRS->SetAuthority( pszTargetKey, CPLGetXMLValue(psIDNode,"codeSpace",""), atoi(CPLGetXMLValue(psIDNode,"code","0")));}/************************************************************************//* getNormalizedValue() *//* *//* Parse a node to get it's numerical value, and then normalize *//* into meters of degrees depending on the measure type. *//************************************************************************/static double getNormalizedValue( CPLXMLNode *psNode, const char *pszPath, const char * /*pszMeasure*/, double dfDefault ){ CPLXMLNode *psTargetNode; CPLXMLNode *psValueNode; if( pszPath == NULL || strlen(pszPath) == 0 ) psTargetNode = psNode; else psTargetNode = CPLGetXMLNode( psNode, pszPath ); if( psTargetNode == NULL ) return dfDefault; for( psValueNode = psTargetNode->psChild; psValueNode != NULL && psValueNode->eType != CXT_Text; psValueNode = psValueNode->psNext ) {} if( psValueNode == NULL ) return dfDefault; // Add normalization later. return atof(psValueNode->pszValue);}/************************************************************************//* importGeogCSFromXML() *//************************************************************************/static OGRErr importGeogCSFromXML( OGRSpatialReference *poSRS, CPLXMLNode *psCRS ){ const char *pszGeogName, *pszDatumName, *pszEllipsoidName, *pszPMName; double dfSemiMajor, dfInvFlattening, dfPMOffset = 0.0; pszGeogName = CPLGetXMLValue( psCRS, "srsName", "Unnamed GeogCS" );/* -------------------------------------------------------------------- *//* Get datum name. *//* -------------------------------------------------------------------- */ CPLXMLNode *psDatum; psDatum = CPLGetXMLNode( psCRS, "usesGeodeticDatum.GeodeticDatum" ); pszDatumName = CPLGetXMLValue( psDatum, "datumName", "Unnamed Datum" );/* -------------------------------------------------------------------- *//* Get ellipsoid information. *//* -------------------------------------------------------------------- */ CPLXMLNode *psE; psE = CPLGetXMLNode( psDatum, "usesEllipsoid.Ellipsoid" ); pszEllipsoidName = CPLGetXMLValue( psE, "ellipsoidName", "Unnamed Ellipsoid" ); dfSemiMajor = getNormalizedValue( psE, "semiMajorAxis", "Linear", SRS_WGS84_SEMIMAJOR ); dfInvFlattening = getNormalizedValue( psE, "secondDefiningParameter.inverseFlattening", "Unitless", 0.0 ); if( dfInvFlattening == 0.0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Ellipsoid inverseFlattening corrupt or missing." ); return OGRERR_CORRUPT_DATA; }/* -------------------------------------------------------------------- *//* Get the prime meridian. *//* -------------------------------------------------------------------- */ CPLXMLNode *psPM; psPM = CPLGetXMLNode( psDatum, "usesPrimeMeridian.PrimeMeridian" ); if( psPM == NULL ) { pszPMName = "Greenwich"; dfPMOffset = 0.0; } else { pszPMName = CPLGetXMLValue( psPM, "meridianName", "Unnamed Prime Meridian"); dfPMOffset = getNormalizedValue( psPM, "greenwichLongitude.angle", "Angular", 0.0 ); }/* -------------------------------------------------------------------- *//* Set the geographic definition. *//* -------------------------------------------------------------------- */ poSRS->SetGeogCS( pszGeogName, pszDatumName, pszEllipsoidName, dfSemiMajor, dfInvFlattening, pszPMName, dfPMOffset );/* -------------------------------------------------------------------- *//* Look for angular units. We don't check that all axes match *//* at this time. *//* -------------------------------------------------------------------- */#ifdef notdef CPLXMLNode *psAxis; psAxis = CPLGetXMLNode( psGeo2DCRS, "EllipsoidalCoordinateSystem.CoordinateAxis" ); importXMLUnits( psAxis, "AngularUnit", poSRS, "GEOGCS" );#endif/* -------------------------------------------------------------------- *//* Can we set authorities for any of the levels? *//* -------------------------------------------------------------------- */ importXMLAuthority( psCRS, poSRS, "srsID", "GEOGCS" ); importXMLAuthority( psDatum, poSRS, "datumID", "GEOGCS|DATUM" ); importXMLAuthority( psE, poSRS, "ellipsoidID", "GEOGCS|DATUM|SPHEROID" ); importXMLAuthority( psDatum, poSRS, "usesPrimeMeridian.PrimeMeridian.meridianID", "GEOGCS|PRIMEM" ); poSRS->Fixup(); return OGRERR_NONE;}/************************************************************************//* importProjCSFromXML() *//************************************************************************/static OGRErr importProjCSFromXML( OGRSpatialReference *poSRS, CPLXMLNode *psCRS ){ CPLXMLNode *psSubXML; OGRErr eErr;/* -------------------------------------------------------------------- *//* Setup the PROJCS node with a name. *//* -------------------------------------------------------------------- */ poSRS->SetProjCS( CPLGetXMLValue( psCRS, "srsName", "Unnamed" ) ); importXMLAuthority( psCRS, poSRS, "srsID", "PROJCS" );/* -------------------------------------------------------------------- *//* Try to set the GEOGCS info. *//* -------------------------------------------------------------------- */ psSubXML = CPLGetXMLNode( psCRS, "baseCRS.GeographicCRS" ); if( psSubXML != NULL ) { eErr = importGeogCSFromXML( poSRS, psSubXML ); if( eErr != OGRERR_NONE ) return eErr; }/* -------------------------------------------------------------------- *//* Get the conversion node. It should be the only child of the *//* definedByConversion node. *//* -------------------------------------------------------------------- */ CPLXMLNode *psConv = NULL; psSubXML = CPLGetXMLNode( psCRS, "definedByConversion" ); if( psSubXML != NULL ) psConv = psSubXML->psChild; if( psConv == NULL || psConv->eType != CXT_Element ) { CPLError( CE_Failure, CPLE_AppDefined, "Unable to find a conversion node under the definedByConversion\n" "node of the ProjectedCRS." ); return OGRERR_CORRUPT_DATA; }/* -------------------------------------------------------------------- *//* Transverse Mercator. *//* -------------------------------------------------------------------- */ if( EQUAL(psConv->pszValue,"TransverseMercatorConversion") ) { poSRS->SetTM( getNormalizedValue( psConv, "usesLatitudeOfNaturalOriginValue.value", "Linear", 0.0 ), getNormalizedValue( psConv, "usesLongitudeOfNaturalOriginValue.value", "Angular", 0.0), getNormalizedValue( psConv, "usesScaleFactorAtNaturalOriginValue.value", "Unitless", 1.0), getNormalizedValue( psConv, "usesFalseEastingValue.value", "Linear", 0.0), getNormalizedValue( psConv, "usesFalseNorthingValue.value", "Linear", 0.0) ); }/* -------------------------------------------------------------------- *//* Didn't recognise? *//* -------------------------------------------------------------------- */ else { CPLError( CE_Failure, CPLE_AppDefined, "Conversion %s not recognised.", psConv->pszValue ); return OGRERR_CORRUPT_DATA; }/* -------------------------------------------------------------------- *//* Cleanup and return. *//* -------------------------------------------------------------------- */ poSRS->Fixup(); // Need to get linear units here! return OGRERR_NONE;}/************************************************************************//* importFromXML() *//************************************************************************/OGRErr OGRSpatialReference::importFromXML( const char *pszXML ){ CPLXMLNode *psTree; this->Clear(); /* -------------------------------------------------------------------- *//* Parse the XML. *//* -------------------------------------------------------------------- */ psTree = CPLParseXMLString( pszXML ); if( psTree == NULL ) return OGRERR_CORRUPT_DATA; CPLStripXMLNamespace( psTree, "gml", TRUE );/* -------------------------------------------------------------------- *//* Import according to the root node type. *//* -------------------------------------------------------------------- */ if( EQUAL(psTree->pszValue,"GeographicCRS") ) { return importGeogCSFromXML( this, psTree ); } if( EQUAL(psTree->pszValue,"ProjectedCRS") ) { return importProjCSFromXML( this, psTree ); } return OGRERR_UNSUPPORTED_SRS;}/************************************************************************//* OSRImportFromXML() *//************************************************************************/OGRErr OSRImportFromXML( OGRSpatialReferenceH hSRS, const char *pszXML ){ return ((OGRSpatialReference *) hSRS)->importFromXML( pszXML );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -