1 : /******************************************************************************
2 : * $Id: ogr_srs_erm.cpp 18063 2009-11-21 21:11:49Z warmerdam $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implement ERMapper projection conversions.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2007, Frank Warmerdam <warmerdam@pobox.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ogr_spatialref.h"
31 : #include "cpl_conv.h"
32 :
33 : CPL_CVSID("$Id: ogr_srs_erm.cpp 18063 2009-11-21 21:11:49Z warmerdam $");
34 :
35 : /************************************************************************/
36 : /* importFromERM() */
37 : /************************************************************************/
38 :
39 : /**
40 : * OGR WKT from ERMapper projection definitions.
41 : *
42 : * Generates an OGRSpatialReference definition from an ERMapper datum
43 : * and projection name. Based on the ecw_cs.wkt dictionary file from
44 : * gdal/data.
45 : *
46 : * @param pszProj the projection name, such as "NUTM11" or "GEOGRAPHIC".
47 : * @param pszDatum the datum name, such as "NAD83".
48 : * @param pszUnits the linear units "FEET" or "METERS".
49 : *
50 : * @return OGRERR_NONE on success or OGRERR_UNSUPPORTED_SRS if not found.
51 : */
52 :
53 34 : OGRErr OGRSpatialReference::importFromERM( const char *pszProj,
54 : const char *pszDatum,
55 : const char *pszUnits )
56 :
57 : {
58 34 : Clear();
59 :
60 : /* -------------------------------------------------------------------- */
61 : /* do we have projection and datum? */
62 : /* -------------------------------------------------------------------- */
63 34 : if( EQUAL(pszProj,"RAW") )
64 27 : return OGRERR_NONE;
65 :
66 : /* -------------------------------------------------------------------- */
67 : /* Set projection if we have it. */
68 : /* -------------------------------------------------------------------- */
69 : OGRErr eErr;
70 :
71 7 : if( EQUAL(pszProj,"GEODETIC") )
72 : {
73 : }
74 : else
75 : {
76 5 : eErr = importFromDict( "ecw_cs.wkt", pszProj );
77 5 : if( eErr != OGRERR_NONE )
78 0 : return eErr;
79 :
80 5 : if( EQUAL(pszUnits,"FEET") )
81 0 : SetLinearUnits( SRS_UL_US_FOOT, atof(SRS_UL_US_FOOT_CONV));
82 : else
83 5 : SetLinearUnits( SRS_UL_METER, 1.0 );
84 : }
85 :
86 : /* -------------------------------------------------------------------- */
87 : /* Set the geogcs. */
88 : /* -------------------------------------------------------------------- */
89 7 : OGRSpatialReference oGeogCS;
90 :
91 7 : eErr = oGeogCS.importFromDict( "ecw_cs.wkt", pszDatum );
92 7 : if( eErr != OGRERR_NONE )
93 : {
94 0 : Clear();
95 0 : return eErr;
96 : }
97 :
98 7 : if( !IsLocal() )
99 7 : CopyGeogCSFrom( &oGeogCS );
100 :
101 7 : return OGRERR_NONE;
102 : }
103 :
104 : /************************************************************************/
105 : /* exportToERM() */
106 : /************************************************************************/
107 :
108 : /**
109 : * Convert coordinate system to ERMapper format.
110 : *
111 : * @param pszProj 32 character buffer to receive projection name.
112 : * @param pszDatum 32 character buffer to recieve datum name.
113 : * @param pszUnits 32 character buffer to receive units name.
114 : *
115 : * @return OGRERR_NONE on success, OGRERR_SRS_UNSUPPORTED if not translation is
116 : * found, or OGRERR_FAILURE on other failures.
117 : */
118 :
119 13 : OGRErr OGRSpatialReference::exportToERM( char *pszProj, char *pszDatum,
120 : char *pszUnits )
121 :
122 : {
123 13 : strcpy( pszProj, "RAW" );
124 13 : strcpy( pszDatum, "RAW" );
125 13 : strcpy( pszUnits, "METERS" );
126 :
127 13 : if( !IsProjected() && !IsGeographic() )
128 0 : return TRUE;
129 :
130 : /* -------------------------------------------------------------------- */
131 : /* Try to find the EPSG code. */
132 : /* -------------------------------------------------------------------- */
133 13 : int nEPSGCode = 0;
134 :
135 13 : if( IsProjected() )
136 : {
137 2 : const char *pszAuthName = GetAuthorityName( "PROJCS" );
138 :
139 2 : if( pszAuthName != NULL && EQUAL(pszAuthName,"epsg") )
140 : {
141 0 : nEPSGCode = atoi(GetAuthorityCode( "PROJCS" ));
142 : }
143 : }
144 11 : else if( IsGeographic() )
145 : {
146 11 : const char *pszAuthName = GetAuthorityName( "GEOGCS" );
147 :
148 11 : if( pszAuthName != NULL && EQUAL(pszAuthName,"epsg") )
149 : {
150 11 : nEPSGCode = atoi(GetAuthorityCode( "GEOGCS" ));
151 : }
152 : }
153 :
154 : /* -------------------------------------------------------------------- */
155 : /* Is our GEOGCS name already defined in ecw_cs.dat? */
156 : /* -------------------------------------------------------------------- */
157 13 : OGRSpatialReference oSRSWork;
158 13 : const char *pszWKTDatum = GetAttrValue( "DATUM" );
159 :
160 13 : if( pszWKTDatum != NULL
161 : && oSRSWork.importFromDict( "ecw_cs.wkt", pszWKTDatum ) == OGRERR_NONE)
162 : {
163 0 : strncpy( pszDatum, pszWKTDatum, 32 );
164 0 : pszDatum[31] = '\0';
165 : }
166 :
167 : /* -------------------------------------------------------------------- */
168 : /* Is this a "well known" geographic coordinate system? */
169 : /* -------------------------------------------------------------------- */
170 13 : if( EQUAL(pszDatum,"RAW") )
171 : {
172 13 : nEPSGCode = GetEPSGGeogCS();
173 :
174 13 : if( nEPSGCode == 4326 )
175 11 : strcpy( pszDatum, "WGS84" );
176 :
177 2 : else if( nEPSGCode == 4322 )
178 0 : strcpy( pszDatum, "WGS72DOD" );
179 :
180 2 : else if( nEPSGCode == 4267 )
181 2 : strcpy( pszDatum, "NAD27" );
182 :
183 0 : else if( nEPSGCode == 4269 )
184 0 : strcpy( pszDatum, "NAD83" );
185 :
186 0 : else if( nEPSGCode == 4277 )
187 0 : strcpy( pszDatum, "OSGB36" );
188 :
189 0 : else if( nEPSGCode == 4278 )
190 0 : strcpy( pszDatum, "OSGB78" );
191 :
192 0 : else if( nEPSGCode == 4201 )
193 0 : strcpy( pszDatum, "ADINDAN" );
194 :
195 0 : else if( nEPSGCode == 4202 )
196 0 : strcpy( pszDatum, "AGD66" );
197 :
198 0 : else if( nEPSGCode == 4203 )
199 0 : strcpy( pszDatum, "AGD84" );
200 :
201 0 : else if( nEPSGCode == 4209 )
202 0 : strcpy( pszDatum, "ARC1950" );
203 :
204 0 : else if( nEPSGCode == 4210 )
205 0 : strcpy( pszDatum, "ARC1960" );
206 :
207 0 : else if( nEPSGCode == 4275 )
208 0 : strcpy( pszDatum, "NTF" );
209 :
210 0 : else if( nEPSGCode == 4284 )
211 0 : strcpy( pszDatum, "PULKOVO" );
212 : }
213 :
214 : /* -------------------------------------------------------------------- */
215 : /* Are we working with a geographic (geodetic) coordinate system? */
216 : /* -------------------------------------------------------------------- */
217 :
218 13 : if( IsGeographic() )
219 : {
220 11 : if( EQUAL(pszDatum,"RAW") )
221 0 : return OGRERR_UNSUPPORTED_SRS;
222 : else
223 : {
224 11 : strcpy( pszProj, "GEODETIC" );
225 11 : return OGRERR_NONE;
226 : }
227 : }
228 :
229 : /* -------------------------------------------------------------------- */
230 : /* Is this a UTM projection? */
231 : /* -------------------------------------------------------------------- */
232 : int bNorth, nZone;
233 :
234 2 : nZone = GetUTMZone( &bNorth );
235 2 : if( nZone > 0 )
236 : {
237 2 : if( bNorth )
238 2 : sprintf( pszProj, "NUTM%02d", nZone );
239 : else
240 0 : sprintf( pszProj, "SUTM%02d", nZone );
241 : }
242 :
243 : /* -------------------------------------------------------------------- */
244 : /* Is our PROJCS name already defined in ecw_cs.dat? */
245 : /* -------------------------------------------------------------------- */
246 : else
247 : {
248 0 : const char *pszPROJCS = GetAttrValue( "PROJCS" );
249 :
250 0 : if( pszPROJCS != NULL
251 : && oSRSWork.importFromDict( "ecw_cs.wkt", pszPROJCS ) == OGRERR_NONE
252 : && oSRSWork.IsProjected() )
253 : {
254 0 : strncpy( pszProj, pszPROJCS, 32 );
255 0 : pszProj[31] = '\0';
256 : }
257 : }
258 :
259 : /* -------------------------------------------------------------------- */
260 : /* Handle the units. */
261 : /* -------------------------------------------------------------------- */
262 2 : double dfUnits = GetLinearUnits();
263 :
264 2 : if( fabs(dfUnits-0.3048) < 0.0001 )
265 0 : strcpy( pszUnits, "FEET" );
266 : else
267 2 : strcpy( pszUnits, "METERS" );
268 :
269 2 : if( EQUAL(pszProj,"RAW") )
270 0 : return OGRERR_UNSUPPORTED_SRS;
271 : else
272 2 : return OGRERR_NONE;
273 : }
|