1 : /******************************************************************************
2 : * $Id: ogr_srs_ozi.cpp 22510 2011-06-07 13:33:40Z 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 22510 2011-06-07 13:33:40Z 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, 1972
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 : { "Potsdam Rauenberg DHDN", 31467 }, // Gauss-Krueger GK3 Central_Meridian 9 deg
55 : { NULL, 0 }
56 : };
57 :
58 : /************************************************************************/
59 : /* OSRImportFromOzi() */
60 : /************************************************************************/
61 :
62 0 : OGRErr OSRImportFromOzi( OGRSpatialReferenceH hSRS,
63 : const char *pszDatum, const char *pszProj,
64 : const char *pszProjParms )
65 :
66 : {
67 : return ((OGRSpatialReference *) hSRS)->importFromOzi( pszDatum, pszProj,
68 0 : pszProjParms );
69 : }
70 :
71 : /************************************************************************/
72 : /* importFromOzi() */
73 : /************************************************************************/
74 :
75 : /**
76 : * Import coordinate system from OziExplorer projection definition.
77 : *
78 : * This method will import projection definition in style, used by
79 : * OziExplorer software.
80 : *
81 : * This function is the equivalent of the C function OSRImportFromOzi().
82 : *
83 : * @param pszDatum Datum string. This is a fifth string in the
84 : * OziExplorer .MAP file.
85 : *
86 : * @param pszProj Projection string. Search for line starting with
87 : * "Map Projection" name in the OziExplorer .MAP file and supply it as a
88 : * whole in this parameter.
89 : *
90 : * @param pszProjParms String containing projection parameters. Search for
91 : * "Projection Setup" name in the OziExplorer .MAP file and supply it as a
92 : * whole in this parameter.
93 : *
94 : * @return OGRERR_NONE on success or an error code in case of failure.
95 : */
96 :
97 1 : OGRErr OGRSpatialReference::importFromOzi( const char *pszDatum,
98 : const char *pszProj,
99 : const char *pszProjParms )
100 :
101 : {
102 1 : Clear();
103 :
104 : /* -------------------------------------------------------------------- */
105 : /* Operate on the basis of the projection name. */
106 : /* -------------------------------------------------------------------- */
107 1 : char **papszProj = CSLTokenizeStringComplex( pszProj, ",", TRUE, TRUE );
108 : char **papszProjParms = CSLTokenizeStringComplex( pszProjParms, ",",
109 1 : TRUE, TRUE );
110 1 : char **papszDatum = NULL;
111 :
112 1 : if (CSLCount(papszProj) < 2)
113 : {
114 0 : goto not_enough_data;
115 : }
116 :
117 1 : if ( EQUALN(papszProj[1], "Latitude/Longitude", 18) )
118 : {
119 : }
120 :
121 1 : else if ( EQUALN(papszProj[1], "Mercator", 8) )
122 : {
123 0 : if (CSLCount(papszProjParms) < 6) goto not_enough_data;
124 0 : double dfScale = CPLAtof(papszProjParms[3]);
125 0 : if (papszProjParms[3][0] == 0) dfScale = 1; /* if unset, default to scale = 1 */
126 0 : SetMercator( CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
127 : dfScale,
128 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
129 : }
130 :
131 1 : else if ( EQUALN(papszProj[1], "Transverse Mercator", 19) )
132 : {
133 0 : if (CSLCount(papszProjParms) < 6) goto not_enough_data;
134 0 : SetTM( CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
135 0 : CPLAtof(papszProjParms[3]),
136 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
137 : }
138 :
139 1 : else if ( EQUALN(papszProj[1], "Lambert Conformal Conic", 23) )
140 : {
141 1 : if (CSLCount(papszProjParms) < 8) goto not_enough_data;
142 2 : SetLCC( CPLAtof(papszProjParms[6]), CPLAtof(papszProjParms[7]),
143 2 : CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
144 5 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
145 : }
146 :
147 0 : else if ( EQUALN(papszProj[1], "Sinusoidal", 10) )
148 : {
149 0 : if (CSLCount(papszProjParms) < 6) goto not_enough_data;
150 0 : SetSinusoidal( CPLAtof(papszProjParms[2]),
151 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
152 : }
153 :
154 0 : else if ( EQUALN(papszProj[1], "Albers Equal Area", 17) )
155 : {
156 0 : if (CSLCount(papszProjParms) < 8) goto not_enough_data;
157 0 : SetACEA( CPLAtof(papszProjParms[6]), CPLAtof(papszProjParms[7]),
158 0 : CPLAtof(papszProjParms[1]), CPLAtof(papszProjParms[2]),
159 0 : CPLAtof(papszProjParms[4]), CPLAtof(papszProjParms[5]) );
160 : }
161 :
162 : else
163 : {
164 0 : CPLDebug( "OSR_Ozi", "Unsupported projection: \"%s\"", papszProj[1] );
165 : SetLocalCS( CPLString().Printf("\"Ozi\" projection \"%s\"",
166 0 : papszProj[1]) );
167 : }
168 :
169 : /* -------------------------------------------------------------------- */
170 : /* Try to translate the datum/spheroid. */
171 : /* -------------------------------------------------------------------- */
172 : papszDatum = CSLTokenizeString2( pszDatum, ",",
173 : CSLT_ALLOWEMPTYTOKENS
174 : | CSLT_STRIPLEADSPACES
175 1 : | CSLT_STRIPENDSPACES );
176 1 : if ( papszDatum == NULL)
177 0 : goto not_enough_data;
178 :
179 1 : if ( !IsLocal() )
180 : {
181 1 : const OZIDatums *paoDatum = aoDatums;
182 :
183 : // Search for matching datum
184 3 : while ( paoDatum->pszOziDatum )
185 : {
186 2 : if ( EQUAL( papszDatum[0], paoDatum->pszOziDatum ) )
187 : {
188 1 : OGRSpatialReference oGCS;
189 1 : oGCS.importFromEPSG( paoDatum->nEPSGCode );
190 1 : CopyGeogCSFrom( &oGCS );
191 1 : break;
192 : }
193 1 : paoDatum++;
194 : }
195 :
196 1 : if ( !paoDatum->pszOziDatum )
197 : {
198 : CPLError( CE_Warning, CPLE_AppDefined,
199 : "Wrong datum name \"%s\". Setting WGS84 as a fallback.",
200 0 : papszDatum[0] );
201 0 : SetWellKnownGeogCS( "WGS84" );
202 : }
203 : }
204 :
205 : /* -------------------------------------------------------------------- */
206 : /* Grid units translation */
207 : /* -------------------------------------------------------------------- */
208 1 : if( IsLocal() || IsProjected() )
209 1 : SetLinearUnits( SRS_UL_METER, 1.0 );
210 :
211 1 : FixupOrdering();
212 :
213 1 : CSLDestroy(papszProj);
214 1 : CSLDestroy(papszProjParms);
215 1 : CSLDestroy(papszDatum);
216 :
217 1 : return OGRERR_NONE;
218 :
219 : not_enough_data:
220 :
221 0 : CSLDestroy(papszProj);
222 0 : CSLDestroy(papszProjParms);
223 0 : CSLDestroy(papszDatum);
224 :
225 0 : return OGRERR_NOT_ENOUGH_DATA;
226 : }
227 :
|