forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiscedataset.cpp
888 lines (774 loc) · 37 KB
/
iscedataset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/******************************************************************************
*
* Project: ISCE Raster Reader
* Purpose: Implementation of the ISCE raster reader
* Author: Matthieu Volat (ISTerre), [email protected]
*
******************************************************************************
* Copyright (c) 2015, Matthieu Volat <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "gdal_frmts.h"
#include "ogr_spatialref.h"
#include "rawdataset.h"
#include <algorithm>
static const char *const apszISCE2GDALDatatypes[] = {
"BYTE:Byte", "CHAR:Byte", "SHORT:Int16", "INT:Int32",
"LONG:Int64", "FLOAT:Float32", "DOUBLE:Float64", "CBYTE:Unknown",
"CCHAR:Unknown", "CSHORT:CInt16", "CINT:CInt32", "CLONG:CInt64",
"CFLOAT:CFloat32", "CDOUBLE:CFloat64", nullptr};
static const char *const apszGDAL2ISCEDatatypes[] = {
"Byte:BYTE", "Int16:SHORT", "Int32:INT", "Int64:LONG",
"Float32:FLOAT", "Float64:DOUBLE", "CInt16:CSHORT", "CInt32:CINT",
"CInt64:CLONG", "CFloat32:CFLOAT", "CFloat64:CDOUBLE", nullptr};
enum Scheme
{
BIL = 0,
BIP = 1,
BSQ = 2
};
static const char *const apszSchemeNames[] = {"BIL", "BIP", "BSQ", nullptr};
/************************************************************************/
/* ==================================================================== */
/* ISCEDataset */
/* ==================================================================== */
/************************************************************************/
class ISCERasterBand;
class ISCEDataset final : public RawDataset
{
friend class ISCERasterBand;
VSILFILE *fpImage;
char *pszXMLFilename;
enum Scheme eScheme;
CPL_DISALLOW_COPY_ASSIGN(ISCEDataset)
CPLErr Close() override;
public:
ISCEDataset();
~ISCEDataset() override;
CPLErr FlushCache(bool bAtClosing) override;
char **GetFileList() override;
static int Identify(GDALOpenInfo *poOpenInfo);
static GDALDataset *Open(GDALOpenInfo *poOpenInfo);
static GDALDataset *Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck);
static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
int nBandsIn, GDALDataType eType,
char **papszOptions);
};
/************************************************************************/
/* ==================================================================== */
/* ISCERasterBand */
/* ==================================================================== */
/************************************************************************/
class ISCERasterBand final : public RawRasterBand
{
CPL_DISALLOW_COPY_ASSIGN(ISCERasterBand)
public:
ISCERasterBand(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
GDALDataType eDataType, int bNativeOrder);
};
/************************************************************************/
/* getXMLFilename() */
/************************************************************************/
static CPLString getXMLFilename(GDALOpenInfo *poOpenInfo)
{
CPLString osXMLFilename;
if (poOpenInfo->fpL == nullptr)
return CPLString();
char **papszSiblingFiles = poOpenInfo->GetSiblingFiles();
if (papszSiblingFiles == nullptr)
{
osXMLFilename =
CPLFormFilename(nullptr, poOpenInfo->pszFilename, "xml");
VSIStatBufL psXMLStatBuf;
if (VSIStatL(osXMLFilename, &psXMLStatBuf) != 0)
{
osXMLFilename = "";
}
}
else
{
/* ------------------------------------------------------------ */
/* We need to tear apart the filename to form a .xml */
/* filename. */
/* ------------------------------------------------------------ */
const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename);
const CPLString osName = CPLGetFilename(poOpenInfo->pszFilename);
const int iFile = CSLFindString(
papszSiblingFiles, CPLFormFilename(nullptr, osName, "xml"));
if (iFile >= 0)
{
osXMLFilename =
CPLFormFilename(osPath, papszSiblingFiles[iFile], nullptr);
}
}
return osXMLFilename;
}
/************************************************************************/
/* ISCEDataset() */
/************************************************************************/
ISCEDataset::ISCEDataset()
: fpImage(nullptr), pszXMLFilename(nullptr), eScheme(BIL)
{
}
/************************************************************************/
/* ~ISCEDataset() */
/************************************************************************/
ISCEDataset::~ISCEDataset()
{
ISCEDataset::Close();
}
/************************************************************************/
/* Close() */
/************************************************************************/
CPLErr ISCEDataset::Close()
{
CPLErr eErr = CE_None;
if (nOpenFlags != OPEN_FLAGS_CLOSED)
{
if (ISCEDataset::FlushCache(true) != CE_None)
eErr = CE_Failure;
if (fpImage)
{
if (VSIFCloseL(fpImage) != 0)
{
CPLError(CE_Failure, CPLE_FileIO, "I/O error");
eErr = CE_Failure;
}
}
CPLFree(pszXMLFilename);
if (GDALPamDataset::Close() != CE_None)
eErr = CE_Failure;
}
return eErr;
}
/************************************************************************/
/* FlushCache() */
/************************************************************************/
CPLErr ISCEDataset::FlushCache(bool bAtClosing)
{
CPLErr eErr = RawDataset::FlushCache(bAtClosing);
GDALRasterBand *band = (GetRasterCount() > 0) ? GetRasterBand(1) : nullptr;
if (eAccess == GA_ReadOnly || band == nullptr)
return eErr;
/* -------------------------------------------------------------------- */
/* Recreate a XML doc with the dataset information. */
/* -------------------------------------------------------------------- */
char sBuf[64] = {'\0'};
CPLXMLNode *psDocNode = CPLCreateXMLNode(nullptr, CXT_Element, "imageFile");
CPLXMLNode *psTmpNode =
CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "WIDTH");
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nRasterXSize);
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "LENGTH");
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nRasterYSize);
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "NUMBER_BANDS");
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nBands);
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
const char *sType = GDALGetDataTypeName(band->GetRasterDataType());
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "DATA_TYPE");
CPLCreateXMLElementAndValue(
psTmpNode, "value",
CSLFetchNameValue(const_cast<char **>(apszGDAL2ISCEDatatypes), sType));
const char *pszScheme = apszSchemeNames[eScheme];
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "SCHEME");
CPLCreateXMLElementAndValue(psTmpNode, "value", pszScheme);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "BYTE_ORDER");
#ifdef CPL_LSB
CPLCreateXMLElementAndValue(psTmpNode, "value", "l");
#else
CPLCreateXMLElementAndValue(psTmpNode, "value", "b");
#endif
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "ACCESS_MODE");
CPLCreateXMLElementAndValue(psTmpNode, "value", "read");
const char *pszFilename = CPLGetBasename(pszXMLFilename);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "FILE_NAME");
CPLCreateXMLElementAndValue(psTmpNode, "value", pszFilename);
/* -------------------------------------------------------------------- */
/* Then, add the ISCE domain metadata. */
/* -------------------------------------------------------------------- */
char **papszISCEMetadata = GetMetadata("ISCE");
for (int i = 0; i < CSLCount(papszISCEMetadata); i++)
{
/* Get the tokens from the metadata item */
char **papszTokens =
CSLTokenizeString2(papszISCEMetadata[i], "=",
CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES);
if (CSLCount(papszTokens) != 2)
{
CPLDebug("ISCE",
"Line of header file could not be split at = into two"
" elements: %s",
papszISCEMetadata[i]);
CSLDestroy(papszTokens);
continue;
}
/* Don't write it out if it is one of the bits of metadata that is
* written out elsewhere in this routine */
if (EQUAL(papszTokens[0], "WIDTH") || EQUAL(papszTokens[0], "LENGTH") ||
EQUAL(papszTokens[0], "NUMBER_BANDS") ||
EQUAL(papszTokens[0], "DATA_TYPE") ||
EQUAL(papszTokens[0], "SCHEME") ||
EQUAL(papszTokens[0], "BYTE_ORDER"))
{
CSLDestroy(papszTokens);
continue;
}
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", papszTokens[0]);
CPLCreateXMLElementAndValue(psTmpNode, "value", papszTokens[1]);
CSLDestroy(papszTokens);
}
/* -------------------------------------------------------------------- */
/* Create the "Coordinate" component elements, possibly with */
/* georeferencing. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psCoordinate1Node, *psCoordinate2Node;
double adfGeoTransform[6];
/* Coordinate 1 */
psCoordinate1Node = CPLCreateXMLNode(psDocNode, CXT_Element, "component");
CPLAddXMLAttributeAndValue(psCoordinate1Node, "name", "Coordinate1");
CPLCreateXMLElementAndValue(psCoordinate1Node, "factorymodule",
"isceobj.Image");
CPLCreateXMLElementAndValue(psCoordinate1Node, "factoryname",
"createCoordinate");
CPLCreateXMLElementAndValue(psCoordinate1Node, "doc",
"First coordinate of a 2D image (width).");
/* Property name */
psTmpNode = CPLCreateXMLNode(psCoordinate1Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "name");
CPLCreateXMLElementAndValue(psTmpNode, "value", "ImageCoordinate_name");
/* Property family */
psTmpNode = CPLCreateXMLNode(psCoordinate1Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "family");
CPLCreateXMLElementAndValue(psTmpNode, "value", "ImageCoordinate");
/* Property size */
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nRasterXSize);
psTmpNode = CPLCreateXMLNode(psCoordinate1Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "size");
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
/* Coordinate 2 */
psCoordinate2Node = CPLCreateXMLNode(psDocNode, CXT_Element, "component");
CPLAddXMLAttributeAndValue(psCoordinate2Node, "name", "Coordinate2");
CPLCreateXMLElementAndValue(psCoordinate2Node, "factorymodule",
"isceobj.Image");
CPLCreateXMLElementAndValue(psCoordinate2Node, "factoryname",
"createCoordinate");
/* Property name */
psTmpNode = CPLCreateXMLNode(psCoordinate2Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "name");
CPLCreateXMLElementAndValue(psTmpNode, "value", "ImageCoordinate_name");
/* Property family */
psTmpNode = CPLCreateXMLNode(psCoordinate2Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "family");
CPLCreateXMLElementAndValue(psTmpNode, "value", "ImageCoordinate");
/* Property size */
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nRasterYSize);
psTmpNode = CPLCreateXMLNode(psCoordinate2Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "size");
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
if (GetGeoTransform(adfGeoTransform) == CE_None)
{
if (adfGeoTransform[2] != 0 || adfGeoTransform[4] != 0)
{
CPLError(CE_Warning, CPLE_AppDefined,
"ISCE format do not support geotransform with "
"rotation, discarding info.");
}
else
{
CPLsnprintf(sBuf, sizeof(sBuf), "%g", adfGeoTransform[0]);
psTmpNode =
CPLCreateXMLNode(psCoordinate1Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "startingValue");
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
CPLsnprintf(sBuf, sizeof(sBuf), "%g", adfGeoTransform[1]);
psTmpNode =
CPLCreateXMLNode(psCoordinate1Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "delta");
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
CPLsnprintf(sBuf, sizeof(sBuf), "%g", adfGeoTransform[3]);
psTmpNode =
CPLCreateXMLNode(psCoordinate2Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "startingValue");
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
CPLsnprintf(sBuf, sizeof(sBuf), "%g", adfGeoTransform[5]);
psTmpNode =
CPLCreateXMLNode(psCoordinate2Node, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "delta");
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
}
}
/* -------------------------------------------------------------------- */
/* Write the XML file. */
/* -------------------------------------------------------------------- */
if (!CPLSerializeXMLTreeToFile(psDocNode, pszXMLFilename))
eErr = CE_Failure;
/* -------------------------------------------------------------------- */
/* Free the XML Doc. */
/* -------------------------------------------------------------------- */
CPLDestroyXMLNode(psDocNode);
return eErr;
}
/************************************************************************/
/* GetFileList() */
/************************************************************************/
char **ISCEDataset::GetFileList()
{
/* Main data file, etc. */
char **papszFileList = RawDataset::GetFileList();
/* XML file. */
papszFileList = CSLAddString(papszFileList, pszXMLFilename);
return papszFileList;
}
/************************************************************************/
/* Identify() */
/************************************************************************/
int ISCEDataset::Identify(GDALOpenInfo *poOpenInfo)
{
/* -------------------------------------------------------------------- */
/* TODO: This function is unusable now: */
/* * we can't just check for the presence of a XML file */
/* * we cannot parse it to check basic tree (Identify() is */
/* supposed to be faster than this */
/* * we could read only a few bytes and strstr() for */
/* "imageData", but what if a file is padded with comments */
/* and/or whitespaces? it would still be legit, but the */
/* driver would fail... */
/* -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- */
/* Check if there is a .xml file */
/* -------------------------------------------------------------------- */
CPLString osXMLFilename = getXMLFilename(poOpenInfo);
if (osXMLFilename.empty())
{
return false;
}
return true;
}
/************************************************************************/
/* Open() */
/************************************************************************/
GDALDataset *ISCEDataset::Open(GDALOpenInfo *poOpenInfo)
{
return Open(poOpenInfo, true);
}
GDALDataset *ISCEDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)
{
/* -------------------------------------------------------------------- */
/* Confirm that the header is compatible with a ISCE dataset. */
/* -------------------------------------------------------------------- */
if (!Identify(poOpenInfo) || poOpenInfo->fpL == nullptr)
{
return nullptr;
}
/* -------------------------------------------------------------------- */
/* Open and parse the .xml file */
/* -------------------------------------------------------------------- */
const CPLString osXMLFilename = getXMLFilename(poOpenInfo);
CPLXMLNode *psNode = CPLParseXMLFile(osXMLFilename);
if (psNode == nullptr || CPLGetXMLNode(psNode, "=imageFile") == nullptr)
{
CPLDestroyXMLNode(psNode);
return nullptr;
}
CPLXMLNode *psCur = CPLGetXMLNode(psNode, "=imageFile")->psChild;
CPLStringList aosXmlProps;
while (psCur != nullptr)
{
if (EQUAL(psCur->pszValue, "property"))
{
/* Top-level property */
const char *pszName = CPLGetXMLValue(psCur, "name", nullptr);
const char *pszValue = CPLGetXMLValue(psCur, "value", nullptr);
if (pszName != nullptr && pszValue != nullptr)
{
aosXmlProps.SetNameValue(pszName, pszValue);
}
}
else if (EQUAL(psCur->pszValue, "component"))
{
/* "components" elements in ISCE store set of properties. */
/* For now, they are avoided as I am not sure the full */
/* scope of these. An exception is made for the ones named */
/* Coordinate1 and Coordinate2, because they may have the */
/* georeferencing information. */
const char *pszCurName = CPLGetXMLValue(psCur, "name", nullptr);
if (pszCurName != nullptr && (EQUAL(pszCurName, "Coordinate1") ||
EQUAL(pszCurName, "Coordinate2")))
{
/* We need two subproperties: startingValue and delta. */
/* To simplify parsing code, we will store them in */
/* aosXmlProps with the coordinate name prefixed to */
/* the property name. */
CPLXMLNode *psCur2 = psCur->psChild;
while (psCur2 != nullptr)
{
if (!EQUAL(psCur2->pszValue, "property"))
{
psCur2 = psCur2->psNext;
continue; /* Skip non property elements */
}
const char *pszCur2Name =
CPLGetXMLValue(psCur2, "name", nullptr),
*pszCur2Value =
CPLGetXMLValue(psCur2, "value", nullptr);
if (pszCur2Name == nullptr || pszCur2Value == nullptr)
{
psCur2 = psCur2->psNext;
continue; /* Skip malformatted elements */
}
if (EQUAL(pszCur2Name, "startingValue") ||
EQUAL(pszCur2Name, "delta"))
{
char szPropName[32];
snprintf(szPropName, sizeof(szPropName), "%s%s",
pszCurName, pszCur2Name);
aosXmlProps.SetNameValue(szPropName, pszCur2Value);
}
psCur2 = psCur2->psNext;
}
}
}
psCur = psCur->psNext;
}
CPLDestroyXMLNode(psNode);
/* -------------------------------------------------------------------- */
/* Fetch required fields. */
/* -------------------------------------------------------------------- */
if (aosXmlProps.FetchNameValue("WIDTH") == nullptr ||
aosXmlProps.FetchNameValue("LENGTH") == nullptr ||
aosXmlProps.FetchNameValue("NUMBER_BANDS") == nullptr ||
aosXmlProps.FetchNameValue("DATA_TYPE") == nullptr ||
aosXmlProps.FetchNameValue("SCHEME") == nullptr)
{
return nullptr;
}
const int nWidth = atoi(aosXmlProps.FetchNameValue("WIDTH"));
const int nHeight = atoi(aosXmlProps.FetchNameValue("LENGTH"));
const int nBands = atoi(aosXmlProps.FetchNameValue("NUMBER_BANDS"));
if (!GDALCheckDatasetDimensions(nWidth, nHeight) ||
!GDALCheckBandCount(nBands, FALSE))
{
return nullptr;
}
/* -------------------------------------------------------------------- */
/* Update byte order info if image specify something. */
/* -------------------------------------------------------------------- */
bool bNativeOrder = true;
const char *pszByteOrder = aosXmlProps.FetchNameValue("BYTE_ORDER");
if (pszByteOrder != nullptr)
{
#ifdef CPL_LSB
if (EQUAL(pszByteOrder, "b"))
#else
if (EQUAL(pszByteOrder, "l"))
#endif
bNativeOrder = false;
}
/* -------------------------------------------------------------------- */
/* Create a corresponding GDALDataset. */
/* -------------------------------------------------------------------- */
auto poDS = std::make_unique<ISCEDataset>();
poDS->nRasterXSize = nWidth;
poDS->nRasterYSize = nHeight;
poDS->eAccess = poOpenInfo->eAccess;
poDS->pszXMLFilename = CPLStrdup(osXMLFilename.c_str());
std::swap(poDS->fpImage, poOpenInfo->fpL);
/* -------------------------------------------------------------------- */
/* Create band information objects. */
/* -------------------------------------------------------------------- */
const char *pszDataType = CSLFetchNameValue(
apszISCE2GDALDatatypes, aosXmlProps.FetchNameValue("DATA_TYPE"));
if (pszDataType == nullptr)
{
return nullptr;
}
const GDALDataType eDataType = GDALGetDataTypeByName(pszDataType);
const int nDTSize = GDALGetDataTypeSizeBytes(eDataType);
if (nDTSize == 0)
{
return nullptr;
}
const char *pszScheme = aosXmlProps.FetchNameValue("SCHEME");
int nPixelOffset = 0;
int nLineOffset = 0;
vsi_l_offset nBandOffset = 0;
bool bIntOverflow = false;
if (EQUAL(pszScheme, "BIL"))
{
poDS->eScheme = BIL;
nPixelOffset = nDTSize;
if (nWidth > INT_MAX / (nPixelOffset * nBands))
bIntOverflow = true;
else
{
nLineOffset = nPixelOffset * nWidth * nBands;
nBandOffset = nDTSize * static_cast<vsi_l_offset>(nWidth);
}
}
else if (EQUAL(pszScheme, "BIP"))
{
poDS->eScheme = BIP;
nPixelOffset = nDTSize * nBands;
if (nWidth > INT_MAX / nPixelOffset)
bIntOverflow = true;
else
{
nLineOffset = nPixelOffset * nWidth;
if (nBands > 1 && nLineOffset < INT_MAX / nBands)
{
// GDAL 2.1.0 had a value of nLineOffset that was equal to the
// theoretical nLineOffset multiplied by nBands...
VSIFSeekL(poDS->fpImage, 0, SEEK_END);
const GUIntBig nWrongFileSize =
static_cast<GUIntBig>(nDTSize) * nWidth *
(static_cast<GUIntBig>(nHeight - 1) * nBands * nBands +
nBands);
if (VSIFTellL(poDS->fpImage) == nWrongFileSize)
{
CPLError(
CE_Warning, CPLE_AppDefined,
"This file has been incorrectly generated by an older "
"GDAL version whose line offset computation was "
"erroneous. "
"Taking that into account, but the file should be "
"re-encoded ideally");
nLineOffset = nLineOffset * nBands;
}
}
nBandOffset = nDTSize;
}
}
else if (EQUAL(pszScheme, "BSQ"))
{
poDS->eScheme = BSQ;
nPixelOffset = nDTSize;
if (nWidth > INT_MAX / nPixelOffset)
bIntOverflow = true;
else
{
nLineOffset = nPixelOffset * nWidth;
nBandOffset = nLineOffset * static_cast<vsi_l_offset>(nHeight);
}
}
else
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Unknown scheme \"%s\" within ISCE raster.", pszScheme);
return nullptr;
}
if (bIntOverflow)
{
CPLError(CE_Failure, CPLE_AppDefined, "Int overflow occurred.");
return nullptr;
}
if (bFileSizeCheck &&
!RAWDatasetCheckMemoryUsage(poDS->nRasterXSize, poDS->nRasterYSize,
nBands, nDTSize, nPixelOffset, nLineOffset,
0, nBandOffset, poDS->fpImage))
{
return nullptr;
}
for (int b = 0; b < nBands; b++)
{
auto poBand = std::make_unique<ISCERasterBand>(
poDS.get(), b + 1, poDS->fpImage, nBandOffset * b, nPixelOffset,
nLineOffset, eDataType, bNativeOrder);
if (!poBand->IsValid())
return nullptr;
poDS->SetBand(b + 1, std::move(poBand));
}
/* -------------------------------------------------------------------- */
/* Interpret georeferencing, if present. */
/* -------------------------------------------------------------------- */
if (aosXmlProps.FetchNameValue("Coordinate1startingValue") != nullptr &&
aosXmlProps.FetchNameValue("Coordinate1delta") != nullptr &&
aosXmlProps.FetchNameValue("Coordinate2startingValue") != nullptr &&
aosXmlProps.FetchNameValue("Coordinate2delta") != nullptr)
{
double adfGeoTransform[6];
adfGeoTransform[0] =
CPLAtof(aosXmlProps.FetchNameValue("Coordinate1startingValue"));
adfGeoTransform[1] =
CPLAtof(aosXmlProps.FetchNameValue("Coordinate1delta"));
adfGeoTransform[2] = 0.0;
adfGeoTransform[3] =
CPLAtof(aosXmlProps.FetchNameValue("Coordinate2startingValue"));
adfGeoTransform[4] = 0.0;
adfGeoTransform[5] =
CPLAtof(aosXmlProps.FetchNameValue("Coordinate2delta"));
poDS->SetGeoTransform(adfGeoTransform);
/* ISCE format seems not to have a projection field, but uses */
/* WGS84. */
poDS->SetProjection(SRS_WKT_WGS84_LAT_LONG);
}
/* -------------------------------------------------------------------- */
/* Set all the other header metadata into the ISCE domain */
/* -------------------------------------------------------------------- */
for (int i = 0; i < aosXmlProps.size(); i++)
{
const CPLStringList aosTokens(CSLTokenizeString2(
aosXmlProps[i], "=", CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES));
if (aosTokens.size() < 2 || EQUAL(aosTokens[0], "WIDTH") ||
EQUAL(aosTokens[0], "LENGTH") ||
EQUAL(aosTokens[0], "NUMBER_BANDS") ||
EQUAL(aosTokens[0], "DATA_TYPE") || EQUAL(aosTokens[0], "SCHEME") ||
EQUAL(aosTokens[0], "BYTE_ORDER") ||
EQUAL(aosTokens[0], "Coordinate1startingValue") ||
EQUAL(aosTokens[0], "Coordinate1delta") ||
EQUAL(aosTokens[0], "Coordinate2startingValue") ||
EQUAL(aosTokens[0], "Coordinate2delta"))
{
continue;
}
poDS->SetMetadataItem(aosTokens[0], aosTokens[1], "ISCE");
}
/* -------------------------------------------------------------------- */
/* Initialize any PAM information. */
/* -------------------------------------------------------------------- */
poDS->SetDescription(poOpenInfo->pszFilename);
poDS->TryLoadXML();
/* -------------------------------------------------------------------- */
/* Check for overviews. */
/* -------------------------------------------------------------------- */
poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename);
return poDS.release();
}
/************************************************************************/
/* Create() */
/************************************************************************/
GDALDataset *ISCEDataset::Create(const char *pszFilename, int nXSize,
int nYSize, int nBandsIn, GDALDataType eType,
char **papszOptions)
{
const char *sType = GDALGetDataTypeName(eType);
const char *pszScheme = CSLFetchNameValueDef(papszOptions, "SCHEME", "BIP");
/* -------------------------------------------------------------------- */
/* Try to create the file. */
/* -------------------------------------------------------------------- */
VSILFILE *fp = VSIFOpenL(pszFilename, "wb");
if (fp == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Attempt to create file `%s' failed.", pszFilename);
return nullptr;
}
/* -------------------------------------------------------------------- */
/* Just write out a couple of bytes to establish the binary */
/* file, and then close it. */
/* -------------------------------------------------------------------- */
CPL_IGNORE_RET_VAL(VSIFWriteL("\0\0", 2, 1, fp));
CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
/* -------------------------------------------------------------------- */
/* Create a minimal XML document. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psDocNode = CPLCreateXMLNode(nullptr, CXT_Element, "imageFile");
CPLXMLNode *psTmpNode =
CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "WIDTH");
char sBuf[64] = {'\0'};
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nXSize);
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "LENGTH");
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nYSize);
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "NUMBER_BANDS");
CPLsnprintf(sBuf, sizeof(sBuf), "%d", nBandsIn);
CPLCreateXMLElementAndValue(psTmpNode, "value", sBuf);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "DATA_TYPE");
CPLCreateXMLElementAndValue(
psTmpNode, "value",
CSLFetchNameValue(const_cast<char **>(apszGDAL2ISCEDatatypes), sType));
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "SCHEME");
CPLCreateXMLElementAndValue(psTmpNode, "value", pszScheme);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "BYTE_ORDER");
#ifdef CPL_LSB
CPLCreateXMLElementAndValue(psTmpNode, "value", "l");
#else
CPLCreateXMLElementAndValue(psTmpNode, "value", "b");
#endif
/* -------------------------------------------------------------------- */
/* Write the XML file. */
/* -------------------------------------------------------------------- */
const char *pszXMLFilename = CPLFormFilename(nullptr, pszFilename, "xml");
CPLSerializeXMLTreeToFile(psDocNode, pszXMLFilename);
/* -------------------------------------------------------------------- */
/* Free the XML Doc. */
/* -------------------------------------------------------------------- */
CPLDestroyXMLNode(psDocNode);
GDALOpenInfo oOpenInfo(pszFilename, GA_Update);
return Open(&oOpenInfo, false);
}
/************************************************************************/
/* ISCERasterBand() */
/************************************************************************/
ISCERasterBand::ISCERasterBand(GDALDataset *poDSIn, int nBandIn,
VSILFILE *fpRawIn, vsi_l_offset nImgOffsetIn,
int nPixelOffsetIn, int nLineOffsetIn,
GDALDataType eDataTypeIn, int bNativeOrderIn)
: RawRasterBand(poDSIn, nBandIn, fpRawIn, nImgOffsetIn, nPixelOffsetIn,
nLineOffsetIn, eDataTypeIn, bNativeOrderIn,
RawRasterBand::OwnFP::NO)
{
}
/************************************************************************/
/* GDALRegister_ISCE() */
/************************************************************************/
void GDALRegister_ISCE()
{
if (GDALGetDriverByName("ISCE") != nullptr)
return;
GDALDriver *poDriver = new GDALDriver();
poDriver->SetDescription("ISCE");
poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "ISCE raster");
poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/isce.html");
poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES,
"Byte Int16 Int32 Int64 Float32"
" Float64 CInt16 CInt64 CFloat32 "
" CFloat64");
poDriver->SetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST,
"<CreationOptionList>"
" <Option name='SCHEME' type='string-select'>"
" <Value>BIP</Value>"
" <Value>BIL</Value>"
" <Value>BSQ</Value>"
" </Option>"
"</CreationOptionList>");
poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
poDriver->pfnOpen = ISCEDataset::Open;
poDriver->pfnCreate = ISCEDataset::Create;
GetGDALDriverManager()->RegisterDriver(poDriver);
}