1 /******************************************************************************
2 * $Id: ogr_srs_dict.cpp 11881 2007-08-13 18:03:48Z mloskot $
3 *
4 * Project: OpenGIS Simple Features Reference Implementation
5 * Purpose: Implement importFromDict() method to read a WKT SRS from a
6 * coordinate system dictionary in a simple text format.
7 * Author: Frank Warmerdam, warmerdam@pobox.com
8 *
9 ******************************************************************************
10 * Copyright (c) 2004, Frank Warmerdam
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 ****************************************************************************/
30
31 #include "ogr_spatialref.h"
32 #include "cpl_conv.h"
33
34 CPL_CVSID("$Id: ogr_srs_dict.cpp 11881 2007-08-13 18:03:48Z mloskot $");
35
36
37 /************************************************************************/
38 /* importFromDict() */
39 /************************************************************************/
40
41 /**
42 * Read SRS from WKT dictionary.
43 *
44 * This method will attempt to find the indicated coordinate system identity
45 * in the indicated dictionary file. If found, the WKT representation is
46 * imported and used to initialize this OGRSpatialReference.
47 *
48 * More complete information on the format of the dictionary files can
49 * be found in the epsg.wkt file in the GDAL data tree. The dictionary
50 * files are searched for in the "GDAL" domain using CPLFindFile(). Normally
51 * this results in searching /usr/local/share/gdal or somewhere similar.
52 *
53 * This method is the same as the C function OSRImportFromDict().
54 *
55 * @param pszDictFile the name of the dictionary file to load.
56 *
57 * @param pszCode the code to lookup in the dictionary.
58 *
59 * @return OGRERR_NONE on success, or OGRERR_SRS_UNSUPPORTED if the code isn't
60 * found, and OGRERR_SRS_FAILURE if something more dramatic goes wrong.
61 */
62
importFromDict(const char * pszDictFile,const char * pszCode)63 OGRErr OGRSpatialReference::importFromDict( const char *pszDictFile,
64 const char *pszCode )
65
66 {
67 const char *pszFilename;
68 FILE *fp;
69 OGRErr eErr = OGRERR_UNSUPPORTED_SRS;
70
71 /* -------------------------------------------------------------------- */
72 /* Find and open file. */
73 /* -------------------------------------------------------------------- */
74 pszFilename = CPLFindFile( "gdal", pszDictFile );
75 if( pszFilename == NULL )
76 return OGRERR_UNSUPPORTED_SRS;
77
78 fp = VSIFOpen( pszFilename, "rb" );
79 if( fp == NULL )
80 return OGRERR_UNSUPPORTED_SRS;
81
82 /* -------------------------------------------------------------------- */
83 /* Process lines. */
84 /* -------------------------------------------------------------------- */
85 const char *pszLine;
86
87 while( (pszLine = CPLReadLine(fp)) != NULL )
88
89 {
90 if( pszLine[0] == '#' )
91 /* do nothing */;
92
93 else if( EQUALN(pszLine,"include ",8) )
94 {
95 eErr = importFromDict( pszLine + 8, pszCode );
96 if( eErr != OGRERR_UNSUPPORTED_SRS )
97 break;
98 }
99
100 else if( strstr(pszLine,",") == NULL )
101 /* do nothing */;
102
103 else if( EQUALN(pszLine,pszCode,strlen(pszCode))
104 && pszLine[strlen(pszCode)] == ',' )
105 {
106 char *pszWKT = (char *) pszLine + strlen(pszCode)+1;
107
108 eErr = importFromWkt( &pszWKT );
109 break;
110 }
111 }
112
113 /* -------------------------------------------------------------------- */
114 /* Cleanup */
115 /* -------------------------------------------------------------------- */
116 VSIFClose( fp );
117
118 return eErr;
119 }
120
121 /************************************************************************/
122 /* OSRImportFromDict() */
123 /************************************************************************/
124
OSRImportFromDict(OGRSpatialReferenceH hSRS,const char * pszDictFile,const char * pszCode)125 OGRErr OSRImportFromDict( OGRSpatialReferenceH hSRS,
126 const char *pszDictFile,
127 const char *pszCode )
128
129 {
130 VALIDATE_POINTER1( hSRS, "OSRImportFromDict", CE_Failure );
131
132 return ((OGRSpatialReference *) hSRS)->importFromDict( pszDictFile,
133 pszCode );
134 }
135