/src/gdal/frmts/adrg/srpdataset.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * Purpose: ASRP/USRP Reader |
3 | | * Author: Frank Warmerdam (warmerdam@pobox.com) |
4 | | * |
5 | | * Derived from ADRG driver by Even Rouault, even.rouault at spatialys.com. |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com> |
9 | | * Copyright (c) 2009, Frank Warmerdam |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "cpl_string.h" |
15 | | #include "gdal_pam.h" |
16 | | #include "gdal_frmts.h" |
17 | | #include "iso8211.h" |
18 | | #include "ogr_spatialref.h" |
19 | | |
20 | | #include <cstdlib> |
21 | | #include <algorithm> |
22 | | #include <limits> |
23 | | |
24 | | // Uncomment to recognize also .gen files in addition to .img files |
25 | | // #define OPEN_GEN |
26 | | |
27 | | class SRPDataset final : public GDALPamDataset |
28 | | { |
29 | | friend class SRPRasterBand; |
30 | | |
31 | | static CPLString ResetTo01(const char *str); |
32 | | |
33 | | VSILFILE *fdIMG; |
34 | | int *TILEINDEX; |
35 | | int offsetInIMG; |
36 | | CPLString osProduct; |
37 | | OGRSpatialReference m_oSRS{}; |
38 | | CPLString osGENFileName; |
39 | | CPLString osQALFileName; |
40 | | CPLString osIMGFileName; |
41 | | int NFC; |
42 | | int NFL; |
43 | | int ZNA; |
44 | | double LSO; |
45 | | double PSO; |
46 | | double LOD; |
47 | | double LAD; |
48 | | int ARV; |
49 | | int BRV; |
50 | | int PCB; |
51 | | int PVB; |
52 | | |
53 | | char **papszSubDatasets; |
54 | | |
55 | | GDALColorTable oCT; |
56 | | |
57 | | static char **GetGENListFromTHF(const char *pszFileName); |
58 | | static char **GetIMGListFromGEN(const char *pszFileName, |
59 | | int *pnRecordIndex = nullptr); |
60 | | static SRPDataset *OpenDataset(const char *pszGENFileName, |
61 | | const char *pszIMGFileName, |
62 | | DDFRecord *record = nullptr); |
63 | | static DDFRecord *FindRecordInGENForIMG(DDFModule &module, |
64 | | const char *pszGENFileName, |
65 | | const char *pszIMGFileName); |
66 | | |
67 | | public: |
68 | | SRPDataset(); |
69 | | ~SRPDataset() override; |
70 | | |
71 | | const OGRSpatialReference *GetSpatialRef() const override; |
72 | | CPLErr GetGeoTransform(double *padfGeoTransform) override; |
73 | | |
74 | | char **GetMetadata(const char *pszDomain = "") override; |
75 | | |
76 | | char **GetFileList() override; |
77 | | |
78 | | bool GetFromRecord(const char *pszFileName, DDFRecord *record); |
79 | | void AddSubDataset(const char *pszGENFileName, const char *pszIMGFileName); |
80 | | void AddMetadatafromFromTHF(const char *pszFileName); |
81 | | |
82 | | static GDALDataset *Open(GDALOpenInfo *); |
83 | | }; |
84 | | |
85 | | /************************************************************************/ |
86 | | /* ==================================================================== */ |
87 | | /* SRPRasterBand */ |
88 | | /* ==================================================================== */ |
89 | | /************************************************************************/ |
90 | | |
91 | | class SRPRasterBand final : public GDALPamRasterBand |
92 | | { |
93 | | friend class SRPDataset; |
94 | | |
95 | | public: |
96 | | SRPRasterBand(SRPDataset *, int); |
97 | | |
98 | | CPLErr IReadBlock(int, int, void *) override; |
99 | | |
100 | | double GetNoDataValue(int *pbSuccess = nullptr) override; |
101 | | |
102 | | GDALColorInterp GetColorInterpretation() override; |
103 | | GDALColorTable *GetColorTable() override; |
104 | | }; |
105 | | |
106 | | /************************************************************************/ |
107 | | /* SRPRasterBand() */ |
108 | | /************************************************************************/ |
109 | | |
110 | | SRPRasterBand::SRPRasterBand(SRPDataset *poDSIn, int nBandIn) |
111 | | |
112 | 0 | { |
113 | 0 | poDS = poDSIn; |
114 | 0 | nBand = nBandIn; |
115 | |
|
116 | 0 | eDataType = GDT_Byte; |
117 | |
|
118 | 0 | nBlockXSize = 128; |
119 | 0 | nBlockYSize = 128; |
120 | 0 | } |
121 | | |
122 | | /************************************************************************/ |
123 | | /* GetNoDataValue() */ |
124 | | /************************************************************************/ |
125 | | |
126 | | double SRPRasterBand::GetNoDataValue(int *pbSuccess) |
127 | 0 | { |
128 | 0 | if (pbSuccess) |
129 | 0 | *pbSuccess = TRUE; |
130 | |
|
131 | 0 | return 0; |
132 | 0 | } |
133 | | |
134 | | /************************************************************************/ |
135 | | /* GetColorInterpretation() */ |
136 | | /************************************************************************/ |
137 | | |
138 | | GDALColorInterp SRPRasterBand::GetColorInterpretation() |
139 | | |
140 | 0 | { |
141 | 0 | SRPDataset *l_poDS = cpl::down_cast<SRPDataset *>(poDS); |
142 | |
|
143 | 0 | if (l_poDS->oCT.GetColorEntryCount() > 0) |
144 | 0 | return GCI_PaletteIndex; |
145 | 0 | else |
146 | 0 | return GCI_GrayIndex; |
147 | 0 | } |
148 | | |
149 | | /************************************************************************/ |
150 | | /* GetColorTable() */ |
151 | | /************************************************************************/ |
152 | | |
153 | | GDALColorTable *SRPRasterBand::GetColorTable() |
154 | | |
155 | 0 | { |
156 | 0 | SRPDataset *l_poDS = cpl::down_cast<SRPDataset *>(poDS); |
157 | |
|
158 | 0 | if (l_poDS->oCT.GetColorEntryCount() > 0) |
159 | 0 | return &(l_poDS->oCT); |
160 | 0 | else |
161 | 0 | return nullptr; |
162 | 0 | } |
163 | | |
164 | | /************************************************************************/ |
165 | | /* IReadBlock() */ |
166 | | /************************************************************************/ |
167 | | |
168 | | CPLErr SRPRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) |
169 | | |
170 | 0 | { |
171 | 0 | SRPDataset *l_poDS = cpl::down_cast<SRPDataset *>(poDS); |
172 | 0 | vsi_l_offset offset; |
173 | 0 | int nBlock = nBlockYOff * l_poDS->NFC + nBlockXOff; |
174 | 0 | if (nBlockXOff >= l_poDS->NFC || nBlockYOff >= l_poDS->NFL) |
175 | 0 | { |
176 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
177 | 0 | "nBlockXOff=%d, NFC=%d, nBlockYOff=%d, NFL=%d", nBlockXOff, |
178 | 0 | l_poDS->NFC, nBlockYOff, l_poDS->NFL); |
179 | 0 | return CE_Failure; |
180 | 0 | } |
181 | | |
182 | | /* -------------------------------------------------------------------- */ |
183 | | /* Is this a null block? */ |
184 | | /* -------------------------------------------------------------------- */ |
185 | 0 | if (l_poDS->TILEINDEX && l_poDS->TILEINDEX[nBlock] <= 0) |
186 | 0 | { |
187 | 0 | memset(pImage, 0, 128 * 128); |
188 | 0 | return CE_None; |
189 | 0 | } |
190 | | |
191 | | /* -------------------------------------------------------------------- */ |
192 | | /* Compute the offset to the block. */ |
193 | | /* -------------------------------------------------------------------- */ |
194 | 0 | if (l_poDS->TILEINDEX) |
195 | 0 | { |
196 | 0 | if (l_poDS->PCB == 0) // uncompressed |
197 | 0 | offset = l_poDS->offsetInIMG + |
198 | 0 | static_cast<vsi_l_offset>(l_poDS->TILEINDEX[nBlock] - 1) * |
199 | 0 | 128 * 128; |
200 | 0 | else // compressed |
201 | 0 | offset = l_poDS->offsetInIMG + |
202 | 0 | static_cast<vsi_l_offset>(l_poDS->TILEINDEX[nBlock] - 1); |
203 | 0 | } |
204 | 0 | else |
205 | 0 | offset = |
206 | 0 | l_poDS->offsetInIMG + static_cast<vsi_l_offset>(nBlock) * 128 * 128; |
207 | | |
208 | | /* -------------------------------------------------------------------- */ |
209 | | /* Seek to target location. */ |
210 | | /* -------------------------------------------------------------------- */ |
211 | 0 | if (VSIFSeekL(l_poDS->fdIMG, offset, SEEK_SET) != 0) |
212 | 0 | { |
213 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
214 | 0 | "Cannot seek to offset " CPL_FRMT_GUIB, offset); |
215 | 0 | return CE_Failure; |
216 | 0 | } |
217 | | |
218 | | /* -------------------------------------------------------------------- */ |
219 | | /* For uncompressed case we read the 128x128 and return with no */ |
220 | | /* further processing. */ |
221 | | /* -------------------------------------------------------------------- */ |
222 | 0 | if (l_poDS->PCB == 0) |
223 | 0 | { |
224 | 0 | if (VSIFReadL(pImage, 1, 128 * 128, l_poDS->fdIMG) != 128 * 128) |
225 | 0 | { |
226 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
227 | 0 | "Cannot read data at offset " CPL_FRMT_GUIB, offset); |
228 | 0 | return CE_Failure; |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | | /* -------------------------------------------------------------------- */ |
233 | | /* If this is compressed data, we read a goodly chunk of data */ |
234 | | /* and then decode it. */ |
235 | | /* -------------------------------------------------------------------- */ |
236 | 0 | else |
237 | 0 | { |
238 | 0 | const int nBufSize = 128 * 128 * 2; |
239 | 0 | GByte *pabyCData = (GByte *)CPLCalloc(nBufSize, 1); |
240 | |
|
241 | 0 | const int nBytesRead = |
242 | 0 | static_cast<int>(VSIFReadL(pabyCData, 1, nBufSize, l_poDS->fdIMG)); |
243 | 0 | if (nBytesRead == 0) |
244 | 0 | { |
245 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
246 | 0 | "Cannot read data at offset " CPL_FRMT_GUIB, offset); |
247 | 0 | CPLFree(pabyCData); |
248 | 0 | return CE_Failure; |
249 | 0 | } |
250 | | |
251 | 0 | CPLAssert(l_poDS->PVB == 8); |
252 | 0 | CPLAssert(l_poDS->PCB == 4 || l_poDS->PCB == 8); |
253 | |
|
254 | 0 | bool bHalfByteUsed = false; |
255 | 0 | for (int iSrc = 0, iPixel = 0; iPixel < 128 * 128;) |
256 | 0 | { |
257 | 0 | if (iSrc + 2 > nBytesRead) |
258 | 0 | { |
259 | 0 | CPLFree(pabyCData); |
260 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
261 | 0 | "Out of data decoding image block, only %d available.", |
262 | 0 | iSrc); |
263 | 0 | return CE_Failure; |
264 | 0 | } |
265 | | |
266 | 0 | int nCount = 0; |
267 | 0 | int nValue = 0; |
268 | |
|
269 | 0 | if (l_poDS->PCB == 8) |
270 | 0 | { |
271 | 0 | nCount = pabyCData[iSrc++]; |
272 | 0 | nValue = pabyCData[iSrc++]; |
273 | 0 | } |
274 | 0 | else if (l_poDS->PCB == 4) |
275 | 0 | { |
276 | 0 | if ((iPixel % 128) == 0 && bHalfByteUsed) |
277 | 0 | { |
278 | 0 | iSrc++; |
279 | 0 | bHalfByteUsed = false; |
280 | 0 | continue; |
281 | 0 | } |
282 | | |
283 | 0 | if (bHalfByteUsed) |
284 | 0 | { |
285 | 0 | nCount = pabyCData[iSrc++] & 0xf; |
286 | 0 | nValue = pabyCData[iSrc++]; |
287 | 0 | bHalfByteUsed = false; |
288 | 0 | } |
289 | 0 | else |
290 | 0 | { |
291 | 0 | nCount = pabyCData[iSrc] >> 4; |
292 | 0 | nValue = ((pabyCData[iSrc] & 0xf) << 4) + |
293 | 0 | (pabyCData[iSrc + 1] >> 4); |
294 | 0 | bHalfByteUsed = true; |
295 | 0 | iSrc++; |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | 0 | if (iPixel + nCount > 128 * 128) |
300 | 0 | { |
301 | 0 | CPLFree(pabyCData); |
302 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
303 | 0 | "Too much data decoding image block, likely corrupt."); |
304 | 0 | return CE_Failure; |
305 | 0 | } |
306 | | |
307 | 0 | while (nCount > 0) |
308 | 0 | { |
309 | 0 | ((GByte *)pImage)[iPixel++] = (GByte)nValue; |
310 | 0 | nCount--; |
311 | 0 | } |
312 | 0 | } |
313 | | |
314 | 0 | CPLFree(pabyCData); |
315 | 0 | } |
316 | | |
317 | 0 | return CE_None; |
318 | 0 | } |
319 | | |
320 | | /************************************************************************/ |
321 | | /* SRPDataset() */ |
322 | | /************************************************************************/ |
323 | | |
324 | | SRPDataset::SRPDataset() |
325 | 0 | : fdIMG(nullptr), TILEINDEX(nullptr), offsetInIMG(0), NFC(0), NFL(0), |
326 | 0 | ZNA(0), LSO(0.0), PSO(0.0), LOD(0.0), LAD(0.0), ARV(0), BRV(0), PCB(0), |
327 | 0 | PVB(0), papszSubDatasets(nullptr) |
328 | 0 | { |
329 | 0 | m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
330 | 0 | } |
331 | | |
332 | | /************************************************************************/ |
333 | | /* ~SRPDataset() */ |
334 | | /************************************************************************/ |
335 | | |
336 | | SRPDataset::~SRPDataset() |
337 | 0 | { |
338 | 0 | CSLDestroy(papszSubDatasets); |
339 | |
|
340 | 0 | if (fdIMG) |
341 | 0 | { |
342 | 0 | VSIFCloseL(fdIMG); |
343 | 0 | } |
344 | |
|
345 | 0 | if (TILEINDEX) |
346 | 0 | { |
347 | 0 | delete[] TILEINDEX; |
348 | 0 | } |
349 | 0 | } |
350 | | |
351 | | /************************************************************************/ |
352 | | /* ResetTo01() */ |
353 | | /* Replace the DD in ZZZZZZDD.XXX with 01. */ |
354 | | /************************************************************************/ |
355 | | |
356 | | CPLString SRPDataset::ResetTo01(const char *str) |
357 | 0 | { |
358 | 0 | CPLString osResult = str; |
359 | |
|
360 | 0 | osResult[6] = '0'; |
361 | 0 | osResult[7] = '1'; |
362 | |
|
363 | 0 | return osResult; |
364 | 0 | } |
365 | | |
366 | | /************************************************************************/ |
367 | | /* GetSpatialRef() */ |
368 | | /************************************************************************/ |
369 | | |
370 | | const OGRSpatialReference *SRPDataset::GetSpatialRef() const |
371 | 0 | { |
372 | 0 | return m_oSRS.IsEmpty() ? nullptr : &m_oSRS; |
373 | 0 | } |
374 | | |
375 | | /************************************************************************/ |
376 | | /* GetGeoTransform() */ |
377 | | /************************************************************************/ |
378 | | |
379 | | CPLErr SRPDataset::GetGeoTransform(double *padfGeoTransform) |
380 | 0 | { |
381 | 0 | if (EQUAL(osProduct, "ASRP")) |
382 | 0 | { |
383 | 0 | if (ARV == 0) |
384 | 0 | return CE_Failure; |
385 | 0 | if (ZNA == 9) |
386 | 0 | { |
387 | | // North Polar Case |
388 | 0 | padfGeoTransform[0] = 111319.4907933 * (90.0 - PSO / 3600.0) * |
389 | 0 | sin(LSO * M_PI / 648000.0); |
390 | 0 | padfGeoTransform[1] = 40075016.68558 / ARV; |
391 | 0 | padfGeoTransform[2] = 0.0; |
392 | 0 | padfGeoTransform[3] = -111319.4907933 * (90.0 - PSO / 3600.0) * |
393 | 0 | cos(LSO * M_PI / 648000.0); |
394 | 0 | padfGeoTransform[4] = 0.0; |
395 | 0 | padfGeoTransform[5] = -40075016.68558 / ARV; |
396 | 0 | } |
397 | 0 | else if (ZNA == 18) |
398 | 0 | { |
399 | | // South Polar Case |
400 | 0 | padfGeoTransform[0] = 111319.4907933 * (90.0 + PSO / 3600.0) * |
401 | 0 | sin(LSO * M_PI / 648000.0); |
402 | 0 | padfGeoTransform[1] = 40075016.68558 / ARV; |
403 | 0 | padfGeoTransform[2] = 0.0; |
404 | 0 | padfGeoTransform[3] = 111319.4907933 * (90.0 + PSO / 3600.0) * |
405 | 0 | cos(LSO * M_PI / 648000.0); |
406 | 0 | padfGeoTransform[4] = 0.0; |
407 | 0 | padfGeoTransform[5] = -40075016.68558 / ARV; |
408 | 0 | } |
409 | 0 | else |
410 | 0 | { |
411 | 0 | if (BRV == 0) |
412 | 0 | return CE_Failure; |
413 | 0 | padfGeoTransform[0] = LSO / 3600.0; |
414 | 0 | padfGeoTransform[1] = 360. / ARV; |
415 | 0 | padfGeoTransform[2] = 0.0; |
416 | 0 | padfGeoTransform[3] = PSO / 3600.0; |
417 | 0 | padfGeoTransform[4] = 0.0; |
418 | 0 | padfGeoTransform[5] = -360. / BRV; |
419 | 0 | } |
420 | | |
421 | 0 | return CE_None; |
422 | 0 | } |
423 | 0 | else if (EQUAL(osProduct, "USRP")) |
424 | 0 | { |
425 | 0 | padfGeoTransform[0] = LSO; |
426 | 0 | padfGeoTransform[1] = LOD; |
427 | 0 | padfGeoTransform[2] = 0.0; |
428 | 0 | padfGeoTransform[3] = PSO; |
429 | 0 | padfGeoTransform[4] = 0.0; |
430 | 0 | padfGeoTransform[5] = -LAD; |
431 | 0 | return CE_None; |
432 | 0 | } |
433 | | |
434 | 0 | return CE_Failure; |
435 | 0 | } |
436 | | |
437 | | /************************************************************************/ |
438 | | /* GetFromRecord() */ |
439 | | /************************************************************************/ |
440 | | |
441 | | bool SRPDataset::GetFromRecord(const char *pszFileName, DDFRecord *record) |
442 | 0 | { |
443 | 0 | int bSuccess; |
444 | | |
445 | | /* -------------------------------------------------------------------- */ |
446 | | /* Read a variety of header fields of interest from the .GEN */ |
447 | | /* file. */ |
448 | | /* -------------------------------------------------------------------- */ |
449 | 0 | const int nSTR = record->GetIntSubfield("GEN", 0, "STR", 0, &bSuccess); |
450 | 0 | if (!bSuccess || nSTR != 4) |
451 | 0 | { |
452 | 0 | CPLDebug("SRP", "Failed to extract STR, or not 4."); |
453 | 0 | return false; |
454 | 0 | } |
455 | | |
456 | 0 | const int SCA = record->GetIntSubfield("GEN", 0, "SCA", 0, &bSuccess); |
457 | 0 | CPLDebug("SRP", "SCA=%d", SCA); |
458 | |
|
459 | 0 | ZNA = record->GetIntSubfield("GEN", 0, "ZNA", 0, &bSuccess); |
460 | 0 | CPLDebug("SRP", "ZNA=%d", ZNA); |
461 | |
|
462 | 0 | const double PSP = record->GetFloatSubfield("GEN", 0, "PSP", 0, &bSuccess); |
463 | 0 | CPLDebug("SRP", "PSP=%f", PSP); |
464 | |
|
465 | 0 | ARV = record->GetIntSubfield("GEN", 0, "ARV", 0, &bSuccess); |
466 | 0 | CPLDebug("SRP", "ARV=%d", ARV); |
467 | |
|
468 | 0 | BRV = record->GetIntSubfield("GEN", 0, "BRV", 0, &bSuccess); |
469 | 0 | CPLDebug("SRP", "BRV=%d", BRV); |
470 | |
|
471 | 0 | LSO = record->GetFloatSubfield("GEN", 0, "LSO", 0, &bSuccess); |
472 | 0 | CPLDebug("SRP", "LSO=%f", LSO); |
473 | |
|
474 | 0 | PSO = record->GetFloatSubfield("GEN", 0, "PSO", 0, &bSuccess); |
475 | 0 | CPLDebug("SRP", "PSO=%f", PSO); |
476 | |
|
477 | 0 | LAD = record->GetFloatSubfield("GEN", 0, "LAD", 0); |
478 | 0 | LOD = record->GetFloatSubfield("GEN", 0, "LOD", 0); |
479 | |
|
480 | 0 | NFL = record->GetIntSubfield("SPR", 0, "NFL", 0, &bSuccess); |
481 | 0 | CPLDebug("SRP", "NFL=%d", NFL); |
482 | |
|
483 | 0 | NFC = record->GetIntSubfield("SPR", 0, "NFC", 0, &bSuccess); |
484 | 0 | CPLDebug("SRP", "NFC=%d", NFC); |
485 | |
|
486 | 0 | const auto knIntMax = std::numeric_limits<int>::max(); |
487 | 0 | if (NFL <= 0 || NFC <= 0 || NFL > knIntMax / 128 || NFC > knIntMax / 128 || |
488 | 0 | NFL > knIntMax / NFC) |
489 | 0 | { |
490 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid NFL / NFC values"); |
491 | 0 | return false; |
492 | 0 | } |
493 | | |
494 | 0 | const int PNC = record->GetIntSubfield("SPR", 0, "PNC", 0, &bSuccess); |
495 | 0 | CPLDebug("SRP", "PNC=%d", PNC); |
496 | |
|
497 | 0 | const int PNL = record->GetIntSubfield("SPR", 0, "PNL", 0, &bSuccess); |
498 | 0 | CPLDebug("SRP", "PNL=%d", PNL); |
499 | |
|
500 | 0 | if (PNL != 128 || PNC != 128) |
501 | 0 | { |
502 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Unsupported PNL or PNC value."); |
503 | 0 | return false; |
504 | 0 | } |
505 | | |
506 | 0 | PCB = record->GetIntSubfield("SPR", 0, "PCB", 0); |
507 | 0 | PVB = record->GetIntSubfield("SPR", 0, "PVB", 0); |
508 | 0 | if ((PCB != 8 && PCB != 4 && PCB != 0) || PVB != 8) |
509 | 0 | { |
510 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
511 | 0 | "PCB(%d) or PVB(%d) value unsupported.", PCB, PVB); |
512 | 0 | return false; |
513 | 0 | } |
514 | | |
515 | 0 | const char *pszBAD = |
516 | 0 | record->GetStringSubfield("SPR", 0, "BAD", 0, &bSuccess); |
517 | 0 | if (pszBAD == nullptr) |
518 | 0 | return false; |
519 | 0 | const CPLString osBAD = pszBAD; |
520 | 0 | { |
521 | 0 | char *c = (char *)strchr(osBAD, ' '); |
522 | 0 | if (c) |
523 | 0 | *c = 0; |
524 | 0 | } |
525 | 0 | CPLDebug("SRP", "BAD=%s", osBAD.c_str()); |
526 | | |
527 | | /* -------------------------------------------------------------------- */ |
528 | | /* Read the tile map if available. */ |
529 | | /* -------------------------------------------------------------------- */ |
530 | 0 | const char *pszTIF = record->GetStringSubfield("SPR", 0, "TIF", 0); |
531 | 0 | const bool TIF = pszTIF != nullptr && EQUAL(pszTIF, "Y"); |
532 | 0 | CPLDebug("SRP", "TIF=%s", TIF ? "true" : "false"); |
533 | |
|
534 | 0 | if (TIF) |
535 | 0 | { |
536 | 0 | DDFField *field = record->FindField("TIM"); |
537 | 0 | if (field == nullptr) |
538 | 0 | return false; |
539 | | |
540 | 0 | const DDFFieldDefn *fieldDefn = field->GetFieldDefn(); |
541 | 0 | const DDFSubfieldDefn *subfieldDefn = |
542 | 0 | fieldDefn->FindSubfieldDefn("TSI"); |
543 | 0 | if (subfieldDefn == nullptr) |
544 | 0 | return false; |
545 | | |
546 | 0 | const int nIndexValueWidth = subfieldDefn->GetWidth(); |
547 | |
|
548 | 0 | char offset[30] = {0}; |
549 | | /* Should be strict comparison, but apparently a few datasets */ |
550 | | /* have GetDataSize() greater than the required minimum (#3862) */ |
551 | 0 | if (nIndexValueWidth <= 0 || |
552 | 0 | static_cast<size_t>(nIndexValueWidth) >= sizeof(offset) || |
553 | 0 | nIndexValueWidth > (INT_MAX - 1) / (NFL * NFC) || |
554 | 0 | field->GetDataSize() < nIndexValueWidth * NFL * NFC + 1) |
555 | 0 | { |
556 | 0 | return false; |
557 | 0 | } |
558 | | |
559 | 0 | try |
560 | 0 | { |
561 | 0 | TILEINDEX = new int[NFL * NFC]; |
562 | 0 | } |
563 | 0 | catch (const std::exception &) |
564 | 0 | { |
565 | 0 | return false; |
566 | 0 | } |
567 | 0 | const char *ptr = field->GetData(); |
568 | 0 | offset[nIndexValueWidth] = '\0'; |
569 | |
|
570 | 0 | for (int i = 0; i < NFL * NFC; i++) |
571 | 0 | { |
572 | 0 | strncpy(offset, ptr, nIndexValueWidth); |
573 | 0 | ptr += nIndexValueWidth; |
574 | 0 | TILEINDEX[i] = atoi(offset); |
575 | | // CPLDebug("SRP", "TSI[%d]=%d", i, TILEINDEX[i]); |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | | /* -------------------------------------------------------------------- */ |
580 | | /* Open the .IMG file. Try to recover gracefully if the case */ |
581 | | /* of the filename is wrong. */ |
582 | | /* -------------------------------------------------------------------- */ |
583 | 0 | const CPLString osDirname = CPLGetDirnameSafe(pszFileName); |
584 | 0 | const CPLString osImgName = |
585 | 0 | CPLFormCIFilenameSafe(osDirname, osBAD, nullptr); |
586 | |
|
587 | 0 | fdIMG = VSIFOpenL(osImgName, "rb"); |
588 | 0 | if (fdIMG == nullptr) |
589 | 0 | { |
590 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot find %s", |
591 | 0 | osImgName.c_str()); |
592 | 0 | return false; |
593 | 0 | } |
594 | | |
595 | | /* -------------------------------------------------------------------- */ |
596 | | /* Establish the offset to the first byte of actual image data */ |
597 | | /* in the IMG file, skipping the ISO8211 header. */ |
598 | | /* */ |
599 | | /* This code is awfully fragile! */ |
600 | | /* -------------------------------------------------------------------- */ |
601 | 0 | char c; |
602 | 0 | if (VSIFReadL(&c, 1, 1, fdIMG) != 1) |
603 | 0 | { |
604 | 0 | return false; |
605 | 0 | } |
606 | 0 | while (!VSIFEofL(fdIMG)) |
607 | 0 | { |
608 | 0 | if (c == 30) |
609 | 0 | { |
610 | 0 | char recordName[3] = {}; |
611 | 0 | if (VSIFReadL(recordName, 1, 3, fdIMG) != 3) |
612 | 0 | { |
613 | 0 | return false; |
614 | 0 | } |
615 | 0 | offsetInIMG += 3; |
616 | 0 | if (STARTS_WITH(recordName, "IMG")) |
617 | 0 | { |
618 | 0 | offsetInIMG += 4; |
619 | 0 | if (VSIFSeekL(fdIMG, 3, SEEK_CUR) != 0) |
620 | 0 | { |
621 | 0 | return false; |
622 | 0 | } |
623 | 0 | if (VSIFReadL(&c, 1, 1, fdIMG) != 1) |
624 | 0 | { |
625 | 0 | return false; |
626 | 0 | } |
627 | 0 | while (c != 30) |
628 | 0 | { |
629 | 0 | offsetInIMG++; |
630 | 0 | if (VSIFReadL(&c, 1, 1, fdIMG) != 1) |
631 | 0 | { |
632 | 0 | return false; |
633 | 0 | } |
634 | 0 | } |
635 | 0 | offsetInIMG++; |
636 | 0 | break; |
637 | 0 | } |
638 | 0 | } |
639 | | |
640 | 0 | offsetInIMG++; |
641 | 0 | if (VSIFReadL(&c, 1, 1, fdIMG) != 1) |
642 | 0 | { |
643 | 0 | return false; |
644 | 0 | } |
645 | 0 | } |
646 | | |
647 | 0 | if (VSIFEofL(fdIMG)) |
648 | 0 | { |
649 | 0 | return false; |
650 | 0 | } |
651 | | |
652 | 0 | CPLDebug("SRP", "Img offset data = %d", offsetInIMG); |
653 | | |
654 | | /* -------------------------------------------------------------------- */ |
655 | | /* Establish the SRP Dataset. */ |
656 | | /* -------------------------------------------------------------------- */ |
657 | 0 | nRasterXSize = NFC * 128; |
658 | 0 | nRasterYSize = NFL * 128; |
659 | |
|
660 | 0 | char szValue[32] = {}; |
661 | 0 | snprintf(szValue, sizeof(szValue), "%d", SCA); |
662 | 0 | SetMetadataItem("SRP_SCA", szValue); |
663 | | |
664 | | // PSP Pixel Spacing, Microns at capture stage {000.0 - 100.0} |
665 | 0 | snprintf(szValue, sizeof(szValue), "%3.1f", PSP); |
666 | 0 | SetMetadataItem("SRP_PSP", szValue); |
667 | |
|
668 | 0 | nBands = 1; |
669 | 0 | for (int i = 0; i < nBands; i++) |
670 | 0 | SetBand(i + 1, new SRPRasterBand(this, i + 1)); |
671 | | |
672 | | /* -------------------------------------------------------------------- */ |
673 | | /* Try to collect a color map from the .QAL file. */ |
674 | | /* -------------------------------------------------------------------- */ |
675 | 0 | const CPLString osBasename = CPLGetBasenameSafe(pszFileName); |
676 | 0 | osQALFileName = CPLFormCIFilenameSafe(osDirname, osBasename, "QAL"); |
677 | |
|
678 | 0 | DDFModule oQALModule; |
679 | |
|
680 | 0 | if (oQALModule.Open(osQALFileName, TRUE)) |
681 | 0 | { |
682 | 0 | while ((record = oQALModule.ReadRecord()) != nullptr) |
683 | 0 | { |
684 | 0 | if (record->FindField("COL") != nullptr) |
685 | 0 | { |
686 | 0 | const int nColorCount = |
687 | 0 | std::min(256, record->FindField("COL")->GetRepeatCount()); |
688 | |
|
689 | 0 | for (int iColor = 0; iColor < nColorCount; iColor++) |
690 | 0 | { |
691 | 0 | const int nCCD = record->GetIntSubfield("COL", 0, "CCD", |
692 | 0 | iColor, &bSuccess); |
693 | 0 | if (!bSuccess || nCCD < 0 || nCCD > 255) |
694 | 0 | break; |
695 | | |
696 | 0 | int nNSR = record->GetIntSubfield("COL", 0, "NSR", iColor); |
697 | 0 | int nNSG = record->GetIntSubfield("COL", 0, "NSG", iColor); |
698 | 0 | int nNSB = record->GetIntSubfield("COL", 0, "NSB", iColor); |
699 | |
|
700 | 0 | GDALColorEntry sEntry = {static_cast<short>(nNSR), |
701 | 0 | static_cast<short>(nNSG), |
702 | 0 | static_cast<short>(nNSB), 255}; |
703 | |
|
704 | 0 | oCT.SetColorEntry(nCCD, &sEntry); |
705 | 0 | } |
706 | 0 | } |
707 | |
|
708 | 0 | if (record->FindField("QUV") != nullptr) |
709 | 0 | { |
710 | | // TODO: Translate to English or state why this should not be in |
711 | | // English. |
712 | | // Date de production du produit : QAL.QUV.DAT1 |
713 | | // Numero d'edition du produit : QAL.QUV.EDN |
714 | |
|
715 | 0 | const int EDN = |
716 | 0 | record->GetIntSubfield("QUV", 0, "EDN", 0, &bSuccess); |
717 | 0 | if (bSuccess) |
718 | 0 | { |
719 | 0 | CPLDebug("SRP", "EDN=%d", EDN); |
720 | 0 | snprintf(szValue, sizeof(szValue), "%d", EDN); |
721 | 0 | SetMetadataItem("SRP_EDN", szValue); |
722 | 0 | } |
723 | |
|
724 | 0 | const char *pszCDV07 = |
725 | 0 | record->GetStringSubfield("QUV", 0, "CDV07", 0); |
726 | 0 | if (pszCDV07 != nullptr) |
727 | 0 | SetMetadataItem("SRP_CREATIONDATE", pszCDV07); |
728 | 0 | else |
729 | 0 | { /*USRP1.2*/ |
730 | 0 | const char *pszDAT = |
731 | 0 | record->GetStringSubfield("QUV", 0, "DAT1", 0); |
732 | 0 | if (pszDAT != nullptr && strlen(pszDAT) >= 12) |
733 | 0 | { |
734 | 0 | char dat[9]; |
735 | 0 | strncpy(dat, pszDAT + 4, 8); |
736 | 0 | dat[8] = '\0'; |
737 | 0 | CPLDebug("SRP", "Record DAT %s", dat); |
738 | 0 | SetMetadataItem("SRP_CREATIONDATE", dat); |
739 | 0 | } |
740 | 0 | } |
741 | |
|
742 | 0 | const char *pszCDV24 = |
743 | 0 | record->GetStringSubfield("QUV", 0, "CDV24", 0); |
744 | 0 | if (pszCDV24 != nullptr) |
745 | 0 | { |
746 | 0 | SetMetadataItem("SRP_REVISIONDATE", pszCDV24); |
747 | 0 | } |
748 | 0 | else |
749 | 0 | { /*USRP1.2*/ |
750 | 0 | const char *pszDAT = |
751 | 0 | record->GetStringSubfield("QUV", 0, "DAT2", 0); |
752 | 0 | if (pszDAT != nullptr && strlen(pszDAT) >= 12) |
753 | 0 | { |
754 | 0 | char dat[9]; |
755 | 0 | strncpy(dat, pszDAT + 4, 8); |
756 | 0 | dat[8] = '\0'; |
757 | 0 | CPLDebug("SRP", "Record DAT %s", dat); |
758 | 0 | SetMetadataItem("SRP_REVISIONDATE", dat); |
759 | 0 | } |
760 | 0 | } |
761 | |
|
762 | 0 | const char *pszQSS = |
763 | 0 | record->GetStringSubfield("QSR", 0, "QSS", 0); |
764 | 0 | if (pszQSS != nullptr) |
765 | 0 | SetMetadataItem("SRP_CLASSIFICATION", pszQSS); |
766 | 0 | } |
767 | 0 | } |
768 | 0 | } |
769 | 0 | else |
770 | 0 | { |
771 | 0 | osQALFileName = ""; |
772 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
773 | 0 | "Unable to find .QAL file, no color table applied."); |
774 | 0 | } |
775 | | |
776 | | /* -------------------------------------------------------------------- */ |
777 | | /* Derive the coordinate system. */ |
778 | | /* -------------------------------------------------------------------- */ |
779 | 0 | if (EQUAL(osProduct, "ASRP")) |
780 | 0 | { |
781 | 0 | m_oSRS.importFromWkt(SRS_WKT_WGS84_LAT_LONG); |
782 | |
|
783 | 0 | if (ZNA == 9) |
784 | 0 | { |
785 | 0 | m_oSRS.importFromWkt( |
786 | 0 | "PROJCS[\"ARC_System_Zone_09\",GEOGCS[\"GCS_Sphere\"," |
787 | 0 | "DATUM[\"D_Sphere\",SPHEROID[\"Sphere\",6378137.0,0.0]]," |
788 | 0 | "PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]]," |
789 | 0 | "PROJECTION[\"Azimuthal_Equidistant\"]," |
790 | 0 | "PARAMETER[\"latitude_of_center\",90]," |
791 | 0 | "PARAMETER[\"longitude_of_center\",0]," |
792 | 0 | "PARAMETER[\"false_easting\",0]," |
793 | 0 | "PARAMETER[\"false_northing\",0]," |
794 | 0 | "UNIT[\"metre\",1]]"); |
795 | 0 | } |
796 | |
|
797 | 0 | if (ZNA == 18) |
798 | 0 | { |
799 | 0 | m_oSRS.importFromWkt( |
800 | 0 | "PROJCS[\"ARC_System_Zone_18\",GEOGCS[\"GCS_Sphere\"," |
801 | 0 | "DATUM[\"D_Sphere\",SPHEROID[\"Sphere\",6378137.0,0.0]]," |
802 | 0 | "PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]]," |
803 | 0 | "PROJECTION[\"Azimuthal_Equidistant\"]," |
804 | 0 | "PARAMETER[\"latitude_of_center\",-90]," |
805 | 0 | "PARAMETER[\"longitude_of_center\",0]," |
806 | 0 | "PARAMETER[\"false_easting\",0]," |
807 | 0 | "PARAMETER[\"false_northing\",0]," |
808 | 0 | "UNIT[\"metre\",1]]"); |
809 | 0 | } |
810 | 0 | } |
811 | 0 | else |
812 | 0 | { |
813 | 0 | if (ZNA >= -60 && ZNA <= 60 && ZNA != 0) |
814 | 0 | { |
815 | 0 | m_oSRS.SetUTM(std::abs(ZNA), ZNA > 0); |
816 | 0 | m_oSRS.SetWellKnownGeogCS("WGS84"); |
817 | 0 | } |
818 | 0 | else if (ZNA == 61) |
819 | 0 | { |
820 | 0 | m_oSRS.importFromEPSG(32661); // WGS84 UPS North |
821 | 0 | } |
822 | 0 | else if (ZNA == -61) |
823 | 0 | { |
824 | 0 | m_oSRS.importFromEPSG(32761); // WGS84 UPS South |
825 | 0 | } |
826 | 0 | } |
827 | |
|
828 | 0 | snprintf(szValue, sizeof(szValue), "%d", ZNA); |
829 | 0 | SetMetadataItem("SRP_ZNA", szValue); |
830 | |
|
831 | 0 | return true; |
832 | 0 | } |
833 | | |
834 | | /************************************************************************/ |
835 | | /* GetFileList() */ |
836 | | /************************************************************************/ |
837 | | |
838 | | char **SRPDataset::GetFileList() |
839 | | |
840 | 0 | { |
841 | 0 | char **papszFileList = GDALPamDataset::GetFileList(); |
842 | 0 | if (!osGENFileName.empty() && !osIMGFileName.empty()) |
843 | 0 | { |
844 | 0 | CPLString osMainFilename = GetDescription(); |
845 | 0 | VSIStatBufL sStat; |
846 | |
|
847 | 0 | const bool bMainFileReal = VSIStatL(osMainFilename, &sStat) == 0; |
848 | 0 | if (bMainFileReal) |
849 | 0 | { |
850 | 0 | CPLString osShortMainFilename = CPLGetFilename(osMainFilename); |
851 | 0 | CPLString osShortGENFileName = CPLGetFilename(osGENFileName); |
852 | 0 | if (!EQUAL(osShortMainFilename.c_str(), osShortGENFileName.c_str())) |
853 | 0 | papszFileList = |
854 | 0 | CSLAddString(papszFileList, osGENFileName.c_str()); |
855 | 0 | } |
856 | 0 | else |
857 | 0 | { |
858 | 0 | papszFileList = CSLAddString(papszFileList, osGENFileName.c_str()); |
859 | 0 | } |
860 | |
|
861 | 0 | papszFileList = CSLAddString(papszFileList, osIMGFileName.c_str()); |
862 | |
|
863 | 0 | if (!osQALFileName.empty()) |
864 | 0 | papszFileList = CSLAddString(papszFileList, osQALFileName); |
865 | 0 | } |
866 | 0 | return papszFileList; |
867 | 0 | } |
868 | | |
869 | | /************************************************************************/ |
870 | | /* AddSubDataset() */ |
871 | | /************************************************************************/ |
872 | | |
873 | | void SRPDataset::AddSubDataset(const char *pszGENFileName, |
874 | | const char *pszIMGFileName) |
875 | 0 | { |
876 | 0 | const int nCount = CSLCount(papszSubDatasets) / 2; |
877 | |
|
878 | 0 | CPLString osSubDatasetName = "SRP:"; |
879 | 0 | osSubDatasetName += pszGENFileName; |
880 | 0 | osSubDatasetName += ","; |
881 | 0 | osSubDatasetName += pszIMGFileName; |
882 | |
|
883 | 0 | char szName[80]; |
884 | 0 | snprintf(szName, sizeof(szName), "SUBDATASET_%d_NAME", nCount + 1); |
885 | 0 | papszSubDatasets = |
886 | 0 | CSLSetNameValue(papszSubDatasets, szName, osSubDatasetName); |
887 | |
|
888 | 0 | snprintf(szName, sizeof(szName), "SUBDATASET_%d_DESC", nCount + 1); |
889 | 0 | papszSubDatasets = |
890 | 0 | CSLSetNameValue(papszSubDatasets, szName, osSubDatasetName); |
891 | 0 | } |
892 | | |
893 | | /************************************************************************/ |
894 | | /* GetMetadata() */ |
895 | | /************************************************************************/ |
896 | | |
897 | | char **SRPDataset::GetMetadata(const char *pszDomain) |
898 | | |
899 | 0 | { |
900 | 0 | if (pszDomain != nullptr && EQUAL(pszDomain, "SUBDATASETS")) |
901 | 0 | return papszSubDatasets; |
902 | | |
903 | 0 | return GDALPamDataset::GetMetadata(pszDomain); |
904 | 0 | } |
905 | | |
906 | | /************************************************************************/ |
907 | | /* FindRecordInGENForIMG() */ |
908 | | /************************************************************************/ |
909 | | |
910 | | DDFRecord *SRPDataset::FindRecordInGENForIMG(DDFModule &module, |
911 | | const char *pszGENFileName, |
912 | | const char *pszIMGFileName) |
913 | 0 | { |
914 | | /* Finds the GEN file corresponding to the IMG file */ |
915 | 0 | if (!module.Open(pszGENFileName, TRUE)) |
916 | 0 | return nullptr; |
917 | | |
918 | 0 | CPLString osShortIMGFilename = CPLGetFilename(pszIMGFileName); |
919 | |
|
920 | 0 | DDFField *field = nullptr; |
921 | 0 | DDFFieldDefn *fieldDefn = nullptr; |
922 | | |
923 | | // Now finds the record. |
924 | 0 | while (true) |
925 | 0 | { |
926 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
927 | 0 | DDFRecord *record = module.ReadRecord(); |
928 | 0 | CPLPopErrorHandler(); |
929 | 0 | CPLErrorReset(); |
930 | 0 | if (record == nullptr) |
931 | 0 | return nullptr; |
932 | | |
933 | 0 | if (record->GetFieldCount() >= 5) |
934 | 0 | { |
935 | 0 | field = record->GetField(0); |
936 | 0 | fieldDefn = field->GetFieldDefn(); |
937 | 0 | if (!(strcmp(fieldDefn->GetName(), "001") == 0 && |
938 | 0 | fieldDefn->GetSubfieldCount() == 2)) |
939 | 0 | { |
940 | 0 | continue; |
941 | 0 | } |
942 | | |
943 | 0 | const char *RTY = record->GetStringSubfield("001", 0, "RTY", 0); |
944 | 0 | if (RTY == nullptr) |
945 | 0 | continue; |
946 | | /* Ignore overviews */ |
947 | 0 | if (strcmp(RTY, "OVV") == 0) |
948 | 0 | continue; |
949 | | |
950 | 0 | if (strcmp(RTY, "GIN") != 0) |
951 | 0 | continue; |
952 | | |
953 | 0 | field = record->GetField(3); |
954 | 0 | fieldDefn = field->GetFieldDefn(); |
955 | |
|
956 | 0 | if (!(strcmp(fieldDefn->GetName(), "SPR") == 0 && |
957 | 0 | fieldDefn->GetSubfieldCount() == 15)) |
958 | 0 | { |
959 | 0 | continue; |
960 | 0 | } |
961 | | |
962 | 0 | const char *pszBAD = record->GetStringSubfield("SPR", 0, "BAD", 0); |
963 | 0 | if (pszBAD == nullptr || strlen(pszBAD) != 12) |
964 | 0 | continue; |
965 | 0 | const CPLString osBAD = pszBAD; |
966 | 0 | { |
967 | 0 | char *c = (char *)strchr(osBAD.c_str(), ' '); |
968 | 0 | if (c) |
969 | 0 | *c = 0; |
970 | 0 | } |
971 | |
|
972 | 0 | if (EQUAL(osShortIMGFilename.c_str(), osBAD.c_str())) |
973 | 0 | { |
974 | 0 | return record; |
975 | 0 | } |
976 | 0 | } |
977 | 0 | } |
978 | 0 | } |
979 | | |
980 | | /************************************************************************/ |
981 | | /* OpenDataset() */ |
982 | | /************************************************************************/ |
983 | | |
984 | | SRPDataset *SRPDataset::OpenDataset(const char *pszGENFileName, |
985 | | const char *pszIMGFileName, |
986 | | DDFRecord *record) |
987 | 0 | { |
988 | 0 | DDFModule module; // Don't move this line as it holds ownership of record. |
989 | |
|
990 | 0 | if (record == nullptr) |
991 | 0 | { |
992 | 0 | record = FindRecordInGENForIMG(module, pszGENFileName, pszIMGFileName); |
993 | 0 | if (record == nullptr) |
994 | 0 | return nullptr; |
995 | 0 | } |
996 | | |
997 | 0 | DDFField *field = record->GetField(1); |
998 | 0 | if (field == nullptr) |
999 | 0 | return nullptr; |
1000 | 0 | DDFFieldDefn *fieldDefn = field->GetFieldDefn(); |
1001 | |
|
1002 | 0 | if (!(strcmp(fieldDefn->GetName(), "DSI") == 0 && |
1003 | 0 | fieldDefn->GetSubfieldCount() == 2)) |
1004 | 0 | { |
1005 | 0 | return nullptr; |
1006 | 0 | } |
1007 | | |
1008 | 0 | const char *pszPRT = record->GetStringSubfield("DSI", 0, "PRT", 0); |
1009 | 0 | if (pszPRT == nullptr) |
1010 | 0 | return nullptr; |
1011 | | |
1012 | 0 | CPLString osPRT = pszPRT; |
1013 | 0 | osPRT.resize(4); |
1014 | 0 | CPLDebug("SRP", "osPRT=%s", osPRT.c_str()); |
1015 | 0 | if (!EQUAL(osPRT, "ASRP") && !EQUAL(osPRT, "USRP")) |
1016 | 0 | return nullptr; |
1017 | | |
1018 | 0 | const char *pszNAM = record->GetStringSubfield("DSI", 0, "NAM", 0); |
1019 | 0 | if (pszNAM == nullptr) |
1020 | 0 | return nullptr; |
1021 | | |
1022 | 0 | const CPLString osNAM = pszNAM; |
1023 | 0 | CPLDebug("SRP", "osNAM=%s", osNAM.c_str()); |
1024 | 0 | if (strlen(pszNAM) != 8) |
1025 | 0 | { |
1026 | 0 | CPLDebug("SRP", "Name Size=%d", (int)strlen(pszNAM)); |
1027 | 0 | } |
1028 | |
|
1029 | 0 | SRPDataset *poDS = new SRPDataset(); |
1030 | |
|
1031 | 0 | poDS->osProduct = osPRT; |
1032 | 0 | poDS->osGENFileName = pszGENFileName; |
1033 | 0 | poDS->osIMGFileName = pszIMGFileName; |
1034 | |
|
1035 | 0 | poDS->SetMetadataItem("SRP_NAM", osNAM); |
1036 | 0 | poDS->SetMetadataItem("SRP_PRODUCT", osPRT); |
1037 | |
|
1038 | 0 | if (!poDS->GetFromRecord(pszGENFileName, record)) |
1039 | 0 | { |
1040 | 0 | delete poDS; |
1041 | 0 | return nullptr; |
1042 | 0 | } |
1043 | | |
1044 | 0 | return poDS; |
1045 | 0 | } |
1046 | | |
1047 | | /************************************************************************/ |
1048 | | /* GetGENListFromTHF() */ |
1049 | | /************************************************************************/ |
1050 | | |
1051 | | char **SRPDataset::GetGENListFromTHF(const char *pszFileName) |
1052 | 142 | { |
1053 | 142 | DDFModule module; |
1054 | 142 | DDFRecord *record = nullptr; |
1055 | 142 | DDFField *field = nullptr; |
1056 | 142 | DDFFieldDefn *fieldDefn = nullptr; |
1057 | 142 | int nFilenames = 0; |
1058 | | |
1059 | 142 | char **papszFileNames = nullptr; |
1060 | 142 | if (!module.Open(pszFileName, TRUE)) |
1061 | 104 | return papszFileNames; |
1062 | | |
1063 | 38 | CPLString osDirName(CPLGetDirnameSafe(pszFileName)); |
1064 | | |
1065 | 38 | while (true) |
1066 | 38 | { |
1067 | 38 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1068 | 38 | record = module.ReadRecord(); |
1069 | 38 | CPLPopErrorHandler(); |
1070 | 38 | CPLErrorReset(); |
1071 | 38 | if (record == nullptr) |
1072 | 38 | break; |
1073 | 0 | if (record->GetFieldCount() > 2) |
1074 | 0 | { |
1075 | 0 | field = record->GetField(0); |
1076 | 0 | fieldDefn = field->GetFieldDefn(); |
1077 | 0 | if (!(strcmp(fieldDefn->GetName(), "001") == 0 && |
1078 | 0 | fieldDefn->GetSubfieldCount() == 2)) |
1079 | 0 | { |
1080 | 0 | continue; |
1081 | 0 | } |
1082 | | |
1083 | 0 | const char *RTY = record->GetStringSubfield("001", 0, "RTY", 0); |
1084 | 0 | if (RTY == nullptr) |
1085 | 0 | { |
1086 | 0 | continue; |
1087 | 0 | } |
1088 | | |
1089 | 0 | if (strcmp(RTY, "THF") == 0) |
1090 | 0 | { |
1091 | 0 | field = record->GetField(1); |
1092 | 0 | fieldDefn = field->GetFieldDefn(); |
1093 | 0 | if (!(strcmp(fieldDefn->GetName(), "VDR") == 0 && |
1094 | 0 | fieldDefn->GetSubfieldCount() == 8)) |
1095 | 0 | { |
1096 | 0 | continue; |
1097 | 0 | } |
1098 | | |
1099 | 0 | int iFDRFieldInstance = 0; |
1100 | 0 | for (int i = 2; i < record->GetFieldCount(); i++) |
1101 | 0 | { |
1102 | 0 | field = record->GetField(i); |
1103 | 0 | fieldDefn = field->GetFieldDefn(); |
1104 | |
|
1105 | 0 | if (!(strcmp(fieldDefn->GetName(), "FDR") == 0 && |
1106 | 0 | fieldDefn->GetSubfieldCount() == 7)) |
1107 | 0 | { |
1108 | 0 | CPLDebug("SRP", "Record FDR %d", |
1109 | 0 | fieldDefn->GetSubfieldCount()); |
1110 | 0 | continue; |
1111 | 0 | } |
1112 | | |
1113 | 0 | const char *pszNAM = record->GetStringSubfield( |
1114 | 0 | "FDR", iFDRFieldInstance++, "NAM", 0); |
1115 | 0 | if (pszNAM == nullptr) |
1116 | 0 | continue; |
1117 | | |
1118 | 0 | CPLString osName = CPLString(pszNAM); |
1119 | | |
1120 | | /* Define a subdirectory from Dataset but with only 6 |
1121 | | * characters */ |
1122 | 0 | CPLString osDirDataset = pszNAM; |
1123 | 0 | osDirDataset.resize(6); |
1124 | 0 | CPLString osDatasetDir = CPLFormFilenameSafe( |
1125 | 0 | osDirName.c_str(), osDirDataset.c_str(), nullptr); |
1126 | |
|
1127 | 0 | CPLString osGENFileName = ""; |
1128 | |
|
1129 | 0 | int bFound = 0; |
1130 | |
|
1131 | 0 | { |
1132 | 0 | char **papszDirContent = |
1133 | 0 | VSIReadDir(osDatasetDir.c_str()); |
1134 | 0 | char **ptrDir = papszDirContent; |
1135 | 0 | if (ptrDir) |
1136 | 0 | { |
1137 | 0 | while (*ptrDir) |
1138 | 0 | { |
1139 | 0 | if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(), |
1140 | 0 | "GEN")) |
1141 | 0 | { |
1142 | 0 | bFound = 1; |
1143 | 0 | osGENFileName = CPLFormFilenameSafe( |
1144 | 0 | osDatasetDir.c_str(), *ptrDir, nullptr); |
1145 | 0 | CPLDebug("SRP", |
1146 | 0 | "Building GEN full file name : %s", |
1147 | 0 | osGENFileName.c_str()); |
1148 | 0 | break; |
1149 | 0 | } |
1150 | 0 | ptrDir++; |
1151 | 0 | } |
1152 | 0 | CSLDestroy(papszDirContent); |
1153 | 0 | } |
1154 | 0 | } |
1155 | | |
1156 | | /* If not found in sub directory then search in the same |
1157 | | * directory of the THF file */ |
1158 | 0 | if (bFound == 0) |
1159 | 0 | { |
1160 | 0 | char **papszDirContent = VSIReadDir(osDirName.c_str()); |
1161 | 0 | char **ptrDir = papszDirContent; |
1162 | 0 | if (ptrDir) |
1163 | 0 | { |
1164 | 0 | while (*ptrDir) |
1165 | 0 | { |
1166 | 0 | if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(), |
1167 | 0 | "GEN") && |
1168 | 0 | EQUALN(CPLGetBasenameSafe(*ptrDir).c_str(), |
1169 | 0 | osName, 6)) |
1170 | 0 | { |
1171 | 0 | bFound = 1; |
1172 | 0 | osGENFileName = CPLFormFilenameSafe( |
1173 | 0 | osDirName.c_str(), *ptrDir, nullptr); |
1174 | 0 | CPLDebug("SRP", |
1175 | 0 | "Building GEN full file name : %s", |
1176 | 0 | osGENFileName.c_str()); |
1177 | 0 | break; |
1178 | 0 | } |
1179 | 0 | ptrDir++; |
1180 | 0 | } |
1181 | 0 | CSLDestroy(papszDirContent); |
1182 | 0 | } |
1183 | 0 | } |
1184 | |
|
1185 | 0 | if (bFound == 1) |
1186 | 0 | { |
1187 | 0 | papszFileNames = (char **)CPLRealloc( |
1188 | 0 | papszFileNames, sizeof(char *) * (nFilenames + 2)); |
1189 | 0 | papszFileNames[nFilenames] = |
1190 | 0 | CPLStrdup(osGENFileName.c_str()); |
1191 | 0 | papszFileNames[nFilenames + 1] = nullptr; |
1192 | 0 | nFilenames++; |
1193 | 0 | } |
1194 | 0 | } |
1195 | 0 | } |
1196 | 0 | } |
1197 | 0 | } |
1198 | 38 | return papszFileNames; |
1199 | 142 | } |
1200 | | |
1201 | | /************************************************************************/ |
1202 | | /* AddMetadatafromFromTHF() */ |
1203 | | /************************************************************************/ |
1204 | | |
1205 | | void SRPDataset::AddMetadatafromFromTHF(const char *pszFileName) |
1206 | 0 | { |
1207 | 0 | DDFModule module; |
1208 | 0 | DDFRecord *record = nullptr; |
1209 | 0 | DDFField *field = nullptr; |
1210 | 0 | DDFFieldDefn *fieldDefn = nullptr; |
1211 | |
|
1212 | 0 | int bSuccess = 0; |
1213 | 0 | if (!module.Open(pszFileName, TRUE)) |
1214 | 0 | return; |
1215 | | |
1216 | 0 | while (true) |
1217 | 0 | { |
1218 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1219 | 0 | record = module.ReadRecord(); |
1220 | 0 | CPLPopErrorHandler(); |
1221 | 0 | CPLErrorReset(); |
1222 | 0 | if (record == nullptr || record->GetFieldCount() <= 2) |
1223 | 0 | break; |
1224 | | |
1225 | 0 | field = record->GetField(0); |
1226 | 0 | fieldDefn = field->GetFieldDefn(); |
1227 | 0 | if (!(strcmp(fieldDefn->GetName(), "001") == 0) || |
1228 | 0 | fieldDefn->GetSubfieldCount() != 2) |
1229 | 0 | break; |
1230 | | |
1231 | 0 | const char *RTY = record->GetStringSubfield("001", 0, "RTY", 0); |
1232 | 0 | if (RTY != nullptr && strcmp(RTY, "THF") == 0) |
1233 | 0 | { |
1234 | 0 | field = record->GetField(1); |
1235 | 0 | fieldDefn = field->GetFieldDefn(); |
1236 | 0 | if ((strcmp(fieldDefn->GetName(), "VDR") == 0 && |
1237 | 0 | fieldDefn->GetSubfieldCount() == 8)) |
1238 | 0 | { |
1239 | |
|
1240 | 0 | const char *pszVOO = |
1241 | 0 | record->GetStringSubfield("VDR", 0, "VOO", 0); |
1242 | 0 | if (pszVOO != nullptr) |
1243 | 0 | { |
1244 | 0 | CPLDebug("SRP", "Record VOO %s", pszVOO); |
1245 | 0 | SetMetadataItem("SRP_VOO", pszVOO); |
1246 | 0 | } |
1247 | |
|
1248 | 0 | int EDN = record->GetIntSubfield("VDR", 0, "EDN", 0, &bSuccess); |
1249 | 0 | if (bSuccess) |
1250 | 0 | { |
1251 | 0 | CPLDebug("SRP", "Record EDN %d", EDN); |
1252 | 0 | char szValue[5]; |
1253 | 0 | snprintf(szValue, sizeof(szValue), "%d", EDN); |
1254 | 0 | SetMetadataItem("SRP_EDN", szValue); |
1255 | 0 | } |
1256 | |
|
1257 | 0 | const char *pszCDV07 = |
1258 | 0 | record->GetStringSubfield("VDR", 0, "CDV07", 0); |
1259 | 0 | if (pszCDV07 != nullptr) |
1260 | 0 | { |
1261 | 0 | CPLDebug("SRP", "Record pszCDV07 %s", pszCDV07); |
1262 | 0 | SetMetadataItem("SRP_CREATIONDATE", pszCDV07); |
1263 | 0 | } |
1264 | 0 | else |
1265 | 0 | { /*USRP1.2*/ |
1266 | 0 | const char *pszDAT = |
1267 | 0 | record->GetStringSubfield("VDR", 0, "DAT", 0); |
1268 | 0 | if (pszDAT != nullptr) |
1269 | 0 | { |
1270 | 0 | char dat[9]; |
1271 | 0 | strncpy(dat, pszDAT + 4, 8); |
1272 | 0 | dat[8] = '\0'; |
1273 | 0 | CPLDebug("SRP", "Record DAT %s", dat); |
1274 | 0 | SetMetadataItem("SRP_CREATIONDATE", dat); |
1275 | 0 | } |
1276 | 0 | } |
1277 | 0 | } |
1278 | 0 | } /* End of THF part */ |
1279 | |
|
1280 | 0 | if (RTY != nullptr && strcmp(RTY, "LCF") == 0) |
1281 | 0 | { |
1282 | 0 | field = record->GetField(1); |
1283 | 0 | fieldDefn = field->GetFieldDefn(); |
1284 | 0 | if ((strcmp(fieldDefn->GetName(), "QSR") == 0 && |
1285 | 0 | fieldDefn->GetSubfieldCount() == 4)) |
1286 | 0 | { |
1287 | |
|
1288 | 0 | const char *pszQSS = |
1289 | 0 | record->GetStringSubfield("QSR", 0, "QSS", 0); |
1290 | 0 | if (pszQSS != nullptr) |
1291 | 0 | { |
1292 | 0 | CPLDebug("SRP", "Record Classification %s", pszQSS); |
1293 | 0 | SetMetadataItem("SRP_CLASSIFICATION", pszQSS); |
1294 | 0 | } |
1295 | 0 | } |
1296 | |
|
1297 | 0 | field = record->GetField(2); |
1298 | 0 | fieldDefn = field->GetFieldDefn(); |
1299 | 0 | if ((strcmp(fieldDefn->GetName(), "QUV") == 0 && |
1300 | 0 | fieldDefn->GetSubfieldCount() == 6)) |
1301 | 0 | { |
1302 | 0 | const char *pszSRC2 = |
1303 | 0 | record->GetStringSubfield("QUV", 0, "SRC1", 0); |
1304 | 0 | if (pszSRC2 != nullptr) |
1305 | 0 | { |
1306 | 0 | SetMetadataItem("SRP_PRODUCTVERSION", pszSRC2); |
1307 | 0 | } |
1308 | 0 | else |
1309 | 0 | { |
1310 | 0 | const char *pszSRC = |
1311 | 0 | record->GetStringSubfield("QUV", 0, "SRC", 0); |
1312 | 0 | if (pszSRC != nullptr) |
1313 | 0 | { |
1314 | 0 | SetMetadataItem("SRP_PRODUCTVERSION", pszSRC); |
1315 | 0 | } |
1316 | 0 | } |
1317 | 0 | } |
1318 | 0 | } /* End of LCF part */ |
1319 | 0 | } |
1320 | 0 | } |
1321 | | |
1322 | | /************************************************************************/ |
1323 | | /* GetIMGListFromGEN() */ |
1324 | | /************************************************************************/ |
1325 | | |
1326 | | char **SRPDataset::GetIMGListFromGEN(const char *pszFileName, |
1327 | | int *pnRecordIndex) |
1328 | 0 | { |
1329 | 0 | DDFRecord *record = nullptr; |
1330 | 0 | DDFField *field = nullptr; |
1331 | 0 | DDFFieldDefn *fieldDefn = nullptr; |
1332 | 0 | int nFilenames = 0; |
1333 | 0 | char **papszFileNames = nullptr; |
1334 | 0 | int nRecordIndex = -1; |
1335 | |
|
1336 | 0 | if (pnRecordIndex) |
1337 | 0 | *pnRecordIndex = -1; |
1338 | |
|
1339 | 0 | DDFModule module; |
1340 | 0 | if (!module.Open(pszFileName, TRUE)) |
1341 | 0 | return nullptr; |
1342 | | |
1343 | 0 | while (true) |
1344 | 0 | { |
1345 | 0 | nRecordIndex++; |
1346 | |
|
1347 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1348 | 0 | record = module.ReadRecord(); |
1349 | 0 | CPLPopErrorHandler(); |
1350 | 0 | CPLErrorReset(); |
1351 | 0 | if (record == nullptr) |
1352 | 0 | break; |
1353 | | |
1354 | 0 | if (record->GetFieldCount() >= 5) |
1355 | 0 | { |
1356 | 0 | field = record->GetField(0); |
1357 | 0 | fieldDefn = field->GetFieldDefn(); |
1358 | 0 | if (!(strcmp(fieldDefn->GetName(), "001") == 0 && |
1359 | 0 | fieldDefn->GetSubfieldCount() == 2)) |
1360 | 0 | { |
1361 | 0 | continue; |
1362 | 0 | } |
1363 | | |
1364 | 0 | const char *RTY = record->GetStringSubfield("001", 0, "RTY", 0); |
1365 | 0 | if (RTY == nullptr) |
1366 | 0 | continue; |
1367 | | /* Ignore overviews */ |
1368 | 0 | if (strcmp(RTY, "OVV") == 0) |
1369 | 0 | continue; |
1370 | | |
1371 | 0 | if (strcmp(RTY, "GIN") != 0) |
1372 | 0 | continue; |
1373 | | |
1374 | | /* make sure that the GEN file is part of a SRP dataset, not an ADRG |
1375 | | * dataset, by checking that the GEN field does not contain a NOW |
1376 | | * subfield */ |
1377 | 0 | const char *NWO = record->GetStringSubfield("GEN", 0, "NWO", 0); |
1378 | 0 | if (NWO) |
1379 | 0 | { |
1380 | 0 | CSLDestroy(papszFileNames); |
1381 | 0 | return nullptr; |
1382 | 0 | } |
1383 | | |
1384 | 0 | field = record->GetField(3); |
1385 | 0 | if (field == nullptr) |
1386 | 0 | continue; |
1387 | 0 | fieldDefn = field->GetFieldDefn(); |
1388 | |
|
1389 | 0 | if (!(strcmp(fieldDefn->GetName(), "SPR") == 0 && |
1390 | 0 | fieldDefn->GetSubfieldCount() == 15)) |
1391 | 0 | { |
1392 | 0 | continue; |
1393 | 0 | } |
1394 | | |
1395 | 0 | const char *pszBAD = record->GetStringSubfield("SPR", 0, "BAD", 0); |
1396 | 0 | if (pszBAD == nullptr || strlen(pszBAD) != 12) |
1397 | 0 | continue; |
1398 | 0 | std::string osBAD = pszBAD; |
1399 | 0 | { |
1400 | 0 | char *c = (char *)strchr(osBAD.c_str(), ' '); |
1401 | 0 | if (c) |
1402 | 0 | *c = 0; |
1403 | 0 | } |
1404 | 0 | CPLDebug("SRP", "BAD=%s", osBAD.c_str()); |
1405 | | |
1406 | | /* Build full IMG file name from BAD value */ |
1407 | 0 | const CPLString osGENDir(CPLGetDirnameSafe(pszFileName)); |
1408 | |
|
1409 | 0 | std::string osFileName = |
1410 | 0 | CPLFormFilenameSafe(osGENDir.c_str(), osBAD.c_str(), nullptr); |
1411 | 0 | VSIStatBufL sStatBuf; |
1412 | 0 | if (VSIStatL(osFileName.c_str(), &sStatBuf) == 0) |
1413 | 0 | { |
1414 | 0 | osBAD = std::move(osFileName); |
1415 | 0 | CPLDebug("SRP", "Building IMG full file name : %s", |
1416 | 0 | osBAD.c_str()); |
1417 | 0 | } |
1418 | 0 | else |
1419 | 0 | { |
1420 | 0 | char **papszDirContent = nullptr; |
1421 | 0 | if (strcmp(osGENDir.c_str(), "/vsimem") == 0) |
1422 | 0 | { |
1423 | 0 | CPLString osTmp = osGENDir + "/"; |
1424 | 0 | papszDirContent = VSIReadDir(osTmp); |
1425 | 0 | } |
1426 | 0 | else |
1427 | 0 | papszDirContent = VSIReadDir(osGENDir); |
1428 | 0 | char **ptrDir = papszDirContent; |
1429 | 0 | while (ptrDir && *ptrDir) |
1430 | 0 | { |
1431 | 0 | if (EQUAL(*ptrDir, osBAD.c_str())) |
1432 | 0 | { |
1433 | 0 | osBAD = CPLFormFilenameSafe(osGENDir.c_str(), *ptrDir, |
1434 | 0 | nullptr); |
1435 | 0 | CPLDebug("SRP", "Building IMG full file name : %s", |
1436 | 0 | osBAD.c_str()); |
1437 | 0 | break; |
1438 | 0 | } |
1439 | 0 | ptrDir++; |
1440 | 0 | } |
1441 | 0 | CSLDestroy(papszDirContent); |
1442 | 0 | } |
1443 | |
|
1444 | 0 | if (nFilenames == 0 && pnRecordIndex) |
1445 | 0 | *pnRecordIndex = nRecordIndex; |
1446 | |
|
1447 | 0 | papszFileNames = (char **)CPLRealloc( |
1448 | 0 | papszFileNames, sizeof(char *) * (nFilenames + 2)); |
1449 | 0 | papszFileNames[nFilenames] = CPLStrdup(osBAD.c_str()); |
1450 | 0 | papszFileNames[nFilenames + 1] = nullptr; |
1451 | 0 | nFilenames++; |
1452 | 0 | } |
1453 | 0 | } |
1454 | | |
1455 | 0 | return papszFileNames; |
1456 | 0 | } |
1457 | | |
1458 | | /************************************************************************/ |
1459 | | /* Open() */ |
1460 | | /************************************************************************/ |
1461 | | |
1462 | | GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo) |
1463 | 15.6k | { |
1464 | 15.6k | int nRecordIndex = -1; |
1465 | 15.6k | CPLString osGENFileName; |
1466 | 15.6k | CPLString osIMGFileName; |
1467 | 15.6k | int bFromSubdataset = FALSE; |
1468 | 15.6k | int bTHFWithSingleGEN = FALSE; |
1469 | | |
1470 | 15.6k | if (STARTS_WITH_CI(poOpenInfo->pszFilename, "SRP:")) |
1471 | 0 | { |
1472 | 0 | char **papszTokens = |
1473 | 0 | CSLTokenizeString2(poOpenInfo->pszFilename + 4, ",", 0); |
1474 | 0 | if (CSLCount(papszTokens) == 2) |
1475 | 0 | { |
1476 | 0 | osGENFileName = papszTokens[0]; |
1477 | 0 | osIMGFileName = papszTokens[1]; |
1478 | 0 | bFromSubdataset = TRUE; |
1479 | 0 | } |
1480 | 0 | CSLDestroy(papszTokens); |
1481 | 0 | } |
1482 | 15.6k | else |
1483 | 15.6k | { |
1484 | 15.6k | if (poOpenInfo->nHeaderBytes < 500) |
1485 | 2.70k | return nullptr; |
1486 | 12.9k | CPLString osFileName(poOpenInfo->pszFilename); |
1487 | | |
1488 | 12.9k | if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "THF")) |
1489 | 142 | { |
1490 | | |
1491 | 142 | CPLDebug("SRP", "Read THF"); |
1492 | | |
1493 | 142 | char **papszFileNames = GetGENListFromTHF(osFileName.c_str()); |
1494 | 142 | if (papszFileNames == nullptr) |
1495 | 142 | return nullptr; |
1496 | 0 | if (papszFileNames[1] == nullptr && |
1497 | 0 | CPLTestBool(CPLGetConfigOption( |
1498 | 0 | "SRP_SINGLE_GEN_IN_THF_AS_DATASET", "TRUE"))) |
1499 | 0 | { |
1500 | 0 | osFileName = papszFileNames[0]; |
1501 | 0 | CSLDestroy(papszFileNames); |
1502 | 0 | bTHFWithSingleGEN = TRUE; |
1503 | 0 | } |
1504 | 0 | else |
1505 | 0 | { |
1506 | 0 | char **ptr = papszFileNames; |
1507 | 0 | SRPDataset *poDS = new SRPDataset(); |
1508 | 0 | poDS->AddMetadatafromFromTHF(osFileName.c_str()); |
1509 | 0 | while (*ptr) |
1510 | 0 | { |
1511 | 0 | char **papszIMGFileNames = GetIMGListFromGEN(*ptr); |
1512 | 0 | char **papszIMGIter = papszIMGFileNames; |
1513 | 0 | while (papszIMGIter && *papszIMGIter) |
1514 | 0 | { |
1515 | 0 | poDS->AddSubDataset(*ptr, *papszIMGIter); |
1516 | 0 | papszIMGIter++; |
1517 | 0 | } |
1518 | 0 | CSLDestroy(papszIMGFileNames); |
1519 | |
|
1520 | 0 | ptr++; |
1521 | 0 | } |
1522 | 0 | CSLDestroy(papszFileNames); |
1523 | 0 | return poDS; |
1524 | 0 | } |
1525 | 0 | } |
1526 | | |
1527 | 12.7k | if (bTHFWithSingleGEN |
1528 | | #ifdef OPEN_GEN |
1529 | | || EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "GEN") |
1530 | | #endif |
1531 | 12.7k | ) |
1532 | 0 | { |
1533 | 0 | osGENFileName = osFileName; |
1534 | |
|
1535 | 0 | char **papszFileNames = |
1536 | 0 | GetIMGListFromGEN(osFileName.c_str(), &nRecordIndex); |
1537 | 0 | if (papszFileNames == nullptr) |
1538 | 0 | return nullptr; |
1539 | 0 | if (papszFileNames[1] == nullptr) |
1540 | 0 | { |
1541 | 0 | osIMGFileName = papszFileNames[0]; |
1542 | 0 | CSLDestroy(papszFileNames); |
1543 | 0 | } |
1544 | 0 | else |
1545 | 0 | { |
1546 | 0 | char **ptr = papszFileNames; |
1547 | 0 | SRPDataset *poDS = new SRPDataset(); |
1548 | 0 | while (*ptr) |
1549 | 0 | { |
1550 | 0 | poDS->AddSubDataset(osFileName.c_str(), *ptr); |
1551 | 0 | ptr++; |
1552 | 0 | } |
1553 | 0 | CSLDestroy(papszFileNames); |
1554 | 0 | return poDS; |
1555 | 0 | } |
1556 | 0 | } |
1557 | | |
1558 | 12.7k | if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "IMG")) |
1559 | 4 | { |
1560 | | |
1561 | 4 | osIMGFileName = osFileName; |
1562 | | |
1563 | 4 | constexpr int nLeaderSize = 24; |
1564 | | |
1565 | 4 | int i = 0; // Used after for. |
1566 | 21 | for (; i < nLeaderSize; i++) |
1567 | 21 | { |
1568 | 21 | if (poOpenInfo->pabyHeader[i] < 32 || |
1569 | 21 | poOpenInfo->pabyHeader[i] > 126) |
1570 | 4 | return nullptr; |
1571 | 21 | } |
1572 | | |
1573 | 0 | if (poOpenInfo->pabyHeader[5] != '1' && |
1574 | 0 | poOpenInfo->pabyHeader[5] != '2' && |
1575 | 0 | poOpenInfo->pabyHeader[5] != '3') |
1576 | 0 | return nullptr; |
1577 | | |
1578 | 0 | if (poOpenInfo->pabyHeader[6] != 'L') |
1579 | 0 | return nullptr; |
1580 | 0 | if (poOpenInfo->pabyHeader[8] != '1' && |
1581 | 0 | poOpenInfo->pabyHeader[8] != ' ') |
1582 | 0 | return nullptr; |
1583 | | |
1584 | | // -------------------------------------------------------------------- |
1585 | | // Find and open the .GEN file. |
1586 | | // -------------------------------------------------------------------- |
1587 | 0 | VSIStatBufL sStatBuf; |
1588 | |
|
1589 | 0 | CPLString basename = CPLGetBasenameSafe(osFileName); |
1590 | 0 | if (basename.size() != 8) |
1591 | 0 | { |
1592 | 0 | CPLDebug("SRP", "Invalid basename file"); |
1593 | 0 | return nullptr; |
1594 | 0 | } |
1595 | | |
1596 | 0 | nRecordIndex = static_cast<int>(CPLScanLong(basename + 6, 2)); |
1597 | |
|
1598 | 0 | CPLString path = CPLGetDirnameSafe(osFileName); |
1599 | 0 | CPLString basename01 = ResetTo01(basename); |
1600 | 0 | osFileName = CPLFormFilenameSafe(path, basename01, ".IMG"); |
1601 | |
|
1602 | 0 | osFileName = CPLResetExtensionSafe(osFileName, "GEN"); |
1603 | 0 | if (VSIStatL(osFileName, &sStatBuf) != 0) |
1604 | 0 | { |
1605 | 0 | osFileName = CPLResetExtensionSafe(osFileName, "gen"); |
1606 | 0 | if (VSIStatL(osFileName, &sStatBuf) != 0) |
1607 | 0 | return nullptr; |
1608 | 0 | } |
1609 | | |
1610 | 0 | osGENFileName = std::move(osFileName); |
1611 | 0 | } |
1612 | 12.7k | } |
1613 | | |
1614 | 12.7k | if (!osGENFileName.empty() && !osIMGFileName.empty()) |
1615 | 0 | { |
1616 | |
|
1617 | 0 | if (poOpenInfo->eAccess == GA_Update) |
1618 | 0 | { |
1619 | 0 | ReportUpdateNotSupportedByDriver("SRP"); |
1620 | 0 | return nullptr; |
1621 | 0 | } |
1622 | | |
1623 | 0 | DDFModule module; |
1624 | 0 | DDFRecord *record = nullptr; |
1625 | 0 | if (nRecordIndex >= 0 && module.Open(osGENFileName.c_str(), TRUE)) |
1626 | 0 | { |
1627 | 0 | for (int i = 0; i < nRecordIndex; i++) |
1628 | 0 | { |
1629 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1630 | 0 | record = module.ReadRecord(); |
1631 | 0 | CPLPopErrorHandler(); |
1632 | 0 | CPLErrorReset(); |
1633 | 0 | if (record == nullptr) |
1634 | 0 | break; |
1635 | 0 | } |
1636 | 0 | } |
1637 | 0 | SRPDataset *poDS = |
1638 | 0 | OpenDataset(osGENFileName.c_str(), osIMGFileName.c_str(), record); |
1639 | |
|
1640 | 0 | if (poDS) |
1641 | 0 | { |
1642 | | /* ---------------------------------------------------------- */ |
1643 | | /* Initialize any PAM information. */ |
1644 | | /* ---------------------------------------------------------- */ |
1645 | 0 | poDS->SetDescription(poOpenInfo->pszFilename); |
1646 | 0 | poDS->TryLoadXML(); |
1647 | | |
1648 | | /* ---------------------------------------------------------- */ |
1649 | | /* Check for external overviews. */ |
1650 | | /* ---------------------------------------------------------- */ |
1651 | 0 | if (bFromSubdataset) |
1652 | 0 | poDS->oOvManager.Initialize(poDS, osIMGFileName.c_str()); |
1653 | 0 | else |
1654 | 0 | poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename); |
1655 | |
|
1656 | 0 | return poDS; |
1657 | 0 | } |
1658 | 0 | } |
1659 | | |
1660 | 12.7k | return nullptr; |
1661 | 12.7k | } |
1662 | | |
1663 | | /************************************************************************/ |
1664 | | /* GDALRegister_SRP() */ |
1665 | | /************************************************************************/ |
1666 | | |
1667 | | void GDALRegister_SRP() |
1668 | | |
1669 | 2 | { |
1670 | 2 | if (GDALGetDriverByName("SRP") != nullptr) |
1671 | 0 | return; |
1672 | | |
1673 | 2 | GDALDriver *poDriver = new GDALDriver(); |
1674 | | |
1675 | 2 | poDriver->SetDescription("SRP"); |
1676 | 2 | poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); |
1677 | 2 | poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, |
1678 | 2 | "Standard Raster Product (ASRP/USRP)"); |
1679 | 2 | poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/srp.html"); |
1680 | 2 | poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "img"); |
1681 | 2 | poDriver->SetMetadataItem(GDAL_DMD_SUBDATASETS, "YES"); |
1682 | 2 | poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); |
1683 | | |
1684 | 2 | poDriver->pfnOpen = SRPDataset::Open; |
1685 | | |
1686 | 2 | GetGDALDriverManager()->RegisterDriver(poDriver); |
1687 | 2 | } |