Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: DRDC Ottawa GEOINT
4 : * Purpose: Radarsat Constellation Mission - XML Products (product.xml) driver
5 : * Author: Roberto Caron, MDA
6 : * on behalf of DRDC Ottawa
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2020, DRDC Ottawa
10 : *
11 : * Based on the RS2 Dataset Class
12 : *
13 : * SPDX-License-Identifier: MIT
14 : ****************************************************************************/
15 :
16 : #include <time.h>
17 : #include <stdio.h>
18 : #include <sstream>
19 :
20 : #include "cpl_minixml.h"
21 : #include "gdal_frmts.h"
22 : #include "gdal_pam.h"
23 : #include "ogr_spatialref.h"
24 : #include "rcmdataset.h"
25 : #include "rcmdrivercore.h"
26 :
27 : #include <limits>
28 :
29 : constexpr int max_space_for_string = 32;
30 :
31 : /* RCM has a special folder that contains all LUT, Incidence Angle and Noise
32 : * Level files */
33 : constexpr const char *CALIBRATION_FOLDER = "calibration";
34 :
35 : /*** Function to format calibration for unique identification for Layer Name
36 : * ***/
37 : /*
38 : * RCM_CALIB : { SIGMA0 | GAMMA0 | BETA0 | UNCALIB } : product.xml full path
39 : */
40 56 : inline CPLString FormatCalibration(const char *pszCalibName,
41 : const char *pszFilename)
42 : {
43 56 : CPLString ptr;
44 :
45 : // Always begin by the layer calibration name
46 56 : ptr.append(szLayerCalibration);
47 :
48 : // A separator is needed before concat calibration name
49 56 : ptr += chLayerSeparator;
50 : // Add calibration name
51 56 : ptr.append(pszCalibName);
52 :
53 : // A separator is needed before concat full filename name
54 56 : ptr += chLayerSeparator;
55 : // Add full filename name
56 56 : ptr.append(pszFilename);
57 :
58 : /* return calibration format */
59 56 : return ptr;
60 : }
61 :
62 : /*** Function to test for valid LUT files ***/
63 72 : static bool IsValidXMLFile(const char *pszPath)
64 : {
65 : /* Return true for valid xml file, false otherwise */
66 72 : CPLXMLTreeCloser psLut(CPLParseXMLFile(pszPath));
67 :
68 72 : if (psLut.get() == nullptr)
69 : {
70 0 : CPLError(CE_Failure, CPLE_OpenFailed,
71 : "ERROR: Failed to open the LUT file %s", pszPath);
72 : }
73 :
74 144 : return psLut.get() != nullptr;
75 : }
76 :
77 14 : static double *InterpolateValues(CSLConstList papszList, int tableSize,
78 : int stepSize, int numberOfValues,
79 : int pixelFirstLutValue)
80 : {
81 : /* Allocate the right LUT size according to the product range pixel */
82 : double *table =
83 14 : static_cast<double *>(VSI_CALLOC_VERBOSE(sizeof(double), tableSize));
84 14 : if (!table)
85 0 : return nullptr;
86 :
87 14 : if (stepSize <= 0)
88 : {
89 : /* When negative, the range of pixel is calculated from the opposite
90 : * starting from the end of gains array */
91 : /* Just step the range with positive value */
92 14 : const int positiveStepSize = abs(stepSize);
93 :
94 14 : int k = 0;
95 :
96 14 : if (positiveStepSize == 1)
97 : {
98 : // Be fast and just copy the values because all gain values
99 : // represent all image wide pixel
100 : /* Start at the end position and store in the opposite */
101 0 : for (int i = pixelFirstLutValue; i >= 0; i--)
102 : {
103 0 : const double value = CPLAtof(papszList[i]);
104 0 : table[k++] = value;
105 : }
106 : }
107 : else
108 : {
109 :
110 : /* Interpolation between 2 numbers */
111 422 : for (int i = numberOfValues - 1; i >= 0; i--)
112 : {
113 : // We will consider the same value to cover the case that we
114 : // will hit the last pixel
115 408 : double valueFrom = CPLAtof(papszList[i]);
116 408 : double valueTo = valueFrom;
117 :
118 408 : if (i > 0)
119 : {
120 : // We have room to pick the previous number to interpolate
121 : // with
122 394 : valueTo = CPLAtof(papszList[i - 1]);
123 : }
124 :
125 : // If the valueFrom minus ValueTo equal 0, it means to finish
126 : // off with the same number until the end of the table size
127 408 : double interp = (valueTo - valueFrom) / positiveStepSize;
128 :
129 : // Always begin with the value FROM found
130 408 : table[k++] = valueFrom;
131 :
132 : // Then add interpolation, don't forget. The stepSize is
133 : // actually counting our valueFrom number thus we add
134 : // interpolation until the last step - 1
135 116341 : for (int j = 0; j < positiveStepSize - 1; j++)
136 : {
137 115933 : valueFrom += interp;
138 115933 : table[k++] = valueFrom;
139 : }
140 : }
141 : }
142 : }
143 : else
144 : {
145 : /* When positive, the range of pixel is calculated from the beginning of
146 : * gains array */
147 0 : if (stepSize == 1)
148 : {
149 : // Be fast and just copy the values because all gain values
150 : // represent all image wide pixel
151 0 : for (int i = 0; i < numberOfValues; i++)
152 : {
153 0 : const double value = CPLAtof(papszList[i]);
154 0 : table[i] = value;
155 : }
156 : }
157 : else
158 : {
159 : /* Interpolation between 2 numbers */
160 0 : int k = 0;
161 0 : for (int i = 0; i < numberOfValues; i++)
162 : {
163 : // We will consider the same value to cover the case that we
164 : // will hit the last pixel
165 0 : double valueFrom = CPLAtof(papszList[i]);
166 0 : double valueTo = valueFrom;
167 :
168 0 : if (i < (numberOfValues)-1)
169 : {
170 : // We have room to pick the next number to interpolate with
171 0 : valueTo = CPLAtof(papszList[i + 1]);
172 : }
173 :
174 : // If the valueFrom minus ValueTo equal 0, it means to finish
175 : // off with the same number until the end of the table size
176 0 : double interp = (valueTo - valueFrom) / stepSize;
177 :
178 : // Always begin with the value FROM found
179 0 : table[k++] = valueFrom;
180 :
181 : // Then add interpolation, don't forget. The stepSize is
182 : // actually counting our valueFrom number thus we add
183 : // interpolation until the last step - 1
184 0 : for (int j = 0; j < stepSize - 1; j++)
185 : {
186 0 : valueFrom += interp;
187 0 : table[k++] = valueFrom;
188 : }
189 : }
190 : }
191 : }
192 :
193 14 : return table;
194 : }
195 :
196 : /*** check that the referenced dataset for each band has the
197 : correct data type and returns whether a 2 band I+Q dataset should
198 : be mapped onto a single complex band.
199 : Returns BANDERROR for error, STRAIGHT for 1:1 mapping, TWOBANDCOMPLEX for 2
200 : bands -> 1 complex band
201 : */
202 : typedef enum
203 : {
204 : BANDERROR,
205 : STRAIGHT,
206 : TWOBANDCOMPLEX
207 : } BandMappingRCM;
208 :
209 16 : static BandMappingRCM checkBandFileMappingRCM(GDALDataType dataType,
210 : GDALDataset *poBandFile,
211 : bool isNITF)
212 : {
213 :
214 16 : GDALRasterBand *band1 = poBandFile->GetRasterBand(1);
215 16 : GDALDataType bandfileType = band1->GetRasterDataType();
216 : // if there is one band and it has the same datatype, the band file gets
217 : // passed straight through
218 16 : if ((poBandFile->GetRasterCount() == 1 ||
219 16 : poBandFile->GetRasterCount() == 4) &&
220 : dataType == bandfileType)
221 16 : return STRAIGHT;
222 :
223 : // if the band file has 2 bands, they should represent I+Q
224 : // and be a compatible data type
225 0 : if (poBandFile->GetRasterCount() == 2 && GDALDataTypeIsComplex(dataType))
226 : {
227 0 : GDALRasterBand *band2 = poBandFile->GetRasterBand(2);
228 :
229 0 : if (bandfileType != band2->GetRasterDataType())
230 0 : return BANDERROR; // both bands must be same datatype
231 :
232 : // check compatible types - there are 4 complex types in GDAL
233 0 : if ((dataType == GDT_CInt16 && bandfileType == GDT_Int16) ||
234 0 : (dataType == GDT_CInt32 && bandfileType == GDT_Int32) ||
235 0 : (dataType == GDT_CFloat32 && bandfileType == GDT_Float32) ||
236 0 : (dataType == GDT_CFloat64 && bandfileType == GDT_Float64))
237 0 : return TWOBANDCOMPLEX;
238 :
239 0 : if ((dataType == GDT_CInt16 && bandfileType == GDT_CInt16) ||
240 0 : (dataType == GDT_CInt32 && bandfileType == GDT_CInt32) ||
241 0 : (dataType == GDT_CFloat32 && bandfileType == GDT_CFloat32) ||
242 0 : (dataType == GDT_CFloat64 && bandfileType == GDT_CFloat64))
243 0 : return TWOBANDCOMPLEX;
244 : }
245 :
246 0 : if (isNITF)
247 : {
248 0 : return STRAIGHT;
249 : }
250 :
251 0 : return BANDERROR; // don't accept any other combinations
252 : }
253 :
254 : /************************************************************************/
255 : /* RCMRasterBand */
256 : /************************************************************************/
257 :
258 10 : RCMRasterBand::RCMRasterBand(RCMDataset *poDSIn, int nBandIn,
259 : GDALDataType eDataTypeIn, const char *pszPole,
260 : GDALDataset *poBandFileIn, bool bTwoBandComplex,
261 10 : bool isOneFilePerPolIn, bool isNITFIn)
262 : : poBandFile(poBandFileIn), poRCMDataset(poDSIn),
263 : twoBandComplex(bTwoBandComplex), isOneFilePerPol(isOneFilePerPolIn),
264 10 : isNITF(isNITFIn)
265 : {
266 10 : poDS = poDSIn;
267 10 : this->nBand = nBandIn;
268 10 : eDataType = eDataTypeIn;
269 :
270 : /*Check image type, whether there is one file per polarization or
271 : *one file containing all polarizations*/
272 10 : if (this->isOneFilePerPol)
273 : {
274 10 : poBand = poBandFile->GetRasterBand(1);
275 : }
276 : else
277 : {
278 0 : poBand = poBandFile->GetRasterBand(this->nBand);
279 : }
280 :
281 10 : poBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
282 :
283 10 : if (pszPole != nullptr && strlen(pszPole) != 0)
284 : {
285 10 : SetMetadataItem("POLARIMETRIC_INTERP", pszPole);
286 : }
287 10 : }
288 :
289 : /************************************************************************/
290 : /* RCMRasterBand() */
291 : /************************************************************************/
292 :
293 20 : RCMRasterBand::~RCMRasterBand()
294 :
295 : {
296 10 : if (poBandFile != nullptr)
297 10 : GDALClose(reinterpret_cast<GDALRasterBandH>(poBandFile));
298 20 : }
299 :
300 : /************************************************************************/
301 : /* IReadBlock() */
302 : /************************************************************************/
303 :
304 13 : CPLErr RCMRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
305 :
306 : {
307 13 : int nRequestXSize = 0;
308 13 : int nRequestYSize = 0;
309 13 : GetActualBlockSize(nBlockXOff, nBlockYOff, &nRequestXSize, &nRequestYSize);
310 :
311 : // Zero initial partial right-most and bottom-most blocks
312 13 : if (nRequestXSize < nBlockXSize || nRequestYSize < nBlockYSize)
313 : {
314 1 : memset(pImage, 0,
315 1 : static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
316 1 : nBlockXSize * nBlockYSize);
317 : }
318 :
319 13 : int dataTypeSize = GDALGetDataTypeSizeBytes(eDataType);
320 : GDALDataType bandFileType =
321 13 : poBandFile->GetRasterBand(1)->GetRasterDataType();
322 13 : int bandFileSize = GDALGetDataTypeSizeBytes(bandFileType);
323 :
324 : // case: 2 bands representing I+Q -> one complex band
325 13 : if (twoBandComplex && !this->isNITF)
326 : {
327 : // int bandFileSize = GDALGetDataTypeSizeBytes(bandFileType);
328 : // this data type is the complex version of the band file
329 : // Roberto: don't check that for the moment: CPLAssert(dataTypeSize ==
330 : // bandFileSize * 2);
331 :
332 : return
333 : // I and Q from each band are pixel-interleaved into this complex
334 : // band
335 0 : poBandFile->RasterIO(
336 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
337 : nRequestXSize, nRequestYSize, pImage, nRequestXSize,
338 : nRequestYSize, bandFileType, 2, nullptr, dataTypeSize,
339 0 : static_cast<GSpacing>(dataTypeSize) * nBlockXSize, bandFileSize,
340 0 : nullptr);
341 : }
342 13 : else if (twoBandComplex && this->isNITF)
343 : {
344 0 : return poBand->RasterIO(
345 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
346 : nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
347 0 : eDataType, 0, static_cast<GSpacing>(dataTypeSize) * nBlockXSize,
348 0 : nullptr);
349 : }
350 :
351 13 : if (poRCMDataset->IsComplexData())
352 : {
353 : // this data type is the complex version of the band file
354 : // Roberto: don't check that for the moment: CPLAssert(dataTypeSize ==
355 : // bandFileSize * 2);
356 : return
357 : // I and Q from each band are pixel-interleaved into this complex
358 : // band
359 0 : poBandFile->RasterIO(
360 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
361 : nRequestXSize, nRequestYSize, pImage, nRequestXSize,
362 : nRequestYSize, bandFileType, 2, nullptr, dataTypeSize,
363 0 : static_cast<GSpacing>(dataTypeSize) * nBlockXSize, bandFileSize,
364 0 : nullptr);
365 : }
366 :
367 : // case: band file == this band
368 : // NOTE: if the underlying band is opened with the NITF driver, it may
369 : // combine 2 band I+Q -> complex band
370 13 : else if (poBandFile->GetRasterBand(1)->GetRasterDataType() == eDataType)
371 : {
372 26 : return poBand->RasterIO(
373 13 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
374 : nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
375 13 : eDataType, 0, static_cast<GSpacing>(dataTypeSize) * nBlockXSize,
376 13 : nullptr);
377 : }
378 : else
379 : {
380 0 : CPLAssert(FALSE);
381 : return CE_Failure;
382 : }
383 : }
384 :
385 : /************************************************************************/
386 : /* ReadLUT() */
387 : /************************************************************************/
388 : /* Read the provided LUT in to m_ndTable */
389 : /* 1. The gains list spans the range extent covered by all */
390 : /* beams(if applicable). */
391 : /* 2. The mapping between the entry of gains */
392 : /* list and the range sample index is : the range sample */
393 : /* index = gains entry index * stepSize + pixelFirstLutValue, */
394 : /* where the gains entry index starts with ‘0’.For ScanSAR SLC, */
395 : /* the range sample index refers to the index on the COPG */
396 : /************************************************************************/
397 6 : void RCMCalibRasterBand::ReadLUT()
398 : {
399 :
400 : char bandNumber[12];
401 6 : snprintf(bandNumber, sizeof(bandNumber), "%d", poDS->GetRasterCount() + 1);
402 :
403 6 : CPLXMLTreeCloser psLUT(CPLParseXMLFile(m_pszLUTFile));
404 6 : if (!psLUT)
405 0 : return;
406 :
407 6 : this->m_nfOffset =
408 6 : CPLAtof(CPLGetXMLValue(psLUT.get(), "=lut.offset", "0.0"));
409 :
410 6 : this->pixelFirstLutValue =
411 6 : atoi(CPLGetXMLValue(psLUT.get(), "=lut.pixelFirstLutValue", "0"));
412 :
413 6 : this->stepSize = atoi(CPLGetXMLValue(psLUT.get(), "=lut.stepSize", "0"));
414 :
415 6 : this->numberOfValues =
416 6 : atoi(CPLGetXMLValue(psLUT.get(), "=lut.numberOfValues", "0"));
417 :
418 6 : if (this->numberOfValues <= 0)
419 : {
420 0 : CPLError(
421 : CE_Failure, CPLE_NotSupported,
422 : "ERROR: The RCM driver does not support the LUT Number Of Values "
423 : "equal or lower than zero.");
424 0 : return;
425 : }
426 :
427 : const CPLStringList aosLUTList(
428 6 : CSLTokenizeString2(CPLGetXMLValue(psLUT.get(), "=lut.gains", ""), " ",
429 6 : CSLT_HONOURSTRINGS));
430 :
431 6 : if (this->stepSize <= 0)
432 : {
433 6 : if (this->pixelFirstLutValue <= 0)
434 : {
435 0 : CPLError(
436 : CE_Failure, CPLE_NotSupported,
437 : "ERROR: The RCM driver does not support LUT Pixel First Lut "
438 : "Value equal or lower than zero when the product is "
439 : "descending.");
440 0 : return;
441 : }
442 : }
443 :
444 : /* Get the Pixel Per range */
445 6 : if (this->stepSize == 0 || this->stepSize == INT_MIN ||
446 6 : this->numberOfValues == INT_MIN ||
447 6 : abs(this->stepSize) > INT_MAX / abs(this->numberOfValues))
448 : {
449 0 : CPLError(CE_Failure, CPLE_AppDefined,
450 : "Bad values of stepSize / numberOfValues");
451 0 : return;
452 : }
453 :
454 6 : this->m_nTableSize = abs(this->stepSize) * abs(this->numberOfValues);
455 :
456 6 : if (this->m_nTableSize < this->m_poBandDataset->GetRasterXSize())
457 : {
458 0 : CPLError(
459 : CE_Failure, CPLE_NotSupported,
460 : "ERROR: The RCM driver does not support range of LUT gain values "
461 : "lower than the full image pixel range.");
462 0 : return;
463 : }
464 :
465 : // Avoid excessive memory allocation
466 6 : if (this->m_nTableSize > 1000 * 1000)
467 : {
468 0 : CPLError(CE_Failure, CPLE_NotSupported, "Too many elements in LUT: %d",
469 : this->m_nTableSize);
470 0 : return;
471 : }
472 :
473 : /* Allocate the right LUT size according to the product range pixel */
474 6 : this->m_nfTable =
475 6 : InterpolateValues(aosLUTList.List(), this->m_nTableSize, this->stepSize,
476 : this->numberOfValues, this->pixelFirstLutValue);
477 6 : if (!this->m_nfTable)
478 0 : return;
479 :
480 : // 32 max + space
481 : char *lut_gains = static_cast<char *>(
482 6 : VSI_CALLOC_VERBOSE(this->m_nTableSize, max_space_for_string));
483 6 : if (!lut_gains)
484 0 : return;
485 :
486 107496 : for (int i = 0; i < this->m_nTableSize; i++)
487 : {
488 : char lut[max_space_for_string];
489 : // 6.123004711900930e+04 %e Scientific annotation
490 107490 : snprintf(lut, sizeof(lut), "%e ", this->m_nfTable[i]);
491 107490 : strcat(lut_gains, lut);
492 : }
493 :
494 6 : poDS->SetMetadataItem(CPLString("LUT_GAINS_").append(bandNumber).c_str(),
495 6 : lut_gains);
496 : // Can free this because the function SetMetadataItem takes a copy
497 6 : CPLFree(lut_gains);
498 :
499 : char snum[256];
500 6 : if (this->m_eCalib == eCalibration::Sigma0)
501 : {
502 2 : poDS->SetMetadataItem(CPLString("LUT_TYPE_").append(bandNumber).c_str(),
503 2 : "SIGMA0");
504 : }
505 4 : else if (this->m_eCalib == eCalibration::Beta0)
506 : {
507 2 : poDS->SetMetadataItem(CPLString("LUT_TYPE_").append(bandNumber).c_str(),
508 2 : "BETA0");
509 : }
510 2 : else if (this->m_eCalib == eCalibration::Gamma)
511 : {
512 2 : poDS->SetMetadataItem(CPLString("LUT_TYPE_").append(bandNumber).c_str(),
513 2 : "GAMMA");
514 : }
515 6 : snprintf(snum, sizeof(snum), "%d", this->m_nTableSize);
516 6 : poDS->SetMetadataItem(CPLString("LUT_SIZE_").append(bandNumber).c_str(),
517 6 : snum);
518 6 : snprintf(snum, sizeof(snum), "%f", this->m_nfOffset);
519 6 : poDS->SetMetadataItem(CPLString("LUT_OFFSET_").append(bandNumber).c_str(),
520 6 : snum);
521 : }
522 :
523 : /************************************************************************/
524 : /* ReadNoiseLevels() */
525 : /************************************************************************/
526 : /* Read the provided LUT in to m_nfTableNoiseLevels */
527 : /* 1. The gains list spans the range extent covered by all */
528 : /* beams(if applicable). */
529 : /* 2. The mapping between the entry of gains */
530 : /* list and the range sample index is : the range sample */
531 : /* index = gains entry index * stepSize + pixelFirstLutValue, */
532 : /* where the gains entry index starts with ‘0’.For ScanSAR SLC, */
533 : /* the range sample index refers to the index on the COPG */
534 : /************************************************************************/
535 6 : void RCMCalibRasterBand::ReadNoiseLevels()
536 : {
537 :
538 6 : this->m_nfTableNoiseLevels = nullptr;
539 :
540 6 : if (this->m_pszNoiseLevelsFile == nullptr)
541 : {
542 0 : return;
543 : }
544 :
545 : char bandNumber[12];
546 6 : snprintf(bandNumber, sizeof(bandNumber), "%d", poDS->GetRasterCount() + 1);
547 :
548 6 : CPLXMLTreeCloser psNoiseLevels(CPLParseXMLFile(this->m_pszNoiseLevelsFile));
549 6 : if (!psNoiseLevels)
550 0 : return;
551 :
552 : // Load Beta Nought, Sigma Nought, Gamma noise levels
553 : // Loop through all nodes with spaces
554 : const CPLXMLNode *psReferenceNoiseLevelNode =
555 6 : CPLGetXMLNode(psNoiseLevels.get(), "=noiseLevels");
556 6 : if (!psReferenceNoiseLevelNode)
557 0 : return;
558 :
559 6 : for (const CPLXMLNode *psNodeInc = psReferenceNoiseLevelNode->psChild;
560 234 : psNodeInc != nullptr; psNodeInc = psNodeInc->psNext)
561 : {
562 228 : if (EQUAL(psNodeInc->pszValue, "referenceNoiseLevel"))
563 : {
564 : const CPLXMLNode *psCalibType =
565 18 : CPLGetXMLNode(psNodeInc, "sarCalibrationType");
566 : const CPLXMLNode *psPixelFirstNoiseValue =
567 18 : CPLGetXMLNode(psNodeInc, "pixelFirstNoiseValue");
568 18 : const CPLXMLNode *psStepSize = CPLGetXMLNode(psNodeInc, "stepSize");
569 : const CPLXMLNode *psNumberOfValues =
570 18 : CPLGetXMLNode(psNodeInc, "numberOfValues");
571 : const CPLXMLNode *psNoiseLevelValues =
572 18 : CPLGetXMLNode(psNodeInc, "noiseLevelValues");
573 :
574 18 : if (psCalibType != nullptr && psPixelFirstNoiseValue != nullptr &&
575 18 : psStepSize != nullptr && psNumberOfValues != nullptr &&
576 : psNoiseLevelValues != nullptr)
577 : {
578 18 : const char *calibType = CPLGetXMLValue(psCalibType, "", "");
579 18 : this->pixelFirstLutValueNoiseLevels =
580 18 : atoi(CPLGetXMLValue(psPixelFirstNoiseValue, "", "0"));
581 18 : this->stepSizeNoiseLevels =
582 18 : atoi(CPLGetXMLValue(psStepSize, "", "0"));
583 18 : this->numberOfValuesNoiseLevels =
584 18 : atoi(CPLGetXMLValue(psNumberOfValues, "", "0"));
585 : const char *noiseLevelValues =
586 18 : CPLGetXMLValue(psNoiseLevelValues, "", "");
587 18 : if (this->stepSizeNoiseLevels > 0 &&
588 0 : this->numberOfValuesNoiseLevels != INT_MIN &&
589 0 : abs(this->numberOfValuesNoiseLevels) <
590 0 : INT_MAX / this->stepSizeNoiseLevels)
591 : {
592 0 : char **papszNoiseLevelList = CSLTokenizeString2(
593 : noiseLevelValues, " ", CSLT_HONOURSTRINGS);
594 : /* Get the Pixel Per range */
595 0 : this->m_nTableNoiseLevelsSize =
596 0 : abs(this->stepSizeNoiseLevels) *
597 0 : abs(this->numberOfValuesNoiseLevels);
598 :
599 0 : if ((EQUAL(calibType, "Beta Nought") &&
600 0 : this->m_eCalib == Beta0) ||
601 0 : (EQUAL(calibType, "Sigma Nought") &&
602 0 : this->m_eCalib == Sigma0) ||
603 0 : (EQUAL(calibType, "Gamma") && this->m_eCalib == Gamma))
604 : {
605 : /* Allocate the right Noise Levels size according to the
606 : * product range pixel */
607 0 : this->m_nfTableNoiseLevels = InterpolateValues(
608 : papszNoiseLevelList, this->m_nTableNoiseLevelsSize,
609 : this->stepSizeNoiseLevels,
610 : this->numberOfValuesNoiseLevels,
611 : this->pixelFirstLutValueNoiseLevels);
612 : }
613 :
614 0 : CSLDestroy(papszNoiseLevelList);
615 : }
616 :
617 18 : if (this->m_nfTableNoiseLevels != nullptr)
618 : {
619 0 : break; // We are done
620 : }
621 : }
622 : }
623 : }
624 : }
625 :
626 : /************************************************************************/
627 : /* RCMCalibRasterBand() */
628 : /************************************************************************/
629 :
630 6 : RCMCalibRasterBand::RCMCalibRasterBand(
631 : RCMDataset *poDataset, const char *pszPolarization, GDALDataType eType,
632 : GDALDataset *poBandDataset, eCalibration eCalib, const char *pszLUT,
633 6 : const char *pszNoiseLevels, GDALDataType eOriginalType)
634 : : m_eCalib(eCalib), m_poBandDataset(poBandDataset),
635 12 : m_eOriginalType(eOriginalType), m_pszLUTFile(VSIStrdup(pszLUT)),
636 6 : m_pszNoiseLevelsFile(VSIStrdup(pszNoiseLevels))
637 : {
638 6 : this->poDS = poDataset;
639 :
640 6 : if (pszPolarization != nullptr && strlen(pszPolarization) != 0)
641 : {
642 6 : SetMetadataItem("POLARIMETRIC_INTERP", pszPolarization);
643 : }
644 :
645 6 : if ((eType == GDT_CInt16) || (eType == GDT_CFloat32))
646 0 : this->eDataType = GDT_CFloat32;
647 : else
648 6 : this->eDataType = GDT_Float32;
649 :
650 6 : GDALRasterBand *poRasterBand = poBandDataset->GetRasterBand(1);
651 6 : poRasterBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
652 :
653 6 : ReadLUT();
654 6 : ReadNoiseLevels();
655 6 : }
656 :
657 : /************************************************************************/
658 : /* ~RCMCalibRasterBand() */
659 : /************************************************************************/
660 :
661 12 : RCMCalibRasterBand::~RCMCalibRasterBand()
662 : {
663 6 : CPLFree(m_nfTable);
664 6 : CPLFree(m_nfTableNoiseLevels);
665 6 : CPLFree(m_pszLUTFile);
666 6 : CPLFree(m_pszNoiseLevelsFile);
667 6 : GDALClose(m_poBandDataset);
668 12 : }
669 :
670 : /************************************************************************/
671 : /* IReadBlock() */
672 : /************************************************************************/
673 :
674 0 : CPLErr RCMCalibRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
675 : void *pImage)
676 : {
677 : CPLErr eErr;
678 0 : int nRequestXSize = 0;
679 0 : int nRequestYSize = 0;
680 0 : GetActualBlockSize(nBlockXOff, nBlockYOff, &nRequestXSize, &nRequestYSize);
681 :
682 : // Zero initial partial right-most and bottom-most blocks
683 0 : if (nRequestXSize < nBlockXSize || nRequestYSize < nBlockYSize)
684 : {
685 0 : memset(pImage, 0,
686 0 : static_cast<size_t>(GDALGetDataTypeSizeBytes(eDataType)) *
687 0 : nBlockXSize * nBlockYSize);
688 : }
689 :
690 0 : if (this->m_eOriginalType == GDT_CInt16)
691 : {
692 : /* read in complex values */
693 : GInt16 *panImageTmp = static_cast<GInt16 *>(
694 0 : VSI_MALLOC3_VERBOSE(nBlockXSize, nBlockYSize,
695 : GDALGetDataTypeSizeBytes(m_eOriginalType)));
696 0 : if (!panImageTmp)
697 0 : return CE_Failure;
698 :
699 0 : if (m_poBandDataset->GetRasterCount() == 2)
700 : {
701 0 : eErr = m_poBandDataset->RasterIO(
702 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
703 : nRequestXSize, nRequestYSize, panImageTmp, nRequestXSize,
704 : nRequestYSize, this->m_eOriginalType, 2, nullptr, 4,
705 0 : nBlockXSize * 4, 4, nullptr);
706 :
707 : /*
708 : eErr = m_poBandDataset->RasterIO(GF_Read,
709 : nBlockXOff * nBlockXSize,
710 : nBlockYOff * nBlockYSize,
711 : nRequestXSize, nRequestYSize,
712 : panImageTmp, nRequestXSize, nRequestYSize,
713 : GDT_Int32,
714 : 2, nullptr, 4, nBlockXSize * 4, 2, nullptr);
715 : */
716 : }
717 : else
718 : {
719 0 : eErr = m_poBandDataset->RasterIO(
720 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
721 : nRequestXSize, nRequestYSize, panImageTmp, nRequestXSize,
722 : nRequestYSize, this->m_eOriginalType, 1, nullptr, 4,
723 0 : nBlockXSize * 4, 0, nullptr);
724 :
725 : /*
726 : eErr =
727 : m_poBandDataset->RasterIO(GF_Read,
728 : nBlockXOff * nBlockXSize,
729 : nBlockYOff * nBlockYSize,
730 : nRequestXSize, nRequestYSize,
731 : panImageTmp, nRequestXSize, nRequestYSize,
732 : GDT_UInt32,
733 : 1, nullptr, 4, nBlockXSize * 4, 0, nullptr);
734 : */
735 :
736 : #ifdef CPL_LSB
737 : /* First, undo the 32bit swap. */
738 0 : GDALSwapWords(pImage, 4, nBlockXSize * nBlockYSize, 4);
739 :
740 : /* Then apply 16 bit swap. */
741 0 : GDALSwapWords(pImage, 2, nBlockXSize * nBlockYSize * 2, 2);
742 : #endif
743 : }
744 :
745 : /* calibrate the complex values */
746 0 : for (int i = 0; i < nRequestYSize; i++)
747 : {
748 0 : for (int j = 0; j < nRequestXSize; j++)
749 : {
750 : /* calculate pixel offset in memory*/
751 0 : const int nPixOff = 2 * (i * nBlockXSize + j);
752 0 : const int nTruePixOff = (i * nBlockXSize) + j;
753 :
754 : // Formula for Complex Q+J
755 0 : const float real = static_cast<float>(panImageTmp[nPixOff]);
756 0 : const float img = static_cast<float>(panImageTmp[nPixOff + 1]);
757 0 : const float digitalValue = (real * real) + (img * img);
758 0 : const float lutValue =
759 0 : static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
760 0 : const float calibValue = digitalValue / (lutValue * lutValue);
761 :
762 0 : reinterpret_cast<float *>(pImage)[nTruePixOff] = calibValue;
763 : }
764 : }
765 :
766 0 : CPLFree(panImageTmp);
767 : }
768 :
769 : // If the underlying file is NITF CFloat32
770 0 : else if (this->m_eOriginalType == GDT_CFloat32 ||
771 0 : this->m_eOriginalType == GDT_CFloat64)
772 : {
773 : /* read in complex values */
774 : const int dataTypeSize =
775 0 : GDALGetDataTypeSizeBytes(this->m_eOriginalType);
776 0 : const GDALDataType bandFileType = this->m_eOriginalType;
777 0 : const int bandFileDataTypeSize = GDALGetDataTypeSizeBytes(bandFileType);
778 :
779 : /* read the original image complex values in a temporary image space */
780 0 : float *pafImageTmp = static_cast<float *>(VSI_MALLOC3_VERBOSE(
781 : nBlockXSize, nBlockYSize, 2 * bandFileDataTypeSize));
782 0 : if (!pafImageTmp)
783 0 : return CE_Failure;
784 :
785 : eErr =
786 : // I and Q from each band are pixel-interleaved into this complex
787 : // band
788 0 : m_poBandDataset->RasterIO(
789 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
790 : nRequestXSize, nRequestYSize, pafImageTmp, nRequestXSize,
791 : nRequestYSize, bandFileType, 2, nullptr, dataTypeSize,
792 0 : static_cast<GSpacing>(dataTypeSize) * nBlockXSize,
793 : bandFileDataTypeSize, nullptr);
794 :
795 : /* calibrate the complex values */
796 0 : for (int i = 0; i < nRequestYSize; i++)
797 : {
798 0 : for (int j = 0; j < nRequestXSize; j++)
799 : {
800 : /* calculate pixel offset in memory*/
801 0 : const int nPixOff = 2 * (i * nBlockXSize + j);
802 0 : const int nTruePixOff = (i * nBlockXSize) + j;
803 :
804 : // Formula for Complex Q+J
805 0 : const float real = pafImageTmp[nPixOff];
806 0 : const float img = pafImageTmp[nPixOff + 1];
807 0 : const float digitalValue = (real * real) + (img * img);
808 0 : const float lutValue =
809 0 : static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
810 0 : const float calibValue = digitalValue / (lutValue * lutValue);
811 :
812 0 : reinterpret_cast<float *>(pImage)[nTruePixOff] = calibValue;
813 : }
814 : }
815 :
816 0 : CPLFree(pafImageTmp);
817 : }
818 :
819 0 : else if (this->m_eOriginalType == GDT_Float32)
820 : {
821 : /* A Float32 is actual 4 bytes */
822 0 : eErr = m_poBandDataset->RasterIO(
823 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
824 : nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
825 0 : GDT_Float32, 1, nullptr, 4, nBlockXSize * 4, 0, nullptr);
826 :
827 : /* iterate over detected values */
828 0 : for (int i = 0; i < nRequestYSize; i++)
829 : {
830 0 : for (int j = 0; j < nRequestXSize; j++)
831 : {
832 0 : int nPixOff = (i * nBlockXSize) + j;
833 :
834 : /* For detected products, in order to convert the digital number
835 : of a given range sample to a calibrated value, the digital value
836 : is first squared, then the offset(B) is added and the result is
837 : divided by the gains value(A) corresponding to the range sample.
838 : RCM-SP-53-0419 Issue 2/5: January 2, 2018 Page 7-56 */
839 0 : const float digitalValue =
840 0 : reinterpret_cast<float *>(pImage)[nPixOff];
841 0 : const float A =
842 0 : static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
843 0 : reinterpret_cast<float *>(pImage)[nPixOff] =
844 0 : ((digitalValue * digitalValue) +
845 0 : static_cast<float>(this->m_nfOffset)) /
846 : A;
847 : }
848 : }
849 : }
850 :
851 0 : else if (this->m_eOriginalType == GDT_Float64)
852 : {
853 : /* A Float64 is actual 8 bytes */
854 0 : eErr = m_poBandDataset->RasterIO(
855 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
856 : nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize,
857 0 : GDT_Float64, 1, nullptr, 8, nBlockXSize * 8, 0, nullptr);
858 :
859 : /* iterate over detected values */
860 0 : for (int i = 0; i < nRequestYSize; i++)
861 : {
862 0 : for (int j = 0; j < nRequestXSize; j++)
863 : {
864 0 : int nPixOff = (i * nBlockXSize) + j;
865 :
866 : /* For detected products, in order to convert the digital number
867 : of a given range sample to a calibrated value, the digital value
868 : is first squared, then the offset(B) is added and the result is
869 : divided by the gains value(A) corresponding to the range sample.
870 : RCM-SP-53-0419 Issue 2/5: January 2, 2018 Page 7-56 */
871 0 : const float digitalValue =
872 0 : reinterpret_cast<float *>(pImage)[nPixOff];
873 0 : const float A =
874 0 : static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
875 0 : reinterpret_cast<float *>(pImage)[nPixOff] =
876 0 : ((digitalValue * digitalValue) +
877 0 : static_cast<float>(this->m_nfOffset)) /
878 : A;
879 : }
880 : }
881 : }
882 :
883 0 : else if (this->m_eOriginalType == GDT_UInt16)
884 : {
885 : /* read in detected values */
886 0 : GUInt16 *panImageTmp = static_cast<GUInt16 *>(VSI_MALLOC3_VERBOSE(
887 : nBlockXSize, nBlockYSize, GDALGetDataTypeSizeBytes(GDT_UInt16)));
888 0 : if (!panImageTmp)
889 0 : return CE_Failure;
890 0 : eErr = m_poBandDataset->RasterIO(
891 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
892 : nRequestXSize, nRequestYSize, panImageTmp, nRequestXSize,
893 0 : nRequestYSize, GDT_UInt16, 1, nullptr, 2, nBlockXSize * 2, 0,
894 : nullptr);
895 :
896 : /* iterate over detected values */
897 0 : for (int i = 0; i < nRequestYSize; i++)
898 : {
899 0 : for (int j = 0; j < nRequestXSize; j++)
900 : {
901 0 : const int nPixOff = (i * nBlockXSize) + j;
902 :
903 0 : const float digitalValue =
904 0 : static_cast<float>(panImageTmp[nPixOff]);
905 0 : const float A =
906 0 : static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
907 0 : reinterpret_cast<float *>(pImage)[nPixOff] =
908 0 : ((digitalValue * digitalValue) +
909 0 : static_cast<float>(this->m_nfOffset)) /
910 : A;
911 : }
912 : }
913 0 : CPLFree(panImageTmp);
914 : } /* Ticket #2104: Support for ScanSAR products */
915 :
916 0 : else if (this->m_eOriginalType == GDT_Byte)
917 : {
918 : GByte *pabyImageTmp =
919 0 : static_cast<GByte *>(VSI_MALLOC2_VERBOSE(nBlockXSize, nBlockYSize));
920 0 : if (!pabyImageTmp)
921 0 : return CE_Failure;
922 0 : eErr = m_poBandDataset->RasterIO(
923 0 : GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
924 : nRequestXSize, nRequestYSize, pabyImageTmp, nRequestXSize,
925 0 : nRequestYSize, GDT_Byte, 1, nullptr, 1, nBlockXSize, 0, nullptr);
926 :
927 : /* iterate over detected values */
928 0 : for (int i = 0; i < nRequestYSize; i++)
929 : {
930 0 : for (int j = 0; j < nRequestXSize; j++)
931 : {
932 0 : const int nPixOff = (i * nBlockXSize) + j;
933 :
934 0 : const float digitalValue =
935 0 : static_cast<float>(pabyImageTmp[nPixOff]);
936 0 : const float A =
937 0 : static_cast<float>(m_nfTable[nBlockXOff * nBlockXSize + j]);
938 0 : reinterpret_cast<float *>(pImage)[nPixOff] =
939 0 : ((digitalValue * digitalValue) +
940 0 : static_cast<float>(this->m_nfOffset)) /
941 : A;
942 : }
943 : }
944 0 : CPLFree(pabyImageTmp);
945 : }
946 : else
947 : {
948 0 : CPLAssert(FALSE);
949 : return CE_Failure;
950 : }
951 0 : return eErr;
952 : }
953 :
954 : /************************************************************************/
955 : /* ==================================================================== */
956 : /* RCMDataset */
957 : /* ==================================================================== */
958 : /************************************************************************/
959 :
960 : /************************************************************************/
961 : /* ~RCMDataset() */
962 : /************************************************************************/
963 :
964 16 : RCMDataset::~RCMDataset()
965 :
966 : {
967 8 : RCMDataset::FlushCache(true);
968 :
969 8 : if (nGCPCount > 0)
970 : {
971 8 : GDALDeinitGCPs(nGCPCount, pasGCPList);
972 8 : CPLFree(pasGCPList);
973 : }
974 :
975 8 : RCMDataset::CloseDependentDatasets();
976 :
977 8 : if (papszSubDatasets != nullptr)
978 4 : CSLDestroy(papszSubDatasets);
979 :
980 8 : if (papszExtraFiles != nullptr)
981 8 : CSLDestroy(papszExtraFiles);
982 :
983 8 : if (m_nfIncidenceAngleTable != nullptr)
984 8 : CPLFree(m_nfIncidenceAngleTable);
985 16 : }
986 :
987 : /************************************************************************/
988 : /* CloseDependentDatasets() */
989 : /************************************************************************/
990 :
991 8 : int RCMDataset::CloseDependentDatasets()
992 : {
993 8 : int bHasDroppedRef = GDALPamDataset::CloseDependentDatasets();
994 :
995 8 : if (nBands != 0)
996 8 : bHasDroppedRef = TRUE;
997 :
998 24 : for (int iBand = 0; iBand < nBands; iBand++)
999 : {
1000 16 : delete papoBands[iBand];
1001 : }
1002 8 : nBands = 0;
1003 :
1004 8 : return bHasDroppedRef;
1005 : }
1006 :
1007 : /************************************************************************/
1008 : /* GetFileList() */
1009 : /************************************************************************/
1010 :
1011 0 : char **RCMDataset::GetFileList()
1012 :
1013 : {
1014 0 : char **papszFileList = GDALPamDataset::GetFileList();
1015 :
1016 0 : papszFileList = CSLInsertStrings(papszFileList, -1, papszExtraFiles);
1017 :
1018 0 : return papszFileList;
1019 : }
1020 :
1021 : /************************************************************************/
1022 : /* Open() */
1023 : /************************************************************************/
1024 :
1025 10 : GDALDataset *RCMDataset::Open(GDALOpenInfo *poOpenInfo)
1026 :
1027 : {
1028 : /* -------------------------------------------------------------------- */
1029 : /* Is this a RCM Product.xml definition? */
1030 : /* -------------------------------------------------------------------- */
1031 10 : if (!RCMDatasetIdentify(poOpenInfo))
1032 : {
1033 0 : return nullptr;
1034 : }
1035 :
1036 : /* -------------------------------------------------------------------- */
1037 : /* Get subdataset information, if relevant */
1038 : /* -------------------------------------------------------------------- */
1039 10 : const char *pszFilename = poOpenInfo->pszFilename;
1040 10 : eCalibration eCalib = None;
1041 :
1042 10 : if (STARTS_WITH_CI(pszFilename, szLayerCalibration) &&
1043 6 : pszFilename[strlen(szLayerCalibration)] == chLayerSeparator)
1044 : {
1045 : // The calibration name and filename begins after the hard coded layer
1046 : // name
1047 6 : pszFilename += strlen(szLayerCalibration) + 1;
1048 :
1049 6 : if (STARTS_WITH_CI(pszFilename, szBETA0))
1050 : {
1051 1 : eCalib = Beta0;
1052 : }
1053 5 : else if (STARTS_WITH_CI(pszFilename, szSIGMA0))
1054 : {
1055 1 : eCalib = Sigma0;
1056 : }
1057 4 : else if (STARTS_WITH_CI(pszFilename, szGAMMA) ||
1058 3 : STARTS_WITH_CI(pszFilename, "GAMMA0")) // Cover both situation
1059 : {
1060 1 : eCalib = Gamma;
1061 : }
1062 3 : else if (STARTS_WITH_CI(pszFilename, szUNCALIB))
1063 : {
1064 2 : eCalib = Uncalib;
1065 : }
1066 : else
1067 : {
1068 1 : CPLError(CE_Failure, CPLE_NotSupported,
1069 : "Unsupported calibration type");
1070 1 : return nullptr;
1071 : }
1072 :
1073 : /* advance the pointer to the actual filename */
1074 35 : while (*pszFilename != '\0' && *pszFilename != ':')
1075 30 : pszFilename++;
1076 :
1077 5 : if (*pszFilename == ':')
1078 5 : pszFilename++;
1079 :
1080 : // need to redo the directory check:
1081 : // the GDALOpenInfo check would have failed because of the calibration
1082 : // string on the filename
1083 : VSIStatBufL sStat;
1084 5 : if (VSIStatL(pszFilename, &sStat) == 0)
1085 4 : poOpenInfo->bIsDirectory = VSI_ISDIR(sStat.st_mode);
1086 : }
1087 :
1088 18 : CPLString osMDFilename;
1089 9 : if (poOpenInfo->bIsDirectory)
1090 : {
1091 : /* Check for directory access when there is a product.xml file in the
1092 : directory. */
1093 : osMDFilename =
1094 2 : CPLFormCIFilenameSafe(pszFilename, "product.xml", nullptr);
1095 :
1096 : VSIStatBufL sStat;
1097 2 : if (VSIStatL(osMDFilename, &sStat) != 0)
1098 : {
1099 : /* If not, check for directory extra 'metadata' access when there is
1100 : a product.xml file in the directory. */
1101 1 : osMDFilename = CPLFormCIFilenameSafe(pszFilename,
1102 2 : GetMetadataProduct(), nullptr);
1103 : }
1104 : }
1105 : else
1106 7 : osMDFilename = pszFilename;
1107 :
1108 : /* -------------------------------------------------------------------- */
1109 : /* Ingest the Product.xml file. */
1110 : /* -------------------------------------------------------------------- */
1111 18 : CPLXMLTreeCloser psProduct(CPLParseXMLFile(osMDFilename));
1112 9 : if (!psProduct)
1113 1 : return nullptr;
1114 :
1115 : /* -------------------------------------------------------------------- */
1116 : /* Confirm the requested access is supported. */
1117 : /* -------------------------------------------------------------------- */
1118 8 : if (poOpenInfo->eAccess == GA_Update)
1119 : {
1120 0 : ReportUpdateNotSupportedByDriver("RCM");
1121 0 : return nullptr;
1122 : }
1123 :
1124 : const CPLXMLNode *psSceneAttributes =
1125 8 : CPLGetXMLNode(psProduct.get(), "=product.sceneAttributes");
1126 8 : if (psSceneAttributes == nullptr)
1127 : {
1128 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1129 : "ERROR: Failed to find <sceneAttributes> in document.");
1130 0 : return nullptr;
1131 : }
1132 :
1133 8 : const CPLXMLNode *psImageAttributes = CPLGetXMLNode(
1134 : psProduct.get(), "=product.sceneAttributes.imageAttributes");
1135 8 : if (psImageAttributes == nullptr)
1136 : {
1137 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1138 : "ERROR: Failed to find <sceneAttributes.imageAttributes> in "
1139 : "document.");
1140 0 : return nullptr;
1141 : }
1142 :
1143 : int numberOfEntries =
1144 8 : atoi(CPLGetXMLValue(psSceneAttributes, "numberOfEntries", "0"));
1145 8 : if (numberOfEntries != 1)
1146 : {
1147 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1148 : "ERROR: Only RCM with Complex Single-beam is supported.");
1149 0 : return nullptr;
1150 : }
1151 :
1152 : const CPLXMLNode *psImageReferenceAttributes =
1153 8 : CPLGetXMLNode(psProduct.get(), "=product.imageReferenceAttributes");
1154 8 : if (psImageReferenceAttributes == nullptr)
1155 : {
1156 0 : CPLError(
1157 : CE_Failure, CPLE_OpenFailed,
1158 : "ERROR: Failed to find <imageReferenceAttributes> in document.");
1159 0 : return nullptr;
1160 : }
1161 :
1162 : const CPLXMLNode *psImageGenerationParameters =
1163 8 : CPLGetXMLNode(psProduct.get(), "=product.imageGenerationParameters");
1164 8 : if (psImageGenerationParameters == nullptr)
1165 : {
1166 0 : CPLError(
1167 : CE_Failure, CPLE_OpenFailed,
1168 : "ERROR: Failed to find <imageGenerationParameters> in document.");
1169 0 : return nullptr;
1170 : }
1171 :
1172 : /* -------------------------------------------------------------------- */
1173 : /* Create the dataset. */
1174 : /* -------------------------------------------------------------------- */
1175 16 : auto poDS = std::make_unique<RCMDataset>();
1176 :
1177 8 : poDS->psProduct = std::move(psProduct);
1178 :
1179 : /* -------------------------------------------------------------------- */
1180 : /* Get overall image information. */
1181 : /* -------------------------------------------------------------------- */
1182 8 : poDS->nRasterXSize = atoi(CPLGetXMLValue(
1183 : psSceneAttributes, "imageAttributes.samplesPerLine", "-1"));
1184 8 : poDS->nRasterYSize = atoi(
1185 : CPLGetXMLValue(psSceneAttributes, "imageAttributes.numLines", "-1"));
1186 8 : if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize))
1187 : {
1188 0 : return nullptr;
1189 : }
1190 :
1191 : /* -------------------------------------------------------------------- */
1192 : /* Check product type, as to determine if there are LUTs for */
1193 : /* calibration purposes. */
1194 : /* -------------------------------------------------------------------- */
1195 :
1196 : const char *pszItem =
1197 8 : CPLGetXMLValue(psImageGenerationParameters,
1198 : "generalProcessingInformation.productType", "UNK");
1199 8 : poDS->SetMetadataItem("PRODUCT_TYPE", pszItem);
1200 8 : const char *pszProductType = pszItem;
1201 :
1202 : pszItem =
1203 8 : CPLGetXMLValue(poDS->psProduct.get(), "=product.productId", "UNK");
1204 8 : poDS->SetMetadataItem("PRODUCT_ID", pszItem);
1205 :
1206 8 : pszItem = CPLGetXMLValue(
1207 8 : poDS->psProduct.get(),
1208 : "=product.securityAttributes.securityClassification", "UNK");
1209 8 : poDS->SetMetadataItem("SECURITY_CLASSIFICATION", pszItem);
1210 :
1211 : pszItem =
1212 8 : CPLGetXMLValue(poDS->psProduct.get(),
1213 : "=product.sourceAttributes.polarizationDataMode", "UNK");
1214 8 : poDS->SetMetadataItem("POLARIZATION_DATA_MODE", pszItem);
1215 :
1216 8 : pszItem = CPLGetXMLValue(psImageGenerationParameters,
1217 : "generalProcessingInformation.processingFacility",
1218 : "UNK");
1219 8 : poDS->SetMetadataItem("PROCESSING_FACILITY", pszItem);
1220 :
1221 : pszItem =
1222 8 : CPLGetXMLValue(psImageGenerationParameters,
1223 : "generalProcessingInformation.processingTime", "UNK");
1224 8 : poDS->SetMetadataItem("PROCESSING_TIME", pszItem);
1225 :
1226 8 : pszItem = CPLGetXMLValue(psImageGenerationParameters,
1227 : "sarProcessingInformation.satelliteHeight", "UNK");
1228 8 : poDS->SetMetadataItem("SATELLITE_HEIGHT", pszItem);
1229 :
1230 8 : pszItem = CPLGetXMLValue(
1231 : psImageGenerationParameters,
1232 : "sarProcessingInformation.zeroDopplerTimeFirstLine", "UNK");
1233 8 : poDS->SetMetadataItem("FIRST_LINE_TIME", pszItem);
1234 :
1235 8 : pszItem = CPLGetXMLValue(psImageGenerationParameters,
1236 : "sarProcessingInformation.zeroDopplerTimeLastLine",
1237 : "UNK");
1238 8 : poDS->SetMetadataItem("LAST_LINE_TIME", pszItem);
1239 :
1240 8 : pszItem = CPLGetXMLValue(psImageGenerationParameters,
1241 : "sarProcessingInformation.lutApplied", "");
1242 8 : poDS->SetMetadataItem("LUT_APPLIED", pszItem);
1243 :
1244 : /*---------------------------------------------------------------------
1245 : If true, a polarization dependent application LUT has been applied
1246 : for each polarization channel. Otherwise the same application LUT
1247 : has been applied for all polarization channels. Not applicable to
1248 : lookupTable = "Unity*" or if dataType = "Floating-Point".
1249 : --------------------------------------------------------------------- */
1250 8 : pszItem = CPLGetXMLValue(psImageGenerationParameters,
1251 : "sarProcessingInformation.perPolarizationScaling",
1252 : "false");
1253 8 : poDS->SetMetadataItem("PER_POLARIZATION_SCALING", pszItem);
1254 8 : if (EQUAL(pszItem, "true") || EQUAL(pszItem, "TRUE"))
1255 : {
1256 8 : poDS->bPerPolarizationScaling = TRUE;
1257 : }
1258 :
1259 : /* the following cases can be assumed to have no LUTs, as per
1260 : * RN-RP-51-2713, but also common sense
1261 : * SLC represents a SLant range georeferenced Complex product
1262 : * (i.e., equivalent to a Single-Look Complex product for RADARSAT-1 or
1263 : * RADARSAT-2). • GRD or GRC represent GRound range georeferenced Detected
1264 : * or Complex products (GRD is equivalent to an SGX, SCN or SCW product for
1265 : * RADARSAT1 or RADARSAT-2). • GCD or GCC represent GeoCoded Detected or
1266 : * Complex products (GCD is equivalent to an SSG or SPG product for
1267 : * RADARSAT-1 or RADARSAT-2).
1268 : */
1269 8 : bool bCanCalib = false;
1270 8 : if (!(STARTS_WITH_CI(pszProductType, "UNK") ||
1271 8 : STARTS_WITH_CI(pszProductType, "GCD") ||
1272 8 : STARTS_WITH_CI(pszProductType, "GCC")))
1273 : {
1274 8 : bCanCalib = true;
1275 : }
1276 :
1277 : /* -------------------------------------------------------------------- */
1278 : /* Get dataType (so we can recognise complex data), and the */
1279 : /* bitsPerSample. */
1280 : /* -------------------------------------------------------------------- */
1281 8 : const char *pszSampleDataType = CPLGetXMLValue(
1282 : psImageReferenceAttributes, "rasterAttributes.sampleType", "");
1283 8 : poDS->SetMetadataItem("SAMPLE_TYPE", pszSampleDataType);
1284 :
1285 : /* Either Integer (16 bits) or Floating-Point (32 bits) */
1286 8 : const char *pszDataType = CPLGetXMLValue(psImageReferenceAttributes,
1287 : "rasterAttributes.dataType", "");
1288 8 : poDS->SetMetadataItem("DATA_TYPE", pszDataType);
1289 :
1290 : /* 2 entries for complex data 1 entry for magnitude detected data */
1291 8 : const char *pszBitsPerSample = CPLGetXMLValue(
1292 : psImageReferenceAttributes, "rasterAttributes.bitsPerSample", "");
1293 8 : const int nBitsPerSample = atoi(pszBitsPerSample);
1294 8 : poDS->SetMetadataItem("BITS_PER_SAMPLE", pszBitsPerSample);
1295 :
1296 : const char *pszSampledPixelSpacingTime =
1297 8 : CPLGetXMLValue(psImageReferenceAttributes,
1298 : "rasterAttributes.sampledPixelSpacingTime", "UNK");
1299 8 : poDS->SetMetadataItem("SAMPLED_PIXEL_SPACING_TIME",
1300 8 : pszSampledPixelSpacingTime);
1301 :
1302 : const char *pszSampledLineSpacingTime =
1303 8 : CPLGetXMLValue(psImageReferenceAttributes,
1304 : "rasterAttributes.sampledLineSpacingTime", "UNK");
1305 8 : poDS->SetMetadataItem("SAMPLED_LINE_SPACING_TIME",
1306 8 : pszSampledLineSpacingTime);
1307 :
1308 : GDALDataType eDataType;
1309 :
1310 8 : if (EQUAL(pszSampleDataType, "Mixed")) /* RCM MLC has Mixed sampleType */
1311 : {
1312 1 : poDS->isComplexData = false; /* RCM MLC is detected, non-complex */
1313 1 : if (nBitsPerSample == 32)
1314 : {
1315 1 : eDataType = GDT_Float32; /* 32 bits, check read block */
1316 1 : poDS->magnitudeBits = 32;
1317 : }
1318 : else
1319 : {
1320 0 : eDataType = GDT_UInt16; /* 16 bits, check read block */
1321 0 : poDS->magnitudeBits = 16;
1322 : }
1323 : }
1324 7 : else if (EQUAL(pszSampleDataType, "Complex"))
1325 : {
1326 0 : poDS->isComplexData = true;
1327 : /* Usually this is the same bits for both */
1328 0 : poDS->realBitsComplexData = nBitsPerSample;
1329 0 : poDS->imaginaryBitsComplexData = nBitsPerSample;
1330 :
1331 0 : if (nBitsPerSample == 32)
1332 : {
1333 0 : eDataType = GDT_CFloat32; /* 32 bits, check read block */
1334 : }
1335 : else
1336 : {
1337 0 : eDataType = GDT_CInt16; /* 16 bits, check read block */
1338 : }
1339 : }
1340 7 : else if (nBitsPerSample == 32 &&
1341 0 : EQUAL(pszSampleDataType, "Magnitude Detected"))
1342 : {
1343 : /* Actually, I don't need to test the 'dataType'=' Floating-Point', we
1344 : * know that a 32 bits per sample */
1345 0 : eDataType = GDT_Float32;
1346 0 : poDS->isComplexData = false;
1347 0 : poDS->magnitudeBits = 32;
1348 : }
1349 7 : else if (nBitsPerSample == 16 &&
1350 7 : EQUAL(pszSampleDataType, "Magnitude Detected"))
1351 : {
1352 : /* Actually, I don't need to test the 'dataType'=' Integer', we know
1353 : * that a 16 bits per sample */
1354 7 : eDataType = GDT_UInt16;
1355 7 : poDS->isComplexData = false;
1356 7 : poDS->magnitudeBits = 16;
1357 : }
1358 : else
1359 : {
1360 0 : CPLError(CE_Failure, CPLE_AppDefined,
1361 : "ERROR: dataType=%s and bitsPerSample=%d are not a supported "
1362 : "configuration.",
1363 : pszDataType, nBitsPerSample);
1364 0 : return nullptr;
1365 : }
1366 :
1367 : /* Indicates whether pixel number (i.e., range) increases or decreases with
1368 : range time. For GCD and GCC products, this applies to intermediate ground
1369 : range image data prior to geocoding. */
1370 : const char *pszPixelTimeOrdering =
1371 8 : CPLGetXMLValue(psImageReferenceAttributes,
1372 : "rasterAttributes.pixelTimeOrdering", "UNK");
1373 8 : poDS->SetMetadataItem("PIXEL_TIME_ORDERING", pszPixelTimeOrdering);
1374 :
1375 : /* Indicates whether line numbers (i.e., azimuth) increase or decrease with
1376 : azimuth time. For GCD and GCC products, this applies to intermediate ground
1377 : range image data prior to geocoding. */
1378 8 : const char *pszLineTimeOrdering = CPLGetXMLValue(
1379 : psImageReferenceAttributes, "rasterAttributes.lineTimeOrdering", "UNK");
1380 8 : poDS->SetMetadataItem("LINE_TIME_ORDERING", pszLineTimeOrdering);
1381 :
1382 : /* while we're at it, extract the pixel spacing information */
1383 : const char *pszPixelSpacing =
1384 8 : CPLGetXMLValue(psImageReferenceAttributes,
1385 : "rasterAttributes.sampledPixelSpacing", "UNK");
1386 8 : poDS->SetMetadataItem("PIXEL_SPACING", pszPixelSpacing);
1387 :
1388 : const char *pszLineSpacing =
1389 8 : CPLGetXMLValue(psImageReferenceAttributes,
1390 : "rasterAttributes.sampledLineSpacing", "UNK");
1391 8 : poDS->SetMetadataItem("LINE_SPACING", pszLineSpacing);
1392 :
1393 : /* -------------------------------------------------------------------- */
1394 : /* Open each of the data files as a complex band. */
1395 : /* -------------------------------------------------------------------- */
1396 16 : std::string osBeta0LUT;
1397 16 : std::string osGammaLUT;
1398 16 : std::string osSigma0LUT;
1399 : // Same file for all calibrations except the calibration is plit inside the
1400 : // XML
1401 16 : std::string osNoiseLevelsValues;
1402 :
1403 16 : const CPLString osPath = CPLGetPathSafe(osMDFilename);
1404 :
1405 : /* Get a list of all polarizations */
1406 : const CPLXMLNode *psSourceAttrs =
1407 8 : CPLGetXMLNode(poDS->psProduct.get(), "=product.sourceAttributes");
1408 8 : if (psSourceAttrs == nullptr)
1409 : {
1410 0 : CPLError(
1411 : CE_Failure, CPLE_OpenFailed,
1412 : "ERROR: RCM source attributes is missing. Please contact your data "
1413 : "provider for a corrected dataset.");
1414 0 : return nullptr;
1415 : }
1416 :
1417 8 : const CPLXMLNode *psRadarParameters = CPLGetXMLNode(
1418 8 : poDS->psProduct.get(), "=product.sourceAttributes.radarParameters");
1419 8 : if (psRadarParameters == nullptr)
1420 : {
1421 0 : CPLError(
1422 : CE_Failure, CPLE_OpenFailed,
1423 : "ERROR: RCM radar parameters is missing. Please contact your data "
1424 : "provider for a corrected dataset.");
1425 0 : return nullptr;
1426 : }
1427 :
1428 : const char *pszPolarizations =
1429 8 : CPLGetXMLValue(psRadarParameters, "polarizations", "");
1430 8 : if (pszPolarizations == nullptr || strlen(pszPolarizations) == 0)
1431 : {
1432 0 : CPLError(
1433 : CE_Failure, CPLE_OpenFailed,
1434 : "ERROR: RCM polarizations list is missing. Please contact your "
1435 : "data provider for a corrected dataset.");
1436 0 : return nullptr;
1437 : }
1438 8 : poDS->SetMetadataItem("POLARIZATIONS", pszPolarizations);
1439 :
1440 : const char *psAcquisitionType =
1441 8 : CPLGetXMLValue(psRadarParameters, "acquisitionType", "UNK");
1442 8 : poDS->SetMetadataItem("ACQUISITION_TYPE", psAcquisitionType);
1443 :
1444 8 : const char *psBeams = CPLGetXMLValue(psRadarParameters, "beams", "UNK");
1445 8 : poDS->SetMetadataItem("BEAMS", psBeams);
1446 :
1447 : const CPLStringList aosPolarizationsGrids(
1448 16 : CSLTokenizeString2(pszPolarizations, " ", 0));
1449 16 : CPLStringList imageBandList;
1450 16 : CPLStringList imageBandFileList;
1451 8 : const int nPolarizationsGridCount = aosPolarizationsGrids.size();
1452 :
1453 : /* File names for full resolution IPDFs. For GeoTIFF format, one entry per
1454 : pole; For NITF 2.1 format, only one entry. */
1455 8 : bool bIsNITF = false;
1456 8 : const char *pszNITF_Filename = nullptr;
1457 8 : int imageBandFileCount = 0;
1458 8 : int imageBandCount = 0;
1459 :
1460 : /* Split the polarization string*/
1461 24 : auto iss = std::istringstream((CPLString(pszPolarizations)).c_str());
1462 16 : auto pol = std::string{};
1463 : /* Count number of polarizations*/
1464 24 : while (iss >> pol)
1465 16 : imageBandCount++;
1466 :
1467 8 : for (const CPLXMLNode *psNode = psImageAttributes->psChild;
1468 131 : psNode != nullptr; psNode = psNode->psNext)
1469 : {
1470 : /* Find the tif or ntf filename */
1471 123 : if (psNode->eType != CXT_Element || !(EQUAL(psNode->pszValue, "ipdf")))
1472 107 : continue;
1473 :
1474 : /* --------------------------------------------------------------------
1475 : */
1476 : /* Fetch ipdf image. Could be either tif or ntf. */
1477 : /* Replace / by \\ */
1478 : /* --------------------------------------------------------------------
1479 : */
1480 17 : const char *pszBasedFilename = CPLGetXMLValue(psNode, "", "");
1481 17 : if (*pszBasedFilename == '\0')
1482 0 : continue;
1483 :
1484 : /* Count number of image names within ipdf tag*/
1485 17 : imageBandFileCount++;
1486 :
1487 17 : CPLString pszUpperBasedFilename(CPLString(pszBasedFilename).toupper());
1488 :
1489 : const bool bEndsWithNTF =
1490 34 : strlen(pszUpperBasedFilename.c_str()) > 4 &&
1491 17 : EQUAL(pszUpperBasedFilename.c_str() +
1492 : strlen(pszUpperBasedFilename.c_str()) - 4,
1493 : ".NTF");
1494 :
1495 17 : if (bEndsWithNTF)
1496 : {
1497 : /* Found it! It would not exist one more */
1498 0 : bIsNITF = true;
1499 0 : pszNITF_Filename = pszBasedFilename;
1500 0 : break;
1501 : }
1502 : else
1503 : {
1504 : /* Keep adding polarizations filename according to the pole */
1505 17 : const char *pszPole = CPLGetXMLValue(psNode, "pole", "");
1506 17 : if (*pszPole == '\0')
1507 : {
1508 : /* Guard against case when pole is a null string, skip it */
1509 0 : imageBandFileCount--;
1510 0 : continue;
1511 : }
1512 :
1513 17 : if (EQUAL(pszPole, "XC"))
1514 : {
1515 : /* Skip RCM MLC's 3rd band file ##XC.tif */
1516 1 : imageBandFileCount--;
1517 1 : continue;
1518 : }
1519 :
1520 16 : imageBandList.AddString(CPLString(pszPole).toupper());
1521 16 : imageBandFileList.AddString(pszBasedFilename);
1522 : }
1523 : }
1524 :
1525 : /* -------------------------------------------------------------------- */
1526 : /* Incidence Angle in a sub-folder */
1527 : /* called 'calibration' from the 'metadata' folder */
1528 : /* -------------------------------------------------------------------- */
1529 :
1530 8 : const char *pszIncidenceAngleFileName = CPLGetXMLValue(
1531 : psImageReferenceAttributes, "incidenceAngleFileName", "");
1532 :
1533 8 : if (pszIncidenceAngleFileName != nullptr)
1534 : {
1535 8 : if (CPLHasPathTraversal(pszIncidenceAngleFileName))
1536 : {
1537 0 : CPLError(CE_Failure, CPLE_NotSupported,
1538 : "Path traversal detected in %s",
1539 : pszIncidenceAngleFileName);
1540 0 : return nullptr;
1541 : }
1542 16 : const CPLString osIncidenceAngleFilePath = CPLFormFilenameSafe(
1543 8 : CPLFormFilenameSafe(osPath, CALIBRATION_FOLDER, nullptr).c_str(),
1544 16 : pszIncidenceAngleFileName, nullptr);
1545 : /* Check if the file exist */
1546 8 : if (IsValidXMLFile(osIncidenceAngleFilePath))
1547 : {
1548 : CPLXMLTreeCloser psIncidenceAngle(
1549 16 : CPLParseXMLFile(osIncidenceAngleFilePath));
1550 :
1551 8 : int pixelFirstLutValue = atoi(
1552 8 : CPLGetXMLValue(psIncidenceAngle.get(),
1553 : "=incidenceAngles.pixelFirstAnglesValue", "0"));
1554 :
1555 8 : const int stepSize = atoi(CPLGetXMLValue(
1556 8 : psIncidenceAngle.get(), "=incidenceAngles.stepSize", "0"));
1557 : const int numberOfValues =
1558 8 : atoi(CPLGetXMLValue(psIncidenceAngle.get(),
1559 : "=incidenceAngles.numberOfValues", "0"));
1560 :
1561 8 : if (!(stepSize == 0 || stepSize == INT_MIN ||
1562 : numberOfValues == INT_MIN ||
1563 8 : abs(numberOfValues) > INT_MAX / abs(stepSize)))
1564 : {
1565 : /* Get the Pixel Per range */
1566 8 : const int tableSize = abs(stepSize) * abs(numberOfValues);
1567 :
1568 16 : CPLString angles;
1569 : // Loop through all nodes with spaces
1570 : CPLXMLNode *psNextNode =
1571 8 : CPLGetXMLNode(psIncidenceAngle.get(), "=incidenceAngles");
1572 :
1573 : CPLXMLNode *psNodeInc;
1574 458 : for (psNodeInc = psNextNode->psChild; psNodeInc != nullptr;
1575 450 : psNodeInc = psNodeInc->psNext)
1576 : {
1577 450 : if (EQUAL(psNodeInc->pszValue, "angles"))
1578 : {
1579 402 : if (angles.length() > 0)
1580 : {
1581 394 : angles.append(" "); /* separator */
1582 : }
1583 : const char *valAngle =
1584 402 : CPLGetXMLValue(psNodeInc, "", "");
1585 402 : angles.append(valAngle);
1586 : }
1587 : }
1588 :
1589 : char **papszAngleList =
1590 8 : CSLTokenizeString2(angles, " ", CSLT_HONOURSTRINGS);
1591 :
1592 : /* Allocate the right LUT size according to the product range pixel
1593 : */
1594 8 : poDS->m_IncidenceAngleTableSize = tableSize;
1595 16 : poDS->m_nfIncidenceAngleTable =
1596 8 : InterpolateValues(papszAngleList, tableSize, stepSize,
1597 : numberOfValues, pixelFirstLutValue);
1598 :
1599 8 : CSLDestroy(papszAngleList);
1600 : }
1601 : }
1602 : }
1603 :
1604 24 : for (int iPoleInx = 0; iPoleInx < nPolarizationsGridCount; iPoleInx++)
1605 : {
1606 : // Search for a specific band name
1607 : const CPLString pszPole =
1608 16 : CPLString(aosPolarizationsGrids[iPoleInx]).toupper();
1609 :
1610 : // Look if the NoiseLevel file xml exist for the
1611 16 : const CPLXMLNode *psRefNode = psImageReferenceAttributes->psChild;
1612 232 : for (; psRefNode != nullptr; psRefNode = psRefNode->psNext)
1613 : {
1614 216 : if (EQUAL(psRefNode->pszValue, "noiseLevelFileName") && bCanCalib)
1615 : {
1616 : /* Determine which incidence angle correction this is */
1617 : const char *pszPoleToMatch =
1618 32 : CPLGetXMLValue(psRefNode, "pole", "");
1619 : const char *pszNoiseLevelFile =
1620 32 : CPLGetXMLValue(psRefNode, "", "");
1621 :
1622 32 : if (*pszPoleToMatch == '\0')
1623 16 : continue;
1624 :
1625 32 : if (EQUAL(pszPoleToMatch, "XC"))
1626 : /* Skip noise for RCM MLC's 3rd band XC */
1627 0 : continue;
1628 :
1629 32 : if (EQUAL(pszNoiseLevelFile, ""))
1630 0 : continue;
1631 :
1632 : /* --------------------------------------------------------------------
1633 : */
1634 : /* With RCM, LUT file is different per polarizarion (pole)
1635 : */
1636 : /* The following code make sure to loop through all
1637 : * possible */
1638 : /* 'noiseLevelFileName' and match the <ipdf 'pole' name */
1639 : /* --------------------------------------------------------------------
1640 : */
1641 32 : if (pszPole.compare(pszPoleToMatch) != 0)
1642 : {
1643 16 : continue;
1644 : }
1645 :
1646 : /* --------------------------------------------------------------------
1647 : */
1648 : /* LUT calibration is unique per pole in a sub-folder */
1649 : /* called 'calibration' from the 'metadata' folder */
1650 : /* --------------------------------------------------------------------
1651 : */
1652 :
1653 16 : if (CPLHasPathTraversal(pszNoiseLevelFile))
1654 : {
1655 0 : CPLError(CE_Failure, CPLE_NotSupported,
1656 : "Path traversal detected in %s",
1657 : pszNoiseLevelFile);
1658 0 : return nullptr;
1659 : }
1660 32 : CPLString oNoiseLevelPath = CPLFormFilenameSafe(
1661 16 : CPLFormFilenameSafe(osPath, CALIBRATION_FOLDER, nullptr)
1662 : .c_str(),
1663 32 : pszNoiseLevelFile, nullptr);
1664 16 : if (IsValidXMLFile(oNoiseLevelPath))
1665 : {
1666 16 : osNoiseLevelsValues = std::move(oNoiseLevelPath);
1667 : }
1668 : }
1669 : }
1670 :
1671 : // Search again with different file
1672 16 : psRefNode = psImageReferenceAttributes->psChild;
1673 232 : for (; psRefNode != nullptr; psRefNode = psRefNode->psNext)
1674 : {
1675 216 : if (EQUAL(psRefNode->pszValue, "lookupTableFileName") && bCanCalib)
1676 : {
1677 : /* Determine which incidence angle correction this is */
1678 : const char *pszLUTType =
1679 102 : CPLGetXMLValue(psRefNode, "sarCalibrationType", "");
1680 : const char *pszPoleToMatch =
1681 102 : CPLGetXMLValue(psRefNode, "pole", "");
1682 102 : const char *pszLUTFile = CPLGetXMLValue(psRefNode, "", "");
1683 :
1684 102 : if (*pszPoleToMatch == '\0')
1685 54 : continue;
1686 :
1687 102 : if (EQUAL(pszPoleToMatch, "XC"))
1688 : /* Skip Calib for RCM MLC's 3rd band XC */
1689 6 : continue;
1690 :
1691 96 : if (*pszLUTType == '\0')
1692 0 : continue;
1693 :
1694 96 : if (EQUAL(pszLUTType, ""))
1695 0 : continue;
1696 :
1697 : /* --------------------------------------------------------------------
1698 : */
1699 : /* With RCM, LUT file is different per polarizarion (pole)
1700 : */
1701 : /* The following code make sure to loop through all
1702 : * possible */
1703 : /* 'lookupTableFileName' and match the <ipdf 'pole' name */
1704 : /* --------------------------------------------------------------------
1705 : */
1706 96 : if (pszPole.compare(pszPoleToMatch) != 0)
1707 : {
1708 48 : continue;
1709 : }
1710 :
1711 : /* --------------------------------------------------------------------
1712 : */
1713 : /* LUT calibration is unique per pole in a sub-folder */
1714 : /* called 'calibration' from the 'metadata' folder */
1715 : /* --------------------------------------------------------------------
1716 : */
1717 48 : if (CPLHasPathTraversal(pszLUTFile))
1718 : {
1719 0 : CPLError(CE_Failure, CPLE_NotSupported,
1720 : "Path traversal detected in %s", pszLUTFile);
1721 0 : return nullptr;
1722 : }
1723 96 : const CPLString osLUTFilePath = CPLFormFilenameSafe(
1724 48 : CPLFormFilenameSafe(osPath, CALIBRATION_FOLDER, nullptr)
1725 : .c_str(),
1726 96 : pszLUTFile, nullptr);
1727 :
1728 64 : if (EQUAL(pszLUTType, "Beta Nought") &&
1729 16 : IsValidXMLFile(osLUTFilePath))
1730 : {
1731 32 : poDS->papszExtraFiles =
1732 16 : CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
1733 :
1734 : CPLString pszBuf(
1735 16 : FormatCalibration(szBETA0, osMDFilename.c_str()));
1736 16 : osBeta0LUT = osLUTFilePath;
1737 :
1738 : const char *oldLut =
1739 16 : poDS->GetMetadataItem("BETA_NOUGHT_LUT");
1740 16 : if (oldLut == nullptr)
1741 : {
1742 8 : poDS->SetMetadataItem("BETA_NOUGHT_LUT", osLUTFilePath);
1743 : }
1744 : else
1745 : {
1746 : /* Keep adding LUT file for all bands, should be planty
1747 : * of space */
1748 : char *ptrConcatLut =
1749 8 : static_cast<char *>(CPLMalloc(2048));
1750 8 : ptrConcatLut[0] =
1751 : '\0'; /* Just initialize the first byte */
1752 8 : strcat(ptrConcatLut, oldLut);
1753 8 : strcat(ptrConcatLut, ",");
1754 8 : strcat(ptrConcatLut, osLUTFilePath);
1755 8 : poDS->SetMetadataItem("BETA_NOUGHT_LUT", ptrConcatLut);
1756 8 : CPLFree(ptrConcatLut);
1757 : }
1758 :
1759 32 : poDS->papszSubDatasets = CSLSetNameValue(
1760 16 : poDS->papszSubDatasets, "SUBDATASET_3_NAME", pszBuf);
1761 16 : poDS->papszSubDatasets = CSLSetNameValue(
1762 16 : poDS->papszSubDatasets, "SUBDATASET_3_DESC",
1763 : "Beta Nought calibrated");
1764 : }
1765 48 : else if (EQUAL(pszLUTType, "Sigma Nought") &&
1766 16 : IsValidXMLFile(osLUTFilePath))
1767 : {
1768 32 : poDS->papszExtraFiles =
1769 16 : CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
1770 :
1771 : CPLString pszBuf(
1772 16 : FormatCalibration(szSIGMA0, osMDFilename.c_str()));
1773 16 : osSigma0LUT = osLUTFilePath;
1774 :
1775 : const char *oldLut =
1776 16 : poDS->GetMetadataItem("SIGMA_NOUGHT_LUT");
1777 16 : if (oldLut == nullptr)
1778 : {
1779 16 : poDS->SetMetadataItem("SIGMA_NOUGHT_LUT",
1780 8 : osLUTFilePath);
1781 : }
1782 : else
1783 : {
1784 : /* Keep adding LUT file for all bands, should be planty
1785 : * of space */
1786 : char *ptrConcatLut =
1787 8 : static_cast<char *>(CPLMalloc(2048));
1788 8 : ptrConcatLut[0] =
1789 : '\0'; /* Just initialize the first byte */
1790 8 : strcat(ptrConcatLut, oldLut);
1791 8 : strcat(ptrConcatLut, ",");
1792 8 : strcat(ptrConcatLut, osLUTFilePath);
1793 8 : poDS->SetMetadataItem("SIGMA_NOUGHT_LUT", ptrConcatLut);
1794 8 : CPLFree(ptrConcatLut);
1795 : }
1796 :
1797 32 : poDS->papszSubDatasets = CSLSetNameValue(
1798 16 : poDS->papszSubDatasets, "SUBDATASET_2_NAME", pszBuf);
1799 16 : poDS->papszSubDatasets = CSLSetNameValue(
1800 16 : poDS->papszSubDatasets, "SUBDATASET_2_DESC",
1801 : "Sigma Nought calibrated");
1802 : }
1803 32 : else if (EQUAL(pszLUTType, "Gamma") &&
1804 16 : IsValidXMLFile(osLUTFilePath))
1805 : {
1806 32 : poDS->papszExtraFiles =
1807 16 : CSLAddString(poDS->papszExtraFiles, osLUTFilePath);
1808 :
1809 : CPLString pszBuf(
1810 16 : FormatCalibration(szGAMMA, osMDFilename.c_str()));
1811 16 : osGammaLUT = osLUTFilePath;
1812 :
1813 16 : const char *oldLut = poDS->GetMetadataItem("GAMMA_LUT");
1814 16 : if (oldLut == nullptr)
1815 : {
1816 8 : poDS->SetMetadataItem("GAMMA_LUT", osLUTFilePath);
1817 : }
1818 : else
1819 : {
1820 : /* Keep adding LUT file for all bands, should be planty
1821 : * of space */
1822 : char *ptrConcatLut =
1823 8 : static_cast<char *>(CPLMalloc(2048));
1824 8 : ptrConcatLut[0] =
1825 : '\0'; /* Just initialize the first byte */
1826 8 : strcat(ptrConcatLut, oldLut);
1827 8 : strcat(ptrConcatLut, ",");
1828 8 : strcat(ptrConcatLut, osLUTFilePath);
1829 8 : poDS->SetMetadataItem("GAMMA_LUT", ptrConcatLut);
1830 8 : CPLFree(ptrConcatLut);
1831 : }
1832 :
1833 32 : poDS->papszSubDatasets = CSLSetNameValue(
1834 16 : poDS->papszSubDatasets, "SUBDATASET_4_NAME", pszBuf);
1835 16 : poDS->papszSubDatasets = CSLSetNameValue(
1836 16 : poDS->papszSubDatasets, "SUBDATASET_4_DESC",
1837 : "Gamma calibrated");
1838 : }
1839 : }
1840 : }
1841 :
1842 : /* --------------------------------------------------------------------
1843 : */
1844 : /* Fetch ipdf image. Could be either tif or ntf. */
1845 : /* Replace / by \\ */
1846 : /* --------------------------------------------------------------------
1847 : */
1848 : const char *pszBasedFilename;
1849 16 : if (bIsNITF)
1850 : {
1851 0 : pszBasedFilename = pszNITF_Filename;
1852 : }
1853 : else
1854 : {
1855 16 : const int bandPositionIndex = imageBandList.FindString(pszPole);
1856 16 : if (bandPositionIndex < 0)
1857 : {
1858 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1859 : "ERROR: RCM cannot find the polarization %s. Please "
1860 : "contact your data provider for a corrected dataset",
1861 : pszPole.c_str());
1862 0 : return nullptr;
1863 : }
1864 :
1865 16 : pszBasedFilename = imageBandFileList[bandPositionIndex];
1866 : }
1867 :
1868 : /* --------------------------------------------------------------------
1869 : */
1870 : /* Form full filename (path of product.xml + basename). */
1871 : /* --------------------------------------------------------------------
1872 : */
1873 16 : std::string osPathImage = osPath;
1874 16 : std::string osBasedImage = pszBasedFilename;
1875 16 : if (STARTS_WITH(osBasedImage.c_str(), "../") ||
1876 0 : STARTS_WITH(osBasedImage.c_str(), "..\\"))
1877 : {
1878 16 : osPathImage = CPLGetPathSafe(osPath);
1879 16 : osBasedImage = osBasedImage.substr(strlen("../"));
1880 : }
1881 16 : if (CPLHasPathTraversal(osBasedImage.c_str()))
1882 : {
1883 0 : CPLError(CE_Failure, CPLE_NotSupported,
1884 : "Path traversal detected in %s", osBasedImage.c_str());
1885 0 : return nullptr;
1886 : }
1887 : const std::string osFullname = CPLFormFilenameSafe(
1888 16 : osPathImage.c_str(), osBasedImage.c_str(), nullptr);
1889 :
1890 : /* --------------------------------------------------------------------
1891 : */
1892 : /* Try and open the file. */
1893 : /* --------------------------------------------------------------------
1894 : */
1895 : auto poBandFile = std::unique_ptr<GDALDataset>(GDALDataset::Open(
1896 16 : osFullname.c_str(), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR));
1897 16 : if (poBandFile == nullptr || poBandFile->GetRasterCount() == 0)
1898 : {
1899 0 : continue;
1900 : }
1901 :
1902 32 : poDS->papszExtraFiles =
1903 16 : CSLAddString(poDS->papszExtraFiles, osFullname.c_str());
1904 :
1905 : /* Some CFloat32 NITF files have nBitsPerSample incorrectly reported */
1906 : /* as 16, and get misinterpreted as CInt16. Check the underlying NITF
1907 : */
1908 : /* and override if this is the case. */
1909 16 : if (poBandFile->GetRasterBand(1)->GetRasterDataType() == GDT_CFloat32)
1910 0 : eDataType = GDT_CFloat32;
1911 :
1912 : BandMappingRCM b =
1913 16 : checkBandFileMappingRCM(eDataType, poBandFile.get(), bIsNITF);
1914 16 : if (b == BANDERROR)
1915 : {
1916 0 : CPLError(CE_Failure, CPLE_AppDefined,
1917 : "The underlying band files do not have an appropriate "
1918 : "data type.");
1919 0 : return nullptr;
1920 : }
1921 16 : bool twoBandComplex = b == TWOBANDCOMPLEX;
1922 16 : bool isOneFilePerPol = (imageBandCount == imageBandFileCount);
1923 :
1924 : /* --------------------------------------------------------------------
1925 : */
1926 : /* Create the band. */
1927 : /* --------------------------------------------------------------------
1928 : */
1929 16 : int bandNum = poDS->GetRasterCount() + 1;
1930 16 : if (eCalib == None || eCalib == Uncalib)
1931 : {
1932 : RCMRasterBand *poBand = new RCMRasterBand(
1933 20 : poDS.get(), bandNum, eDataType, pszPole, poBandFile.release(),
1934 20 : twoBandComplex, isOneFilePerPol, bIsNITF);
1935 :
1936 10 : poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
1937 : }
1938 : else
1939 : {
1940 6 : const char *pszLUT = osSigma0LUT.c_str();
1941 6 : switch (eCalib)
1942 : {
1943 2 : case Sigma0:
1944 2 : pszLUT = osSigma0LUT.c_str();
1945 2 : break;
1946 2 : case Beta0:
1947 2 : pszLUT = osBeta0LUT.c_str();
1948 2 : break;
1949 2 : case Gamma:
1950 2 : pszLUT = osGammaLUT.c_str();
1951 2 : break;
1952 0 : default:
1953 : /* we should bomb gracefully... */
1954 0 : break;
1955 : }
1956 6 : if (pszLUT[0] == '\0')
1957 : {
1958 0 : CPLError(CE_Failure, CPLE_AppDefined, "LUT missing.");
1959 0 : return nullptr;
1960 : }
1961 :
1962 : // The variable 'osNoiseLevelsValues' is always the same for a ban
1963 : // name except the XML contains different calibration name
1964 6 : if (poDS->isComplexData)
1965 : {
1966 : // If Complex, always 32 bits
1967 : RCMCalibRasterBand *poBand = new RCMCalibRasterBand(
1968 0 : poDS.get(), pszPole, GDT_Float32, poBandFile.release(),
1969 0 : eCalib, pszLUT, osNoiseLevelsValues.c_str(), eDataType);
1970 0 : poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
1971 : }
1972 : else
1973 : {
1974 : // Whatever the datatype was previously set
1975 : RCMCalibRasterBand *poBand = new RCMCalibRasterBand(
1976 12 : poDS.get(), pszPole, eDataType, poBandFile.release(),
1977 12 : eCalib, pszLUT, osNoiseLevelsValues.c_str(), eDataType);
1978 6 : poDS->SetBand(poDS->GetRasterCount() + 1, poBand);
1979 : }
1980 : }
1981 : }
1982 :
1983 8 : if (poDS->papszSubDatasets != nullptr && eCalib == None)
1984 : {
1985 4 : CPLString pszBuf = FormatCalibration(szUNCALIB, osMDFilename.c_str());
1986 4 : poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
1987 : "SUBDATASET_1_NAME", pszBuf);
1988 4 : poDS->papszSubDatasets =
1989 4 : CSLSetNameValue(poDS->papszSubDatasets, "SUBDATASET_1_DESC",
1990 : "Uncalibrated digital numbers");
1991 : }
1992 4 : else if (poDS->papszSubDatasets != nullptr)
1993 : {
1994 4 : CSLDestroy(poDS->papszSubDatasets);
1995 4 : poDS->papszSubDatasets = nullptr;
1996 : }
1997 :
1998 : /* -------------------------------------------------------------------- */
1999 : /* Set the appropriate MATRIX_REPRESENTATION. */
2000 : /* -------------------------------------------------------------------- */
2001 :
2002 8 : if (poDS->GetRasterCount() == 4 &&
2003 0 : (eDataType == GDT_CInt16 || eDataType == GDT_CFloat32))
2004 : {
2005 0 : poDS->SetMetadataItem("MATRIX_REPRESENTATION", "SCATTERING");
2006 : }
2007 :
2008 : /* -------------------------------------------------------------------- */
2009 : /* Collect a few useful metadata items. */
2010 : /* -------------------------------------------------------------------- */
2011 :
2012 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "satellite", "");
2013 8 : poDS->SetMetadataItem("SATELLITE_IDENTIFIER", pszItem);
2014 :
2015 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "sensor", "");
2016 8 : poDS->SetMetadataItem("SENSOR_IDENTIFIER", pszItem);
2017 :
2018 : /* Get beam mode mnemonic */
2019 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "beamMode", "UNK");
2020 8 : poDS->SetMetadataItem("BEAM_MODE", pszItem);
2021 :
2022 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "beamModeMnemonic", "UNK");
2023 8 : poDS->SetMetadataItem("BEAM_MODE_MNEMONIC", pszItem);
2024 :
2025 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "beamModeDefinitionId", "UNK");
2026 8 : poDS->SetMetadataItem("BEAM_MODE_DEFINITION_ID", pszItem);
2027 :
2028 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "rawDataStartTime", "UNK");
2029 8 : poDS->SetMetadataItem("ACQUISITION_START_TIME", pszItem);
2030 :
2031 8 : pszItem = CPLGetXMLValue(psSourceAttrs, "inputDatasetFacilityId", "UNK");
2032 8 : poDS->SetMetadataItem("FACILITY_IDENTIFIER", pszItem);
2033 :
2034 8 : pszItem = CPLGetXMLValue(psSourceAttrs,
2035 : "orbitAndAttitude.orbitInformation.passDirection",
2036 : "UNK");
2037 8 : poDS->SetMetadataItem("ORBIT_DIRECTION", pszItem);
2038 8 : pszItem = CPLGetXMLValue(
2039 : psSourceAttrs, "orbitAndAttitude.orbitInformation.orbitDataSource",
2040 : "UNK");
2041 8 : poDS->SetMetadataItem("ORBIT_DATA_SOURCE", pszItem);
2042 8 : pszItem = CPLGetXMLValue(
2043 : psSourceAttrs, "orbitAndAttitude.orbitInformation.orbitDataFileName",
2044 : "UNK");
2045 8 : poDS->SetMetadataItem("ORBIT_DATA_FILE", pszItem);
2046 :
2047 : /* Get incidence angle information. DONE */
2048 8 : pszItem = CPLGetXMLValue(psSceneAttributes, "imageAttributes.incAngNearRng",
2049 : "UNK");
2050 8 : poDS->SetMetadataItem("NEAR_RANGE_INCIDENCE_ANGLE", pszItem);
2051 :
2052 8 : pszItem = CPLGetXMLValue(psSceneAttributes, "imageAttributes.incAngFarRng",
2053 : "UNK");
2054 8 : poDS->SetMetadataItem("FAR_RANGE_INCIDENCE_ANGLE", pszItem);
2055 :
2056 8 : pszItem = CPLGetXMLValue(psSceneAttributes,
2057 : "imageAttributes.slantRangeNearEdge", "UNK");
2058 8 : poDS->SetMetadataItem("SLANT_RANGE_NEAR_EDGE", pszItem);
2059 :
2060 8 : pszItem = CPLGetXMLValue(psSceneAttributes,
2061 : "imageAttributes.slantRangeFarEdge", "UNK");
2062 8 : poDS->SetMetadataItem("SLANT_RANGE_FAR_EDGE", pszItem);
2063 :
2064 : /*--------------------------------------------------------------------- */
2065 : /* Collect Map projection/Geotransform information, if present.DONE */
2066 : /* In RCM, there is no file that indicates */
2067 : /* -------------------------------------------------------------------- */
2068 8 : const CPLXMLNode *psMapProjection = CPLGetXMLNode(
2069 : psImageReferenceAttributes, "geographicInformation.mapProjection");
2070 :
2071 8 : if (psMapProjection != nullptr)
2072 : {
2073 : const CPLXMLNode *psPos =
2074 0 : CPLGetXMLNode(psMapProjection, "positioningInformation");
2075 :
2076 : pszItem =
2077 0 : CPLGetXMLValue(psMapProjection, "mapProjectionDescriptor", "UNK");
2078 0 : poDS->SetMetadataItem("MAP_PROJECTION_DESCRIPTOR", pszItem);
2079 :
2080 : pszItem =
2081 0 : CPLGetXMLValue(psMapProjection, "mapProjectionOrientation", "UNK");
2082 0 : poDS->SetMetadataItem("MAP_PROJECTION_ORIENTATION", pszItem);
2083 :
2084 0 : pszItem = CPLGetXMLValue(psMapProjection, "resamplingKernel", "UNK");
2085 0 : poDS->SetMetadataItem("RESAMPLING_KERNEL", pszItem);
2086 :
2087 0 : pszItem = CPLGetXMLValue(psMapProjection, "satelliteHeading", "UNK");
2088 0 : poDS->SetMetadataItem("SATELLITE_HEADING", pszItem);
2089 :
2090 0 : if (psPos != nullptr)
2091 : {
2092 0 : const double tl_x = CPLStrtod(
2093 : CPLGetXMLValue(psPos, "upperLeftCorner.mapCoordinate.easting",
2094 : "0.0"),
2095 : nullptr);
2096 0 : const double tl_y = CPLStrtod(
2097 : CPLGetXMLValue(psPos, "upperLeftCorner.mapCoordinate.northing",
2098 : "0.0"),
2099 : nullptr);
2100 0 : const double bl_x = CPLStrtod(
2101 : CPLGetXMLValue(psPos, "lowerLeftCorner.mapCoordinate.easting",
2102 : "0.0"),
2103 : nullptr);
2104 0 : const double bl_y = CPLStrtod(
2105 : CPLGetXMLValue(psPos, "lowerLeftCorner.mapCoordinate.northing",
2106 : "0.0"),
2107 : nullptr);
2108 0 : const double tr_x = CPLStrtod(
2109 : CPLGetXMLValue(psPos, "upperRightCorner.mapCoordinate.easting",
2110 : "0.0"),
2111 : nullptr);
2112 0 : const double tr_y = CPLStrtod(
2113 : CPLGetXMLValue(psPos, "upperRightCorner.mapCoordinate.northing",
2114 : "0.0"),
2115 : nullptr);
2116 0 : poDS->m_gt[1] = (tr_x - tl_x) / (poDS->nRasterXSize - 1);
2117 0 : poDS->m_gt[4] = (tr_y - tl_y) / (poDS->nRasterXSize - 1);
2118 0 : poDS->m_gt[2] = (bl_x - tl_x) / (poDS->nRasterYSize - 1);
2119 0 : poDS->m_gt[5] = (bl_y - tl_y) / (poDS->nRasterYSize - 1);
2120 0 : poDS->m_gt[0] = (tl_x - 0.5 * poDS->m_gt[1] - 0.5 * poDS->m_gt[2]);
2121 0 : poDS->m_gt[3] = (tl_y - 0.5 * poDS->m_gt[4] - 0.5 * poDS->m_gt[5]);
2122 :
2123 : /* Use bottom right pixel to test geotransform */
2124 0 : const double br_x = CPLStrtod(
2125 : CPLGetXMLValue(psPos, "lowerRightCorner.mapCoordinate.easting",
2126 : "0.0"),
2127 : nullptr);
2128 0 : const double br_y = CPLStrtod(
2129 : CPLGetXMLValue(psPos, "lowerRightCorner.mapCoordinate.northing",
2130 : "0.0"),
2131 : nullptr);
2132 0 : const double testx = poDS->m_gt[0] +
2133 0 : poDS->m_gt[1] * (poDS->nRasterXSize - 0.5) +
2134 0 : poDS->m_gt[2] * (poDS->nRasterYSize - 0.5);
2135 0 : const double testy = poDS->m_gt[3] +
2136 0 : poDS->m_gt[4] * (poDS->nRasterXSize - 0.5) +
2137 0 : poDS->m_gt[5] * (poDS->nRasterYSize - 0.5);
2138 :
2139 : /* Give 1/4 pixel numerical error leeway in calculating location
2140 : based on affine transform */
2141 0 : if ((fabs(testx - br_x) >
2142 0 : fabs(0.25 * (poDS->m_gt[1] + poDS->m_gt[2]))) ||
2143 0 : (fabs(testy - br_y) >
2144 0 : fabs(0.25 * (poDS->m_gt[4] + poDS->m_gt[5]))))
2145 : {
2146 0 : CPLError(CE_Warning, CPLE_AppDefined,
2147 : "WARNING: Unexpected error in calculating affine "
2148 : "transform: corner coordinates inconsistent.");
2149 : }
2150 : else
2151 : {
2152 0 : poDS->bHaveGeoTransform = TRUE;
2153 : }
2154 : }
2155 : }
2156 :
2157 : /* -------------------------------------------------------------------- */
2158 : /* Collect Projection String Information.DONE */
2159 : /* -------------------------------------------------------------------- */
2160 : const CPLXMLNode *psEllipsoid =
2161 8 : CPLGetXMLNode(psImageReferenceAttributes,
2162 : "geographicInformation.ellipsoidParameters");
2163 :
2164 8 : if (psEllipsoid != nullptr)
2165 : {
2166 16 : OGRSpatialReference oLL, oPrj;
2167 :
2168 : const char *pszGeodeticTerrainHeight =
2169 8 : CPLGetXMLValue(psEllipsoid, "geodeticTerrainHeight", "UNK");
2170 8 : poDS->SetMetadataItem("GEODETIC_TERRAIN_HEIGHT",
2171 8 : pszGeodeticTerrainHeight);
2172 :
2173 : const char *pszEllipsoidName =
2174 8 : CPLGetXMLValue(psEllipsoid, "ellipsoidName", "");
2175 : double minor_axis =
2176 8 : CPLAtof(CPLGetXMLValue(psEllipsoid, "semiMinorAxis", "0.0"));
2177 : double major_axis =
2178 8 : CPLAtof(CPLGetXMLValue(psEllipsoid, "semiMajorAxis", "0.0"));
2179 :
2180 8 : if (EQUAL(pszEllipsoidName, "") || (minor_axis == 0.0) ||
2181 : (major_axis == 0.0))
2182 : {
2183 0 : oLL.SetWellKnownGeogCS("WGS84");
2184 0 : oPrj.SetWellKnownGeogCS("WGS84");
2185 :
2186 0 : CPLError(CE_Warning, CPLE_AppDefined,
2187 : "WARNING: Incomplete ellipsoid "
2188 : "information. Using wgs-84 parameters.");
2189 : }
2190 8 : else if (EQUAL(pszEllipsoidName, "WGS84") ||
2191 8 : EQUAL(pszEllipsoidName, "WGS 1984"))
2192 : {
2193 8 : oLL.SetWellKnownGeogCS("WGS84");
2194 8 : oPrj.SetWellKnownGeogCS("WGS84");
2195 : }
2196 : else
2197 : {
2198 0 : const double inv_flattening =
2199 0 : major_axis / (major_axis - minor_axis);
2200 0 : oLL.SetGeogCS("", "", pszEllipsoidName, major_axis, inv_flattening);
2201 0 : oPrj.SetGeogCS("", "", pszEllipsoidName, major_axis,
2202 : inv_flattening);
2203 : }
2204 :
2205 8 : if (psMapProjection != nullptr)
2206 : {
2207 : const char *pszProj =
2208 0 : CPLGetXMLValue(psMapProjection, "mapProjectionDescriptor", "");
2209 0 : bool bUseProjInfo = false;
2210 :
2211 : const CPLXMLNode *psUtmParams =
2212 0 : CPLGetXMLNode(psMapProjection, "utmProjectionParameters");
2213 :
2214 : const CPLXMLNode *psNspParams =
2215 0 : CPLGetXMLNode(psMapProjection, "nspProjectionParameters");
2216 :
2217 0 : if ((psUtmParams != nullptr) && poDS->bHaveGeoTransform)
2218 : {
2219 : /* double origEasting, origNorthing; */
2220 0 : bool bNorth = true;
2221 :
2222 : const int utmZone =
2223 0 : atoi(CPLGetXMLValue(psUtmParams, "utmZone", ""));
2224 : const char *pszHemisphere =
2225 0 : CPLGetXMLValue(psUtmParams, "hemisphere", "");
2226 : #if 0
2227 : origEasting = CPLStrtod(CPLGetXMLValue(
2228 : psUtmParams, "mapOriginFalseEasting", "0.0"), nullptr);
2229 : origNorthing = CPLStrtod(CPLGetXMLValue(
2230 : psUtmParams, "mapOriginFalseNorthing", "0.0"), nullptr);
2231 : #endif
2232 0 : if (STARTS_WITH_CI(pszHemisphere, "southern"))
2233 0 : bNorth = FALSE;
2234 :
2235 0 : if (STARTS_WITH_CI(pszProj, "UTM"))
2236 : {
2237 0 : oPrj.SetUTM(utmZone, bNorth);
2238 0 : bUseProjInfo = true;
2239 : }
2240 : }
2241 0 : else if ((psNspParams != nullptr) && poDS->bHaveGeoTransform)
2242 : {
2243 0 : const double origEasting = CPLStrtod(
2244 : CPLGetXMLValue(psNspParams, "mapOriginFalseEasting", "0.0"),
2245 : nullptr);
2246 : const double origNorthing =
2247 0 : CPLStrtod(CPLGetXMLValue(psNspParams,
2248 : "mapOriginFalseNorthing", "0.0"),
2249 : nullptr);
2250 0 : const double copLong = CPLStrtod(
2251 : CPLGetXMLValue(psNspParams, "centerOfProjectionLongitude",
2252 : "0.0"),
2253 : nullptr);
2254 0 : const double copLat = CPLStrtod(
2255 : CPLGetXMLValue(psNspParams, "centerOfProjectionLatitude",
2256 : "0.0"),
2257 : nullptr);
2258 0 : const double sP1 = CPLStrtod(
2259 : CPLGetXMLValue(psNspParams, "standardParallels1", "0.0"),
2260 : nullptr);
2261 0 : const double sP2 = CPLStrtod(
2262 : CPLGetXMLValue(psNspParams, "standardParallels2", "0.0"),
2263 : nullptr);
2264 :
2265 0 : if (STARTS_WITH_CI(pszProj, "ARC"))
2266 : {
2267 : /* Albers Conical Equal Area */
2268 0 : oPrj.SetACEA(sP1, sP2, copLat, copLong, origEasting,
2269 : origNorthing);
2270 0 : bUseProjInfo = true;
2271 : }
2272 0 : else if (STARTS_WITH_CI(pszProj, "LCC"))
2273 : {
2274 : /* Lambert Conformal Conic */
2275 0 : oPrj.SetLCC(sP1, sP2, copLat, copLong, origEasting,
2276 : origNorthing);
2277 0 : bUseProjInfo = true;
2278 : }
2279 0 : else if (STARTS_WITH_CI(pszProj, "STPL"))
2280 : {
2281 : /* State Plate
2282 : ASSUMPTIONS: "zone" in product.xml matches USGS
2283 : definition as expected by ogr spatial reference; NAD83
2284 : zones (versus NAD27) are assumed. */
2285 :
2286 : const int nSPZone =
2287 0 : atoi(CPLGetXMLValue(psNspParams, "zone", "1"));
2288 :
2289 0 : oPrj.SetStatePlane(nSPZone, TRUE, nullptr, 0.0);
2290 0 : bUseProjInfo = true;
2291 : }
2292 : }
2293 :
2294 0 : if (bUseProjInfo)
2295 : {
2296 0 : poDS->m_oSRS = std::move(oPrj);
2297 : }
2298 : else
2299 : {
2300 0 : CPLError(CE_Warning, CPLE_AppDefined,
2301 : "WARNING: Unable to interpret projection information; "
2302 : "check mapProjection info in product.xml!");
2303 : }
2304 : }
2305 :
2306 8 : poDS->m_oGCPSRS = std::move(oLL);
2307 : }
2308 :
2309 : /* -------------------------------------------------------------------- */
2310 : /* Collect GCPs.DONE */
2311 : /* -------------------------------------------------------------------- */
2312 8 : const CPLXMLNode *psGeoGrid = CPLGetXMLNode(
2313 : psImageReferenceAttributes, "geographicInformation.geolocationGrid");
2314 :
2315 8 : if (psGeoGrid != nullptr)
2316 : {
2317 : /* count GCPs */
2318 8 : poDS->nGCPCount = 0;
2319 :
2320 136 : for (const CPLXMLNode *psNode = psGeoGrid->psChild; psNode != nullptr;
2321 128 : psNode = psNode->psNext)
2322 : {
2323 128 : if (EQUAL(psNode->pszValue, "imageTiePoint"))
2324 128 : poDS->nGCPCount++;
2325 : }
2326 :
2327 16 : poDS->pasGCPList = reinterpret_cast<GDAL_GCP *>(
2328 8 : CPLCalloc(sizeof(GDAL_GCP), poDS->nGCPCount));
2329 :
2330 8 : poDS->nGCPCount = 0;
2331 :
2332 136 : for (const CPLXMLNode *psNode = psGeoGrid->psChild; psNode != nullptr;
2333 128 : psNode = psNode->psNext)
2334 : {
2335 128 : GDAL_GCP *psGCP = poDS->pasGCPList + poDS->nGCPCount;
2336 :
2337 128 : if (!EQUAL(psNode->pszValue, "imageTiePoint"))
2338 0 : continue;
2339 :
2340 128 : poDS->nGCPCount++;
2341 :
2342 : char szID[32];
2343 128 : snprintf(szID, sizeof(szID), "%d", poDS->nGCPCount);
2344 128 : psGCP->pszId = CPLStrdup(szID);
2345 128 : psGCP->pszInfo = CPLStrdup("");
2346 128 : psGCP->dfGCPPixel =
2347 128 : CPLAtof(CPLGetXMLValue(psNode, "imageCoordinate.pixel", "0"));
2348 128 : psGCP->dfGCPLine =
2349 128 : CPLAtof(CPLGetXMLValue(psNode, "imageCoordinate.line", "0"));
2350 128 : psGCP->dfGCPX = CPLAtof(
2351 : CPLGetXMLValue(psNode, "geodeticCoordinate.longitude", ""));
2352 128 : psGCP->dfGCPY = CPLAtof(
2353 : CPLGetXMLValue(psNode, "geodeticCoordinate.latitude", ""));
2354 128 : psGCP->dfGCPZ = CPLAtof(
2355 : CPLGetXMLValue(psNode, "geodeticCoordinate.height", ""));
2356 : }
2357 : }
2358 :
2359 : /* -------------------------------------------------------------------- */
2360 : /* Collect RPC.DONE */
2361 : /* -------------------------------------------------------------------- */
2362 8 : const CPLXMLNode *psRationalFunctions = CPLGetXMLNode(
2363 : psImageReferenceAttributes, "geographicInformation.rationalFunctions");
2364 8 : if (psRationalFunctions != nullptr)
2365 : {
2366 8 : char **papszRPC = nullptr;
2367 : static const char *const apszXMLToGDALMapping[] = {
2368 : "biasError",
2369 : "ERR_BIAS",
2370 : "randomError",
2371 : "ERR_RAND",
2372 : //"lineFitQuality", "????",
2373 : //"pixelFitQuality", "????",
2374 : "lineOffset",
2375 : "LINE_OFF",
2376 : "pixelOffset",
2377 : "SAMP_OFF",
2378 : "latitudeOffset",
2379 : "LAT_OFF",
2380 : "longitudeOffset",
2381 : "LONG_OFF",
2382 : "heightOffset",
2383 : "HEIGHT_OFF",
2384 : "lineScale",
2385 : "LINE_SCALE",
2386 : "pixelScale",
2387 : "SAMP_SCALE",
2388 : "latitudeScale",
2389 : "LAT_SCALE",
2390 : "longitudeScale",
2391 : "LONG_SCALE",
2392 : "heightScale",
2393 : "HEIGHT_SCALE",
2394 : "lineNumeratorCoefficients",
2395 : "LINE_NUM_COEFF",
2396 : "lineDenominatorCoefficients",
2397 : "LINE_DEN_COEFF",
2398 : "pixelNumeratorCoefficients",
2399 : "SAMP_NUM_COEFF",
2400 : "pixelDenominatorCoefficients",
2401 : "SAMP_DEN_COEFF",
2402 : };
2403 136 : for (size_t i = 0; i < CPL_ARRAYSIZE(apszXMLToGDALMapping); i += 2)
2404 : {
2405 256 : const char *pszValue = CPLGetXMLValue(
2406 128 : psRationalFunctions, apszXMLToGDALMapping[i], nullptr);
2407 128 : if (pszValue)
2408 128 : papszRPC = CSLSetNameValue(
2409 128 : papszRPC, apszXMLToGDALMapping[i + 1], pszValue);
2410 : }
2411 8 : poDS->GDALDataset::SetMetadata(papszRPC, "RPC");
2412 8 : CSLDestroy(papszRPC);
2413 : }
2414 :
2415 : /* -------------------------------------------------------------------- */
2416 : /* Initialize any PAM information. */
2417 : /* -------------------------------------------------------------------- */
2418 16 : CPLString osDescription;
2419 16 : CPLString osSubdatasetName;
2420 8 : bool useSubdatasets = true;
2421 :
2422 8 : switch (eCalib)
2423 : {
2424 1 : case Sigma0:
2425 : {
2426 1 : osSubdatasetName = szSIGMA0;
2427 1 : osDescription = FormatCalibration(szSIGMA0, osMDFilename.c_str());
2428 : }
2429 1 : break;
2430 1 : case Beta0:
2431 : {
2432 1 : osSubdatasetName = szBETA0;
2433 1 : osDescription = FormatCalibration(szBETA0, osMDFilename.c_str());
2434 : }
2435 1 : break;
2436 1 : case Gamma:
2437 : {
2438 1 : osSubdatasetName = szGAMMA;
2439 1 : osDescription = FormatCalibration(szGAMMA, osMDFilename.c_str());
2440 : }
2441 1 : break;
2442 1 : case Uncalib:
2443 : {
2444 1 : osSubdatasetName = szUNCALIB;
2445 1 : osDescription = FormatCalibration(szUNCALIB, osMDFilename.c_str());
2446 : }
2447 1 : break;
2448 4 : default:
2449 4 : osSubdatasetName = szUNCALIB;
2450 4 : osDescription = osMDFilename;
2451 4 : useSubdatasets = false;
2452 : }
2453 :
2454 8 : CPL_IGNORE_RET_VAL(osSubdatasetName);
2455 :
2456 8 : if (eCalib != None)
2457 4 : poDS->papszExtraFiles =
2458 4 : CSLAddString(poDS->papszExtraFiles, osMDFilename);
2459 :
2460 : /* -------------------------------------------------------------------- */
2461 : /* Initialize any PAM information. */
2462 : /* -------------------------------------------------------------------- */
2463 8 : poDS->SetDescription(osDescription);
2464 :
2465 8 : poDS->SetPhysicalFilename(osMDFilename);
2466 8 : poDS->SetSubdatasetName(osDescription);
2467 :
2468 8 : poDS->TryLoadXML();
2469 :
2470 : /* -------------------------------------------------------------------- */
2471 : /* Check for overviews. */
2472 : /* -------------------------------------------------------------------- */
2473 8 : if (useSubdatasets)
2474 4 : poDS->oOvManager.Initialize(poDS.get(), ":::VIRTUAL:::");
2475 : else
2476 4 : poDS->oOvManager.Initialize(poDS.get(), osMDFilename);
2477 :
2478 8 : return poDS.release();
2479 : }
2480 :
2481 : /************************************************************************/
2482 : /* GetGCPCount() */
2483 : /************************************************************************/
2484 :
2485 6 : int RCMDataset::GetGCPCount()
2486 :
2487 : {
2488 6 : return nGCPCount;
2489 : }
2490 :
2491 : /************************************************************************/
2492 : /* GetGCPSpatialRef() */
2493 : /************************************************************************/
2494 :
2495 1 : const OGRSpatialReference *RCMDataset::GetGCPSpatialRef() const
2496 :
2497 : {
2498 1 : return m_oGCPSRS.IsEmpty() || nGCPCount == 0 ? nullptr : &m_oGCPSRS;
2499 : }
2500 :
2501 : /************************************************************************/
2502 : /* GetGCPs() */
2503 : /************************************************************************/
2504 :
2505 5 : const GDAL_GCP *RCMDataset::GetGCPs()
2506 :
2507 : {
2508 5 : return pasGCPList;
2509 : }
2510 :
2511 : /************************************************************************/
2512 : /* GetSpatialRef() */
2513 : /************************************************************************/
2514 :
2515 0 : const OGRSpatialReference *RCMDataset::GetSpatialRef() const
2516 :
2517 : {
2518 0 : return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
2519 : }
2520 :
2521 : /************************************************************************/
2522 : /* GetGeoTransform() */
2523 : /************************************************************************/
2524 :
2525 0 : CPLErr RCMDataset::GetGeoTransform(GDALGeoTransform >) const
2526 :
2527 : {
2528 0 : gt = m_gt;
2529 :
2530 0 : if (bHaveGeoTransform)
2531 : {
2532 0 : return CE_None;
2533 : }
2534 :
2535 0 : return CE_Failure;
2536 : }
2537 :
2538 : /************************************************************************/
2539 : /* GetMetadataDomainList() */
2540 : /************************************************************************/
2541 :
2542 0 : char **RCMDataset::GetMetadataDomainList()
2543 : {
2544 0 : return BuildMetadataDomainList(GDALDataset::GetMetadataDomainList(), TRUE,
2545 0 : "SUBDATASETS", nullptr);
2546 : }
2547 :
2548 : /************************************************************************/
2549 : /* GetMetadata() */
2550 : /************************************************************************/
2551 :
2552 2 : char **RCMDataset::GetMetadata(const char *pszDomain)
2553 :
2554 : {
2555 2 : if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "SUBDATASETS") &&
2556 0 : papszSubDatasets != nullptr)
2557 0 : return papszSubDatasets;
2558 :
2559 2 : return GDALDataset::GetMetadata(pszDomain);
2560 : }
2561 :
2562 : /************************************************************************/
2563 : /* GDALRegister_RCM() */
2564 : /************************************************************************/
2565 :
2566 2024 : void GDALRegister_RCM()
2567 :
2568 : {
2569 2024 : if (GDALGetDriverByName("RCM") != nullptr)
2570 : {
2571 283 : return;
2572 : }
2573 :
2574 1741 : GDALDriver *poDriver = new GDALDriver();
2575 1741 : RCMDriverSetCommonMetadata(poDriver);
2576 :
2577 1741 : poDriver->pfnOpen = RCMDataset::Open;
2578 :
2579 1741 : GetGDALDriverManager()->RegisterDriver(poDriver);
2580 : }
|