1 : /******************************************************************************
2 : * $Id: ogrsegukooadatasource.cpp 23259 2011-10-20 21:11:42Z rouault $
3 : *
4 : * Project: SEG-P1 / UKOOA P1-90 Translator
5 : * Purpose: Implements OGRSEGUKOOADataSource class
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, 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, DAMSEGUKOOAS 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_segukooa.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ogrsegukooadatasource.cpp 23259 2011-10-20 21:11:42Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGRSEGUKOOADataSource() */
38 : /************************************************************************/
39 :
40 126 : OGRSEGUKOOADataSource::OGRSEGUKOOADataSource()
41 :
42 : {
43 126 : papoLayers = NULL;
44 126 : nLayers = 0;
45 :
46 126 : pszName = NULL;
47 126 : }
48 :
49 : /************************************************************************/
50 : /* ~OGRSEGUKOOADataSource() */
51 : /************************************************************************/
52 :
53 126 : OGRSEGUKOOADataSource::~OGRSEGUKOOADataSource()
54 :
55 : {
56 142 : for( int i = 0; i < nLayers; i++ )
57 16 : delete papoLayers[i];
58 126 : CPLFree( papoLayers );
59 :
60 126 : CPLFree( pszName );
61 126 : }
62 :
63 : /************************************************************************/
64 : /* TestCapability() */
65 : /************************************************************************/
66 :
67 0 : int OGRSEGUKOOADataSource::TestCapability( const char * pszCap )
68 :
69 : {
70 0 : return FALSE;
71 : }
72 :
73 : /************************************************************************/
74 : /* GetLayer() */
75 : /************************************************************************/
76 :
77 8 : OGRLayer *OGRSEGUKOOADataSource::GetLayer( int iLayer )
78 :
79 : {
80 8 : if( iLayer < 0 || iLayer >= nLayers )
81 0 : return NULL;
82 : else
83 8 : return papoLayers[iLayer];
84 : }
85 :
86 : /************************************************************************/
87 : /* Open() */
88 : /************************************************************************/
89 :
90 126 : int OGRSEGUKOOADataSource::Open( const char * pszFilename, int bUpdateIn)
91 :
92 : {
93 126 : if (bUpdateIn)
94 : {
95 14 : return FALSE;
96 : }
97 :
98 112 : pszName = CPLStrdup( pszFilename );
99 :
100 112 : VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
101 112 : if (fp == NULL)
102 26 : return FALSE;
103 :
104 : const char* pszLine;
105 86 : CPLPushErrorHandler(CPLQuietErrorHandler);
106 86 : pszLine = CPLReadLine2L(fp,81,NULL);
107 86 : CPLPopErrorHandler();
108 86 : CPLErrorReset();
109 :
110 : /* Both UKOOA P1/90 and SEG-P1 begins by a H character */
111 86 : if (pszLine == NULL || pszLine[0] != 'H')
112 : {
113 78 : VSIFCloseL(fp);
114 78 : return FALSE;
115 : }
116 :
117 : // --------------------------------------------------------------------
118 : // Does this appear to be a UKOOA P1/90 file?
119 : // --------------------------------------------------------------------
120 :
121 8 : if (strncmp(pszLine, "H0100 ", 6) == 0)
122 : {
123 4 : VSIFSeekL( fp, 0, SEEK_SET );
124 :
125 4 : VSILFILE* fp2 = VSIFOpenL(pszFilename, "rb");
126 4 : if (fp2 == NULL)
127 : {
128 0 : VSIFCloseL(fp);
129 0 : return FALSE;
130 : }
131 :
132 4 : nLayers = 2;
133 4 : papoLayers = (OGRLayer**) CPLMalloc(2 * sizeof(OGRLayer*));
134 4 : papoLayers[0] = new OGRUKOOAP190Layer(pszName, fp);
135 4 : papoLayers[1] = new OGRSEGUKOOALineLayer(pszName,
136 8 : new OGRUKOOAP190Layer(pszName, fp2));
137 :
138 4 : return TRUE;
139 : }
140 :
141 : // --------------------------------------------------------------------
142 : // Does this appear to be a SEG-P1 file?
143 : // --------------------------------------------------------------------
144 :
145 : /* Check first 20 header lines, and fetch the first point */
146 84 : for(int iLine = 0; iLine < 21; iLine ++)
147 : {
148 84 : const char* szPtr = pszLine;
149 740 : for(;*szPtr != '\0';szPtr++)
150 : {
151 656 : if (*szPtr != 9 && *szPtr < 32)
152 : {
153 0 : VSIFCloseL(fp);
154 0 : return FALSE;
155 : }
156 : }
157 :
158 84 : if (iLine == 20)
159 4 : break;
160 :
161 80 : CPLPushErrorHandler(CPLQuietErrorHandler);
162 80 : pszLine = CPLReadLine2L(fp,81,NULL);
163 80 : CPLPopErrorHandler();
164 80 : CPLErrorReset();
165 80 : if (pszLine == NULL)
166 : {
167 0 : VSIFCloseL(fp);
168 0 : return FALSE;
169 : }
170 : }
171 :
172 4 : char* pszExpandedLine = OGRSEGP1Layer::ExpandTabs(pszLine);
173 4 : int nLatitudeCol = OGRSEGP1Layer::DetectLatitudeColumn(pszExpandedLine);
174 4 : CPLFree(pszExpandedLine);
175 :
176 4 : if (nLatitudeCol > 0)
177 : {
178 4 : VSIFSeekL( fp, 0, SEEK_SET );
179 :
180 4 : VSILFILE* fp2 = VSIFOpenL(pszFilename, "rb");
181 4 : if (fp2 == NULL)
182 : {
183 0 : VSIFCloseL(fp);
184 0 : return FALSE;
185 : }
186 :
187 4 : nLayers = 2;
188 4 : papoLayers = (OGRLayer**) CPLMalloc(2 * sizeof(OGRLayer*));
189 4 : papoLayers[0] = new OGRSEGP1Layer(pszName, fp, nLatitudeCol);
190 4 : papoLayers[1] = new OGRSEGUKOOALineLayer(pszName,
191 : new OGRSEGP1Layer(pszName, fp2,
192 8 : nLatitudeCol));
193 :
194 4 : return TRUE;
195 : }
196 :
197 0 : VSIFCloseL(fp);
198 0 : return FALSE;
199 : }
|