-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathjaxapalsardataset.cpp
718 lines (614 loc) · 24.9 KB
/
jaxapalsardataset.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
/******************************************************************************
*
* Project: PALSAR JAXA imagery reader
* Purpose: Support for PALSAR L1.1/1.5 imagery and appropriate metadata from
* JAXA and JAXA-supported ground stations (ASF, ESA, etc.). This
* driver does not support ERSDAC products.
* Author: Philippe Vachon <philippe@cowpig.ca>
*
******************************************************************************
* Copyright (c) 2007, Philippe P. Vachon <philippe@cowpig.ca>
* Copyright (c) 2008-2011, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdal_frmts.h"
#include "gdal_pam.h"
#if defined(_WIN32)
#define SEP_STRING "\\"
#else
#define SEP_STRING "/"
#endif
/* read binary fields */
#ifdef CPL_LSB
#define READ_WORD(f, x) \
do \
{ \
VSIFReadL(&(x), 4, 1, (f)); \
(x) = CPL_SWAP32((x)); \
} while (false);
#define READ_SHORT(f, x) \
do \
{ \
VSIFReadL(&(x), 2, 1, (f)); \
(x) = CPL_SWAP16((x)); \
} while (false);
#else
#define READ_WORD(f, x) \
do \
{ \
VSIFReadL(&(x), 4, 1, (f)); \
} while (false);
#define READ_SHORT(f, x) \
do \
{ \
VSIFReadL(&(x), 2, 1, (f)); \
} while (false);
#endif /* def CPL_LSB */
#define READ_BYTE(f, x) \
do \
{ \
VSIFReadL(&(x), 1, 1, (f)); \
} while (false);
/* read floating point value stored as ASCII */
#define READ_CHAR_FLOAT(n, l, f) \
do \
{ \
char psBuf[(l + 1)]; \
psBuf[(l)] = '\0'; \
VSIFReadL(&psBuf, (l), 1, (f)); \
(n) = CPLAtof(psBuf); \
} while (false);
/* read numbers stored as ASCII */
#define READ_CHAR_VAL(x, n, f) \
do \
{ \
char psBuf[(n + 1)]; \
psBuf[(n)] = '\0'; \
VSIFReadL(&psBuf, (n), 1, (f)); \
(x) = atoi(psBuf); \
} while (false);
/* read string fields
* note: string must be size of field to be extracted + 1
*/
#define READ_STRING(s, n, f) \
do \
{ \
VSIFReadL(&(s), 1, (n), (f)); \
(s)[(n)] = '\0'; \
} while (false);
/*************************************************************************/
/* a few key offsets in the volume directory file */
#define VOL_DESC_RECORD_LENGTH 360
#define FILE_PTR_RECORD_LENGTH 360
#define NUM_RECORDS_OFFSET 160
/* a few key offsets and values within the File Pointer record */
#define REF_FILE_CLASS_CODE_OFFSET 66
#define REF_FILE_CLASS_CODE_LENGTH 4
#define FILE_NAME_OFFSET 310
/* some image option descriptor records */
#define BITS_PER_SAMPLE_OFFSET 216
#define BITS_PER_SAMPLE_LENGTH 4
#define SAMPLES_PER_GROUP_OFFSET 220
#define SAMPLES_PER_GROUP_LENGTH 4
#define NUMBER_LINES_OFFSET 236
#define NUMBER_LINES_LENGTH 8
#define SAR_DATA_RECORD_LENGTH_OFFSET 186
#define SAR_DATA_RECORD_LENGTH_LENGTH 6
#define IMAGE_OPT_DESC_LENGTH 720
#define SIG_DAT_REC_OFFSET 412
#define PROC_DAT_REC_OFFSET 192
/* metadata to be extracted from the leader file */
#define LEADER_FILE_DESCRIPTOR_LENGTH 720
#define DATA_SET_SUMMARY_LENGTH 4096
/* relative to end of leader file descriptor */
#define EFFECTIVE_LOOKS_AZIMUTH_OFFSET 1174 /* floating point text */
#define EFFECTIVE_LOOKS_AZIMUTH_LENGTH 16
/* relative to leader file descriptor + dataset summary length */
#define PIXEL_SPACING_OFFSET 92
#define LINE_SPACING_OFFSET 108
#define ALPHANUMERIC_PROJECTION_NAME_OFFSET 412
#define TOP_LEFT_LAT_OFFSET 1072
#define TOP_LEFT_LON_OFFSET 1088
#define TOP_RIGHT_LAT_OFFSET 1104
#define TOP_RIGHT_LON_OFFSET 1120
#define BOTTOM_RIGHT_LAT_OFFSET 1136
#define BOTTOM_RIGHT_LON_OFFSET 1152
#define BOTTOM_LEFT_LAT_OFFSET 1168
#define BOTTOM_LEFT_LON_OFFSET 1184
namespace gdal::PSALSARJaxa
{
/* a few useful enums */
enum eFileType
{
level_11 = 0,
level_15,
level_10,
level_unknown = 999,
};
enum ePolarization
{
hh = 0,
hv,
vh,
vv
};
} // namespace gdal::PSALSARJaxa
using namespace gdal::PSALSARJaxa;
/************************************************************************/
/* ==================================================================== */
/* PALSARJaxaDataset */
/* ==================================================================== */
/************************************************************************/
class PALSARJaxaRasterBand;
class PALSARJaxaDataset final : public GDALPamDataset
{
friend class PALSARJaxaRasterBand;
private:
GDAL_GCP *pasGCPList;
int nGCPCount;
eFileType nFileType;
public:
PALSARJaxaDataset();
~PALSARJaxaDataset();
int GetGCPCount() override;
const GDAL_GCP *GetGCPs() override;
static GDALDataset *Open(GDALOpenInfo *poOpenInfo);
static int Identify(GDALOpenInfo *poOpenInfo);
static void ReadMetadata(PALSARJaxaDataset *poDS, VSILFILE *fp);
};
PALSARJaxaDataset::PALSARJaxaDataset()
: pasGCPList(nullptr), nGCPCount(0), nFileType(level_unknown)
{
}
PALSARJaxaDataset::~PALSARJaxaDataset()
{
if (nGCPCount > 0)
{
GDALDeinitGCPs(nGCPCount, pasGCPList);
CPLFree(pasGCPList);
}
}
/************************************************************************/
/* ==================================================================== */
/* PALSARJaxaRasterBand */
/* ==================================================================== */
/************************************************************************/
class PALSARJaxaRasterBand final : public GDALRasterBand
{
VSILFILE *fp;
ePolarization nPolarization;
eFileType nFileType;
int nBitsPerSample;
int nSamplesPerGroup;
int nRecordSize;
public:
PALSARJaxaRasterBand(PALSARJaxaDataset *poDS, int nBand, VSILFILE *fp);
~PALSARJaxaRasterBand();
CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override;
};
/************************************************************************/
/* PALSARJaxaRasterBand() */
/************************************************************************/
PALSARJaxaRasterBand::PALSARJaxaRasterBand(PALSARJaxaDataset *poDSIn,
int nBandIn, VSILFILE *fpIn)
: fp(fpIn), nPolarization(hh), nBitsPerSample(0), nSamplesPerGroup(0),
nRecordSize(0)
{
poDS = poDSIn;
nBand = nBandIn;
/* Read image options record to determine the type of data */
VSIFSeekL(fp, BITS_PER_SAMPLE_OFFSET, SEEK_SET);
READ_CHAR_VAL(nBitsPerSample, BITS_PER_SAMPLE_LENGTH, fp);
READ_CHAR_VAL(nSamplesPerGroup, SAMPLES_PER_GROUP_LENGTH, fp);
if (nBitsPerSample == 32 && nSamplesPerGroup == 2)
{
eDataType = GDT_CFloat32;
nFileType = level_11;
}
else if (nBitsPerSample == 8 && nSamplesPerGroup == 2)
{
eDataType = GDT_CInt16; /* shuold be 2 x signed byte */
nFileType = level_10;
}
else
{
eDataType = GDT_UInt16;
nFileType = level_15;
}
poDSIn->nFileType = nFileType;
/* Read number of range/azimuth lines */
VSIFSeekL(fp, NUMBER_LINES_OFFSET, SEEK_SET);
READ_CHAR_VAL(nRasterYSize, NUMBER_LINES_LENGTH, fp);
VSIFSeekL(fp, SAR_DATA_RECORD_LENGTH_OFFSET, SEEK_SET);
READ_CHAR_VAL(nRecordSize, SAR_DATA_RECORD_LENGTH_LENGTH, fp);
const int nDenom = ((nBitsPerSample / 8) * nSamplesPerGroup);
if (nDenom != 0)
nRasterXSize =
(nRecordSize - (nFileType != level_15 ? SIG_DAT_REC_OFFSET
: PROC_DAT_REC_OFFSET)) /
nDenom;
poDSIn->nRasterXSize = nRasterXSize;
poDSIn->nRasterYSize = nRasterYSize;
/* Polarization */
switch (nBand)
{
case 0:
nPolarization = hh;
SetMetadataItem("POLARIMETRIC_INTERP", "HH");
break;
case 1:
nPolarization = hv;
SetMetadataItem("POLARIMETRIC_INTERP", "HV");
break;
case 2:
nPolarization = vh;
SetMetadataItem("POLARIMETRIC_INTERP", "VH");
break;
case 3:
nPolarization = vv;
SetMetadataItem("POLARIMETRIC_INTERP", "VV");
break;
// TODO: What about the default?
}
/* size of block we can read */
nBlockXSize = nRasterXSize;
nBlockYSize = 1;
/* set the file pointer to the first SAR data record */
VSIFSeekL(fp, IMAGE_OPT_DESC_LENGTH, SEEK_SET);
}
/************************************************************************/
/* ~PALSARJaxaRasterBand() */
/************************************************************************/
PALSARJaxaRasterBand::~PALSARJaxaRasterBand()
{
if (fp)
VSIFCloseL(fp);
}
/************************************************************************/
/* IReadBlock() */
/************************************************************************/
CPLErr PALSARJaxaRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff,
int nBlockYOff, void *pImage)
{
int nNumBytes = 0;
if (nFileType == level_11)
{
nNumBytes = 8;
}
else
{
nNumBytes = 2;
}
int nOffset =
IMAGE_OPT_DESC_LENGTH + ((nBlockYOff - 1) * nRecordSize) +
(nFileType == level_11 ? SIG_DAT_REC_OFFSET : PROC_DAT_REC_OFFSET);
VSIFSeekL(fp, nOffset, SEEK_SET);
VSIFReadL(pImage, nNumBytes, nRasterXSize, fp);
#ifdef CPL_LSB
if (nFileType == level_11)
GDALSwapWords(pImage, 4, nBlockXSize * 2, 4);
else
GDALSwapWords(pImage, 2, nBlockXSize, 2);
#endif
return CE_None;
}
/************************************************************************/
/* ==================================================================== */
/* PALSARJaxaDataset */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* ReadMetadata() */
/************************************************************************/
int PALSARJaxaDataset::GetGCPCount()
{
return nGCPCount;
}
/************************************************************************/
/* GetGCPs() */
/************************************************************************/
const GDAL_GCP *PALSARJaxaDataset::GetGCPs()
{
return pasGCPList;
}
/************************************************************************/
/* ReadMetadata() */
/************************************************************************/
void PALSARJaxaDataset::ReadMetadata(PALSARJaxaDataset *poDS, VSILFILE *fp)
{
/* seek to the end of the leader file descriptor */
VSIFSeekL(fp, LEADER_FILE_DESCRIPTOR_LENGTH, SEEK_SET);
if (poDS->nFileType == level_10)
{
poDS->SetMetadataItem("PRODUCT_LEVEL", "1.0");
poDS->SetMetadataItem("AZIMUTH_LOOKS", "1.0");
}
else if (poDS->nFileType == level_11)
{
poDS->SetMetadataItem("PRODUCT_LEVEL", "1.1");
poDS->SetMetadataItem("AZIMUTH_LOOKS", "1.0");
}
else
{
poDS->SetMetadataItem("PRODUCT_LEVEL", "1.5");
/* extract equivalent number of looks */
VSIFSeekL(
fp, LEADER_FILE_DESCRIPTOR_LENGTH + EFFECTIVE_LOOKS_AZIMUTH_OFFSET,
SEEK_SET);
char szENL[17];
double dfENL;
READ_CHAR_FLOAT(dfENL, 16, fp);
snprintf(szENL, sizeof(szENL), "%-16.1f", dfENL);
poDS->SetMetadataItem("AZIMUTH_LOOKS", szENL);
/* extract pixel spacings */
VSIFSeekL(fp,
LEADER_FILE_DESCRIPTOR_LENGTH + DATA_SET_SUMMARY_LENGTH +
PIXEL_SPACING_OFFSET,
SEEK_SET);
double dfPixelSpacing;
double dfLineSpacing;
char szPixelSpacing[33];
char szLineSpacing[33];
READ_CHAR_FLOAT(dfPixelSpacing, 16, fp);
READ_CHAR_FLOAT(dfLineSpacing, 16, fp);
snprintf(szPixelSpacing, sizeof(szPixelSpacing), "%-32.1f",
dfPixelSpacing);
snprintf(szLineSpacing, sizeof(szLineSpacing), "%-32.1f",
dfLineSpacing);
poDS->SetMetadataItem("PIXEL_SPACING", szPixelSpacing);
poDS->SetMetadataItem("LINE_SPACING", szPixelSpacing);
/* Alphanumeric projection name */
VSIFSeekL(fp,
LEADER_FILE_DESCRIPTOR_LENGTH + DATA_SET_SUMMARY_LENGTH +
ALPHANUMERIC_PROJECTION_NAME_OFFSET,
SEEK_SET);
char szProjName[33];
READ_STRING(szProjName, 32, fp);
poDS->SetMetadataItem("PROJECTION_NAME", szProjName);
/* Extract corner GCPs */
poDS->nGCPCount = 4;
poDS->pasGCPList =
(GDAL_GCP *)CPLCalloc(sizeof(GDAL_GCP), poDS->nGCPCount);
GDALInitGCPs(poDS->nGCPCount, poDS->pasGCPList);
/* setup the GCPs */
int i;
for (i = 0; i < poDS->nGCPCount; i++)
{
char szID[30];
snprintf(szID, sizeof(szID), "%d", i + 1);
CPLFree(poDS->pasGCPList[i].pszId);
poDS->pasGCPList[i].pszId = CPLStrdup(szID);
poDS->pasGCPList[i].dfGCPZ = 0.0;
}
double dfTemp = 0.0;
/* seek to start of GCPs */
VSIFSeekL(fp,
LEADER_FILE_DESCRIPTOR_LENGTH + DATA_SET_SUMMARY_LENGTH +
TOP_LEFT_LAT_OFFSET,
SEEK_SET);
/* top-left GCP */
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[0].dfGCPY = dfTemp;
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[0].dfGCPX = dfTemp;
poDS->pasGCPList[0].dfGCPLine = 0.5;
poDS->pasGCPList[0].dfGCPPixel = 0.5;
/* top right GCP */
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[1].dfGCPY = dfTemp;
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[1].dfGCPX = dfTemp;
poDS->pasGCPList[1].dfGCPLine = 0.5;
poDS->pasGCPList[1].dfGCPPixel = poDS->nRasterYSize - 0.5;
/* bottom right GCP */
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[2].dfGCPY = dfTemp;
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[2].dfGCPX = dfTemp;
poDS->pasGCPList[2].dfGCPLine = poDS->nRasterYSize - 0.5;
poDS->pasGCPList[2].dfGCPPixel = poDS->nRasterYSize - 0.5;
/* bottom left GCP */
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[3].dfGCPY = dfTemp;
READ_CHAR_FLOAT(dfTemp, 16, fp);
poDS->pasGCPList[3].dfGCPX = dfTemp;
poDS->pasGCPList[3].dfGCPLine = poDS->nRasterYSize - 0.5;
poDS->pasGCPList[3].dfGCPPixel = 0.5;
}
/* some generic metadata items */
poDS->SetMetadataItem("SENSOR_BAND", "L"); /* PALSAR is L-band */
poDS->SetMetadataItem("RANGE_LOOKS", "1.0");
/* Check if this is a PolSAR dataset */
if (poDS->GetRasterCount() == 4)
{
/* PALSAR data is only available from JAXA in Scattering Matrix form */
poDS->SetMetadataItem("MATRIX_REPRESENTATION", "SCATTERING");
}
}
/************************************************************************/
/* Identify() */
/************************************************************************/
static void ReadWord(VSILFILE *fp, int *pVal)
{
READ_WORD(fp, *pVal);
}
static void ReadByte(VSILFILE *fp, int *pVal)
{
READ_BYTE(fp, *pVal);
}
int PALSARJaxaDataset::Identify(GDALOpenInfo *poOpenInfo)
{
if (poOpenInfo->nHeaderBytes < 360 || poOpenInfo->fpL == nullptr)
return 0;
/* First, check that this is a PALSAR image indeed */
if (!STARTS_WITH_CI((char *)(poOpenInfo->pabyHeader + 60), "AL"))
{
return 0;
}
const std::string osBasename = CPLGetBasenameSafe(poOpenInfo->pszFilename);
if (osBasename.size() < 9 ||
!STARTS_WITH_CI(osBasename.c_str() + 4, "ALPSR"))
{
return 0;
}
/* Check that this is a volume directory file */
int nRecordSeq = 0;
int nRecordSubtype = 0;
int nRecordType = 0;
int nSecondSubtype = 0;
int nThirdSubtype = 0;
int nLengthRecord = 0;
VSIFSeekL(poOpenInfo->fpL, 0, SEEK_SET);
ReadWord(poOpenInfo->fpL, &nRecordSeq);
ReadByte(poOpenInfo->fpL, &nRecordSubtype);
ReadByte(poOpenInfo->fpL, &nRecordType);
ReadByte(poOpenInfo->fpL, &nSecondSubtype);
ReadByte(poOpenInfo->fpL, &nThirdSubtype);
ReadWord(poOpenInfo->fpL, &nLengthRecord);
VSIFSeekL(poOpenInfo->fpL, 0, SEEK_SET);
/* Check that we have the right record */
if (nRecordSeq == 1 && nRecordSubtype == 192 && nRecordType == 192 &&
nSecondSubtype == 18 && nThirdSubtype == 18 && nLengthRecord == 360)
{
return 1;
}
return 0;
}
/************************************************************************/
/* Open() */
/************************************************************************/
GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo)
{
/* Check that this actually is a JAXA PALSAR product */
if (!PALSARJaxaDataset::Identify(poOpenInfo))
return nullptr;
/* -------------------------------------------------------------------- */
/* Confirm the requested access is supported. */
/* -------------------------------------------------------------------- */
if (poOpenInfo->eAccess == GA_Update)
{
CPLError(
CE_Failure, CPLE_NotSupported,
"The JAXAPALSAR driver does not support update access to existing"
" datasets.\n");
return nullptr;
}
PALSARJaxaDataset *poDS = new PALSARJaxaDataset();
/* Get the suffix of the filename, we'll need this */
char *pszSuffix =
VSIStrdup((char *)(CPLGetFilename(poOpenInfo->pszFilename) + 3));
/* Try to read each of the polarizations */
const size_t nImgFileLen =
CPLGetDirnameSafe(poOpenInfo->pszFilename).size() + strlen(pszSuffix) +
8;
char *pszImgFile = (char *)CPLMalloc(nImgFileLen);
int nBandNum = 1;
/* HH */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-HH%s",
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpHH = VSIFOpenL(pszImgFile, "rb");
if (fpHH != nullptr)
{
poDS->SetBand(nBandNum, new PALSARJaxaRasterBand(poDS, 0, fpHH));
nBandNum++;
}
/* HV */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-HV%s",
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpHV = VSIFOpenL(pszImgFile, "rb");
if (fpHV != nullptr)
{
poDS->SetBand(nBandNum, new PALSARJaxaRasterBand(poDS, 1, fpHV));
nBandNum++;
}
/* VH */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-VH%s",
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpVH = VSIFOpenL(pszImgFile, "rb");
if (fpVH != nullptr)
{
poDS->SetBand(nBandNum, new PALSARJaxaRasterBand(poDS, 2, fpVH));
nBandNum++;
}
/* VV */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-VV%s",
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpVV = VSIFOpenL(pszImgFile, "rb");
if (fpVV != nullptr)
{
poDS->SetBand(nBandNum, new PALSARJaxaRasterBand(poDS, 3, fpVV));
/* nBandNum++; */
}
VSIFree(pszImgFile);
/* did we get at least one band? */
if (fpVV == nullptr && fpVH == nullptr && fpHV == nullptr &&
fpHH == nullptr)
{
CPLError(
CE_Failure, CPLE_AppDefined,
"Unable to find any image data. Aborting opening as PALSAR image.");
delete poDS;
VSIFree(pszSuffix);
return nullptr;
}
/* Level 1.0 products are not supported */
if (poDS->nFileType == level_10)
{
CPLError(CE_Failure, CPLE_AppDefined,
"ALOS PALSAR Level 1.0 products are not supported. Aborting "
"opening as PALSAR image.");
delete poDS;
VSIFree(pszSuffix);
return nullptr;
}
/* read metadata from Leader file. */
const size_t nLeaderFilenameLen =
strlen(CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str()) +
strlen(pszSuffix) + 5;
char *pszLeaderFilename = (char *)CPLMalloc(nLeaderFilenameLen);
snprintf(pszLeaderFilename, nLeaderFilenameLen, "%s%sLED%s",
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpLeader = VSIFOpenL(pszLeaderFilename, "rb");
/* check if the leader is actually present */
if (fpLeader != nullptr)
{
ReadMetadata(poDS, fpLeader);
VSIFCloseL(fpLeader);
}
VSIFree(pszLeaderFilename);
VSIFree(pszSuffix);
/* -------------------------------------------------------------------- */
/* Initialize any PAM information. */
/* -------------------------------------------------------------------- */
poDS->SetDescription(poOpenInfo->pszFilename);
poDS->TryLoadXML();
/* -------------------------------------------------------------------- */
/* Check for overviews. */
/* -------------------------------------------------------------------- */
poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
return poDS;
}
/************************************************************************/
/* GDALRegister_PALSARJaxa() */
/************************************************************************/
void GDALRegister_PALSARJaxa()
{
if (GDALGetDriverByName("JAXAPALSAR") != nullptr)
return;
GDALDriver *poDriver = new GDALDriver();
poDriver->SetDescription("JAXAPALSAR");
poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
"JAXA PALSAR Product Reader (Level 1.1/1.5)");
poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/palsar.html");
poDriver->pfnOpen = PALSARJaxaDataset::Open;
poDriver->pfnIdentify = PALSARJaxaDataset::Identify;
poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
GetGDALDriverManager()->RegisterDriver(poDriver);
}