1 : /******************************************************************************
2 : * $Id: iso19115_srs.cpp 17985 2009-11-10 13:39:46Z rouault $
3 : *
4 : * Project: BAG Driver
5 : * Purpose: Implements code to parse ISO 19115 metadata to extract a
6 : * spatial reference system. Eventually intended to be made
7 : * a method on OGRSpatialReference.
8 : * Author: Frank Warmerdam <warmerdam@pobox.com>
9 : *
10 : ******************************************************************************
11 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
12 : *
13 : * Permission is hereby granted, free of charge, to any person obtaining a
14 : * copy of this software and associated documentation files (the "Software"),
15 : * to deal in the Software without restriction, including without limitation
16 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 : * and/or sell copies of the Software, and to permit persons to whom the
18 : * Software is furnished to do so, subject to the following conditions:
19 : *
20 : * The above copyright notice and this permission notice shall be included
21 : * in all copies or substantial portions of the Software.
22 : *
23 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 : * DEALINGS IN THE SOFTWARE.
30 : ****************************************************************************/
31 :
32 : #include "ogr_spatialref.h"
33 : #include "cpl_minixml.h"
34 : #include "cpl_error.h"
35 :
36 : CPL_CVSID("$Id: iso19115_srs.cpp 17985 2009-11-10 13:39:46Z rouault $");
37 :
38 : /************************************************************************/
39 : /* OGR_SRS_ImportFromISO19115() */
40 : /************************************************************************/
41 :
42 2 : OGRErr OGR_SRS_ImportFromISO19115( OGRSpatialReference *poThis,
43 : const char *pszISOXML )
44 :
45 : {
46 : /* -------------------------------------------------------------------- */
47 : /* Parse the XML into tree form. */
48 : /* -------------------------------------------------------------------- */
49 2 : CPLXMLNode *psRoot = CPLParseXMLString( pszISOXML );
50 :
51 2 : if( psRoot == NULL )
52 0 : return OGRERR_FAILURE;
53 :
54 2 : CPLStripXMLNamespace( psRoot, NULL, TRUE );
55 :
56 :
57 : /* -------------------------------------------------------------------- */
58 : /* For now we look for projection codes recognised in the BAG */
59 : /* format (see ons_fsd.pdf: Metadata Dataset Character String */
60 : /* Constants). */
61 : /* -------------------------------------------------------------------- */
62 2 : CPLXMLNode *psRSI = CPLSearchXMLNode( psRoot, "=referenceSystemInfo" );
63 2 : if( psRSI == NULL )
64 : {
65 : CPLError( CE_Failure, CPLE_AppDefined,
66 0 : "Unable to find <referenceSystemInfo> in metadata." );
67 0 : CPLDestroyXMLNode( psRoot );
68 0 : return OGRERR_FAILURE;
69 : }
70 :
71 2 : poThis->Clear();
72 :
73 : /* -------------------------------------------------------------------- */
74 : /* First, set the datum. */
75 : /* -------------------------------------------------------------------- */
76 : const char *pszDatum =
77 2 : CPLGetXMLValue( psRSI, "MD_CRS.datum.RS_Identifier.code", "" );
78 :
79 2 : if( strlen(pszDatum) > 0
80 : && poThis->SetWellKnownGeogCS( pszDatum ) != OGRERR_NONE )
81 : {
82 2 : CPLDestroyXMLNode( psRoot );
83 2 : return OGRERR_FAILURE;
84 : }
85 :
86 : /* -------------------------------------------------------------------- */
87 : /* Then try to extract the projection. */
88 : /* -------------------------------------------------------------------- */
89 : const char *pszProjection =
90 0 : CPLGetXMLValue( psRSI, "MD_CRS.projection.RS_Identifier.code", "" );
91 :
92 0 : if( EQUAL(pszProjection,"UTM") )
93 : {
94 0 : int nZone = atoi(CPLGetXMLValue( psRSI, "MD_CRS.projectionParameters.MD_ProjectionParameters.zone", "0" ));
95 :
96 0 : poThis->SetUTM( ABS(nZone), nZone > 0 );
97 : }
98 : else
99 : {
100 : CPLError( CE_Failure, CPLE_AppDefined,
101 : "projection = %s not recognised by ISO 19115 parser.",
102 0 : pszProjection );
103 0 : CPLDestroyXMLNode( psRoot );
104 0 : return OGRERR_FAILURE;
105 : }
106 :
107 0 : CPLDestroyXMLNode( psRoot );
108 :
109 0 : return OGRERR_NONE;
110 : }
111 :
|