1 : /******************************************************************************
2 : * $Id: ogrsdtsdatasource.cpp 13025 2007-11-25 18:03:46Z rouault $
3 : *
4 : * Project: SDTS Translator
5 : * Purpose: Implements OGRSDTSDataSource class
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
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_sdts.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ogrsdtsdatasource.cpp 13025 2007-11-25 18:03:46Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGRSDTSDataSource() */
38 : /************************************************************************/
39 :
40 193 : OGRSDTSDataSource::OGRSDTSDataSource()
41 :
42 : {
43 193 : nLayers = 0;
44 193 : papoLayers = NULL;
45 :
46 193 : pszName = NULL;
47 193 : poSRS = NULL;
48 :
49 193 : poTransfer = NULL;
50 193 : }
51 :
52 : /************************************************************************/
53 : /* ~OGRSDTSDataSource() */
54 : /************************************************************************/
55 :
56 386 : OGRSDTSDataSource::~OGRSDTSDataSource()
57 :
58 : {
59 : int i;
60 :
61 201 : for( i = 0; i < nLayers; i++ )
62 8 : delete papoLayers[i];
63 :
64 193 : CPLFree( papoLayers );
65 :
66 193 : CPLFree( pszName );
67 :
68 193 : if( poSRS )
69 1 : poSRS->Release();
70 :
71 193 : if( poTransfer )
72 1 : delete poTransfer;
73 386 : }
74 :
75 : /************************************************************************/
76 : /* TestCapability() */
77 : /************************************************************************/
78 :
79 0 : int OGRSDTSDataSource::TestCapability( const char * )
80 :
81 : {
82 0 : return FALSE;
83 : }
84 :
85 : /************************************************************************/
86 : /* GetLayer() */
87 : /************************************************************************/
88 :
89 36 : OGRLayer *OGRSDTSDataSource::GetLayer( int iLayer )
90 :
91 : {
92 36 : if( iLayer < 0 || iLayer >= nLayers )
93 0 : return NULL;
94 : else
95 36 : return papoLayers[iLayer];
96 : }
97 :
98 : /************************************************************************/
99 : /* Open() */
100 : /************************************************************************/
101 :
102 193 : int OGRSDTSDataSource::Open( const char * pszFilename, int bTestOpen )
103 :
104 : {
105 193 : pszName = CPLStrdup( pszFilename );
106 :
107 : /* -------------------------------------------------------------------- */
108 : /* Verify that the extension is DDF if we are testopening. */
109 : /* -------------------------------------------------------------------- */
110 193 : if( bTestOpen && !(strlen(pszFilename) > 4 &&
111 : EQUAL(pszFilename+strlen(pszFilename)-4,".ddf")) )
112 192 : return FALSE;
113 :
114 : /* -------------------------------------------------------------------- */
115 : /* Check a few bits of the header to see if it looks like an */
116 : /* SDTS file (really, if it looks like an ISO8211 file). */
117 : /* -------------------------------------------------------------------- */
118 1 : if( bTestOpen )
119 : {
120 : FILE *fp;
121 : char pachLeader[10];
122 :
123 1 : fp = VSIFOpen( pszFilename, "rb" );
124 1 : if( fp == NULL )
125 0 : return FALSE;
126 :
127 6 : if( VSIFRead( pachLeader, 1, 10, fp ) != 10
128 2 : || (pachLeader[5] != '1' && pachLeader[5] != '2'
129 0 : && pachLeader[5] != '3' )
130 1 : || pachLeader[6] != 'L'
131 2 : || (pachLeader[8] != '1' && pachLeader[8] != ' ') )
132 : {
133 0 : VSIFClose( fp );
134 0 : return FALSE;
135 : }
136 :
137 1 : VSIFClose( fp );
138 : }
139 :
140 : /* -------------------------------------------------------------------- */
141 : /* Create a transfer, and open it. */
142 : /* -------------------------------------------------------------------- */
143 1 : poTransfer = new SDTSTransfer();
144 :
145 1 : if( !poTransfer->Open( pszFilename ) )
146 : {
147 0 : delete poTransfer;
148 0 : poTransfer = NULL;
149 :
150 0 : return FALSE;
151 : }
152 :
153 : /* -------------------------------------------------------------------- */
154 : /* Initialize the projection. */
155 : /* -------------------------------------------------------------------- */
156 1 : SDTS_XREF *poXREF = poTransfer->GetXREF();
157 :
158 1 : poSRS = new OGRSpatialReference();
159 :
160 1 : if( EQUAL(poXREF->pszSystemName,"UTM") )
161 : {
162 1 : poSRS->SetUTM( poXREF->nZone, TRUE );
163 : }
164 :
165 1 : if( EQUAL(poXREF->pszDatum,"NAS") )
166 : poSRS->SetGeogCS("NAD27", "North_American_Datum_1927",
167 1 : "Clarke 1866", 6378206.4, 294.978698213901 );
168 :
169 0 : else if( EQUAL(poXREF->pszDatum,"NAX") )
170 : poSRS->SetGeogCS("NAD83", "North_American_Datum_1983",
171 0 : "GRS 1980", 6378137, 298.257222101 );
172 :
173 0 : else if( EQUAL(poXREF->pszDatum,"WGC") )
174 0 : poSRS->SetGeogCS("WGS 72", "WGS_1972", "NWL 10D", 6378135, 298.26 );
175 :
176 0 : else if( EQUAL(poXREF->pszDatum,"WGE") )
177 : poSRS->SetGeogCS("WGS 84", "WGS_1984",
178 0 : "WGS 84", 6378137, 298.257223563 );
179 :
180 : else
181 : poSRS->SetGeogCS("WGS 84", "WGS_1984",
182 0 : "WGS 84", 6378137, 298.257223563 );
183 :
184 1 : poSRS->Fixup();
185 :
186 : /* -------------------------------------------------------------------- */
187 : /* Initialize a layer for each source dataset layer. */
188 : /* -------------------------------------------------------------------- */
189 9 : for( int iLayer = 0; iLayer < poTransfer->GetLayerCount(); iLayer++ )
190 : {
191 : SDTSIndexedReader *poReader;
192 :
193 8 : if( poTransfer->GetLayerType( iLayer ) == SLTRaster )
194 0 : continue;
195 :
196 8 : poReader = poTransfer->GetLayerIndexedReader( iLayer );
197 8 : if( poReader == NULL )
198 0 : continue;
199 :
200 : papoLayers = (OGRSDTSLayer **)
201 8 : CPLRealloc( papoLayers, sizeof(void*) * ++nLayers );
202 8 : papoLayers[nLayers-1] = new OGRSDTSLayer( poTransfer, iLayer, this );
203 : }
204 :
205 1 : return TRUE;
206 : }
207 :
|