Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: ISIS Version 3 Driver
4 : * Purpose: Implementation of ISIS3Dataset
5 : * Author: Trent Hare (thare@usgs.gov)
6 : * Frank Warmerdam (warmerdam@pobox.com)
7 : * Even Rouault (even.rouault at spatialys.com)
8 : *
9 : * NOTE: Original code authored by Trent and placed in the public domain as
10 : * per US government policy. I have (within my rights) appropriated it and
11 : * placed it under the following license. This is not intended to diminish
12 : * Trents contribution.
13 : ******************************************************************************
14 : * Copyright (c) 2007, Frank Warmerdam <warmerdam@pobox.com>
15 : * Copyright (c) 2009-2010, Even Rouault <even.rouault at spatialys.com>
16 : * Copyright (c) 2017 Hobu Inc
17 : * Copyright (c) 2017, Dmitry Baryshnikov <polimax@mail.ru>
18 : * Copyright (c) 2017, NextGIS <info@nextgis.com>
19 : *
20 : * SPDX-License-Identifier: MIT
21 : ****************************************************************************/
22 :
23 : #include "cpl_json.h"
24 : #include "cpl_string.h"
25 : #include "cpl_time.h"
26 : #include "cpl_vsi_error.h"
27 : #include "cpl_vsi_virtual.h"
28 : #include "gdal_frmts.h"
29 : #include "gdal_proxy.h"
30 : #include "nasakeywordhandler.h"
31 : #include "ogrgeojsonreader.h"
32 : #include "ogr_spatialref.h"
33 : #include "rawdataset.h"
34 : #include "vrtdataset.h"
35 : #include "cpl_safemaths.hpp"
36 : #include "pdsdrivercore.h"
37 : #include "json_utils.h"
38 :
39 : // For gethostname()
40 : #ifdef _WIN32
41 : #include <winsock2.h>
42 : #else
43 : #include <unistd.h>
44 : #endif
45 :
46 : #include <algorithm>
47 : #include <cinttypes>
48 : #include <map>
49 : #include <utility> // pair
50 : #include <vector>
51 :
52 : // Constants coming from ISIS3 source code
53 : // in isis/src/base/objs/SpecialPixel/SpecialPixel.h
54 :
55 : // There are several types of special pixels
56 : // * Isis::Null Pixel has no data available
57 : // * Isis::Lis Pixel was saturated on the instrument
58 : // * Isis::His Pixel was saturated on the instrument
59 : // * Isis::Lrs Pixel was saturated during a computation
60 : // * Isis::Hrs Pixel was saturated during a computation
61 :
62 : // 1-byte special pixel values
63 : const unsigned char ISIS3_NULL1 = 0;
64 : const unsigned char LOW_REPR_SAT1 = 0;
65 : const unsigned char LOW_INSTR_SAT1 = 0;
66 : const unsigned char HIGH_INSTR_SAT1 = 255;
67 : const unsigned char HIGH_REPR_SAT1 = 255;
68 :
69 : // 2-byte unsigned special pixel values
70 : const unsigned short ISIS3_NULLU2 = 0;
71 : const unsigned short LOW_REPR_SATU2 = 1;
72 : const unsigned short LOW_INSTR_SATU2 = 2;
73 : const unsigned short HIGH_INSTR_SATU2 = 65534;
74 : const unsigned short HIGH_REPR_SATU2 = 65535;
75 :
76 : // 2-byte signed special pixel values
77 : const short ISIS3_NULL2 = -32768;
78 : const short LOW_REPR_SAT2 = -32767;
79 : const short LOW_INSTR_SAT2 = -32766;
80 : const short HIGH_INSTR_SAT2 = -32765;
81 : const short HIGH_REPR_SAT2 = -32764;
82 :
83 : // Define 4-byte special pixel values for IEEE floating point
84 : const float ISIS3_NULL4 = -3.4028226550889045e+38f; // 0xFF7FFFFB;
85 : const float LOW_REPR_SAT4 = -3.4028228579130005e+38f; // 0xFF7FFFFC;
86 : const float LOW_INSTR_SAT4 = -3.4028230607370965e+38f; // 0xFF7FFFFD;
87 : const float HIGH_INSTR_SAT4 = -3.4028232635611926e+38f; // 0xFF7FFFFE;
88 : const float HIGH_REPR_SAT4 = -3.4028234663852886e+38f; // 0xFF7FFFFF;
89 :
90 : // Must be large enough to hold an integer
91 : static const char *const pszSTARTBYTE_PLACEHOLDER = "!*^STARTBYTE^*!";
92 : // Must be large enough to hold an integer
93 : static const char *const pszLABEL_BYTES_PLACEHOLDER = "!*^LABEL_BYTES^*!";
94 : // Must be large enough to hold an integer
95 : static const char *const pszHISTORY_STARTBYTE_PLACEHOLDER =
96 : "!*^HISTORY_STARTBYTE^*!";
97 :
98 : /************************************************************************/
99 : /* ==================================================================== */
100 : /* ISISDataset */
101 : /* ==================================================================== */
102 : /************************************************************************/
103 :
104 : class ISIS3Dataset final : public RawDataset
105 : {
106 : friend class ISIS3RawRasterBand;
107 : friend class ISISTiledBand;
108 : friend class ISIS3WrapperRasterBand;
109 :
110 : class NonPixelSection
111 : {
112 : public:
113 : CPLString osSrcFilename{};
114 : CPLString osDstFilename{}; // empty for same file
115 : vsi_l_offset nSrcOffset{};
116 : vsi_l_offset nSize{};
117 : CPLString osPlaceHolder{}; // empty if not same file
118 : };
119 :
120 : VSILFILE *m_fpLabel{}; // label file (only used for writing)
121 : VSILFILE *m_fpImage{}; // image data file. May be == fpLabel
122 : GDALDataset *m_poExternalDS{}; // external dataset (GeoTIFF)
123 : bool m_bGeoTIFFAsRegularExternal{}; // creation only
124 : bool m_bGeoTIFFInitDone{true}; // creation only
125 :
126 : CPLString m_osExternalFilename{};
127 : bool m_bIsLabelWritten{true}; // creation only
128 :
129 : bool m_bIsTiled{};
130 : bool m_bInitToNodata{}; // creation only
131 :
132 : NASAKeywordHandler m_oKeywords{};
133 :
134 : bool m_bGotTransform{};
135 : GDALGeoTransform m_gt{};
136 :
137 : bool m_bHasSrcNoData{}; // creation only
138 : double m_dfSrcNoData{}; // creation only
139 :
140 : OGRSpatialReference m_oSRS{};
141 :
142 : // creation only variables
143 : CPLString m_osComment{};
144 : CPLString m_osLatitudeType{};
145 : CPLString m_osLongitudeDirection{};
146 : CPLString m_osTargetName{};
147 : bool m_bForce360{};
148 : bool m_bWriteBoundingDegrees{true};
149 : CPLString m_osBoundingDegrees{};
150 :
151 : CPLJSONObject m_oJSonLabel{};
152 : CPLString m_osHistory{}; // creation only
153 : bool m_bUseSrcLabel{true}; // creation only
154 : bool m_bUseSrcMapping{}; // creation only
155 : bool m_bUseSrcHistory{true}; // creation only
156 : bool m_bAddGDALHistory{true}; // creation only
157 : CPLString m_osGDALHistory{}; // creation only
158 : std::vector<NonPixelSection> m_aoNonPixelSections{}; // creation only
159 : CPLJSONObject m_oSrcJSonLabel{}; // creation only
160 : CPLStringList m_aosISIS3MD{};
161 : CPLStringList m_aosAdditionalFiles{};
162 : CPLString m_osFromFilename{}; // creation only
163 :
164 : RawBinaryLayout m_sLayout{};
165 :
166 : bool m_bResolveOfflineContent = true;
167 : int m_nMaxOfflineContentSize = 100000000;
168 : bool m_bHasResolvedOfflineContent = false;
169 :
170 : const char *GetKeyword(const char *pszPath, const char *pszDefault = "");
171 :
172 : double FixLong(double dfLong);
173 : void BuildLabel();
174 : void BuildHistory();
175 : void WriteLabel();
176 : void InvalidateLabel();
177 : void ResolveOfflineContentOfLabel();
178 :
179 : static CPLString SerializeAsPDL(const CPLJSONObject &oObj);
180 : static void SerializeAsPDL(VSILFILE *fp, const CPLJSONObject &oObj,
181 : int nDepth = 0);
182 :
183 : CPL_DISALLOW_COPY_ASSIGN(ISIS3Dataset)
184 :
185 : protected:
186 : CPLErr Close(GDALProgressFunc = nullptr, void * = nullptr) override;
187 :
188 : public:
189 : ISIS3Dataset();
190 : ~ISIS3Dataset() override;
191 :
192 : int CloseDependentDatasets() override;
193 :
194 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
195 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
196 :
197 : const OGRSpatialReference *GetSpatialRef() const override;
198 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
199 :
200 : char **GetFileList() override;
201 :
202 : char **GetMetadataDomainList() override;
203 : CSLConstList GetMetadata(const char *pszDomain = "") override;
204 : CPLErr SetMetadata(CSLConstList papszMD,
205 : const char *pszDomain = "") override;
206 :
207 : bool GetRawBinaryLayout(GDALDataset::RawBinaryLayout &) override;
208 :
209 : static GDALDataset *Open(GDALOpenInfo *);
210 : static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
211 : int nBandsIn, GDALDataType eType,
212 : CSLConstList papszOptions);
213 : static GDALDataset *CreateCopy(const char *pszFilename,
214 : GDALDataset *poSrcDS, int bStrict,
215 : CSLConstList papszOptions,
216 : GDALProgressFunc pfnProgress,
217 : void *pProgressData);
218 : };
219 :
220 : /************************************************************************/
221 : /* ==================================================================== */
222 : /* ISISTiledBand */
223 : /* ==================================================================== */
224 : /************************************************************************/
225 :
226 : class ISISTiledBand final : public GDALPamRasterBand
227 : {
228 : friend class ISIS3Dataset;
229 :
230 : VSILFILE *const m_fpVSIL{};
231 : GIntBig m_nFirstTileOffset{};
232 : GIntBig m_nXTileOffset{};
233 : GIntBig m_nYTileOffset{};
234 : const bool m_bNativeOrder{};
235 : bool m_bHasOffset{};
236 : bool m_bHasScale{};
237 : double m_dfOffset{};
238 : double m_dfScale{1.0};
239 : double m_dfNoData{};
240 : bool m_bValid = false;
241 :
242 : CPL_DISALLOW_COPY_ASSIGN(ISISTiledBand)
243 :
244 : public:
245 : ISISTiledBand(GDALDataset *poDS, VSILFILE *fpVSIL, int nBand,
246 : GDALDataType eDT, int nTileXSize, int nTileYSize,
247 : GIntBig nFirstTileOffset, GIntBig nXTileOffset,
248 : GIntBig nYTileOffset, int bNativeOrder);
249 :
250 41 : bool IsValid() const
251 : {
252 41 : return m_bValid;
253 : }
254 :
255 : CPLErr IReadBlock(int, int, void *) override;
256 : CPLErr IWriteBlock(int, int, void *) override;
257 :
258 : double GetOffset(int *pbSuccess = nullptr) override;
259 : double GetScale(int *pbSuccess = nullptr) override;
260 : CPLErr SetOffset(double dfNewOffset) override;
261 : CPLErr SetScale(double dfNewScale) override;
262 : double GetNoDataValue(int *pbSuccess = nullptr) override;
263 : CPLErr SetNoDataValue(double dfNewNoData) override;
264 :
265 : void SetMaskBand(std::unique_ptr<GDALRasterBand> poMaskBand);
266 : };
267 :
268 : /************************************************************************/
269 : /* ==================================================================== */
270 : /* ISIS3RawRasterBand */
271 : /* ==================================================================== */
272 : /************************************************************************/
273 :
274 : class ISIS3RawRasterBand final : public RawRasterBand
275 : {
276 : friend class ISIS3Dataset;
277 :
278 : bool m_bHasOffset;
279 : bool m_bHasScale;
280 : double m_dfOffset;
281 : double m_dfScale{1.0};
282 : double m_dfNoData;
283 :
284 : public:
285 : ISIS3RawRasterBand(GDALDataset *l_poDS, int l_nBand, VSILFILE *l_fpRaw,
286 : vsi_l_offset l_nImgOffset, int l_nPixelOffset,
287 : int l_nLineOffset, GDALDataType l_eDataType,
288 : int l_bNativeOrder);
289 :
290 : CPLErr IReadBlock(int, int, void *) override;
291 : CPLErr IWriteBlock(int, int, void *) override;
292 :
293 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
294 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
295 : GDALRasterIOExtraArg *psExtraArg) override;
296 :
297 : double GetOffset(int *pbSuccess = nullptr) override;
298 : double GetScale(int *pbSuccess = nullptr) override;
299 : CPLErr SetOffset(double dfNewOffset) override;
300 : CPLErr SetScale(double dfNewScale) override;
301 : double GetNoDataValue(int *pbSuccess = nullptr) override;
302 : CPLErr SetNoDataValue(double dfNewNoData) override;
303 :
304 : void SetMaskBand(std::unique_ptr<GDALRasterBand> poMaskBand);
305 : };
306 :
307 : /************************************************************************/
308 : /* ==================================================================== */
309 : /* ISIS3WrapperRasterBand */
310 : /* */
311 : /* proxy for bands stored in other formats. */
312 : /* ==================================================================== */
313 : /************************************************************************/
314 : class ISIS3WrapperRasterBand final : public GDALProxyRasterBand
315 : {
316 : friend class ISIS3Dataset;
317 :
318 : GDALRasterBand *m_poBaseBand{};
319 : bool m_bHasOffset{};
320 : bool m_bHasScale{};
321 : double m_dfOffset{};
322 : double m_dfScale{1.0};
323 : double m_dfNoData{};
324 :
325 : CPL_DISALLOW_COPY_ASSIGN(ISIS3WrapperRasterBand)
326 :
327 : protected:
328 : virtual GDALRasterBand *
329 127 : RefUnderlyingRasterBand(bool /* bForceOpen */) const override
330 : {
331 127 : return m_poBaseBand;
332 : }
333 :
334 : public:
335 : explicit ISIS3WrapperRasterBand(GDALRasterBand *poBaseBandIn);
336 :
337 : void InitFile();
338 :
339 : virtual CPLErr Fill(double dfRealValue,
340 : double dfImaginaryValue = 0) override;
341 : CPLErr IWriteBlock(int, int, void *) override;
342 :
343 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
344 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
345 : GDALRasterIOExtraArg *psExtraArg) override;
346 :
347 : double GetOffset(int *pbSuccess = nullptr) override;
348 : double GetScale(int *pbSuccess = nullptr) override;
349 : CPLErr SetOffset(double dfNewOffset) override;
350 : CPLErr SetScale(double dfNewScale) override;
351 : double GetNoDataValue(int *pbSuccess = nullptr) override;
352 : CPLErr SetNoDataValue(double dfNewNoData) override;
353 :
354 4 : int GetMaskFlags() override
355 : {
356 4 : return nMaskFlags;
357 : }
358 :
359 4 : GDALRasterBand *GetMaskBand() override
360 : {
361 4 : return poMask;
362 : }
363 :
364 : void SetMaskBand(std::unique_ptr<GDALRasterBand> poMaskBand);
365 : };
366 :
367 : /************************************************************************/
368 : /* ==================================================================== */
369 : /* ISISMaskBand */
370 : /* ==================================================================== */
371 :
372 : class ISISMaskBand final : public GDALRasterBand
373 : {
374 : GDALRasterBand *m_poBaseBand{};
375 : void *m_pBuffer{};
376 :
377 : CPL_DISALLOW_COPY_ASSIGN(ISISMaskBand)
378 :
379 : public:
380 : explicit ISISMaskBand(GDALRasterBand *poBaseBand);
381 : ~ISISMaskBand() override;
382 :
383 : CPLErr IReadBlock(int, int, void *) override;
384 : };
385 :
386 : /************************************************************************/
387 : /* ISISTiledBand() */
388 : /************************************************************************/
389 :
390 51 : ISISTiledBand::ISISTiledBand(GDALDataset *poDSIn, VSILFILE *fpVSILIn,
391 : int nBandIn, GDALDataType eDT, int nTileXSize,
392 : int nTileYSize, GIntBig nFirstTileOffsetIn,
393 : GIntBig nXTileOffsetIn, GIntBig nYTileOffsetIn,
394 51 : int bNativeOrderIn)
395 : : m_fpVSIL(fpVSILIn), m_nXTileOffset(nXTileOffsetIn),
396 : m_nYTileOffset(nYTileOffsetIn),
397 51 : m_bNativeOrder(CPL_TO_BOOL(bNativeOrderIn))
398 : {
399 51 : poDS = poDSIn;
400 51 : nBand = nBandIn;
401 51 : eDataType = eDT;
402 51 : nBlockXSize = nTileXSize;
403 51 : nBlockYSize = nTileYSize;
404 51 : nRasterXSize = poDSIn->GetRasterXSize();
405 51 : nRasterYSize = poDSIn->GetRasterYSize();
406 :
407 51 : const int l_nBlocksPerRow = DIV_ROUND_UP(nRasterXSize, nBlockXSize);
408 51 : const int l_nBlocksPerColumn = DIV_ROUND_UP(nRasterYSize, nBlockYSize);
409 :
410 51 : if (m_nXTileOffset == 0 && m_nYTileOffset == 0)
411 : {
412 51 : m_nXTileOffset =
413 51 : static_cast<GIntBig>(GDALGetDataTypeSizeBytes(eDT)) * nTileXSize;
414 51 : if (m_nXTileOffset > GINTBIG_MAX / nTileYSize)
415 : {
416 0 : CPLError(CE_Failure, CPLE_AppDefined, "Integer overflow");
417 0 : return;
418 : }
419 51 : m_nXTileOffset *= nTileYSize;
420 :
421 51 : if (m_nXTileOffset > GINTBIG_MAX / l_nBlocksPerRow)
422 : {
423 0 : CPLError(CE_Failure, CPLE_AppDefined, "Integer overflow");
424 0 : return;
425 : }
426 51 : m_nYTileOffset = m_nXTileOffset * l_nBlocksPerRow;
427 : }
428 :
429 51 : m_nFirstTileOffset = nFirstTileOffsetIn;
430 51 : if (nBand > 1)
431 : {
432 19 : if (m_nYTileOffset > GINTBIG_MAX / (nBand - 1) ||
433 19 : (nBand - 1) * m_nYTileOffset > GINTBIG_MAX / l_nBlocksPerColumn ||
434 19 : m_nFirstTileOffset >
435 19 : GINTBIG_MAX - (nBand - 1) * m_nYTileOffset * l_nBlocksPerColumn)
436 : {
437 0 : CPLError(CE_Failure, CPLE_AppDefined, "Integer overflow");
438 0 : return;
439 : }
440 19 : m_nFirstTileOffset += (nBand - 1) * m_nYTileOffset * l_nBlocksPerColumn;
441 : }
442 51 : m_bValid = true;
443 : }
444 :
445 : /************************************************************************/
446 : /* IReadBlock() */
447 : /************************************************************************/
448 :
449 476 : CPLErr ISISTiledBand::IReadBlock(int nXBlock, int nYBlock, void *pImage)
450 :
451 : {
452 476 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
453 476 : if (poGDS->m_osExternalFilename.empty())
454 : {
455 232 : if (!poGDS->m_bIsLabelWritten)
456 2 : poGDS->WriteLabel();
457 : }
458 :
459 476 : const vsi_l_offset nOffset = m_nFirstTileOffset + nXBlock * m_nXTileOffset +
460 476 : nYBlock * m_nYTileOffset;
461 476 : const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
462 476 : const size_t nBlockSize =
463 476 : static_cast<size_t>(nDTSize) * nBlockXSize * nBlockYSize;
464 :
465 476 : if (VSIFSeekL(m_fpVSIL, nOffset, SEEK_SET) != 0)
466 : {
467 0 : CPLError(CE_Failure, CPLE_FileIO,
468 : "Failed to seek to offset %d to read tile %d,%d.",
469 : static_cast<int>(nOffset), nXBlock, nYBlock);
470 0 : return CE_Failure;
471 : }
472 :
473 476 : if (VSIFReadL(pImage, 1, nBlockSize, m_fpVSIL) != nBlockSize)
474 : {
475 0 : CPLError(CE_Failure, CPLE_FileIO,
476 : "Failed to read %d bytes for tile %d,%d.",
477 : static_cast<int>(nBlockSize), nXBlock, nYBlock);
478 0 : return CE_Failure;
479 : }
480 :
481 476 : if (!m_bNativeOrder && eDataType != GDT_UInt8)
482 0 : GDALSwapWords(pImage, nDTSize, nBlockXSize * nBlockYSize, nDTSize);
483 :
484 476 : return CE_None;
485 : }
486 :
487 : /************************************************************************/
488 : /* RemapNoDataT() */
489 : /************************************************************************/
490 :
491 : template <class T>
492 6 : static void RemapNoDataT(T *pBuffer, int nItems, T srcNoData, T dstNoData)
493 : {
494 67542 : for (int i = 0; i < nItems; i++)
495 : {
496 67536 : if (pBuffer[i] == srcNoData)
497 6 : pBuffer[i] = dstNoData;
498 : }
499 6 : }
500 :
501 : /************************************************************************/
502 : /* RemapNoData() */
503 : /************************************************************************/
504 :
505 6 : static void RemapNoData(GDALDataType eDataType, void *pBuffer, int nItems,
506 : double dfSrcNoData, double dfDstNoData)
507 : {
508 6 : if (eDataType == GDT_UInt8)
509 : {
510 3 : RemapNoDataT(reinterpret_cast<GByte *>(pBuffer), nItems,
511 3 : static_cast<GByte>(dfSrcNoData),
512 3 : static_cast<GByte>(dfDstNoData));
513 : }
514 3 : else if (eDataType == GDT_UInt16)
515 : {
516 1 : RemapNoDataT(reinterpret_cast<GUInt16 *>(pBuffer), nItems,
517 1 : static_cast<GUInt16>(dfSrcNoData),
518 1 : static_cast<GUInt16>(dfDstNoData));
519 : }
520 2 : else if (eDataType == GDT_Int16)
521 : {
522 1 : RemapNoDataT(reinterpret_cast<GInt16 *>(pBuffer), nItems,
523 1 : static_cast<GInt16>(dfSrcNoData),
524 1 : static_cast<GInt16>(dfDstNoData));
525 : }
526 : else
527 : {
528 1 : CPLAssert(eDataType == GDT_Float32);
529 1 : RemapNoDataT(reinterpret_cast<float *>(pBuffer), nItems,
530 : static_cast<float>(dfSrcNoData),
531 : static_cast<float>(dfDstNoData));
532 : }
533 6 : }
534 :
535 : /************************************************************************/
536 : /* IReadBlock() */
537 : /************************************************************************/
538 :
539 225 : CPLErr ISISTiledBand::IWriteBlock(int nXBlock, int nYBlock, void *pImage)
540 :
541 : {
542 225 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
543 225 : if (poGDS->m_osExternalFilename.empty())
544 : {
545 223 : if (!poGDS->m_bIsLabelWritten)
546 2 : poGDS->WriteLabel();
547 : }
548 :
549 225 : if (poGDS->m_bHasSrcNoData && poGDS->m_dfSrcNoData != m_dfNoData)
550 : {
551 1 : RemapNoData(eDataType, pImage, nBlockXSize * nBlockYSize,
552 : poGDS->m_dfSrcNoData, m_dfNoData);
553 : }
554 :
555 225 : const vsi_l_offset nOffset = m_nFirstTileOffset + nXBlock * m_nXTileOffset +
556 225 : nYBlock * m_nYTileOffset;
557 225 : const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
558 225 : const size_t nBlockSize =
559 225 : static_cast<size_t>(nDTSize) * nBlockXSize * nBlockYSize;
560 :
561 225 : const int l_nBlocksPerRow = DIV_ROUND_UP(nRasterXSize, nBlockXSize);
562 225 : const int l_nBlocksPerColumn = DIV_ROUND_UP(nRasterYSize, nBlockYSize);
563 :
564 : // Pad partial blocks to nodata value
565 225 : if (nXBlock == l_nBlocksPerRow - 1 && (nRasterXSize % nBlockXSize) != 0)
566 : {
567 24 : GByte *pabyImage = static_cast<GByte *>(pImage);
568 24 : int nXStart = nRasterXSize % nBlockXSize;
569 1688 : for (int iY = 0; iY < nBlockYSize; iY++)
570 : {
571 1664 : GDALCopyWords(&m_dfNoData, GDT_Float64, 0,
572 1664 : pabyImage + (iY * nBlockXSize + nXStart) * nDTSize,
573 1664 : eDataType, nDTSize, nBlockXSize - nXStart);
574 : }
575 : }
576 225 : if (nYBlock == l_nBlocksPerColumn - 1 && (nRasterYSize % nBlockYSize) != 0)
577 : {
578 49 : GByte *pabyImage = static_cast<GByte *>(pImage);
579 1685 : for (int iY = nRasterYSize % nBlockYSize; iY < nBlockYSize; iY++)
580 : {
581 1636 : GDALCopyWords(&m_dfNoData, GDT_Float64, 0,
582 1636 : pabyImage + iY * nBlockXSize * nDTSize, eDataType,
583 : nDTSize, nBlockXSize);
584 : }
585 : }
586 :
587 225 : if (VSIFSeekL(m_fpVSIL, nOffset, SEEK_SET) != 0)
588 : {
589 0 : CPLError(CE_Failure, CPLE_FileIO,
590 : "Failed to seek to offset %d to read tile %d,%d.",
591 : static_cast<int>(nOffset), nXBlock, nYBlock);
592 0 : return CE_Failure;
593 : }
594 :
595 225 : if (!m_bNativeOrder && eDataType != GDT_UInt8)
596 0 : GDALSwapWords(pImage, nDTSize, nBlockXSize * nBlockYSize, nDTSize);
597 :
598 225 : if (VSIFWriteL(pImage, 1, nBlockSize, m_fpVSIL) != nBlockSize)
599 : {
600 0 : CPLError(CE_Failure, CPLE_FileIO,
601 : "Failed to write %d bytes for tile %d,%d.",
602 : static_cast<int>(nBlockSize), nXBlock, nYBlock);
603 0 : return CE_Failure;
604 : }
605 :
606 225 : if (!m_bNativeOrder && eDataType != GDT_UInt8)
607 0 : GDALSwapWords(pImage, nDTSize, nBlockXSize * nBlockYSize, nDTSize);
608 :
609 225 : return CE_None;
610 : }
611 :
612 : /************************************************************************/
613 : /* SetMaskBand() */
614 : /************************************************************************/
615 :
616 41 : void ISISTiledBand::SetMaskBand(std::unique_ptr<GDALRasterBand> poMaskBand)
617 : {
618 41 : poMask.reset(std::move(poMaskBand));
619 41 : nMaskFlags = 0;
620 41 : }
621 :
622 : /************************************************************************/
623 : /* GetOffset() */
624 : /************************************************************************/
625 :
626 14 : double ISISTiledBand::GetOffset(int *pbSuccess)
627 : {
628 14 : if (pbSuccess)
629 4 : *pbSuccess = m_bHasOffset;
630 14 : return m_dfOffset;
631 : }
632 :
633 : /************************************************************************/
634 : /* GetScale() */
635 : /************************************************************************/
636 :
637 14 : double ISISTiledBand::GetScale(int *pbSuccess)
638 : {
639 14 : if (pbSuccess)
640 4 : *pbSuccess = m_bHasScale;
641 14 : return m_dfScale;
642 : }
643 :
644 : /************************************************************************/
645 : /* SetOffset() */
646 : /************************************************************************/
647 :
648 14 : CPLErr ISISTiledBand::SetOffset(double dfNewOffset)
649 : {
650 14 : m_dfOffset = dfNewOffset;
651 14 : m_bHasOffset = true;
652 14 : return CE_None;
653 : }
654 :
655 : /************************************************************************/
656 : /* SetScale() */
657 : /************************************************************************/
658 :
659 14 : CPLErr ISISTiledBand::SetScale(double dfNewScale)
660 : {
661 14 : m_dfScale = dfNewScale;
662 14 : m_bHasScale = true;
663 14 : return CE_None;
664 : }
665 :
666 : /************************************************************************/
667 : /* GetNoDataValue() */
668 : /************************************************************************/
669 :
670 12 : double ISISTiledBand::GetNoDataValue(int *pbSuccess)
671 : {
672 12 : if (pbSuccess)
673 8 : *pbSuccess = true;
674 12 : return m_dfNoData;
675 : }
676 :
677 : /************************************************************************/
678 : /* SetNoDataValue() */
679 : /************************************************************************/
680 :
681 51 : CPLErr ISISTiledBand::SetNoDataValue(double dfNewNoData)
682 : {
683 51 : m_dfNoData = dfNewNoData;
684 51 : return CE_None;
685 : }
686 :
687 : /************************************************************************/
688 : /* ISIS3RawRasterBand() */
689 : /************************************************************************/
690 :
691 378 : ISIS3RawRasterBand::ISIS3RawRasterBand(GDALDataset *l_poDS, int l_nBand,
692 : VSILFILE *l_fpRaw,
693 : vsi_l_offset l_nImgOffset,
694 : int l_nPixelOffset, int l_nLineOffset,
695 : GDALDataType l_eDataType,
696 378 : int l_bNativeOrder)
697 : : RawRasterBand(l_poDS, l_nBand, l_fpRaw, l_nImgOffset, l_nPixelOffset,
698 : l_nLineOffset, l_eDataType, l_bNativeOrder,
699 : RawRasterBand::OwnFP::NO),
700 : m_bHasOffset(false), m_bHasScale(false), m_dfOffset(0.0), m_dfScale(1.0),
701 378 : m_dfNoData(0.0)
702 : {
703 378 : }
704 :
705 : /************************************************************************/
706 : /* IReadBlock() */
707 : /************************************************************************/
708 :
709 2071 : CPLErr ISIS3RawRasterBand::IReadBlock(int nXBlock, int nYBlock, void *pImage)
710 :
711 : {
712 2071 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
713 2071 : if (poGDS->m_osExternalFilename.empty())
714 : {
715 930 : if (!poGDS->m_bIsLabelWritten)
716 0 : poGDS->WriteLabel();
717 : }
718 2071 : return RawRasterBand::IReadBlock(nXBlock, nYBlock, pImage);
719 : }
720 :
721 : /************************************************************************/
722 : /* IWriteBlock() */
723 : /************************************************************************/
724 :
725 856 : CPLErr ISIS3RawRasterBand::IWriteBlock(int nXBlock, int nYBlock, void *pImage)
726 :
727 : {
728 856 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
729 856 : if (poGDS->m_osExternalFilename.empty())
730 : {
731 796 : if (!poGDS->m_bIsLabelWritten)
732 0 : poGDS->WriteLabel();
733 : }
734 :
735 856 : if (poGDS->m_bHasSrcNoData && poGDS->m_dfSrcNoData != m_dfNoData)
736 : {
737 0 : RemapNoData(eDataType, pImage, nBlockXSize * nBlockYSize,
738 : poGDS->m_dfSrcNoData, m_dfNoData);
739 : }
740 :
741 856 : return RawRasterBand::IWriteBlock(nXBlock, nYBlock, pImage);
742 : }
743 :
744 : /************************************************************************/
745 : /* IRasterIO() */
746 : /************************************************************************/
747 :
748 1956 : CPLErr ISIS3RawRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
749 : int nXSize, int nYSize, void *pData,
750 : int nBufXSize, int nBufYSize,
751 : GDALDataType eBufType,
752 : GSpacing nPixelSpace, GSpacing nLineSpace,
753 : GDALRasterIOExtraArg *psExtraArg)
754 :
755 : {
756 1956 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
757 1956 : if (poGDS->m_osExternalFilename.empty())
758 : {
759 1153 : if (!poGDS->m_bIsLabelWritten)
760 55 : poGDS->WriteLabel();
761 : }
762 1956 : if (eRWFlag == GF_Write && poGDS->m_bHasSrcNoData &&
763 30 : poGDS->m_dfSrcNoData != m_dfNoData)
764 : {
765 4 : const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
766 4 : if (eBufType == eDataType && nPixelSpace == nDTSize &&
767 4 : nLineSpace == nPixelSpace * nBufXSize)
768 : {
769 4 : RemapNoData(eDataType, pData, nBufXSize * nBufYSize,
770 : poGDS->m_dfSrcNoData, m_dfNoData);
771 : }
772 : else
773 : {
774 0 : const GByte *pabySrc = reinterpret_cast<GByte *>(pData);
775 : GByte *pabyTemp = reinterpret_cast<GByte *>(
776 0 : VSI_MALLOC3_VERBOSE(nDTSize, nBufXSize, nBufYSize));
777 0 : for (int i = 0; i < nBufYSize; i++)
778 : {
779 0 : GDALCopyWords(pabySrc + i * nLineSpace, eBufType,
780 : static_cast<int>(nPixelSpace),
781 0 : pabyTemp + i * nBufXSize * nDTSize, eDataType,
782 : nDTSize, nBufXSize);
783 : }
784 0 : RemapNoData(eDataType, pabyTemp, nBufXSize * nBufYSize,
785 : poGDS->m_dfSrcNoData, m_dfNoData);
786 0 : CPLErr eErr = RawRasterBand::IRasterIO(
787 : eRWFlag, nXOff, nYOff, nXSize, nYSize, pabyTemp, nBufXSize,
788 : nBufYSize, eDataType, nDTSize,
789 0 : static_cast<GSpacing>(nDTSize) * nBufXSize, psExtraArg);
790 0 : VSIFree(pabyTemp);
791 0 : return eErr;
792 : }
793 : }
794 1956 : return RawRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
795 : pData, nBufXSize, nBufYSize, eBufType,
796 1956 : nPixelSpace, nLineSpace, psExtraArg);
797 : }
798 :
799 : /************************************************************************/
800 : /* SetMaskBand() */
801 : /************************************************************************/
802 :
803 240 : void ISIS3RawRasterBand::SetMaskBand(std::unique_ptr<GDALRasterBand> poMaskBand)
804 : {
805 240 : poMask.reset(std::move(poMaskBand));
806 240 : nMaskFlags = 0;
807 240 : }
808 :
809 : /************************************************************************/
810 : /* GetOffset() */
811 : /************************************************************************/
812 :
813 170 : double ISIS3RawRasterBand::GetOffset(int *pbSuccess)
814 : {
815 170 : if (pbSuccess)
816 25 : *pbSuccess = m_bHasOffset;
817 170 : return m_dfOffset;
818 : }
819 :
820 : /************************************************************************/
821 : /* GetScale() */
822 : /************************************************************************/
823 :
824 170 : double ISIS3RawRasterBand::GetScale(int *pbSuccess)
825 : {
826 170 : if (pbSuccess)
827 25 : *pbSuccess = m_bHasScale;
828 170 : return m_dfScale;
829 : }
830 :
831 : /************************************************************************/
832 : /* SetOffset() */
833 : /************************************************************************/
834 :
835 51 : CPLErr ISIS3RawRasterBand::SetOffset(double dfNewOffset)
836 : {
837 51 : m_dfOffset = dfNewOffset;
838 51 : m_bHasOffset = true;
839 51 : return CE_None;
840 : }
841 :
842 : /************************************************************************/
843 : /* SetScale() */
844 : /************************************************************************/
845 :
846 51 : CPLErr ISIS3RawRasterBand::SetScale(double dfNewScale)
847 : {
848 51 : m_dfScale = dfNewScale;
849 51 : m_bHasScale = true;
850 51 : return CE_None;
851 : }
852 :
853 : /************************************************************************/
854 : /* GetNoDataValue() */
855 : /************************************************************************/
856 :
857 148 : double ISIS3RawRasterBand::GetNoDataValue(int *pbSuccess)
858 : {
859 148 : if (pbSuccess)
860 97 : *pbSuccess = true;
861 148 : return m_dfNoData;
862 : }
863 :
864 : /************************************************************************/
865 : /* SetNoDataValue() */
866 : /************************************************************************/
867 :
868 379 : CPLErr ISIS3RawRasterBand::SetNoDataValue(double dfNewNoData)
869 : {
870 379 : m_dfNoData = dfNewNoData;
871 379 : return CE_None;
872 : }
873 :
874 : /************************************************************************/
875 : /* ISIS3WrapperRasterBand() */
876 : /************************************************************************/
877 :
878 45 : ISIS3WrapperRasterBand::ISIS3WrapperRasterBand(GDALRasterBand *poBaseBandIn)
879 45 : : m_poBaseBand(poBaseBandIn)
880 : {
881 45 : eDataType = m_poBaseBand->GetRasterDataType();
882 45 : m_poBaseBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
883 45 : }
884 :
885 : /************************************************************************/
886 : /* SetMaskBand() */
887 : /************************************************************************/
888 :
889 21 : void ISIS3WrapperRasterBand::SetMaskBand(
890 : std::unique_ptr<GDALRasterBand> poMaskBand)
891 : {
892 21 : poMask.reset(std::move(poMaskBand));
893 21 : nMaskFlags = 0;
894 21 : }
895 :
896 : /************************************************************************/
897 : /* GetOffset() */
898 : /************************************************************************/
899 :
900 22 : double ISIS3WrapperRasterBand::GetOffset(int *pbSuccess)
901 : {
902 22 : if (pbSuccess)
903 4 : *pbSuccess = m_bHasOffset;
904 22 : return m_dfOffset;
905 : }
906 :
907 : /************************************************************************/
908 : /* GetScale() */
909 : /************************************************************************/
910 :
911 22 : double ISIS3WrapperRasterBand::GetScale(int *pbSuccess)
912 : {
913 22 : if (pbSuccess)
914 4 : *pbSuccess = m_bHasScale;
915 22 : return m_dfScale;
916 : }
917 :
918 : /************************************************************************/
919 : /* SetOffset() */
920 : /************************************************************************/
921 :
922 12 : CPLErr ISIS3WrapperRasterBand::SetOffset(double dfNewOffset)
923 : {
924 12 : m_dfOffset = dfNewOffset;
925 12 : m_bHasOffset = true;
926 :
927 12 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
928 12 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
929 4 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetOffset(dfNewOffset);
930 :
931 12 : return CE_None;
932 : }
933 :
934 : /************************************************************************/
935 : /* SetScale() */
936 : /************************************************************************/
937 :
938 12 : CPLErr ISIS3WrapperRasterBand::SetScale(double dfNewScale)
939 : {
940 12 : m_dfScale = dfNewScale;
941 12 : m_bHasScale = true;
942 :
943 12 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
944 12 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
945 4 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetScale(dfNewScale);
946 :
947 12 : return CE_None;
948 : }
949 :
950 : /************************************************************************/
951 : /* GetNoDataValue() */
952 : /************************************************************************/
953 :
954 4 : double ISIS3WrapperRasterBand::GetNoDataValue(int *pbSuccess)
955 : {
956 4 : if (pbSuccess)
957 4 : *pbSuccess = true;
958 4 : return m_dfNoData;
959 : }
960 :
961 : /************************************************************************/
962 : /* SetNoDataValue() */
963 : /************************************************************************/
964 :
965 45 : CPLErr ISIS3WrapperRasterBand::SetNoDataValue(double dfNewNoData)
966 : {
967 45 : m_dfNoData = dfNewNoData;
968 :
969 45 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
970 45 : if (poGDS->m_poExternalDS && eAccess == GA_Update)
971 25 : poGDS->m_poExternalDS->GetRasterBand(nBand)->SetNoDataValue(
972 25 : dfNewNoData);
973 :
974 45 : return CE_None;
975 : }
976 :
977 : /************************************************************************/
978 : /* InitFile() */
979 : /************************************************************************/
980 :
981 8 : void ISIS3WrapperRasterBand::InitFile()
982 : {
983 8 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
984 8 : if (poGDS->m_bGeoTIFFAsRegularExternal && !poGDS->m_bGeoTIFFInitDone)
985 : {
986 8 : poGDS->m_bGeoTIFFInitDone = true;
987 :
988 8 : const int nBands = poGDS->GetRasterCount();
989 : // We need to make sure that blocks are written in the right order
990 22 : for (int i = 0; i < nBands; i++)
991 : {
992 14 : poGDS->m_poExternalDS->GetRasterBand(i + 1)->Fill(m_dfNoData);
993 : }
994 8 : poGDS->m_poExternalDS->FlushCache(false);
995 :
996 : // Check that blocks are effectively written in expected order.
997 : const int nBlockSizeBytes =
998 8 : nBlockXSize * nBlockYSize * GDALGetDataTypeSizeBytes(eDataType);
999 :
1000 8 : GIntBig nLastOffset = 0;
1001 8 : bool bGoOn = true;
1002 8 : const int l_nBlocksPerRow = DIV_ROUND_UP(nRasterXSize, nBlockXSize);
1003 8 : const int l_nBlocksPerColumn = DIV_ROUND_UP(nRasterYSize, nBlockYSize);
1004 22 : for (int i = 0; i < nBands && bGoOn; i++)
1005 : {
1006 641 : for (int y = 0; y < l_nBlocksPerColumn && bGoOn; y++)
1007 : {
1008 1473 : for (int x = 0; x < l_nBlocksPerRow && bGoOn; x++)
1009 : {
1010 : const char *pszBlockOffset =
1011 846 : poGDS->m_poExternalDS->GetRasterBand(i + 1)
1012 846 : ->GetMetadataItem(
1013 846 : CPLSPrintf("BLOCK_OFFSET_%d_%d", x, y), "TIFF");
1014 846 : if (pszBlockOffset)
1015 : {
1016 846 : GIntBig nOffset = CPLAtoGIntBig(pszBlockOffset);
1017 846 : if (i != 0 || x != 0 || y != 0)
1018 : {
1019 838 : if (nOffset != nLastOffset + nBlockSizeBytes)
1020 : {
1021 0 : CPLError(CE_Warning, CPLE_AppDefined,
1022 : "Block %d,%d band %d not at expected "
1023 : "offset",
1024 : x, y, i + 1);
1025 0 : bGoOn = false;
1026 0 : poGDS->m_bGeoTIFFAsRegularExternal = false;
1027 : }
1028 : }
1029 846 : nLastOffset = nOffset;
1030 : }
1031 : else
1032 : {
1033 0 : CPLError(CE_Warning, CPLE_AppDefined,
1034 : "Block %d,%d band %d not at expected "
1035 : "offset",
1036 : x, y, i + 1);
1037 0 : bGoOn = false;
1038 0 : poGDS->m_bGeoTIFFAsRegularExternal = false;
1039 : }
1040 : }
1041 : }
1042 : }
1043 : }
1044 8 : }
1045 :
1046 : /************************************************************************/
1047 : /* Fill() */
1048 : /************************************************************************/
1049 :
1050 4 : CPLErr ISIS3WrapperRasterBand::Fill(double dfRealValue, double dfImaginaryValue)
1051 : {
1052 4 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
1053 4 : if (poGDS->m_bHasSrcNoData && poGDS->m_dfSrcNoData == dfRealValue)
1054 : {
1055 0 : dfRealValue = m_dfNoData;
1056 : }
1057 4 : if (poGDS->m_bGeoTIFFAsRegularExternal && !poGDS->m_bGeoTIFFInitDone)
1058 : {
1059 1 : InitFile();
1060 : }
1061 :
1062 4 : return GDALProxyRasterBand::Fill(dfRealValue, dfImaginaryValue);
1063 : }
1064 :
1065 : /************************************************************************/
1066 : /* IWriteBlock() */
1067 : /************************************************************************/
1068 :
1069 0 : CPLErr ISIS3WrapperRasterBand::IWriteBlock(int nXBlock, int nYBlock,
1070 : void *pImage)
1071 :
1072 : {
1073 0 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
1074 0 : if (poGDS->m_bHasSrcNoData && poGDS->m_dfSrcNoData != m_dfNoData)
1075 : {
1076 0 : RemapNoData(eDataType, pImage, nBlockXSize * nBlockYSize,
1077 : poGDS->m_dfSrcNoData, m_dfNoData);
1078 : }
1079 0 : if (poGDS->m_bGeoTIFFAsRegularExternal && !poGDS->m_bGeoTIFFInitDone)
1080 : {
1081 0 : InitFile();
1082 : }
1083 :
1084 0 : return GDALProxyRasterBand::IWriteBlock(nXBlock, nYBlock, pImage);
1085 : }
1086 :
1087 : /************************************************************************/
1088 : /* IRasterIO() */
1089 : /************************************************************************/
1090 :
1091 46 : CPLErr ISIS3WrapperRasterBand::IRasterIO(
1092 : GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
1093 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
1094 : GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg)
1095 :
1096 : {
1097 46 : ISIS3Dataset *poGDS = cpl::down_cast<ISIS3Dataset *>(poDS);
1098 46 : if (eRWFlag == GF_Write && poGDS->m_bGeoTIFFAsRegularExternal &&
1099 17 : !poGDS->m_bGeoTIFFInitDone)
1100 : {
1101 6 : InitFile();
1102 : }
1103 46 : if (eRWFlag == GF_Write && poGDS->m_bHasSrcNoData &&
1104 9 : poGDS->m_dfSrcNoData != m_dfNoData)
1105 : {
1106 1 : const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
1107 1 : if (eBufType == eDataType && nPixelSpace == nDTSize &&
1108 1 : nLineSpace == nPixelSpace * nBufXSize)
1109 : {
1110 1 : RemapNoData(eDataType, pData, nBufXSize * nBufYSize,
1111 : poGDS->m_dfSrcNoData, m_dfNoData);
1112 : }
1113 : else
1114 : {
1115 0 : const GByte *pabySrc = reinterpret_cast<GByte *>(pData);
1116 : GByte *pabyTemp = reinterpret_cast<GByte *>(
1117 0 : VSI_MALLOC3_VERBOSE(nDTSize, nBufXSize, nBufYSize));
1118 0 : for (int i = 0; i < nBufYSize; i++)
1119 : {
1120 0 : GDALCopyWords(pabySrc + i * nLineSpace, eBufType,
1121 : static_cast<int>(nPixelSpace),
1122 0 : pabyTemp + i * nBufXSize * nDTSize, eDataType,
1123 : nDTSize, nBufXSize);
1124 : }
1125 0 : RemapNoData(eDataType, pabyTemp, nBufXSize * nBufYSize,
1126 : poGDS->m_dfSrcNoData, m_dfNoData);
1127 0 : CPLErr eErr = GDALProxyRasterBand::IRasterIO(
1128 : eRWFlag, nXOff, nYOff, nXSize, nYSize, pabyTemp, nBufXSize,
1129 : nBufYSize, eDataType, nDTSize,
1130 0 : static_cast<GSpacing>(nDTSize) * nBufXSize, psExtraArg);
1131 0 : VSIFree(pabyTemp);
1132 0 : return eErr;
1133 : }
1134 : }
1135 46 : return GDALProxyRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
1136 : pData, nBufXSize, nBufYSize, eBufType,
1137 46 : nPixelSpace, nLineSpace, psExtraArg);
1138 : }
1139 :
1140 : /************************************************************************/
1141 : /* ISISMaskBand() */
1142 : /************************************************************************/
1143 :
1144 302 : ISISMaskBand::ISISMaskBand(GDALRasterBand *poBaseBand)
1145 302 : : m_poBaseBand(poBaseBand), m_pBuffer(nullptr)
1146 : {
1147 302 : eDataType = GDT_UInt8;
1148 302 : poBaseBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
1149 302 : nRasterXSize = poBaseBand->GetXSize();
1150 302 : nRasterYSize = poBaseBand->GetYSize();
1151 302 : }
1152 :
1153 : /************************************************************************/
1154 : /* ~ISISMaskBand() */
1155 : /************************************************************************/
1156 :
1157 604 : ISISMaskBand::~ISISMaskBand()
1158 : {
1159 302 : VSIFree(m_pBuffer);
1160 604 : }
1161 :
1162 : /************************************************************************/
1163 : /* FillMask() */
1164 : /************************************************************************/
1165 :
1166 : template <class T>
1167 72 : static void FillMask(void *pvBuffer, GByte *pabyDst, int nReqXSize,
1168 : int nReqYSize, int nBlockXSize, T NULL_VAL, T LOW_REPR_SAT,
1169 : T LOW_INSTR_SAT, T HIGH_INSTR_SAT, T HIGH_REPR_SAT)
1170 : {
1171 72 : const T *pSrc = static_cast<T *>(pvBuffer);
1172 148 : for (int y = 0; y < nReqYSize; y++)
1173 : {
1174 9708 : for (int x = 0; x < nReqXSize; x++)
1175 : {
1176 9632 : const T nSrc = pSrc[y * nBlockXSize + x];
1177 9632 : if (nSrc == NULL_VAL || nSrc == LOW_REPR_SAT ||
1178 6418 : nSrc == LOW_INSTR_SAT || nSrc == HIGH_INSTR_SAT ||
1179 : nSrc == HIGH_REPR_SAT)
1180 : {
1181 3214 : pabyDst[y * nBlockXSize + x] = 0;
1182 : }
1183 : else
1184 : {
1185 6418 : pabyDst[y * nBlockXSize + x] = 255;
1186 : }
1187 : }
1188 : }
1189 72 : }
1190 :
1191 : /************************************************************************/
1192 : /* IReadBlock() */
1193 : /************************************************************************/
1194 :
1195 72 : CPLErr ISISMaskBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
1196 :
1197 : {
1198 72 : const GDALDataType eSrcDT = m_poBaseBand->GetRasterDataType();
1199 72 : const int nSrcDTSize = GDALGetDataTypeSizeBytes(eSrcDT);
1200 72 : if (m_pBuffer == nullptr)
1201 : {
1202 24 : m_pBuffer = VSI_MALLOC3_VERBOSE(nBlockXSize, nBlockYSize, nSrcDTSize);
1203 24 : if (m_pBuffer == nullptr)
1204 0 : return CE_Failure;
1205 : }
1206 :
1207 72 : const int nXOff = nBlockXOff * nBlockXSize;
1208 72 : const int nReqXSize = std::min(nBlockXSize, nRasterXSize - nXOff);
1209 72 : const int nYOff = nBlockYOff * nBlockYSize;
1210 72 : const int nReqYSize = std::min(nBlockYSize, nRasterYSize - nYOff);
1211 :
1212 144 : if (m_poBaseBand->RasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize,
1213 : m_pBuffer, nReqXSize, nReqYSize, eSrcDT,
1214 : nSrcDTSize,
1215 72 : static_cast<GSpacing>(nSrcDTSize) * nBlockXSize,
1216 72 : nullptr) != CE_None)
1217 : {
1218 0 : return CE_Failure;
1219 : }
1220 :
1221 72 : GByte *pabyDst = static_cast<GByte *>(pImage);
1222 72 : if (eSrcDT == GDT_UInt8)
1223 : {
1224 48 : FillMask<GByte>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
1225 : ISIS3_NULL1, LOW_REPR_SAT1, LOW_INSTR_SAT1,
1226 : HIGH_INSTR_SAT1, HIGH_REPR_SAT1);
1227 : }
1228 24 : else if (eSrcDT == GDT_UInt16)
1229 : {
1230 8 : FillMask<GUInt16>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
1231 : ISIS3_NULLU2, LOW_REPR_SATU2, LOW_INSTR_SATU2,
1232 : HIGH_INSTR_SATU2, HIGH_REPR_SATU2);
1233 : }
1234 16 : else if (eSrcDT == GDT_Int16)
1235 : {
1236 8 : FillMask<GInt16>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
1237 : ISIS3_NULL2, LOW_REPR_SAT2, LOW_INSTR_SAT2,
1238 : HIGH_INSTR_SAT2, HIGH_REPR_SAT2);
1239 : }
1240 : else
1241 : {
1242 8 : CPLAssert(eSrcDT == GDT_Float32);
1243 8 : FillMask<float>(m_pBuffer, pabyDst, nReqXSize, nReqYSize, nBlockXSize,
1244 : ISIS3_NULL4, LOW_REPR_SAT4, LOW_INSTR_SAT4,
1245 : HIGH_INSTR_SAT4, HIGH_REPR_SAT4);
1246 : }
1247 :
1248 72 : return CE_None;
1249 : }
1250 :
1251 : /************************************************************************/
1252 : /* ISIS3Dataset() */
1253 : /************************************************************************/
1254 :
1255 397 : ISIS3Dataset::ISIS3Dataset()
1256 : {
1257 397 : m_oKeywords.SetStripSurroundingQuotes(true);
1258 :
1259 : // Deinit JSON objects
1260 397 : m_oJSonLabel.Deinit();
1261 397 : m_oSrcJSonLabel.Deinit();
1262 397 : }
1263 :
1264 : /************************************************************************/
1265 : /* ~ISIS3Dataset() */
1266 : /************************************************************************/
1267 :
1268 794 : ISIS3Dataset::~ISIS3Dataset()
1269 :
1270 : {
1271 397 : ISIS3Dataset::Close();
1272 794 : }
1273 :
1274 : /************************************************************************/
1275 : /* Close() */
1276 : /************************************************************************/
1277 :
1278 768 : CPLErr ISIS3Dataset::Close(GDALProgressFunc, void *)
1279 : {
1280 768 : CPLErr eErr = CE_None;
1281 768 : if (nOpenFlags != OPEN_FLAGS_CLOSED)
1282 : {
1283 397 : if (!m_bIsLabelWritten)
1284 72 : WriteLabel();
1285 397 : if (m_poExternalDS && m_bGeoTIFFAsRegularExternal &&
1286 8 : !m_bGeoTIFFInitDone)
1287 : {
1288 : cpl::down_cast<ISIS3WrapperRasterBand *>(GetRasterBand(1))
1289 0 : ->InitFile();
1290 : }
1291 397 : if (ISIS3Dataset::FlushCache(true) != CE_None)
1292 0 : eErr = CE_Failure;
1293 397 : if (m_fpLabel)
1294 : {
1295 131 : if (VSIFCloseL(m_fpLabel) != 0)
1296 0 : eErr = CE_Failure;
1297 : }
1298 397 : if (m_fpImage && m_fpImage != m_fpLabel)
1299 : {
1300 244 : if (VSIFCloseL(m_fpImage) != 0)
1301 0 : eErr = CE_Failure;
1302 : }
1303 :
1304 397 : ISIS3Dataset::CloseDependentDatasets();
1305 397 : if (GDALPamDataset::Close() != CE_None)
1306 0 : eErr = CE_Failure;
1307 : }
1308 768 : return eErr;
1309 : }
1310 :
1311 : /************************************************************************/
1312 : /* CloseDependentDatasets() */
1313 : /************************************************************************/
1314 :
1315 397 : int ISIS3Dataset::CloseDependentDatasets()
1316 : {
1317 397 : int bHasDroppedRef = GDALPamDataset::CloseDependentDatasets();
1318 :
1319 397 : if (m_poExternalDS)
1320 : {
1321 43 : bHasDroppedRef = FALSE;
1322 43 : delete m_poExternalDS;
1323 43 : m_poExternalDS = nullptr;
1324 : }
1325 :
1326 871 : for (int iBand = 0; iBand < nBands; iBand++)
1327 : {
1328 474 : delete papoBands[iBand];
1329 : }
1330 397 : nBands = 0;
1331 :
1332 397 : return bHasDroppedRef;
1333 : }
1334 :
1335 : /************************************************************************/
1336 : /* GetFileList() */
1337 : /************************************************************************/
1338 :
1339 90 : char **ISIS3Dataset::GetFileList()
1340 :
1341 : {
1342 90 : char **papszFileList = GDALPamDataset::GetFileList();
1343 :
1344 90 : if (!m_osExternalFilename.empty())
1345 27 : papszFileList = CSLAddString(papszFileList, m_osExternalFilename);
1346 116 : for (int i = 0; i < m_aosAdditionalFiles.Count(); ++i)
1347 : {
1348 26 : if (CSLFindString(papszFileList, m_aosAdditionalFiles[i]) < 0)
1349 : {
1350 : papszFileList =
1351 26 : CSLAddString(papszFileList, m_aosAdditionalFiles[i]);
1352 : }
1353 : }
1354 :
1355 90 : return papszFileList;
1356 : }
1357 :
1358 : /************************************************************************/
1359 : /* GetSpatialRef() */
1360 : /************************************************************************/
1361 :
1362 75 : const OGRSpatialReference *ISIS3Dataset::GetSpatialRef() const
1363 :
1364 : {
1365 75 : if (!m_oSRS.IsEmpty())
1366 52 : return &m_oSRS;
1367 :
1368 23 : return GDALPamDataset::GetSpatialRef();
1369 : }
1370 :
1371 : /************************************************************************/
1372 : /* SetSpatialRef() */
1373 : /************************************************************************/
1374 :
1375 64 : CPLErr ISIS3Dataset::SetSpatialRef(const OGRSpatialReference *poSRS)
1376 : {
1377 64 : if (eAccess == GA_ReadOnly)
1378 0 : return GDALPamDataset::SetSpatialRef(poSRS);
1379 64 : if (poSRS)
1380 64 : m_oSRS = *poSRS;
1381 : else
1382 0 : m_oSRS.Clear();
1383 64 : if (m_poExternalDS)
1384 6 : m_poExternalDS->SetSpatialRef(poSRS);
1385 64 : InvalidateLabel();
1386 64 : return CE_None;
1387 : }
1388 :
1389 : /************************************************************************/
1390 : /* GetGeoTransform() */
1391 : /************************************************************************/
1392 :
1393 86 : CPLErr ISIS3Dataset::GetGeoTransform(GDALGeoTransform >) const
1394 :
1395 : {
1396 86 : if (m_bGotTransform)
1397 : {
1398 64 : gt = m_gt;
1399 64 : return CE_None;
1400 : }
1401 :
1402 22 : return GDALPamDataset::GetGeoTransform(gt);
1403 : }
1404 :
1405 : /************************************************************************/
1406 : /* SetGeoTransform() */
1407 : /************************************************************************/
1408 :
1409 56 : CPLErr ISIS3Dataset::SetGeoTransform(const GDALGeoTransform >)
1410 :
1411 : {
1412 56 : if (eAccess == GA_ReadOnly)
1413 0 : return GDALPamDataset::SetGeoTransform(gt);
1414 56 : if (gt.xscale <= 0.0 || gt.xscale != -gt.yscale || gt.xrot != 0.0 ||
1415 56 : gt.yrot != 0.0)
1416 : {
1417 0 : CPLError(CE_Failure, CPLE_NotSupported,
1418 : "Only north-up geotransform with square pixels supported");
1419 0 : return CE_Failure;
1420 : }
1421 56 : m_bGotTransform = true;
1422 56 : m_gt = gt;
1423 56 : if (m_poExternalDS)
1424 6 : m_poExternalDS->SetGeoTransform(m_gt);
1425 56 : InvalidateLabel();
1426 56 : return CE_None;
1427 : }
1428 :
1429 : /************************************************************************/
1430 : /* GetMetadataDomainList() */
1431 : /************************************************************************/
1432 :
1433 1 : char **ISIS3Dataset::GetMetadataDomainList()
1434 : {
1435 1 : return BuildMetadataDomainList(nullptr, FALSE, "", "json:ISIS3", nullptr);
1436 : }
1437 :
1438 : /************************************************************************/
1439 : /* GetMetadata() */
1440 : /************************************************************************/
1441 :
1442 128 : CSLConstList ISIS3Dataset::GetMetadata(const char *pszDomain)
1443 : {
1444 128 : if (pszDomain != nullptr && EQUAL(pszDomain, "json:ISIS3"))
1445 : {
1446 66 : if (m_aosISIS3MD.empty())
1447 : {
1448 60 : if (eAccess == GA_Update && !m_oJSonLabel.IsValid())
1449 : {
1450 1 : BuildLabel();
1451 : }
1452 60 : CPLAssert(m_oJSonLabel.IsValid());
1453 60 : if (m_bResolveOfflineContent && !m_bHasResolvedOfflineContent)
1454 : {
1455 59 : ResolveOfflineContentOfLabel();
1456 : }
1457 : const CPLString osJson =
1458 120 : m_oJSonLabel.Format(CPLJSONObject::PrettyFormat::Pretty);
1459 60 : m_aosISIS3MD.InsertString(0, osJson.c_str());
1460 : }
1461 66 : return m_aosISIS3MD.List();
1462 : }
1463 62 : return GDALPamDataset::GetMetadata(pszDomain);
1464 : }
1465 :
1466 : /************************************************************************/
1467 : /* ResolveOfflineContentOfLabel() */
1468 : /************************************************************************/
1469 :
1470 59 : void ISIS3Dataset::ResolveOfflineContentOfLabel()
1471 : {
1472 59 : m_bHasResolvedOfflineContent = true;
1473 :
1474 118 : std::vector<GByte> abyData;
1475 :
1476 296 : for (CPLJSONObject &oObj : m_oJSonLabel.GetChildren())
1477 : {
1478 237 : if (oObj.GetType() == CPLJSONObject::Type::Object)
1479 : {
1480 179 : CPLString osContainerName = oObj.GetName();
1481 358 : CPLJSONObject oContainerName = oObj.GetObj("_container_name");
1482 179 : if (oContainerName.GetType() == CPLJSONObject::Type::String)
1483 : {
1484 72 : osContainerName = oContainerName.ToString();
1485 : }
1486 :
1487 179 : std::string osFilename;
1488 179 : CPLJSONObject oFilename = oObj.GetObj("^" + osContainerName);
1489 179 : if (oFilename.GetType() == CPLJSONObject::Type::String)
1490 : {
1491 : VSIStatBufL sStat;
1492 108 : osFilename = CPLFormFilenameSafe(
1493 72 : CPLGetPathSafe(GetDescription()).c_str(),
1494 108 : oFilename.ToString().c_str(), nullptr);
1495 36 : if (CPLHasPathTraversal(oFilename.ToString().c_str()))
1496 : {
1497 0 : CPLError(CE_Warning, CPLE_AppDefined,
1498 : "Path traversal detected for ^%s: %s",
1499 : osContainerName.c_str(),
1500 0 : oFilename.ToString().c_str());
1501 17 : continue;
1502 : }
1503 36 : else if (VSIStatL(osFilename.c_str(), &sStat) != 0)
1504 : {
1505 17 : CPLDebug("ISIS3", "File %s referenced but not foud",
1506 : osFilename.c_str());
1507 17 : continue;
1508 : }
1509 : }
1510 : else
1511 : {
1512 143 : osFilename = GetDescription();
1513 : }
1514 :
1515 324 : CPLJSONObject oBytes = oObj.GetObj("Bytes");
1516 162 : if (oBytes.GetType() == CPLJSONObject::Type::Long)
1517 : {
1518 0 : CPLError(CE_Warning, CPLE_AppDefined,
1519 : "Too large content reference by %s to be captured in "
1520 : "json:ISIS3 metadata domain",
1521 0 : oObj.GetName().c_str());
1522 0 : continue;
1523 : }
1524 257 : else if (oBytes.GetType() != CPLJSONObject::Type::Integer ||
1525 95 : oBytes.ToInteger() <= 0)
1526 : {
1527 67 : continue;
1528 : }
1529 95 : if (oBytes.ToInteger() > m_nMaxOfflineContentSize)
1530 : {
1531 1 : CPLError(CE_Warning, CPLE_AppDefined,
1532 : "Too large content reference by %s to be captured in "
1533 : "json:ISIS3 metadata domain",
1534 2 : oObj.GetName().c_str());
1535 1 : continue;
1536 : }
1537 :
1538 188 : CPLJSONObject oStartByte = oObj.GetObj("StartByte");
1539 94 : if ((oStartByte.GetType() != CPLJSONObject::Type::Integer &&
1540 143 : oStartByte.GetType() != CPLJSONObject::Type::Long) ||
1541 49 : oStartByte.ToLong() <= 0)
1542 : {
1543 45 : continue;
1544 : }
1545 :
1546 : // 1-based offsets
1547 : const auto nOffset =
1548 49 : static_cast<vsi_l_offset>(oStartByte.ToLong() - 1);
1549 :
1550 49 : const auto nSize = static_cast<size_t>(oBytes.ToInteger());
1551 : try
1552 : {
1553 49 : abyData.resize(nSize);
1554 : }
1555 0 : catch (const std::exception &)
1556 : {
1557 0 : CPLError(CE_Warning, CPLE_AppDefined,
1558 : "Out of memory: too large content referenced by %s "
1559 : "to be captured in json:ISIS3 metadata domain",
1560 0 : oObj.GetName().c_str());
1561 0 : continue;
1562 : }
1563 :
1564 49 : VSIVirtualHandleUniquePtr fp(VSIFOpenL(osFilename.c_str(), "rb"));
1565 49 : if (!fp)
1566 : {
1567 0 : CPLError(CE_Warning, CPLE_FileIO,
1568 : "Cannot open %s referenced by %s", osFilename.c_str(),
1569 0 : oObj.GetName().c_str());
1570 0 : continue;
1571 : }
1572 :
1573 98 : if (fp->Seek(nOffset, SEEK_SET) != 0 ||
1574 49 : fp->Read(abyData.data(), abyData.size(), 1) != 1)
1575 : {
1576 0 : CPLError(CE_Warning, CPLE_FileIO,
1577 : "Cannot read %u bytes at offset %" PRIu64
1578 : " in %s, referenced by %s",
1579 0 : static_cast<unsigned>(abyData.size()),
1580 : static_cast<uint64_t>(nOffset), osFilename.c_str(),
1581 0 : oObj.GetName().c_str());
1582 0 : continue;
1583 : }
1584 :
1585 98 : CPLJSONObject oData;
1586 49 : char *pszHex = CPLBinaryToHex(static_cast<int>(abyData.size()),
1587 49 : abyData.data());
1588 49 : oObj.Add("_data", pszHex);
1589 49 : CPLFree(pszHex);
1590 : }
1591 : }
1592 59 : }
1593 :
1594 : /************************************************************************/
1595 : /* InvalidateLabel() */
1596 : /************************************************************************/
1597 :
1598 148 : void ISIS3Dataset::InvalidateLabel()
1599 : {
1600 148 : m_oJSonLabel.Deinit();
1601 148 : m_aosISIS3MD.Clear();
1602 148 : m_bHasResolvedOfflineContent = false;
1603 148 : }
1604 :
1605 : /************************************************************************/
1606 : /* SetMetadata() */
1607 : /************************************************************************/
1608 :
1609 28 : CPLErr ISIS3Dataset::SetMetadata(CSLConstList papszMD, const char *pszDomain)
1610 : {
1611 28 : if (m_bUseSrcLabel && eAccess == GA_Update && pszDomain != nullptr &&
1612 28 : EQUAL(pszDomain, "json:ISIS3"))
1613 : {
1614 28 : m_oSrcJSonLabel.Deinit();
1615 28 : InvalidateLabel();
1616 28 : if (papszMD != nullptr && papszMD[0] != nullptr)
1617 : {
1618 28 : CPLJSONDocument oJSONDocument;
1619 28 : const GByte *pabyData = reinterpret_cast<const GByte *>(papszMD[0]);
1620 28 : if (!oJSONDocument.LoadMemory(pabyData))
1621 : {
1622 1 : return CE_Failure;
1623 : }
1624 :
1625 27 : m_oSrcJSonLabel = oJSONDocument.GetRoot();
1626 27 : if (!m_oSrcJSonLabel.IsValid())
1627 : {
1628 0 : return CE_Failure;
1629 : }
1630 : }
1631 27 : return CE_None;
1632 : }
1633 0 : return GDALPamDataset::SetMetadata(papszMD, pszDomain);
1634 : }
1635 :
1636 : /************************************************************************/
1637 : /* GetRawBinaryLayout() */
1638 : /************************************************************************/
1639 :
1640 2 : bool ISIS3Dataset::GetRawBinaryLayout(GDALDataset::RawBinaryLayout &sLayout)
1641 : {
1642 2 : if (m_sLayout.osRawFilename.empty())
1643 0 : return false;
1644 2 : sLayout = m_sLayout;
1645 2 : return true;
1646 : }
1647 :
1648 : /************************************************************************/
1649 : /* GetValueAndUnits() */
1650 : /************************************************************************/
1651 :
1652 50 : static void GetValueAndUnits(const CPLJSONObject &obj,
1653 : std::vector<double> &adfValues,
1654 : std::vector<std::string> &aosUnits,
1655 : int nExpectedVals)
1656 : {
1657 100 : if (obj.GetType() == CPLJSONObject::Type::Integer ||
1658 50 : obj.GetType() == CPLJSONObject::Type::Double)
1659 : {
1660 32 : adfValues.push_back(obj.ToDouble());
1661 : }
1662 18 : else if (obj.GetType() == CPLJSONObject::Type::Object)
1663 : {
1664 24 : auto oValue = obj.GetObj("value");
1665 24 : auto oUnit = obj.GetObj("unit");
1666 8 : if (oValue.IsValid() &&
1667 8 : (oValue.GetType() == CPLJSONObject::Type::Integer ||
1668 2 : oValue.GetType() == CPLJSONObject::Type::Double ||
1669 2 : oValue.GetType() == CPLJSONObject::Type::Array) &&
1670 16 : oUnit.IsValid() && oUnit.GetType() == CPLJSONObject::Type::String)
1671 : {
1672 8 : if (oValue.GetType() == CPLJSONObject::Type::Array)
1673 : {
1674 2 : GetValueAndUnits(oValue, adfValues, aosUnits, nExpectedVals);
1675 : }
1676 : else
1677 : {
1678 6 : adfValues.push_back(oValue.ToDouble());
1679 : }
1680 8 : aosUnits.push_back(oUnit.ToString());
1681 : }
1682 : }
1683 10 : else if (obj.GetType() == CPLJSONObject::Type::Array)
1684 : {
1685 10 : auto oArray = obj.ToArray();
1686 10 : if (oArray.Size() == nExpectedVals)
1687 : {
1688 34 : for (int i = 0; i < nExpectedVals; i++)
1689 : {
1690 60 : if (oArray[i].GetType() == CPLJSONObject::Type::Integer ||
1691 36 : oArray[i].GetType() == CPLJSONObject::Type::Double)
1692 : {
1693 24 : adfValues.push_back(oArray[i].ToDouble());
1694 : }
1695 : else
1696 : {
1697 0 : adfValues.clear();
1698 0 : return;
1699 : }
1700 : }
1701 : }
1702 : }
1703 : }
1704 :
1705 : /************************************************************************/
1706 : /* Open() */
1707 : /************************************************************************/
1708 :
1709 266 : GDALDataset *ISIS3Dataset::Open(GDALOpenInfo *poOpenInfo)
1710 :
1711 : {
1712 : /* -------------------------------------------------------------------- */
1713 : /* Does this look like a CUBE dataset? */
1714 : /* -------------------------------------------------------------------- */
1715 266 : if (!ISIS3DriverIdentify(poOpenInfo))
1716 0 : return nullptr;
1717 :
1718 : /* -------------------------------------------------------------------- */
1719 : /* Open the file using the large file API. */
1720 : /* -------------------------------------------------------------------- */
1721 532 : auto poDS = std::make_unique<ISIS3Dataset>();
1722 :
1723 266 : if (!poDS->m_oKeywords.Ingest(poOpenInfo->fpL, 0))
1724 : {
1725 1 : VSIFCloseL(poOpenInfo->fpL);
1726 1 : poOpenInfo->fpL = nullptr;
1727 1 : return nullptr;
1728 : }
1729 :
1730 265 : poDS->m_bResolveOfflineContent = CPLTestBool(CSLFetchNameValueDef(
1731 265 : poOpenInfo->papszOpenOptions, "INCLUDE_OFFLINE_CONTENT", "YES"));
1732 265 : poDS->m_nMaxOfflineContentSize = std::max(
1733 265 : 0, atoi(CSLFetchNameValueDef(poOpenInfo->papszOpenOptions,
1734 265 : "MAX_SIZE_OFFLINE_CONTENT", "100000000")));
1735 :
1736 265 : poDS->m_oJSonLabel = poDS->m_oKeywords.GetJsonObject();
1737 265 : poDS->m_oJSonLabel.Add("_filename", poOpenInfo->pszFilename);
1738 :
1739 : // Find additional files from the label
1740 1320 : for (const CPLJSONObject &oObj : poDS->m_oJSonLabel.GetChildren())
1741 : {
1742 1055 : if (oObj.GetType() == CPLJSONObject::Type::Object)
1743 : {
1744 1580 : CPLString osContainerName = oObj.GetName();
1745 2370 : CPLJSONObject oContainerName = oObj.GetObj("_container_name");
1746 790 : if (oContainerName.GetType() == CPLJSONObject::Type::String)
1747 : {
1748 286 : osContainerName = oContainerName.ToString();
1749 : }
1750 :
1751 1580 : CPLJSONObject oFilename = oObj.GetObj("^" + osContainerName);
1752 790 : if (oFilename.GetType() == CPLJSONObject::Type::String)
1753 : {
1754 : VSIStatBufL sStat;
1755 369 : const CPLString osFilename(CPLFormFilenameSafe(
1756 246 : CPLGetPathSafe(poOpenInfo->pszFilename).c_str(),
1757 492 : oFilename.ToString().c_str(), nullptr));
1758 123 : if (CPLHasPathTraversal(oFilename.ToString().c_str()))
1759 : {
1760 0 : CPLError(CE_Warning, CPLE_AppDefined,
1761 : "Path traversal detected for ^%s: %s",
1762 : osContainerName.c_str(),
1763 0 : oFilename.ToString().c_str());
1764 : }
1765 123 : else if (VSIStatL(osFilename, &sStat) == 0)
1766 : {
1767 81 : poDS->m_aosAdditionalFiles.AddString(osFilename);
1768 : }
1769 : else
1770 : {
1771 42 : CPLDebug("ISIS3", "File %s referenced but not foud",
1772 : osFilename.c_str());
1773 : }
1774 : }
1775 : }
1776 : }
1777 :
1778 265 : VSIFCloseL(poOpenInfo->fpL);
1779 265 : poOpenInfo->fpL = nullptr;
1780 :
1781 : /* -------------------------------------------------------------------- */
1782 : /* Assume user is pointing to label (i.e. .lbl) file for detached option */
1783 : /* -------------------------------------------------------------------- */
1784 : // Image can be inline or detached and point to an image name
1785 : // the Format can be Tiled or Raw
1786 : // Object = Core
1787 : // StartByte = 65537
1788 : // Format = Tile
1789 : // TileSamples = 128
1790 : // TileLines = 128
1791 : // OR-----
1792 : // Object = Core
1793 : // StartByte = 1
1794 : // ^Core = r0200357_detatched.cub
1795 : // Format = BandSequential
1796 : // OR-----
1797 : // Object = Core
1798 : // StartByte = 1
1799 : // ^Core = r0200357_detached_tiled.cub
1800 : // Format = Tile
1801 : // TileSamples = 128
1802 : // TileLines = 128
1803 : // OR-----
1804 : // Object = Core
1805 : // StartByte = 1
1806 : // ^Core = some.tif
1807 : // Format = GeoTIFF
1808 :
1809 : /* -------------------------------------------------------------------- */
1810 : /* What file contains the actual data? */
1811 : /* -------------------------------------------------------------------- */
1812 265 : const char *pszCore = poDS->GetKeyword("IsisCube.Core.^Core");
1813 : CPLString osQubeFile(
1814 265 : EQUAL(pszCore, "")
1815 358 : ? CPLString(poOpenInfo->pszFilename)
1816 : : CPLFormFilenameSafe(
1817 451 : CPLGetPathSafe(poOpenInfo->pszFilename).c_str(), pszCore,
1818 530 : nullptr));
1819 265 : if (CPLHasPathTraversal(pszCore))
1820 : {
1821 0 : CPLError(CE_Failure, CPLE_NotSupported,
1822 : "Path traversal detected in IsisCube.Core.^Core: %s", pszCore);
1823 0 : return nullptr;
1824 : }
1825 265 : if (!EQUAL(pszCore, ""))
1826 : {
1827 93 : poDS->m_osExternalFilename = osQubeFile;
1828 : }
1829 :
1830 : /* -------------------------------------------------------------------- */
1831 : /* Check if file an ISIS3 header file? Read a few lines of text */
1832 : /* searching for something starting with nrows or ncols. */
1833 : /* -------------------------------------------------------------------- */
1834 :
1835 : /************* Skipbytes *****************************/
1836 265 : int nSkipBytes = atoi(poDS->GetKeyword("IsisCube.Core.StartByte", "1"));
1837 265 : if (nSkipBytes <= 1)
1838 96 : nSkipBytes = 0;
1839 : else
1840 169 : nSkipBytes -= 1;
1841 :
1842 : /******* Grab format type (BandSequential, Tiled) *******/
1843 530 : CPLString osFormat = poDS->GetKeyword("IsisCube.Core.Format");
1844 :
1845 265 : int tileSizeX = 0;
1846 265 : int tileSizeY = 0;
1847 :
1848 265 : if (EQUAL(osFormat, "Tile"))
1849 : {
1850 26 : poDS->m_bIsTiled = true;
1851 : /******* Get Tile Sizes *********/
1852 26 : tileSizeX = atoi(poDS->GetKeyword("IsisCube.Core.TileSamples"));
1853 26 : tileSizeY = atoi(poDS->GetKeyword("IsisCube.Core.TileLines"));
1854 26 : if (tileSizeX <= 0 || tileSizeY <= 0)
1855 : {
1856 1 : CPLError(CE_Failure, CPLE_OpenFailed,
1857 : "Wrong tile dimensions : %d x %d", tileSizeX, tileSizeY);
1858 1 : return nullptr;
1859 : }
1860 : }
1861 239 : else if (!EQUAL(osFormat, "BandSequential") && !EQUAL(osFormat, "GeoTIFF"))
1862 : {
1863 1 : CPLError(CE_Failure, CPLE_OpenFailed, "%s format not supported.",
1864 : osFormat.c_str());
1865 1 : return nullptr;
1866 : }
1867 :
1868 : /*********** Grab samples lines band ************/
1869 : const int nCols =
1870 263 : atoi(poDS->GetKeyword("IsisCube.Core.Dimensions.Samples"));
1871 263 : const int nRows = atoi(poDS->GetKeyword("IsisCube.Core.Dimensions.Lines"));
1872 263 : const int nBands = atoi(poDS->GetKeyword("IsisCube.Core.Dimensions.Bands"));
1873 :
1874 : /****** Grab format type - ISIS3 only supports 8,U16,S16,32 *****/
1875 263 : GDALDataType eDataType = GDT_UInt8;
1876 263 : double dfNoData = 0.0;
1877 :
1878 263 : const char *itype = poDS->GetKeyword("IsisCube.Core.Pixels.Type");
1879 263 : if (EQUAL(itype, "UnsignedByte"))
1880 : {
1881 212 : eDataType = GDT_UInt8;
1882 212 : dfNoData = ISIS3_NULL1;
1883 : }
1884 51 : else if (EQUAL(itype, "UnsignedWord"))
1885 : {
1886 17 : eDataType = GDT_UInt16;
1887 17 : dfNoData = ISIS3_NULLU2;
1888 : }
1889 34 : else if (EQUAL(itype, "SignedWord"))
1890 : {
1891 14 : eDataType = GDT_Int16;
1892 14 : dfNoData = ISIS3_NULL2;
1893 : }
1894 20 : else if (EQUAL(itype, "Real") || EQUAL(itype, ""))
1895 : {
1896 19 : eDataType = GDT_Float32;
1897 19 : dfNoData = ISIS3_NULL4;
1898 : }
1899 : else
1900 : {
1901 1 : CPLError(CE_Failure, CPLE_OpenFailed, "%s pixel type not supported.",
1902 : itype);
1903 1 : return nullptr;
1904 : }
1905 :
1906 : /*********** Grab samples lines band ************/
1907 :
1908 : // default to MSB
1909 : const bool bIsLSB =
1910 262 : EQUAL(poDS->GetKeyword("IsisCube.Core.Pixels.ByteOrder"), "Lsb");
1911 :
1912 : /*********** Grab Cellsize ************/
1913 262 : double dfXDim = 1.0;
1914 262 : double dfYDim = 1.0;
1915 :
1916 262 : const char *pszRes = poDS->GetKeyword("IsisCube.Mapping.PixelResolution");
1917 262 : if (strlen(pszRes) > 0)
1918 : {
1919 102 : dfXDim = CPLAtof(pszRes); /* values are in meters */
1920 102 : dfYDim = -CPLAtof(pszRes);
1921 : }
1922 :
1923 : /*********** Grab UpperLeftCornerY ************/
1924 262 : double dfULYMap = 0.5;
1925 :
1926 262 : const char *pszULY = poDS->GetKeyword("IsisCube.Mapping.UpperLeftCornerY");
1927 262 : if (strlen(pszULY) > 0)
1928 : {
1929 102 : dfULYMap = CPLAtof(pszULY);
1930 : }
1931 :
1932 : /*********** Grab UpperLeftCornerX ************/
1933 262 : double dfULXMap = 0.5;
1934 :
1935 262 : const char *pszULX = poDS->GetKeyword("IsisCube.Mapping.UpperLeftCornerX");
1936 262 : if (strlen(pszULX) > 0)
1937 : {
1938 102 : dfULXMap = CPLAtof(pszULX);
1939 : }
1940 :
1941 : /*********** Grab TARGET_NAME ************/
1942 : /**** This is the planets name i.e. Mars ***/
1943 262 : const char *target_name = poDS->GetKeyword("IsisCube.Mapping.TargetName");
1944 :
1945 : #ifdef notdef
1946 : const double dfLongitudeMulFactor =
1947 : EQUAL(poDS->GetKeyword("IsisCube.Mapping.LongitudeDirection",
1948 : "PositiveEast"),
1949 : "PositiveEast")
1950 : ? 1
1951 : : -1;
1952 : #else
1953 262 : const double dfLongitudeMulFactor = 1;
1954 : #endif
1955 :
1956 : /*********** Grab MAP_PROJECTION_TYPE ************/
1957 : const char *map_proj_name =
1958 262 : poDS->GetKeyword("IsisCube.Mapping.ProjectionName");
1959 :
1960 : /*********** Grab SEMI-MAJOR ************/
1961 : const double semi_major =
1962 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.EquatorialRadius"));
1963 :
1964 : /*********** Grab semi-minor ************/
1965 : const double semi_minor =
1966 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.PolarRadius"));
1967 :
1968 : /*********** Grab CENTER_LAT ************/
1969 : const double center_lat =
1970 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.CenterLatitude"));
1971 :
1972 : /*********** Grab CENTER_LON ************/
1973 : const double center_lon =
1974 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.CenterLongitude")) *
1975 : dfLongitudeMulFactor;
1976 :
1977 : /*********** Grab 1st std parallel ************/
1978 : const double first_std_parallel =
1979 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.FirstStandardParallel"));
1980 :
1981 : /*********** Grab 2nd std parallel ************/
1982 : const double second_std_parallel =
1983 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.SecondStandardParallel"));
1984 :
1985 : /*********** Grab scaleFactor ************/
1986 : const double scaleFactor =
1987 262 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.scaleFactor", "1.0"));
1988 :
1989 : /*** grab LatitudeType = Planetographic ****/
1990 : // Need to further study how ocentric/ographic will effect the gdal library
1991 : // So far we will use this fact to define a sphere or ellipse for some
1992 : // projections
1993 :
1994 : // Frank - may need to talk this over
1995 262 : bool bIsGeographic = true;
1996 262 : if (EQUAL(poDS->GetKeyword("IsisCube.Mapping.LatitudeType"),
1997 : "Planetocentric"))
1998 92 : bIsGeographic = false;
1999 :
2000 : // Set oSRS projection and parameters
2001 : // ############################################################
2002 : // ISIS3 Projection types
2003 : // Equirectangular
2004 : // LambertConformal
2005 : // Mercator
2006 : // ObliqueCylindrical
2007 : // Orthographic
2008 : // PolarStereographic
2009 : // SimpleCylindrical
2010 : // Sinusoidal
2011 : // TransverseMercator
2012 :
2013 : #ifdef DEBUG
2014 262 : CPLDebug("ISIS3", "using projection %s", map_proj_name);
2015 : #endif
2016 :
2017 524 : OGRSpatialReference oSRS;
2018 262 : bool bProjectionSet = true;
2019 :
2020 262 : if ((EQUAL(map_proj_name, "Equirectangular")) ||
2021 195 : (EQUAL(map_proj_name, "SimpleCylindrical")))
2022 : {
2023 82 : oSRS.SetEquirectangular2(0.0, center_lon, center_lat, 0, 0);
2024 : }
2025 180 : else if (EQUAL(map_proj_name, "Orthographic"))
2026 : {
2027 2 : oSRS.SetOrthographic(center_lat, center_lon, 0, 0);
2028 : }
2029 178 : else if (EQUAL(map_proj_name, "Sinusoidal"))
2030 : {
2031 2 : oSRS.SetSinusoidal(center_lon, 0, 0);
2032 : }
2033 176 : else if (EQUAL(map_proj_name, "Mercator"))
2034 : {
2035 2 : oSRS.SetMercator(center_lat, center_lon, scaleFactor, 0, 0);
2036 : }
2037 174 : else if (EQUAL(map_proj_name, "PolarStereographic"))
2038 : {
2039 2 : oSRS.SetPS(center_lat, center_lon, scaleFactor, 0, 0);
2040 : }
2041 172 : else if (EQUAL(map_proj_name, "TransverseMercator"))
2042 : {
2043 21 : oSRS.SetTM(center_lat, center_lon, scaleFactor, 0, 0);
2044 : }
2045 151 : else if (EQUAL(map_proj_name, "LambertConformal"))
2046 : {
2047 2 : oSRS.SetLCC(first_std_parallel, second_std_parallel, center_lat,
2048 : center_lon, 0, 0);
2049 : }
2050 149 : else if (EQUAL(map_proj_name, "PointPerspective"))
2051 : {
2052 : // Distance parameter is the distance to the center of the body, and is
2053 : // given in km
2054 : const double distance =
2055 1 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.Distance")) * 1000.0;
2056 1 : const double height_above_ground = distance - semi_major;
2057 1 : oSRS.SetVerticalPerspective(center_lat, center_lon, 0,
2058 : height_above_ground, 0, 0);
2059 : }
2060 148 : else if (EQUAL(map_proj_name, "ObliqueCylindrical"))
2061 : {
2062 : const double poleLatitude =
2063 4 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.PoleLatitude"));
2064 : const double poleLongitude =
2065 4 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.PoleLongitude")) *
2066 : dfLongitudeMulFactor;
2067 : const double poleRotation =
2068 4 : CPLAtof(poDS->GetKeyword("IsisCube.Mapping.PoleRotation"));
2069 8 : CPLString oProj4String;
2070 : // ISIS3 rotated pole doesn't use the same conventions than PROJ ob_tran
2071 : // Compare the sign difference in
2072 : // https://github.com/USGS-Astrogeology/ISIS3/blob/3.8.0/isis/src/base/objs/ObliqueCylindrical/ObliqueCylindrical.cpp#L244
2073 : // and
2074 : // https://github.com/OSGeo/PROJ/blob/6.2/src/projections/ob_tran.cpp#L34
2075 : // They can be compensated by modifying the poleLatitude to
2076 : // 180-poleLatitude There's also a sign difference for the poleRotation
2077 : // parameter The existence of those different conventions is
2078 : // acknowledged in
2079 : // https://pds-imaging.jpl.nasa.gov/documentation/Cassini_BIDRSIS.PDF in
2080 : // the middle of page 10
2081 : oProj4String.Printf("+proj=ob_tran +o_proj=eqc +o_lon_p=%.17g "
2082 : "+o_lat_p=%.17g +lon_0=%.17g",
2083 4 : -poleRotation, 180 - poleLatitude, poleLongitude);
2084 4 : oSRS.SetFromUserInput(oProj4String);
2085 : }
2086 : else
2087 : {
2088 144 : CPLDebug("ISIS3",
2089 : "Dataset projection %s is not supported. Continuing...",
2090 : map_proj_name);
2091 144 : bProjectionSet = false;
2092 : }
2093 :
2094 262 : if (bProjectionSet)
2095 : {
2096 : // Create projection name, i.e. MERCATOR MARS and set as ProjCS keyword
2097 236 : CPLString osProjTargetName(map_proj_name);
2098 118 : osProjTargetName += " ";
2099 118 : osProjTargetName += target_name;
2100 118 : oSRS.SetProjCS(osProjTargetName); // set ProjCS keyword
2101 :
2102 : // The geographic/geocentric name will be the same basic name as the
2103 : // body name 'GCS' = Geographic/Geocentric Coordinate System
2104 236 : CPLString osGeogName("GCS_");
2105 118 : osGeogName += target_name;
2106 :
2107 : // The datum name will be the same basic name as the planet
2108 236 : CPLString osDatumName("D_");
2109 118 : osDatumName += target_name;
2110 :
2111 236 : CPLString osSphereName(target_name);
2112 : // strcat(osSphereName, "_IAU_IAG"); //Might not be IAU defined so
2113 : // don't add
2114 :
2115 : // calculate inverse flattening from major and minor axis: 1/f = a/(a-b)
2116 118 : double iflattening = 0.0;
2117 118 : if ((semi_major - semi_minor) < 0.0000001)
2118 38 : iflattening = 0;
2119 : else
2120 80 : iflattening = semi_major / (semi_major - semi_minor);
2121 :
2122 : // Set the body size but take into consideration which proj is being
2123 : // used to help w/ proj4 compatibility The use of a Sphere, polar radius
2124 : // or ellipse here is based on how ISIS does it internally
2125 118 : if (((EQUAL(map_proj_name, "Stereographic") &&
2126 0 : (fabs(center_lat) == 90))) ||
2127 118 : (EQUAL(map_proj_name, "PolarStereographic")))
2128 : {
2129 2 : if (bIsGeographic)
2130 : {
2131 : // Geograpraphic, so set an ellipse
2132 0 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
2133 : semi_major, iflattening, "Reference_Meridian",
2134 : 0.0);
2135 : }
2136 : else
2137 : {
2138 : // Geocentric, so force a sphere using the semi-minor axis. I
2139 : // hope...
2140 2 : osSphereName += "_polarRadius";
2141 2 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
2142 : semi_minor, 0.0, "Reference_Meridian", 0.0);
2143 : }
2144 : }
2145 116 : else if ((EQUAL(map_proj_name, "SimpleCylindrical")) ||
2146 101 : (EQUAL(map_proj_name, "Orthographic")) ||
2147 99 : (EQUAL(map_proj_name, "Stereographic")) ||
2148 99 : (EQUAL(map_proj_name, "Sinusoidal")) ||
2149 97 : (EQUAL(map_proj_name, "PointPerspective")))
2150 : {
2151 : // ISIS uses the spherical equation for these projections
2152 : // so force a sphere.
2153 20 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName, semi_major,
2154 : 0.0, "Reference_Meridian", 0.0);
2155 : }
2156 96 : else if (EQUAL(map_proj_name, "Equirectangular"))
2157 : {
2158 : // Calculate localRadius using ISIS3 simple elliptical method
2159 : // not the more standard Radius of Curvature method
2160 : // PI = 4 * atan(1);
2161 67 : const double radLat = center_lat * M_PI / 180; // in radians
2162 67 : const double meanRadius = sqrt(pow(semi_minor * cos(radLat), 2) +
2163 67 : pow(semi_major * sin(radLat), 2));
2164 67 : const double localRadius =
2165 67 : (meanRadius == 0.0) ? 0.0
2166 67 : : semi_major * semi_minor / meanRadius;
2167 67 : osSphereName += "_localRadius";
2168 67 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName, localRadius,
2169 : 0.0, "Reference_Meridian", 0.0);
2170 : }
2171 : else
2172 : {
2173 : // All other projections: Mercator, Transverse Mercator, Lambert
2174 : // Conformal, etc. Geographic, so set an ellipse
2175 29 : if (bIsGeographic)
2176 : {
2177 0 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
2178 : semi_major, iflattening, "Reference_Meridian",
2179 : 0.0);
2180 : }
2181 : else
2182 : {
2183 : // Geocentric, so force a sphere. I hope...
2184 29 : oSRS.SetGeogCS(osGeogName, osDatumName, osSphereName,
2185 : semi_major, 0.0, "Reference_Meridian", 0.0);
2186 : }
2187 : }
2188 :
2189 : // translate back into a projection string.
2190 118 : poDS->m_oSRS = std::move(oSRS);
2191 118 : poDS->m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2192 : }
2193 :
2194 : /* END ISIS3 Label Read */
2195 : /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
2196 :
2197 : /* -------------------------------------------------------------------- */
2198 : /* Did we get the required keywords? If not we return with */
2199 : /* this never having been considered to be a match. This isn't */
2200 : /* an error! */
2201 : /* -------------------------------------------------------------------- */
2202 523 : if (!GDALCheckDatasetDimensions(nCols, nRows) ||
2203 261 : !GDALCheckBandCount(nBands, false))
2204 : {
2205 2 : return nullptr;
2206 : }
2207 :
2208 : /* -------------------------------------------------------------------- */
2209 : /* Capture some information from the file that is of interest. */
2210 : /* -------------------------------------------------------------------- */
2211 260 : poDS->nRasterXSize = nCols;
2212 260 : poDS->nRasterYSize = nRows;
2213 :
2214 : /* -------------------------------------------------------------------- */
2215 : /* Open target binary file. */
2216 : /* -------------------------------------------------------------------- */
2217 260 : if (EQUAL(osFormat, "GeoTIFF"))
2218 : {
2219 27 : if (nSkipBytes != 0)
2220 : {
2221 2 : CPLError(CE_Warning, CPLE_NotSupported,
2222 : "Ignoring StartByte=%d for format=GeoTIFF",
2223 : 1 + nSkipBytes);
2224 : }
2225 27 : if (osQubeFile == poOpenInfo->pszFilename)
2226 : {
2227 0 : CPLError(CE_Failure, CPLE_AppDefined, "A ^Core file must be set");
2228 0 : return nullptr;
2229 : }
2230 54 : poDS->m_poExternalDS =
2231 27 : GDALDataset::Open(osQubeFile, poOpenInfo->eAccess);
2232 27 : if (poDS->m_poExternalDS == nullptr)
2233 : {
2234 2 : return nullptr;
2235 : }
2236 25 : if (poDS->m_poExternalDS->GetRasterXSize() != poDS->nRasterXSize ||
2237 24 : poDS->m_poExternalDS->GetRasterYSize() != poDS->nRasterYSize ||
2238 71 : poDS->m_poExternalDS->GetRasterCount() != nBands ||
2239 22 : poDS->m_poExternalDS->GetRasterBand(1)->GetRasterDataType() !=
2240 : eDataType)
2241 : {
2242 4 : CPLError(CE_Failure, CPLE_AppDefined,
2243 : "%s has incompatible characteristics with the ones "
2244 : "declared in the label.",
2245 : osQubeFile.c_str());
2246 4 : return nullptr;
2247 : }
2248 : }
2249 : else
2250 : {
2251 233 : if (poOpenInfo->eAccess == GA_ReadOnly)
2252 231 : poDS->m_fpImage = VSIFOpenL(osQubeFile, "r");
2253 : else
2254 2 : poDS->m_fpImage = VSIFOpenL(osQubeFile, "r+");
2255 :
2256 233 : if (poDS->m_fpImage == nullptr)
2257 : {
2258 2 : CPLError(CE_Failure, CPLE_OpenFailed, "Failed to open %s: %s.",
2259 2 : osQubeFile.c_str(), VSIStrerror(errno));
2260 2 : return nullptr;
2261 : }
2262 :
2263 : // Sanity checks in case the external raw file appears to be a
2264 : // TIFF file
2265 231 : if (EQUAL(CPLGetExtensionSafe(osQubeFile).c_str(), "tif"))
2266 : {
2267 23 : const char *const apszAllowedDrivers[] = {"GTiff", nullptr};
2268 : auto poTIF_DS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
2269 : osQubeFile, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
2270 46 : apszAllowedDrivers));
2271 23 : if (poTIF_DS)
2272 : {
2273 23 : bool bWarned = false;
2274 23 : if (poTIF_DS->GetRasterXSize() != poDS->nRasterXSize ||
2275 22 : poTIF_DS->GetRasterYSize() != poDS->nRasterYSize ||
2276 21 : poTIF_DS->GetRasterCount() != nBands ||
2277 20 : poTIF_DS->GetRasterBand(1)->GetRasterDataType() !=
2278 45 : eDataType ||
2279 38 : poTIF_DS->GetMetadataItem(GDALMD_COMPRESSION,
2280 19 : GDAL_MDD_IMAGE_STRUCTURE) !=
2281 : nullptr)
2282 : {
2283 5 : bWarned = true;
2284 5 : CPLError(
2285 : CE_Warning, CPLE_AppDefined,
2286 : "%s has incompatible characteristics with the ones "
2287 : "declared in the label.",
2288 : osQubeFile.c_str());
2289 : }
2290 23 : int nBlockXSize = 1, nBlockYSize = 1;
2291 23 : poTIF_DS->GetRasterBand(1)->GetBlockSize(&nBlockXSize,
2292 : &nBlockYSize);
2293 23 : if ((poDS->m_bIsTiled &&
2294 46 : (nBlockXSize != tileSizeX || nBlockYSize != tileSizeY)) ||
2295 23 : (!poDS->m_bIsTiled && (nBlockXSize != nCols ||
2296 2 : (nBands > 1 && nBlockYSize != 1))))
2297 : {
2298 2 : if (!bWarned)
2299 : {
2300 1 : bWarned = true;
2301 1 : CPLError(
2302 : CE_Warning, CPLE_AppDefined,
2303 : "%s has incompatible characteristics with the ones "
2304 : "declared in the label.",
2305 : osQubeFile.c_str());
2306 : }
2307 : }
2308 : // to please Clang Static Analyzer
2309 23 : nBlockXSize = std::max(1, nBlockXSize);
2310 23 : nBlockYSize = std::max(1, nBlockYSize);
2311 :
2312 : // Check that blocks are effectively written in expected order.
2313 23 : const int nBlockSizeBytes = nBlockXSize * nBlockYSize *
2314 23 : GDALGetDataTypeSizeBytes(eDataType);
2315 23 : bool bGoOn = !bWarned;
2316 23 : const int l_nBlocksPerRow = DIV_ROUND_UP(nCols, nBlockXSize);
2317 23 : const int l_nBlocksPerColumn = DIV_ROUND_UP(nRows, nBlockYSize);
2318 23 : int nBlockNo = 0;
2319 52 : for (int i = 0; i < nBands && bGoOn; i++)
2320 : {
2321 1285 : for (int y = 0; y < l_nBlocksPerColumn && bGoOn; y++)
2322 : {
2323 2950 : for (int x = 0; x < l_nBlocksPerRow && bGoOn; x++)
2324 : {
2325 : const char *pszBlockOffset =
2326 3388 : poTIF_DS->GetRasterBand(i + 1)->GetMetadataItem(
2327 : CPLSPrintf("BLOCK_OFFSET_%d_%d", x, y),
2328 1694 : "TIFF");
2329 1694 : if (pszBlockOffset)
2330 : {
2331 1694 : GIntBig nOffset = CPLAtoGIntBig(pszBlockOffset);
2332 1694 : if (nOffset !=
2333 1694 : nSkipBytes + nBlockNo * nBlockSizeBytes)
2334 : {
2335 : // bWarned = true;
2336 2 : CPLError(CE_Warning, CPLE_AppDefined,
2337 : "%s has incompatible "
2338 : "characteristics with the ones "
2339 : "declared in the label.",
2340 : osQubeFile.c_str());
2341 2 : bGoOn = false;
2342 : }
2343 : }
2344 1694 : nBlockNo++;
2345 : }
2346 : }
2347 : }
2348 : }
2349 : }
2350 : }
2351 :
2352 252 : poDS->eAccess = poOpenInfo->eAccess;
2353 :
2354 : /* -------------------------------------------------------------------- */
2355 : /* Compute the line offset. */
2356 : /* -------------------------------------------------------------------- */
2357 252 : int nLineOffset = 0;
2358 252 : int nPixelOffset = 0;
2359 252 : vsi_l_offset nBandOffset = 0;
2360 :
2361 252 : if (EQUAL(osFormat, "BandSequential"))
2362 : {
2363 206 : const int nItemSize = GDALGetDataTypeSizeBytes(eDataType);
2364 206 : nPixelOffset = nItemSize;
2365 : try
2366 : {
2367 206 : nLineOffset = (CPLSM(nPixelOffset) * CPLSM(nCols)).v();
2368 : }
2369 0 : catch (const CPLSafeIntOverflow &)
2370 : {
2371 0 : return nullptr;
2372 : }
2373 206 : nBandOffset = static_cast<vsi_l_offset>(nLineOffset) * nRows;
2374 :
2375 206 : poDS->m_sLayout.osRawFilename = std::move(osQubeFile);
2376 206 : if (nBands > 1)
2377 16 : poDS->m_sLayout.eInterleaving = RawBinaryLayout::Interleaving::BSQ;
2378 206 : poDS->m_sLayout.eDataType = eDataType;
2379 206 : poDS->m_sLayout.bLittleEndianOrder = bIsLSB;
2380 206 : poDS->m_sLayout.nImageOffset = nSkipBytes;
2381 206 : poDS->m_sLayout.nPixelOffset = nPixelOffset;
2382 206 : poDS->m_sLayout.nLineOffset = nLineOffset;
2383 206 : poDS->m_sLayout.nBandOffset = static_cast<GIntBig>(nBandOffset);
2384 : }
2385 : /* else Tiled or external */
2386 :
2387 : /* -------------------------------------------------------------------- */
2388 : /* Extract BandBin info. */
2389 : /* -------------------------------------------------------------------- */
2390 504 : std::vector<std::string> aosBandNames;
2391 504 : std::vector<std::string> aosBandUnits;
2392 504 : std::vector<double> adfWavelengths;
2393 504 : std::vector<std::string> aosWavelengthsUnit;
2394 504 : std::vector<double> adfBandwidth;
2395 504 : std::vector<std::string> aosBandwidthUnit;
2396 756 : const auto oIsisCube = poDS->m_oJSonLabel.GetObj("IsisCube");
2397 252 : if (oIsisCube.GetType() == CPLJSONObject::Type::Object)
2398 : {
2399 967 : for (const auto &oChildIsisCube : oIsisCube.GetChildren())
2400 : {
2401 455 : if (oChildIsisCube.GetType() == CPLJSONObject::Type::Object &&
2402 2048 : oChildIsisCube.GetString("_type") == "group" &&
2403 1079 : (oChildIsisCube.GetName() == "BandBin" ||
2404 878 : oChildIsisCube.GetString("_container_name") == "BandBin"))
2405 : {
2406 187 : for (const auto &child : oChildIsisCube.GetChildren())
2407 : {
2408 146 : if (CPLString(child.GetName()).ifind("name") !=
2409 : std::string::npos)
2410 : {
2411 : // Use "name" in priority
2412 15 : if (EQUAL(child.GetName().c_str(), "name"))
2413 : {
2414 5 : aosBandNames.clear();
2415 : }
2416 10 : else if (!aosBandNames.empty())
2417 : {
2418 3 : continue;
2419 : }
2420 :
2421 12 : if (child.GetType() == CPLJSONObject::Type::String &&
2422 : nBands == 1)
2423 : {
2424 4 : aosBandNames.push_back(child.ToString());
2425 : }
2426 8 : else if (child.GetType() == CPLJSONObject::Type::Array)
2427 : {
2428 16 : auto oArray = child.ToArray();
2429 8 : if (oArray.Size() == nBands)
2430 : {
2431 28 : for (int i = 0; i < nBands; i++)
2432 : {
2433 20 : if (oArray[i].GetType() ==
2434 : CPLJSONObject::Type::String)
2435 : {
2436 20 : aosBandNames.push_back(
2437 40 : oArray[i].ToString());
2438 : }
2439 : else
2440 : {
2441 0 : aosBandNames.clear();
2442 0 : break;
2443 : }
2444 : }
2445 : }
2446 : }
2447 : }
2448 135 : else if (EQUAL(child.GetName().c_str(), "BandSuffixUnit") &&
2449 4 : child.GetType() == CPLJSONObject::Type::Array)
2450 : {
2451 8 : auto oArray = child.ToArray();
2452 4 : if (oArray.Size() == nBands)
2453 : {
2454 12 : for (int i = 0; i < nBands; i++)
2455 : {
2456 8 : if (oArray[i].GetType() ==
2457 : CPLJSONObject::Type::String)
2458 : {
2459 8 : aosBandUnits.push_back(
2460 16 : oArray[i].ToString());
2461 : }
2462 : else
2463 : {
2464 0 : aosBandUnits.clear();
2465 0 : break;
2466 : }
2467 : }
2468 : }
2469 : }
2470 377 : else if (EQUAL(child.GetName().c_str(), "BandBinCenter") ||
2471 250 : EQUAL(child.GetName().c_str(), "Center"))
2472 : {
2473 41 : GetValueAndUnits(child, adfWavelengths,
2474 : aosWavelengthsUnit, nBands);
2475 : }
2476 90 : else if (EQUAL(child.GetName().c_str(), "BandBinUnit") &&
2477 4 : child.GetType() == CPLJSONObject::Type::String)
2478 : {
2479 12 : CPLString unit(child.ToString());
2480 4 : if (STARTS_WITH_CI(unit, "micromet") ||
2481 0 : EQUAL(unit, "um") ||
2482 4 : STARTS_WITH_CI(unit, "nanomet") ||
2483 0 : EQUAL(unit, "nm"))
2484 : {
2485 4 : aosWavelengthsUnit.push_back(child.ToString());
2486 : }
2487 : }
2488 82 : else if (EQUAL(child.GetName().c_str(), "Width"))
2489 : {
2490 7 : GetValueAndUnits(child, adfBandwidth, aosBandwidthUnit,
2491 : nBands);
2492 : }
2493 : }
2494 :
2495 41 : if (!adfWavelengths.empty() && aosWavelengthsUnit.size() == 1)
2496 : {
2497 11 : for (int i = 1; i < nBands; i++)
2498 : {
2499 4 : aosWavelengthsUnit.push_back(aosWavelengthsUnit[0]);
2500 : }
2501 : }
2502 41 : if (!adfBandwidth.empty() && aosBandwidthUnit.size() == 1)
2503 : {
2504 7 : for (int i = 1; i < nBands; i++)
2505 : {
2506 2 : aosBandwidthUnit.push_back(aosBandwidthUnit[0]);
2507 : }
2508 : }
2509 : }
2510 : }
2511 : }
2512 :
2513 : /* -------------------------------------------------------------------- */
2514 : /* Create band information objects. */
2515 : /* -------------------------------------------------------------------- */
2516 : #ifdef CPL_LSB
2517 252 : const bool bNativeOrder = bIsLSB;
2518 : #else
2519 : const bool bNativeOrder = !bIsLSB;
2520 : #endif
2521 :
2522 554 : for (int i = 0; i < nBands; i++)
2523 : {
2524 : GDALRasterBand *poBand;
2525 :
2526 302 : if (poDS->m_poExternalDS != nullptr)
2527 : {
2528 : auto poISISBand = std::make_unique<ISIS3WrapperRasterBand>(
2529 21 : poDS->m_poExternalDS->GetRasterBand(i + 1));
2530 42 : poISISBand->SetMaskBand(
2531 42 : std::make_unique<ISISMaskBand>(poISISBand.get()));
2532 21 : poDS->SetBand(i + 1, std::move(poISISBand));
2533 21 : poBand = poDS->GetRasterBand(i + 1);
2534 : }
2535 281 : else if (poDS->m_bIsTiled)
2536 : {
2537 : auto poISISBand = std::make_unique<ISISTiledBand>(
2538 41 : poDS.get(), poDS->m_fpImage, i + 1, eDataType, tileSizeX,
2539 82 : tileSizeY, nSkipBytes, 0, 0, bNativeOrder);
2540 41 : if (!poISISBand->IsValid())
2541 : {
2542 0 : return nullptr;
2543 : }
2544 82 : poISISBand->SetMaskBand(
2545 82 : std::make_unique<ISISMaskBand>(poISISBand.get()));
2546 41 : poDS->SetBand(i + 1, std::move(poISISBand));
2547 41 : poBand = poDS->GetRasterBand(i + 1);
2548 : }
2549 : else
2550 : {
2551 : auto poISISBand = std::make_unique<ISIS3RawRasterBand>(
2552 240 : poDS.get(), i + 1, poDS->m_fpImage,
2553 240 : nSkipBytes + nBandOffset * i, nPixelOffset, nLineOffset,
2554 240 : eDataType, bNativeOrder);
2555 240 : if (!poISISBand->IsValid())
2556 : {
2557 0 : return nullptr;
2558 : }
2559 480 : poISISBand->SetMaskBand(
2560 480 : std::make_unique<ISISMaskBand>(poISISBand.get()));
2561 240 : poDS->SetBand(i + 1, std::move(poISISBand));
2562 240 : poBand = poDS->GetRasterBand(i + 1);
2563 : }
2564 :
2565 302 : if (i < static_cast<int>(aosBandNames.size()))
2566 : {
2567 17 : poBand->SetDescription(aosBandNames[i].c_str());
2568 : }
2569 351 : if (i < static_cast<int>(adfWavelengths.size()) &&
2570 49 : i < static_cast<int>(aosWavelengthsUnit.size()))
2571 : {
2572 11 : poBand->SetMetadataItem("WAVELENGTH",
2573 11 : CPLSPrintf("%f", adfWavelengths[i]));
2574 11 : poBand->SetMetadataItem("WAVELENGTH_UNIT",
2575 11 : aosWavelengthsUnit[i].c_str());
2576 18 : if (i < static_cast<int>(adfBandwidth.size()) &&
2577 7 : i < static_cast<int>(aosBandwidthUnit.size()))
2578 : {
2579 7 : poBand->SetMetadataItem("BANDWIDTH",
2580 7 : CPLSPrintf("%f", adfBandwidth[i]));
2581 7 : poBand->SetMetadataItem("BANDWIDTH_UNIT",
2582 7 : aosBandwidthUnit[i].c_str());
2583 : }
2584 : }
2585 302 : if (i < static_cast<int>(aosBandUnits.size()))
2586 : {
2587 8 : poBand->SetUnitType(aosBandUnits[i].c_str());
2588 : }
2589 :
2590 302 : poBand->SetNoDataValue(dfNoData);
2591 :
2592 : // Set offset/scale values.
2593 : const double dfOffset =
2594 302 : CPLAtofM(poDS->GetKeyword("IsisCube.Core.Pixels.Base", "0.0"));
2595 302 : const double dfScale = CPLAtofM(
2596 : poDS->GetKeyword("IsisCube.Core.Pixels.Multiplier", "1.0"));
2597 302 : if (dfOffset != 0.0 || dfScale != 1.0)
2598 : {
2599 52 : poBand->SetOffset(dfOffset);
2600 52 : poBand->SetScale(dfScale);
2601 : }
2602 : }
2603 :
2604 : /* -------------------------------------------------------------------- */
2605 : /* Check for a .prj file. For ISIS3 I would like to keep this in */
2606 : /* -------------------------------------------------------------------- */
2607 504 : const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename);
2608 504 : const CPLString osName = CPLGetBasenameSafe(poOpenInfo->pszFilename);
2609 504 : const std::string osPrjFile = CPLFormCIFilenameSafe(osPath, osName, "prj");
2610 :
2611 252 : VSILFILE *fp = VSIFOpenL(osPrjFile.c_str(), "r");
2612 252 : if (fp != nullptr)
2613 : {
2614 0 : VSIFCloseL(fp);
2615 :
2616 0 : char **papszLines = CSLLoad(osPrjFile.c_str());
2617 :
2618 0 : OGRSpatialReference oSRS2;
2619 0 : if (oSRS2.importFromESRI(papszLines) == OGRERR_NONE)
2620 : {
2621 0 : poDS->m_aosAdditionalFiles.AddString(osPrjFile.c_str());
2622 0 : poDS->m_oSRS = std::move(oSRS2);
2623 0 : poDS->m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2624 : }
2625 :
2626 0 : CSLDestroy(papszLines);
2627 : }
2628 :
2629 252 : if (dfULXMap != 0.5 || dfULYMap != 0.5 || dfXDim != 1.0 || dfYDim != 1.0)
2630 : {
2631 99 : poDS->m_bGotTransform = true;
2632 99 : poDS->m_gt.xorig = dfULXMap;
2633 99 : poDS->m_gt.xscale = dfXDim;
2634 99 : poDS->m_gt.xrot = 0.0;
2635 99 : poDS->m_gt.yorig = dfULYMap;
2636 99 : poDS->m_gt.yrot = 0.0;
2637 99 : poDS->m_gt.yscale = dfYDim;
2638 : }
2639 :
2640 252 : if (!poDS->m_bGotTransform)
2641 : {
2642 306 : poDS->m_bGotTransform = CPL_TO_BOOL(GDALReadWorldFile(
2643 306 : poOpenInfo->pszFilename, "cbw", poDS->m_gt.data()));
2644 153 : if (poDS->m_bGotTransform)
2645 : {
2646 0 : poDS->m_aosAdditionalFiles.AddString(
2647 0 : CPLResetExtensionSafe(poOpenInfo->pszFilename, "cbw").c_str());
2648 : }
2649 : }
2650 :
2651 252 : if (!poDS->m_bGotTransform)
2652 : {
2653 306 : poDS->m_bGotTransform = CPL_TO_BOOL(GDALReadWorldFile(
2654 306 : poOpenInfo->pszFilename, "wld", poDS->m_gt.data()));
2655 153 : if (poDS->m_bGotTransform)
2656 : {
2657 0 : poDS->m_aosAdditionalFiles.AddString(
2658 0 : CPLResetExtensionSafe(poOpenInfo->pszFilename, "wld").c_str());
2659 : }
2660 : }
2661 :
2662 : /* -------------------------------------------------------------------- */
2663 : /* Initialize any PAM information. */
2664 : /* -------------------------------------------------------------------- */
2665 252 : poDS->SetDescription(poOpenInfo->pszFilename);
2666 252 : poDS->TryLoadXML();
2667 :
2668 : /* -------------------------------------------------------------------- */
2669 : /* Check for overviews. */
2670 : /* -------------------------------------------------------------------- */
2671 252 : poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename);
2672 :
2673 252 : return poDS.release();
2674 : }
2675 :
2676 : /************************************************************************/
2677 : /* GetKeyword() */
2678 : /************************************************************************/
2679 :
2680 6184 : const char *ISIS3Dataset::GetKeyword(const char *pszPath,
2681 : const char *pszDefault)
2682 :
2683 : {
2684 6184 : return m_oKeywords.GetKeyword(pszPath, pszDefault);
2685 : }
2686 :
2687 : /************************************************************************/
2688 : /* FixLong() */
2689 : /************************************************************************/
2690 :
2691 247 : double ISIS3Dataset::FixLong(double dfLong)
2692 : {
2693 247 : if (m_osLongitudeDirection == "PositiveWest")
2694 4 : dfLong = -dfLong;
2695 247 : if (m_bForce360 && dfLong < 0)
2696 2 : dfLong += 360.0;
2697 247 : return dfLong;
2698 : }
2699 :
2700 : /************************************************************************/
2701 : /* BuildLabel() */
2702 : /************************************************************************/
2703 :
2704 132 : void ISIS3Dataset::BuildLabel()
2705 : {
2706 264 : CPLJSONObject oLabel = m_oSrcJSonLabel;
2707 132 : if (!oLabel.IsValid())
2708 : {
2709 105 : oLabel = CPLJSONObject();
2710 : }
2711 : // If we have a source label, then edit it directly
2712 396 : CPLJSONObject oIsisCube = GetOrCreateJSONObject(oLabel, "IsisCube");
2713 132 : oIsisCube.Set("_type", "object");
2714 :
2715 132 : if (!m_osComment.empty())
2716 1 : oIsisCube.Set("_comment", m_osComment);
2717 :
2718 396 : CPLJSONObject oCore = GetOrCreateJSONObject(oIsisCube, "Core");
2719 132 : if (oCore.GetType() != CPLJSONObject::Type::Object)
2720 : {
2721 0 : oIsisCube.Delete("Core");
2722 0 : oCore = CPLJSONObject();
2723 0 : oIsisCube.Add("Core", oCore);
2724 : }
2725 132 : oCore.Set("_type", "object");
2726 :
2727 132 : if (!m_osExternalFilename.empty())
2728 : {
2729 31 : if (m_poExternalDS && m_bGeoTIFFAsRegularExternal)
2730 : {
2731 8 : if (!m_bGeoTIFFInitDone)
2732 : {
2733 : cpl::down_cast<ISIS3WrapperRasterBand *>(GetRasterBand(1))
2734 1 : ->InitFile();
2735 : }
2736 :
2737 : const char *pszOffset =
2738 8 : m_poExternalDS->GetRasterBand(1)->GetMetadataItem(
2739 8 : "BLOCK_OFFSET_0_0", "TIFF");
2740 8 : if (pszOffset)
2741 : {
2742 8 : oCore.Set("StartByte", 1 + atoi(pszOffset));
2743 : }
2744 : else
2745 : {
2746 : // Shouldn't happen normally
2747 0 : CPLError(CE_Warning, CPLE_AppDefined,
2748 : "Missing BLOCK_OFFSET_0_0");
2749 0 : m_bGeoTIFFAsRegularExternal = false;
2750 0 : oCore.Set("StartByte", 1);
2751 8 : }
2752 : }
2753 : else
2754 : {
2755 23 : oCore.Set("StartByte", 1);
2756 : }
2757 31 : if (!m_osExternalFilename.empty())
2758 : {
2759 : const CPLString osExternalFilename =
2760 31 : CPLGetFilename(m_osExternalFilename);
2761 31 : oCore.Set("^Core", osExternalFilename);
2762 : }
2763 : }
2764 : else
2765 : {
2766 101 : oCore.Set("StartByte", pszSTARTBYTE_PLACEHOLDER);
2767 101 : oCore.Delete("^Core");
2768 : }
2769 :
2770 132 : if (m_poExternalDS && !m_bGeoTIFFAsRegularExternal)
2771 : {
2772 10 : oCore.Set("Format", "GeoTIFF");
2773 10 : oCore.Delete("TileSamples");
2774 10 : oCore.Delete("TileLines");
2775 : }
2776 122 : else if (m_bIsTiled)
2777 : {
2778 9 : oCore.Set("Format", "Tile");
2779 9 : int nBlockXSize = 1, nBlockYSize = 1;
2780 9 : GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize);
2781 9 : oCore.Set("TileSamples", nBlockXSize);
2782 9 : oCore.Set("TileLines", nBlockYSize);
2783 : }
2784 : else
2785 : {
2786 113 : oCore.Set("Format", "BandSequential");
2787 113 : oCore.Delete("TileSamples");
2788 113 : oCore.Delete("TileLines");
2789 : }
2790 :
2791 396 : CPLJSONObject oDimensions = GetOrCreateJSONObject(oCore, "Dimensions");
2792 132 : oDimensions.Set("_type", "group");
2793 132 : oDimensions.Set("Samples", nRasterXSize);
2794 132 : oDimensions.Set("Lines", nRasterYSize);
2795 132 : oDimensions.Set("Bands", nBands);
2796 :
2797 396 : CPLJSONObject oPixels = GetOrCreateJSONObject(oCore, "Pixels");
2798 132 : oPixels.Set("_type", "group");
2799 132 : const GDALDataType eDT = GetRasterBand(1)->GetRasterDataType();
2800 159 : oPixels.Set("Type", (eDT == GDT_UInt8) ? "UnsignedByte"
2801 44 : : (eDT == GDT_UInt16) ? "UnsignedWord"
2802 17 : : (eDT == GDT_Int16) ? "SignedWord"
2803 : : "Real");
2804 :
2805 132 : oPixels.Set("ByteOrder", "Lsb");
2806 132 : oPixels.Set("Base", GetRasterBand(1)->GetOffset());
2807 132 : oPixels.Set("Multiplier", GetRasterBand(1)->GetScale());
2808 :
2809 132 : const OGRSpatialReference &oSRS = m_oSRS;
2810 :
2811 132 : if (!m_bUseSrcMapping)
2812 : {
2813 130 : oIsisCube.Delete("Mapping");
2814 : }
2815 :
2816 396 : CPLJSONObject oMapping = GetOrCreateJSONObject(oIsisCube, "Mapping");
2817 134 : if (m_bUseSrcMapping && oMapping.IsValid() &&
2818 2 : oMapping.GetType() == CPLJSONObject::Type::Object)
2819 : {
2820 2 : if (!m_osTargetName.empty())
2821 1 : oMapping.Set("TargetName", m_osTargetName);
2822 2 : if (!m_osLatitudeType.empty())
2823 1 : oMapping.Set("LatitudeType", m_osLatitudeType);
2824 2 : if (!m_osLongitudeDirection.empty())
2825 1 : oMapping.Set("LongitudeDirection", m_osLongitudeDirection);
2826 : }
2827 130 : else if (!m_bUseSrcMapping && !m_oSRS.IsEmpty())
2828 : {
2829 62 : oMapping.Add("_type", "group");
2830 :
2831 62 : if (oSRS.IsProjected() || oSRS.IsGeographic())
2832 : {
2833 62 : const char *pszDatum = oSRS.GetAttrValue("DATUM");
2834 124 : CPLString osTargetName(m_osTargetName);
2835 62 : if (osTargetName.empty())
2836 : {
2837 61 : if (pszDatum && STARTS_WITH(pszDatum, "D_"))
2838 : {
2839 24 : osTargetName = pszDatum + 2;
2840 : }
2841 37 : else if (pszDatum)
2842 : {
2843 37 : osTargetName = pszDatum;
2844 : }
2845 : }
2846 62 : if (!osTargetName.empty())
2847 62 : oMapping.Add("TargetName", osTargetName);
2848 :
2849 62 : oMapping.Add("EquatorialRadius/value", oSRS.GetSemiMajor());
2850 62 : oMapping.Add("EquatorialRadius/unit", "meters");
2851 62 : oMapping.Add("PolarRadius/value", oSRS.GetSemiMinor());
2852 62 : oMapping.Add("PolarRadius/unit", "meters");
2853 :
2854 62 : if (!m_osLatitudeType.empty())
2855 1 : oMapping.Add("LatitudeType", m_osLatitudeType);
2856 : else
2857 61 : oMapping.Add("LatitudeType", "Planetocentric");
2858 :
2859 62 : if (!m_osLongitudeDirection.empty())
2860 1 : oMapping.Add("LongitudeDirection", m_osLongitudeDirection);
2861 : else
2862 61 : oMapping.Add("LongitudeDirection", "PositiveEast");
2863 :
2864 62 : double adfX[4] = {0};
2865 62 : double adfY[4] = {0};
2866 62 : bool bLongLatCorners = false;
2867 62 : if (m_bGotTransform)
2868 : {
2869 270 : for (int i = 0; i < 4; i++)
2870 : {
2871 216 : adfX[i] = m_gt.xorig + (i % 2) * nRasterXSize * m_gt.xscale;
2872 216 : adfY[i] = m_gt.yorig + ((i == 0 || i == 3) ? 0 : 1) *
2873 216 : nRasterYSize * m_gt.yscale;
2874 : }
2875 54 : if (oSRS.IsGeographic())
2876 : {
2877 31 : bLongLatCorners = true;
2878 : }
2879 : else
2880 : {
2881 23 : OGRSpatialReference *poSRSLongLat = oSRS.CloneGeogCS();
2882 23 : if (poSRSLongLat)
2883 : {
2884 23 : poSRSLongLat->SetAxisMappingStrategy(
2885 : OAMS_TRADITIONAL_GIS_ORDER);
2886 : OGRCoordinateTransformation *poCT =
2887 23 : OGRCreateCoordinateTransformation(&oSRS,
2888 : poSRSLongLat);
2889 23 : if (poCT)
2890 : {
2891 23 : if (poCT->Transform(4, adfX, adfY))
2892 : {
2893 23 : bLongLatCorners = true;
2894 : }
2895 23 : delete poCT;
2896 : }
2897 23 : delete poSRSLongLat;
2898 : }
2899 : }
2900 : }
2901 62 : if (bLongLatCorners)
2902 : {
2903 270 : for (int i = 0; i < 4; i++)
2904 : {
2905 216 : adfX[i] = FixLong(adfX[i]);
2906 : }
2907 : }
2908 :
2909 62 : if (bLongLatCorners &&
2910 54 : (m_bForce360 || adfX[0] < -180.0 || adfX[3] > 180.0))
2911 : {
2912 1 : oMapping.Add("LongitudeDomain", 360);
2913 : }
2914 : else
2915 : {
2916 61 : oMapping.Add("LongitudeDomain", 180);
2917 : }
2918 :
2919 62 : if (m_bWriteBoundingDegrees && !m_osBoundingDegrees.empty())
2920 : {
2921 : char **papszTokens =
2922 1 : CSLTokenizeString2(m_osBoundingDegrees, ",", 0);
2923 1 : if (CSLCount(papszTokens) == 4)
2924 : {
2925 1 : oMapping.Add("MinimumLatitude", CPLAtof(papszTokens[1]));
2926 1 : oMapping.Add("MinimumLongitude", CPLAtof(papszTokens[0]));
2927 1 : oMapping.Add("MaximumLatitude", CPLAtof(papszTokens[3]));
2928 1 : oMapping.Add("MaximumLongitude", CPLAtof(papszTokens[2]));
2929 : }
2930 1 : CSLDestroy(papszTokens);
2931 : }
2932 61 : else if (m_bWriteBoundingDegrees && bLongLatCorners)
2933 : {
2934 52 : oMapping.Add("MinimumLatitude",
2935 : std::min(std::min(adfY[0], adfY[1]),
2936 52 : std::min(adfY[2], adfY[3])));
2937 52 : oMapping.Add("MinimumLongitude",
2938 : std::min(std::min(adfX[0], adfX[1]),
2939 52 : std::min(adfX[2], adfX[3])));
2940 52 : oMapping.Add("MaximumLatitude",
2941 : std::max(std::max(adfY[0], adfY[1]),
2942 52 : std::max(adfY[2], adfY[3])));
2943 52 : oMapping.Add("MaximumLongitude",
2944 : std::max(std::max(adfX[0], adfX[1]),
2945 52 : std::max(adfX[2], adfX[3])));
2946 : }
2947 :
2948 62 : const char *pszProjection = oSRS.GetAttrValue("PROJECTION");
2949 62 : if (pszProjection == nullptr)
2950 : {
2951 31 : oMapping.Add("ProjectionName", "SimpleCylindrical");
2952 31 : oMapping.Add("CenterLongitude", 0.0);
2953 31 : oMapping.Add("CenterLatitude", 0.0);
2954 31 : oMapping.Add("CenterLatitudeRadius", oSRS.GetSemiMajor());
2955 : }
2956 31 : else if (EQUAL(pszProjection, SRS_PT_EQUIRECTANGULAR))
2957 : {
2958 14 : oMapping.Add("ProjectionName", "Equirectangular");
2959 14 : if (oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0) != 0.0)
2960 : {
2961 1 : CPLError(CE_Warning, CPLE_NotSupported,
2962 : "Ignoring %s. Only 0 value supported",
2963 : SRS_PP_LATITUDE_OF_ORIGIN);
2964 : }
2965 14 : oMapping.Add("CenterLongitude",
2966 : FixLong(oSRS.GetNormProjParm(
2967 : SRS_PP_CENTRAL_MERIDIAN, 0.0)));
2968 : const double dfCenterLat =
2969 14 : oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, 0.0);
2970 14 : oMapping.Add("CenterLatitude", dfCenterLat);
2971 :
2972 : // in radians
2973 14 : const double radLat = dfCenterLat * M_PI / 180;
2974 14 : const double semi_major = oSRS.GetSemiMajor();
2975 14 : const double semi_minor = oSRS.GetSemiMinor();
2976 : const double localRadius =
2977 14 : semi_major * semi_minor /
2978 14 : sqrt(pow(semi_minor * cos(radLat), 2) +
2979 14 : pow(semi_major * sin(radLat), 2));
2980 14 : oMapping.Add("CenterLatitudeRadius", localRadius);
2981 : }
2982 :
2983 17 : else if (EQUAL(pszProjection, SRS_PT_ORTHOGRAPHIC))
2984 : {
2985 1 : oMapping.Add("ProjectionName", "Orthographic");
2986 1 : oMapping.Add("CenterLongitude",
2987 : FixLong(oSRS.GetNormProjParm(
2988 : SRS_PP_CENTRAL_MERIDIAN, 0.0)));
2989 1 : oMapping.Add(
2990 : "CenterLatitude",
2991 : oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0));
2992 : }
2993 :
2994 16 : else if (EQUAL(pszProjection, SRS_PT_SINUSOIDAL))
2995 : {
2996 1 : oMapping.Add("ProjectionName", "Sinusoidal");
2997 1 : oMapping.Add("CenterLongitude",
2998 : FixLong(oSRS.GetNormProjParm(
2999 : SRS_PP_LONGITUDE_OF_CENTER, 0.0)));
3000 : }
3001 :
3002 15 : else if (EQUAL(pszProjection, SRS_PT_MERCATOR_1SP))
3003 : {
3004 1 : oMapping.Add("ProjectionName", "Mercator");
3005 1 : oMapping.Add("CenterLongitude",
3006 : FixLong(oSRS.GetNormProjParm(
3007 : SRS_PP_CENTRAL_MERIDIAN, 0.0)));
3008 1 : oMapping.Add(
3009 : "CenterLatitude",
3010 : oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0));
3011 1 : oMapping.Add("scaleFactor",
3012 : oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0));
3013 : }
3014 :
3015 14 : else if (EQUAL(pszProjection, SRS_PT_POLAR_STEREOGRAPHIC))
3016 : {
3017 1 : oMapping.Add("ProjectionName", "PolarStereographic");
3018 1 : oMapping.Add("CenterLongitude",
3019 : FixLong(oSRS.GetNormProjParm(
3020 : SRS_PP_CENTRAL_MERIDIAN, 0.0)));
3021 1 : oMapping.Add(
3022 : "CenterLatitude",
3023 : oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0));
3024 1 : oMapping.Add("scaleFactor",
3025 : oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0));
3026 : }
3027 :
3028 13 : else if (EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR))
3029 : {
3030 11 : oMapping.Add("ProjectionName", "TransverseMercator");
3031 11 : oMapping.Add("CenterLongitude",
3032 : FixLong(oSRS.GetNormProjParm(
3033 : SRS_PP_CENTRAL_MERIDIAN, 0.0)));
3034 11 : oMapping.Add(
3035 : "CenterLatitude",
3036 : oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0));
3037 11 : oMapping.Add("scaleFactor",
3038 : oSRS.GetNormProjParm(SRS_PP_SCALE_FACTOR, 1.0));
3039 : }
3040 :
3041 2 : else if (EQUAL(pszProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP))
3042 : {
3043 1 : oMapping.Add("ProjectionName", "LambertConformal");
3044 1 : oMapping.Add("CenterLongitude",
3045 : FixLong(oSRS.GetNormProjParm(
3046 : SRS_PP_CENTRAL_MERIDIAN, 0.0)));
3047 1 : oMapping.Add(
3048 : "CenterLatitude",
3049 : oSRS.GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0));
3050 1 : oMapping.Add(
3051 : "FirstStandardParallel",
3052 : oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, 0.0));
3053 1 : oMapping.Add(
3054 : "SecondStandardParallel",
3055 : oSRS.GetNormProjParm(SRS_PP_STANDARD_PARALLEL_2, 0.0));
3056 : }
3057 :
3058 1 : else if (EQUAL(pszProjection,
3059 : "Vertical Perspective")) // PROJ 7 required
3060 : {
3061 0 : oMapping.Add("ProjectionName", "PointPerspective");
3062 0 : oMapping.Add("CenterLongitude",
3063 : FixLong(oSRS.GetNormProjParm(
3064 : "Longitude of topocentric origin", 0.0)));
3065 0 : oMapping.Add("CenterLatitude",
3066 : oSRS.GetNormProjParm(
3067 : "Latitude of topocentric origin", 0.0));
3068 : // ISIS3 value is the distance from center of ellipsoid, in km
3069 0 : oMapping.Add("Distance",
3070 0 : (oSRS.GetNormProjParm("Viewpoint height", 0.0) +
3071 0 : oSRS.GetSemiMajor()) /
3072 : 1000.0);
3073 : }
3074 :
3075 1 : else if (EQUAL(pszProjection, "custom_proj4"))
3076 : {
3077 : const char *pszProj4 =
3078 1 : oSRS.GetExtension("PROJCS", "PROJ4", nullptr);
3079 1 : if (pszProj4 && strstr(pszProj4, "+proj=ob_tran") &&
3080 1 : strstr(pszProj4, "+o_proj=eqc"))
3081 : {
3082 : const auto FetchParam =
3083 3 : [](const char *pszProj4Str, const char *pszKey)
3084 : {
3085 6 : CPLString needle;
3086 3 : needle.Printf("+%s=", pszKey);
3087 : const char *pszVal =
3088 3 : strstr(pszProj4Str, needle.c_str());
3089 3 : if (pszVal)
3090 3 : return CPLAtof(pszVal + needle.size());
3091 0 : return 0.0;
3092 : };
3093 :
3094 1 : double dfLonP = FetchParam(pszProj4, "o_lon_p");
3095 1 : double dfLatP = FetchParam(pszProj4, "o_lat_p");
3096 1 : double dfLon0 = FetchParam(pszProj4, "lon_0");
3097 1 : double dfPoleRotation = -dfLonP;
3098 1 : double dfPoleLatitude = 180 - dfLatP;
3099 1 : double dfPoleLongitude = dfLon0;
3100 1 : oMapping.Add("ProjectionName", "ObliqueCylindrical");
3101 1 : oMapping.Add("PoleLatitude", dfPoleLatitude);
3102 1 : oMapping.Add("PoleLongitude", FixLong(dfPoleLongitude));
3103 1 : oMapping.Add("PoleRotation", dfPoleRotation);
3104 : }
3105 : else
3106 : {
3107 0 : CPLError(CE_Warning, CPLE_NotSupported,
3108 : "Projection %s not supported", pszProjection);
3109 : }
3110 : }
3111 : else
3112 : {
3113 0 : CPLError(CE_Warning, CPLE_NotSupported,
3114 : "Projection %s not supported", pszProjection);
3115 : }
3116 :
3117 62 : if (oMapping["ProjectionName"].IsValid())
3118 : {
3119 62 : if (oSRS.GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0) != 0.0)
3120 : {
3121 9 : CPLError(CE_Warning, CPLE_NotSupported,
3122 : "Ignoring %s. Only 0 value supported",
3123 : SRS_PP_FALSE_EASTING);
3124 : }
3125 62 : if (oSRS.GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0) != 0.0)
3126 : {
3127 1 : CPLError(CE_Warning, CPLE_AppDefined,
3128 : "Ignoring %s. Only 0 value supported",
3129 : SRS_PP_FALSE_NORTHING);
3130 : }
3131 : }
3132 : }
3133 : else
3134 : {
3135 0 : CPLError(CE_Warning, CPLE_NotSupported, "SRS not supported");
3136 : }
3137 : }
3138 :
3139 132 : if (!m_bUseSrcMapping && m_bGotTransform)
3140 : {
3141 54 : oMapping.Add("_type", "group");
3142 :
3143 54 : const double dfDegToMeter = oSRS.GetSemiMajor() * M_PI / 180.0;
3144 54 : if (!m_oSRS.IsEmpty() && oSRS.IsProjected())
3145 : {
3146 23 : const double dfLinearUnits = oSRS.GetLinearUnits();
3147 : // Maybe we should deal differently with non meter units ?
3148 23 : const double dfRes = m_gt.xscale * dfLinearUnits;
3149 23 : const double dfScale = dfDegToMeter / dfRes;
3150 23 : oMapping.Add("UpperLeftCornerX", m_gt.xorig);
3151 23 : oMapping.Add("UpperLeftCornerY", m_gt.yorig);
3152 23 : oMapping.Add("PixelResolution/value", dfRes);
3153 23 : oMapping.Add("PixelResolution/unit", "meters/pixel");
3154 23 : oMapping.Add("Scale/value", dfScale);
3155 23 : oMapping.Add("Scale/unit", "pixels/degree");
3156 : }
3157 31 : else if (!m_oSRS.IsEmpty() && oSRS.IsGeographic())
3158 : {
3159 31 : const double dfScale = 1.0 / m_gt.xscale;
3160 31 : const double dfRes = m_gt.xscale * dfDegToMeter;
3161 31 : oMapping.Add("UpperLeftCornerX", m_gt.xorig * dfDegToMeter);
3162 31 : oMapping.Add("UpperLeftCornerY", m_gt.yorig * dfDegToMeter);
3163 31 : oMapping.Add("PixelResolution/value", dfRes);
3164 31 : oMapping.Add("PixelResolution/unit", "meters/pixel");
3165 31 : oMapping.Add("Scale/value", dfScale);
3166 31 : oMapping.Add("Scale/unit", "pixels/degree");
3167 : }
3168 : else
3169 : {
3170 0 : oMapping.Add("UpperLeftCornerX", m_gt.xorig);
3171 0 : oMapping.Add("UpperLeftCornerY", m_gt.yorig);
3172 0 : oMapping.Add("PixelResolution", m_gt.xscale);
3173 : }
3174 : }
3175 :
3176 396 : CPLJSONObject oLabelLabel = GetOrCreateJSONObject(oLabel, "Label");
3177 132 : oLabelLabel.Set("_type", "object");
3178 132 : oLabelLabel.Set("Bytes", pszLABEL_BYTES_PLACEHOLDER);
3179 :
3180 : // Deal with History object
3181 132 : BuildHistory();
3182 :
3183 132 : oLabel.Delete("History_IsisCube");
3184 132 : if (!m_osHistory.empty())
3185 : {
3186 130 : CPLJSONObject oHistory;
3187 130 : oHistory.Add("_type", "object");
3188 130 : oHistory.Add("_container_name", "History");
3189 130 : oHistory.Add("Name", "IsisCube");
3190 130 : if (m_osExternalFilename.empty())
3191 100 : oHistory.Add("StartByte", pszHISTORY_STARTBYTE_PLACEHOLDER);
3192 : else
3193 30 : oHistory.Add("StartByte", 1);
3194 130 : oHistory.Add("Bytes", static_cast<GIntBig>(m_osHistory.size()));
3195 130 : if (!m_osExternalFilename.empty())
3196 : {
3197 30 : CPLString osFilename(CPLGetBasenameSafe(GetDescription()));
3198 30 : osFilename += ".History.IsisCube";
3199 30 : oHistory.Add("^History", osFilename);
3200 : }
3201 130 : oLabel.Add("History_IsisCube", oHistory);
3202 : }
3203 :
3204 : // Deal with other objects that have StartByte & Bytes
3205 132 : m_aoNonPixelSections.clear();
3206 132 : if (m_oSrcJSonLabel.IsValid())
3207 : {
3208 54 : CPLString osLabelSrcFilename;
3209 81 : CPLJSONObject oFilename = oLabel["_filename"];
3210 27 : if (oFilename.GetType() == CPLJSONObject::Type::String)
3211 : {
3212 22 : osLabelSrcFilename = oFilename.ToString();
3213 : }
3214 :
3215 148 : for (CPLJSONObject &oObj : oLabel.GetChildren())
3216 : {
3217 121 : CPLString osKey = oObj.GetName();
3218 121 : if (osKey == "History_IsisCube")
3219 : {
3220 25 : continue;
3221 : }
3222 :
3223 192 : CPLJSONObject oBytes = oObj.GetObj("Bytes");
3224 109 : if (oBytes.GetType() != CPLJSONObject::Type::Integer ||
3225 13 : oBytes.ToInteger() <= 0)
3226 : {
3227 83 : continue;
3228 : }
3229 :
3230 26 : CPLJSONObject oStartByte = oObj.GetObj("StartByte");
3231 26 : if (oStartByte.GetType() != CPLJSONObject::Type::Integer ||
3232 13 : oStartByte.ToInteger() <= 0)
3233 : {
3234 0 : continue;
3235 : }
3236 :
3237 13 : if (osLabelSrcFilename.empty())
3238 : {
3239 0 : CPLError(CE_Warning, CPLE_AppDefined,
3240 : "Cannot find _filename attribute in "
3241 : "source ISIS3 metadata. Removing object "
3242 : "%s from the label.",
3243 : osKey.c_str());
3244 0 : oLabel.Delete(osKey);
3245 0 : continue;
3246 : }
3247 :
3248 13 : NonPixelSection oSection;
3249 13 : oSection.osSrcFilename = osLabelSrcFilename;
3250 13 : oSection.nSrcOffset =
3251 13 : static_cast<vsi_l_offset>(oObj.GetInteger("StartByte")) - 1U;
3252 13 : oSection.nSize =
3253 13 : static_cast<vsi_l_offset>(oObj.GetInteger("Bytes"));
3254 :
3255 13 : CPLString osName;
3256 26 : CPLJSONObject oName = oObj.GetObj("Name");
3257 13 : if (oName.GetType() == CPLJSONObject::Type::String)
3258 : {
3259 13 : osName = oName.ToString();
3260 : }
3261 :
3262 13 : CPLString osContainerName(osKey);
3263 26 : CPLJSONObject oContainerName = oObj.GetObj("_container_name");
3264 13 : if (oContainerName.GetType() == CPLJSONObject::Type::String)
3265 : {
3266 13 : osContainerName = oContainerName.ToString();
3267 : }
3268 :
3269 13 : const CPLString osKeyFilename("^" + osContainerName);
3270 13 : CPLJSONObject oFilenameCap = oObj.GetObj(osKeyFilename);
3271 13 : if (oFilenameCap.GetType() == CPLJSONObject::Type::String)
3272 : {
3273 : VSIStatBufL sStat;
3274 30 : CPLString osSrcFilename(CPLFormFilenameSafe(
3275 20 : CPLGetPathSafe(osLabelSrcFilename).c_str(),
3276 30 : oFilenameCap.ToString().c_str(), nullptr));
3277 :
3278 10 : if (CPLHasPathTraversal(oFilenameCap.ToString().c_str()))
3279 : {
3280 0 : CPLError(CE_Warning, CPLE_AppDefined,
3281 : "Path traversal detected for %s: %s. Removing "
3282 : "this section from the label",
3283 0 : osKey.c_str(), oFilenameCap.ToString().c_str());
3284 0 : oLabel.Delete(osKey);
3285 0 : continue;
3286 : }
3287 10 : else if (VSIStatL(osSrcFilename, &sStat) == 0)
3288 : {
3289 4 : oSection.osSrcFilename = std::move(osSrcFilename);
3290 : }
3291 : else
3292 : {
3293 6 : CPLError(CE_Warning, CPLE_AppDefined,
3294 : "Object %s points to %s, which does "
3295 : "not exist. Removing this section "
3296 : "from the label",
3297 : osKey.c_str(), osSrcFilename.c_str());
3298 6 : oLabel.Delete(osKey);
3299 6 : continue;
3300 : }
3301 : }
3302 :
3303 7 : if (!m_osExternalFilename.empty())
3304 : {
3305 2 : oObj.Set("StartByte", 1);
3306 : }
3307 : else
3308 : {
3309 10 : CPLString osPlaceHolder;
3310 : osPlaceHolder.Printf(
3311 : "!*^PLACEHOLDER_%d_STARTBYTE^*!",
3312 5 : static_cast<int>(m_aoNonPixelSections.size()) + 1);
3313 5 : oObj.Set("StartByte", osPlaceHolder);
3314 5 : oSection.osPlaceHolder = std::move(osPlaceHolder);
3315 : }
3316 :
3317 7 : if (!m_osExternalFilename.empty())
3318 : {
3319 4 : CPLString osDstFilename(CPLGetBasenameSafe(GetDescription()));
3320 2 : osDstFilename += ".";
3321 2 : osDstFilename += osContainerName;
3322 2 : if (!osName.empty())
3323 : {
3324 2 : osDstFilename += ".";
3325 2 : osDstFilename += osName;
3326 : }
3327 :
3328 4 : oSection.osDstFilename = CPLFormFilenameSafe(
3329 4 : CPLGetPathSafe(GetDescription()).c_str(), osDstFilename,
3330 2 : nullptr);
3331 :
3332 2 : oObj.Set(osKeyFilename, osDstFilename);
3333 : }
3334 : else
3335 : {
3336 5 : oObj.Delete(osKeyFilename);
3337 : }
3338 :
3339 7 : m_aoNonPixelSections.push_back(std::move(oSection));
3340 : }
3341 : }
3342 132 : m_oJSonLabel = std::move(oLabel);
3343 132 : }
3344 :
3345 : /************************************************************************/
3346 : /* BuildHistory() */
3347 : /************************************************************************/
3348 :
3349 132 : void ISIS3Dataset::BuildHistory()
3350 : {
3351 264 : CPLString osHistory;
3352 :
3353 132 : if (m_oSrcJSonLabel.IsValid() && m_bUseSrcHistory)
3354 : {
3355 25 : vsi_l_offset nHistoryOffset = 0;
3356 25 : int nHistorySize = 0;
3357 50 : CPLString osSrcFilename;
3358 :
3359 75 : CPLJSONObject oFilename = m_oSrcJSonLabel["_filename"];
3360 25 : if (oFilename.GetType() == CPLJSONObject::Type::String)
3361 : {
3362 20 : osSrcFilename = oFilename.ToString();
3363 : }
3364 50 : CPLString osHistoryFilename(osSrcFilename);
3365 75 : CPLJSONObject oHistory = m_oSrcJSonLabel["History_IsisCube"];
3366 25 : if (oHistory.GetType() == CPLJSONObject::Type::Object)
3367 : {
3368 33 : CPLJSONObject oHistoryFilename = oHistory["^History"];
3369 11 : if (oHistoryFilename.GetType() == CPLJSONObject::Type::String)
3370 : {
3371 14 : osHistoryFilename = CPLFormFilenameSafe(
3372 14 : CPLGetPathSafe(osSrcFilename).c_str(),
3373 21 : oHistoryFilename.ToString().c_str(), nullptr);
3374 7 : if (CPLHasPathTraversal(oHistoryFilename.ToString().c_str()))
3375 : {
3376 0 : CPLError(CE_Warning, CPLE_AppDefined,
3377 : "Path traversal detected for History: %s. Not "
3378 : "including it in the label",
3379 0 : oHistoryFilename.ToString().c_str());
3380 0 : osHistoryFilename.clear();
3381 : }
3382 : }
3383 :
3384 33 : CPLJSONObject oStartByte = oHistory["StartByte"];
3385 11 : if (oStartByte.GetType() == CPLJSONObject::Type::Integer)
3386 : {
3387 11 : if (oStartByte.ToInteger() > 0)
3388 : {
3389 11 : nHistoryOffset =
3390 11 : static_cast<vsi_l_offset>(oStartByte.ToInteger()) - 1U;
3391 : }
3392 : }
3393 :
3394 33 : CPLJSONObject oBytes = oHistory["Bytes"];
3395 11 : if (oBytes.GetType() == CPLJSONObject::Type::Integer)
3396 : {
3397 11 : nHistorySize = static_cast<int>(oBytes.ToInteger());
3398 : }
3399 : }
3400 :
3401 25 : if (osHistoryFilename.empty())
3402 : {
3403 5 : CPLDebug("ISIS3", "Cannot find filename for source history");
3404 : }
3405 20 : else if (nHistorySize <= 0 || nHistorySize > 1000000)
3406 : {
3407 9 : CPLDebug("ISIS3", "Invalid or missing value for History.Bytes "
3408 : "for source history");
3409 : }
3410 : else
3411 : {
3412 11 : VSILFILE *fpHistory = VSIFOpenL(osHistoryFilename, "rb");
3413 11 : if (fpHistory != nullptr)
3414 : {
3415 6 : VSIFSeekL(fpHistory, nHistoryOffset, SEEK_SET);
3416 6 : osHistory.resize(nHistorySize);
3417 6 : if (VSIFReadL(&osHistory[0], nHistorySize, 1, fpHistory) != 1)
3418 : {
3419 0 : CPLError(CE_Warning, CPLE_FileIO,
3420 : "Cannot read %d bytes at offset " CPL_FRMT_GUIB
3421 : "of %s: history will not be preserved",
3422 : nHistorySize, nHistoryOffset,
3423 : osHistoryFilename.c_str());
3424 0 : osHistory.clear();
3425 : }
3426 6 : VSIFCloseL(fpHistory);
3427 : }
3428 : else
3429 : {
3430 5 : CPLError(CE_Warning, CPLE_FileIO,
3431 : "Cannot open %s: history will not be preserved",
3432 : osHistoryFilename.c_str());
3433 : }
3434 : }
3435 : }
3436 :
3437 132 : if (m_bAddGDALHistory && !m_osGDALHistory.empty())
3438 : {
3439 1 : if (!osHistory.empty())
3440 0 : osHistory += "\n";
3441 1 : osHistory += m_osGDALHistory;
3442 : }
3443 131 : else if (m_bAddGDALHistory)
3444 : {
3445 129 : if (!osHistory.empty())
3446 6 : osHistory += "\n";
3447 :
3448 258 : CPLJSONObject oHistoryObj;
3449 129 : char szFullFilename[2048] = {0};
3450 129 : if (!CPLGetExecPath(szFullFilename, sizeof(szFullFilename) - 1))
3451 0 : strcpy(szFullFilename, "unknown_program");
3452 258 : const CPLString osProgram(CPLGetBasenameSafe(szFullFilename));
3453 258 : const CPLString osPath(CPLGetPathSafe(szFullFilename));
3454 :
3455 258 : CPLJSONObject oObj;
3456 129 : oHistoryObj.Add(osProgram, oObj);
3457 :
3458 129 : oObj.Add("_type", "object");
3459 129 : oObj.Add("GdalVersion", GDALVersionInfo("RELEASE_NAME"));
3460 129 : if (osPath != ".")
3461 129 : oObj.Add("ProgramPath", osPath);
3462 129 : time_t nCurTime = time(nullptr);
3463 129 : if (nCurTime != -1)
3464 : {
3465 : struct tm mytm;
3466 129 : CPLUnixTimeToYMDHMS(nCurTime, &mytm);
3467 129 : oObj.Add("ExecutionDateTime",
3468 : CPLSPrintf("%04d-%02d-%02dT%02d:%02d:%02d",
3469 129 : mytm.tm_year + 1900, mytm.tm_mon + 1,
3470 : mytm.tm_mday, mytm.tm_hour, mytm.tm_min,
3471 : mytm.tm_sec));
3472 : }
3473 129 : char szHostname[256] = {0};
3474 129 : if (gethostname(szHostname, sizeof(szHostname) - 1) == 0)
3475 : {
3476 129 : oObj.Add("HostName", std::string(szHostname));
3477 : }
3478 129 : const char *pszUsername = CPLGetConfigOption("USERNAME", nullptr);
3479 129 : if (pszUsername == nullptr)
3480 129 : pszUsername = CPLGetConfigOption("USER", nullptr);
3481 129 : if (pszUsername != nullptr)
3482 : {
3483 0 : oObj.Add("UserName", pszUsername);
3484 : }
3485 129 : oObj.Add("Description", "GDAL conversion");
3486 :
3487 129 : CPLJSONObject oUserParameters;
3488 129 : oObj.Add("UserParameters", oUserParameters);
3489 :
3490 129 : oUserParameters.Add("_type", "group");
3491 129 : if (!m_osFromFilename.empty())
3492 : {
3493 38 : const CPLString osFromFilename = CPLGetFilename(m_osFromFilename);
3494 38 : oUserParameters.Add("FROM", osFromFilename);
3495 : }
3496 129 : if (nullptr != GetDescription())
3497 : {
3498 129 : const CPLString osToFileName = CPLGetFilename(GetDescription());
3499 129 : oUserParameters.Add("TO", osToFileName);
3500 : }
3501 129 : if (m_bForce360)
3502 1 : oUserParameters.Add("Force_360", "true");
3503 :
3504 129 : osHistory += SerializeAsPDL(oHistoryObj);
3505 : }
3506 :
3507 132 : m_osHistory = std::move(osHistory);
3508 132 : }
3509 :
3510 : /************************************************************************/
3511 : /* WriteLabel() */
3512 : /************************************************************************/
3513 :
3514 131 : void ISIS3Dataset::WriteLabel()
3515 : {
3516 131 : m_bIsLabelWritten = true;
3517 :
3518 131 : if (!m_oJSonLabel.IsValid())
3519 131 : BuildLabel();
3520 :
3521 : // Serialize label
3522 262 : CPLString osLabel(SerializeAsPDL(m_oJSonLabel));
3523 131 : osLabel += "End\n";
3524 131 : if (m_osExternalFilename.empty() && osLabel.size() < 65536)
3525 : {
3526 : // In-line labels have conventionally a minimize size of 65536 bytes
3527 : // See #2741
3528 100 : osLabel.resize(65536);
3529 : }
3530 131 : char *pszLabel = &osLabel[0];
3531 131 : const int nLabelSize = static_cast<int>(osLabel.size());
3532 :
3533 : // Hack back StartByte value
3534 : {
3535 131 : char *pszStartByte = strstr(pszLabel, pszSTARTBYTE_PLACEHOLDER);
3536 131 : if (pszStartByte != nullptr)
3537 : {
3538 100 : const char *pszOffset = CPLSPrintf("%d", 1 + nLabelSize);
3539 100 : memcpy(pszStartByte, pszOffset, strlen(pszOffset));
3540 100 : memset(pszStartByte + strlen(pszOffset), ' ',
3541 100 : strlen(pszSTARTBYTE_PLACEHOLDER) - strlen(pszOffset));
3542 : }
3543 : }
3544 :
3545 : // Hack back Label.Bytes value
3546 : {
3547 131 : char *pszLabelBytes = strstr(pszLabel, pszLABEL_BYTES_PLACEHOLDER);
3548 131 : if (pszLabelBytes != nullptr)
3549 : {
3550 131 : const char *pszBytes = CPLSPrintf("%d", nLabelSize);
3551 131 : memcpy(pszLabelBytes, pszBytes, strlen(pszBytes));
3552 131 : memset(pszLabelBytes + strlen(pszBytes), ' ',
3553 131 : strlen(pszLABEL_BYTES_PLACEHOLDER) - strlen(pszBytes));
3554 : }
3555 : }
3556 :
3557 131 : const GDALDataType eType = GetRasterBand(1)->GetRasterDataType();
3558 131 : const int nDTSize = GDALGetDataTypeSizeBytes(eType);
3559 131 : vsi_l_offset nImagePixels = 0;
3560 131 : if (m_poExternalDS == nullptr)
3561 : {
3562 113 : if (m_bIsTiled)
3563 : {
3564 7 : int nBlockXSize = 1, nBlockYSize = 1;
3565 7 : GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize);
3566 7 : nImagePixels = static_cast<vsi_l_offset>(nBlockXSize) *
3567 14 : nBlockYSize * nBands *
3568 7 : DIV_ROUND_UP(nRasterXSize, nBlockXSize) *
3569 7 : DIV_ROUND_UP(nRasterYSize, nBlockYSize);
3570 : }
3571 : else
3572 : {
3573 106 : nImagePixels =
3574 106 : static_cast<vsi_l_offset>(nRasterXSize) * nRasterYSize * nBands;
3575 : }
3576 : }
3577 :
3578 : // Hack back History.StartBytes value
3579 : char *pszHistoryStartByte =
3580 131 : strstr(pszLabel, pszHISTORY_STARTBYTE_PLACEHOLDER);
3581 :
3582 131 : vsi_l_offset nHistoryOffset = 0;
3583 131 : vsi_l_offset nLastOffset = 0;
3584 131 : if (pszHistoryStartByte != nullptr)
3585 : {
3586 99 : CPLAssert(m_osExternalFilename.empty());
3587 99 : nHistoryOffset = nLabelSize + nImagePixels * nDTSize;
3588 99 : nLastOffset = nHistoryOffset + m_osHistory.size();
3589 : const char *pszStartByte =
3590 99 : CPLSPrintf(CPL_FRMT_GUIB, nHistoryOffset + 1);
3591 99 : CPLAssert(strlen(pszStartByte) <
3592 : strlen(pszHISTORY_STARTBYTE_PLACEHOLDER));
3593 99 : memcpy(pszHistoryStartByte, pszStartByte, strlen(pszStartByte));
3594 99 : memset(pszHistoryStartByte + strlen(pszStartByte), ' ',
3595 99 : strlen(pszHISTORY_STARTBYTE_PLACEHOLDER) - strlen(pszStartByte));
3596 : }
3597 :
3598 : // Replace placeholders in other sections
3599 138 : for (size_t i = 0; i < m_aoNonPixelSections.size(); ++i)
3600 : {
3601 7 : if (!m_aoNonPixelSections[i].osPlaceHolder.empty())
3602 : {
3603 : char *pszPlaceHolder =
3604 5 : strstr(pszLabel, m_aoNonPixelSections[i].osPlaceHolder.c_str());
3605 5 : CPLAssert(pszPlaceHolder != nullptr);
3606 : const char *pszStartByte =
3607 5 : CPLSPrintf(CPL_FRMT_GUIB, nLastOffset + 1);
3608 5 : nLastOffset += m_aoNonPixelSections[i].nSize;
3609 5 : CPLAssert(strlen(pszStartByte) <
3610 : m_aoNonPixelSections[i].osPlaceHolder.size());
3611 :
3612 5 : memcpy(pszPlaceHolder, pszStartByte, strlen(pszStartByte));
3613 5 : memset(pszPlaceHolder + strlen(pszStartByte), ' ',
3614 5 : m_aoNonPixelSections[i].osPlaceHolder.size() -
3615 5 : strlen(pszStartByte));
3616 : }
3617 : }
3618 :
3619 : // Write to final file
3620 131 : VSIFSeekL(m_fpLabel, 0, SEEK_SET);
3621 131 : VSIFWriteL(pszLabel, 1, osLabel.size(), m_fpLabel);
3622 :
3623 131 : if (m_osExternalFilename.empty())
3624 : {
3625 : // Update image offset in bands
3626 100 : if (m_bIsTiled)
3627 : {
3628 15 : for (int i = 0; i < nBands; i++)
3629 : {
3630 : ISISTiledBand *poBand =
3631 9 : reinterpret_cast<ISISTiledBand *>(GetRasterBand(i + 1));
3632 9 : poBand->m_nFirstTileOffset += nLabelSize;
3633 : }
3634 : }
3635 : else
3636 : {
3637 218 : for (int i = 0; i < nBands; i++)
3638 : {
3639 : ISIS3RawRasterBand *poBand =
3640 : reinterpret_cast<ISIS3RawRasterBand *>(
3641 124 : GetRasterBand(i + 1));
3642 124 : poBand->nImgOffset += nLabelSize;
3643 : }
3644 : }
3645 : }
3646 :
3647 131 : if (m_bInitToNodata)
3648 : {
3649 : // Initialize the image to nodata
3650 55 : const double dfNoData = GetRasterBand(1)->GetNoDataValue();
3651 55 : if (dfNoData == 0.0)
3652 : {
3653 43 : VSIFTruncateL(m_fpImage,
3654 43 : VSIFTellL(m_fpImage) + nImagePixels * nDTSize);
3655 : }
3656 12 : else if (nDTSize != 0) // to make Coverity not warn about div by 0
3657 : {
3658 12 : const int nPageSize = 4096; // Must be multiple of 4 since
3659 : // Float32 is the largest type
3660 12 : CPLAssert((nPageSize % nDTSize) == 0);
3661 12 : const int nMaxPerPage = nPageSize / nDTSize;
3662 12 : GByte *pabyTemp = static_cast<GByte *>(CPLMalloc(nPageSize));
3663 12 : GDALCopyWords(&dfNoData, GDT_Float64, 0, pabyTemp, eType, nDTSize,
3664 : nMaxPerPage);
3665 : #ifdef CPL_MSB
3666 : GDALSwapWords(pabyTemp, nDTSize, nMaxPerPage, nDTSize);
3667 : #endif
3668 80 : for (vsi_l_offset i = 0; i < nImagePixels; i += nMaxPerPage)
3669 : {
3670 : int n;
3671 68 : if (i + nMaxPerPage <= nImagePixels)
3672 56 : n = nMaxPerPage;
3673 : else
3674 12 : n = static_cast<int>(nImagePixels - i);
3675 68 : if (VSIFWriteL(pabyTemp, static_cast<size_t>(n) * nDTSize, 1,
3676 68 : m_fpImage) != 1)
3677 : {
3678 0 : CPLError(CE_Failure, CPLE_FileIO,
3679 : "Cannot initialize imagery to null");
3680 0 : break;
3681 : }
3682 : }
3683 :
3684 12 : CPLFree(pabyTemp);
3685 : }
3686 : }
3687 :
3688 : // Write history
3689 131 : if (!m_osHistory.empty())
3690 : {
3691 129 : if (m_osExternalFilename.empty())
3692 : {
3693 99 : VSIFSeekL(m_fpLabel, nHistoryOffset, SEEK_SET);
3694 99 : VSIFWriteL(m_osHistory.c_str(), 1, m_osHistory.size(), m_fpLabel);
3695 : }
3696 : else
3697 : {
3698 60 : CPLString osFilename(CPLGetBasenameSafe(GetDescription()));
3699 30 : osFilename += ".History.IsisCube";
3700 60 : osFilename = CPLFormFilenameSafe(
3701 60 : CPLGetPathSafe(GetDescription()).c_str(), osFilename, nullptr);
3702 30 : VSILFILE *fp = VSIFOpenL(osFilename, "wb");
3703 30 : if (fp)
3704 : {
3705 30 : m_aosAdditionalFiles.AddString(osFilename);
3706 :
3707 30 : VSIFWriteL(m_osHistory.c_str(), 1, m_osHistory.size(), fp);
3708 30 : VSIFCloseL(fp);
3709 : }
3710 : else
3711 : {
3712 0 : CPLError(CE_Warning, CPLE_FileIO, "Cannot write %s",
3713 : osFilename.c_str());
3714 : }
3715 : }
3716 : }
3717 :
3718 : // Write other non pixel sections
3719 138 : for (size_t i = 0; i < m_aoNonPixelSections.size(); ++i)
3720 : {
3721 : VSILFILE *fpSrc =
3722 7 : VSIFOpenL(m_aoNonPixelSections[i].osSrcFilename, "rb");
3723 7 : if (fpSrc == nullptr)
3724 : {
3725 0 : CPLError(CE_Warning, CPLE_FileIO, "Cannot open %s",
3726 0 : m_aoNonPixelSections[i].osSrcFilename.c_str());
3727 0 : continue;
3728 : }
3729 :
3730 7 : VSILFILE *fpDest = m_fpLabel;
3731 7 : if (!m_aoNonPixelSections[i].osDstFilename.empty())
3732 : {
3733 2 : fpDest = VSIFOpenL(m_aoNonPixelSections[i].osDstFilename, "wb");
3734 2 : if (fpDest == nullptr)
3735 : {
3736 0 : CPLError(CE_Warning, CPLE_FileIO, "Cannot create %s",
3737 0 : m_aoNonPixelSections[i].osDstFilename.c_str());
3738 0 : VSIFCloseL(fpSrc);
3739 0 : continue;
3740 : }
3741 :
3742 : m_aosAdditionalFiles.AddString(
3743 2 : m_aoNonPixelSections[i].osDstFilename);
3744 : }
3745 :
3746 7 : VSIFSeekL(fpSrc, m_aoNonPixelSections[i].nSrcOffset, SEEK_SET);
3747 : GByte abyBuffer[4096];
3748 7 : vsi_l_offset nRemaining = m_aoNonPixelSections[i].nSize;
3749 14 : while (nRemaining)
3750 : {
3751 7 : size_t nToRead = 4096;
3752 7 : if (nRemaining < nToRead)
3753 7 : nToRead = static_cast<size_t>(nRemaining);
3754 7 : size_t nRead = VSIFReadL(abyBuffer, 1, nToRead, fpSrc);
3755 7 : if (nRead != nToRead)
3756 : {
3757 0 : CPLError(CE_Warning, CPLE_FileIO,
3758 : "Could not read " CPL_FRMT_GUIB " bytes from %s",
3759 0 : m_aoNonPixelSections[i].nSize,
3760 0 : m_aoNonPixelSections[i].osSrcFilename.c_str());
3761 0 : break;
3762 : }
3763 7 : VSIFWriteL(abyBuffer, 1, nRead, fpDest);
3764 7 : nRemaining -= nRead;
3765 : }
3766 :
3767 7 : VSIFCloseL(fpSrc);
3768 7 : if (fpDest != m_fpLabel)
3769 2 : VSIFCloseL(fpDest);
3770 : }
3771 131 : }
3772 :
3773 : /************************************************************************/
3774 : /* SerializeAsPDL() */
3775 : /************************************************************************/
3776 :
3777 260 : CPLString ISIS3Dataset::SerializeAsPDL(const CPLJSONObject &oObj)
3778 : {
3779 520 : const CPLString osTmpFile(VSIMemGenerateHiddenFilename("isis3_pdl"));
3780 260 : VSILFILE *fpTmp = VSIFOpenL(osTmpFile, "wb+");
3781 260 : SerializeAsPDL(fpTmp, oObj);
3782 260 : VSIFCloseL(fpTmp);
3783 : CPLString osContent(reinterpret_cast<char *>(
3784 260 : VSIGetMemFileBuffer(osTmpFile, nullptr, FALSE)));
3785 260 : VSIUnlink(osTmpFile);
3786 520 : return osContent;
3787 : }
3788 :
3789 : /************************************************************************/
3790 : /* SerializeAsPDL() */
3791 : /************************************************************************/
3792 :
3793 : constexpr size_t WIDTH = 79;
3794 :
3795 1398 : void ISIS3Dataset::SerializeAsPDL(VSILFILE *fp, const CPLJSONObject &oObj,
3796 : int nDepth)
3797 : {
3798 2796 : CPLString osIndentation;
3799 3402 : for (int i = 0; i < nDepth; i++)
3800 2004 : osIndentation += " ";
3801 :
3802 2796 : std::vector<CPLJSONObject> aoChildren = oObj.GetChildren();
3803 1398 : size_t nMaxKeyLength = 0;
3804 2796 : std::vector<std::pair<CPLString, CPLJSONObject>> aoChildren2;
3805 7697 : for (const CPLJSONObject &oChild : aoChildren)
3806 : {
3807 6299 : const CPLString osKey = oChild.GetName();
3808 11460 : if (EQUAL(osKey, "_type") || EQUAL(osKey, "_container_name") ||
3809 11460 : EQUAL(osKey, "_filename") || EQUAL(osKey, "_data"))
3810 : {
3811 1307 : continue;
3812 : }
3813 :
3814 4992 : const auto eType = oChild.GetType();
3815 4992 : if (eType == CPLJSONObject::Type::String ||
3816 2256 : eType == CPLJSONObject::Type::Integer ||
3817 1462 : eType == CPLJSONObject::Type::Double ||
3818 : eType == CPLJSONObject::Type::Array)
3819 : {
3820 3540 : aoChildren2.emplace_back(osKey, oChild);
3821 3540 : if (osKey.size() > nMaxKeyLength)
3822 : {
3823 1680 : nMaxKeyLength = osKey.size();
3824 : }
3825 : }
3826 1452 : else if (eType == CPLJSONObject::Type::Object)
3827 : {
3828 4356 : CPLJSONObject oValue = oChild.GetObj("value");
3829 4356 : CPLJSONObject oUnit = oChild.GetObj("unit");
3830 1695 : if (oValue.IsValid() &&
3831 243 : oUnit.GetType() == CPLJSONObject::Type::String)
3832 : {
3833 243 : aoChildren2.emplace_back(osKey, oChild);
3834 243 : if (osKey.size() > nMaxKeyLength)
3835 : {
3836 64 : nMaxKeyLength = osKey.size();
3837 : }
3838 : }
3839 1209 : else if (oChild.GetObj("values").GetType() ==
3840 : CPLJSONObject::Type::Array)
3841 : {
3842 4 : if (osKey.size() > nMaxKeyLength)
3843 : {
3844 1 : nMaxKeyLength = osKey.size();
3845 : }
3846 13 : for (const auto &oSubChild : oChild.GetObj("values").ToArray())
3847 : {
3848 9 : aoChildren2.emplace_back(osKey, oSubChild);
3849 : }
3850 : }
3851 : else
3852 : {
3853 1205 : aoChildren2.emplace_back(osKey, oChild);
3854 : }
3855 : }
3856 : else
3857 : {
3858 0 : aoChildren2.emplace_back(osKey, oChild);
3859 : }
3860 : }
3861 :
3862 6395 : for (const auto &[osKey, oChild] : aoChildren2)
3863 : {
3864 4997 : if (STARTS_WITH(osKey, "_comment"))
3865 : {
3866 1 : if (oChild.GetType() == CPLJSONObject::Type::String)
3867 : {
3868 1 : VSIFPrintfL(fp, "#%s\n", oChild.ToString().c_str());
3869 : }
3870 1 : continue;
3871 : }
3872 9992 : CPLString osPadding;
3873 4996 : size_t nLen = osKey.size();
3874 4996 : if (nLen < nMaxKeyLength)
3875 : {
3876 3047 : osPadding.append(nMaxKeyLength - nLen, ' ');
3877 : }
3878 :
3879 4996 : const auto eType = oChild.GetType();
3880 4996 : if (eType == CPLJSONObject::Type::Object)
3881 : {
3882 4350 : CPLJSONObject oType = oChild.GetObj("_type");
3883 4350 : CPLJSONObject oContainerName = oChild.GetObj("_container_name");
3884 2900 : CPLString osContainerName = osKey;
3885 1450 : if (oContainerName.GetType() == CPLJSONObject::Type::String)
3886 : {
3887 140 : osContainerName = oContainerName.ToString();
3888 : }
3889 1450 : if (oType.GetType() == CPLJSONObject::Type::String)
3890 : {
3891 3414 : const CPLString osType = oType.ToString();
3892 1138 : if (EQUAL(osType, "Object"))
3893 : {
3894 665 : if (nDepth == 0 && VSIFTellL(fp) != 0)
3895 274 : VSIFPrintfL(fp, "\n");
3896 665 : VSIFPrintfL(fp, "%sObject = %s\n", osIndentation.c_str(),
3897 : osContainerName.c_str());
3898 665 : SerializeAsPDL(fp, oChild, nDepth + 1);
3899 665 : VSIFPrintfL(fp, "%sEnd_Object\n", osIndentation.c_str());
3900 : }
3901 473 : else if (EQUAL(osType, "Group"))
3902 : {
3903 473 : VSIFPrintfL(fp, "\n");
3904 473 : VSIFPrintfL(fp, "%sGroup = %s\n", osIndentation.c_str(),
3905 : osContainerName.c_str());
3906 473 : SerializeAsPDL(fp, oChild, nDepth + 1);
3907 473 : VSIFPrintfL(fp, "%sEnd_Group\n", osIndentation.c_str());
3908 : }
3909 : }
3910 : else
3911 : {
3912 936 : CPLJSONObject oValue = oChild.GetObj("value");
3913 936 : CPLJSONObject oUnit = oChild.GetObj("unit");
3914 557 : if (oValue.IsValid() &&
3915 245 : oUnit.GetType() == CPLJSONObject::Type::String)
3916 : {
3917 735 : const CPLString osUnit = oUnit.ToString();
3918 245 : const auto eValueType = oValue.GetType();
3919 245 : if (eValueType == CPLJSONObject::Type::Integer)
3920 : {
3921 3 : VSIFPrintfL(fp, "%s%s%s = %d <%s>\n",
3922 : osIndentation.c_str(), osKey.c_str(),
3923 : osPadding.c_str(), oValue.ToInteger(),
3924 : osUnit.c_str());
3925 : }
3926 242 : else if (eValueType == CPLJSONObject::Type::Double)
3927 : {
3928 241 : const double dfVal = oValue.ToDouble();
3929 241 : if (dfVal >= INT_MIN && dfVal <= INT_MAX &&
3930 241 : static_cast<int>(dfVal) == dfVal)
3931 : {
3932 90 : VSIFPrintfL(fp, "%s%s%s = %d.0 <%s>\n",
3933 : osIndentation.c_str(), osKey.c_str(),
3934 : osPadding.c_str(),
3935 : static_cast<int>(dfVal),
3936 : osUnit.c_str());
3937 : }
3938 : else
3939 : {
3940 151 : VSIFPrintfL(fp, "%s%s%s = %.17g <%s>\n",
3941 : osIndentation.c_str(), osKey.c_str(),
3942 : osPadding.c_str(), dfVal,
3943 : osUnit.c_str());
3944 : }
3945 : }
3946 : }
3947 : }
3948 : }
3949 3546 : else if (eType == CPLJSONObject::Type::String)
3950 : {
3951 6141 : CPLString osVal = oChild.ToString();
3952 2047 : const char *pszVal = osVal.c_str();
3953 2047 : if (pszVal[0] == '\0' || strchr(pszVal, ' ') ||
3954 1909 : strstr(pszVal, "\\n") || strstr(pszVal, "\\r"))
3955 : {
3956 138 : osVal.replaceAll("\\n", "\n");
3957 138 : osVal.replaceAll("\\r", "\r");
3958 138 : VSIFPrintfL(fp, "%s%s%s = \"%s\"\n", osIndentation.c_str(),
3959 : osKey.c_str(), osPadding.c_str(), osVal.c_str());
3960 : }
3961 : else
3962 : {
3963 1909 : if (osIndentation.size() + osKey.size() + osPadding.size() +
3964 1909 : strlen(" = ") + strlen(pszVal) >
3965 1910 : WIDTH &&
3966 1 : osIndentation.size() + osKey.size() + osPadding.size() +
3967 : strlen(" = ") <
3968 : WIDTH)
3969 : {
3970 1 : size_t nFirstPos = osIndentation.size() + osKey.size() +
3971 1 : osPadding.size() + strlen(" = ");
3972 1 : VSIFPrintfL(fp, "%s%s%s = ", osIndentation.c_str(),
3973 : osKey.c_str(), osPadding.c_str());
3974 1 : size_t nCurPos = nFirstPos;
3975 96 : for (int j = 0; pszVal[j] != '\0'; j++)
3976 : {
3977 95 : nCurPos++;
3978 95 : if (nCurPos == WIDTH && pszVal[j + 1] != '\0')
3979 : {
3980 1 : VSIFPrintfL(fp, "-\n");
3981 15 : for (size_t k = 0; k < nFirstPos; k++)
3982 : {
3983 14 : const char chSpace = ' ';
3984 14 : VSIFWriteL(&chSpace, 1, 1, fp);
3985 : }
3986 1 : nCurPos = nFirstPos + 1;
3987 : }
3988 95 : VSIFWriteL(&pszVal[j], 1, 1, fp);
3989 : }
3990 1 : VSIFPrintfL(fp, "\n");
3991 : }
3992 : else
3993 : {
3994 1908 : VSIFPrintfL(fp, "%s%s%s = %s\n", osIndentation.c_str(),
3995 : osKey.c_str(), osPadding.c_str(), pszVal);
3996 : }
3997 : }
3998 : }
3999 1499 : else if (eType == CPLJSONObject::Type::Integer)
4000 : {
4001 693 : const int nVal = oChild.ToInteger();
4002 693 : VSIFPrintfL(fp, "%s%s%s = %d\n", osIndentation.c_str(),
4003 : osKey.c_str(), osPadding.c_str(), nVal);
4004 : }
4005 806 : else if (eType == CPLJSONObject::Type::Double)
4006 : {
4007 794 : const double dfVal = oChild.ToDouble();
4008 794 : if (dfVal >= INT_MIN && dfVal <= INT_MAX &&
4009 794 : static_cast<int>(dfVal) == dfVal)
4010 : {
4011 550 : VSIFPrintfL(fp, "%s%s%s = %d.0\n", osIndentation.c_str(),
4012 : osKey.c_str(), osPadding.c_str(),
4013 : static_cast<int>(dfVal));
4014 : }
4015 : else
4016 : {
4017 244 : VSIFPrintfL(fp, "%s%s%s = %.17g\n", osIndentation.c_str(),
4018 : osKey.c_str(), osPadding.c_str(), dfVal);
4019 : }
4020 : }
4021 12 : else if (eType == CPLJSONObject::Type::Array)
4022 : {
4023 24 : CPLJSONArray oArrayItem(oChild);
4024 12 : const int nLength = oArrayItem.Size();
4025 12 : size_t nFirstPos = osIndentation.size() + osKey.size() +
4026 12 : osPadding.size() + strlen(" = (");
4027 12 : VSIFPrintfL(fp, "%s%s%s = (", osIndentation.c_str(), osKey.c_str(),
4028 : osPadding.c_str());
4029 12 : size_t nCurPos = nFirstPos;
4030 :
4031 57 : for (int idx = 0; idx < nLength; idx++)
4032 : {
4033 90 : CPLJSONObject oItem = oArrayItem[idx];
4034 45 : const auto eArrayItemType = oItem.GetType();
4035 :
4036 : const auto outputArrayVal =
4037 244 : [fp, idx, nFirstPos, &nCurPos](const std::string &osVal)
4038 : {
4039 31 : const size_t nValLen = osVal.size();
4040 31 : if (nFirstPos < WIDTH && idx > 0 &&
4041 23 : nCurPos + nValLen > WIDTH)
4042 : {
4043 2 : VSIFPrintfL(fp, "\n");
4044 32 : for (size_t j = 0; j < nFirstPos; j++)
4045 : {
4046 30 : constexpr char chSpace = ' ';
4047 30 : VSIFWriteL(&chSpace, 1, 1, fp);
4048 : }
4049 2 : nCurPos = nFirstPos;
4050 : }
4051 31 : VSIFPrintfL(fp, "%s", osVal.c_str());
4052 31 : nCurPos += nValLen;
4053 31 : };
4054 :
4055 45 : if (eArrayItemType == CPLJSONObject::Type::Object)
4056 : {
4057 9 : const auto oValue = oItem["value"];
4058 9 : const auto oUnit = oItem["unit"];
4059 6 : if (oValue.IsValid() && oUnit.IsValid() &&
4060 3 : (oValue.GetType() == CPLJSONObject::Type::Integer ||
4061 1 : oValue.GetType() == CPLJSONObject::Type::Double))
4062 : {
4063 3 : if (oValue.GetType() == CPLJSONObject::Type::Integer)
4064 : {
4065 2 : const int nVal = oValue.ToInteger();
4066 6 : outputArrayVal(CPLSPrintf(
4067 4 : "%d <%s>", nVal, oUnit.ToString().c_str()));
4068 : }
4069 : else
4070 : {
4071 1 : const double dfVal = oValue.ToDouble();
4072 1 : if (dfVal >= INT_MIN && dfVal <= INT_MAX &&
4073 1 : static_cast<int>(dfVal) == dfVal)
4074 : {
4075 0 : outputArrayVal(CPLSPrintf(
4076 : "%d.0 <%s>", static_cast<int>(dfVal),
4077 0 : oUnit.ToString().c_str()));
4078 : }
4079 : else
4080 : {
4081 3 : outputArrayVal(
4082 : CPLSPrintf("%.17g <%s>", dfVal,
4083 2 : oUnit.ToString().c_str()));
4084 : }
4085 : }
4086 : }
4087 : else
4088 : {
4089 0 : CPLError(CE_Warning, CPLE_AppDefined,
4090 : "Invalid JSON object");
4091 : }
4092 : }
4093 42 : else if (eArrayItemType == CPLJSONObject::Type::String)
4094 : {
4095 42 : CPLString osVal = oItem.ToString();
4096 14 : const char *pszVal = osVal.c_str();
4097 14 : if (pszVal[0] == '\0' || strchr(pszVal, ' ') ||
4098 7 : strstr(pszVal, "\\n") || strstr(pszVal, "\\r"))
4099 : {
4100 7 : osVal.replaceAll("\\n", "\n");
4101 7 : osVal.replaceAll("\\r", "\r");
4102 7 : VSIFPrintfL(fp, "\"%s\"", osVal.c_str());
4103 : }
4104 7 : else if (nFirstPos < WIDTH &&
4105 7 : nCurPos + strlen(pszVal) > WIDTH)
4106 : {
4107 1 : if (idx > 0)
4108 : {
4109 1 : VSIFPrintfL(fp, "\n");
4110 16 : for (size_t j = 0; j < nFirstPos; j++)
4111 : {
4112 15 : const char chSpace = ' ';
4113 15 : VSIFWriteL(&chSpace, 1, 1, fp);
4114 : }
4115 1 : nCurPos = nFirstPos;
4116 : }
4117 :
4118 102 : for (int j = 0; pszVal[j] != '\0'; j++)
4119 : {
4120 101 : nCurPos++;
4121 101 : if (nCurPos == WIDTH && pszVal[j + 1] != '\0')
4122 : {
4123 1 : VSIFPrintfL(fp, "-\n");
4124 16 : for (size_t k = 0; k < nFirstPos; k++)
4125 : {
4126 15 : const char chSpace = ' ';
4127 15 : VSIFWriteL(&chSpace, 1, 1, fp);
4128 : }
4129 1 : nCurPos = nFirstPos + 1;
4130 : }
4131 101 : VSIFWriteL(&pszVal[j], 1, 1, fp);
4132 1 : }
4133 : }
4134 : else
4135 : {
4136 6 : VSIFPrintfL(fp, "%s", pszVal);
4137 6 : nCurPos += strlen(pszVal);
4138 : }
4139 : }
4140 28 : else if (eArrayItemType == CPLJSONObject::Type::Integer)
4141 : {
4142 19 : const int nVal = oItem.ToInteger();
4143 19 : outputArrayVal(CPLSPrintf("%d", nVal));
4144 : }
4145 9 : else if (eArrayItemType == CPLJSONObject::Type::Double)
4146 : {
4147 9 : const double dfVal = oItem.ToDouble();
4148 18 : CPLString osVal;
4149 9 : if (dfVal >= INT_MIN && dfVal <= INT_MAX &&
4150 9 : static_cast<int>(dfVal) == dfVal)
4151 : {
4152 8 : osVal = CPLSPrintf("%d.0", static_cast<int>(dfVal));
4153 : }
4154 : else
4155 : {
4156 1 : osVal = CPLSPrintf("%.17g", dfVal);
4157 : }
4158 9 : outputArrayVal(osVal);
4159 : }
4160 45 : if (idx < nLength - 1)
4161 : {
4162 33 : VSIFPrintfL(fp, ", ");
4163 33 : nCurPos += 2;
4164 : }
4165 : }
4166 12 : VSIFPrintfL(fp, ")\n");
4167 : }
4168 : }
4169 1398 : }
4170 :
4171 : /************************************************************************/
4172 : /* Create() */
4173 : /************************************************************************/
4174 :
4175 165 : GDALDataset *ISIS3Dataset::Create(const char *pszFilename, int nXSize,
4176 : int nYSize, int nBandsIn, GDALDataType eType,
4177 : CSLConstList papszOptions)
4178 : {
4179 165 : if (eType != GDT_UInt8 && eType != GDT_UInt16 && eType != GDT_Int16 &&
4180 : eType != GDT_Float32)
4181 : {
4182 27 : CPLError(CE_Failure, CPLE_NotSupported, "Unsupported data type");
4183 27 : return nullptr;
4184 : }
4185 138 : if (nBandsIn == 0 || nBandsIn > 32767)
4186 : {
4187 1 : CPLError(CE_Failure, CPLE_NotSupported, "Unsupported band count");
4188 1 : return nullptr;
4189 : }
4190 :
4191 : const char *pszDataLocation =
4192 137 : CSLFetchNameValueDef(papszOptions, "DATA_LOCATION", "LABEL");
4193 137 : const bool bIsTiled = CPLFetchBool(papszOptions, "TILED", false);
4194 : const int nBlockXSize = std::max(
4195 137 : 1, atoi(CSLFetchNameValueDef(papszOptions, "BLOCKXSIZE", "256")));
4196 : const int nBlockYSize = std::max(
4197 137 : 1, atoi(CSLFetchNameValueDef(papszOptions, "BLOCKYSIZE", "256")));
4198 171 : if (!EQUAL(pszDataLocation, "LABEL") &&
4199 171 : !EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "LBL"))
4200 : {
4201 1 : CPLError(CE_Failure, CPLE_NotSupported,
4202 : "For DATA_LOCATION=%s, "
4203 : "the main filename should have a .lbl extension",
4204 : pszDataLocation);
4205 1 : return nullptr;
4206 : }
4207 :
4208 : const char *pszPermission =
4209 136 : VSISupportsRandomWrite(pszFilename, true) ? "wb+" : "wb";
4210 :
4211 136 : VSILFILE *fp = VSIFOpenExL(pszFilename, pszPermission, true);
4212 136 : if (fp == nullptr)
4213 : {
4214 3 : CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s: %s", pszFilename,
4215 : VSIGetLastErrorMsg());
4216 3 : return nullptr;
4217 : }
4218 133 : VSILFILE *fpImage = nullptr;
4219 266 : std::string osExternalFilename;
4220 133 : GDALDataset *poExternalDS = nullptr;
4221 133 : bool bGeoTIFFAsRegularExternal = false;
4222 133 : if (EQUAL(pszDataLocation, "EXTERNAL"))
4223 : {
4224 : osExternalFilename = CSLFetchNameValueDef(
4225 : papszOptions, "EXTERNAL_FILENAME",
4226 14 : CPLResetExtensionSafe(pszFilename, "cub").c_str());
4227 14 : fpImage = VSIFOpenExL(osExternalFilename.c_str(), pszPermission, true);
4228 14 : if (fpImage == nullptr)
4229 : {
4230 1 : CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s: %s",
4231 : osExternalFilename.c_str(), VSIGetLastErrorMsg());
4232 1 : VSIFCloseL(fp);
4233 1 : return nullptr;
4234 : }
4235 : }
4236 119 : else if (EQUAL(pszDataLocation, "GEOTIFF"))
4237 : {
4238 : osExternalFilename = CSLFetchNameValueDef(
4239 : papszOptions, "EXTERNAL_FILENAME",
4240 19 : CPLResetExtensionSafe(pszFilename, "tif").c_str());
4241 : GDALDriver *poDrv =
4242 19 : static_cast<GDALDriver *>(GDALGetDriverByName("GTiff"));
4243 19 : if (poDrv == nullptr)
4244 : {
4245 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find GTiff driver");
4246 0 : VSIFCloseL(fp);
4247 0 : return nullptr;
4248 : }
4249 19 : char **papszGTiffOptions = nullptr;
4250 : papszGTiffOptions =
4251 19 : CSLSetNameValue(papszGTiffOptions, "ENDIANNESS", "LITTLE");
4252 19 : if (bIsTiled)
4253 : {
4254 : papszGTiffOptions =
4255 3 : CSLSetNameValue(papszGTiffOptions, "TILED", "YES");
4256 3 : papszGTiffOptions = CSLSetNameValue(papszGTiffOptions, "BLOCKXSIZE",
4257 : CPLSPrintf("%d", nBlockXSize));
4258 3 : papszGTiffOptions = CSLSetNameValue(papszGTiffOptions, "BLOCKYSIZE",
4259 : CPLSPrintf("%d", nBlockYSize));
4260 : }
4261 : const char *pszGTiffOptions =
4262 19 : CSLFetchNameValueDef(papszOptions, "GEOTIFF_OPTIONS", "");
4263 19 : char **papszTokens = CSLTokenizeString2(pszGTiffOptions, ",", 0);
4264 28 : for (int i = 0; papszTokens[i] != nullptr; i++)
4265 : {
4266 9 : papszGTiffOptions = CSLAddString(papszGTiffOptions, papszTokens[i]);
4267 : }
4268 19 : CSLDestroy(papszTokens);
4269 :
4270 : // If the user didn't specify any compression and
4271 : // GEOTIFF_AS_REGULAR_EXTERNAL is set (or unspecified), then the
4272 : // GeoTIFF file can be seen as a regular external raw file, provided
4273 : // we make some provision on its organization.
4274 29 : if (CSLFetchNameValue(papszGTiffOptions, "COMPRESS") == nullptr &&
4275 10 : CPLFetchBool(papszOptions, "GEOTIFF_AS_REGULAR_EXTERNAL", true))
4276 : {
4277 9 : bGeoTIFFAsRegularExternal = true;
4278 : papszGTiffOptions =
4279 9 : CSLSetNameValue(papszGTiffOptions, GDALMD_INTERLEAVE, "BAND");
4280 : // Will make sure that our blocks at nodata are not optimized
4281 : // away but indeed well written
4282 9 : papszGTiffOptions = CSLSetNameValue(
4283 : papszGTiffOptions, "@WRITE_EMPTY_TILES_SYNCHRONOUSLY", "YES");
4284 9 : if (!bIsTiled && nBandsIn > 1)
4285 : {
4286 : papszGTiffOptions =
4287 1 : CSLSetNameValue(papszGTiffOptions, "BLOCKYSIZE", "1");
4288 : }
4289 : }
4290 :
4291 19 : poExternalDS = poDrv->Create(osExternalFilename.c_str(), nXSize, nYSize,
4292 : nBandsIn, eType, papszGTiffOptions);
4293 19 : CSLDestroy(papszGTiffOptions);
4294 19 : if (poExternalDS == nullptr)
4295 : {
4296 1 : CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s",
4297 : osExternalFilename.c_str());
4298 1 : VSIFCloseL(fp);
4299 1 : return nullptr;
4300 : }
4301 : }
4302 :
4303 131 : ISIS3Dataset *poDS = new ISIS3Dataset();
4304 131 : poDS->SetDescription(pszFilename);
4305 131 : poDS->eAccess = GA_Update;
4306 131 : poDS->nRasterXSize = nXSize;
4307 131 : poDS->nRasterYSize = nYSize;
4308 131 : poDS->m_osExternalFilename = std::move(osExternalFilename);
4309 131 : poDS->m_poExternalDS = poExternalDS;
4310 131 : poDS->m_bGeoTIFFAsRegularExternal = bGeoTIFFAsRegularExternal;
4311 131 : if (bGeoTIFFAsRegularExternal)
4312 8 : poDS->m_bGeoTIFFInitDone = false;
4313 131 : poDS->m_fpLabel = fp;
4314 131 : poDS->m_fpImage = fpImage ? fpImage : fp;
4315 131 : poDS->m_bIsLabelWritten = false;
4316 131 : poDS->m_bIsTiled = bIsTiled;
4317 131 : poDS->m_bInitToNodata = (poDS->m_poExternalDS == nullptr);
4318 131 : poDS->m_osComment = CSLFetchNameValueDef(papszOptions, "COMMENT", "");
4319 : poDS->m_osLatitudeType =
4320 131 : CSLFetchNameValueDef(papszOptions, "LATITUDE_TYPE", "");
4321 : poDS->m_osLongitudeDirection =
4322 131 : CSLFetchNameValueDef(papszOptions, "LONGITUDE_DIRECTION", "");
4323 : poDS->m_osTargetName =
4324 131 : CSLFetchNameValueDef(papszOptions, "TARGET_NAME", "");
4325 131 : poDS->m_bForce360 = CPLFetchBool(papszOptions, "FORCE_360", false);
4326 131 : poDS->m_bWriteBoundingDegrees =
4327 131 : CPLFetchBool(papszOptions, "WRITE_BOUNDING_DEGREES", true);
4328 : poDS->m_osBoundingDegrees =
4329 131 : CSLFetchNameValueDef(papszOptions, "BOUNDING_DEGREES", "");
4330 131 : poDS->m_bUseSrcLabel = CPLFetchBool(papszOptions, "USE_SRC_LABEL", true);
4331 131 : poDS->m_bUseSrcMapping =
4332 131 : CPLFetchBool(papszOptions, "USE_SRC_MAPPING", false);
4333 131 : poDS->m_bUseSrcHistory =
4334 131 : CPLFetchBool(papszOptions, "USE_SRC_HISTORY", true);
4335 131 : poDS->m_bAddGDALHistory =
4336 131 : CPLFetchBool(papszOptions, "ADD_GDAL_HISTORY", true);
4337 131 : if (poDS->m_bAddGDALHistory)
4338 : {
4339 : poDS->m_osGDALHistory =
4340 129 : CSLFetchNameValueDef(papszOptions, "GDAL_HISTORY", "");
4341 : }
4342 131 : const double dfNoData = (eType == GDT_UInt8) ? ISIS3_NULL1
4343 : : (eType == GDT_UInt16) ? ISIS3_NULLU2
4344 : : (eType == GDT_Int16)
4345 : ? ISIS3_NULL2
4346 : :
4347 : /*(eType == GDT_Float32) ?*/ ISIS3_NULL4;
4348 :
4349 303 : for (int i = 0; i < nBandsIn; i++)
4350 : {
4351 172 : GDALRasterBand *poBand = nullptr;
4352 :
4353 172 : if (poDS->m_poExternalDS != nullptr)
4354 : {
4355 : ISIS3WrapperRasterBand *poISISBand = new ISIS3WrapperRasterBand(
4356 24 : poDS->m_poExternalDS->GetRasterBand(i + 1));
4357 24 : poBand = poISISBand;
4358 : }
4359 148 : else if (bIsTiled)
4360 : {
4361 : ISISTiledBand *poISISBand = new ISISTiledBand(
4362 : poDS, poDS->m_fpImage, i + 1, eType, nBlockXSize, nBlockYSize,
4363 : 0, // nSkipBytes, to be hacked
4364 : // afterwards for in-label imagery
4365 10 : 0, 0, CPL_IS_LSB);
4366 :
4367 10 : poBand = poISISBand;
4368 : }
4369 : else
4370 : {
4371 138 : const int nPixelOffset = GDALGetDataTypeSizeBytes(eType);
4372 138 : const int nLineOffset = nPixelOffset * nXSize;
4373 138 : const vsi_l_offset nBandOffset =
4374 138 : static_cast<vsi_l_offset>(nLineOffset) * nYSize;
4375 : ISIS3RawRasterBand *poISISBand = new ISIS3RawRasterBand(
4376 : poDS, i + 1, poDS->m_fpImage,
4377 138 : nBandOffset * i, // nImgOffset, to be
4378 : // hacked afterwards for in-label imagery
4379 138 : nPixelOffset, nLineOffset, eType, CPL_IS_LSB);
4380 :
4381 138 : poBand = poISISBand;
4382 : }
4383 172 : poDS->SetBand(i + 1, poBand);
4384 172 : poBand->SetNoDataValue(dfNoData);
4385 : }
4386 :
4387 131 : return poDS;
4388 : }
4389 :
4390 : /************************************************************************/
4391 : /* GetUnderlyingDataset() */
4392 : /************************************************************************/
4393 :
4394 79 : static GDALDataset *GetUnderlyingDataset(GDALDataset *poSrcDS)
4395 : {
4396 158 : if (poSrcDS->GetDriver() != nullptr &&
4397 79 : poSrcDS->GetDriver() == GDALGetDriverByName("VRT"))
4398 : {
4399 2 : VRTDataset *poVRTDS = cpl::down_cast<VRTDataset *>(poSrcDS);
4400 2 : poSrcDS = poVRTDS->GetSingleSimpleSource();
4401 : }
4402 :
4403 79 : return poSrcDS;
4404 : }
4405 :
4406 : /************************************************************************/
4407 : /* CreateCopy() */
4408 : /************************************************************************/
4409 :
4410 79 : GDALDataset *ISIS3Dataset::CreateCopy(const char *pszFilename,
4411 : GDALDataset *poSrcDS, int /*bStrict*/,
4412 : CSLConstList papszOptions,
4413 : GDALProgressFunc pfnProgress,
4414 : void *pProgressData)
4415 : {
4416 : const char *pszDataLocation =
4417 79 : CSLFetchNameValueDef(papszOptions, "DATA_LOCATION", "LABEL");
4418 79 : GDALDataset *poSrcUnderlyingDS = GetUnderlyingDataset(poSrcDS);
4419 79 : if (poSrcUnderlyingDS == nullptr)
4420 2 : poSrcUnderlyingDS = poSrcDS;
4421 89 : if (EQUAL(pszDataLocation, "GEOTIFF") &&
4422 10 : strcmp(poSrcUnderlyingDS->GetDescription(),
4423 : CSLFetchNameValueDef(
4424 : papszOptions, "EXTERNAL_FILENAME",
4425 89 : CPLResetExtensionSafe(pszFilename, "tif").c_str())) == 0)
4426 : {
4427 1 : CPLError(CE_Failure, CPLE_NotSupported,
4428 : "Output file has same name as input file");
4429 1 : return nullptr;
4430 : }
4431 78 : if (poSrcDS->GetRasterCount() == 0)
4432 : {
4433 1 : CPLError(CE_Failure, CPLE_NotSupported, "Unsupported band count");
4434 1 : return nullptr;
4435 : }
4436 :
4437 77 : const int nXSize = poSrcDS->GetRasterXSize();
4438 77 : const int nYSize = poSrcDS->GetRasterYSize();
4439 77 : const int nBands = poSrcDS->GetRasterCount();
4440 77 : GDALDataType eType = poSrcDS->GetRasterBand(1)->GetRasterDataType();
4441 : ISIS3Dataset *poDS = reinterpret_cast<ISIS3Dataset *>(
4442 77 : Create(pszFilename, nXSize, nYSize, nBands, eType, papszOptions));
4443 77 : if (poDS == nullptr)
4444 10 : return nullptr;
4445 67 : poDS->m_osFromFilename = poSrcUnderlyingDS->GetDescription();
4446 :
4447 67 : GDALGeoTransform gt;
4448 67 : if (poSrcDS->GetGeoTransform(gt) == CE_None && gt != GDALGeoTransform())
4449 : {
4450 40 : poDS->SetGeoTransform(gt);
4451 : }
4452 :
4453 67 : auto poSrcSRS = poSrcDS->GetSpatialRef();
4454 67 : if (poSrcSRS)
4455 : {
4456 40 : poDS->SetSpatialRef(poSrcSRS);
4457 : }
4458 :
4459 159 : for (int i = 1; i <= nBands; i++)
4460 : {
4461 92 : const double dfOffset = poSrcDS->GetRasterBand(i)->GetOffset();
4462 92 : if (dfOffset != 0.0)
4463 1 : poDS->GetRasterBand(i)->SetOffset(dfOffset);
4464 :
4465 92 : const double dfScale = poSrcDS->GetRasterBand(i)->GetScale();
4466 92 : if (dfScale != 1.0)
4467 1 : poDS->GetRasterBand(i)->SetScale(dfScale);
4468 : }
4469 :
4470 : // Do we need to remap nodata ?
4471 67 : int bHasNoData = FALSE;
4472 67 : poDS->m_dfSrcNoData =
4473 67 : poSrcDS->GetRasterBand(1)->GetNoDataValue(&bHasNoData);
4474 67 : poDS->m_bHasSrcNoData = CPL_TO_BOOL(bHasNoData);
4475 :
4476 67 : if (poDS->m_bUseSrcLabel)
4477 : {
4478 59 : CSLConstList papszMD_ISIS3 = poSrcDS->GetMetadata("json:ISIS3");
4479 59 : if (papszMD_ISIS3 != nullptr)
4480 : {
4481 21 : poDS->SetMetadata(papszMD_ISIS3, "json:ISIS3");
4482 : }
4483 : }
4484 :
4485 : // We don't need to initialize the imagery as we are going to copy it
4486 : // completely
4487 67 : poDS->m_bInitToNodata = false;
4488 67 : CPLErr eErr = GDALDatasetCopyWholeRaster(poSrcDS, poDS, nullptr,
4489 : pfnProgress, pProgressData);
4490 67 : poDS->FlushCache(false);
4491 67 : poDS->m_bHasSrcNoData = false;
4492 67 : if (eErr != CE_None)
4493 : {
4494 11 : delete poDS;
4495 11 : return nullptr;
4496 : }
4497 :
4498 56 : return poDS;
4499 : }
4500 :
4501 : /************************************************************************/
4502 : /* GDALRegister_ISIS3() */
4503 : /************************************************************************/
4504 :
4505 2135 : void GDALRegister_ISIS3()
4506 :
4507 : {
4508 2135 : if (GDALGetDriverByName(ISIS3_DRIVER_NAME) != nullptr)
4509 263 : return;
4510 :
4511 1872 : GDALDriver *poDriver = new GDALDriver();
4512 1872 : ISIS3DriverSetCommonMetadata(poDriver);
4513 :
4514 1872 : poDriver->pfnOpen = ISIS3Dataset::Open;
4515 1872 : poDriver->pfnCreate = ISIS3Dataset::Create;
4516 1872 : poDriver->pfnCreateCopy = ISIS3Dataset::CreateCopy;
4517 :
4518 1872 : GetGDALDriverManager()->RegisterDriver(poDriver);
4519 : }
|