1 : /******************************************************************************
2 : * $Id: ograeronavfaadatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
3 : *
4 : * Project: AeronavFAA Translator
5 : * Purpose: Implements OGRAeronavFAADataSource 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_aeronavfaa.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ograeronavfaadatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGRAeronavFAADataSource() */
38 : /************************************************************************/
39 :
40 62 : OGRAeronavFAADataSource::OGRAeronavFAADataSource()
41 :
42 : {
43 62 : papoLayers = NULL;
44 62 : nLayers = 0;
45 :
46 62 : pszName = NULL;
47 62 : }
48 :
49 : /************************************************************************/
50 : /* ~OGRAeronavFAADataSource() */
51 : /************************************************************************/
52 :
53 62 : OGRAeronavFAADataSource::~OGRAeronavFAADataSource()
54 :
55 : {
56 62 : for( int i = 0; i < nLayers; i++ )
57 0 : delete papoLayers[i];
58 62 : CPLFree( papoLayers );
59 :
60 62 : CPLFree( pszName );
61 62 : }
62 :
63 : /************************************************************************/
64 : /* TestCapability() */
65 : /************************************************************************/
66 :
67 0 : int OGRAeronavFAADataSource::TestCapability( const char * pszCap )
68 :
69 : {
70 0 : return FALSE;
71 : }
72 :
73 : /************************************************************************/
74 : /* GetLayer() */
75 : /************************************************************************/
76 :
77 0 : OGRLayer *OGRAeronavFAADataSource::GetLayer( int iLayer )
78 :
79 : {
80 0 : if( iLayer < 0 || iLayer >= nLayers )
81 0 : return NULL;
82 : else
83 0 : return papoLayers[iLayer];
84 : }
85 :
86 : /************************************************************************/
87 : /* Open() */
88 : /************************************************************************/
89 :
90 62 : int OGRAeronavFAADataSource::Open( const char * pszFilename, int bUpdateIn)
91 :
92 : {
93 62 : if (bUpdateIn)
94 : {
95 11 : return FALSE;
96 : }
97 :
98 51 : pszName = CPLStrdup( pszFilename );
99 :
100 : // --------------------------------------------------------------------
101 : // Does this appear to be a .dat file?
102 : // --------------------------------------------------------------------
103 51 : if( !EQUAL(CPLGetExtension(pszFilename), "dat") )
104 51 : return FALSE;
105 :
106 0 : VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
107 0 : if (fp == NULL)
108 0 : return FALSE;
109 :
110 : char szBuffer[10000];
111 0 : int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
112 0 : szBuffer[nbRead] = '\0';
113 :
114 0 : int bIsDOF = (szBuffer[128] == 13 && szBuffer[128+1] == 10 &&
115 0 : szBuffer[130+128] == 13 && szBuffer[130+129] == 10 &&
116 0 : szBuffer[2*130+128] == 13 && szBuffer[2*130+129] == 10 &&
117 0 : strncmp(szBuffer + 3 * 130, "------------------------------------------------------------------------------------------------------------------------- ", 122) == 0);
118 :
119 0 : int bIsNAVAID = (szBuffer[132] == 13 && szBuffer[132+1] == 10 &&
120 : strncmp(szBuffer + 20 - 1, "CREATION DATE", strlen("CREATION DATE")) == 0 &&
121 0 : szBuffer[134 + 132] == 13 && szBuffer[134 + 132+1] == 10);
122 :
123 : int bIsROUTE = strncmp(szBuffer, " UNITED STATES GOVERNMENT FLIGHT INFORMATION PUBLICATION 149343", 85) == 0 &&
124 0 : szBuffer[85] == 13 && szBuffer[85+1] == 10;
125 :
126 : int bIsIAP = strstr(szBuffer, "INSTRUMENT APPROACH PROCEDURE NAVAID & FIX DATA") != NULL &&
127 0 : szBuffer[85] == 13 && szBuffer[85+1] == 10;
128 0 : if (bIsIAP)
129 0 : bIsROUTE = FALSE;
130 :
131 0 : if (bIsDOF)
132 : {
133 0 : VSIFSeekL( fp, 0, SEEK_SET );
134 0 : nLayers = 1;
135 0 : papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*));
136 0 : papoLayers[0] = new OGRAeronavFAADOFLayer(fp, CPLGetBasename(pszFilename));
137 0 : return TRUE;
138 : }
139 0 : else if (bIsNAVAID)
140 : {
141 0 : VSIFSeekL( fp, 0, SEEK_SET );
142 0 : nLayers = 1;
143 0 : papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*));
144 0 : papoLayers[0] = new OGRAeronavFAANAVAIDLayer(fp, CPLGetBasename(pszFilename));
145 0 : return TRUE;
146 : }
147 0 : else if (bIsIAP)
148 : {
149 0 : VSIFSeekL( fp, 0, SEEK_SET );
150 0 : nLayers = 1;
151 0 : papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*));
152 0 : papoLayers[0] = new OGRAeronavFAAIAPLayer(fp, CPLGetBasename(pszFilename));
153 0 : return TRUE;
154 : }
155 0 : else if (bIsROUTE)
156 : {
157 : int bIsDPOrSTARS = strstr(szBuffer, "DPs - DEPARTURE PROCEDURES") != NULL ||
158 0 : strstr(szBuffer, "STARS - STANDARD TERMINAL ARRIVALS") != NULL;
159 0 : VSIFSeekL( fp, 0, SEEK_SET );
160 0 : nLayers = 1;
161 0 : papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*));
162 0 : papoLayers[0] = new OGRAeronavFAARouteLayer(fp, CPLGetBasename(pszFilename), bIsDPOrSTARS);
163 0 : return TRUE;
164 : }
165 : else
166 : {
167 0 : VSIFCloseL(fp);
168 0 : return FALSE;
169 : }
170 : }
|