1 : /******************************************************************************
2 : * $Id: ogropenairdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
3 : *
4 : * Project: OpenAir Translator
5 : * Purpose: Implements OGROpenAirDataSource class
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
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_openair.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ogropenairdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGROpenAirDataSource() */
38 : /************************************************************************/
39 :
40 230 : OGROpenAirDataSource::OGROpenAirDataSource()
41 :
42 : {
43 230 : papoLayers = NULL;
44 230 : nLayers = 0;
45 :
46 230 : pszName = NULL;
47 230 : }
48 :
49 : /************************************************************************/
50 : /* ~OGROpenAirDataSource() */
51 : /************************************************************************/
52 :
53 230 : OGROpenAirDataSource::~OGROpenAirDataSource()
54 :
55 : {
56 234 : for( int i = 0; i < nLayers; i++ )
57 4 : delete papoLayers[i];
58 230 : CPLFree( papoLayers );
59 :
60 230 : CPLFree( pszName );
61 230 : }
62 :
63 : /************************************************************************/
64 : /* TestCapability() */
65 : /************************************************************************/
66 :
67 0 : int OGROpenAirDataSource::TestCapability( const char * pszCap )
68 :
69 : {
70 0 : return FALSE;
71 : }
72 :
73 : /************************************************************************/
74 : /* GetLayer() */
75 : /************************************************************************/
76 :
77 6 : OGRLayer *OGROpenAirDataSource::GetLayer( int iLayer )
78 :
79 : {
80 6 : if( iLayer < 0 || iLayer >= nLayers )
81 0 : return NULL;
82 : else
83 6 : return papoLayers[iLayer];
84 : }
85 :
86 : /************************************************************************/
87 : /* Open() */
88 : /************************************************************************/
89 :
90 230 : int OGROpenAirDataSource::Open( const char * pszFilename, int bUpdateIn)
91 :
92 : {
93 230 : if (bUpdateIn)
94 : {
95 26 : return FALSE;
96 : }
97 :
98 204 : pszName = CPLStrdup( pszFilename );
99 :
100 : // --------------------------------------------------------------------
101 : // Does this appear to be an openair file?
102 : // --------------------------------------------------------------------
103 :
104 204 : VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
105 204 : if (fp == NULL)
106 78 : return FALSE;
107 :
108 : char szBuffer[10000];
109 126 : int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
110 126 : szBuffer[nbRead] = '\0';
111 :
112 : int bIsOpenAir = (strstr(szBuffer, "\nAC ") != NULL &&
113 : strstr(szBuffer, "\nAN ") != NULL &&
114 : strstr(szBuffer, "\nAL ") != NULL &&
115 126 : strstr(szBuffer, "\nAH") != NULL);
116 :
117 126 : if (bIsOpenAir)
118 : {
119 2 : VSIFSeekL( fp, 0, SEEK_SET );
120 :
121 2 : VSILFILE* fp2 = VSIFOpenL(pszFilename, "rb");
122 2 : if (fp2)
123 : {
124 2 : nLayers = 2;
125 2 : papoLayers = (OGRLayer**) CPLMalloc(2 * sizeof(OGRLayer*));
126 2 : papoLayers[0] = new OGROpenAirLayer(fp);
127 4 : papoLayers[1] = new OGROpenAirLabelLayer(fp2);
128 : }
129 : }
130 : else
131 124 : VSIFCloseL(fp);
132 :
133 126 : return bIsOpenAir;
134 : }
135 :
136 :
137 : /************************************************************************/
138 : /* GetLatLon() */
139 : /************************************************************************/
140 :
141 : enum { DEGREE, MINUTE, SECOND };
142 :
143 16 : int OGROpenAirGetLatLon(const char* pszStr, double& dfLat, double& dfLon)
144 : {
145 16 : dfLat = 0;
146 16 : dfLon = 0;
147 :
148 16 : int nCurInt = 0;
149 16 : double dfExp = 1.;
150 16 : int bHasExp = FALSE;
151 16 : int nCurPart = DEGREE;
152 16 : double dfDegree = 0, dfMinute = 0, dfSecond = 0;
153 : char c;
154 16 : int bHasLat = FALSE, bHasLon = FALSE;
155 252 : while((c = *pszStr) != 0)
156 : {
157 356 : if (c >= '0' && c <= '9')
158 : {
159 120 : nCurInt = nCurInt * 10 + c - '0';
160 120 : if (bHasExp)
161 16 : dfExp *= 10;
162 : }
163 116 : else if (c == '.')
164 : {
165 8 : bHasExp = TRUE;
166 : }
167 108 : else if (c == ':')
168 : {
169 28 : double dfVal = nCurInt / dfExp;
170 28 : if (nCurPart == DEGREE)
171 24 : dfDegree = dfVal;
172 4 : else if (nCurPart == MINUTE)
173 4 : dfMinute = dfVal;
174 0 : else if (nCurPart == SECOND)
175 0 : dfSecond = dfVal;
176 28 : nCurPart ++;
177 28 : nCurInt = 0;
178 28 : dfExp = 1.;
179 28 : bHasExp = FALSE;
180 : }
181 80 : else if (c == ' ')
182 : {
183 :
184 : }
185 48 : else if (c == 'N' || c == 'S')
186 : {
187 16 : double dfVal = nCurInt / dfExp;
188 16 : if (nCurPart == DEGREE)
189 4 : dfDegree = dfVal;
190 12 : else if (nCurPart == MINUTE)
191 8 : dfMinute = dfVal;
192 4 : else if (nCurPart == SECOND)
193 4 : dfSecond = dfVal;
194 :
195 16 : dfLat = dfDegree + dfMinute / 60 + dfSecond / 3600;
196 16 : if (c == 'S')
197 0 : dfLat = -dfLat;
198 16 : nCurInt = 0;
199 16 : dfExp = 1.;
200 16 : bHasExp = FALSE;
201 16 : nCurPart = DEGREE;
202 16 : bHasLat = TRUE;
203 : }
204 16 : else if (c == 'E' || c == 'W')
205 : {
206 16 : double dfVal = nCurInt / dfExp;
207 16 : if (nCurPart == DEGREE)
208 4 : dfDegree = dfVal;
209 12 : else if (nCurPart == MINUTE)
210 12 : dfMinute = dfVal;
211 0 : else if (nCurPart == SECOND)
212 0 : dfSecond = dfVal;
213 :
214 16 : dfLon = dfDegree + dfMinute / 60 + dfSecond / 3600;
215 16 : if (c == 'W')
216 0 : dfLon = -dfLon;
217 16 : bHasLon = TRUE;
218 16 : break;
219 : }
220 :
221 220 : pszStr++;
222 : }
223 :
224 16 : return bHasLat && bHasLon;
225 : }
|