Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: PDS 4 Driver; Planetary Data System Format
4 : * Purpose: Implementation of PDS4Dataset
5 : * Author: Even Rouault, even.rouault at spatialys.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2017, Hobu Inc
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "cpl_vsi_error.h"
14 : #include "gdal_proxy.h"
15 : #include "gdal_frmts.h"
16 : #include "rawdataset.h"
17 : #include "vrtdataset.h"
18 : #include "ogrsf_frmts.h"
19 : #include "ogr_spatialref.h"
20 : #include "gdal_priv_templates.hpp"
21 : #include "ogreditablelayer.h"
22 : #include "pds4dataset.h"
23 : #include "pdsdrivercore.h"
24 :
25 : #ifdef EMBED_RESOURCE_FILES
26 : #include "embedded_resources.h"
27 : #endif
28 :
29 : #include <cstdlib>
30 : #include <vector>
31 : #include <algorithm>
32 : #include <optional>
33 :
34 : #define TIFF_GEOTIFF_STRING "TIFF 6.0"
35 : #define BIGTIFF_GEOTIFF_STRING "TIFF 6.0"
36 : #define PREEXISTING_BINARY_FILE \
37 : "Binary file pre-existing PDS4 label. This comment is used by GDAL to " \
38 : "avoid deleting the binary file when the label is deleted. Keep it to " \
39 : "preserve this behavior."
40 :
41 : #define CURRENT_CART_VERSION "1O00_1970"
42 :
43 : /************************************************************************/
44 : /* PDS4WrapperRasterBand() */
45 : /************************************************************************/
46 :
47 24 : PDS4WrapperRasterBand::PDS4WrapperRasterBand(GDALRasterBand *poBaseBandIn)
48 24 : : m_poBaseBand(poBaseBandIn)
49 : {
50 24 : eDataType = m_poBaseBand->GetRasterDataType();
51 24 : m_poBaseBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
52 24 : }
53 :
54 : /************************************************************************/
55 : /* SetMaskBand() */
56 : /************************************************************************/
57 :
58 0 : void PDS4WrapperRasterBand::SetMaskBand(
59 : std::unique_ptr<GDALRasterBand> poMaskBand)
60 : {
61 0 : poMask.reset(std::move(poMaskBand));
62 0 : nMaskFlags = 0;
63 0 : }
64 :
65 : /************************************************************************/
66 : /* GetOffset() */
67 : /************************************************************************/
68 :
69 18 : double PDS4WrapperRasterBand::GetOffset(int *pbSuccess)
70 : {
71 18 : if (pbSuccess)
72 18 : *pbSuccess = m_bHasOffset;
73 18 : return m_dfOffset;
74 : }
75 :
76 : /************************************************************************/
77 : /* GetScale() */
78 : /************************************************************************/
79 :
80 18 : double PDS4WrapperRasterBand::GetScale(int *pbSuccess)
81 : {
82 18 : if (pbSuccess)
83 18 : *pbSuccess = m_bHasScale;
84 18 : return m_dfScale;
85 : }
86 :
87 : /************************************************************************/
88 : /* SetOffset() */
89 : /************************************************************************/
90 :
91 1 : CPLErr PDS4WrapperRasterBand::SetOffset(double dfNewOffset)
92 : {
93 1 : m_dfOffset = dfNewOffset;
94 1 : m_bHasOffset = true;
95 :
96 1 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
97 1 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
98 1 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetOffset(dfNewOffset);
99 :
100 1 : return CE_None;
101 : }
102 :
103 : /************************************************************************/
104 : /* SetScale() */
105 : /************************************************************************/
106 :
107 1 : CPLErr PDS4WrapperRasterBand::SetScale(double dfNewScale)
108 : {
109 1 : m_dfScale = dfNewScale;
110 1 : m_bHasScale = true;
111 :
112 1 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
113 1 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
114 1 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetScale(dfNewScale);
115 :
116 1 : return CE_None;
117 : }
118 :
119 : /************************************************************************/
120 : /* GetNoDataValue() */
121 : /************************************************************************/
122 :
123 35 : double PDS4WrapperRasterBand::GetNoDataValue(int *pbSuccess)
124 : {
125 35 : if (m_bHasNoDataInt64)
126 : {
127 0 : if (pbSuccess)
128 0 : *pbSuccess = true;
129 0 : return GDALGetNoDataValueCastToDouble(m_nNoDataInt64);
130 : }
131 :
132 35 : if (m_bHasNoDataUInt64)
133 : {
134 0 : if (pbSuccess)
135 0 : *pbSuccess = true;
136 0 : return GDALGetNoDataValueCastToDouble(m_nNoDataUInt64);
137 : }
138 :
139 35 : if (pbSuccess)
140 35 : *pbSuccess = m_bHasNoData;
141 35 : return m_dfNoData;
142 : }
143 :
144 : /************************************************************************/
145 : /* SetNoDataValue() */
146 : /************************************************************************/
147 :
148 9 : CPLErr PDS4WrapperRasterBand::SetNoDataValue(double dfNewNoData)
149 : {
150 9 : m_dfNoData = dfNewNoData;
151 9 : m_bHasNoData = true;
152 :
153 9 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
154 9 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
155 9 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetNoDataValue(
156 9 : dfNewNoData);
157 :
158 9 : return CE_None;
159 : }
160 :
161 : /************************************************************************/
162 : /* GetNoDataValueAsInt64() */
163 : /************************************************************************/
164 :
165 3 : int64_t PDS4WrapperRasterBand::GetNoDataValueAsInt64(int *pbSuccess)
166 : {
167 3 : if (pbSuccess)
168 3 : *pbSuccess = m_bHasNoDataInt64;
169 3 : return m_nNoDataInt64;
170 : }
171 :
172 : /************************************************************************/
173 : /* SetNoDataValueAsInt64() */
174 : /************************************************************************/
175 :
176 1 : CPLErr PDS4WrapperRasterBand::SetNoDataValueAsInt64(int64_t nNewNoData)
177 : {
178 1 : m_nNoDataInt64 = nNewNoData;
179 1 : m_bHasNoDataInt64 = true;
180 :
181 1 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
182 1 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
183 1 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetNoDataValueAsInt64(
184 1 : nNewNoData);
185 :
186 1 : return CE_None;
187 : }
188 :
189 : /************************************************************************/
190 : /* GetNoDataValueAsUInt64() */
191 : /************************************************************************/
192 :
193 3 : uint64_t PDS4WrapperRasterBand::GetNoDataValueAsUInt64(int *pbSuccess)
194 : {
195 3 : if (pbSuccess)
196 3 : *pbSuccess = m_bHasNoDataUInt64;
197 3 : return m_nNoDataUInt64;
198 : }
199 :
200 : /************************************************************************/
201 : /* SetNoDataValueAsUInt64() */
202 : /************************************************************************/
203 :
204 1 : CPLErr PDS4WrapperRasterBand::SetNoDataValueAsUInt64(uint64_t nNewNoData)
205 : {
206 1 : m_nNoDataUInt64 = nNewNoData;
207 1 : m_bHasNoDataUInt64 = true;
208 :
209 1 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
210 1 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
211 1 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetNoDataValueAsUInt64(
212 1 : nNewNoData);
213 :
214 1 : return CE_None;
215 : }
216 :
217 : /************************************************************************/
218 : /* Fill() */
219 : /************************************************************************/
220 :
221 2 : CPLErr PDS4WrapperRasterBand::Fill(double dfRealValue, double dfImaginaryValue)
222 : {
223 2 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
224 2 : if (poGDS->m_bMustInitImageFile)
225 : {
226 2 : if (!poGDS->InitImageFile())
227 0 : return CE_Failure;
228 : }
229 2 : return GDALProxyRasterBand::Fill(dfRealValue, dfImaginaryValue);
230 : }
231 :
232 : /************************************************************************/
233 : /* IWriteBlock() */
234 : /************************************************************************/
235 :
236 0 : CPLErr PDS4WrapperRasterBand::IWriteBlock(int nXBlock, int nYBlock,
237 : void *pImage)
238 :
239 : {
240 0 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
241 0 : if (poGDS->m_bMustInitImageFile)
242 : {
243 0 : if (!poGDS->InitImageFile())
244 0 : return CE_Failure;
245 : }
246 0 : return GDALProxyRasterBand::IWriteBlock(nXBlock, nYBlock, pImage);
247 : }
248 :
249 : /************************************************************************/
250 : /* IRasterIO() */
251 : /************************************************************************/
252 :
253 115 : CPLErr PDS4WrapperRasterBand::IRasterIO(
254 : GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
255 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
256 : GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg)
257 :
258 : {
259 115 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
260 115 : if (eRWFlag == GF_Write && poGDS->m_bMustInitImageFile)
261 : {
262 9 : if (!poGDS->InitImageFile())
263 0 : return CE_Failure;
264 : }
265 115 : return GDALProxyRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
266 : pData, nBufXSize, nBufYSize, eBufType,
267 115 : nPixelSpace, nLineSpace, psExtraArg);
268 : }
269 :
270 : /************************************************************************/
271 : /* PDS4RawRasterBand() */
272 : /************************************************************************/
273 :
274 460 : PDS4RawRasterBand::PDS4RawRasterBand(GDALDataset *l_poDS, int l_nBand,
275 : VSILFILE *l_fpRaw,
276 : vsi_l_offset l_nImgOffset,
277 : int l_nPixelOffset, int l_nLineOffset,
278 : GDALDataType l_eDataType,
279 460 : RawRasterBand::ByteOrder eByteOrderIn)
280 : : RawRasterBand(l_poDS, l_nBand, l_fpRaw, l_nImgOffset, l_nPixelOffset,
281 : l_nLineOffset, l_eDataType, eByteOrderIn,
282 460 : RawRasterBand::OwnFP::NO)
283 : {
284 460 : }
285 :
286 : /************************************************************************/
287 : /* SetMaskBand() */
288 : /************************************************************************/
289 :
290 43 : void PDS4RawRasterBand::SetMaskBand(std::unique_ptr<GDALRasterBand> poMaskBand)
291 : {
292 43 : poMask.reset(std::move(poMaskBand));
293 43 : nMaskFlags = 0;
294 43 : }
295 :
296 : /************************************************************************/
297 : /* GetOffset() */
298 : /************************************************************************/
299 :
300 127 : double PDS4RawRasterBand::GetOffset(int *pbSuccess)
301 : {
302 127 : if (pbSuccess)
303 119 : *pbSuccess = m_bHasOffset;
304 127 : return m_dfOffset;
305 : }
306 :
307 : /************************************************************************/
308 : /* GetScale() */
309 : /************************************************************************/
310 :
311 127 : double PDS4RawRasterBand::GetScale(int *pbSuccess)
312 : {
313 127 : if (pbSuccess)
314 119 : *pbSuccess = m_bHasScale;
315 127 : return m_dfScale;
316 : }
317 :
318 : /************************************************************************/
319 : /* SetOffset() */
320 : /************************************************************************/
321 :
322 289 : CPLErr PDS4RawRasterBand::SetOffset(double dfNewOffset)
323 : {
324 289 : m_dfOffset = dfNewOffset;
325 289 : m_bHasOffset = true;
326 289 : return CE_None;
327 : }
328 :
329 : /************************************************************************/
330 : /* SetScale() */
331 : /************************************************************************/
332 :
333 289 : CPLErr PDS4RawRasterBand::SetScale(double dfNewScale)
334 : {
335 289 : m_dfScale = dfNewScale;
336 289 : m_bHasScale = true;
337 289 : return CE_None;
338 : }
339 :
340 : /************************************************************************/
341 : /* GetNoDataValue() */
342 : /************************************************************************/
343 :
344 230 : double PDS4RawRasterBand::GetNoDataValue(int *pbSuccess)
345 : {
346 230 : if (m_bHasNoDataInt64)
347 : {
348 0 : if (pbSuccess)
349 0 : *pbSuccess = true;
350 0 : return GDALGetNoDataValueCastToDouble(m_nNoDataInt64);
351 : }
352 :
353 230 : if (m_bHasNoDataUInt64)
354 : {
355 0 : if (pbSuccess)
356 0 : *pbSuccess = true;
357 0 : return GDALGetNoDataValueCastToDouble(m_nNoDataUInt64);
358 : }
359 :
360 230 : if (pbSuccess)
361 230 : *pbSuccess = m_bHasNoData;
362 230 : return m_dfNoData;
363 : }
364 :
365 : /************************************************************************/
366 : /* SetNoDataValue() */
367 : /************************************************************************/
368 :
369 92 : CPLErr PDS4RawRasterBand::SetNoDataValue(double dfNewNoData)
370 : {
371 92 : m_dfNoData = dfNewNoData;
372 92 : m_bHasNoData = true;
373 92 : return CE_None;
374 : }
375 :
376 : /************************************************************************/
377 : /* GetNoDataValueAsInt64() */
378 : /************************************************************************/
379 :
380 9 : int64_t PDS4RawRasterBand::GetNoDataValueAsInt64(int *pbSuccess)
381 : {
382 9 : if (pbSuccess)
383 9 : *pbSuccess = m_bHasNoDataInt64;
384 9 : return m_nNoDataInt64;
385 : }
386 :
387 : /************************************************************************/
388 : /* SetNoDataValueAsInt64() */
389 : /************************************************************************/
390 :
391 3 : CPLErr PDS4RawRasterBand::SetNoDataValueAsInt64(int64_t nNewNoData)
392 : {
393 3 : m_nNoDataInt64 = nNewNoData;
394 3 : m_bHasNoDataInt64 = true;
395 :
396 3 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
397 3 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
398 0 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetNoDataValueAsInt64(
399 0 : nNewNoData);
400 :
401 3 : return CE_None;
402 : }
403 :
404 : /************************************************************************/
405 : /* GetNoDataValueAsUInt64() */
406 : /************************************************************************/
407 :
408 9 : uint64_t PDS4RawRasterBand::GetNoDataValueAsUInt64(int *pbSuccess)
409 : {
410 9 : if (pbSuccess)
411 9 : *pbSuccess = m_bHasNoDataUInt64;
412 9 : return m_nNoDataUInt64;
413 : }
414 :
415 : /************************************************************************/
416 : /* SetNoDataValueAsUInt64() */
417 : /************************************************************************/
418 :
419 3 : CPLErr PDS4RawRasterBand::SetNoDataValueAsUInt64(uint64_t nNewNoData)
420 : {
421 3 : m_nNoDataUInt64 = nNewNoData;
422 3 : m_bHasNoDataUInt64 = true;
423 :
424 3 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
425 3 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
426 0 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetNoDataValueAsUInt64(
427 0 : nNewNoData);
428 :
429 3 : return CE_None;
430 : }
431 :
432 : /************************************************************************/
433 : /* IReadBlock() */
434 : /************************************************************************/
435 :
436 132 : CPLErr PDS4RawRasterBand::IWriteBlock(int nXBlock, int nYBlock, void *pImage)
437 :
438 : {
439 132 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
440 132 : if (poGDS->m_bMustInitImageFile)
441 : {
442 0 : if (!poGDS->InitImageFile())
443 0 : return CE_Failure;
444 : }
445 :
446 132 : return RawRasterBand::IWriteBlock(nXBlock, nYBlock, pImage);
447 : }
448 :
449 : /************************************************************************/
450 : /* IRasterIO() */
451 : /************************************************************************/
452 :
453 1106 : CPLErr PDS4RawRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
454 : int nXSize, int nYSize, void *pData,
455 : int nBufXSize, int nBufYSize,
456 : GDALDataType eBufType, GSpacing nPixelSpace,
457 : GSpacing nLineSpace,
458 : GDALRasterIOExtraArg *psExtraArg)
459 :
460 : {
461 1106 : PDS4Dataset *poGDS = cpl::down_cast<PDS4Dataset *>(poDS);
462 1106 : if (eRWFlag == GF_Write && poGDS->m_bMustInitImageFile)
463 : {
464 10 : if (!poGDS->InitImageFile())
465 0 : return CE_Failure;
466 : }
467 :
468 1106 : return RawRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
469 : pData, nBufXSize, nBufYSize, eBufType,
470 1106 : nPixelSpace, nLineSpace, psExtraArg);
471 : }
472 :
473 : /************************************************************************/
474 : /* PDS4MaskBand() */
475 : /************************************************************************/
476 :
477 43 : PDS4MaskBand::PDS4MaskBand(GDALRasterBand *poBaseBand,
478 43 : const std::vector<double> &adfConstants)
479 43 : : m_poBaseBand(poBaseBand), m_pBuffer(nullptr), m_adfConstants(adfConstants)
480 : {
481 43 : eDataType = GDT_UInt8;
482 43 : poBaseBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
483 43 : nRasterXSize = poBaseBand->GetXSize();
484 43 : nRasterYSize = poBaseBand->GetYSize();
485 43 : }
486 :
487 : /************************************************************************/
488 : /* ~PDS4MaskBand() */
489 : /************************************************************************/
490 :
491 86 : PDS4MaskBand::~PDS4MaskBand()
492 : {
493 43 : VSIFree(m_pBuffer);
494 86 : }
495 :
496 : /************************************************************************/
497 : /* FillMask() */
498 : /************************************************************************/
499 :
500 : template <class T>
501 88 : static void FillMask(void *pvBuffer, GByte *pabyDst, int nReqXSize,
502 : int nReqYSize, int nBlockXSize,
503 : const std::vector<double> &adfConstants)
504 : {
505 88 : const T *pSrc = static_cast<T *>(pvBuffer);
506 176 : std::vector<T> aConstants;
507 248 : for (size_t i = 0; i < adfConstants.size(); i++)
508 : {
509 : T cst;
510 160 : GDALCopyWord(adfConstants[i], cst);
511 160 : aConstants.push_back(cst);
512 : }
513 :
514 176 : for (int y = 0; y < nReqYSize; y++)
515 : {
516 1696 : for (int x = 0; x < nReqXSize; x++)
517 : {
518 1608 : const T nSrc = pSrc[y * nBlockXSize + x];
519 1608 : if (std::find(aConstants.begin(), aConstants.end(), nSrc) !=
520 : aConstants.end())
521 : {
522 6 : pabyDst[y * nBlockXSize + x] = 0;
523 : }
524 : else
525 : {
526 1602 : pabyDst[y * nBlockXSize + x] = 255;
527 : }
528 : }
529 : }
530 88 : }
531 :
532 : /************************************************************************/
533 : /* IReadBlock() */
534 : /************************************************************************/
535 :
536 88 : CPLErr PDS4MaskBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
537 :
538 : {
539 88 : const GDALDataType eSrcDT = m_poBaseBand->GetRasterDataType();
540 88 : const int nSrcDTSize = GDALGetDataTypeSizeBytes(eSrcDT);
541 88 : if (m_pBuffer == nullptr)
542 : {
543 12 : m_pBuffer = VSI_MALLOC3_VERBOSE(nBlockXSize, nBlockYSize, nSrcDTSize);
544 12 : if (m_pBuffer == nullptr)
545 0 : return CE_Failure;
546 : }
547 :
548 88 : const int nXOff = nBlockXOff * nBlockXSize;
549 88 : const int nReqXSize = std::min(nBlockXSize, nRasterXSize - nXOff);
550 88 : const int nYOff = nBlockYOff * nBlockYSize;
551 88 : const int nReqYSize = std::min(nBlockYSize, nRasterYSize - nYOff);
552 :
553 176 : if (m_poBaseBand->RasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize,
554 : m_pBuffer, nReqXSize, nReqYSize, eSrcDT,
555 : nSrcDTSize,
556 88 : static_cast<GSpacing>(nSrcDTSize) * nBlockXSize,
557 88 : nullptr) != CE_None)
558 : {
559 0 : return CE_Failure;
560 : }
561 :
562 88 : GByte *pabyDst = static_cast<GByte *>(pImage);
563 88 : if (eSrcDT == GDT_UInt8)
564 : {
565 81 : FillMask<GByte>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
566 81 : m_adfConstants);
567 : }
568 7 : else if (eSrcDT == GDT_Int8)
569 : {
570 1 : FillMask<GInt8>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
571 1 : m_adfConstants);
572 : }
573 6 : else if (eSrcDT == GDT_UInt16)
574 : {
575 1 : FillMask<GUInt16>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
576 1 : m_adfConstants);
577 : }
578 5 : else if (eSrcDT == GDT_Int16)
579 : {
580 1 : FillMask<GInt16>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
581 1 : m_adfConstants);
582 : }
583 4 : else if (eSrcDT == GDT_UInt32)
584 : {
585 1 : FillMask<GUInt32>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
586 1 : m_adfConstants);
587 : }
588 3 : else if (eSrcDT == GDT_Int32)
589 : {
590 1 : FillMask<GInt32>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
591 1 : m_adfConstants);
592 : }
593 2 : else if (eSrcDT == GDT_UInt64)
594 : {
595 0 : FillMask<uint64_t>(m_pBuffer, pabyDst, nReqXSize, nReqYSize,
596 0 : nBlockXSize, m_adfConstants);
597 : }
598 2 : else if (eSrcDT == GDT_Int64)
599 : {
600 0 : FillMask<int64_t>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
601 0 : m_adfConstants);
602 : }
603 2 : else if (eSrcDT == GDT_Float32)
604 : {
605 1 : FillMask<float>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
606 1 : m_adfConstants);
607 : }
608 1 : else if (eSrcDT == GDT_Float64)
609 : {
610 1 : FillMask<double>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
611 1 : m_adfConstants);
612 : }
613 :
614 88 : return CE_None;
615 : }
616 :
617 : /************************************************************************/
618 : /* PDS4Dataset() */
619 : /************************************************************************/
620 :
621 488 : PDS4Dataset::PDS4Dataset()
622 : {
623 488 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
624 488 : }
625 :
626 : /************************************************************************/
627 : /* ~PDS4Dataset() */
628 : /************************************************************************/
629 :
630 976 : PDS4Dataset::~PDS4Dataset()
631 : {
632 488 : PDS4Dataset::Close();
633 976 : }
634 :
635 : /************************************************************************/
636 : /* Close() */
637 : /************************************************************************/
638 :
639 840 : CPLErr PDS4Dataset::Close(GDALProgressFunc, void *)
640 : {
641 840 : CPLErr eErr = CE_None;
642 840 : if (nOpenFlags != OPEN_FLAGS_CLOSED)
643 : {
644 488 : if (m_bMustInitImageFile)
645 : {
646 72 : if (!InitImageFile())
647 0 : eErr = CE_Failure;
648 : }
649 :
650 488 : if (PDS4Dataset::FlushCache(true) != CE_None)
651 1 : eErr = CE_Failure;
652 :
653 488 : if (m_bCreateHeader || m_bDirtyHeader)
654 194 : WriteHeader();
655 488 : if (m_fpImage)
656 337 : VSIFCloseL(m_fpImage);
657 488 : CSLDestroy(m_papszCreationOptions);
658 488 : PDS4Dataset::CloseDependentDatasets();
659 :
660 488 : if (GDALPamDataset::Close() != CE_None)
661 0 : eErr = CE_Failure;
662 : }
663 840 : return eErr;
664 : }
665 :
666 : /************************************************************************/
667 : /* GetRawBinaryLayout() */
668 : /************************************************************************/
669 :
670 1 : bool PDS4Dataset::GetRawBinaryLayout(GDALDataset::RawBinaryLayout &sLayout)
671 : {
672 1 : if (!RawDataset::GetRawBinaryLayout(sLayout))
673 0 : return false;
674 1 : sLayout.osRawFilename = m_osImageFilename;
675 1 : return true;
676 : }
677 :
678 : /************************************************************************/
679 : /* CloseDependentDatasets() */
680 : /************************************************************************/
681 :
682 488 : int PDS4Dataset::CloseDependentDatasets()
683 : {
684 488 : int bHasDroppedRef = GDALPamDataset::CloseDependentDatasets();
685 :
686 488 : if (m_poExternalDS)
687 : {
688 18 : bHasDroppedRef = FALSE;
689 18 : delete m_poExternalDS;
690 18 : m_poExternalDS = nullptr;
691 :
692 42 : for (int iBand = 0; iBand < nBands; iBand++)
693 : {
694 24 : delete papoBands[iBand];
695 24 : papoBands[iBand] = nullptr;
696 : }
697 18 : nBands = 0;
698 : }
699 :
700 488 : return bHasDroppedRef;
701 : }
702 :
703 : /************************************************************************/
704 : /* GetSpatialRef() */
705 : /************************************************************************/
706 :
707 44 : const OGRSpatialReference *PDS4Dataset::GetSpatialRef() const
708 : {
709 44 : if (!m_oSRS.IsEmpty())
710 40 : return &m_oSRS;
711 4 : return GDALPamDataset::GetSpatialRef();
712 : }
713 :
714 : /************************************************************************/
715 : /* SetSpatialRef() */
716 : /************************************************************************/
717 :
718 101 : CPLErr PDS4Dataset::SetSpatialRef(const OGRSpatialReference *poSRS)
719 :
720 : {
721 101 : if (eAccess == GA_ReadOnly)
722 0 : return CE_Failure;
723 101 : m_oSRS.Clear();
724 101 : if (poSRS)
725 101 : m_oSRS = *poSRS;
726 101 : if (m_poExternalDS)
727 10 : m_poExternalDS->SetSpatialRef(poSRS);
728 101 : return CE_None;
729 : }
730 :
731 : /************************************************************************/
732 : /* GetGeoTransform() */
733 : /************************************************************************/
734 :
735 48 : CPLErr PDS4Dataset::GetGeoTransform(GDALGeoTransform >) const
736 :
737 : {
738 48 : if (m_bGotTransform)
739 : {
740 46 : gt = m_gt;
741 46 : return CE_None;
742 : }
743 :
744 2 : return GDALPamDataset::GetGeoTransform(gt);
745 : }
746 :
747 : /************************************************************************/
748 : /* SetGeoTransform() */
749 : /************************************************************************/
750 :
751 101 : CPLErr PDS4Dataset::SetGeoTransform(const GDALGeoTransform >)
752 :
753 : {
754 101 : if (!((gt.xscale > 0.0 && gt.xrot == 0.0 && gt.yrot == 0.0 &&
755 100 : gt.yscale < 0.0) ||
756 1 : (gt.xscale == 0.0 && gt.xrot > 0.0 && gt.yrot > 0.0 &&
757 1 : gt.yscale == 0.0)))
758 : {
759 0 : CPLError(CE_Failure, CPLE_NotSupported,
760 : "Only north-up geotransform or map_projection_rotation=90 "
761 : "supported");
762 0 : return CE_Failure;
763 : }
764 101 : m_gt = gt;
765 101 : m_bGotTransform = true;
766 101 : if (m_poExternalDS)
767 10 : m_poExternalDS->SetGeoTransform(m_gt);
768 101 : return CE_None;
769 : }
770 :
771 : /************************************************************************/
772 : /* SetMetadata() */
773 : /************************************************************************/
774 :
775 8 : CPLErr PDS4Dataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
776 : {
777 8 : if (m_bUseSrcLabel && eAccess == GA_Update && pszDomain != nullptr &&
778 8 : EQUAL(pszDomain, "xml:PDS4"))
779 : {
780 6 : if (papszMD != nullptr && papszMD[0] != nullptr)
781 : {
782 6 : m_osXMLPDS4 = papszMD[0];
783 : }
784 6 : return CE_None;
785 : }
786 2 : return GDALPamDataset::SetMetadata(papszMD, pszDomain);
787 : }
788 :
789 : /************************************************************************/
790 : /* GetFileList() */
791 : /************************************************************************/
792 :
793 137 : char **PDS4Dataset::GetFileList()
794 : {
795 137 : char **papszFileList = GDALPamDataset::GetFileList();
796 274 : if (!m_osXMLFilename.empty() &&
797 137 : CSLFindString(papszFileList, m_osXMLFilename) < 0)
798 : {
799 0 : papszFileList = CSLAddString(papszFileList, m_osXMLFilename);
800 : }
801 137 : if (!m_osImageFilename.empty())
802 : {
803 88 : papszFileList = CSLAddString(papszFileList, m_osImageFilename);
804 : }
805 201 : for (const auto &poLayer : m_apoLayers)
806 : {
807 64 : auto papszTemp = poLayer->GetFileList();
808 64 : papszFileList = CSLInsertStrings(papszFileList, -1, papszTemp);
809 64 : CSLDestroy(papszTemp);
810 : }
811 137 : return papszFileList;
812 : }
813 :
814 : /************************************************************************/
815 : /* GetLinearValue() */
816 : /************************************************************************/
817 :
818 : static const struct
819 : {
820 : const char *pszUnit;
821 : double dfToMeter;
822 : } apsLinearUnits[] = {
823 : {"AU", 149597870700.0}, {"Angstrom", 1e-10}, {"cm", 1e-2}, {"km", 1e3},
824 : {"micrometer", 1e-6}, {"mm", 1e-3}, {"nm", 1e-9}};
825 :
826 802 : static double GetLinearValue(const CPLXMLNode *psParent,
827 : const char *pszElementName)
828 : {
829 802 : const CPLXMLNode *psNode = CPLGetXMLNode(psParent, pszElementName);
830 802 : if (psNode == nullptr)
831 0 : return 0.0;
832 802 : double dfVal = CPLAtof(CPLGetXMLValue(psNode, nullptr, ""));
833 802 : const char *pszUnit = CPLGetXMLValue(psNode, "unit", nullptr);
834 802 : if (pszUnit && !EQUAL(pszUnit, "m"))
835 : {
836 21 : bool bFound = false;
837 84 : for (size_t i = 0; i < CPL_ARRAYSIZE(apsLinearUnits); i++)
838 : {
839 84 : if (EQUAL(pszUnit, apsLinearUnits[i].pszUnit))
840 : {
841 21 : dfVal *= apsLinearUnits[i].dfToMeter;
842 21 : bFound = true;
843 21 : break;
844 : }
845 : }
846 21 : if (!bFound)
847 : {
848 0 : CPLError(CE_Warning, CPLE_AppDefined, "Unknown unit '%s' for '%s'",
849 : pszUnit, pszElementName);
850 : }
851 : }
852 802 : return dfVal;
853 : }
854 :
855 : /************************************************************************/
856 : /* GetResolutionValue() */
857 : /************************************************************************/
858 :
859 : static const struct
860 : {
861 : const char *pszUnit;
862 : double dfToMeter;
863 : } apsResolutionUnits[] = {
864 : {"km/pixel", 1e3},
865 : {"mm/pixel", 1e-3},
866 : };
867 :
868 316 : static double GetResolutionValue(CPLXMLNode *psParent,
869 : const char *pszElementName)
870 : {
871 316 : CPLXMLNode *psNode = CPLGetXMLNode(psParent, pszElementName);
872 316 : if (psNode == nullptr)
873 0 : return 0.0;
874 316 : double dfVal = CPLAtof(CPLGetXMLValue(psNode, nullptr, ""));
875 316 : const char *pszUnit = CPLGetXMLValue(psNode, "unit", nullptr);
876 316 : if (pszUnit && !EQUAL(pszUnit, "m/pixel"))
877 : {
878 21 : bool bFound = false;
879 21 : for (size_t i = 0; i < CPL_ARRAYSIZE(apsResolutionUnits); i++)
880 : {
881 21 : if (EQUAL(pszUnit, apsResolutionUnits[i].pszUnit))
882 : {
883 21 : dfVal *= apsResolutionUnits[i].dfToMeter;
884 21 : bFound = true;
885 21 : break;
886 : }
887 : }
888 21 : if (!bFound)
889 : {
890 0 : CPLError(CE_Warning, CPLE_AppDefined, "Unknown unit '%s' for '%s'",
891 : pszUnit, pszElementName);
892 : }
893 : }
894 316 : return dfVal;
895 : }
896 :
897 : /************************************************************************/
898 : /* GetAngularValue() */
899 : /************************************************************************/
900 :
901 : static const struct
902 : {
903 : const char *pszUnit;
904 : double dfToDeg;
905 : } apsAngularUnits[] = {{"arcmin", 1. / 60.},
906 : {"arcsec", 1. / 3600},
907 : {"hr", 15.0},
908 : {"mrad", 180.0 / M_PI / 1000.},
909 : {"rad", 180.0 / M_PI}};
910 :
911 819 : static double GetAngularValue(CPLXMLNode *psParent, const char *pszElementName,
912 : bool *pbGotVal = nullptr)
913 : {
914 819 : CPLXMLNode *psNode = CPLGetXMLNode(psParent, pszElementName);
915 819 : if (psNode == nullptr)
916 : {
917 424 : if (pbGotVal)
918 265 : *pbGotVal = false;
919 424 : return 0.0;
920 : }
921 395 : double dfVal = CPLAtof(CPLGetXMLValue(psNode, nullptr, ""));
922 395 : const char *pszUnit = CPLGetXMLValue(psNode, "unit", nullptr);
923 395 : if (pszUnit && !EQUAL(pszUnit, "deg"))
924 : {
925 0 : bool bFound = false;
926 0 : for (size_t i = 0; i < CPL_ARRAYSIZE(apsAngularUnits); i++)
927 : {
928 0 : if (EQUAL(pszUnit, apsAngularUnits[i].pszUnit))
929 : {
930 0 : dfVal *= apsAngularUnits[i].dfToDeg;
931 0 : bFound = true;
932 0 : break;
933 : }
934 : }
935 0 : if (!bFound)
936 : {
937 0 : CPLError(CE_Warning, CPLE_AppDefined, "Unknown unit '%s' for '%s'",
938 : pszUnit, pszElementName);
939 : }
940 : }
941 395 : if (pbGotVal)
942 220 : *pbGotVal = true;
943 395 : return dfVal;
944 : }
945 :
946 : /************************************************************************/
947 : /* ReadGeoreferencing() */
948 : /************************************************************************/
949 :
950 : // See https://pds.nasa.gov/pds4/cart/v1/PDS4_CART_1G00_1950.xsd, (GDAL 3.4)
951 : // https://pds.nasa.gov/pds4/cart/v1/PDS4_CART_1D00_1933.xsd,
952 : // https://raw.githubusercontent.com/nasa-pds-data-dictionaries/ldd-cart/master/build/1.B.0.0/PDS4_CART_1B00.xsd,
953 : // https://pds.nasa.gov/pds4/cart/v1/PDS4_CART_1700.xsd
954 : // and the corresponding .sch files
955 295 : void PDS4Dataset::ReadGeoreferencing(CPLXMLNode *psProduct)
956 : {
957 295 : CPLXMLNode *psCart = CPLGetXMLNode(
958 : psProduct, "Observation_Area.Discipline_Area.Cartography");
959 295 : if (psCart == nullptr)
960 : {
961 133 : CPLDebug("PDS4",
962 : "Did not find Observation_Area.Discipline_Area.Cartography");
963 133 : return;
964 : }
965 :
966 : // Bounding box: informative only
967 : CPLXMLNode *psBounding =
968 162 : CPLGetXMLNode(psCart, "Spatial_Domain.Bounding_Coordinates");
969 162 : if (psBounding)
970 : {
971 : const char *pszWest =
972 162 : CPLGetXMLValue(psBounding, "west_bounding_coordinate", nullptr);
973 : const char *pszEast =
974 162 : CPLGetXMLValue(psBounding, "east_bounding_coordinate", nullptr);
975 : const char *pszNorth =
976 162 : CPLGetXMLValue(psBounding, "north_bounding_coordinate", nullptr);
977 : const char *pszSouth =
978 162 : CPLGetXMLValue(psBounding, "south_bounding_coordinate", nullptr);
979 162 : if (pszWest)
980 162 : CPLDebug("PDS4", "West: %s", pszWest);
981 162 : if (pszEast)
982 162 : CPLDebug("PDS4", "East: %s", pszEast);
983 162 : if (pszNorth)
984 162 : CPLDebug("PDS4", "North: %s", pszNorth);
985 162 : if (pszSouth)
986 162 : CPLDebug("PDS4", "South: %s", pszSouth);
987 : }
988 :
989 : CPLXMLNode *psSR =
990 162 : CPLGetXMLNode(psCart, "Spatial_Reference_Information.Horizontal_"
991 : "Coordinate_System_Definition");
992 162 : if (psSR == nullptr)
993 : {
994 0 : CPLDebug("PDS4", "Did not find Spatial_Reference_Information."
995 : "Horizontal_Coordinate_System_Definition");
996 0 : return;
997 : }
998 :
999 162 : double dfLongitudeMultiplier = 1;
1000 162 : const CPLXMLNode *psGeodeticModel = CPLGetXMLNode(psSR, "Geodetic_Model");
1001 162 : if (psGeodeticModel != nullptr)
1002 : {
1003 162 : if (EQUAL(CPLGetXMLValue(psGeodeticModel, "longitude_direction", ""),
1004 : "Positive West"))
1005 : {
1006 3 : dfLongitudeMultiplier = -1;
1007 : }
1008 : }
1009 :
1010 324 : OGRSpatialReference oSRS;
1011 : CPLXMLNode *psGridCoordinateSystem =
1012 162 : CPLGetXMLNode(psSR, "Planar.Grid_Coordinate_System");
1013 162 : CPLXMLNode *psMapProjection = CPLGetXMLNode(psSR, "Planar.Map_Projection");
1014 324 : CPLString osProjName;
1015 162 : double dfCenterLon = 0.0;
1016 162 : double dfCenterLat = 0.0;
1017 162 : double dfStdParallel1 = 0.0;
1018 162 : double dfStdParallel2 = 0.0;
1019 162 : double dfScale = 1.0;
1020 162 : double dfMapProjectionRotation = 0.0;
1021 162 : if (psGridCoordinateSystem != nullptr)
1022 : {
1023 : osProjName = CPLGetXMLValue(psGridCoordinateSystem,
1024 0 : "grid_coordinate_system_name", "");
1025 0 : if (!osProjName.empty())
1026 : {
1027 0 : if (osProjName == "Universal Transverse Mercator")
1028 : {
1029 0 : CPLXMLNode *psUTMZoneNumber = CPLGetXMLNode(
1030 : psGridCoordinateSystem,
1031 : "Universal_Transverse_Mercator.utm_zone_number");
1032 0 : if (psUTMZoneNumber)
1033 : {
1034 : int nZone =
1035 0 : atoi(CPLGetXMLValue(psUTMZoneNumber, nullptr, ""));
1036 0 : oSRS.SetUTM(std::abs(nZone), nZone >= 0);
1037 : }
1038 : }
1039 0 : else if (osProjName == "Universal Polar Stereographic")
1040 : {
1041 0 : CPLXMLNode *psProjParamNode = CPLGetXMLNode(
1042 : psGridCoordinateSystem,
1043 : "Universal_Polar_Stereographic.Polar_Stereographic");
1044 0 : if (psProjParamNode)
1045 : {
1046 0 : dfCenterLon =
1047 0 : GetAngularValue(psProjParamNode,
1048 : "longitude_of_central_meridian") *
1049 : dfLongitudeMultiplier;
1050 0 : dfCenterLat = GetAngularValue(
1051 : psProjParamNode, "latitude_of_projection_origin");
1052 0 : dfScale = CPLAtof(CPLGetXMLValue(
1053 : psProjParamNode, "scale_factor_at_projection_origin",
1054 : "1"));
1055 0 : oSRS.SetPS(dfCenterLat, dfCenterLon, dfScale, 0, 0);
1056 : }
1057 : }
1058 : else
1059 : {
1060 0 : CPLError(CE_Warning, CPLE_NotSupported,
1061 : "grid_coordinate_system_name = %s not supported",
1062 : osProjName.c_str());
1063 : }
1064 : }
1065 : }
1066 162 : else if (psMapProjection != nullptr)
1067 : {
1068 161 : osProjName = CPLGetXMLValue(psMapProjection, "map_projection_name", "");
1069 161 : if (!osProjName.empty())
1070 : {
1071 161 : CPLXMLNode *psProjParamNode = CPLGetXMLNode(
1072 : psMapProjection,
1073 322 : CPLString(osProjName).replaceAll(' ', '_').c_str());
1074 161 : if (psProjParamNode == nullptr &&
1075 : // typo in https://pds.nasa.gov/pds4/cart/v1/PDS4_CART_1700.sch
1076 0 : EQUAL(osProjName, "Orothographic"))
1077 : {
1078 : psProjParamNode =
1079 0 : CPLGetXMLNode(psMapProjection, "Orthographic");
1080 : }
1081 161 : bool bGotStdParallel1 = false;
1082 161 : bool bGotStdParallel2 = false;
1083 161 : bool bGotScale = false;
1084 161 : if (psProjParamNode)
1085 : {
1086 161 : bool bGotCenterLon = false;
1087 161 : dfCenterLon = GetAngularValue(psProjParamNode,
1088 : "longitude_of_central_meridian",
1089 : &bGotCenterLon) *
1090 : dfLongitudeMultiplier;
1091 161 : if (!bGotCenterLon)
1092 : {
1093 2 : dfCenterLon =
1094 2 : GetAngularValue(psProjParamNode,
1095 : "straight_vertical_longitude_from_pole",
1096 : &bGotCenterLon) *
1097 : dfLongitudeMultiplier;
1098 : }
1099 161 : dfCenterLat = GetAngularValue(psProjParamNode,
1100 : "latitude_of_projection_origin");
1101 161 : dfStdParallel1 = GetAngularValue(
1102 : psProjParamNode, "standard_parallel_1", &bGotStdParallel1);
1103 161 : dfStdParallel2 = GetAngularValue(
1104 : psProjParamNode, "standard_parallel_2", &bGotStdParallel2);
1105 : const char *pszScaleParam =
1106 161 : (osProjName == "Transverse Mercator")
1107 161 : ? "scale_factor_at_central_meridian"
1108 161 : : "scale_factor_at_projection_origin";
1109 : const char *pszScaleVal =
1110 161 : CPLGetXMLValue(psProjParamNode, pszScaleParam, nullptr);
1111 161 : bGotScale = pszScaleVal != nullptr;
1112 161 : dfScale = (pszScaleVal) ? CPLAtof(pszScaleVal) : 1.0;
1113 :
1114 : dfMapProjectionRotation =
1115 161 : GetAngularValue(psProjParamNode, "map_projection_rotation");
1116 : }
1117 :
1118 : CPLXMLNode *psObliqueAzimuth =
1119 161 : CPLGetXMLNode(psProjParamNode, "Oblique_Line_Azimuth");
1120 : CPLXMLNode *psObliquePoint =
1121 161 : CPLGetXMLNode(psProjParamNode, "Oblique_Line_Point");
1122 :
1123 161 : if (EQUAL(osProjName, "Equirectangular"))
1124 : {
1125 53 : oSRS.SetEquirectangular2(dfCenterLat, dfCenterLon,
1126 : dfStdParallel1, 0, 0);
1127 : }
1128 108 : else if (EQUAL(osProjName, "Lambert Conformal Conic"))
1129 : {
1130 4 : if (bGotScale)
1131 : {
1132 2 : if ((bGotStdParallel1 && dfStdParallel1 != dfCenterLat) ||
1133 0 : (bGotStdParallel2 && dfStdParallel2 != dfCenterLat))
1134 : {
1135 0 : CPLError(
1136 : CE_Warning, CPLE_AppDefined,
1137 : "Ignoring standard_parallel_1 and/or "
1138 : "standard_parallel_2 with LCC_1SP formulation");
1139 : }
1140 2 : oSRS.SetLCC1SP(dfCenterLat, dfCenterLon, dfScale, 0, 0);
1141 : }
1142 : else
1143 : {
1144 2 : oSRS.SetLCC(dfStdParallel1, dfStdParallel2, dfCenterLat,
1145 : dfCenterLon, 0, 0);
1146 : }
1147 : }
1148 104 : else if (EQUAL(osProjName, "Mercator"))
1149 : {
1150 6 : if (bGotScale)
1151 : {
1152 4 : oSRS.SetMercator(dfCenterLat, // should be 0 normally
1153 : dfCenterLon, dfScale, 0.0, 0.0);
1154 : }
1155 : else
1156 : {
1157 2 : oSRS.SetMercator2SP(dfStdParallel1,
1158 : dfCenterLat, // should be 0 normally
1159 : dfCenterLon, 0.0, 0.0);
1160 : }
1161 : }
1162 98 : else if (EQUAL(osProjName, "Orthographic"))
1163 : {
1164 2 : oSRS.SetOrthographic(dfCenterLat, dfCenterLon, 0.0, 0.0);
1165 : }
1166 98 : else if (EQUAL(osProjName, "Oblique Mercator") &&
1167 2 : (psObliqueAzimuth != nullptr || psObliquePoint != nullptr))
1168 : {
1169 4 : if (psObliqueAzimuth)
1170 : {
1171 : // Not sure of this
1172 2 : dfCenterLon = CPLAtof(
1173 : CPLGetXMLValue(psObliqueAzimuth,
1174 : "azimuth_measure_point_longitude", "0"));
1175 :
1176 2 : double dfAzimuth = CPLAtof(CPLGetXMLValue(
1177 : psObliqueAzimuth, "azimuthal_angle", "0"));
1178 2 : oSRS.SetProjection(
1179 : SRS_PT_HOTINE_OBLIQUE_MERCATOR_AZIMUTH_CENTER);
1180 2 : oSRS.SetNormProjParm(SRS_PP_LATITUDE_OF_CENTER,
1181 : dfCenterLat);
1182 2 : oSRS.SetNormProjParm(SRS_PP_LONGITUDE_OF_CENTER,
1183 : dfCenterLon);
1184 2 : oSRS.SetNormProjParm(SRS_PP_AZIMUTH, dfAzimuth);
1185 : // SetNormProjParm( SRS_PP_RECTIFIED_GRID_ANGLE,
1186 : // dfRectToSkew );
1187 2 : oSRS.SetNormProjParm(SRS_PP_SCALE_FACTOR, dfScale);
1188 2 : oSRS.SetNormProjParm(SRS_PP_FALSE_EASTING, 0.0);
1189 2 : oSRS.SetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0);
1190 : }
1191 : else
1192 : {
1193 2 : double dfLat1 = 0.0;
1194 2 : double dfLong1 = 0.0;
1195 2 : double dfLat2 = 0.0;
1196 2 : double dfLong2 = 0.0;
1197 2 : CPLXMLNode *psPoint = CPLGetXMLNode(
1198 : psObliquePoint, "Oblique_Line_Point_Group");
1199 2 : if (psPoint)
1200 : {
1201 2 : dfLat1 = CPLAtof(CPLGetXMLValue(
1202 : psPoint, "oblique_line_latitude", "0.0"));
1203 2 : dfLong1 = CPLAtof(CPLGetXMLValue(
1204 : psPoint, "oblique_line_longitude", "0.0"));
1205 2 : psPoint = psPoint->psNext;
1206 2 : if (psPoint && psPoint->eType == CXT_Element &&
1207 2 : EQUAL(psPoint->pszValue,
1208 : "Oblique_Line_Point_Group"))
1209 : {
1210 2 : dfLat2 = CPLAtof(CPLGetXMLValue(
1211 : psPoint, "oblique_line_latitude", "0.0"));
1212 2 : dfLong2 = CPLAtof(CPLGetXMLValue(
1213 : psPoint, "oblique_line_longitude", "0.0"));
1214 : }
1215 : }
1216 2 : oSRS.SetHOM2PNO(dfCenterLat, dfLat1, dfLong1, dfLat2,
1217 : dfLong2, dfScale, 0.0, 0.0);
1218 : }
1219 : }
1220 92 : else if (EQUAL(osProjName, "Polar Stereographic"))
1221 : {
1222 2 : oSRS.SetPS(dfCenterLat, dfCenterLon, dfScale, 0, 0);
1223 : }
1224 90 : else if (EQUAL(osProjName, "Polyconic"))
1225 : {
1226 2 : oSRS.SetPolyconic(dfCenterLat, dfCenterLon, 0, 0);
1227 : }
1228 88 : else if (EQUAL(osProjName, "Sinusoidal"))
1229 : {
1230 4 : oSRS.SetSinusoidal(dfCenterLon, 0, 0);
1231 : }
1232 84 : else if (EQUAL(osProjName, "Transverse Mercator"))
1233 : {
1234 78 : oSRS.SetTM(dfCenterLat, dfCenterLon, dfScale, 0, 0);
1235 : }
1236 :
1237 : // Below values are valid map_projection_name according to
1238 : // the schematron but they don't have a dedicated element to
1239 : // hold the projection parameter. Assumed the schema is extended
1240 : // similarly to the existing for a few obvious ones
1241 6 : else if (EQUAL(osProjName, "Albers Conical Equal Area"))
1242 : {
1243 0 : oSRS.SetACEA(dfStdParallel1, dfStdParallel2, dfCenterLat,
1244 : dfCenterLon, 0.0, 0.0);
1245 : }
1246 6 : else if (EQUAL(osProjName, "Azimuthal Equidistant"))
1247 : {
1248 0 : oSRS.SetAE(dfCenterLat, dfCenterLon, 0, 0);
1249 : }
1250 6 : else if (EQUAL(osProjName, "Equidistant Conic"))
1251 : {
1252 0 : oSRS.SetEC(dfStdParallel1, dfStdParallel2, dfCenterLat,
1253 : dfCenterLon, 0.0, 0.0);
1254 : }
1255 : // Unhandled: General Vertical Near-sided Projection
1256 6 : else if (EQUAL(osProjName, "Gnomonic"))
1257 : {
1258 0 : oSRS.SetGnomonic(dfCenterLat, dfCenterLon, 0, 0);
1259 : }
1260 6 : else if (EQUAL(osProjName, "Lambert Azimuthal Equal Area"))
1261 : {
1262 2 : oSRS.SetLAEA(dfCenterLat, dfCenterLon, 0, 0);
1263 : }
1264 4 : else if (EQUAL(osProjName, "Miller Cylindrical"))
1265 : {
1266 0 : oSRS.SetMC(dfCenterLat, dfCenterLon, 0, 0);
1267 : }
1268 4 : else if (EQUAL(osProjName, "Orothographic") // typo
1269 4 : || EQUAL(osProjName, "Orthographic"))
1270 : {
1271 0 : osProjName = "Orthographic";
1272 0 : oSRS.SetOrthographic(dfCenterLat, dfCenterLon, 0, 0);
1273 : }
1274 4 : else if (EQUAL(osProjName, "Robinson"))
1275 : {
1276 0 : oSRS.SetRobinson(dfCenterLon, 0, 0);
1277 : }
1278 : // Unhandled: Space Oblique Mercator
1279 4 : else if (EQUAL(osProjName, "Stereographic"))
1280 : {
1281 0 : oSRS.SetStereographic(dfCenterLat, dfCenterLon, dfScale, 0, 0);
1282 : }
1283 4 : else if (EQUAL(osProjName, "van der Grinten"))
1284 : {
1285 0 : oSRS.SetVDG(dfCenterLon, 0, 0);
1286 : }
1287 4 : else if (EQUAL(osProjName, "Oblique Cylindrical"))
1288 : {
1289 4 : const double poleLatitude = GetAngularValue(
1290 : psProjParamNode, "oblique_proj_pole_latitude");
1291 : const double poleLongitude =
1292 4 : GetAngularValue(psProjParamNode,
1293 : "oblique_proj_pole_longitude") *
1294 4 : dfLongitudeMultiplier;
1295 4 : const double poleRotation = GetAngularValue(
1296 : psProjParamNode, "oblique_proj_pole_rotation");
1297 :
1298 8 : CPLString oProj4String;
1299 : // Cf isis3dataset.cpp comments for ObliqueCylindrical
1300 : oProj4String.Printf("+proj=ob_tran +o_proj=eqc +o_lon_p=%.17g "
1301 : "+o_lat_p=%.17g +lon_0=%.17g",
1302 : -poleRotation, 180 - poleLatitude,
1303 4 : poleLongitude);
1304 4 : oSRS.SetFromUserInput(oProj4String);
1305 : }
1306 : else
1307 : {
1308 0 : CPLError(CE_Warning, CPLE_NotSupported,
1309 : "map_projection_name = %s not supported",
1310 : osProjName.c_str());
1311 : }
1312 : }
1313 : }
1314 : else
1315 : {
1316 1 : CPLXMLNode *psGeographic = CPLGetXMLNode(psSR, "Geographic");
1317 1 : if (GetLayerCount() && psGeographic)
1318 : {
1319 : // do nothing
1320 : }
1321 : else
1322 : {
1323 0 : CPLError(CE_Warning, CPLE_AppDefined,
1324 : "Planar.Map_Projection not found");
1325 : }
1326 : }
1327 :
1328 162 : if (oSRS.IsProjected())
1329 : {
1330 161 : oSRS.SetLinearUnits("Metre", 1.0);
1331 : }
1332 :
1333 162 : if (psGeodeticModel != nullptr)
1334 : {
1335 : const char *pszLatitudeType =
1336 162 : CPLGetXMLValue(psGeodeticModel, "latitude_type", "");
1337 162 : bool bIsOgraphic = EQUAL(pszLatitudeType, "Planetographic");
1338 :
1339 : const bool bUseLDD1930RadiusNames =
1340 162 : CPLGetXMLNode(psGeodeticModel, "a_axis_radius") != nullptr;
1341 :
1342 : // Before PDS CART schema pre-1.B.10.0 (pre LDD version 1.9.3.0),
1343 : // the confusing semi_major_radius, semi_minor_radius and polar_radius
1344 : // were used but did not follow the recommended
1345 : // FGDC names. Using both "semi" and "radius" in the same keyword,
1346 : // which both mean half, does not make sense.
1347 162 : const char *pszAAxis =
1348 162 : bUseLDD1930RadiusNames ? "a_axis_radius" : "semi_major_radius";
1349 162 : const char *pszBAxis =
1350 162 : bUseLDD1930RadiusNames ? "b_axis_radius" : "semi_minor_radius";
1351 162 : const char *pszCAxis =
1352 162 : bUseLDD1930RadiusNames ? "c_axis_radius" : "polar_radius";
1353 :
1354 162 : const double dfSemiMajor = GetLinearValue(psGeodeticModel, pszAAxis);
1355 :
1356 : // a_axis_radius and b_axis_radius should be the same in most cases
1357 : // unless a triaxial body is being defined. This should be extremely
1358 : // rare (and not used) since the IAU generally defines a best-fit sphere
1359 : // for triaxial bodies: https://astrogeology.usgs.gov/groups/IAU-WGCCRE
1360 162 : const double dfBValue = GetLinearValue(psGeodeticModel, pszBAxis);
1361 162 : if (dfSemiMajor != dfBValue)
1362 : {
1363 0 : CPLError(CE_Warning, CPLE_AppDefined,
1364 : "%s = %f m, different from "
1365 : "%s = %f, will be ignored",
1366 : pszBAxis, dfBValue, pszAAxis, dfSemiMajor);
1367 : }
1368 :
1369 162 : const double dfPolarRadius = GetLinearValue(psGeodeticModel, pszCAxis);
1370 : // Use the polar_radius as the actual semi minor
1371 162 : const double dfSemiMinor = dfPolarRadius;
1372 :
1373 : // Compulsory
1374 162 : const char *pszTargetName = CPLGetXMLValue(
1375 : psProduct, "Observation_Area.Target_Identification.name",
1376 : "unknown");
1377 :
1378 162 : if (oSRS.IsProjected())
1379 : {
1380 483 : CPLString osProjTargetName = osProjName + " " + pszTargetName;
1381 161 : oSRS.SetProjCS(osProjTargetName);
1382 : }
1383 :
1384 486 : CPLString osGeogName = CPLString("GCS_") + pszTargetName;
1385 :
1386 : CPLString osSphereName =
1387 324 : CPLGetXMLValue(psGeodeticModel, "spheroid_name", pszTargetName);
1388 324 : CPLString osDatumName = "D_" + osSphereName;
1389 :
1390 : // calculate inverse flattening from major and minor axis: 1/f = a/(a-b)
1391 162 : double dfInvFlattening = 0;
1392 162 : if ((dfSemiMajor - dfSemiMinor) >= 0.00000001)
1393 : {
1394 82 : dfInvFlattening = dfSemiMajor / (dfSemiMajor - dfSemiMinor);
1395 : }
1396 :
1397 : //(if stereographic with center lat ==90) or (polar stereographic )
1398 324 : if ((EQUAL(osProjName, "STEREOGRAPHIC") && fabs(dfCenterLat) == 90) ||
1399 162 : (EQUAL(osProjName, "POLAR STEREOGRAPHIC")))
1400 : {
1401 2 : if (bIsOgraphic)
1402 : {
1403 0 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
1404 : dfSemiMajor, dfInvFlattening,
1405 : "Reference_Meridian", 0.0);
1406 : }
1407 : else
1408 : {
1409 2 : osSphereName += "_polarRadius";
1410 2 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
1411 : dfPolarRadius, 0.0, "Reference_Meridian", 0.0);
1412 : }
1413 : }
1414 160 : else if ((EQUAL(osProjName, "EQUIRECTANGULAR")) ||
1415 107 : (EQUAL(osProjName, "ORTHOGRAPHIC")) ||
1416 372 : (EQUAL(osProjName, "STEREOGRAPHIC")) ||
1417 105 : (EQUAL(osProjName, "SINUSOIDAL")))
1418 : {
1419 59 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName, dfSemiMajor,
1420 : 0.0, "Reference_Meridian", 0.0);
1421 : }
1422 : else
1423 : {
1424 101 : if (bIsOgraphic)
1425 : {
1426 2 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
1427 : dfSemiMajor, dfInvFlattening,
1428 : "Reference_Meridian", 0.0);
1429 : }
1430 : else
1431 : {
1432 99 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
1433 : dfSemiMajor, 0.0, "Reference_Meridian", 0.0);
1434 : }
1435 : }
1436 : }
1437 :
1438 : CPLXMLNode *psPCI =
1439 162 : CPLGetXMLNode(psSR, "Planar.Planar_Coordinate_Information");
1440 162 : CPLXMLNode *psGT = CPLGetXMLNode(psSR, "Planar.Geo_Transformation");
1441 162 : if (psPCI && psGT)
1442 : {
1443 : const char *pszPCIEncoding =
1444 158 : CPLGetXMLValue(psPCI, "planar_coordinate_encoding_method", "");
1445 158 : CPLXMLNode *psCR = CPLGetXMLNode(psPCI, "Coordinate_Representation");
1446 158 : if (!EQUAL(pszPCIEncoding, "Coordinate Pair"))
1447 : {
1448 0 : CPLError(CE_Warning, CPLE_NotSupported,
1449 : "planar_coordinate_encoding_method = %s not supported",
1450 : pszPCIEncoding);
1451 : }
1452 158 : else if (psCR != nullptr)
1453 : {
1454 158 : double dfXRes = GetResolutionValue(psCR, "pixel_resolution_x");
1455 158 : double dfYRes = GetResolutionValue(psCR, "pixel_resolution_y");
1456 158 : double dfULX = GetLinearValue(psGT, "upperleft_corner_x");
1457 158 : double dfULY = GetLinearValue(psGT, "upperleft_corner_y");
1458 :
1459 : // The PDS4 specification is not really clear about the
1460 : // origin convention, but it appears from
1461 : // https://github.com/OSGeo/gdal/issues/735 that it matches GDAL
1462 : // top-left corner of top-left pixel
1463 158 : m_gt.xorig = dfULX;
1464 158 : m_gt.xscale = dfXRes;
1465 158 : m_gt.xrot = 0.0;
1466 158 : m_gt.yorig = dfULY;
1467 158 : m_gt.yrot = 0.0;
1468 158 : m_gt.yscale = -dfYRes;
1469 158 : m_bGotTransform = true;
1470 :
1471 158 : if (dfMapProjectionRotation != 0)
1472 : {
1473 4 : const double sin_rot =
1474 : dfMapProjectionRotation == 90
1475 4 : ? 1.0
1476 0 : : sin(dfMapProjectionRotation / 180 * M_PI);
1477 4 : const double cos_rot =
1478 : dfMapProjectionRotation == 90
1479 4 : ? 0.0
1480 0 : : cos(dfMapProjectionRotation / 180 * M_PI);
1481 4 : const double gt_1 = cos_rot * m_gt.xscale - sin_rot * m_gt.yrot;
1482 4 : const double gt_2 = cos_rot * m_gt.xrot - sin_rot * m_gt.yscale;
1483 4 : const double gt_0 = cos_rot * m_gt.xorig - sin_rot * m_gt.yorig;
1484 4 : const double gt_4 = sin_rot * m_gt.xscale + cos_rot * m_gt.yrot;
1485 4 : const double gt_5 = sin_rot * m_gt.xrot + cos_rot * m_gt.yscale;
1486 4 : const double gt_3 = sin_rot * m_gt.xorig + cos_rot * m_gt.yorig;
1487 4 : m_gt.xscale = gt_1;
1488 4 : m_gt.xrot = gt_2;
1489 4 : m_gt.xorig = gt_0;
1490 4 : m_gt.yrot = gt_4;
1491 4 : m_gt.yscale = gt_5;
1492 4 : m_gt.yorig = gt_3;
1493 : }
1494 : }
1495 : }
1496 :
1497 162 : if (!oSRS.IsEmpty())
1498 : {
1499 162 : if (GetRasterCount())
1500 : {
1501 158 : m_oSRS = std::move(oSRS);
1502 : }
1503 4 : else if (GetLayerCount())
1504 : {
1505 8 : for (auto &poLayer : m_apoLayers)
1506 : {
1507 4 : if (poLayer->GetGeomType() != wkbNone)
1508 : {
1509 4 : auto poSRSClone = oSRS.Clone();
1510 4 : poLayer->SetSpatialRef(poSRSClone);
1511 4 : poSRSClone->Release();
1512 : }
1513 : }
1514 : }
1515 : }
1516 : }
1517 :
1518 : /************************************************************************/
1519 : /* GetLayer() */
1520 : /************************************************************************/
1521 :
1522 319 : const OGRLayer *PDS4Dataset::GetLayer(int nIndex) const
1523 : {
1524 319 : if (nIndex < 0 || nIndex >= GetLayerCount())
1525 8 : return nullptr;
1526 311 : return m_apoLayers[nIndex].get();
1527 : }
1528 :
1529 : /************************************************************************/
1530 : /* FixupTableFilename() */
1531 : /************************************************************************/
1532 :
1533 97 : static std::string FixupTableFilename(const std::string &osFilename)
1534 : {
1535 : VSIStatBufL sStat;
1536 97 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
1537 : {
1538 97 : return osFilename;
1539 : }
1540 0 : const std::string osExt = CPLGetExtensionSafe(osFilename.c_str());
1541 0 : if (!osExt.empty())
1542 : {
1543 0 : std::string osTry(osFilename);
1544 0 : if (osExt[0] >= 'a' && osExt[0] <= 'z')
1545 : {
1546 0 : osTry = CPLResetExtensionSafe(osFilename.c_str(),
1547 0 : CPLString(osExt).toupper());
1548 : }
1549 : else
1550 : {
1551 0 : osTry = CPLResetExtensionSafe(osFilename.c_str(),
1552 0 : CPLString(osExt).tolower());
1553 : }
1554 0 : if (VSIStatL(osTry.c_str(), &sStat) == 0)
1555 : {
1556 0 : return osTry;
1557 : }
1558 : }
1559 0 : return osFilename;
1560 : }
1561 :
1562 : /************************************************************************/
1563 : /* OpenTableCharacter() */
1564 : /************************************************************************/
1565 :
1566 22 : bool PDS4Dataset::OpenTableCharacter(const char *pszFilename,
1567 : const CPLXMLNode *psTable)
1568 : {
1569 22 : if (CPLHasPathTraversal(pszFilename))
1570 : {
1571 0 : CPLError(CE_Failure, CPLE_NotSupported,
1572 : "OpenTableCharacter(): path traversal detected: %s",
1573 : pszFilename);
1574 0 : return false;
1575 : }
1576 44 : std::string osLayerName(CPLGetBasenameSafe(pszFilename));
1577 22 : if (cpl::starts_with(osLayerName,
1578 44 : CPLGetBasenameSafe(m_osXMLFilename.c_str()) + "_"))
1579 26 : osLayerName = osLayerName.substr(
1580 39 : CPLGetBasenameSafe(m_osXMLFilename.c_str()).size() + 1);
1581 :
1582 44 : const std::string osFullFilename = FixupTableFilename(CPLFormFilenameSafe(
1583 66 : CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(), pszFilename, nullptr));
1584 : auto poLayer = std::make_unique<PDS4TableCharacter>(
1585 44 : this, osLayerName.c_str(), osFullFilename.c_str());
1586 22 : if (!poLayer->ReadTableDef(psTable))
1587 : {
1588 0 : return false;
1589 : }
1590 22 : m_apoLayers.push_back(
1591 44 : std::make_unique<PDS4EditableLayer>(std::move(poLayer)));
1592 22 : return true;
1593 : }
1594 :
1595 : /************************************************************************/
1596 : /* OpenTableBinary() */
1597 : /************************************************************************/
1598 :
1599 11 : bool PDS4Dataset::OpenTableBinary(const char *pszFilename,
1600 : const CPLXMLNode *psTable)
1601 : {
1602 11 : if (CPLHasPathTraversal(pszFilename))
1603 : {
1604 0 : CPLError(CE_Failure, CPLE_NotSupported,
1605 : "OpenTableBinary(): path traversal detected: %s", pszFilename);
1606 0 : return false;
1607 : }
1608 :
1609 22 : std::string osLayerName(CPLGetBasenameSafe(pszFilename));
1610 11 : if (cpl::starts_with(osLayerName,
1611 22 : CPLGetBasenameSafe(m_osXMLFilename.c_str()) + "_"))
1612 20 : osLayerName = osLayerName.substr(
1613 30 : CPLGetBasenameSafe(m_osXMLFilename.c_str()).size() + 1);
1614 :
1615 22 : const std::string osFullFilename = FixupTableFilename(CPLFormFilenameSafe(
1616 33 : CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(), pszFilename, nullptr));
1617 0 : auto poLayer = std::make_unique<PDS4TableBinary>(this, osLayerName.c_str(),
1618 22 : osFullFilename.c_str());
1619 11 : if (!poLayer->ReadTableDef(psTable))
1620 : {
1621 0 : return false;
1622 : }
1623 11 : m_apoLayers.push_back(
1624 22 : std::make_unique<PDS4EditableLayer>(std::move(poLayer)));
1625 11 : return true;
1626 : }
1627 :
1628 : /************************************************************************/
1629 : /* OpenTableDelimited() */
1630 : /************************************************************************/
1631 :
1632 64 : bool PDS4Dataset::OpenTableDelimited(const char *pszFilename,
1633 : const CPLXMLNode *psTable)
1634 : {
1635 64 : if (CPLHasPathTraversal(pszFilename))
1636 : {
1637 0 : CPLError(CE_Failure, CPLE_NotSupported,
1638 : "OpenTableDelimited(): path traversal detected: %s",
1639 : pszFilename);
1640 0 : return false;
1641 : }
1642 :
1643 128 : std::string osLayerName(CPLGetBasenameSafe(pszFilename));
1644 64 : if (cpl::starts_with(osLayerName,
1645 128 : CPLGetBasenameSafe(m_osXMLFilename.c_str()) + "_"))
1646 120 : osLayerName = osLayerName.substr(
1647 180 : CPLGetBasenameSafe(m_osXMLFilename.c_str()).size() + 1);
1648 :
1649 128 : const std::string osFullFilename = FixupTableFilename(CPLFormFilenameSafe(
1650 192 : CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(), pszFilename, nullptr));
1651 : auto poLayer = std::make_unique<PDS4DelimitedTable>(
1652 128 : this, osLayerName.c_str(), osFullFilename.c_str());
1653 64 : if (!poLayer->ReadTableDef(psTable))
1654 : {
1655 0 : return false;
1656 : }
1657 64 : m_apoLayers.push_back(
1658 128 : std::make_unique<PDS4EditableLayer>(std::move(poLayer)));
1659 64 : return true;
1660 : }
1661 :
1662 : /************************************************************************/
1663 : /* ConstantToDouble() */
1664 : /************************************************************************/
1665 :
1666 180 : static std::optional<double> ConstantToDouble(const char *pszItem,
1667 : const char *pszVal)
1668 : {
1669 180 : if (STARTS_WITH(pszVal, "0x"))
1670 : {
1671 21 : if (strlen(pszVal) == strlen("0x") + 2 * sizeof(float))
1672 : {
1673 15 : char *endptr = nullptr;
1674 : const uint32_t nVal =
1675 15 : static_cast<uint32_t>(std::strtoull(pszVal, &endptr, 0));
1676 15 : if (endptr == pszVal + strlen(pszVal))
1677 : {
1678 : float fVal;
1679 15 : memcpy(&fVal, &nVal, sizeof(nVal));
1680 15 : return fVal;
1681 : }
1682 : }
1683 6 : else if (strlen(pszVal) == strlen("0x") + 2 * sizeof(double))
1684 : {
1685 6 : char *endptr = nullptr;
1686 : const uint64_t nVal =
1687 6 : static_cast<uint64_t>(std::strtoull(pszVal, &endptr, 0));
1688 6 : if (endptr == pszVal + strlen(pszVal))
1689 : {
1690 : double dfVal;
1691 6 : memcpy(&dfVal, &nVal, sizeof(nVal));
1692 6 : return dfVal;
1693 : }
1694 : }
1695 0 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid value for '%s': '%s'",
1696 : pszItem, pszVal);
1697 0 : return std::nullopt;
1698 : }
1699 : else
1700 : {
1701 159 : char *endptr = nullptr;
1702 159 : double dfVal = std::strtod(pszVal, &endptr);
1703 159 : if (endptr == pszVal + strlen(pszVal))
1704 : {
1705 159 : return dfVal;
1706 : }
1707 : else
1708 : {
1709 0 : CPLError(CE_Failure, CPLE_AppDefined,
1710 : "Invalid value for '%s': '%s'", pszItem, pszVal);
1711 0 : return std::nullopt;
1712 : }
1713 : }
1714 : }
1715 :
1716 : /************************************************************************/
1717 : /* Open() */
1718 : /************************************************************************/
1719 :
1720 : // See https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.xsd
1721 : // and https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.sch
1722 306 : std::unique_ptr<PDS4Dataset> PDS4Dataset::OpenInternal(GDALOpenInfo *poOpenInfo)
1723 : {
1724 306 : if (!PDS4DriverIdentify(poOpenInfo))
1725 0 : return nullptr;
1726 :
1727 612 : CPLString osXMLFilename(poOpenInfo->pszFilename);
1728 306 : int nFAOIdxLookup = -1;
1729 306 : int nArrayIdxLookup = -1;
1730 306 : if (STARTS_WITH_CI(poOpenInfo->pszFilename, "PDS4:"))
1731 : {
1732 : char **papszTokens =
1733 15 : CSLTokenizeString2(poOpenInfo->pszFilename, ":", 0);
1734 15 : int nCount = CSLCount(papszTokens);
1735 15 : if (nCount == 5 && strlen(papszTokens[1]) == 1 &&
1736 1 : (papszTokens[2][0] == '\\' || papszTokens[2][0] == '/'))
1737 : {
1738 1 : osXMLFilename = CPLString(papszTokens[1]) + ":" + papszTokens[2];
1739 1 : nFAOIdxLookup = atoi(papszTokens[3]);
1740 1 : nArrayIdxLookup = atoi(papszTokens[4]);
1741 : }
1742 14 : else if (nCount == 5 && (EQUAL(papszTokens[1], "/vsicurl/http") ||
1743 0 : EQUAL(papszTokens[1], "/vsicurl/https")))
1744 : {
1745 0 : osXMLFilename = CPLString(papszTokens[1]) + ":" + papszTokens[2];
1746 0 : nFAOIdxLookup = atoi(papszTokens[3]);
1747 0 : nArrayIdxLookup = atoi(papszTokens[4]);
1748 : }
1749 14 : else if (nCount == 4)
1750 : {
1751 13 : osXMLFilename = papszTokens[1];
1752 13 : nFAOIdxLookup = atoi(papszTokens[2]);
1753 13 : nArrayIdxLookup = atoi(papszTokens[3]);
1754 : }
1755 : else
1756 : {
1757 1 : CPLError(CE_Failure, CPLE_AppDefined,
1758 : "Invalid syntax for PDS4 subdataset name");
1759 1 : CSLDestroy(papszTokens);
1760 1 : return nullptr;
1761 : }
1762 14 : CSLDestroy(papszTokens);
1763 : }
1764 :
1765 610 : CPLXMLTreeCloser oCloser(CPLParseXMLFile(osXMLFilename));
1766 305 : CPLXMLNode *psRoot = oCloser.get();
1767 305 : CPLStripXMLNamespace(psRoot, nullptr, TRUE);
1768 :
1769 610 : GDALAccess eAccess = STARTS_WITH_CI(poOpenInfo->pszFilename, "PDS4:")
1770 305 : ? GA_ReadOnly
1771 : : poOpenInfo->eAccess;
1772 :
1773 305 : CPLXMLNode *psProduct = CPLGetXMLNode(psRoot, "=Product_Observational");
1774 305 : if (psProduct == nullptr)
1775 : {
1776 4 : eAccess = GA_ReadOnly;
1777 4 : psProduct = CPLGetXMLNode(psRoot, "=Product_Ancillary");
1778 4 : if (psProduct == nullptr)
1779 : {
1780 4 : psProduct = CPLGetXMLNode(psRoot, "=Product_Collection");
1781 : }
1782 : }
1783 305 : if (psProduct == nullptr)
1784 : {
1785 3 : return nullptr;
1786 : }
1787 :
1788 : // Test case:
1789 : // https://starbase.jpl.nasa.gov/pds4/1700/dph_example_products/test_Images_DisplaySettings/TestPattern_Image/TestPattern.xml
1790 302 : const char *pszVertDir = CPLGetXMLValue(
1791 : psProduct,
1792 : "Observation_Area.Discipline_Area.Display_Settings.Display_Direction."
1793 : "vertical_display_direction",
1794 : "");
1795 302 : const bool bBottomToTop = EQUAL(pszVertDir, "Bottom to Top");
1796 :
1797 302 : const char *pszHorizDir = CPLGetXMLValue(
1798 : psProduct,
1799 : "Observation_Area.Discipline_Area.Display_Settings.Display_Direction."
1800 : "horizontal_display_direction",
1801 : "");
1802 302 : const bool bRightToLeft = EQUAL(pszHorizDir, "Right to Left");
1803 :
1804 604 : auto poDS = std::make_unique<PDS4Dataset>();
1805 302 : poDS->m_osXMLFilename = osXMLFilename;
1806 302 : poDS->eAccess = eAccess;
1807 302 : poDS->papszOpenOptions = CSLDuplicate(poOpenInfo->papszOpenOptions);
1808 :
1809 604 : CPLStringList aosSubdatasets;
1810 302 : int nFAOIdx = 0;
1811 2946 : for (CPLXMLNode *psIter = psProduct->psChild; psIter != nullptr;
1812 2644 : psIter = psIter->psNext)
1813 : {
1814 2646 : if (psIter->eType != CXT_Element ||
1815 933 : (strcmp(psIter->pszValue, "File_Area_Observational") != 0 &&
1816 602 : strcmp(psIter->pszValue, "File_Area_Ancillary") != 0 &&
1817 602 : strcmp(psIter->pszValue, "File_Area_Inventory") != 0))
1818 : {
1819 2314 : continue;
1820 : }
1821 :
1822 332 : nFAOIdx++;
1823 332 : CPLXMLNode *psFile = CPLGetXMLNode(psIter, "File");
1824 332 : if (psFile == nullptr)
1825 : {
1826 1 : continue;
1827 : }
1828 331 : const char *pszFilename = CPLGetXMLValue(psFile, "file_name", nullptr);
1829 331 : if (pszFilename == nullptr)
1830 : {
1831 1 : continue;
1832 : }
1833 :
1834 693 : for (CPLXMLNode *psSubIter = psFile->psChild; psSubIter != nullptr;
1835 363 : psSubIter = psSubIter->psNext)
1836 : {
1837 363 : if (psSubIter->eType == CXT_Comment &&
1838 14 : EQUAL(psSubIter->pszValue, PREEXISTING_BINARY_FILE))
1839 : {
1840 14 : poDS->m_bCreatedFromExistingBinaryFile = true;
1841 : }
1842 : }
1843 :
1844 330 : int nArrayIdx = 0;
1845 330 : for (CPLXMLNode *psSubIter = psIter->psChild;
1846 1050 : (nFAOIdxLookup < 0 || nFAOIdxLookup == nFAOIdx) &&
1847 : psSubIter != nullptr;
1848 720 : psSubIter = psSubIter->psNext)
1849 : {
1850 722 : if (psSubIter->eType != CXT_Element)
1851 : {
1852 504 : continue;
1853 : }
1854 722 : int nDIM = 0;
1855 722 : if (STARTS_WITH(psSubIter->pszValue, "Array_1D"))
1856 : {
1857 0 : nDIM = 1;
1858 : }
1859 722 : else if (STARTS_WITH(psSubIter->pszValue, "Array_2D"))
1860 : {
1861 6 : nDIM = 2;
1862 : }
1863 716 : else if (STARTS_WITH(psSubIter->pszValue, "Array_3D"))
1864 : {
1865 241 : nDIM = 3;
1866 : }
1867 475 : else if (strcmp(psSubIter->pszValue, "Array") == 0)
1868 : {
1869 3 : nDIM = atoi(CPLGetXMLValue(psSubIter, "axes", "0"));
1870 : }
1871 472 : else if (strcmp(psSubIter->pszValue, "Table_Character") == 0)
1872 : {
1873 22 : poDS->OpenTableCharacter(pszFilename, psSubIter);
1874 22 : continue;
1875 : }
1876 450 : else if (strcmp(psSubIter->pszValue, "Table_Binary") == 0)
1877 : {
1878 11 : poDS->OpenTableBinary(pszFilename, psSubIter);
1879 11 : continue;
1880 : }
1881 439 : else if (strcmp(psSubIter->pszValue, "Table_Delimited") == 0 ||
1882 376 : strcmp(psSubIter->pszValue, "Inventory") == 0)
1883 : {
1884 64 : poDS->OpenTableDelimited(pszFilename, psSubIter);
1885 64 : continue;
1886 : }
1887 625 : if (nDIM == 0)
1888 : {
1889 375 : continue;
1890 : }
1891 250 : if (!(nDIM >= 1 && nDIM <= 3))
1892 : {
1893 0 : CPLError(CE_Warning, CPLE_NotSupported,
1894 : "Array with %d dimensions not supported", nDIM);
1895 0 : continue;
1896 : }
1897 :
1898 250 : nArrayIdx++;
1899 : // Does it match a selected subdataset ?
1900 250 : if (nArrayIdxLookup > 0 && nArrayIdx != nArrayIdxLookup)
1901 : {
1902 13 : continue;
1903 : }
1904 :
1905 : const char *pszArrayName =
1906 237 : CPLGetXMLValue(psSubIter, "name", nullptr);
1907 : const char *pszArrayId =
1908 237 : CPLGetXMLValue(psSubIter, "local_identifier", nullptr);
1909 : vsi_l_offset nOffset = static_cast<vsi_l_offset>(
1910 237 : CPLAtoGIntBig(CPLGetXMLValue(psSubIter, "offset", "0")));
1911 :
1912 : const char *pszAxisIndexOrder =
1913 237 : CPLGetXMLValue(psSubIter, "axis_index_order", "");
1914 237 : if (!EQUAL(pszAxisIndexOrder, "Last Index Fastest"))
1915 : {
1916 1 : CPLError(CE_Warning, CPLE_NotSupported,
1917 : "axis_index_order = '%s' unhandled",
1918 : pszAxisIndexOrder);
1919 1 : continue;
1920 : }
1921 :
1922 : // Figure out data type
1923 : const char *pszDataType =
1924 236 : CPLGetXMLValue(psSubIter, "Element_Array.data_type", "");
1925 236 : GDALDataType eDT = GDT_UInt8;
1926 236 : bool bLSBOrder = strstr(pszDataType, "LSB") != nullptr;
1927 :
1928 : // ComplexLSB16', 'ComplexLSB8', 'ComplexMSB16', 'ComplexMSB8',
1929 : // 'IEEE754LSBDouble', 'IEEE754LSBSingle', 'IEEE754MSBDouble',
1930 : // 'IEEE754MSBSingle', 'SignedBitString', 'SignedByte',
1931 : // 'SignedLSB2', 'SignedLSB4', 'SignedLSB8', 'SignedMSB2',
1932 : // 'SignedMSB4', 'SignedMSB8', 'UnsignedBitString', 'UnsignedByte',
1933 : // 'UnsignedLSB2', 'UnsignedLSB4', 'UnsignedLSB8', 'UnsignedMSB2',
1934 : // 'UnsignedMSB4', 'UnsignedMSB8'
1935 236 : if (EQUAL(pszDataType, "ComplexLSB16") ||
1936 232 : EQUAL(pszDataType, "ComplexMSB16"))
1937 : {
1938 6 : eDT = GDT_CFloat64;
1939 : }
1940 230 : else if (EQUAL(pszDataType, "ComplexLSB8") ||
1941 226 : EQUAL(pszDataType, "ComplexMSB8"))
1942 : {
1943 4 : eDT = GDT_CFloat32;
1944 : }
1945 226 : else if (EQUAL(pszDataType, "IEEE754LSBDouble") ||
1946 219 : EQUAL(pszDataType, "IEEE754MSBDouble"))
1947 : {
1948 7 : eDT = GDT_Float64;
1949 : }
1950 219 : else if (EQUAL(pszDataType, "IEEE754LSBSingle") ||
1951 208 : EQUAL(pszDataType, "IEEE754MSBSingle"))
1952 : {
1953 12 : eDT = GDT_Float32;
1954 : }
1955 : // SignedBitString unhandled
1956 207 : else if (EQUAL(pszDataType, "SignedByte"))
1957 : {
1958 6 : eDT = GDT_Int8;
1959 : }
1960 201 : else if (EQUAL(pszDataType, "SignedLSB2") ||
1961 197 : EQUAL(pszDataType, "SignedMSB2"))
1962 : {
1963 6 : eDT = GDT_Int16;
1964 : }
1965 195 : else if (EQUAL(pszDataType, "SignedLSB4") ||
1966 191 : EQUAL(pszDataType, "SignedMSB4"))
1967 : {
1968 4 : eDT = GDT_Int32;
1969 : }
1970 191 : else if (EQUAL(pszDataType, "SignedLSB8") ||
1971 187 : EQUAL(pszDataType, "SignedMSB8"))
1972 : {
1973 4 : eDT = GDT_Int64;
1974 : }
1975 187 : else if (EQUAL(pszDataType, "UnsignedByte"))
1976 : {
1977 170 : eDT = GDT_UInt8;
1978 : }
1979 17 : else if (EQUAL(pszDataType, "UnsignedLSB2") ||
1980 9 : EQUAL(pszDataType, "UnsignedMSB2"))
1981 : {
1982 8 : eDT = GDT_UInt16;
1983 : }
1984 9 : else if (EQUAL(pszDataType, "UnsignedLSB4") ||
1985 5 : EQUAL(pszDataType, "UnsignedMSB4"))
1986 : {
1987 4 : eDT = GDT_UInt32;
1988 : }
1989 5 : else if (EQUAL(pszDataType, "UnsignedLSB8") ||
1990 1 : EQUAL(pszDataType, "UnsignedMSB8"))
1991 : {
1992 4 : eDT = GDT_UInt64;
1993 : }
1994 : else
1995 : {
1996 1 : CPLDebug("PDS4", "data_type = '%s' unhandled", pszDataType);
1997 1 : continue;
1998 : }
1999 :
2000 235 : poDS->m_osUnits =
2001 470 : CPLGetXMLValue(psSubIter, "Element_Array.unit", "");
2002 :
2003 235 : double dfValueOffset = CPLAtof(
2004 : CPLGetXMLValue(psSubIter, "Element_Array.value_offset", "0"));
2005 235 : double dfValueScale = CPLAtof(
2006 : CPLGetXMLValue(psSubIter, "Element_Array.scaling_factor", "1"));
2007 :
2008 : // Parse Axis_Array elements
2009 235 : int l_nBands = 1;
2010 235 : int nLines = 0;
2011 235 : int nSamples = 0;
2012 235 : std::vector<int> anElements;
2013 235 : std::vector<std::string> axisNames;
2014 235 : std::vector<char> dimSemantics;
2015 235 : anElements.resize(nDIM);
2016 235 : axisNames.resize(nDIM);
2017 235 : dimSemantics.resize(nDIM);
2018 235 : int iBandIdx = -1;
2019 235 : int iLineIdx = -1;
2020 235 : int iSampleIdx = -1;
2021 235 : int nAxisOKCount = 0;
2022 235 : for (CPLXMLNode *psAxisIter = psSubIter->psChild;
2023 2161 : psAxisIter != nullptr; psAxisIter = psAxisIter->psNext)
2024 : {
2025 1927 : if (psAxisIter->eType != CXT_Element ||
2026 1927 : strcmp(psAxisIter->pszValue, "Axis_Array") != 0)
2027 : {
2028 1224 : continue;
2029 : }
2030 : const char *pszAxisName =
2031 703 : CPLGetXMLValue(psAxisIter, "axis_name", nullptr);
2032 : const char *pszElements =
2033 703 : CPLGetXMLValue(psAxisIter, "elements", nullptr);
2034 : const char *pszSequenceNumber =
2035 703 : CPLGetXMLValue(psAxisIter, "sequence_number", nullptr);
2036 703 : if (pszAxisName == nullptr || pszElements == nullptr ||
2037 : pszSequenceNumber == nullptr)
2038 : {
2039 1 : continue;
2040 : }
2041 702 : int nSeqNumber = atoi(pszSequenceNumber);
2042 702 : if (nSeqNumber < 1 || nSeqNumber > nDIM)
2043 : {
2044 2 : CPLError(CE_Warning, CPLE_AppDefined,
2045 : "Invalid sequence_number = %s", pszSequenceNumber);
2046 2 : continue;
2047 : }
2048 700 : int nElements = atoi(pszElements);
2049 700 : if (nElements <= 0)
2050 : {
2051 1 : CPLError(CE_Warning, CPLE_AppDefined,
2052 : "Invalid elements = %s", pszElements);
2053 1 : continue;
2054 : }
2055 :
2056 699 : const int nIdx = nSeqNumber - 1;
2057 699 : if (STARTS_WITH_CI(pszAxisName, "Line"))
2058 : {
2059 234 : if (iLineIdx < 0)
2060 234 : iLineIdx = nIdx;
2061 : else
2062 : {
2063 0 : CPLError(CE_Warning, CPLE_AppDefined,
2064 : "Several axis with Line identifier");
2065 0 : break;
2066 : }
2067 : }
2068 465 : else if (STARTS_WITH_CI(pszAxisName, "Sample"))
2069 : {
2070 234 : if (iSampleIdx < 0)
2071 234 : iSampleIdx = nIdx;
2072 : else
2073 : {
2074 0 : CPLError(CE_Warning, CPLE_AppDefined,
2075 : "Several axis with Sample identifier");
2076 0 : break;
2077 : }
2078 : }
2079 231 : else if (STARTS_WITH_CI(pszAxisName, "Band"))
2080 : {
2081 229 : if (iBandIdx < 0)
2082 228 : iBandIdx = nIdx;
2083 : else
2084 : {
2085 1 : CPLError(CE_Warning, CPLE_AppDefined,
2086 : "Several axis with Band identifier");
2087 1 : break;
2088 : }
2089 : }
2090 :
2091 698 : anElements[nIdx] = nElements;
2092 698 : axisNames[nIdx] = pszAxisName;
2093 :
2094 698 : ++nAxisOKCount;
2095 : }
2096 :
2097 235 : if (nAxisOKCount != nDIM)
2098 : {
2099 1 : CPLError(CE_Warning, CPLE_AppDefined,
2100 : "Found only %d Axis_Array elements. %d expected",
2101 : nAxisOKCount, nDIM);
2102 1 : continue;
2103 : }
2104 :
2105 234 : if (nDIM == 1)
2106 : {
2107 0 : dimSemantics[0] = 'S';
2108 0 : nLines = 1;
2109 0 : nSamples = anElements[0];
2110 : }
2111 234 : else if (nDIM == 2)
2112 : {
2113 6 : if (iLineIdx < 0 || iSampleIdx < 0)
2114 : {
2115 0 : CPLDebug("PDS4", "Assume that axis %s is Line",
2116 0 : axisNames[0].c_str());
2117 0 : CPLDebug("PDS4", "Assume that axis %s is Sample",
2118 0 : axisNames[1].c_str());
2119 0 : iLineIdx = 0;
2120 0 : iSampleIdx = 1;
2121 : }
2122 6 : CPLAssert(iLineIdx >= 0 && iLineIdx < 2);
2123 6 : CPLAssert(iSampleIdx >= 0 && iSampleIdx < 2);
2124 6 : dimSemantics[iLineIdx] = 'L';
2125 6 : dimSemantics[iSampleIdx] = 'S';
2126 6 : nLines = anElements[iLineIdx];
2127 6 : nSamples = anElements[iSampleIdx];
2128 : }
2129 : else /* if (nDim == 3) */
2130 : {
2131 228 : if (iLineIdx < 0 || iSampleIdx < 0)
2132 : {
2133 0 : CPLDebug("PDS4", "Assume that axis %s is Band",
2134 0 : axisNames[0].c_str());
2135 0 : CPLDebug("PDS4", "Assume that axis %s is Line",
2136 0 : axisNames[1].c_str());
2137 0 : CPLDebug("PDS4", "Assume that axis %s is Sample",
2138 0 : axisNames[2].c_str());
2139 0 : iBandIdx = 0;
2140 0 : iLineIdx = 1;
2141 0 : iSampleIdx = 2;
2142 : }
2143 228 : else if (iBandIdx < 0)
2144 : {
2145 1 : CPLAssert(iLineIdx >= 0 && iLineIdx < 3);
2146 1 : CPLAssert(iSampleIdx >= 0 && iSampleIdx < 3);
2147 1 : bool abUsedIndices[3] = {false, false, false};
2148 1 : abUsedIndices[iLineIdx] = true;
2149 1 : abUsedIndices[iSampleIdx] = true;
2150 4 : for (int i = 0; i < 3; ++i)
2151 : {
2152 3 : if (!abUsedIndices[i])
2153 1 : iBandIdx = i;
2154 : }
2155 : }
2156 228 : CPLAssert(iLineIdx >= 0 && iLineIdx < 3);
2157 228 : CPLAssert(iSampleIdx >= 0 && iSampleIdx < 3);
2158 228 : CPLAssert(iBandIdx >= 0 && iSampleIdx < 3);
2159 228 : dimSemantics[iBandIdx] = 'B';
2160 228 : dimSemantics[iLineIdx] = 'L';
2161 228 : dimSemantics[iSampleIdx] = 'S';
2162 228 : l_nBands = anElements[iBandIdx];
2163 228 : nLines = anElements[iLineIdx];
2164 228 : nSamples = anElements[iSampleIdx];
2165 : }
2166 :
2167 468 : if (!GDALCheckDatasetDimensions(nSamples, nLines) ||
2168 234 : !GDALCheckBandCount(l_nBands, FALSE))
2169 : {
2170 1 : continue;
2171 : }
2172 :
2173 : // Compute pixel, line and band spacing
2174 233 : vsi_l_offset nSpacing = GDALGetDataTypeSizeBytes(eDT);
2175 233 : int nPixelOffset = 0;
2176 233 : int nLineOffset = 0;
2177 233 : vsi_l_offset nBandOffset = 0;
2178 233 : int nCountPreviousDim = 1;
2179 924 : for (int i = nDIM - 1; i >= 0; i--)
2180 : {
2181 693 : if (dimSemantics[i] == 'S')
2182 : {
2183 233 : if (nCountPreviousDim > 0 &&
2184 233 : nSpacing > static_cast<vsi_l_offset>(INT_MAX /
2185 : nCountPreviousDim))
2186 : {
2187 1 : CPLError(CE_Failure, CPLE_NotSupported,
2188 : "Integer overflow");
2189 1 : return nullptr;
2190 : }
2191 232 : nPixelOffset =
2192 232 : static_cast<int>(nSpacing * nCountPreviousDim);
2193 232 : nSpacing = nPixelOffset;
2194 : }
2195 460 : else if (dimSemantics[i] == 'L')
2196 : {
2197 233 : if (nCountPreviousDim > 0 &&
2198 233 : nSpacing > static_cast<vsi_l_offset>(INT_MAX /
2199 : nCountPreviousDim))
2200 : {
2201 1 : CPLError(CE_Failure, CPLE_NotSupported,
2202 : "Integer overflow");
2203 1 : return nullptr;
2204 : }
2205 232 : nLineOffset =
2206 232 : static_cast<int>(nSpacing * nCountPreviousDim);
2207 232 : nSpacing = nLineOffset;
2208 : }
2209 : else
2210 : {
2211 227 : nBandOffset = nSpacing * nCountPreviousDim;
2212 227 : nSpacing = nBandOffset;
2213 : }
2214 691 : nCountPreviousDim = anElements[i];
2215 : }
2216 :
2217 : // Retrieve no data value
2218 231 : bool bNoDataSet = false;
2219 231 : double dfNoData = 0.0;
2220 231 : bool bNoDataSetInt64 = false;
2221 231 : int64_t nNoDataInt64 = 0;
2222 231 : bool bNoDataSetUInt64 = false;
2223 231 : int64_t nNoDataUInt64 = 0;
2224 231 : std::vector<double> adfConstants;
2225 231 : CPLXMLNode *psSC = CPLGetXMLNode(psSubIter, "Special_Constants");
2226 231 : if (psSC)
2227 : {
2228 : const auto GetNoDataFromConstant =
2229 76 : [psSC, eDT, &bNoDataSetInt64, &nNoDataInt64,
2230 : &bNoDataSetUInt64, &nNoDataUInt64, &bNoDataSet,
2231 372 : &dfNoData](const char *pszConstantName)
2232 : {
2233 76 : if (const char *pszVal =
2234 76 : CPLGetXMLValue(psSC, pszConstantName, nullptr))
2235 : {
2236 75 : if (eDT == GDT_Int64)
2237 : {
2238 2 : bNoDataSetInt64 = true;
2239 2 : nNoDataInt64 = std::strtoll(pszVal, nullptr, 10);
2240 : }
2241 73 : else if (eDT == GDT_UInt64)
2242 : {
2243 2 : bNoDataSetUInt64 = true;
2244 2 : nNoDataUInt64 = std::strtoull(pszVal, nullptr, 10);
2245 : }
2246 : else
2247 : {
2248 : auto val =
2249 71 : ConstantToDouble(pszConstantName, pszVal);
2250 71 : if (val)
2251 : {
2252 71 : bNoDataSet = true;
2253 71 : dfNoData = *val;
2254 : }
2255 : }
2256 75 : return true;
2257 : }
2258 1 : return false;
2259 75 : };
2260 :
2261 75 : if (!GetNoDataFromConstant("missing_constant"))
2262 : {
2263 : // For example in https://d34uoeqxvp7znu.cloudfront.net/data/l2/200908/20090811/m3g20090811t012112_l2.xml
2264 1 : GetNoDataFromConstant("invalid_constant");
2265 : }
2266 :
2267 75 : const char *const apszConstantNames[] = {
2268 : "saturated_constant",
2269 : "missing_constant",
2270 : "error_constant",
2271 : "invalid_constant",
2272 : "unknown_constant",
2273 : "not_applicable_constant",
2274 : "high_instrument_saturation",
2275 : "high_representation_saturation",
2276 : "low_instrument_saturation",
2277 : "low_representation_saturation"};
2278 825 : for (const char *pszItem : apszConstantNames)
2279 : {
2280 750 : if (const char *pszConstant =
2281 750 : CPLGetXMLValue(psSC, pszItem, nullptr))
2282 : {
2283 109 : auto val = ConstantToDouble(pszItem, pszConstant);
2284 109 : if (val)
2285 : {
2286 109 : adfConstants.push_back(*val);
2287 : }
2288 : }
2289 : }
2290 : }
2291 :
2292 : // Add subdatasets
2293 231 : const int nSDSIdx = 1 + aosSubdatasets.size() / 2;
2294 : aosSubdatasets.SetNameValue(
2295 : CPLSPrintf("SUBDATASET_%d_NAME", nSDSIdx),
2296 : CPLSPrintf("PDS4:%s:%d:%d", osXMLFilename.c_str(), nFAOIdx,
2297 231 : nArrayIdx));
2298 : aosSubdatasets.SetNameValue(
2299 : CPLSPrintf("SUBDATASET_%d_DESC", nSDSIdx),
2300 : CPLSPrintf("Image file %s, array %s", pszFilename,
2301 : pszArrayName ? pszArrayName
2302 231 : : pszArrayId ? pszArrayId
2303 462 : : CPLSPrintf("%d", nArrayIdx)));
2304 :
2305 231 : if (poDS->nBands != 0)
2306 14 : continue;
2307 :
2308 : const std::string osImageFullFilename = CPLFormFilenameSafe(
2309 217 : CPLGetPathSafe(osXMLFilename.c_str()).c_str(), pszFilename,
2310 217 : nullptr);
2311 217 : if (CPLHasPathTraversal(pszFilename))
2312 : {
2313 0 : CPLError(CE_Failure, CPLE_NotSupported,
2314 : "Path traversal detected in %s", pszFilename);
2315 0 : return nullptr;
2316 : }
2317 217 : VSILFILE *fp = VSIFOpenExL(
2318 : osImageFullFilename.c_str(),
2319 217 : (poOpenInfo->eAccess == GA_Update) ? "rb+" : "rb", true);
2320 217 : if (fp == nullptr)
2321 : {
2322 1 : CPLError(CE_Warning, CPLE_FileIO, "Cannot open %s: %s",
2323 : osImageFullFilename.c_str(), VSIGetLastErrorMsg());
2324 1 : continue;
2325 : }
2326 216 : poDS->nRasterXSize = nSamples;
2327 216 : poDS->nRasterYSize = nLines;
2328 216 : poDS->m_osImageFilename = osImageFullFilename;
2329 216 : poDS->m_fpImage = fp;
2330 216 : poDS->m_bIsLSB = bLSBOrder;
2331 :
2332 35 : if (l_nBands > 1 && dimSemantics[0] == 'B' &&
2333 251 : dimSemantics[1] == 'L' && dimSemantics[2] == 'S')
2334 : {
2335 27 : poDS->GDALDataset::SetMetadataItem("INTERLEAVE", "BAND",
2336 : "IMAGE_STRUCTURE");
2337 : }
2338 35 : if (l_nBands > 1 && dimSemantics[0] == 'L' &&
2339 251 : dimSemantics[1] == 'S' && dimSemantics[2] == 'B')
2340 : {
2341 6 : poDS->GDALDataset::SetMetadataItem("INTERLEAVE", "PIXEL",
2342 : "IMAGE_STRUCTURE");
2343 : }
2344 :
2345 216 : CPLXMLNode *psOS = CPLGetXMLNode(psSubIter, "Object_Statistics");
2346 216 : const char *pszMin = nullptr;
2347 216 : const char *pszMax = nullptr;
2348 216 : const char *pszMean = nullptr;
2349 216 : const char *pszStdDev = nullptr;
2350 216 : if (psOS)
2351 : {
2352 17 : pszMin = CPLGetXMLValue(psOS, "minimum", nullptr);
2353 17 : pszMax = CPLGetXMLValue(psOS, "maximum", nullptr);
2354 17 : pszMean = CPLGetXMLValue(psOS, "mean", nullptr);
2355 17 : pszStdDev = CPLGetXMLValue(psOS, "standard_deviation", nullptr);
2356 : }
2357 :
2358 501 : for (int i = 0; i < l_nBands; i++)
2359 : {
2360 285 : vsi_l_offset nThisBandOffset = nOffset + nBandOffset * i;
2361 285 : if (bBottomToTop)
2362 : {
2363 2 : nThisBandOffset +=
2364 2 : static_cast<vsi_l_offset>(nLines - 1) * nLineOffset;
2365 : }
2366 285 : if (bRightToLeft)
2367 : {
2368 1 : nThisBandOffset +=
2369 1 : static_cast<vsi_l_offset>(nSamples - 1) * nPixelOffset;
2370 : }
2371 : auto poBand = std::make_unique<PDS4RawRasterBand>(
2372 285 : poDS.get(), i + 1, poDS->m_fpImage, nThisBandOffset,
2373 285 : bRightToLeft ? -nPixelOffset : nPixelOffset,
2374 285 : bBottomToTop ? -nLineOffset : nLineOffset, eDT,
2375 285 : bLSBOrder ? RawRasterBand::ByteOrder::ORDER_LITTLE_ENDIAN
2376 285 : : RawRasterBand::ByteOrder::ORDER_BIG_ENDIAN);
2377 285 : if (!poBand->IsValid())
2378 0 : return nullptr;
2379 285 : if (bNoDataSet)
2380 : {
2381 75 : poBand->SetNoDataValue(dfNoData);
2382 : }
2383 210 : else if (bNoDataSetInt64)
2384 : {
2385 2 : poBand->SetNoDataValueAsInt64(nNoDataInt64);
2386 : }
2387 208 : else if (bNoDataSetUInt64)
2388 : {
2389 2 : poBand->SetNoDataValueAsUInt64(nNoDataUInt64);
2390 : }
2391 285 : poBand->SetOffset(dfValueOffset);
2392 285 : poBand->SetScale(dfValueScale);
2393 :
2394 285 : if (l_nBands == 1)
2395 : {
2396 181 : if (pszMin)
2397 : {
2398 17 : poBand->GDALRasterBand::SetMetadataItem(
2399 : "STATISTICS_MINIMUM", pszMin);
2400 : }
2401 181 : if (pszMax)
2402 : {
2403 17 : poBand->GDALRasterBand::SetMetadataItem(
2404 : "STATISTICS_MAXIMUM", pszMax);
2405 : }
2406 181 : if (pszMean)
2407 : {
2408 17 : poBand->GDALRasterBand::SetMetadataItem(
2409 : "STATISTICS_MEAN", pszMean);
2410 : }
2411 181 : if (pszStdDev)
2412 : {
2413 17 : poBand->GDALRasterBand::SetMetadataItem(
2414 : "STATISTICS_STDDEV", pszStdDev);
2415 : }
2416 : }
2417 :
2418 : // Only instantiate explicit mask band if we have at least one
2419 : // special constant (that is not the missing_constant,
2420 : // already exposed as nodata value)
2421 558 : if (!GDALDataTypeIsComplex(eDT) &&
2422 538 : (CPLTestBool(CPLGetConfigOption("PDS4_FORCE_MASK", "NO")) ||
2423 499 : adfConstants.size() >= 2 ||
2424 282 : (adfConstants.size() == 1 && !bNoDataSet)))
2425 : {
2426 86 : poBand->SetMaskBand(std::make_unique<PDS4MaskBand>(
2427 86 : poBand.get(), adfConstants));
2428 : }
2429 :
2430 285 : poDS->SetBand(i + 1, std::move(poBand));
2431 : }
2432 : }
2433 : }
2434 :
2435 300 : if (nFAOIdxLookup < 0 && aosSubdatasets.size() > 2)
2436 : {
2437 10 : poDS->GDALDataset::SetMetadata(aosSubdatasets.List(), "SUBDATASETS");
2438 : }
2439 290 : else if (poDS->nBands == 0 &&
2440 300 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
2441 10 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0)
2442 : {
2443 5 : return nullptr;
2444 : }
2445 285 : else if (poDS->m_apoLayers.empty() &&
2446 287 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
2447 2 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0)
2448 : {
2449 0 : return nullptr;
2450 : }
2451 :
2452 : // Expose XML content in xml:PDS4 metadata domain
2453 295 : GByte *pabyRet = nullptr;
2454 295 : CPL_IGNORE_RET_VAL(VSIIngestFile(nullptr, osXMLFilename, &pabyRet, nullptr,
2455 : 10 * 1024 * 1024));
2456 295 : if (pabyRet)
2457 : {
2458 295 : char *apszXML[2] = {reinterpret_cast<char *>(pabyRet), nullptr};
2459 295 : poDS->GDALDataset::SetMetadata(apszXML, "xml:PDS4");
2460 : }
2461 295 : VSIFree(pabyRet);
2462 :
2463 : /*--------------------------------------------------------------------------*/
2464 : /* Parse georeferencing info */
2465 : /*--------------------------------------------------------------------------*/
2466 295 : poDS->ReadGeoreferencing(psProduct);
2467 :
2468 : /*--------------------------------------------------------------------------*/
2469 : /* Check for overviews */
2470 : /*--------------------------------------------------------------------------*/
2471 295 : poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename);
2472 :
2473 : /*--------------------------------------------------------------------------*/
2474 : /* Initialize any PAM information */
2475 : /*--------------------------------------------------------------------------*/
2476 295 : poDS->SetDescription(poOpenInfo->pszFilename);
2477 295 : poDS->TryLoadXML();
2478 :
2479 295 : return poDS;
2480 : }
2481 :
2482 : /************************************************************************/
2483 : /* IsCARTVersionGTE() */
2484 : /************************************************************************/
2485 :
2486 : // Returns true is pszCur >= pszRef
2487 : // Must be things like 1900, 1B00, 1D00_1933 ...
2488 489 : static bool IsCARTVersionGTE(const char *pszCur, const char *pszRef)
2489 : {
2490 489 : return strcmp(pszCur, pszRef) >= 0;
2491 : }
2492 :
2493 : /************************************************************************/
2494 : /* WriteGeoreferencing() */
2495 : /************************************************************************/
2496 :
2497 98 : void PDS4Dataset::WriteGeoreferencing(CPLXMLNode *psCart,
2498 : const char *pszCARTVersion)
2499 : {
2500 98 : bool bHasBoundingBox = false;
2501 98 : double adfX[4] = {0};
2502 98 : double adfY[4] = {0};
2503 98 : CPLString osPrefix;
2504 98 : const char *pszColon = strchr(psCart->pszValue, ':');
2505 98 : if (pszColon)
2506 98 : osPrefix.assign(psCart->pszValue, pszColon - psCart->pszValue + 1);
2507 :
2508 98 : if (m_bGotTransform)
2509 : {
2510 96 : bHasBoundingBox = true;
2511 :
2512 : // upper left
2513 96 : adfX[0] = m_gt.xorig;
2514 96 : adfY[0] = m_gt.yorig;
2515 :
2516 : // upper right
2517 96 : adfX[1] = m_gt.xorig + m_gt.xscale * nRasterXSize;
2518 96 : adfY[1] = m_gt.yorig;
2519 :
2520 : // lower left
2521 96 : adfX[2] = m_gt.xorig;
2522 96 : adfY[2] = m_gt.yorig + m_gt.yscale * nRasterYSize;
2523 :
2524 : // lower right
2525 96 : adfX[3] = m_gt.xorig + m_gt.xscale * nRasterXSize;
2526 96 : adfY[3] = m_gt.yorig + m_gt.yscale * nRasterYSize;
2527 : }
2528 : else
2529 : {
2530 2 : OGRLayer *poLayer = GetLayer(0);
2531 2 : OGREnvelope sEnvelope;
2532 2 : if (poLayer->GetExtent(&sEnvelope) == OGRERR_NONE)
2533 : {
2534 1 : bHasBoundingBox = true;
2535 :
2536 1 : adfX[0] = sEnvelope.MinX;
2537 1 : adfY[0] = sEnvelope.MaxY;
2538 :
2539 1 : adfX[1] = sEnvelope.MaxX;
2540 1 : adfY[1] = sEnvelope.MaxY;
2541 :
2542 1 : adfX[2] = sEnvelope.MinX;
2543 1 : adfY[2] = sEnvelope.MinY;
2544 :
2545 1 : adfX[3] = sEnvelope.MaxX;
2546 1 : adfY[3] = sEnvelope.MinY;
2547 : }
2548 : }
2549 :
2550 98 : if (bHasBoundingBox && !m_oSRS.IsGeographic())
2551 : {
2552 33 : bHasBoundingBox = false;
2553 33 : OGRSpatialReference *poSRSLongLat = m_oSRS.CloneGeogCS();
2554 33 : if (poSRSLongLat)
2555 : {
2556 33 : poSRSLongLat->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2557 : OGRCoordinateTransformation *poCT =
2558 33 : OGRCreateCoordinateTransformation(&m_oSRS, poSRSLongLat);
2559 33 : if (poCT)
2560 : {
2561 33 : if (poCT->Transform(4, adfX, adfY))
2562 : {
2563 33 : bHasBoundingBox = true;
2564 : }
2565 33 : delete poCT;
2566 : }
2567 33 : delete poSRSLongLat;
2568 : }
2569 : }
2570 :
2571 98 : if (!bHasBoundingBox)
2572 : {
2573 : // Write dummy values
2574 1 : adfX[0] = -180.0;
2575 1 : adfY[0] = 90.0;
2576 1 : adfX[1] = 180.0;
2577 1 : adfY[1] = 90.0;
2578 1 : adfX[2] = -180.0;
2579 1 : adfY[2] = -90.0;
2580 1 : adfX[3] = 180.0;
2581 1 : adfY[3] = -90.0;
2582 : }
2583 :
2584 196 : const char *pszLongitudeDirection = CSLFetchNameValueDef(
2585 98 : m_papszCreationOptions, "LONGITUDE_DIRECTION", "Positive East");
2586 98 : const double dfLongitudeMultiplier =
2587 98 : EQUAL(pszLongitudeDirection, "Positive West") ? -1 : 1;
2588 212 : const auto FixLong = [dfLongitudeMultiplier](double dfLon)
2589 212 : { return dfLon * dfLongitudeMultiplier; };
2590 :
2591 : // Note: starting with CART 1900, Spatial_Domain is actually optional
2592 98 : CPLXMLNode *psSD = CPLCreateXMLNode(psCart, CXT_Element,
2593 196 : (osPrefix + "Spatial_Domain").c_str());
2594 98 : CPLXMLNode *psBC = CPLCreateXMLNode(
2595 196 : psSD, CXT_Element, (osPrefix + "Bounding_Coordinates").c_str());
2596 :
2597 : const char *pszBoundingDegrees =
2598 98 : CSLFetchNameValue(m_papszCreationOptions, "BOUNDING_DEGREES");
2599 98 : double dfWest = FixLong(
2600 98 : std::min(std::min(adfX[0], adfX[1]), std::min(adfX[2], adfX[3])));
2601 98 : double dfEast = FixLong(
2602 98 : std::max(std::max(adfX[0], adfX[1]), std::max(adfX[2], adfX[3])));
2603 : double dfNorth =
2604 98 : std::max(std::max(adfY[0], adfY[1]), std::max(adfY[2], adfY[3]));
2605 : double dfSouth =
2606 98 : std::min(std::min(adfY[0], adfY[1]), std::min(adfY[2], adfY[3]));
2607 98 : if (pszBoundingDegrees)
2608 : {
2609 1 : char **papszTokens = CSLTokenizeString2(pszBoundingDegrees, ",", 0);
2610 1 : if (CSLCount(papszTokens) == 4)
2611 : {
2612 1 : dfWest = CPLAtof(papszTokens[0]);
2613 1 : dfSouth = CPLAtof(papszTokens[1]);
2614 1 : dfEast = CPLAtof(papszTokens[2]);
2615 1 : dfNorth = CPLAtof(papszTokens[3]);
2616 : }
2617 1 : CSLDestroy(papszTokens);
2618 : }
2619 :
2620 196 : CPLAddXMLAttributeAndValue(
2621 : CPLCreateXMLElementAndValue(
2622 196 : psBC, (osPrefix + "west_bounding_coordinate").c_str(),
2623 : CPLSPrintf("%.17g", dfWest)),
2624 : "unit", "deg");
2625 196 : CPLAddXMLAttributeAndValue(
2626 : CPLCreateXMLElementAndValue(
2627 196 : psBC, (osPrefix + "east_bounding_coordinate").c_str(),
2628 : CPLSPrintf("%.17g", dfEast)),
2629 : "unit", "deg");
2630 196 : CPLAddXMLAttributeAndValue(
2631 : CPLCreateXMLElementAndValue(
2632 196 : psBC, (osPrefix + "north_bounding_coordinate").c_str(),
2633 : CPLSPrintf("%.17g", dfNorth)),
2634 : "unit", "deg");
2635 196 : CPLAddXMLAttributeAndValue(
2636 : CPLCreateXMLElementAndValue(
2637 196 : psBC, (osPrefix + "south_bounding_coordinate").c_str(),
2638 : CPLSPrintf("%.17g", dfSouth)),
2639 : "unit", "deg");
2640 :
2641 : CPLXMLNode *psSRI =
2642 98 : CPLCreateXMLNode(psCart, CXT_Element,
2643 196 : (osPrefix + "Spatial_Reference_Information").c_str());
2644 98 : CPLXMLNode *psHCSD = CPLCreateXMLNode(
2645 : psSRI, CXT_Element,
2646 196 : (osPrefix + "Horizontal_Coordinate_System_Definition").c_str());
2647 :
2648 98 : double dfUnrotatedULX = m_gt.xorig;
2649 98 : double dfUnrotatedULY = m_gt.yorig;
2650 98 : double dfUnrotatedResX = m_gt.xscale;
2651 98 : double dfUnrotatedResY = m_gt.yscale;
2652 98 : double dfMapProjectionRotation = 0.0;
2653 98 : if (m_gt.xscale == 0.0 && m_gt.xrot > 0.0 && m_gt.yrot > 0.0 &&
2654 1 : m_gt.yscale == 0.0)
2655 : {
2656 1 : dfUnrotatedULX = m_gt.yorig;
2657 1 : dfUnrotatedULY = -m_gt.xorig;
2658 1 : dfUnrotatedResX = m_gt.yrot;
2659 1 : dfUnrotatedResY = -m_gt.xrot;
2660 1 : dfMapProjectionRotation = 90.0;
2661 : }
2662 :
2663 98 : if (GetRasterCount() || m_oSRS.IsProjected())
2664 : {
2665 97 : CPLXMLNode *psPlanar = CPLCreateXMLNode(psHCSD, CXT_Element,
2666 194 : (osPrefix + "Planar").c_str());
2667 97 : CPLXMLNode *psMP = CPLCreateXMLNode(
2668 194 : psPlanar, CXT_Element, (osPrefix + "Map_Projection").c_str());
2669 97 : const char *pszProjection = m_oSRS.GetAttrValue("PROJECTION");
2670 194 : CPLString pszPDS4ProjectionName = "";
2671 : typedef std::pair<const char *, double> ProjParam;
2672 194 : std::vector<ProjParam> aoProjParams;
2673 :
2674 : const bool bUse_CART_1933_Or_Later =
2675 97 : IsCARTVersionGTE(pszCARTVersion, "1D00_1933");
2676 :
2677 : const bool bUse_CART_1950_Or_Later =
2678 97 : IsCARTVersionGTE(pszCARTVersion, "1G00_1950");
2679 :
2680 : const bool bUse_CART_1970_Or_Later =
2681 97 : IsCARTVersionGTE(pszCARTVersion, "1O00_1970");
2682 :
2683 97 : if (pszProjection == nullptr)
2684 : {
2685 63 : pszPDS4ProjectionName = "Equirectangular";
2686 63 : if (bUse_CART_1933_Or_Later)
2687 : {
2688 61 : aoProjParams.push_back(
2689 61 : ProjParam("latitude_of_projection_origin", 0.0));
2690 61 : aoProjParams.push_back(ProjParam("standard_parallel_1", 0.0));
2691 61 : aoProjParams.push_back(
2692 122 : ProjParam("longitude_of_central_meridian", 0.0));
2693 : }
2694 : else
2695 : {
2696 2 : aoProjParams.push_back(ProjParam("standard_parallel_1", 0.0));
2697 2 : aoProjParams.push_back(
2698 2 : ProjParam("longitude_of_central_meridian", 0.0));
2699 2 : aoProjParams.push_back(
2700 4 : ProjParam("latitude_of_projection_origin", 0.0));
2701 : }
2702 : }
2703 :
2704 34 : else if (EQUAL(pszProjection, SRS_PT_EQUIRECTANGULAR))
2705 : {
2706 2 : pszPDS4ProjectionName = "Equirectangular";
2707 2 : if (bUse_CART_1933_Or_Later)
2708 : {
2709 2 : aoProjParams.push_back(ProjParam(
2710 0 : "latitude_of_projection_origin",
2711 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2712 2 : aoProjParams.push_back(ProjParam(
2713 0 : "standard_parallel_1",
2714 2 : m_oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, 1.0)));
2715 2 : aoProjParams.push_back(
2716 0 : ProjParam("longitude_of_central_meridian",
2717 4 : FixLong(m_oSRS.GetNormProjParm(
2718 : SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2719 : }
2720 : else
2721 : {
2722 0 : aoProjParams.push_back(ProjParam(
2723 0 : "standard_parallel_1",
2724 0 : m_oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, 1.0)));
2725 0 : aoProjParams.push_back(
2726 0 : ProjParam("longitude_of_central_meridian",
2727 0 : FixLong(m_oSRS.GetNormProjParm(
2728 : SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2729 0 : aoProjParams.push_back(ProjParam(
2730 0 : "latitude_of_projection_origin",
2731 0 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2732 : }
2733 : }
2734 :
2735 32 : else if (EQUAL(pszProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP))
2736 : {
2737 1 : pszPDS4ProjectionName = "Lambert Conformal Conic";
2738 1 : if (bUse_CART_1933_Or_Later)
2739 : {
2740 1 : if (bUse_CART_1970_Or_Later)
2741 : {
2742 : // Note: in EPSG (and PROJ "metadata" part), there is
2743 : // no standard_parallel_1 parameter for LCC_1SP. But
2744 : // for historical reason we do map the latitude of origin
2745 : // to +lat_1 (in addition to +lat_0). So do the same here.
2746 1 : aoProjParams.push_back(
2747 0 : ProjParam("standard_parallel_1",
2748 2 : m_oSRS.GetNormProjParm(
2749 : SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2750 : }
2751 1 : aoProjParams.push_back(
2752 0 : ProjParam("longitude_of_central_meridian",
2753 1 : FixLong(m_oSRS.GetNormProjParm(
2754 : SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2755 1 : aoProjParams.push_back(ProjParam(
2756 0 : "latitude_of_projection_origin",
2757 1 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2758 1 : aoProjParams.push_back(ProjParam(
2759 0 : "scale_factor_at_projection_origin",
2760 2 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0)));
2761 : }
2762 : else
2763 : {
2764 0 : aoProjParams.push_back(ProjParam(
2765 0 : "scale_factor_at_projection_origin",
2766 0 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0)));
2767 0 : aoProjParams.push_back(
2768 0 : ProjParam("longitude_of_central_meridian",
2769 0 : FixLong(m_oSRS.GetNormProjParm(
2770 : SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2771 0 : aoProjParams.push_back(ProjParam(
2772 0 : "latitude_of_projection_origin",
2773 0 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2774 : }
2775 : }
2776 :
2777 31 : else if (EQUAL(pszProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP))
2778 : {
2779 1 : pszPDS4ProjectionName = "Lambert Conformal Conic";
2780 1 : aoProjParams.push_back(ProjParam(
2781 0 : "standard_parallel_1",
2782 1 : m_oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, 0.0)));
2783 1 : aoProjParams.push_back(ProjParam(
2784 0 : "standard_parallel_2",
2785 1 : m_oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_2, 0.0)));
2786 1 : aoProjParams.push_back(ProjParam(
2787 0 : "longitude_of_central_meridian",
2788 1 : FixLong(m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2789 1 : aoProjParams.push_back(ProjParam(
2790 0 : "latitude_of_projection_origin",
2791 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2792 : }
2793 :
2794 30 : else if (EQUAL(pszProjection,
2795 : SRS_PT_HOTINE_OBLIQUE_MERCATOR_AZIMUTH_CENTER))
2796 : {
2797 1 : pszPDS4ProjectionName = "Oblique Mercator";
2798 : // Proj params defined later
2799 : }
2800 :
2801 29 : else if (EQUAL(pszProjection,
2802 : SRS_PT_HOTINE_OBLIQUE_MERCATOR_TWO_POINT_NATURAL_ORIGIN))
2803 : {
2804 1 : pszPDS4ProjectionName = "Oblique Mercator";
2805 : // Proj params defined later
2806 : }
2807 :
2808 28 : else if (EQUAL(pszProjection, SRS_PT_POLAR_STEREOGRAPHIC))
2809 : {
2810 1 : pszPDS4ProjectionName = "Polar Stereographic";
2811 1 : if (bUse_CART_1950_Or_Later)
2812 : {
2813 1 : aoProjParams.push_back(
2814 0 : ProjParam("longitude_of_central_meridian",
2815 1 : FixLong(m_oSRS.GetNormProjParm(
2816 : SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2817 1 : aoProjParams.push_back(ProjParam(
2818 0 : "latitude_of_projection_origin",
2819 1 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2820 1 : aoProjParams.push_back(ProjParam(
2821 0 : "scale_factor_at_projection_origin",
2822 2 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0)));
2823 : }
2824 : else
2825 : {
2826 0 : aoProjParams.push_back(
2827 0 : ProjParam(bUse_CART_1933_Or_Later
2828 0 : ? "longitude_of_central_meridian"
2829 : : "straight_vertical_longitude_from_pole",
2830 0 : FixLong(m_oSRS.GetNormProjParm(
2831 : SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2832 0 : aoProjParams.push_back(ProjParam(
2833 0 : "scale_factor_at_projection_origin",
2834 0 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0)));
2835 0 : aoProjParams.push_back(ProjParam(
2836 0 : "latitude_of_projection_origin",
2837 0 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2838 : }
2839 : }
2840 :
2841 27 : else if (EQUAL(pszProjection, SRS_PT_POLYCONIC))
2842 : {
2843 1 : pszPDS4ProjectionName = "Polyconic";
2844 1 : aoProjParams.push_back(ProjParam(
2845 0 : "longitude_of_central_meridian",
2846 1 : m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0)));
2847 1 : aoProjParams.push_back(ProjParam(
2848 0 : "latitude_of_projection_origin",
2849 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2850 : }
2851 26 : else if (EQUAL(pszProjection, SRS_PT_SINUSOIDAL))
2852 : {
2853 2 : pszPDS4ProjectionName = "Sinusoidal";
2854 2 : aoProjParams.push_back(ProjParam(
2855 0 : "longitude_of_central_meridian",
2856 2 : FixLong(m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2857 2 : aoProjParams.push_back(ProjParam(
2858 0 : "latitude_of_projection_origin",
2859 4 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2860 : }
2861 :
2862 24 : else if (EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR))
2863 : {
2864 18 : pszPDS4ProjectionName = "Transverse Mercator";
2865 18 : aoProjParams.push_back(
2866 0 : ProjParam("scale_factor_at_central_meridian",
2867 18 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0)));
2868 18 : aoProjParams.push_back(ProjParam(
2869 0 : "longitude_of_central_meridian",
2870 18 : m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0)));
2871 18 : aoProjParams.push_back(ProjParam(
2872 0 : "latitude_of_projection_origin",
2873 36 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2874 : }
2875 6 : else if (EQUAL(pszProjection, SRS_PT_ORTHOGRAPHIC))
2876 : {
2877 1 : pszPDS4ProjectionName = "Orthographic";
2878 1 : aoProjParams.push_back(ProjParam(
2879 0 : "longitude_of_central_meridian",
2880 1 : FixLong(m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2881 1 : aoProjParams.push_back(ProjParam(
2882 0 : "latitude_of_projection_origin",
2883 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2884 : }
2885 :
2886 5 : else if (EQUAL(pszProjection, SRS_PT_MERCATOR_1SP))
2887 : {
2888 2 : pszPDS4ProjectionName = "Mercator";
2889 2 : aoProjParams.push_back(ProjParam(
2890 0 : "longitude_of_central_meridian",
2891 2 : FixLong(m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2892 2 : aoProjParams.push_back(ProjParam(
2893 0 : "latitude_of_projection_origin",
2894 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2895 2 : aoProjParams.push_back(
2896 0 : ProjParam("scale_factor_at_projection_origin",
2897 4 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0)));
2898 : }
2899 :
2900 3 : else if (EQUAL(pszProjection, SRS_PT_MERCATOR_2SP))
2901 : {
2902 1 : pszPDS4ProjectionName = "Mercator";
2903 1 : aoProjParams.push_back(ProjParam(
2904 0 : "standard_parallel_1",
2905 1 : m_oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, 0.0)));
2906 1 : aoProjParams.push_back(ProjParam(
2907 0 : "longitude_of_central_meridian",
2908 1 : FixLong(m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2909 1 : aoProjParams.push_back(ProjParam(
2910 0 : "latitude_of_projection_origin",
2911 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2912 : }
2913 :
2914 2 : else if (EQUAL(pszProjection, SRS_PT_LAMBERT_AZIMUTHAL_EQUAL_AREA))
2915 : {
2916 1 : pszPDS4ProjectionName = "Lambert Azimuthal Equal Area";
2917 1 : aoProjParams.push_back(ProjParam(
2918 0 : "longitude_of_central_meridian",
2919 1 : FixLong(m_oSRS.GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0))));
2920 1 : aoProjParams.push_back(ProjParam(
2921 0 : "latitude_of_projection_origin",
2922 2 : m_oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0)));
2923 : }
2924 :
2925 1 : else if (EQUAL(pszProjection, "custom_proj4"))
2926 : {
2927 : const char *pszProj4 =
2928 1 : m_oSRS.GetExtension("PROJCS", "PROJ4", nullptr);
2929 1 : if (pszProj4 && strstr(pszProj4, "+proj=ob_tran") &&
2930 1 : strstr(pszProj4, "+o_proj=eqc"))
2931 : {
2932 1 : pszPDS4ProjectionName = "Oblique Cylindrical";
2933 : const auto FetchParam =
2934 3 : [](const char *pszProj4Str, const char *pszKey)
2935 : {
2936 6 : CPLString needle;
2937 3 : needle.Printf("+%s=", pszKey);
2938 3 : const char *pszVal = strstr(pszProj4Str, needle.c_str());
2939 3 : if (pszVal)
2940 3 : return CPLAtof(pszVal + needle.size());
2941 0 : return 0.0;
2942 : };
2943 :
2944 1 : double dfLonP = FetchParam(pszProj4, "o_lon_p");
2945 1 : double dfLatP = FetchParam(pszProj4, "o_lat_p");
2946 1 : double dfLon0 = FetchParam(pszProj4, "lon_0");
2947 1 : double dfPoleRotation = -dfLonP;
2948 1 : double dfPoleLatitude = 180 - dfLatP;
2949 1 : double dfPoleLongitude = dfLon0;
2950 :
2951 1 : aoProjParams.push_back(ProjParam("map_projection_rotation",
2952 : dfMapProjectionRotation));
2953 1 : aoProjParams.push_back(
2954 1 : ProjParam("oblique_proj_pole_latitude", dfPoleLatitude));
2955 1 : aoProjParams.push_back(ProjParam("oblique_proj_pole_longitude",
2956 1 : FixLong(dfPoleLongitude)));
2957 1 : aoProjParams.push_back(
2958 2 : ProjParam("oblique_proj_pole_rotation", dfPoleRotation));
2959 : }
2960 : else
2961 : {
2962 0 : CPLError(CE_Warning, CPLE_NotSupported,
2963 : "Projection %s not supported", pszProjection);
2964 : }
2965 : }
2966 : else
2967 : {
2968 0 : CPLError(CE_Warning, CPLE_NotSupported,
2969 : "Projection %s not supported", pszProjection);
2970 : }
2971 194 : CPLCreateXMLElementAndValue(psMP,
2972 194 : (osPrefix + "map_projection_name").c_str(),
2973 : pszPDS4ProjectionName);
2974 97 : CPLXMLNode *psProj = CPLCreateXMLNode(
2975 : psMP, CXT_Element,
2976 194 : CPLString(osPrefix + pszPDS4ProjectionName).replaceAll(' ', '_'));
2977 380 : for (size_t i = 0; i < aoProjParams.size(); i++)
2978 : {
2979 566 : CPLXMLNode *psParam = CPLCreateXMLElementAndValue(
2980 566 : psProj, (osPrefix + aoProjParams[i].first).c_str(),
2981 283 : CPLSPrintf("%.17g", aoProjParams[i].second));
2982 283 : if (!STARTS_WITH(aoProjParams[i].first, "scale_factor"))
2983 : {
2984 261 : CPLAddXMLAttributeAndValue(psParam, "unit", "deg");
2985 : }
2986 : }
2987 :
2988 97 : if (pszProjection &&
2989 34 : EQUAL(pszProjection, SRS_PT_HOTINE_OBLIQUE_MERCATOR_AZIMUTH_CENTER))
2990 : {
2991 : CPLXMLNode *psOLA =
2992 1 : CPLCreateXMLNode(nullptr, CXT_Element,
2993 2 : (osPrefix + "Oblique_Line_Azimuth").c_str());
2994 2 : CPLAddXMLAttributeAndValue(
2995 : CPLCreateXMLElementAndValue(
2996 2 : psOLA, (osPrefix + "azimuthal_angle").c_str(),
2997 : CPLSPrintf("%.17g",
2998 : m_oSRS.GetNormProjParm(SRS_PP_AZIMUTH, 0.0))),
2999 : "unit", "deg");
3000 : ;
3001 : // Not completely sure of this
3002 2 : CPLAddXMLAttributeAndValue(
3003 : CPLCreateXMLElementAndValue(
3004 : psOLA,
3005 2 : (osPrefix + "azimuth_measure_point_longitude").c_str(),
3006 : CPLSPrintf("%.17g", FixLong(m_oSRS.GetNormProjParm(
3007 : SRS_PP_CENTRAL_MERIDIAN, 0.0)))),
3008 : "unit", "deg");
3009 :
3010 1 : if (bUse_CART_1933_Or_Later)
3011 : {
3012 1 : CPLAddXMLChild(psProj, psOLA);
3013 :
3014 1 : CPLAddXMLAttributeAndValue(
3015 : CPLCreateXMLElementAndValue(
3016 : psProj,
3017 2 : (osPrefix + "longitude_of_central_meridian").c_str(),
3018 : "0"),
3019 : "unit", "deg");
3020 :
3021 : const double dfScaleFactor =
3022 1 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 0.0);
3023 1 : if (dfScaleFactor != 1.0)
3024 : {
3025 0 : CPLError(CE_Warning, CPLE_NotSupported,
3026 : "Scale factor on initial support = %.17g cannot "
3027 : "be encoded in PDS4",
3028 : dfScaleFactor);
3029 : }
3030 : }
3031 : else
3032 : {
3033 0 : CPLCreateXMLElementAndValue(
3034 : psProj,
3035 0 : (osPrefix + "scale_factor_at_projection_origin").c_str(),
3036 : CPLSPrintf("%.17g", m_oSRS.GetNormProjParm(
3037 : SRS_PP_SCALE_FACTOR, 0.0)));
3038 :
3039 0 : CPLAddXMLChild(psProj, psOLA);
3040 : }
3041 :
3042 2 : CPLAddXMLAttributeAndValue(
3043 : CPLCreateXMLElementAndValue(
3044 : psProj,
3045 2 : (osPrefix + "latitude_of_projection_origin").c_str(),
3046 : CPLSPrintf("%.17g", m_oSRS.GetNormProjParm(
3047 : SRS_PP_LATITUDE_OF_ORIGIN, 0.0))),
3048 1 : "unit", "deg");
3049 : }
3050 96 : else if (pszProjection &&
3051 33 : EQUAL(pszProjection,
3052 : SRS_PT_HOTINE_OBLIQUE_MERCATOR_TWO_POINT_NATURAL_ORIGIN))
3053 : {
3054 1 : if (bUse_CART_1933_Or_Later)
3055 : {
3056 : const double dfScaleFactor =
3057 1 : m_oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 0.0);
3058 1 : if (dfScaleFactor != 1.0)
3059 : {
3060 0 : CPLError(CE_Warning, CPLE_NotSupported,
3061 : "Scale factor on initial support = %.17g cannot "
3062 : "be encoded in PDS4",
3063 : dfScaleFactor);
3064 : }
3065 : }
3066 : else
3067 : {
3068 0 : CPLCreateXMLElementAndValue(
3069 : psProj,
3070 0 : (osPrefix + "scale_factor_at_projection_origin").c_str(),
3071 : CPLSPrintf("%.17g", m_oSRS.GetNormProjParm(
3072 : SRS_PP_SCALE_FACTOR, 0.0)));
3073 : }
3074 :
3075 1 : CPLXMLNode *psOLP = CPLCreateXMLNode(
3076 2 : psProj, CXT_Element, (osPrefix + "Oblique_Line_Point").c_str());
3077 1 : CPLXMLNode *psOLPG1 = CPLCreateXMLNode(
3078 : psOLP, CXT_Element,
3079 2 : (osPrefix + "Oblique_Line_Point_Group").c_str());
3080 2 : CPLAddXMLAttributeAndValue(
3081 : CPLCreateXMLElementAndValue(
3082 2 : psOLPG1, (osPrefix + "oblique_line_latitude").c_str(),
3083 : CPLSPrintf("%.17g", m_oSRS.GetNormProjParm(
3084 : SRS_PP_LATITUDE_OF_POINT_1, 0.0))),
3085 : "unit", "deg");
3086 2 : CPLAddXMLAttributeAndValue(
3087 : CPLCreateXMLElementAndValue(
3088 2 : psOLPG1, (osPrefix + "oblique_line_longitude").c_str(),
3089 : CPLSPrintf("%.17g",
3090 : FixLong(m_oSRS.GetNormProjParm(
3091 : SRS_PP_LONGITUDE_OF_POINT_1, 0.0)))),
3092 : "unit", "deg");
3093 1 : CPLXMLNode *psOLPG2 = CPLCreateXMLNode(
3094 : psOLP, CXT_Element,
3095 2 : (osPrefix + "Oblique_Line_Point_Group").c_str());
3096 2 : CPLAddXMLAttributeAndValue(
3097 : CPLCreateXMLElementAndValue(
3098 2 : psOLPG2, (osPrefix + "oblique_line_latitude").c_str(),
3099 : CPLSPrintf("%.17g", m_oSRS.GetNormProjParm(
3100 : SRS_PP_LATITUDE_OF_POINT_2, 0.0))),
3101 : "unit", "deg");
3102 2 : CPLAddXMLAttributeAndValue(
3103 : CPLCreateXMLElementAndValue(
3104 2 : psOLPG2, (osPrefix + "oblique_line_longitude").c_str(),
3105 : CPLSPrintf("%.17g", m_oSRS.GetNormProjParm(
3106 : SRS_PP_LONGITUDE_OF_POINT_2, 0.0))),
3107 : "unit", "deg");
3108 :
3109 1 : if (bUse_CART_1933_Or_Later)
3110 : {
3111 1 : CPLAddXMLAttributeAndValue(
3112 : CPLCreateXMLElementAndValue(
3113 : psProj,
3114 2 : (osPrefix + "longitude_of_central_meridian").c_str(),
3115 : "0"),
3116 : "unit", "deg");
3117 : }
3118 :
3119 2 : CPLAddXMLAttributeAndValue(
3120 : CPLCreateXMLElementAndValue(
3121 : psProj,
3122 2 : (osPrefix + "latitude_of_projection_origin").c_str(),
3123 : CPLSPrintf("%.17g", FixLong(m_oSRS.GetNormProjParm(
3124 : SRS_PP_LATITUDE_OF_ORIGIN, 0.0)))),
3125 : "unit", "deg");
3126 : }
3127 :
3128 97 : CPLXMLNode *psCR = nullptr;
3129 97 : if (m_bGotTransform || !IsCARTVersionGTE(pszCARTVersion, "1B00"))
3130 : {
3131 96 : CPLXMLNode *psPCI = CPLCreateXMLNode(
3132 : psPlanar, CXT_Element,
3133 192 : (osPrefix + "Planar_Coordinate_Information").c_str());
3134 96 : CPLCreateXMLElementAndValue(
3135 192 : psPCI, (osPrefix + "planar_coordinate_encoding_method").c_str(),
3136 : "Coordinate Pair");
3137 96 : psCR = CPLCreateXMLNode(
3138 : psPCI, CXT_Element,
3139 192 : (osPrefix + "Coordinate_Representation").c_str());
3140 : }
3141 97 : const double dfLinearUnits = m_oSRS.GetLinearUnits();
3142 97 : const double dfDegToMeter = m_oSRS.GetSemiMajor() * M_PI / 180.0;
3143 :
3144 97 : if (psCR == nullptr)
3145 : {
3146 : // do nothing
3147 : }
3148 96 : else if (!m_bGotTransform)
3149 : {
3150 0 : CPLAddXMLAttributeAndValue(
3151 : CPLCreateXMLElementAndValue(
3152 0 : psCR, (osPrefix + "pixel_resolution_x").c_str(), "0"),
3153 : "unit", "m/pixel");
3154 0 : CPLAddXMLAttributeAndValue(
3155 : CPLCreateXMLElementAndValue(
3156 0 : psCR, (osPrefix + "pixel_resolution_y").c_str(), "0"),
3157 : "unit", "m/pixel");
3158 0 : CPLAddXMLAttributeAndValue(
3159 : CPLCreateXMLElementAndValue(
3160 0 : psCR, (osPrefix + "pixel_scale_x").c_str(), "0"),
3161 : "unit", "pixel/deg");
3162 0 : CPLAddXMLAttributeAndValue(
3163 : CPLCreateXMLElementAndValue(
3164 0 : psCR, (osPrefix + "pixel_scale_y").c_str(), "0"),
3165 : "unit", "pixel/deg");
3166 : }
3167 96 : else if (m_oSRS.IsGeographic())
3168 : {
3169 126 : CPLAddXMLAttributeAndValue(
3170 : CPLCreateXMLElementAndValue(
3171 126 : psCR, (osPrefix + "pixel_resolution_x").c_str(),
3172 : CPLSPrintf("%.17g", dfUnrotatedResX * dfDegToMeter)),
3173 : "unit", "m/pixel");
3174 63 : CPLAddXMLAttributeAndValue(
3175 : CPLCreateXMLElementAndValue(
3176 126 : psCR, (osPrefix + "pixel_resolution_y").c_str(),
3177 63 : CPLSPrintf("%.17g", -dfUnrotatedResY * dfDegToMeter)),
3178 : "unit", "m/pixel");
3179 126 : CPLAddXMLAttributeAndValue(
3180 : CPLCreateXMLElementAndValue(
3181 126 : psCR, (osPrefix + "pixel_scale_x").c_str(),
3182 : CPLSPrintf("%.17g", 1.0 / (dfUnrotatedResX))),
3183 : "unit", "pixel/deg");
3184 126 : CPLAddXMLAttributeAndValue(
3185 : CPLCreateXMLElementAndValue(
3186 126 : psCR, (osPrefix + "pixel_scale_y").c_str(),
3187 : CPLSPrintf("%.17g", 1.0 / (-dfUnrotatedResY))),
3188 : "unit", "pixel/deg");
3189 : }
3190 33 : else if (m_oSRS.IsProjected())
3191 : {
3192 66 : CPLAddXMLAttributeAndValue(
3193 : CPLCreateXMLElementAndValue(
3194 66 : psCR, (osPrefix + "pixel_resolution_x").c_str(),
3195 : CPLSPrintf("%.17g", dfUnrotatedResX * dfLinearUnits)),
3196 : "unit", "m/pixel");
3197 33 : CPLAddXMLAttributeAndValue(
3198 : CPLCreateXMLElementAndValue(
3199 66 : psCR, (osPrefix + "pixel_resolution_y").c_str(),
3200 33 : CPLSPrintf("%.17g", -dfUnrotatedResY * dfLinearUnits)),
3201 : "unit", "m/pixel");
3202 33 : CPLAddXMLAttributeAndValue(
3203 : CPLCreateXMLElementAndValue(
3204 66 : psCR, (osPrefix + "pixel_scale_x").c_str(),
3205 : CPLSPrintf("%.17g", dfDegToMeter /
3206 33 : (dfUnrotatedResX * dfLinearUnits))),
3207 : "unit", "pixel/deg");
3208 33 : CPLAddXMLAttributeAndValue(
3209 : CPLCreateXMLElementAndValue(
3210 66 : psCR, (osPrefix + "pixel_scale_y").c_str(),
3211 33 : CPLSPrintf("%.17g", dfDegToMeter / (-dfUnrotatedResY *
3212 : dfLinearUnits))),
3213 : "unit", "pixel/deg");
3214 : }
3215 :
3216 97 : if (m_bGotTransform)
3217 : {
3218 : CPLXMLNode *psGT =
3219 96 : CPLCreateXMLNode(psPlanar, CXT_Element,
3220 192 : (osPrefix + "Geo_Transformation").c_str());
3221 : const double dfFalseEasting =
3222 96 : m_oSRS.GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0);
3223 : const double dfFalseNorthing =
3224 96 : m_oSRS.GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0);
3225 96 : const double dfULX = -dfFalseEasting + dfUnrotatedULX;
3226 96 : const double dfULY = -dfFalseNorthing + dfUnrotatedULY;
3227 96 : if (m_oSRS.IsGeographic())
3228 : {
3229 126 : CPLAddXMLAttributeAndValue(
3230 : CPLCreateXMLElementAndValue(
3231 126 : psGT, (osPrefix + "upperleft_corner_x").c_str(),
3232 : CPLSPrintf("%.17g", dfULX * dfDegToMeter)),
3233 : "unit", "m");
3234 126 : CPLAddXMLAttributeAndValue(
3235 : CPLCreateXMLElementAndValue(
3236 126 : psGT, (osPrefix + "upperleft_corner_y").c_str(),
3237 : CPLSPrintf("%.17g", dfULY * dfDegToMeter)),
3238 : "unit", "m");
3239 : }
3240 33 : else if (m_oSRS.IsProjected())
3241 : {
3242 66 : CPLAddXMLAttributeAndValue(
3243 : CPLCreateXMLElementAndValue(
3244 66 : psGT, (osPrefix + "upperleft_corner_x").c_str(),
3245 : CPLSPrintf("%.17g", dfULX * dfLinearUnits)),
3246 : "unit", "m");
3247 66 : CPLAddXMLAttributeAndValue(
3248 : CPLCreateXMLElementAndValue(
3249 66 : psGT, (osPrefix + "upperleft_corner_y").c_str(),
3250 : CPLSPrintf("%.17g", dfULY * dfLinearUnits)),
3251 : "unit", "m");
3252 : }
3253 : }
3254 : }
3255 : else
3256 : {
3257 1 : CPLXMLNode *psGeographic = CPLCreateXMLNode(
3258 2 : psHCSD, CXT_Element, (osPrefix + "Geographic").c_str());
3259 1 : if (!IsCARTVersionGTE(pszCARTVersion, "1B00"))
3260 : {
3261 0 : CPLAddXMLAttributeAndValue(
3262 : CPLCreateXMLElementAndValue(
3263 0 : psGeographic, (osPrefix + "latitude_resolution").c_str(),
3264 : "0"),
3265 : "unit", "deg");
3266 0 : CPLAddXMLAttributeAndValue(
3267 : CPLCreateXMLElementAndValue(
3268 0 : psGeographic, (osPrefix + "longitude_resolution").c_str(),
3269 : "0"),
3270 : "unit", "deg");
3271 : }
3272 : }
3273 :
3274 98 : CPLXMLNode *psGM = CPLCreateXMLNode(psHCSD, CXT_Element,
3275 196 : (osPrefix + "Geodetic_Model").c_str());
3276 196 : const char *pszLatitudeType = CSLFetchNameValueDef(
3277 98 : m_papszCreationOptions, "LATITUDE_TYPE", "Planetocentric");
3278 : // Fix case
3279 98 : if (EQUAL(pszLatitudeType, "Planetocentric"))
3280 97 : pszLatitudeType = "Planetocentric";
3281 1 : else if (EQUAL(pszLatitudeType, "Planetographic"))
3282 1 : pszLatitudeType = "Planetographic";
3283 98 : CPLCreateXMLElementAndValue(psGM, (osPrefix + "latitude_type").c_str(),
3284 : pszLatitudeType);
3285 :
3286 98 : const char *pszDatum = m_oSRS.GetAttrValue("DATUM");
3287 98 : if (pszDatum && STARTS_WITH(pszDatum, "D_"))
3288 : {
3289 4 : CPLCreateXMLElementAndValue(psGM, (osPrefix + "spheroid_name").c_str(),
3290 : pszDatum + 2);
3291 : }
3292 94 : else if (pszDatum)
3293 : {
3294 94 : CPLCreateXMLElementAndValue(psGM, (osPrefix + "spheroid_name").c_str(),
3295 : pszDatum);
3296 : }
3297 :
3298 98 : double dfSemiMajor = m_oSRS.GetSemiMajor();
3299 98 : double dfSemiMinor = m_oSRS.GetSemiMinor();
3300 98 : const char *pszRadii = CSLFetchNameValue(m_papszCreationOptions, "RADII");
3301 98 : if (pszRadii)
3302 : {
3303 1 : char **papszTokens = CSLTokenizeString2(pszRadii, " ,", 0);
3304 1 : if (CSLCount(papszTokens) == 2)
3305 : {
3306 1 : dfSemiMajor = CPLAtof(papszTokens[0]);
3307 1 : dfSemiMinor = CPLAtof(papszTokens[1]);
3308 : }
3309 1 : CSLDestroy(papszTokens);
3310 : }
3311 :
3312 : const bool bUseLDD1930RadiusNames =
3313 98 : IsCARTVersionGTE(pszCARTVersion, "1B10_1930");
3314 :
3315 196 : CPLAddXMLAttributeAndValue(
3316 : CPLCreateXMLElementAndValue(
3317 : psGM,
3318 196 : (osPrefix +
3319 : (bUseLDD1930RadiusNames ? "a_axis_radius" : "semi_major_radius"))
3320 : .c_str(),
3321 : CPLSPrintf("%.17g", dfSemiMajor)),
3322 : "unit", "m");
3323 : // No, this is not a bug. The PDS4 b_axis_radius/semi_minor_radius is the
3324 : // minor radius on the equatorial plane. Which in WKT doesn't really exist,
3325 : // so reuse the WKT semi major
3326 196 : CPLAddXMLAttributeAndValue(
3327 : CPLCreateXMLElementAndValue(
3328 : psGM,
3329 196 : (osPrefix +
3330 : (bUseLDD1930RadiusNames ? "b_axis_radius" : "semi_minor_radius"))
3331 : .c_str(),
3332 : CPLSPrintf("%.17g", dfSemiMajor)),
3333 : "unit", "m");
3334 196 : CPLAddXMLAttributeAndValue(
3335 : CPLCreateXMLElementAndValue(
3336 : psGM,
3337 196 : (osPrefix +
3338 : (bUseLDD1930RadiusNames ? "c_axis_radius" : "polar_radius"))
3339 : .c_str(),
3340 : CPLSPrintf("%.17g", dfSemiMinor)),
3341 : "unit", "m");
3342 :
3343 : // Fix case
3344 98 : if (EQUAL(pszLongitudeDirection, "Positive East"))
3345 97 : pszLongitudeDirection = "Positive East";
3346 1 : else if (EQUAL(pszLongitudeDirection, "Positive West"))
3347 1 : pszLongitudeDirection = "Positive West";
3348 98 : CPLCreateXMLElementAndValue(psGM,
3349 196 : (osPrefix + "longitude_direction").c_str(),
3350 : pszLongitudeDirection);
3351 98 : }
3352 :
3353 : /************************************************************************/
3354 : /* SubstituteVariables() */
3355 : /************************************************************************/
3356 :
3357 16490 : void PDS4Dataset::SubstituteVariables(CPLXMLNode *psNode, char **papszDict)
3358 : {
3359 16490 : if (psNode->eType == CXT_Text && psNode->pszValue &&
3360 6668 : strstr(psNode->pszValue, "${"))
3361 : {
3362 1382 : CPLString osVal(psNode->pszValue);
3363 :
3364 1556 : if (strstr(psNode->pszValue, "${TITLE}") != nullptr &&
3365 174 : CSLFetchNameValue(papszDict, "VAR_TITLE") == nullptr)
3366 : {
3367 160 : const CPLString osTitle(CPLGetFilename(GetDescription()));
3368 160 : CPLError(CE_Warning, CPLE_AppDefined,
3369 : "VAR_TITLE not defined. Using %s by default",
3370 : osTitle.c_str());
3371 160 : osVal.replaceAll("${TITLE}", osTitle);
3372 : }
3373 :
3374 4599 : for (char **papszIter = papszDict; papszIter && *papszIter; papszIter++)
3375 : {
3376 3217 : if (STARTS_WITH_CI(*papszIter, "VAR_"))
3377 : {
3378 2746 : char *pszKey = nullptr;
3379 2746 : const char *pszValue = CPLParseNameValue(*papszIter, &pszKey);
3380 2746 : if (pszKey && pszValue)
3381 : {
3382 2746 : const char *pszVarName = pszKey + strlen("VAR_");
3383 : osVal.replaceAll(
3384 2746 : (CPLString("${") + pszVarName + "}").c_str(), pszValue);
3385 : osVal.replaceAll(
3386 5492 : CPLString(CPLString("${") + pszVarName + "}")
3387 2746 : .tolower()
3388 : .c_str(),
3389 8238 : CPLString(pszValue).tolower());
3390 2746 : CPLFree(pszKey);
3391 : }
3392 : }
3393 : }
3394 1382 : if (osVal.find("${") != std::string::npos)
3395 : {
3396 778 : CPLError(CE_Warning, CPLE_AppDefined, "%s could not be substituted",
3397 : osVal.c_str());
3398 : }
3399 1382 : CPLFree(psNode->pszValue);
3400 1382 : psNode->pszValue = CPLStrdup(osVal);
3401 : }
3402 :
3403 32799 : for (CPLXMLNode *psIter = psNode->psChild; psIter; psIter = psIter->psNext)
3404 : {
3405 16309 : SubstituteVariables(psIter, papszDict);
3406 : }
3407 16490 : }
3408 :
3409 : /************************************************************************/
3410 : /* InitImageFile() */
3411 : /************************************************************************/
3412 :
3413 93 : bool PDS4Dataset::InitImageFile()
3414 : {
3415 93 : m_bMustInitImageFile = false;
3416 :
3417 93 : int bHasNoData = FALSE;
3418 93 : double dfNoData = 0;
3419 93 : int bHasNoDataAsInt64 = FALSE;
3420 93 : int64_t nNoDataInt64 = 0;
3421 93 : int bHasNoDataAsUInt64 = FALSE;
3422 93 : uint64_t nNoDataUInt64 = 0;
3423 93 : const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
3424 93 : if (eDT == GDT_Int64)
3425 : {
3426 4 : nNoDataInt64 =
3427 4 : GetRasterBand(1)->GetNoDataValueAsInt64(&bHasNoDataAsInt64);
3428 4 : if (!bHasNoDataAsInt64)
3429 2 : nNoDataInt64 = 0;
3430 : }
3431 89 : else if (eDT == GDT_UInt64)
3432 : {
3433 4 : nNoDataUInt64 =
3434 4 : GetRasterBand(1)->GetNoDataValueAsUInt64(&bHasNoDataAsUInt64);
3435 4 : if (!bHasNoDataAsUInt64)
3436 2 : nNoDataUInt64 = 0;
3437 : }
3438 : else
3439 : {
3440 85 : dfNoData = GetRasterBand(1)->GetNoDataValue(&bHasNoData);
3441 85 : if (!bHasNoData)
3442 69 : dfNoData = 0;
3443 : }
3444 :
3445 93 : if (m_poExternalDS)
3446 : {
3447 : int nBlockXSize, nBlockYSize;
3448 18 : GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize);
3449 18 : const int nDTSize = GDALGetDataTypeSizeBytes(eDT);
3450 18 : const int nBlockSizeBytes = nBlockXSize * nBlockYSize * nDTSize;
3451 18 : const int l_nBlocksPerColumn = DIV_ROUND_UP(nRasterYSize, nBlockYSize);
3452 :
3453 18 : if (nBands == 1 || EQUAL(m_osInterleave, "BSQ"))
3454 : {
3455 : // We need to make sure that blocks are written in the right order
3456 38 : for (int i = 0; i < nBands; i++)
3457 : {
3458 21 : if (eDT == GDT_Int64)
3459 : {
3460 1 : if (m_poExternalDS->GetRasterBand(i + 1)->RasterIO(
3461 : GF_Write, 0, 0, nRasterXSize, nRasterYSize,
3462 1 : &nNoDataInt64, 1, 1, eDT, 0, 0, nullptr) != CE_None)
3463 : {
3464 0 : return false;
3465 : }
3466 : }
3467 20 : else if (eDT == GDT_UInt64)
3468 : {
3469 1 : if (m_poExternalDS->GetRasterBand(i + 1)->RasterIO(
3470 : GF_Write, 0, 0, nRasterXSize, nRasterYSize,
3471 : &nNoDataUInt64, 1, 1, eDT, 0, 0,
3472 1 : nullptr) != CE_None)
3473 : {
3474 0 : return false;
3475 : }
3476 : }
3477 : else
3478 : {
3479 19 : if (m_poExternalDS->GetRasterBand(i + 1)->Fill(dfNoData) !=
3480 : CE_None)
3481 : {
3482 0 : return false;
3483 : }
3484 : }
3485 : }
3486 17 : m_poExternalDS->FlushCache(false);
3487 :
3488 : // Check that blocks are effectively written in expected order.
3489 17 : GIntBig nLastOffset = 0;
3490 38 : for (int i = 0; i < nBands; i++)
3491 : {
3492 333 : for (int y = 0; y < l_nBlocksPerColumn; y++)
3493 : {
3494 : const char *pszBlockOffset =
3495 624 : m_poExternalDS->GetRasterBand(i + 1)->GetMetadataItem(
3496 312 : CPLSPrintf("BLOCK_OFFSET_%d_%d", 0, y), "TIFF");
3497 312 : if (pszBlockOffset)
3498 : {
3499 312 : GIntBig nOffset = CPLAtoGIntBig(pszBlockOffset);
3500 312 : if (i != 0 || y != 0)
3501 : {
3502 295 : if (nOffset != nLastOffset + nBlockSizeBytes)
3503 : {
3504 0 : CPLError(CE_Warning, CPLE_AppDefined,
3505 : "Block %d,%d band %d not at expected "
3506 : "offset",
3507 : 0, y, i + 1);
3508 0 : return false;
3509 : }
3510 : }
3511 312 : nLastOffset = nOffset;
3512 : }
3513 : else
3514 : {
3515 0 : CPLError(CE_Warning, CPLE_AppDefined,
3516 : "Block %d,%d band %d not at expected "
3517 : "offset",
3518 : 0, y, i + 1);
3519 0 : return false;
3520 : }
3521 : }
3522 : }
3523 : }
3524 : else
3525 : {
3526 1 : void *pBlockData = VSI_MALLOC_VERBOSE(nBlockSizeBytes);
3527 1 : if (pBlockData == nullptr)
3528 0 : return false;
3529 1 : if (eDT == GDT_Int64)
3530 : {
3531 0 : GDALCopyWords(&nNoDataInt64, eDT, 0, pBlockData, eDT, nDTSize,
3532 : nBlockXSize * nBlockYSize);
3533 : }
3534 1 : else if (eDT == GDT_UInt64)
3535 : {
3536 0 : GDALCopyWords(&nNoDataUInt64, eDT, 0, pBlockData, eDT, nDTSize,
3537 : nBlockXSize * nBlockYSize);
3538 : }
3539 : else
3540 : {
3541 1 : GDALCopyWords(&dfNoData, GDT_Float64, 0, pBlockData, eDT,
3542 : nDTSize, nBlockXSize * nBlockYSize);
3543 : }
3544 2 : for (int y = 0; y < l_nBlocksPerColumn; y++)
3545 : {
3546 4 : for (int i = 0; i < nBands; i++)
3547 : {
3548 3 : if (m_poExternalDS->GetRasterBand(i + 1)->WriteBlock(
3549 3 : 0, y, pBlockData) != CE_None)
3550 : {
3551 0 : VSIFree(pBlockData);
3552 0 : return false;
3553 : }
3554 : }
3555 : }
3556 1 : VSIFree(pBlockData);
3557 1 : m_poExternalDS->FlushCache(false);
3558 :
3559 : // Check that blocks are effectively written in expected order.
3560 1 : GIntBig nLastOffset = 0;
3561 2 : for (int y = 0; y < l_nBlocksPerColumn; y++)
3562 : {
3563 : const char *pszBlockOffset =
3564 2 : m_poExternalDS->GetRasterBand(1)->GetMetadataItem(
3565 1 : CPLSPrintf("BLOCK_OFFSET_%d_%d", 0, y), "TIFF");
3566 1 : if (pszBlockOffset)
3567 : {
3568 1 : GIntBig nOffset = CPLAtoGIntBig(pszBlockOffset);
3569 1 : if (y != 0)
3570 : {
3571 0 : if (nOffset !=
3572 0 : nLastOffset +
3573 0 : static_cast<GIntBig>(nBlockSizeBytes) * nBands)
3574 : {
3575 0 : CPLError(CE_Warning, CPLE_AppDefined,
3576 : "Block %d,%d not at expected "
3577 : "offset",
3578 : 0, y);
3579 0 : return false;
3580 : }
3581 : }
3582 1 : nLastOffset = nOffset;
3583 : }
3584 : else
3585 : {
3586 0 : CPLError(CE_Warning, CPLE_AppDefined,
3587 : "Block %d,%d not at expected "
3588 : "offset",
3589 : 0, y);
3590 0 : return false;
3591 : }
3592 : }
3593 : }
3594 :
3595 18 : return true;
3596 : }
3597 :
3598 75 : const int nDTSize = GDALGetDataTypeSizeBytes(eDT);
3599 75 : const vsi_l_offset nFileSize = static_cast<vsi_l_offset>(nRasterXSize) *
3600 75 : nRasterYSize * nBands * nDTSize;
3601 75 : if ((eDT == GDT_Int64 && (nNoDataInt64 == 0 || !bHasNoDataAsInt64)) ||
3602 73 : (eDT == GDT_UInt64 && (nNoDataUInt64 == 0 || !bHasNoDataAsUInt64)) ||
3603 70 : (eDT != GDT_Int64 && eDT != GDT_UInt64 &&
3604 69 : (dfNoData == 0 || !bHasNoData)))
3605 : {
3606 66 : if (VSIFTruncateL(m_fpImage, nFileSize) != 0)
3607 : {
3608 0 : CPLError(CE_Failure, CPLE_FileIO,
3609 : "Cannot create file of size " CPL_FRMT_GUIB " bytes",
3610 : nFileSize);
3611 0 : return false;
3612 : }
3613 : }
3614 : else
3615 : {
3616 9 : size_t nLineSize = static_cast<size_t>(nRasterXSize) * nDTSize;
3617 9 : void *pData = VSI_MALLOC_VERBOSE(nLineSize);
3618 9 : if (pData == nullptr)
3619 0 : return false;
3620 9 : if (eDT == GDT_Int64)
3621 : {
3622 1 : GDALCopyWords(&nNoDataInt64, eDT, 0, pData, eDT, nDTSize,
3623 : nRasterXSize);
3624 : }
3625 8 : else if (eDT == GDT_UInt64)
3626 : {
3627 1 : GDALCopyWords(&nNoDataUInt64, eDT, 0, pData, eDT, nDTSize,
3628 : nRasterXSize);
3629 : }
3630 : else
3631 : {
3632 7 : GDALCopyWords(&dfNoData, GDT_Float64, 0, pData, eDT, nDTSize,
3633 : nRasterXSize);
3634 : }
3635 : #ifdef CPL_MSB
3636 : if (GDALDataTypeIsComplex(eDT))
3637 : {
3638 : GDALSwapWords(pData, nDTSize / 2, nRasterXSize * 2, nDTSize / 2);
3639 : }
3640 : else
3641 : {
3642 : GDALSwapWords(pData, nDTSize, nRasterXSize, nDTSize);
3643 : }
3644 : #endif
3645 18 : for (vsi_l_offset i = 0;
3646 18 : i < static_cast<vsi_l_offset>(nRasterYSize) * nBands; i++)
3647 : {
3648 9 : size_t nBytesWritten = VSIFWriteL(pData, 1, nLineSize, m_fpImage);
3649 9 : if (nBytesWritten != nLineSize)
3650 : {
3651 0 : CPLError(CE_Failure, CPLE_FileIO,
3652 : "Cannot create file of size " CPL_FRMT_GUIB " bytes",
3653 : nFileSize);
3654 0 : VSIFree(pData);
3655 0 : return false;
3656 : }
3657 : }
3658 9 : VSIFree(pData);
3659 : }
3660 75 : return true;
3661 : }
3662 :
3663 : /************************************************************************/
3664 : /* GetSpecialConstants() */
3665 : /************************************************************************/
3666 :
3667 12 : static CPLXMLNode *GetSpecialConstants(const CPLString &osPrefix,
3668 : CPLXMLNode *psFileAreaObservational)
3669 : {
3670 27 : for (CPLXMLNode *psIter = psFileAreaObservational->psChild; psIter;
3671 15 : psIter = psIter->psNext)
3672 : {
3673 48 : if (psIter->eType == CXT_Element &&
3674 48 : STARTS_WITH(psIter->pszValue, (osPrefix + "Array").c_str()))
3675 : {
3676 : CPLXMLNode *psSC =
3677 11 : CPLGetXMLNode(psIter, (osPrefix + "Special_Constants").c_str());
3678 11 : if (psSC)
3679 : {
3680 9 : CPLXMLNode *psNext = psSC->psNext;
3681 9 : psSC->psNext = nullptr;
3682 9 : CPLXMLNode *psRet = CPLCloneXMLTree(psSC);
3683 9 : psSC->psNext = psNext;
3684 9 : return psRet;
3685 : }
3686 : }
3687 : }
3688 3 : return nullptr;
3689 : }
3690 :
3691 : /************************************************************************/
3692 : /* WriteHeaderAppendCase() */
3693 : /************************************************************************/
3694 :
3695 4 : void PDS4Dataset::WriteHeaderAppendCase()
3696 : {
3697 4 : CPLXMLTreeCloser oCloser(CPLParseXMLFile(GetDescription()));
3698 4 : CPLXMLNode *psRoot = oCloser.get();
3699 4 : if (psRoot == nullptr)
3700 0 : return;
3701 4 : CPLString osPrefix;
3702 4 : CPLXMLNode *psProduct = CPLGetXMLNode(psRoot, "=Product_Observational");
3703 4 : if (psProduct == nullptr)
3704 : {
3705 0 : psProduct = CPLGetXMLNode(psRoot, "=pds:Product_Observational");
3706 0 : if (psProduct)
3707 0 : osPrefix = "pds:";
3708 : }
3709 4 : if (psProduct == nullptr)
3710 : {
3711 0 : CPLError(CE_Failure, CPLE_AppDefined,
3712 : "Cannot find Product_Observational element");
3713 0 : return;
3714 : }
3715 4 : CPLXMLNode *psFAO = CPLGetXMLNode(
3716 8 : psProduct, (osPrefix + "File_Area_Observational").c_str());
3717 4 : if (psFAO == nullptr)
3718 : {
3719 0 : CPLError(CE_Failure, CPLE_AppDefined,
3720 : "Cannot find File_Area_Observational element");
3721 0 : return;
3722 : }
3723 :
3724 4 : WriteArray(osPrefix, psFAO, nullptr, nullptr);
3725 :
3726 4 : CPLSerializeXMLTreeToFile(psRoot, GetDescription());
3727 : }
3728 :
3729 : /************************************************************************/
3730 : /* WriteArray() */
3731 : /************************************************************************/
3732 :
3733 135 : void PDS4Dataset::WriteArray(const CPLString &osPrefix, CPLXMLNode *psFAO,
3734 : const char *pszLocalIdentifierDefault,
3735 : CPLXMLNode *psTemplateSpecialConstants)
3736 : {
3737 270 : const char *pszArrayType = CSLFetchNameValueDef(
3738 135 : m_papszCreationOptions, "ARRAY_TYPE", "Array_3D_Image");
3739 135 : const bool bIsArray2D = STARTS_WITH(pszArrayType, "Array_2D");
3740 : CPLXMLNode *psArray =
3741 135 : CPLCreateXMLNode(psFAO, CXT_Element, (osPrefix + pszArrayType).c_str());
3742 :
3743 270 : const char *pszLocalIdentifier = CSLFetchNameValueDef(
3744 135 : m_papszCreationOptions, "ARRAY_IDENTIFIER", pszLocalIdentifierDefault);
3745 135 : if (pszLocalIdentifier)
3746 : {
3747 131 : CPLCreateXMLElementAndValue(psArray,
3748 262 : (osPrefix + "local_identifier").c_str(),
3749 : pszLocalIdentifier);
3750 : }
3751 :
3752 135 : GUIntBig nOffset = m_nBaseOffset;
3753 135 : if (m_poExternalDS)
3754 : {
3755 : const char *pszOffset =
3756 18 : m_poExternalDS->GetRasterBand(1)->GetMetadataItem(
3757 18 : "BLOCK_OFFSET_0_0", "TIFF");
3758 18 : if (pszOffset)
3759 18 : nOffset = CPLAtoGIntBig(pszOffset);
3760 : }
3761 270 : CPLAddXMLAttributeAndValue(
3762 270 : CPLCreateXMLElementAndValue(psArray, (osPrefix + "offset").c_str(),
3763 : CPLSPrintf(CPL_FRMT_GUIB, nOffset)),
3764 : "unit", "byte");
3765 135 : CPLCreateXMLElementAndValue(psArray, (osPrefix + "axes").c_str(),
3766 : (bIsArray2D) ? "2" : "3");
3767 135 : CPLCreateXMLElementAndValue(
3768 270 : psArray, (osPrefix + "axis_index_order").c_str(), "Last Index Fastest");
3769 135 : CPLXMLNode *psElementArray = CPLCreateXMLNode(
3770 270 : psArray, CXT_Element, (osPrefix + "Element_Array").c_str());
3771 135 : GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
3772 135 : const char *pszDataType =
3773 187 : (eDT == GDT_UInt8) ? "UnsignedByte"
3774 101 : : (eDT == GDT_Int8) ? "SignedByte"
3775 91 : : (eDT == GDT_UInt16) ? "UnsignedLSB2"
3776 79 : : (eDT == GDT_Int16) ? (m_bIsLSB ? "SignedLSB2" : "SignedMSB2")
3777 70 : : (eDT == GDT_UInt32) ? (m_bIsLSB ? "UnsignedLSB4" : "UnsignedMSB4")
3778 62 : : (eDT == GDT_Int32) ? (m_bIsLSB ? "SignedLSB4" : "SignedMSB4")
3779 54 : : (eDT == GDT_UInt64) ? (m_bIsLSB ? "UnsignedLSB8" : "UnsignedMSB8")
3780 46 : : (eDT == GDT_Int64) ? (m_bIsLSB ? "SignedLSB8" : "SignedMSB8")
3781 : : (eDT == GDT_Float32)
3782 35 : ? (m_bIsLSB ? "IEEE754LSBSingle" : "IEEE754MSBSingle")
3783 : : (eDT == GDT_Float64)
3784 22 : ? (m_bIsLSB ? "IEEE754LSBDouble" : "IEEE754MSBDouble")
3785 12 : : (eDT == GDT_CFloat32) ? (m_bIsLSB ? "ComplexLSB8" : "ComplexMSB8")
3786 4 : : (eDT == GDT_CFloat64) ? (m_bIsLSB ? "ComplexLSB16" : "ComplexMSB16")
3787 : : "should not happen";
3788 135 : CPLCreateXMLElementAndValue(psElementArray,
3789 270 : (osPrefix + "data_type").c_str(), pszDataType);
3790 :
3791 135 : const char *pszUnits = GetRasterBand(1)->GetUnitType();
3792 135 : const char *pszUnitsCO = CSLFetchNameValue(m_papszCreationOptions, "UNIT");
3793 135 : if (pszUnitsCO)
3794 : {
3795 0 : pszUnits = pszUnitsCO;
3796 : }
3797 135 : if (pszUnits && pszUnits[0] != 0)
3798 : {
3799 1 : CPLCreateXMLElementAndValue(psElementArray, (osPrefix + "unit").c_str(),
3800 : pszUnits);
3801 : }
3802 :
3803 135 : int bHasScale = FALSE;
3804 135 : double dfScale = GetRasterBand(1)->GetScale(&bHasScale);
3805 135 : if (bHasScale && dfScale != 1.0)
3806 : {
3807 10 : CPLCreateXMLElementAndValue(psElementArray,
3808 10 : (osPrefix + "scaling_factor").c_str(),
3809 : CPLSPrintf("%.17g", dfScale));
3810 : }
3811 :
3812 135 : int bHasOffset = FALSE;
3813 135 : double dfOffset = GetRasterBand(1)->GetOffset(&bHasOffset);
3814 135 : if (bHasOffset && dfOffset != 1.0)
3815 : {
3816 10 : CPLCreateXMLElementAndValue(psElementArray,
3817 10 : (osPrefix + "value_offset").c_str(),
3818 : CPLSPrintf("%.17g", dfOffset));
3819 : }
3820 :
3821 : // Axis definitions
3822 : {
3823 135 : CPLXMLNode *psAxis = CPLCreateXMLNode(
3824 270 : psArray, CXT_Element, (osPrefix + "Axis_Array").c_str());
3825 270 : CPLCreateXMLElementAndValue(
3826 270 : psAxis, (osPrefix + "axis_name").c_str(),
3827 135 : EQUAL(m_osInterleave, "BSQ")
3828 : ? "Band"
3829 : :
3830 : /* EQUAL(m_osInterleave, "BIL") ? "Line" : */
3831 : "Line");
3832 270 : CPLCreateXMLElementAndValue(
3833 270 : psAxis, (osPrefix + "elements").c_str(),
3834 : CPLSPrintf("%d",
3835 135 : EQUAL(m_osInterleave, "BSQ")
3836 : ? nBands
3837 : :
3838 : /* EQUAL(m_osInterleave, "BIL") ? nRasterYSize : */
3839 : nRasterYSize));
3840 135 : CPLCreateXMLElementAndValue(
3841 270 : psAxis, (osPrefix + "sequence_number").c_str(), "1");
3842 : }
3843 : {
3844 135 : CPLXMLNode *psAxis = CPLCreateXMLNode(
3845 270 : psArray, CXT_Element, (osPrefix + "Axis_Array").c_str());
3846 135 : CPLCreateXMLElementAndValue(psAxis, (osPrefix + "axis_name").c_str(),
3847 135 : EQUAL(m_osInterleave, "BSQ") ? "Line"
3848 11 : : EQUAL(m_osInterleave, "BIL") ? "Band"
3849 : : "Sample");
3850 270 : CPLCreateXMLElementAndValue(
3851 270 : psAxis, (osPrefix + "elements").c_str(),
3852 135 : CPLSPrintf("%d", EQUAL(m_osInterleave, "BSQ") ? nRasterYSize
3853 11 : : EQUAL(m_osInterleave, "BIL") ? nBands
3854 : : nRasterXSize));
3855 135 : CPLCreateXMLElementAndValue(
3856 270 : psAxis, (osPrefix + "sequence_number").c_str(), "2");
3857 : }
3858 135 : if (!bIsArray2D)
3859 : {
3860 134 : CPLXMLNode *psAxis = CPLCreateXMLNode(
3861 268 : psArray, CXT_Element, (osPrefix + "Axis_Array").c_str());
3862 134 : CPLCreateXMLElementAndValue(psAxis, (osPrefix + "axis_name").c_str(),
3863 134 : EQUAL(m_osInterleave, "BSQ") ? "Sample"
3864 10 : : EQUAL(m_osInterleave, "BIL") ? "Sample"
3865 : : "Band");
3866 268 : CPLCreateXMLElementAndValue(
3867 268 : psAxis, (osPrefix + "elements").c_str(),
3868 134 : CPLSPrintf("%d", EQUAL(m_osInterleave, "BSQ") ? nRasterXSize
3869 10 : : EQUAL(m_osInterleave, "BIL") ? nRasterXSize
3870 : : nBands));
3871 134 : CPLCreateXMLElementAndValue(
3872 268 : psAxis, (osPrefix + "sequence_number").c_str(), "3");
3873 : }
3874 :
3875 135 : int bHasNoData = FALSE;
3876 135 : int64_t nNoDataInt64 = 0;
3877 135 : uint64_t nNoDataUInt64 = 0;
3878 135 : double dfNoData = 0;
3879 135 : if (eDT == GDT_Int64)
3880 4 : nNoDataInt64 = GetRasterBand(1)->GetNoDataValueAsInt64(&bHasNoData);
3881 131 : else if (eDT == GDT_UInt64)
3882 4 : nNoDataUInt64 = GetRasterBand(1)->GetNoDataValueAsUInt64(&bHasNoData);
3883 : else
3884 127 : dfNoData = GetRasterBand(1)->GetNoDataValue(&bHasNoData);
3885 :
3886 270 : std::string osNoData;
3887 135 : if (bHasNoData)
3888 : {
3889 29 : if (eDT == GDT_Int64)
3890 2 : osNoData = std::to_string(nNoDataInt64);
3891 27 : else if (eDT == GDT_UInt64)
3892 2 : osNoData = std::to_string(nNoDataUInt64);
3893 25 : else if (GDALDataTypeIsInteger(GetRasterBand(1)->GetRasterDataType()))
3894 20 : osNoData = std::to_string(static_cast<int64_t>(dfNoData));
3895 5 : else if (eDT == GDT_Float32)
3896 : {
3897 : uint32_t nVal;
3898 3 : float fVal = static_cast<float>(dfNoData);
3899 3 : memcpy(&nVal, &fVal, sizeof(nVal));
3900 3 : osNoData = CPLSPrintf("0x%08X", nVal);
3901 : }
3902 : else
3903 : {
3904 : uint64_t nVal;
3905 2 : memcpy(&nVal, &dfNoData, sizeof(nVal));
3906 : osNoData =
3907 2 : CPLSPrintf("0x%16llX", static_cast<unsigned long long>(nVal));
3908 : }
3909 : }
3910 :
3911 135 : if (psTemplateSpecialConstants)
3912 : {
3913 9 : CPLAddXMLChild(psArray, psTemplateSpecialConstants);
3914 9 : if (bHasNoData)
3915 : {
3916 : CPLXMLNode *psMC =
3917 6 : CPLGetXMLNode(psTemplateSpecialConstants,
3918 12 : (osPrefix + "missing_constant").c_str());
3919 6 : if (psMC != nullptr)
3920 : {
3921 4 : if (psMC->psChild && psMC->psChild->eType == CXT_Text)
3922 : {
3923 4 : CPLFree(psMC->psChild->pszValue);
3924 4 : psMC->psChild->pszValue = CPLStrdup(osNoData.c_str());
3925 : }
3926 : }
3927 : else
3928 : {
3929 : CPLXMLNode *psSaturatedConstant =
3930 2 : CPLGetXMLNode(psTemplateSpecialConstants,
3931 4 : (osPrefix + "saturated_constant").c_str());
3932 4 : psMC = CPLCreateXMLElementAndValue(
3933 4 : nullptr, (osPrefix + "missing_constant").c_str(),
3934 : osNoData.c_str());
3935 : CPLXMLNode *psNext;
3936 2 : if (psSaturatedConstant)
3937 : {
3938 1 : psNext = psSaturatedConstant->psNext;
3939 1 : psSaturatedConstant->psNext = psMC;
3940 : }
3941 : else
3942 : {
3943 1 : psNext = psTemplateSpecialConstants->psChild;
3944 1 : psTemplateSpecialConstants->psChild = psMC;
3945 : }
3946 2 : psMC->psNext = psNext;
3947 : }
3948 : }
3949 : }
3950 126 : else if (bHasNoData)
3951 : {
3952 23 : CPLXMLNode *psSC = CPLCreateXMLNode(
3953 46 : psArray, CXT_Element, (osPrefix + "Special_Constants").c_str());
3954 46 : CPLCreateXMLElementAndValue(
3955 46 : psSC, (osPrefix + "missing_constant").c_str(), osNoData.c_str());
3956 : }
3957 135 : }
3958 :
3959 : /************************************************************************/
3960 : /* WriteVectorLayers() */
3961 : /************************************************************************/
3962 :
3963 189 : void PDS4Dataset::WriteVectorLayers(CPLXMLNode *psProduct)
3964 : {
3965 378 : CPLString osPrefix;
3966 189 : if (STARTS_WITH(psProduct->pszValue, "pds:"))
3967 0 : osPrefix = "pds:";
3968 :
3969 262 : for (auto &poLayer : m_apoLayers)
3970 : {
3971 73 : if (!poLayer->IsDirtyHeader())
3972 4 : continue;
3973 :
3974 69 : if (poLayer->GetFeatureCount(false) == 0)
3975 : {
3976 16 : CPLError(CE_Warning, CPLE_AppDefined,
3977 : "Writing header for layer %s which has 0 features. "
3978 : "This is not legal in PDS4",
3979 16 : poLayer->GetName());
3980 : }
3981 :
3982 69 : if (poLayer->GetRawFieldCount() == 0)
3983 : {
3984 16 : CPLError(CE_Warning, CPLE_AppDefined,
3985 : "Writing header for layer %s which has 0 fields. "
3986 : "This is not legal in PDS4",
3987 16 : poLayer->GetName());
3988 : }
3989 :
3990 : const std::string osRelativePath(
3991 138 : CPLExtractRelativePath(CPLGetPathSafe(m_osXMLFilename).c_str(),
3992 207 : poLayer->GetFileName(), nullptr));
3993 :
3994 69 : bool bFound = false;
3995 634 : for (CPLXMLNode *psIter = psProduct->psChild; psIter != nullptr;
3996 565 : psIter = psIter->psNext)
3997 : {
3998 733 : if (psIter->eType == CXT_Element &&
3999 164 : strcmp(psIter->pszValue,
4000 733 : (osPrefix + "File_Area_Observational").c_str()) == 0)
4001 : {
4002 23 : const char *pszFilename = CPLGetXMLValue(
4003 : psIter,
4004 46 : (osPrefix + "File." + osPrefix + "file_name").c_str(), "");
4005 23 : if (strcmp(pszFilename, osRelativePath.c_str()) == 0)
4006 : {
4007 4 : poLayer->RefreshFileAreaObservational(psIter);
4008 4 : bFound = true;
4009 4 : break;
4010 : }
4011 : }
4012 : }
4013 69 : if (!bFound)
4014 : {
4015 65 : CPLXMLNode *psFAO = CPLCreateXMLNode(
4016 : psProduct, CXT_Element,
4017 130 : (osPrefix + "File_Area_Observational").c_str());
4018 65 : CPLXMLNode *psFile = CPLCreateXMLNode(psFAO, CXT_Element,
4019 130 : (osPrefix + "File").c_str());
4020 130 : CPLCreateXMLElementAndValue(psFile,
4021 130 : (osPrefix + "file_name").c_str(),
4022 : osRelativePath.c_str());
4023 65 : poLayer->RefreshFileAreaObservational(psFAO);
4024 : }
4025 : }
4026 189 : }
4027 :
4028 : /************************************************************************/
4029 : /* CreateHeader() */
4030 : /************************************************************************/
4031 :
4032 181 : void PDS4Dataset::CreateHeader(CPLXMLNode *psProduct,
4033 : const char *pszCARTVersion)
4034 : {
4035 181 : CPLString osPrefix;
4036 181 : if (STARTS_WITH(psProduct->pszValue, "pds:"))
4037 0 : osPrefix = "pds:";
4038 :
4039 181 : if (m_oSRS.IsEmpty() && GetLayerCount() >= 1)
4040 : {
4041 46 : if (const auto poSRS = GetLayer(0)->GetSpatialRef())
4042 2 : m_oSRS = *poSRS;
4043 : }
4044 :
4045 280 : if (!m_oSRS.IsEmpty() &&
4046 99 : CSLFetchNameValue(m_papszCreationOptions, "VAR_TARGET") == nullptr)
4047 : {
4048 98 : const char *pszTarget = nullptr;
4049 98 : if (fabs(m_oSRS.GetSemiMajor() - 6378137) < 0.001 * 6378137)
4050 : {
4051 77 : pszTarget = "Earth";
4052 77 : m_papszCreationOptions = CSLSetNameValue(
4053 : m_papszCreationOptions, "VAR_TARGET_TYPE", "Planet");
4054 : }
4055 : else
4056 : {
4057 21 : const char *pszDatum = m_oSRS.GetAttrValue("DATUM");
4058 21 : if (pszDatum && STARTS_WITH(pszDatum, "D_"))
4059 : {
4060 3 : pszTarget = pszDatum + 2;
4061 : }
4062 18 : else if (pszDatum)
4063 : {
4064 18 : pszTarget = pszDatum;
4065 : }
4066 : }
4067 98 : if (pszTarget)
4068 : {
4069 98 : m_papszCreationOptions = CSLSetNameValue(m_papszCreationOptions,
4070 : "VAR_TARGET", pszTarget);
4071 : }
4072 : }
4073 181 : SubstituteVariables(psProduct, m_papszCreationOptions);
4074 :
4075 : // Remove <Discipline_Area>/<disp:Display_Settings> if there is no raster
4076 181 : if (GetRasterCount() == 0)
4077 : {
4078 : CPLXMLNode *psDisciplineArea =
4079 141 : CPLGetXMLNode(psProduct, (osPrefix + "Observation_Area." +
4080 94 : osPrefix + "Discipline_Area")
4081 : .c_str());
4082 47 : if (psDisciplineArea)
4083 : {
4084 : CPLXMLNode *psDisplaySettings =
4085 47 : CPLGetXMLNode(psDisciplineArea, "disp:Display_Settings");
4086 47 : if (psDisplaySettings)
4087 : {
4088 47 : CPLRemoveXMLChild(psDisciplineArea, psDisplaySettings);
4089 47 : CPLDestroyXMLNode(psDisplaySettings);
4090 : }
4091 : }
4092 : }
4093 :
4094 : // Depending on the version of the DISP schema, Local_Internal_Reference
4095 : // may be in the disp: namespace or the default one.
4096 : const auto GetLocalIdentifierReferenceFromDisciplineArea =
4097 224 : [](const CPLXMLNode *psDisciplineArea, const char *pszDefault)
4098 : {
4099 224 : return CPLGetXMLValue(
4100 : psDisciplineArea,
4101 : "disp:Display_Settings.Local_Internal_Reference."
4102 : "local_identifier_reference",
4103 : CPLGetXMLValue(
4104 : psDisciplineArea,
4105 : "disp:Display_Settings.disp:Local_Internal_Reference."
4106 : "local_identifier_reference",
4107 224 : pszDefault));
4108 : };
4109 :
4110 181 : if (GetRasterCount() || !m_oSRS.IsEmpty())
4111 : {
4112 : CPLXMLNode *psDisciplineArea =
4113 408 : CPLGetXMLNode(psProduct, (osPrefix + "Observation_Area." +
4114 272 : osPrefix + "Discipline_Area")
4115 : .c_str());
4116 136 : if (GetRasterCount() && !(m_bGotTransform && !m_oSRS.IsEmpty()))
4117 : {
4118 : // if we have no georeferencing, strip any existing georeferencing
4119 : // from the template
4120 37 : if (psDisciplineArea)
4121 : {
4122 : CPLXMLNode *psCart =
4123 33 : CPLGetXMLNode(psDisciplineArea, "cart:Cartography");
4124 33 : if (psCart == nullptr)
4125 32 : psCart = CPLGetXMLNode(psDisciplineArea, "Cartography");
4126 33 : if (psCart)
4127 : {
4128 1 : CPLRemoveXMLChild(psDisciplineArea, psCart);
4129 1 : CPLDestroyXMLNode(psCart);
4130 : }
4131 :
4132 33 : if (CPLGetXMLNode(psDisciplineArea,
4133 33 : "sp:Spectral_Characteristics"))
4134 : {
4135 : const char *pszArrayType =
4136 1 : CSLFetchNameValue(m_papszCreationOptions, "ARRAY_TYPE");
4137 : // The schematron PDS4_SP_1100.sch requires that
4138 : // sp:local_identifier_reference is used by
4139 : // Array_[2D|3D]_Spectrum/pds:local_identifier
4140 1 : if (pszArrayType == nullptr)
4141 : {
4142 1 : m_papszCreationOptions =
4143 1 : CSLSetNameValue(m_papszCreationOptions,
4144 : "ARRAY_TYPE", "Array_3D_Spectrum");
4145 : }
4146 0 : else if (!EQUAL(pszArrayType, "Array_2D_Spectrum") &&
4147 0 : !EQUAL(pszArrayType, "Array_3D_Spectrum"))
4148 : {
4149 0 : CPLError(CE_Warning, CPLE_AppDefined,
4150 : "PDS4_SP_xxxx.sch schematron requires the "
4151 : "use of ARRAY_TYPE=Array_2D_Spectrum or "
4152 : "Array_3D_Spectrum");
4153 : }
4154 : }
4155 : }
4156 : }
4157 : else
4158 : {
4159 99 : if (psDisciplineArea == nullptr)
4160 : {
4161 2 : CPLXMLNode *psTI = CPLGetXMLNode(
4162 4 : psProduct, (osPrefix + "Observation_Area." + osPrefix +
4163 : "Target_Identification")
4164 : .c_str());
4165 2 : if (psTI == nullptr)
4166 : {
4167 1 : CPLError(CE_Failure, CPLE_AppDefined,
4168 : "Cannot find Target_Identification element in "
4169 : "template");
4170 1 : return;
4171 : }
4172 : psDisciplineArea =
4173 1 : CPLCreateXMLNode(nullptr, CXT_Element,
4174 2 : (osPrefix + "Discipline_Area").c_str());
4175 1 : if (psTI->psNext)
4176 0 : psDisciplineArea->psNext = psTI->psNext;
4177 1 : psTI->psNext = psDisciplineArea;
4178 : }
4179 : CPLXMLNode *psCart =
4180 98 : CPLGetXMLNode(psDisciplineArea, "cart:Cartography");
4181 98 : if (psCart == nullptr)
4182 93 : psCart = CPLGetXMLNode(psDisciplineArea, "Cartography");
4183 98 : if (psCart == nullptr)
4184 : {
4185 93 : psCart = CPLCreateXMLNode(psDisciplineArea, CXT_Element,
4186 : "cart:Cartography");
4187 93 : if (CPLGetXMLNode(psProduct, "xmlns:cart") == nullptr)
4188 : {
4189 : CPLXMLNode *psNS =
4190 1 : CPLCreateXMLNode(nullptr, CXT_Attribute, "xmlns:cart");
4191 1 : CPLCreateXMLNode(psNS, CXT_Text,
4192 : "http://pds.nasa.gov/pds4/cart/v1");
4193 1 : CPLAddXMLChild(psProduct, psNS);
4194 : CPLXMLNode *psSchemaLoc =
4195 1 : CPLGetXMLNode(psProduct, "xsi:schemaLocation");
4196 1 : if (psSchemaLoc != nullptr &&
4197 1 : psSchemaLoc->psChild != nullptr &&
4198 1 : psSchemaLoc->psChild->pszValue != nullptr)
4199 : {
4200 2 : CPLString osCartSchema;
4201 1 : if (strstr(psSchemaLoc->psChild->pszValue,
4202 : "PDS4_PDS_1800.xsd"))
4203 : {
4204 : // GDAL 2.4
4205 : osCartSchema = "https://pds.nasa.gov/pds4/cart/v1/"
4206 1 : "PDS4_CART_1700.xsd";
4207 1 : pszCARTVersion = "1700";
4208 : }
4209 0 : else if (strstr(psSchemaLoc->psChild->pszValue,
4210 : "PDS4_PDS_1B00.xsd"))
4211 : {
4212 : // GDAL 3.0
4213 : osCartSchema =
4214 : "https://raw.githubusercontent.com/"
4215 : "nasa-pds-data-dictionaries/ldd-cart/master/"
4216 0 : "build/1.B.0.0/PDS4_CART_1B00.xsd";
4217 0 : pszCARTVersion = "1B00";
4218 : }
4219 0 : else if (strstr(psSchemaLoc->psChild->pszValue,
4220 : "PDS4_PDS_1D00.xsd"))
4221 : {
4222 : // GDAL 3.1
4223 : osCartSchema = "https://pds.nasa.gov/pds4/cart/v1/"
4224 0 : "PDS4_CART_1D00_1933.xsd";
4225 0 : pszCARTVersion = "1D00_1933";
4226 : }
4227 0 : else if (strstr(psSchemaLoc->psChild->pszValue,
4228 : "PDS4_PDS_1G00_1950.xsd"))
4229 : {
4230 : // GDAL 3.4
4231 : osCartSchema = "https://pds.nasa.gov/pds4/cart/v1/"
4232 0 : "PDS4_CART_1G00_1950.xsd";
4233 0 : pszCARTVersion = "1G00_1950";
4234 : }
4235 : else
4236 : {
4237 : // GDAL 3.12
4238 : osCartSchema =
4239 : "https://pds.nasa.gov/pds4/cart/v1/"
4240 0 : "PDS4_CART_" CURRENT_CART_VERSION ".xsd";
4241 0 : pszCARTVersion = CURRENT_CART_VERSION;
4242 : }
4243 1 : CPLString osNewVal(psSchemaLoc->psChild->pszValue);
4244 : osNewVal +=
4245 1 : " http://pds.nasa.gov/pds4/cart/v1 " + osCartSchema;
4246 1 : CPLFree(psSchemaLoc->psChild->pszValue);
4247 1 : psSchemaLoc->psChild->pszValue = CPLStrdup(osNewVal);
4248 : }
4249 : }
4250 : }
4251 : else
4252 : {
4253 5 : if (psCart->psChild)
4254 : {
4255 5 : CPLDestroyXMLNode(psCart->psChild);
4256 5 : psCart->psChild = nullptr;
4257 : }
4258 : }
4259 :
4260 98 : if (IsCARTVersionGTE(pszCARTVersion, "1900"))
4261 : {
4262 : const char *pszLocalIdentifier =
4263 93 : GetLocalIdentifierReferenceFromDisciplineArea(
4264 : psDisciplineArea,
4265 93 : GetRasterCount() == 0 && GetLayerCount() > 0
4266 2 : ? GetLayer(0)->GetName()
4267 : : "image");
4268 93 : CPLXMLNode *psLIR = CPLCreateXMLNode(
4269 : psCart, CXT_Element,
4270 186 : (osPrefix + "Local_Internal_Reference").c_str());
4271 93 : CPLCreateXMLElementAndValue(
4272 186 : psLIR, (osPrefix + "local_identifier_reference").c_str(),
4273 : pszLocalIdentifier);
4274 93 : CPLCreateXMLElementAndValue(
4275 186 : psLIR, (osPrefix + "local_reference_type").c_str(),
4276 : "cartography_parameters_to_image_object");
4277 : }
4278 :
4279 98 : WriteGeoreferencing(psCart, pszCARTVersion);
4280 : }
4281 :
4282 270 : const char *pszVertDir = CSLFetchNameValue(
4283 135 : m_papszCreationOptions, "VAR_VERTICAL_DISPLAY_DIRECTION");
4284 135 : if (pszVertDir)
4285 : {
4286 : CPLXMLNode *psVertDirNode =
4287 1 : CPLGetXMLNode(psDisciplineArea,
4288 : "disp:Display_Settings.disp:Display_Direction."
4289 : "disp:vertical_display_direction");
4290 1 : if (psVertDirNode == nullptr)
4291 : {
4292 0 : CPLError(
4293 : CE_Warning, CPLE_AppDefined,
4294 : "PDS4 template lacks a disp:vertical_display_direction "
4295 : "element where to write %s",
4296 : pszVertDir);
4297 : }
4298 : else
4299 : {
4300 1 : CPLDestroyXMLNode(psVertDirNode->psChild);
4301 1 : psVertDirNode->psChild =
4302 1 : CPLCreateXMLNode(nullptr, CXT_Text, pszVertDir);
4303 : }
4304 : }
4305 : }
4306 : else
4307 : {
4308 : // Remove Observation_Area.Discipline_Area if it contains only
4309 : // <disp:Display_Settings> or is empty
4310 : CPLXMLNode *psObservationArea =
4311 45 : CPLGetXMLNode(psProduct, (osPrefix + "Observation_Area").c_str());
4312 45 : if (psObservationArea)
4313 : {
4314 45 : CPLXMLNode *psDisciplineArea = CPLGetXMLNode(
4315 90 : psObservationArea, (osPrefix + "Discipline_Area").c_str());
4316 45 : if (psDisciplineArea &&
4317 45 : (psDisciplineArea->psChild == nullptr ||
4318 0 : (psDisciplineArea->psChild->eType == CXT_Element &&
4319 0 : psDisciplineArea->psChild->psNext == nullptr &&
4320 0 : strcmp(psDisciplineArea->psChild->pszValue,
4321 : "disp:Display_Settings") == 0)))
4322 : {
4323 45 : CPLRemoveXMLChild(psObservationArea, psDisciplineArea);
4324 45 : CPLDestroyXMLNode(psDisciplineArea);
4325 : }
4326 : }
4327 : }
4328 :
4329 180 : if (m_bStripFileAreaObservationalFromTemplate)
4330 : {
4331 180 : m_bStripFileAreaObservationalFromTemplate = false;
4332 180 : CPLXMLNode *psObservationArea = nullptr;
4333 180 : CPLXMLNode *psPrev = nullptr;
4334 180 : CPLXMLNode *psTemplateSpecialConstants = nullptr;
4335 1618 : for (CPLXMLNode *psIter = psProduct->psChild; psIter != nullptr;)
4336 : {
4337 1809 : if (psIter->eType == CXT_Element &&
4338 1809 : psIter->pszValue == osPrefix + "Observation_Area")
4339 : {
4340 179 : psObservationArea = psIter;
4341 179 : psPrev = psIter;
4342 179 : psIter = psIter->psNext;
4343 : }
4344 1631 : else if (psIter->eType == CXT_Element &&
4345 192 : (psIter->pszValue ==
4346 1631 : osPrefix + "File_Area_Observational" ||
4347 180 : psIter->pszValue ==
4348 1439 : osPrefix + "File_Area_Observational_Supplemental"))
4349 : {
4350 12 : if (psIter->pszValue == osPrefix + "File_Area_Observational")
4351 : {
4352 : psTemplateSpecialConstants =
4353 12 : GetSpecialConstants(osPrefix, psIter);
4354 : }
4355 12 : if (psPrev)
4356 12 : psPrev->psNext = psIter->psNext;
4357 : else
4358 : {
4359 0 : CPLAssert(psProduct->psChild == psIter);
4360 0 : psProduct->psChild = psIter->psNext;
4361 : }
4362 12 : CPLXMLNode *psNext = psIter->psNext;
4363 12 : psIter->psNext = nullptr;
4364 12 : CPLDestroyXMLNode(psIter);
4365 12 : psIter = psNext;
4366 : }
4367 : else
4368 : {
4369 1247 : psPrev = psIter;
4370 1247 : psIter = psIter->psNext;
4371 : }
4372 : }
4373 180 : if (psObservationArea == nullptr)
4374 : {
4375 1 : CPLError(CE_Failure, CPLE_AppDefined,
4376 : "Cannot find Observation_Area in template");
4377 1 : CPLDestroyXMLNode(psTemplateSpecialConstants);
4378 1 : return;
4379 : }
4380 :
4381 179 : if (GetRasterCount())
4382 : {
4383 132 : CPLXMLNode *psFAOPrev = psObservationArea;
4384 134 : while (psFAOPrev->psNext != nullptr &&
4385 4 : psFAOPrev->psNext->eType == CXT_Comment)
4386 : {
4387 2 : psFAOPrev = psFAOPrev->psNext;
4388 : }
4389 132 : if (psFAOPrev->psNext != nullptr)
4390 : {
4391 : // There may be an optional Reference_List element between
4392 : // Observation_Area and File_Area_Observational
4393 4 : if (!(psFAOPrev->psNext->eType == CXT_Element &&
4394 2 : psFAOPrev->psNext->pszValue ==
4395 4 : osPrefix + "Reference_List"))
4396 : {
4397 1 : CPLError(CE_Failure, CPLE_AppDefined,
4398 : "Unexpected content found after Observation_Area "
4399 : "in template");
4400 1 : CPLDestroyXMLNode(psTemplateSpecialConstants);
4401 1 : return;
4402 : }
4403 1 : psFAOPrev = psFAOPrev->psNext;
4404 2 : while (psFAOPrev->psNext != nullptr &&
4405 1 : psFAOPrev->psNext->eType == CXT_Comment)
4406 : {
4407 1 : psFAOPrev = psFAOPrev->psNext;
4408 : }
4409 1 : if (psFAOPrev->psNext != nullptr)
4410 : {
4411 0 : CPLError(CE_Failure, CPLE_AppDefined,
4412 : "Unexpected content found after Reference_List in "
4413 : "template");
4414 0 : CPLDestroyXMLNode(psTemplateSpecialConstants);
4415 0 : return;
4416 : }
4417 : }
4418 :
4419 131 : CPLXMLNode *psFAO = CPLCreateXMLNode(
4420 : nullptr, CXT_Element,
4421 262 : (osPrefix + "File_Area_Observational").c_str());
4422 131 : psFAOPrev->psNext = psFAO;
4423 :
4424 131 : CPLXMLNode *psFile = CPLCreateXMLNode(psFAO, CXT_Element,
4425 262 : (osPrefix + "File").c_str());
4426 262 : CPLCreateXMLElementAndValue(psFile,
4427 262 : (osPrefix + "file_name").c_str(),
4428 : CPLGetFilename(m_osImageFilename));
4429 131 : if (m_bCreatedFromExistingBinaryFile)
4430 : {
4431 7 : CPLCreateXMLNode(psFile, CXT_Comment, PREEXISTING_BINARY_FILE);
4432 : }
4433 : CPLXMLNode *psDisciplineArea =
4434 393 : CPLGetXMLNode(psProduct, (osPrefix + "Observation_Area." +
4435 262 : osPrefix + "Discipline_Area")
4436 : .c_str());
4437 : const char *pszLocalIdentifier =
4438 131 : GetLocalIdentifierReferenceFromDisciplineArea(psDisciplineArea,
4439 : "image");
4440 :
4441 147 : if (m_poExternalDS && m_poExternalDS->GetDriver() &&
4442 16 : EQUAL(m_poExternalDS->GetDriver()->GetDescription(), "GTiff"))
4443 : {
4444 : VSILFILE *fpTemp =
4445 16 : VSIFOpenL(m_poExternalDS->GetDescription(), "rb");
4446 16 : if (fpTemp)
4447 : {
4448 16 : GByte abySignature[4] = {0};
4449 16 : VSIFReadL(abySignature, 1, 4, fpTemp);
4450 16 : VSIFCloseL(fpTemp);
4451 16 : const bool bBigTIFF =
4452 16 : abySignature[2] == 43 || abySignature[3] == 43;
4453 : m_osHeaderParsingStandard =
4454 16 : bBigTIFF ? BIGTIFF_GEOTIFF_STRING : TIFF_GEOTIFF_STRING;
4455 : const char *pszOffset =
4456 16 : m_poExternalDS->GetRasterBand(1)->GetMetadataItem(
4457 16 : "BLOCK_OFFSET_0_0", "TIFF");
4458 16 : if (pszOffset)
4459 16 : m_nBaseOffset = CPLAtoGIntBig(pszOffset);
4460 : }
4461 : }
4462 :
4463 131 : if (!m_osHeaderParsingStandard.empty() && m_nBaseOffset > 0)
4464 : {
4465 22 : CPLXMLNode *psHeader = CPLCreateXMLNode(
4466 44 : psFAO, CXT_Element, (osPrefix + "Header").c_str());
4467 22 : CPLAddXMLAttributeAndValue(
4468 : CPLCreateXMLElementAndValue(
4469 44 : psHeader, (osPrefix + "offset").c_str(), "0"),
4470 : "unit", "byte");
4471 22 : CPLAddXMLAttributeAndValue(
4472 : CPLCreateXMLElementAndValue(
4473 44 : psHeader, (osPrefix + "object_length").c_str(),
4474 : CPLSPrintf(CPL_FRMT_GUIB,
4475 22 : static_cast<GUIntBig>(m_nBaseOffset))),
4476 : "unit", "byte");
4477 44 : CPLCreateXMLElementAndValue(
4478 44 : psHeader, (osPrefix + "parsing_standard_id").c_str(),
4479 : m_osHeaderParsingStandard.c_str());
4480 22 : if (m_osHeaderParsingStandard == TIFF_GEOTIFF_STRING)
4481 : {
4482 18 : CPLCreateXMLElementAndValue(
4483 36 : psHeader, (osPrefix + "description").c_str(),
4484 : "TIFF/GeoTIFF header. The TIFF/GeoTIFF format is used "
4485 : "throughout the geospatial and science communities "
4486 : "to share geographic image data. ");
4487 : }
4488 4 : else if (m_osHeaderParsingStandard == BIGTIFF_GEOTIFF_STRING)
4489 : {
4490 0 : CPLCreateXMLElementAndValue(
4491 0 : psHeader, (osPrefix + "description").c_str(),
4492 : "BigTIFF/GeoTIFF header. The BigTIFF/GeoTIFF format is "
4493 : "used "
4494 : "throughout the geospatial and science communities "
4495 : "to share geographic image data. ");
4496 : }
4497 : }
4498 :
4499 131 : WriteArray(osPrefix, psFAO, pszLocalIdentifier,
4500 : psTemplateSpecialConstants);
4501 : }
4502 : }
4503 : }
4504 :
4505 : /************************************************************************/
4506 : /* WriteHeader() */
4507 : /************************************************************************/
4508 :
4509 194 : void PDS4Dataset::WriteHeader()
4510 : {
4511 : const bool bAppend =
4512 194 : CPLFetchBool(m_papszCreationOptions, "APPEND_SUBDATASET", false);
4513 194 : if (bAppend)
4514 : {
4515 4 : WriteHeaderAppendCase();
4516 5 : return;
4517 : }
4518 :
4519 : CPLXMLNode *psRoot;
4520 190 : if (m_bCreateHeader)
4521 : {
4522 : CPLString osTemplateFilename =
4523 182 : CSLFetchNameValueDef(m_papszCreationOptions, "TEMPLATE", "");
4524 182 : if (!osTemplateFilename.empty())
4525 : {
4526 20 : if (STARTS_WITH(osTemplateFilename, "http://") ||
4527 10 : STARTS_WITH(osTemplateFilename, "https://"))
4528 : {
4529 0 : osTemplateFilename = "/vsicurl_streaming/" + osTemplateFilename;
4530 : }
4531 10 : psRoot = CPLParseXMLFile(osTemplateFilename);
4532 : }
4533 172 : else if (!m_osXMLPDS4.empty())
4534 6 : psRoot = CPLParseXMLString(m_osXMLPDS4);
4535 : else
4536 : {
4537 : #ifndef USE_ONLY_EMBEDDED_RESOURCE_FILES
4538 : #ifdef EMBED_RESOURCE_FILES
4539 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
4540 : #endif
4541 : const char *pszDefaultTemplateFilename =
4542 166 : CPLFindFile("gdal", "pds4_template.xml");
4543 166 : if (pszDefaultTemplateFilename)
4544 : {
4545 166 : psRoot = CPLParseXMLFile(pszDefaultTemplateFilename);
4546 : }
4547 : else
4548 : #endif
4549 : {
4550 : #ifdef EMBED_RESOURCE_FILES
4551 : static const bool bOnce [[maybe_unused]] = []()
4552 : {
4553 : CPLDebug("PDS4", "Using embedded pds4_template.xml");
4554 : return true;
4555 : }();
4556 : psRoot = CPLParseXMLString(PDS4GetEmbeddedTemplate());
4557 : #else
4558 0 : CPLError(CE_Failure, CPLE_AppDefined,
4559 : "Cannot find pds4_template.xml and TEMPLATE "
4560 : "creation option not specified");
4561 0 : return;
4562 : #endif
4563 : }
4564 : }
4565 : }
4566 : else
4567 : {
4568 8 : psRoot = CPLParseXMLFile(m_osXMLFilename);
4569 : }
4570 190 : CPLXMLTreeCloser oCloser(psRoot);
4571 190 : psRoot = oCloser.get();
4572 190 : if (psRoot == nullptr)
4573 0 : return;
4574 190 : CPLXMLNode *psProduct = CPLGetXMLNode(psRoot, "=Product_Observational");
4575 190 : if (psProduct == nullptr)
4576 : {
4577 1 : psProduct = CPLGetXMLNode(psRoot, "=pds:Product_Observational");
4578 : }
4579 190 : if (psProduct == nullptr)
4580 : {
4581 1 : CPLError(CE_Failure, CPLE_AppDefined,
4582 : "Cannot find Product_Observational element in template");
4583 1 : return;
4584 : }
4585 :
4586 189 : if (m_bCreateHeader)
4587 : {
4588 362 : CPLString osCARTVersion(CURRENT_CART_VERSION);
4589 181 : char *pszXML = CPLSerializeXMLTree(psRoot);
4590 181 : if (pszXML)
4591 : {
4592 181 : const char *pszIter = pszXML;
4593 : while (true)
4594 : {
4595 355 : const char *pszCartSchema = strstr(pszIter, "PDS4_CART_");
4596 355 : if (pszCartSchema)
4597 : {
4598 348 : const char *pszXSDExtension = strstr(pszCartSchema, ".xsd");
4599 348 : if (pszXSDExtension &&
4600 348 : pszXSDExtension - pszCartSchema <= 20)
4601 : {
4602 174 : osCARTVersion = pszCartSchema + strlen("PDS4_CART_");
4603 174 : osCARTVersion.resize(pszXSDExtension - pszCartSchema -
4604 : strlen("PDS4_CART_"));
4605 174 : break;
4606 : }
4607 : else
4608 : {
4609 174 : pszIter = pszCartSchema + 1;
4610 : }
4611 : }
4612 : else
4613 : {
4614 7 : break;
4615 : }
4616 174 : }
4617 :
4618 181 : CPLFree(pszXML);
4619 : }
4620 :
4621 181 : CreateHeader(psProduct, osCARTVersion.c_str());
4622 : }
4623 :
4624 189 : WriteVectorLayers(psProduct);
4625 :
4626 189 : CPLSerializeXMLTreeToFile(psRoot, GetDescription());
4627 : }
4628 :
4629 : /************************************************************************/
4630 : /* ICreateLayer() */
4631 : /************************************************************************/
4632 :
4633 66 : OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName,
4634 : const OGRGeomFieldDefn *poGeomFieldDefn,
4635 : CSLConstList papszOptions)
4636 : {
4637 : const char *pszTableType =
4638 66 : CSLFetchNameValueDef(papszOptions, "TABLE_TYPE", "DELIMITED");
4639 66 : if (!EQUAL(pszTableType, "CHARACTER") && !EQUAL(pszTableType, "BINARY") &&
4640 55 : !EQUAL(pszTableType, "DELIMITED"))
4641 : {
4642 0 : return nullptr;
4643 : }
4644 :
4645 66 : const auto eGType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
4646 : const auto poSpatialRef =
4647 66 : poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
4648 :
4649 126 : const char *pszExt = EQUAL(pszTableType, "CHARACTER") ? "dat"
4650 60 : : EQUAL(pszTableType, "BINARY") ? "bin"
4651 : : "csv";
4652 132 : std::string osBasename(pszName);
4653 629 : for (char &ch : osBasename)
4654 : {
4655 563 : if (!isalnum(static_cast<unsigned char>(ch)) &&
4656 53 : static_cast<unsigned>(ch) <= 127)
4657 53 : ch = '_';
4658 : }
4659 :
4660 198 : CPLString osFullFilename(CPLFormFilenameSafe(
4661 132 : CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(),
4662 198 : CPLGetBasenameSafe(m_osXMLFilename.c_str()).c_str(), nullptr));
4663 66 : osFullFilename += '_';
4664 66 : osFullFilename += osBasename.c_str();
4665 66 : osFullFilename += '.';
4666 66 : osFullFilename += pszExt;
4667 : VSIStatBufL sStat;
4668 66 : if (VSIStatL(osFullFilename, &sStat) == 0)
4669 : {
4670 0 : CPLError(CE_Failure, CPLE_AppDefined,
4671 : "%s already exists. Please delete it before, or "
4672 : "rename the layer",
4673 : osFullFilename.c_str());
4674 0 : return nullptr;
4675 : }
4676 :
4677 66 : if (EQUAL(pszTableType, "DELIMITED"))
4678 : {
4679 : auto poLayer =
4680 55 : std::make_unique<PDS4DelimitedTable>(this, pszName, osFullFilename);
4681 55 : if (!poLayer->InitializeNewLayer(poSpatialRef, false, eGType,
4682 : papszOptions))
4683 : {
4684 1 : return nullptr;
4685 : }
4686 54 : m_apoLayers.push_back(
4687 108 : std::make_unique<PDS4EditableLayer>(std::move(poLayer)));
4688 : }
4689 : else
4690 : {
4691 0 : std::unique_ptr<PDS4FixedWidthTable> poLayer;
4692 11 : if (EQUAL(pszTableType, "CHARACTER"))
4693 12 : poLayer = std::make_unique<PDS4TableCharacter>(this, pszName,
4694 6 : osFullFilename);
4695 : else
4696 10 : poLayer = std::make_unique<PDS4TableBinary>(this, pszName,
4697 5 : osFullFilename);
4698 11 : if (!poLayer->InitializeNewLayer(poSpatialRef, false, eGType,
4699 : papszOptions))
4700 : {
4701 0 : return nullptr;
4702 : }
4703 11 : m_apoLayers.push_back(
4704 22 : std::make_unique<PDS4EditableLayer>(std::move(poLayer)));
4705 : }
4706 65 : return m_apoLayers.back().get();
4707 : }
4708 :
4709 : /************************************************************************/
4710 : /* TestCapability() */
4711 : /************************************************************************/
4712 :
4713 69 : int PDS4Dataset::TestCapability(const char *pszCap) const
4714 : {
4715 69 : if (EQUAL(pszCap, ODsCCreateLayer))
4716 35 : return eAccess == GA_Update;
4717 34 : else if (EQUAL(pszCap, ODsCZGeometries))
4718 6 : return TRUE;
4719 : else
4720 28 : return FALSE;
4721 : }
4722 :
4723 : /************************************************************************/
4724 : /* Create() */
4725 : /************************************************************************/
4726 :
4727 140 : GDALDataset *PDS4Dataset::Create(const char *pszFilename, int nXSize,
4728 : int nYSize, int nBandsIn, GDALDataType eType,
4729 : CSLConstList papszOptions)
4730 : {
4731 280 : return CreateInternal(pszFilename, nullptr, nXSize, nYSize, nBandsIn, eType,
4732 : papszOptions)
4733 140 : .release();
4734 : }
4735 :
4736 : /************************************************************************/
4737 : /* CreateInternal() */
4738 : /************************************************************************/
4739 :
4740 201 : std::unique_ptr<PDS4Dataset> PDS4Dataset::CreateInternal(
4741 : const char *pszFilename, GDALDataset *poSrcDS, int nXSize, int nYSize,
4742 : int nBandsIn, GDALDataType eType, const char *const *papszOptionsIn)
4743 : {
4744 402 : CPLStringList aosOptions(papszOptionsIn);
4745 :
4746 201 : if (nXSize == 0 && nYSize == 0 && nBandsIn == 0 && eType == GDT_Unknown)
4747 : {
4748 : // Vector file creation
4749 94 : auto poDS = std::make_unique<PDS4Dataset>();
4750 47 : poDS->SetDescription(pszFilename);
4751 47 : poDS->nRasterXSize = 0;
4752 47 : poDS->nRasterYSize = 0;
4753 47 : poDS->eAccess = GA_Update;
4754 47 : poDS->m_osXMLFilename = pszFilename;
4755 47 : poDS->m_bCreateHeader = true;
4756 47 : poDS->m_bStripFileAreaObservationalFromTemplate = true;
4757 47 : poDS->m_papszCreationOptions = CSLDuplicate(aosOptions.List());
4758 47 : poDS->m_bUseSrcLabel = aosOptions.FetchBool("USE_SRC_LABEL", true);
4759 47 : return poDS;
4760 : }
4761 :
4762 154 : if (nXSize == 0)
4763 0 : return nullptr;
4764 :
4765 154 : if (!(eType == GDT_UInt8 || eType == GDT_Int8 || eType == GDT_Int16 ||
4766 50 : eType == GDT_UInt16 || eType == GDT_Int32 || eType == GDT_UInt32 ||
4767 35 : eType == GDT_Int64 || eType == GDT_UInt64 || eType == GDT_Float32 ||
4768 20 : eType == GDT_Float64 || eType == GDT_CFloat32 ||
4769 10 : eType == GDT_CFloat64))
4770 : {
4771 6 : CPLError(
4772 : CE_Failure, CPLE_NotSupported,
4773 : "The PDS4 driver does not supporting creating files of type %s.",
4774 : GDALGetDataTypeName(eType));
4775 6 : return nullptr;
4776 : }
4777 :
4778 148 : if (nBandsIn == 0)
4779 : {
4780 1 : CPLError(CE_Failure, CPLE_NotSupported, "Invalid number of bands");
4781 1 : return nullptr;
4782 : }
4783 :
4784 : const char *pszArrayType =
4785 147 : aosOptions.FetchNameValueDef("ARRAY_TYPE", "Array_3D_Image");
4786 147 : const bool bIsArray2D = STARTS_WITH(pszArrayType, "Array_2D");
4787 147 : if (nBandsIn > 1 && bIsArray2D)
4788 : {
4789 1 : CPLError(CE_Failure, CPLE_NotSupported,
4790 : "ARRAY_TYPE=%s is not supported for a multi-band raster",
4791 : pszArrayType);
4792 1 : return nullptr;
4793 : }
4794 :
4795 : /* -------------------------------------------------------------------- */
4796 : /* Compute pixel, line and band offsets */
4797 : /* -------------------------------------------------------------------- */
4798 146 : const int nItemSize = GDALGetDataTypeSizeBytes(eType);
4799 : int nLineOffset, nPixelOffset;
4800 : vsi_l_offset nBandOffset;
4801 :
4802 : const char *pszInterleave =
4803 146 : aosOptions.FetchNameValueDef("INTERLEAVE", "BSQ");
4804 146 : if (bIsArray2D)
4805 1 : pszInterleave = "BIP";
4806 :
4807 146 : if (EQUAL(pszInterleave, "BIP"))
4808 : {
4809 4 : nPixelOffset = nItemSize * nBandsIn;
4810 4 : if (nPixelOffset > INT_MAX / nBandsIn)
4811 : {
4812 0 : return nullptr;
4813 : }
4814 4 : nLineOffset = nPixelOffset * nXSize;
4815 4 : nBandOffset = nItemSize;
4816 : }
4817 142 : else if (EQUAL(pszInterleave, "BSQ"))
4818 : {
4819 138 : nPixelOffset = nItemSize;
4820 138 : if (nPixelOffset > INT_MAX / nXSize)
4821 : {
4822 0 : return nullptr;
4823 : }
4824 138 : nLineOffset = nPixelOffset * nXSize;
4825 138 : nBandOffset = static_cast<vsi_l_offset>(nLineOffset) * nYSize;
4826 : }
4827 4 : else if (EQUAL(pszInterleave, "BIL"))
4828 : {
4829 3 : nPixelOffset = nItemSize;
4830 3 : if (nPixelOffset > INT_MAX / nBandsIn ||
4831 3 : nPixelOffset * nBandsIn > INT_MAX / nXSize)
4832 : {
4833 0 : return nullptr;
4834 : }
4835 3 : nLineOffset = nItemSize * nBandsIn * nXSize;
4836 3 : nBandOffset = static_cast<vsi_l_offset>(nItemSize) * nXSize;
4837 : }
4838 : else
4839 : {
4840 1 : CPLError(CE_Failure, CPLE_NotSupported, "Invalid value for INTERLEAVE");
4841 1 : return nullptr;
4842 : }
4843 :
4844 : const char *pszImageFormat =
4845 145 : aosOptions.FetchNameValueDef("IMAGE_FORMAT", "RAW");
4846 145 : const char *pszImageExtension = aosOptions.FetchNameValueDef(
4847 145 : "IMAGE_EXTENSION", EQUAL(pszImageFormat, "RAW") ? "img" : "tif");
4848 : CPLString osImageFilename(aosOptions.FetchNameValueDef(
4849 : "IMAGE_FILENAME",
4850 290 : CPLResetExtensionSafe(pszFilename, pszImageExtension).c_str()));
4851 :
4852 145 : const bool bAppend = aosOptions.FetchBool("APPEND_SUBDATASET", false);
4853 145 : if (bAppend)
4854 : {
4855 4 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
4856 4 : auto poExistingPDS4 = OpenInternal(&oOpenInfo);
4857 4 : if (!poExistingPDS4)
4858 : {
4859 0 : return nullptr;
4860 : }
4861 4 : osImageFilename = poExistingPDS4->m_osImageFilename;
4862 4 : poExistingPDS4.reset();
4863 :
4864 : auto poImageDS = std::unique_ptr<GDALDataset>(
4865 8 : GDALDataset::Open(osImageFilename, GDAL_OF_RASTER));
4866 6 : if (poImageDS && poImageDS->GetDriver() &&
4867 2 : EQUAL(poImageDS->GetDriver()->GetDescription(), "GTiff"))
4868 : {
4869 2 : pszImageFormat = "GEOTIFF";
4870 : }
4871 : }
4872 :
4873 145 : GDALDataset *poExternalDS = nullptr;
4874 145 : VSILFILE *fpImage = nullptr;
4875 145 : vsi_l_offset nBaseOffset = 0;
4876 145 : bool bIsLSB = true;
4877 290 : CPLString osHeaderParsingStandard;
4878 : const bool bCreateLabelOnly =
4879 145 : aosOptions.FetchBool("CREATE_LABEL_ONLY", false);
4880 145 : if (bCreateLabelOnly)
4881 : {
4882 8 : if (poSrcDS == nullptr)
4883 : {
4884 0 : CPLError(
4885 : CE_Failure, CPLE_AppDefined,
4886 : "CREATE_LABEL_ONLY is only compatible of CreateCopy() mode");
4887 1 : return nullptr;
4888 : }
4889 8 : RawBinaryLayout sLayout;
4890 8 : if (!poSrcDS->GetRawBinaryLayout(sLayout))
4891 : {
4892 1 : CPLError(CE_Failure, CPLE_AppDefined,
4893 : "Source dataset is not compatible of a raw binary format");
4894 1 : return nullptr;
4895 : }
4896 7 : if ((nBandsIn > 1 &&
4897 7 : sLayout.eInterleaving == RawBinaryLayout::Interleaving::UNKNOWN) ||
4898 6 : (nBandsIn == 1 &&
4899 6 : !(sLayout.nPixelOffset == nItemSize &&
4900 6 : sLayout.nLineOffset == sLayout.nPixelOffset * nXSize)))
4901 : {
4902 0 : CPLError(CE_Failure, CPLE_AppDefined,
4903 : "Source dataset has an interleaving not handled in PDS4");
4904 0 : return nullptr;
4905 : }
4906 7 : fpImage = VSIFOpenL(sLayout.osRawFilename.c_str(), "rb");
4907 7 : if (fpImage == nullptr)
4908 : {
4909 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot open raw image %s",
4910 : sLayout.osRawFilename.c_str());
4911 0 : return nullptr;
4912 : }
4913 7 : osImageFilename = sLayout.osRawFilename;
4914 7 : if (nBandsIn == 1 ||
4915 1 : sLayout.eInterleaving == RawBinaryLayout::Interleaving::BIP)
4916 7 : pszInterleave = "BIP";
4917 0 : else if (sLayout.eInterleaving == RawBinaryLayout::Interleaving::BIL)
4918 0 : pszInterleave = "BIL";
4919 : else
4920 0 : pszInterleave = "BSQ";
4921 7 : nBaseOffset = sLayout.nImageOffset;
4922 7 : nPixelOffset = static_cast<int>(sLayout.nPixelOffset);
4923 7 : nLineOffset = static_cast<int>(sLayout.nLineOffset);
4924 7 : nBandOffset = static_cast<vsi_l_offset>(sLayout.nBandOffset);
4925 7 : bIsLSB = sLayout.bLittleEndianOrder;
4926 7 : auto poSrcDriver = poSrcDS->GetDriver();
4927 7 : if (poSrcDriver)
4928 : {
4929 7 : auto pszDriverName = poSrcDriver->GetDescription();
4930 7 : if (EQUAL(pszDriverName, "GTiff"))
4931 : {
4932 2 : GByte abySignature[4] = {0};
4933 2 : VSIFReadL(abySignature, 1, 4, fpImage);
4934 2 : const bool bBigTIFF =
4935 2 : abySignature[2] == 43 || abySignature[3] == 43;
4936 : osHeaderParsingStandard =
4937 2 : bBigTIFF ? BIGTIFF_GEOTIFF_STRING : TIFF_GEOTIFF_STRING;
4938 : }
4939 5 : else if (EQUAL(pszDriverName, "ISIS3"))
4940 : {
4941 1 : osHeaderParsingStandard = "ISIS3";
4942 : }
4943 4 : else if (EQUAL(pszDriverName, "VICAR"))
4944 : {
4945 1 : osHeaderParsingStandard = "VICAR2";
4946 : }
4947 3 : else if (EQUAL(pszDriverName, "PDS"))
4948 : {
4949 1 : osHeaderParsingStandard = "PDS3";
4950 : }
4951 2 : else if (EQUAL(pszDriverName, "FITS"))
4952 : {
4953 1 : osHeaderParsingStandard = "FITS 3.0";
4954 : aosOptions.SetNameValue("VAR_VERTICAL_DISPLAY_DIRECTION",
4955 1 : "Bottom to Top");
4956 : }
4957 : }
4958 : }
4959 137 : else if (EQUAL(pszImageFormat, "GEOTIFF"))
4960 : {
4961 20 : if (EQUAL(pszInterleave, "BIL"))
4962 : {
4963 2 : if (aosOptions.FetchBool("@INTERLEAVE_ADDED_AUTOMATICALLY", false))
4964 : {
4965 1 : pszInterleave = "BSQ";
4966 : }
4967 : else
4968 : {
4969 1 : CPLError(CE_Failure, CPLE_AppDefined,
4970 : "INTERLEAVE=BIL not supported for GeoTIFF in PDS4");
4971 1 : return nullptr;
4972 : }
4973 : }
4974 : GDALDriver *poDrv =
4975 19 : static_cast<GDALDriver *>(GDALGetDriverByName("GTiff"));
4976 19 : if (poDrv == nullptr)
4977 : {
4978 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find GTiff driver");
4979 0 : return nullptr;
4980 : }
4981 19 : char **papszGTiffOptions = nullptr;
4982 : #ifdef notdef
4983 : // In practice I can't see which option we can really use
4984 : const char *pszGTiffOptions =
4985 : CSLFetchNameValueDef(papszOptions, "GEOTIFF_OPTIONS", "");
4986 : char **papszTokens = CSLTokenizeString2(pszGTiffOptions, ",", 0);
4987 : if (CPLFetchBool(papszTokens, "TILED", false))
4988 : {
4989 : CSLDestroy(papszTokens);
4990 : CPLError(CE_Failure, CPLE_AppDefined,
4991 : "Tiled GeoTIFF is not supported for PDS4");
4992 : return NULL;
4993 : }
4994 : if (!EQUAL(CSLFetchNameValueDef(papszTokens, "COMPRESS", "NONE"),
4995 : "NONE"))
4996 : {
4997 : CSLDestroy(papszTokens);
4998 : CPLError(CE_Failure, CPLE_AppDefined,
4999 : "Compressed GeoTIFF is not supported for PDS4");
5000 : return NULL;
5001 : }
5002 : papszGTiffOptions =
5003 : CSLSetNameValue(papszGTiffOptions, "ENDIANNESS", "LITTLE");
5004 : for (int i = 0; papszTokens[i] != NULL; i++)
5005 : {
5006 : papszGTiffOptions = CSLAddString(papszGTiffOptions, papszTokens[i]);
5007 : }
5008 : CSLDestroy(papszTokens);
5009 : #endif
5010 :
5011 : papszGTiffOptions =
5012 19 : CSLSetNameValue(papszGTiffOptions, "INTERLEAVE",
5013 19 : EQUAL(pszInterleave, "BSQ") ? "BAND" : "PIXEL");
5014 : // Will make sure that our blocks at nodata are not optimized
5015 : // away but indeed well written
5016 19 : papszGTiffOptions = CSLSetNameValue(
5017 : papszGTiffOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "YES");
5018 19 : if (nBandsIn > 1 && EQUAL(pszInterleave, "BSQ"))
5019 : {
5020 : papszGTiffOptions =
5021 2 : CSLSetNameValue(papszGTiffOptions, "BLOCKYSIZE", "1");
5022 : }
5023 :
5024 19 : if (bAppend)
5025 : {
5026 : papszGTiffOptions =
5027 2 : CSLAddString(papszGTiffOptions, "APPEND_SUBDATASET=YES");
5028 : }
5029 :
5030 19 : poExternalDS = poDrv->Create(osImageFilename, nXSize, nYSize, nBandsIn,
5031 : eType, papszGTiffOptions);
5032 19 : CSLDestroy(papszGTiffOptions);
5033 19 : if (poExternalDS == nullptr)
5034 : {
5035 1 : CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s",
5036 : osImageFilename.c_str());
5037 1 : return nullptr;
5038 : }
5039 : }
5040 : else
5041 : {
5042 232 : fpImage = VSIFOpenL(
5043 : osImageFilename,
5044 : bAppend ? "rb+"
5045 115 : : VSISupportsRandomWrite(osImageFilename.c_str(), true) ? "wb+"
5046 : : "wb");
5047 117 : if (fpImage == nullptr)
5048 : {
5049 3 : CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s",
5050 : osImageFilename.c_str());
5051 3 : return nullptr;
5052 : }
5053 114 : if (bAppend)
5054 : {
5055 2 : VSIFSeekL(fpImage, 0, SEEK_END);
5056 2 : nBaseOffset = VSIFTellL(fpImage);
5057 : }
5058 : }
5059 :
5060 278 : auto poDS = std::make_unique<PDS4Dataset>();
5061 139 : poDS->SetDescription(pszFilename);
5062 139 : poDS->m_bMustInitImageFile = true;
5063 139 : poDS->m_fpImage = fpImage;
5064 139 : poDS->m_nBaseOffset = nBaseOffset;
5065 139 : poDS->m_poExternalDS = poExternalDS;
5066 139 : poDS->nRasterXSize = nXSize;
5067 139 : poDS->nRasterYSize = nYSize;
5068 139 : poDS->eAccess = GA_Update;
5069 139 : poDS->m_osImageFilename = std::move(osImageFilename);
5070 139 : poDS->m_bCreateHeader = true;
5071 139 : poDS->m_bStripFileAreaObservationalFromTemplate = true;
5072 139 : poDS->m_osInterleave = pszInterleave;
5073 139 : poDS->m_papszCreationOptions = CSLDuplicate(aosOptions.List());
5074 139 : poDS->m_bUseSrcLabel = aosOptions.FetchBool("USE_SRC_LABEL", true);
5075 139 : poDS->m_bIsLSB = bIsLSB;
5076 139 : poDS->m_osHeaderParsingStandard = std::move(osHeaderParsingStandard);
5077 139 : poDS->m_bCreatedFromExistingBinaryFile = bCreateLabelOnly;
5078 :
5079 139 : if (EQUAL(pszInterleave, "BIP"))
5080 : {
5081 10 : poDS->GDALDataset::SetMetadataItem("INTERLEAVE", "PIXEL",
5082 : "IMAGE_STRUCTURE");
5083 : }
5084 129 : else if (EQUAL(pszInterleave, "BSQ"))
5085 : {
5086 128 : poDS->GDALDataset::SetMetadataItem("INTERLEAVE", "BAND",
5087 : "IMAGE_STRUCTURE");
5088 : }
5089 :
5090 338 : for (int i = 0; i < nBandsIn; i++)
5091 : {
5092 199 : if (poDS->m_poExternalDS != nullptr)
5093 : {
5094 : auto poBand = std::make_unique<PDS4WrapperRasterBand>(
5095 24 : poDS->m_poExternalDS->GetRasterBand(i + 1));
5096 24 : poDS->SetBand(i + 1, std::move(poBand));
5097 : }
5098 : else
5099 : {
5100 : auto poBand = std::make_unique<PDS4RawRasterBand>(
5101 175 : poDS.get(), i + 1, poDS->m_fpImage,
5102 175 : poDS->m_nBaseOffset + nBandOffset * i, nPixelOffset,
5103 : nLineOffset, eType,
5104 175 : bIsLSB ? RawRasterBand::ByteOrder::ORDER_LITTLE_ENDIAN
5105 175 : : RawRasterBand::ByteOrder::ORDER_BIG_ENDIAN);
5106 175 : poDS->SetBand(i + 1, std::move(poBand));
5107 : }
5108 : }
5109 :
5110 139 : return poDS;
5111 : }
5112 :
5113 : /************************************************************************/
5114 : /* PDS4GetUnderlyingDataset() */
5115 : /************************************************************************/
5116 :
5117 65 : static GDALDataset *PDS4GetUnderlyingDataset(GDALDataset *poSrcDS)
5118 : {
5119 130 : if (poSrcDS->GetDriver() != nullptr &&
5120 65 : poSrcDS->GetDriver() == GDALGetDriverByName("VRT"))
5121 : {
5122 4 : VRTDataset *poVRTDS = cpl::down_cast<VRTDataset *>(poSrcDS);
5123 4 : poSrcDS = poVRTDS->GetSingleSimpleSource();
5124 : }
5125 :
5126 65 : return poSrcDS;
5127 : }
5128 :
5129 : /************************************************************************/
5130 : /* CreateCopy() */
5131 : /************************************************************************/
5132 :
5133 65 : GDALDataset *PDS4Dataset::CreateCopy(const char *pszFilename,
5134 : GDALDataset *poSrcDS, int bStrict,
5135 : CSLConstList papszOptions,
5136 : GDALProgressFunc pfnProgress,
5137 : void *pProgressData)
5138 : {
5139 : const char *pszImageFormat =
5140 65 : CSLFetchNameValueDef(papszOptions, "IMAGE_FORMAT", "RAW");
5141 65 : GDALDataset *poSrcUnderlyingDS = PDS4GetUnderlyingDataset(poSrcDS);
5142 65 : if (poSrcUnderlyingDS == nullptr)
5143 1 : poSrcUnderlyingDS = poSrcDS;
5144 73 : if (EQUAL(pszImageFormat, "GEOTIFF") &&
5145 8 : strcmp(poSrcUnderlyingDS->GetDescription(),
5146 : CSLFetchNameValueDef(
5147 : papszOptions, "IMAGE_FILENAME",
5148 73 : CPLResetExtensionSafe(pszFilename, "tif").c_str())) == 0)
5149 : {
5150 1 : CPLError(CE_Failure, CPLE_NotSupported,
5151 : "Output file has same name as input file");
5152 1 : return nullptr;
5153 : }
5154 64 : if (poSrcDS->GetRasterCount() == 0)
5155 : {
5156 1 : CPLError(CE_Failure, CPLE_NotSupported, "Unsupported band count");
5157 1 : return nullptr;
5158 : }
5159 :
5160 63 : const bool bAppend = CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
5161 63 : if (bAppend)
5162 : {
5163 6 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
5164 6 : auto poExistingDS = OpenInternal(&oOpenInfo);
5165 6 : if (poExistingDS)
5166 : {
5167 6 : GDALGeoTransform existingGT;
5168 : const bool bExistingHasGT =
5169 6 : poExistingDS->GetGeoTransform(existingGT) == CE_None;
5170 6 : GDALGeoTransform gt;
5171 6 : const bool bSrcHasGT = poSrcDS->GetGeoTransform(gt) == CE_None;
5172 :
5173 6 : CPLString osExistingProj4;
5174 6 : if (const auto poExistingSRS = poExistingDS->GetSpatialRef())
5175 : {
5176 6 : char *pszExistingProj4 = nullptr;
5177 6 : poExistingSRS->exportToProj4(&pszExistingProj4);
5178 6 : if (pszExistingProj4)
5179 6 : osExistingProj4 = pszExistingProj4;
5180 6 : CPLFree(pszExistingProj4);
5181 : }
5182 6 : CPLString osSrcProj4;
5183 6 : if (const auto poSrcSRS = poSrcDS->GetSpatialRef())
5184 : {
5185 6 : char *pszSrcProj4 = nullptr;
5186 6 : poSrcSRS->exportToProj4(&pszSrcProj4);
5187 6 : if (pszSrcProj4)
5188 6 : osSrcProj4 = pszSrcProj4;
5189 6 : CPLFree(pszSrcProj4);
5190 : }
5191 :
5192 6 : poExistingDS.reset();
5193 :
5194 : const auto maxRelErrorGT =
5195 6 : [](const GDALGeoTransform >1, const GDALGeoTransform >2)
5196 : {
5197 6 : double maxRelError = 0.0;
5198 42 : for (int i = 0; i < 6; i++)
5199 : {
5200 36 : if (gt1[i] == 0.0)
5201 : {
5202 12 : maxRelError = std::max(maxRelError, std::abs(gt2[i]));
5203 : }
5204 : else
5205 : {
5206 24 : maxRelError =
5207 48 : std::max(maxRelError, std::abs(gt2[i] - gt1[i]) /
5208 24 : std::abs(gt1[i]));
5209 : }
5210 : }
5211 6 : return maxRelError;
5212 : };
5213 :
5214 6 : if ((bExistingHasGT && !bSrcHasGT) ||
5215 18 : (!bExistingHasGT && bSrcHasGT) ||
5216 6 : (bExistingHasGT && bSrcHasGT &&
5217 6 : maxRelErrorGT(existingGT, gt) > 1e-10))
5218 : {
5219 1 : CPLError(bStrict ? CE_Failure : CE_Warning, CPLE_NotSupported,
5220 : "Appending to a dataset with a different "
5221 : "geotransform is not supported");
5222 1 : if (bStrict)
5223 1 : return nullptr;
5224 : }
5225 : // Do proj string comparison, as it is unlikely that
5226 : // OGRSpatialReference::IsSame() will lead to identical reasons due
5227 : // to PDS changing CRS names, etc...
5228 5 : if (osExistingProj4 != osSrcProj4)
5229 : {
5230 1 : CPLError(bStrict ? CE_Failure : CE_Warning, CPLE_NotSupported,
5231 : "Appending to a dataset with a different "
5232 : "coordinate reference system is not supported");
5233 1 : if (bStrict)
5234 1 : return nullptr;
5235 : }
5236 : }
5237 : }
5238 :
5239 61 : const int nXSize = poSrcDS->GetRasterXSize();
5240 61 : const int nYSize = poSrcDS->GetRasterYSize();
5241 61 : const int nBands = poSrcDS->GetRasterCount();
5242 61 : GDALDataType eType = poSrcDS->GetRasterBand(1)->GetRasterDataType();
5243 : auto poDS = CreateInternal(pszFilename, poSrcDS, nXSize, nYSize, nBands,
5244 122 : eType, papszOptions);
5245 61 : if (poDS == nullptr)
5246 6 : return nullptr;
5247 :
5248 55 : GDALGeoTransform gt;
5249 55 : if (poSrcDS->GetGeoTransform(gt) == CE_None && gt != GDALGeoTransform())
5250 : {
5251 52 : poDS->SetGeoTransform(gt);
5252 : }
5253 :
5254 110 : if (poSrcDS->GetProjectionRef() != nullptr &&
5255 55 : strlen(poSrcDS->GetProjectionRef()) > 0)
5256 : {
5257 52 : poDS->SetProjection(poSrcDS->GetProjectionRef());
5258 : }
5259 :
5260 137 : for (int i = 1; i <= nBands; i++)
5261 : {
5262 82 : int bHasNoData = false;
5263 :
5264 82 : if (poSrcDS->GetRasterBand(i)->GetRasterDataType() == GDT_Int64)
5265 : {
5266 : const auto nNoData =
5267 0 : poSrcDS->GetRasterBand(i)->GetNoDataValueAsInt64(&bHasNoData);
5268 0 : if (bHasNoData)
5269 0 : poDS->GetRasterBand(i)->SetNoDataValueAsInt64(nNoData);
5270 : }
5271 82 : else if (poSrcDS->GetRasterBand(i)->GetRasterDataType() == GDT_UInt64)
5272 : {
5273 : const auto nNoData =
5274 0 : poSrcDS->GetRasterBand(i)->GetNoDataValueAsUInt64(&bHasNoData);
5275 0 : if (bHasNoData)
5276 0 : poDS->GetRasterBand(i)->SetNoDataValueAsUInt64(nNoData);
5277 : }
5278 : else
5279 : {
5280 : const double dfNoData =
5281 82 : poSrcDS->GetRasterBand(i)->GetNoDataValue(&bHasNoData);
5282 82 : if (bHasNoData)
5283 14 : poDS->GetRasterBand(i)->SetNoDataValue(dfNoData);
5284 : }
5285 :
5286 82 : const double dfOffset = poSrcDS->GetRasterBand(i)->GetOffset();
5287 82 : if (dfOffset != 0.0)
5288 3 : poDS->GetRasterBand(i)->SetOffset(dfOffset);
5289 :
5290 82 : const double dfScale = poSrcDS->GetRasterBand(i)->GetScale();
5291 82 : if (dfScale != 1.0)
5292 3 : poDS->GetRasterBand(i)->SetScale(dfScale);
5293 :
5294 164 : poDS->GetRasterBand(i)->SetUnitType(
5295 82 : poSrcDS->GetRasterBand(i)->GetUnitType());
5296 : }
5297 :
5298 55 : if (poDS->m_bUseSrcLabel)
5299 : {
5300 53 : CSLConstList papszMD_PDS4 = poSrcDS->GetMetadata("xml:PDS4");
5301 53 : if (papszMD_PDS4 != nullptr)
5302 : {
5303 6 : poDS->SetMetadata(papszMD_PDS4, "xml:PDS4");
5304 : }
5305 : }
5306 :
5307 55 : if (poDS->m_poExternalDS == nullptr)
5308 : {
5309 : // We don't need to initialize the imagery as we are going to copy it
5310 : // completely
5311 46 : poDS->m_bMustInitImageFile = false;
5312 : }
5313 :
5314 55 : if (!CPLFetchBool(papszOptions, "CREATE_LABEL_ONLY", false))
5315 : {
5316 48 : CPLErr eErr = GDALDatasetCopyWholeRaster(poSrcDS, poDS.get(), nullptr,
5317 : pfnProgress, pProgressData);
5318 48 : poDS->FlushCache(false);
5319 48 : if (eErr != CE_None)
5320 : {
5321 1 : return nullptr;
5322 : }
5323 :
5324 47 : if (CPLFetchBool(papszOptions, "PROPAGATE_SRC_METADATA", true))
5325 : {
5326 46 : CSLConstList papszISIS3MD = poSrcDS->GetMetadata("json:ISIS3");
5327 46 : if (papszISIS3MD)
5328 : {
5329 2 : poDS->SetMetadata(papszISIS3MD, "json:ISIS3");
5330 :
5331 2 : if (poDS->m_poExternalDS)
5332 1 : poDS->m_poExternalDS->SetMetadata(papszISIS3MD,
5333 1 : "json:ISIS3");
5334 : }
5335 : }
5336 : }
5337 :
5338 54 : return poDS.release();
5339 : }
5340 :
5341 : /************************************************************************/
5342 : /* Delete() */
5343 : /************************************************************************/
5344 :
5345 109 : CPLErr PDS4Dataset::Delete(const char *pszFilename)
5346 :
5347 : {
5348 : /* -------------------------------------------------------------------- */
5349 : /* Collect file list. */
5350 : /* -------------------------------------------------------------------- */
5351 218 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
5352 218 : auto poDS = PDS4Dataset::OpenInternal(&oOpenInfo);
5353 109 : if (poDS == nullptr)
5354 : {
5355 0 : if (CPLGetLastErrorNo() == 0)
5356 0 : CPLError(CE_Failure, CPLE_OpenFailed,
5357 : "Unable to open %s to obtain file list.", pszFilename);
5358 :
5359 0 : return CE_Failure;
5360 : }
5361 :
5362 109 : char **papszFileList = poDS->GetFileList();
5363 218 : CPLString osImageFilename = poDS->m_osImageFilename;
5364 : bool bCreatedFromExistingBinaryFile =
5365 109 : poDS->m_bCreatedFromExistingBinaryFile;
5366 :
5367 109 : poDS.reset();
5368 :
5369 109 : if (CSLCount(papszFileList) == 0)
5370 : {
5371 0 : CPLError(CE_Failure, CPLE_NotSupported,
5372 : "Unable to determine files associated with %s, "
5373 : "delete fails.",
5374 : pszFilename);
5375 0 : CSLDestroy(papszFileList);
5376 0 : return CE_Failure;
5377 : }
5378 :
5379 : /* -------------------------------------------------------------------- */
5380 : /* Delete all files. */
5381 : /* -------------------------------------------------------------------- */
5382 109 : CPLErr eErr = CE_None;
5383 392 : for (int i = 0; papszFileList[i] != nullptr; ++i)
5384 : {
5385 297 : if (bCreatedFromExistingBinaryFile &&
5386 14 : EQUAL(papszFileList[i], osImageFilename))
5387 : {
5388 7 : continue;
5389 : }
5390 276 : if (VSIUnlink(papszFileList[i]) != 0)
5391 : {
5392 0 : CPLError(CE_Failure, CPLE_AppDefined, "Deleting %s failed:\n%s",
5393 0 : papszFileList[i], VSIStrerror(errno));
5394 0 : eErr = CE_Failure;
5395 : }
5396 : }
5397 :
5398 109 : CSLDestroy(papszFileList);
5399 :
5400 109 : return eErr;
5401 : }
5402 :
5403 : /************************************************************************/
5404 : /* GDALRegister_PDS4() */
5405 : /************************************************************************/
5406 :
5407 2063 : void GDALRegister_PDS4()
5408 :
5409 : {
5410 2063 : if (GDALGetDriverByName(PDS4_DRIVER_NAME) != nullptr)
5411 283 : return;
5412 :
5413 1780 : GDALDriver *poDriver = new GDALDriver();
5414 1780 : PDS4DriverSetCommonMetadata(poDriver);
5415 :
5416 1780 : poDriver->pfnOpen = PDS4Dataset::Open;
5417 1780 : poDriver->pfnCreate = PDS4Dataset::Create;
5418 1780 : poDriver->pfnCreateCopy = PDS4Dataset::CreateCopy;
5419 1780 : poDriver->pfnDelete = PDS4Dataset::Delete;
5420 :
5421 1780 : GetGDALDriverManager()->RegisterDriver(poDriver);
5422 : }
|