1 : /******************************************************************************
2 : * $Id: ogr_srs_ozi.cpp 18063 2009-11-21 21:11:49Z warmerdam $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: OGRSpatialReference translation from OziExplorer
6 : * georeferencing information.
7 : * Author: Andrey Kiselev, dron@ak4719.spb.edu
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2009, Andrey Kiselev <dron@ak4719.spb.edu>
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 : #include "cpl_csv.h"
34 :
35 : CPL_CVSID("$Id: ogr_srs_ozi.cpp 18063 2009-11-21 21:11:49Z warmerdam $");
36 :
37 : /************************************************************************/
38 : /* Correspondence between Ozi and EPSG datum codes. */
39 : /************************************************************************/
40 :
41 : typedef struct
42 : {
43 : const char *pszOziDatum;
44 : int nEPSGCode;
45 : } OZIDatums;
46 :
47 : static const OZIDatums aoDatums[] =
48 : {
49 : { "WGS 72", 4322 }, // WGS, 1984
50 : { "WGS 84", 4326 }, // WGS, 1984
51 : { "Pulkovo 1942 (1)", 4284 }, // Pulkovo 1942
52 : { "Pulkovo 1942 (2)", 4284 }, // Pulkovo 1942, XXX: What is a difference
53 : // with the previous one?
54 : { NULL, 0 }
55 : };
56 :
57 : /************************************************************************/
58 : /* OSRImportFromOzi() */
59 : /************************************************************************/
60 :
61 0 : OGRErr OSRImportFromOzi( OGRSpatialReferenceH hSRS,
62 : const char *pszDatum, const char *pszProj,
63 : const char *pszProjParms )
64 :
65 : {
66 : return ((OGRSpatialReference *) hSRS)->importFromOzi( pszDatum, pszProj,
67 0 : pszProjParms );
68 : }
69 :
70 : /************************************************************************/
71 : /* importFromOzi() */
72 : /************************************************************************/
73 :
74 : /**
75 : * Import coordinate system from OziExplorer projection definition.
76 : *
77 : * This method will import projection definition in style, used by
78 : * OziExplorer software.
79 : *
80 : * This function is the equivalent of the C function OSRImportFromOzi().
81 : *
82 : * @param pszDatum Datum string. This is a fifth string in the
83 : * OziExplorer .MAP file.
84 : *
85 : * @param pszProj Projection string. Search for line starting with
86 : * "Map Projection" name in the OziExplorer .MAP file and supply it as a
87 : * whole in this parameter.
88 : *
89 : * @param pszProjParms String containing projection parameters. Search for
90 : * "Projection Setup" name in the OziExplorer .MAP file and supply it as a
91 : * whole in this parameter.
92 : *
93 : * @return OGRERR_NONE on success or an error code in case of failure.
94 : */
95 :
96 0 : OGRErr OGRSpatialReference::importFromOzi( const char *pszDatum,
97 : const char *pszProj,
98 : const char *pszProjParms )
99 :
100 : {
101 0 : Clear();
102 :
103 : /* -------------------------------------------------------------------- */
104 : /* Operate on the basis of the projection name. */
105 : /* -------------------------------------------------------------------- */
106 0 : char **papszProj = CSLTokenizeStringComplex( pszProj, ",", TRUE, TRUE );
107 : char **papszProjParms = CSLTokenizeStringComplex( pszProjParms, ",",
108 0 : TRUE, TRUE );
109 0 : char **papszDatum = NULL;
110 :
111 0 : if (CSLCount(papszProj) < 2)
112 : {
113 0 : goto not_enough_data;
114 : }
115 :
116 0 : if ( EQUALN(papszProj[1], "Latitude/Longitude", 18) )
117 : {
118 : }
119 :
120 0 : else if ( EQUALN(papszProj[1], "Mercator", 8) )
121 : {
122 0 : if (CSLCount(papszProjParms) < 6) goto not_enough_data;
123 0 : SetMercator( CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
124 0 : CPLAtof(papszProjParms[3]),
125 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
126 : }
127 :
128 0 : else if ( EQUALN(papszProj[1], "Transverse Mercator", 19) )
129 : {
130 0 : if (CSLCount(papszProjParms) < 6) goto not_enough_data;
131 0 : SetTM( CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
132 0 : CPLAtof(papszProjParms[3]),
133 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
134 : }
135 :
136 0 : else if ( EQUALN(papszProj[1], "Lambert Conformal Conic", 23) )
137 : {
138 0 : if (CSLCount(papszProjParms) < 8) goto not_enough_data;
139 0 : SetLCC( CPLAtof(papszProjParms[6]), CPLAtof(papszProjParms[7]),
140 0 : CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
141 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
142 : }
143 :
144 0 : else if ( EQUALN(papszProj[1], "Sinusoidal", 10) )
145 : {
146 0 : if (CSLCount(papszProjParms) < 6) goto not_enough_data;
147 0 : SetSinusoidal( CPLAtof(papszProjParms[2]),
148 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
149 : }
150 :
151 0 : else if ( EQUALN(papszProj[1], "Albers Equal Area", 17) )
152 : {
153 0 : if (CSLCount(papszProjParms) < 8) goto not_enough_data;
154 0 : SetACEA( CPLAtof(papszProjParms[6]), CPLAtof(papszProjParms[7]),
155 0 : CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
156 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
157 : }
158 :
159 : else
160 : {
161 0 : CPLDebug( "OSR_Ozi", "Unsupported projection: \"%s\"", papszProj[1] );
162 : SetLocalCS( CPLString().Printf("\"Ozi\" projection \"%s\"",
163 0 : papszProj[1]) );
164 : }
165 :
166 : /* -------------------------------------------------------------------- */
167 : /* Try to translate the datum/spheroid. */
168 : /* -------------------------------------------------------------------- */
169 : papszDatum = CSLTokenizeString2( pszDatum, ",",
170 : CSLT_ALLOWEMPTYTOKENS
171 : | CSLT_STRIPLEADSPACES
172 0 : | CSLT_STRIPENDSPACES );
173 0 : if ( papszDatum == NULL)
174 0 : goto not_enough_data;
175 :
176 0 : if ( !IsLocal() )
177 : {
178 0 : const OZIDatums *paoDatum = aoDatums;
179 :
180 : // Search for matching datum
181 0 : while ( paoDatum->pszOziDatum )
182 : {
183 0 : if ( EQUAL( papszDatum[0], paoDatum->pszOziDatum ) )
184 : {
185 0 : OGRSpatialReference oGCS;
186 0 : oGCS.importFromEPSG( paoDatum->nEPSGCode );
187 0 : CopyGeogCSFrom( &oGCS );
188 0 : break;
189 : }
190 0 : paoDatum++;
191 : }
192 :
193 0 : if ( !paoDatum->pszOziDatum )
194 : {
195 : CPLError( CE_Warning, CPLE_AppDefined,
196 : "Wrong datum name \"%s\". Setting WGS84 as a fallback.",
197 0 : papszDatum[0] );
198 0 : SetWellKnownGeogCS( "WGS84" );
199 : }
200 : }
201 :
202 : /* -------------------------------------------------------------------- */
203 : /* Grid units translation */
204 : /* -------------------------------------------------------------------- */
205 0 : if( IsLocal() || IsProjected() )
206 0 : SetLinearUnits( SRS_UL_METER, 1.0 );
207 :
208 0 : FixupOrdering();
209 :
210 0 : CSLDestroy(papszProj);
211 0 : CSLDestroy(papszProjParms);
212 0 : CSLDestroy(papszDatum);
213 :
214 0 : return OGRERR_NONE;
215 :
216 : not_enough_data:
217 :
218 0 : CSLDestroy(papszProj);
219 0 : CSLDestroy(papszProjParms);
220 0 : CSLDestroy(papszDatum);
221 :
222 0 : return OGRERR_NOT_ENOUGH_DATA;
223 : }
224 :
|