Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: SDTS Translator
4 : * Purpose: Implements OGRSDTSDataSource class
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1999, Frank Warmerdam
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #include "ogr_sdts.h"
30 : #include "cpl_conv.h"
31 : #include "cpl_string.h"
32 :
33 : /************************************************************************/
34 : /* OGRSDTSDataSource() */
35 : /************************************************************************/
36 :
37 1 : OGRSDTSDataSource::OGRSDTSDataSource()
38 : : poTransfer(nullptr), pszName(nullptr), nLayers(0), papoLayers(nullptr),
39 1 : poSRS(nullptr)
40 : {
41 1 : }
42 :
43 : /************************************************************************/
44 : /* ~OGRSDTSDataSource() */
45 : /************************************************************************/
46 :
47 2 : OGRSDTSDataSource::~OGRSDTSDataSource()
48 :
49 : {
50 9 : for (int i = 0; i < nLayers; i++)
51 8 : delete papoLayers[i];
52 :
53 1 : CPLFree(papoLayers);
54 :
55 1 : CPLFree(pszName);
56 :
57 1 : if (poSRS)
58 1 : poSRS->Release();
59 :
60 1 : if (poTransfer)
61 1 : delete poTransfer;
62 2 : }
63 :
64 : /************************************************************************/
65 : /* TestCapability() */
66 : /************************************************************************/
67 :
68 0 : int OGRSDTSDataSource::TestCapability(const char *)
69 :
70 : {
71 0 : return FALSE;
72 : }
73 :
74 : /************************************************************************/
75 : /* GetLayer() */
76 : /************************************************************************/
77 :
78 44 : OGRLayer *OGRSDTSDataSource::GetLayer(int iLayer)
79 :
80 : {
81 44 : if (iLayer < 0 || iLayer >= nLayers)
82 0 : return nullptr;
83 : else
84 44 : return papoLayers[iLayer];
85 : }
86 :
87 : /************************************************************************/
88 : /* Open() */
89 : /************************************************************************/
90 :
91 1 : int OGRSDTSDataSource::Open(const char *pszFilename, int bTestOpen)
92 :
93 : {
94 1 : pszName = CPLStrdup(pszFilename);
95 :
96 : /* -------------------------------------------------------------------- */
97 : /* Verify that the extension is DDF if we are testopening. */
98 : /* -------------------------------------------------------------------- */
99 1 : if (bTestOpen && !(strlen(pszFilename) > 4 &&
100 1 : EQUAL(pszFilename + strlen(pszFilename) - 4, ".ddf")))
101 0 : return FALSE;
102 :
103 : /* -------------------------------------------------------------------- */
104 : /* Check a few bits of the header to see if it looks like an */
105 : /* SDTS file (really, if it looks like an ISO8211 file). */
106 : /* -------------------------------------------------------------------- */
107 1 : if (bTestOpen)
108 : {
109 1 : VSILFILE *fp = VSIFOpenL(pszFilename, "rb");
110 1 : if (fp == nullptr)
111 0 : return FALSE;
112 :
113 1 : char pachLeader[10] = {};
114 1 : if (VSIFReadL(pachLeader, 1, 10, fp) != 10 ||
115 1 : (pachLeader[5] != '1' && pachLeader[5] != '2' &&
116 0 : pachLeader[5] != '3') ||
117 3 : pachLeader[6] != 'L' ||
118 1 : (pachLeader[8] != '1' && pachLeader[8] != ' '))
119 : {
120 0 : VSIFCloseL(fp);
121 0 : return FALSE;
122 : }
123 :
124 1 : VSIFCloseL(fp);
125 : }
126 :
127 : /* -------------------------------------------------------------------- */
128 : /* Create a transfer, and open it. */
129 : /* -------------------------------------------------------------------- */
130 1 : poTransfer = new SDTSTransfer();
131 :
132 1 : GUInt32 nInitialErrorCounter = CPLGetErrorCounter();
133 2 : if (!poTransfer->Open(pszFilename) ||
134 1 : CPLGetErrorCounter() > nInitialErrorCounter + 100)
135 : {
136 0 : delete poTransfer;
137 0 : poTransfer = nullptr;
138 :
139 0 : return FALSE;
140 : }
141 :
142 : /* -------------------------------------------------------------------- */
143 : /* Initialize the projection. */
144 : /* -------------------------------------------------------------------- */
145 1 : SDTS_XREF *poXREF = poTransfer->GetXREF();
146 :
147 1 : poSRS = new OGRSpatialReference();
148 1 : poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
149 :
150 1 : if (EQUAL(poXREF->pszSystemName, "UTM"))
151 : {
152 1 : poSRS->SetUTM(poXREF->nZone, TRUE);
153 : }
154 :
155 1 : if (EQUAL(poXREF->pszDatum, "NAS"))
156 1 : poSRS->SetGeogCS("NAD27", "North_American_Datum_1927", "Clarke 1866",
157 : 6378206.4, 294.978698213901);
158 :
159 0 : else if (EQUAL(poXREF->pszDatum, "NAX"))
160 0 : poSRS->SetGeogCS("NAD83", "North_American_Datum_1983", "GRS 1980",
161 : 6378137, 298.257222101);
162 :
163 0 : else if (EQUAL(poXREF->pszDatum, "WGC"))
164 0 : poSRS->SetGeogCS("WGS 72", "WGS_1972", "NWL 10D", 6378135, 298.26);
165 :
166 : else /* if( EQUAL(poXREF->pszDatum,"WGE") ) or default case */
167 0 : poSRS->SetGeogCS("WGS 84", "WGS_1984", "WGS 84", 6378137,
168 : 298.257223563);
169 :
170 : /* -------------------------------------------------------------------- */
171 : /* Initialize a layer for each source dataset layer. */
172 : /* -------------------------------------------------------------------- */
173 :
174 9 : for (int iLayer = 0; iLayer < poTransfer->GetLayerCount(); iLayer++)
175 : {
176 8 : if (poTransfer->GetLayerType(iLayer) == SLTRaster)
177 0 : continue;
178 :
179 8 : SDTSIndexedReader *poReader = poTransfer->GetLayerIndexedReader(iLayer);
180 8 : if (poReader == nullptr)
181 0 : continue;
182 8 : if (CPLGetErrorCounter() > nInitialErrorCounter + 100)
183 0 : return FALSE;
184 :
185 16 : papoLayers =
186 8 : (OGRSDTSLayer **)CPLRealloc(papoLayers, sizeof(void *) * ++nLayers);
187 8 : papoLayers[nLayers - 1] = new OGRSDTSLayer(poTransfer, iLayer, this);
188 : }
189 :
190 1 : return TRUE;
191 : }
|