forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdalinfo_lib.cpp
2348 lines (2104 loc) · 90.3 KB
/
gdalinfo_lib.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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: GDAL Utilities
* Purpose: Command line application to list info about a file.
* Author: Frank Warmerdam, [email protected]
*
* ****************************************************************************
* Copyright (c) 1998, Frank Warmerdam
* Copyright (c) 2007-2015, Even Rouault <even.rouault at spatialys.com>
* Copyright (c) 2015, Faza Mahamood
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_utils.h"
#include "gdal_utils_priv.h"
#include "gdalargumentparser.h"
#include <cmath>
#include <limits>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <new>
#include <string>
#include <vector>
#include "commonutils.h"
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_json_header.h"
#include "cpl_minixml.h"
#include "cpl_progress.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "gdal.h"
#include "gdal_alg.h"
#include "gdal_priv.h"
#include "gdal_rat.h"
#include "ogr_api.h"
#include "ogr_srs_api.h"
#include "ogr_spatialref.h"
#include "ogrlibjsonutils.h"
#include "ogrgeojsongeometry.h"
#include "ogrgeojsonwriter.h"
using std::vector;
/*! output format */
typedef enum
{
/*! output in text format */ GDALINFO_FORMAT_TEXT = 0,
/*! output in json format */ GDALINFO_FORMAT_JSON = 1
} GDALInfoFormat;
/************************************************************************/
/* GDALInfoOptions */
/************************************************************************/
/** Options for use with GDALInfo(). GDALInfoOptions* must be allocated and
* freed with GDALInfoOptionsNew() and GDALInfoOptionsFree() respectively.
*/
struct GDALInfoOptions
{
/*! output format */
GDALInfoFormat eFormat = GDALINFO_FORMAT_TEXT;
bool bComputeMinMax = false;
/*! report histogram information for all bands */
bool bReportHistograms = false;
/*! report a PROJ.4 string corresponding to the file's coordinate system */
bool bReportProj4 = false;
/*! read and display image statistics. Force computation if no statistics
are stored in an image */
bool bStats = false;
/*! read and display image statistics. Force computation if no statistics
are stored in an image. However, they may be computed based on
overviews or a subset of all tiles. Useful if you are in a hurry and
don't want precise stats. */
bool bApproxStats = true;
bool bSample = false;
/*! force computation of the checksum for each band in the dataset */
bool bComputeChecksum = false;
/*! allow or suppress printing of nodata value */
bool bShowNodata = true;
/*! allow or suppress printing of mask information */
bool bShowMask = true;
/*! allow or suppress ground control points list printing. It may be useful
for datasets with huge amount of GCPs, such as L1B AVHRR or HDF4 MODIS
which contain thousands of them. */
bool bShowGCPs = true;
/*! allow or suppress metadata printing. Some datasets may contain a lot of
metadata strings. */
bool bShowMetadata = true;
/*! allow or suppress printing of raster attribute table */
bool bShowRAT = true;
/*! allow or suppress printing of color table */
bool bShowColorTable = true;
/*! list all metadata domains available for the dataset */
bool bListMDD = false;
/*! display the file list or the first file of the file list */
bool bShowFileList = true;
/*! report metadata for the specified domains. "all" can be used to report
metadata in all domains.
*/
CPLStringList aosExtraMDDomains{};
/*! WKT format used for SRS */
std::string osWKTFormat = "WKT2";
bool bStdoutOutput = false;
};
static int GDALInfoReportCorner(const GDALInfoOptions *psOptions,
GDALDatasetH hDataset,
OGRCoordinateTransformationH hTransform,
const char *corner_name, double x, double y,
bool bJson, json_object *poCornerCoordinates,
json_object *poLongLatExtentCoordinates,
CPLString &osStr);
static void GDALInfoReportMetadata(const GDALInfoOptions *psOptions,
GDALMajorObjectH hObject, bool bIsBand,
bool bJson, json_object *poMetadata,
CPLString &osStr);
#ifndef Concat_defined
#define Concat_defined
static void Concat(CPLString &osRet, bool bStdoutOutput, const char *pszFormat,
...) CPL_PRINT_FUNC_FORMAT(3, 4);
static void Concat(CPLString &osRet, bool bStdoutOutput, const char *pszFormat,
...)
{
va_list args;
va_start(args, pszFormat);
if (bStdoutOutput)
{
vfprintf(stdout, pszFormat, args);
}
else
{
try
{
CPLString osTarget;
osTarget.vPrintf(pszFormat, args);
osRet += osTarget;
}
catch (const std::bad_alloc &)
{
CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
}
}
va_end(args);
}
#endif
/************************************************************************/
/* gdal_json_object_new_double_or_str_for_non_finite() */
/************************************************************************/
static json_object *
gdal_json_object_new_double_or_str_for_non_finite(double dfVal, int nPrecision)
{
if (std::isinf(dfVal))
return json_object_new_string(dfVal < 0 ? "-Infinity" : "Infinity");
else if (std::isnan(dfVal))
return json_object_new_string("NaN");
else
return json_object_new_double_with_precision(dfVal, nPrecision);
}
/************************************************************************/
/* gdal_json_object_new_double_significant_digits() */
/************************************************************************/
static json_object *
gdal_json_object_new_double_significant_digits(double dfVal,
int nSignificantDigits)
{
if (std::isinf(dfVal))
return json_object_new_string(dfVal < 0 ? "-Infinity" : "Infinity");
else if (std::isnan(dfVal))
return json_object_new_string("NaN");
else
return json_object_new_double_with_significant_figures(
dfVal, nSignificantDigits);
}
/************************************************************************/
/* GDALWarpAppOptionsGetParser() */
/************************************************************************/
static std::unique_ptr<GDALArgumentParser>
GDALInfoAppOptionsGetParser(GDALInfoOptions *psOptions,
GDALInfoOptionsForBinary *psOptionsForBinary)
{
auto argParser = std::make_unique<GDALArgumentParser>(
"gdalinfo", /* bForBinary=*/psOptionsForBinary != nullptr);
argParser->add_description(_("Raster dataset information utility."));
argParser->add_epilog(
_("For more details, consult https://gdal.org/programs/gdalinfo.html"));
argParser->add_argument("-json")
.flag()
.action([psOptions](const auto &)
{ psOptions->eFormat = GDALINFO_FORMAT_JSON; })
.help(_("Display the output in json format."));
argParser->add_argument("-mm")
.store_into(psOptions->bComputeMinMax)
.help(_("Force computation of the actual min/max values for each band "
"in the dataset."));
{
auto &group = argParser->add_mutually_exclusive_group();
group.add_argument("-stats")
.store_into(psOptions->bStats)
.help(_("Read and display image statistics computing exact values "
"if required."));
group.add_argument("-approx_stats")
.store_into(psOptions->bApproxStats)
.help(
_("Read and display image statistics computing approximated "
"values on overviews or a subset of all tiles if required."));
}
argParser->add_argument("-hist")
.store_into(psOptions->bReportHistograms)
.help(_("Report histogram information for all bands."));
argParser->add_usage_newline();
argParser->add_inverted_logic_flag(
"-nogcp", &psOptions->bShowGCPs,
_("Suppress ground control points list printing."));
argParser->add_inverted_logic_flag("-nomd", &psOptions->bShowMetadata,
_("Suppress metadata printing."));
argParser->add_inverted_logic_flag(
"-norat", &psOptions->bShowRAT,
_("Suppress printing of raster attribute table."));
argParser->add_inverted_logic_flag("-noct", &psOptions->bShowColorTable,
_("Suppress printing of color table."));
argParser->add_inverted_logic_flag("-nofl", &psOptions->bShowFileList,
_("Suppress display of the file list."));
argParser->add_inverted_logic_flag(
"-nonodata", &psOptions->bShowNodata,
_("Suppress nodata printing (implies -nomask)."));
argParser->add_inverted_logic_flag("-nomask", &psOptions->bShowMask,
_("Suppress mask printing."));
argParser->add_usage_newline();
argParser->add_argument("-checksum")
.flag()
.store_into(psOptions->bComputeChecksum)
.help(_(
"Force computation of the checksum for each band in the dataset."));
argParser->add_argument("-listmdd")
.flag()
.store_into(psOptions->bListMDD)
.help(_("List all metadata domains available for the dataset."));
argParser->add_argument("-proj4")
.flag()
.store_into(psOptions->bReportProj4)
.help(_("Report a PROJ.4 string corresponding to the file's coordinate "
"system."));
argParser->add_argument("-wkt_format")
.metavar("<WKT1|WKT2|WKT2_2015|WKT2_2018|WKT2_2019>")
.choices("WKT1", "WKT2", "WKT2_2015", "WKT2_2018", "WKT2_2019")
.store_into(psOptions->osWKTFormat)
.help(_("WKT format used for SRS."));
if (psOptionsForBinary)
{
argParser->add_argument("-sd")
.metavar("<n>")
.store_into(psOptionsForBinary->nSubdataset)
.help(_(
"Use subdataset of specified index (starting at 1), instead of "
"the source dataset itself."));
}
argParser->add_argument("-oo")
.metavar("<NAME>=<VALUE>")
.append()
.action(
[psOptionsForBinary](const std::string &s)
{
if (psOptionsForBinary)
psOptionsForBinary->aosOpenOptions.AddString(s.c_str());
})
.help(_("Open option(s) for dataset."));
argParser->add_input_format_argument(
psOptionsForBinary ? &psOptionsForBinary->aosAllowedInputDrivers
: nullptr);
argParser->add_argument("-mdd")
.metavar("<domain>|all")
.action(
[psOptions](const std::string &value)
{
psOptions->aosExtraMDDomains =
CSLAddString(psOptions->aosExtraMDDomains, value.c_str());
})
.help(_("Report metadata for the specified domains. 'all' can be used "
"to report metadata in all domains."));
/* Not documented: used by gdalinfo_bin.cpp only */
argParser->add_argument("-stdout").flag().hidden().store_into(
psOptions->bStdoutOutput);
if (psOptionsForBinary)
{
argParser->add_argument("dataset_name")
.metavar("<dataset_name>")
.store_into(psOptionsForBinary->osFilename)
.help("Input dataset.");
}
return argParser;
}
/************************************************************************/
/* GDALInfoAppGetParserUsage() */
/************************************************************************/
std::string GDALInfoAppGetParserUsage()
{
try
{
GDALInfoOptions sOptions;
GDALInfoOptionsForBinary sOptionsForBinary;
auto argParser =
GDALInfoAppOptionsGetParser(&sOptions, &sOptionsForBinary);
return argParser->usage();
}
catch (const std::exception &err)
{
CPLError(CE_Failure, CPLE_AppDefined, "Unexpected exception: %s",
err.what());
return std::string();
}
}
/************************************************************************/
/* GDALInfo() */
/************************************************************************/
/**
* Lists various information about a GDAL supported raster dataset.
*
* This is the equivalent of the <a href="/programs/gdalinfo.html">gdalinfo</a>
* utility.
*
* GDALInfoOptions* must be allocated and freed with GDALInfoOptionsNew()
* and GDALInfoOptionsFree() respectively.
*
* @param hDataset the dataset handle.
* @param psOptions the options structure returned by GDALInfoOptionsNew() or
* NULL.
* @return string corresponding to the information about the raster dataset
* (must be freed with CPLFree()), or NULL in case of error.
*
* @since GDAL 2.1
*/
char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions)
{
if (hDataset == nullptr)
return nullptr;
GDALInfoOptions *psOptionsToFree = nullptr;
if (psOptions == nullptr)
{
psOptionsToFree = GDALInfoOptionsNew(nullptr, nullptr);
psOptions = psOptionsToFree;
}
CPLString osStr;
json_object *poJsonObject = nullptr;
json_object *poBands = nullptr;
json_object *poMetadata = nullptr;
json_object *poStac = nullptr;
json_object *poStacRasterBands = nullptr;
json_object *poStacEOBands = nullptr;
const bool bJson = psOptions->eFormat == GDALINFO_FORMAT_JSON;
/* -------------------------------------------------------------------- */
/* Report general info. */
/* -------------------------------------------------------------------- */
GDALDriverH hDriver = GDALGetDatasetDriver(hDataset);
if (bJson)
{
json_object *poDescription =
json_object_new_string(GDALGetDescription(hDataset));
json_object *poDriverShortName =
json_object_new_string(GDALGetDriverShortName(hDriver));
json_object *poDriverLongName =
json_object_new_string(GDALGetDriverLongName(hDriver));
poJsonObject = json_object_new_object();
poBands = json_object_new_array();
poMetadata = json_object_new_object();
poStac = json_object_new_object();
poStacRasterBands = json_object_new_array();
poStacEOBands = json_object_new_array();
json_object_object_add(poJsonObject, "description", poDescription);
json_object_object_add(poJsonObject, "driverShortName",
poDriverShortName);
json_object_object_add(poJsonObject, "driverLongName",
poDriverLongName);
}
else
{
Concat(osStr, psOptions->bStdoutOutput, "Driver: %s/%s\n",
GDALGetDriverShortName(hDriver), GDALGetDriverLongName(hDriver));
}
if (psOptions->bShowFileList)
{
// The list of files of a raster FileGDB is not super useful and potentially
// super long, so omit it, unless the -json mode is enabled
char **papszFileList =
(!bJson && EQUAL(GDALGetDriverShortName(hDriver), "OpenFileGDB"))
? nullptr
: GDALGetFileList(hDataset);
if (!papszFileList || *papszFileList == nullptr)
{
if (bJson)
{
json_object *poFiles = json_object_new_array();
json_object_object_add(poJsonObject, "files", poFiles);
}
else
{
Concat(osStr, psOptions->bStdoutOutput,
"Files: none associated\n");
}
}
else
{
if (bJson)
{
json_object *poFiles = json_object_new_array();
for (int i = 0; papszFileList[i] != nullptr; i++)
{
json_object *poFile =
json_object_new_string(papszFileList[i]);
json_object_array_add(poFiles, poFile);
}
json_object_object_add(poJsonObject, "files", poFiles);
}
else
{
Concat(osStr, psOptions->bStdoutOutput, "Files: %s\n",
papszFileList[0]);
for (int i = 1; papszFileList[i] != nullptr; i++)
Concat(osStr, psOptions->bStdoutOutput, " %s\n",
papszFileList[i]);
}
}
CSLDestroy(papszFileList);
}
if (bJson)
{
{
json_object *poSize = json_object_new_array();
json_object *poSizeX =
json_object_new_int(GDALGetRasterXSize(hDataset));
json_object *poSizeY =
json_object_new_int(GDALGetRasterYSize(hDataset));
// size is X, Y ordered
json_object_array_add(poSize, poSizeX);
json_object_array_add(poSize, poSizeY);
json_object_object_add(poJsonObject, "size", poSize);
}
{
json_object *poStacSize = json_object_new_array();
json_object *poSizeX =
json_object_new_int(GDALGetRasterXSize(hDataset));
json_object *poSizeY =
json_object_new_int(GDALGetRasterYSize(hDataset));
// ... but ... proj:shape is Y, X ordered.
json_object_array_add(poStacSize, poSizeY);
json_object_array_add(poStacSize, poSizeX);
json_object_object_add(poStac, "proj:shape", poStacSize);
}
}
else
{
Concat(osStr, psOptions->bStdoutOutput, "Size is %d, %d\n",
GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset));
}
CPLString osWKTFormat("FORMAT=");
osWKTFormat += psOptions->osWKTFormat;
const char *const apszWKTOptions[] = {osWKTFormat.c_str(), "MULTILINE=YES",
nullptr};
/* -------------------------------------------------------------------- */
/* Report projection. */
/* -------------------------------------------------------------------- */
auto hSRS = GDALGetSpatialRef(hDataset);
if (hSRS != nullptr)
{
json_object *poCoordinateSystem = nullptr;
if (bJson)
poCoordinateSystem = json_object_new_object();
char *pszPrettyWkt = nullptr;
OSRExportToWktEx(hSRS, &pszPrettyWkt, apszWKTOptions);
int nAxesCount = 0;
const int *panAxes = OSRGetDataAxisToSRSAxisMapping(hSRS, &nAxesCount);
const double dfCoordinateEpoch = OSRGetCoordinateEpoch(hSRS);
if (bJson)
{
json_object *poWkt = json_object_new_string(pszPrettyWkt);
if (psOptions->osWKTFormat == "WKT2")
{
json_object *poStacWkt = nullptr;
json_object_deep_copy(poWkt, &poStacWkt, nullptr);
json_object_object_add(poStac, "proj:wkt2", poStacWkt);
}
json_object_object_add(poCoordinateSystem, "wkt", poWkt);
const char *pszAuthCode = OSRGetAuthorityCode(hSRS, nullptr);
const char *pszAuthName = OSRGetAuthorityName(hSRS, nullptr);
if (pszAuthCode && pszAuthName && EQUAL(pszAuthName, "EPSG"))
{
json_object *poEPSG = json_object_new_int64(atoi(pszAuthCode));
json_object_object_add(poStac, "proj:epsg", poEPSG);
}
else
{
// Setting it to null is mandated by the
// https://github.com/stac-extensions/projection#projepsg
// when setting proj:projjson or proj:wkt2
json_object_object_add(poStac, "proj:epsg", nullptr);
}
{
// PROJJSON requires PROJ >= 6.2
CPLErrorStateBackuper oCPLErrorHandlerPusher(
CPLQuietErrorHandler);
char *pszProjJson = nullptr;
OGRErr result =
OSRExportToPROJJSON(hSRS, &pszProjJson, nullptr);
if (result == OGRERR_NONE)
{
json_object *poStacProjJson =
json_tokener_parse(pszProjJson);
json_object_object_add(poStac, "proj:projjson",
poStacProjJson);
CPLFree(pszProjJson);
}
}
json_object *poAxisMapping = json_object_new_array();
for (int i = 0; i < nAxesCount; i++)
{
json_object_array_add(poAxisMapping,
json_object_new_int(panAxes[i]));
}
json_object_object_add(poCoordinateSystem,
"dataAxisToSRSAxisMapping", poAxisMapping);
if (dfCoordinateEpoch > 0)
{
json_object_object_add(
poJsonObject, "coordinateEpoch",
json_object_new_double(dfCoordinateEpoch));
}
}
else
{
Concat(osStr, psOptions->bStdoutOutput,
"Coordinate System is:\n%s\n", pszPrettyWkt);
Concat(osStr, psOptions->bStdoutOutput,
"Data axis to CRS axis mapping: ");
for (int i = 0; i < nAxesCount; i++)
{
if (i > 0)
{
Concat(osStr, psOptions->bStdoutOutput, ",");
}
Concat(osStr, psOptions->bStdoutOutput, "%d", panAxes[i]);
}
Concat(osStr, psOptions->bStdoutOutput, "\n");
if (dfCoordinateEpoch > 0)
{
std::string osCoordinateEpoch =
CPLSPrintf("%f", dfCoordinateEpoch);
const size_t nDotPos = osCoordinateEpoch.find('.');
if (nDotPos != std::string::npos)
{
while (osCoordinateEpoch.size() > nDotPos + 2 &&
osCoordinateEpoch.back() == '0')
osCoordinateEpoch.pop_back();
}
Concat(osStr, psOptions->bStdoutOutput,
"Coordinate epoch: %s\n", osCoordinateEpoch.c_str());
}
}
CPLFree(pszPrettyWkt);
if (psOptions->bReportProj4)
{
char *pszProj4 = nullptr;
OSRExportToProj4(hSRS, &pszProj4);
if (bJson)
{
json_object *proj4 = json_object_new_string(pszProj4);
json_object_object_add(poCoordinateSystem, "proj4", proj4);
}
else
Concat(osStr, psOptions->bStdoutOutput,
"PROJ.4 string is:\n\'%s\'\n", pszProj4);
CPLFree(pszProj4);
}
if (bJson)
json_object_object_add(poJsonObject, "coordinateSystem",
poCoordinateSystem);
}
/* -------------------------------------------------------------------- */
/* Report Geotransform. */
/* -------------------------------------------------------------------- */
double adfGeoTransform[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
if (GDALGetGeoTransform(hDataset, adfGeoTransform) == CE_None)
{
if (bJson)
{
json_object *poGeoTransform = json_object_new_array();
// Deep copy wasn't working on the array, for some reason, so we
// build the geotransform STAC array at the same time.
json_object *poStacGeoTransform = json_object_new_array();
for (int i = 0; i < 6; i++)
{
json_object *poGeoTransformCoefficient =
json_object_new_double_with_precision(adfGeoTransform[i],
16);
json_object *poStacGeoTransformCoefficient =
json_object_new_double_with_precision(adfGeoTransform[i],
16);
json_object_array_add(poGeoTransform,
poGeoTransformCoefficient);
json_object_array_add(poStacGeoTransform,
poStacGeoTransformCoefficient);
}
json_object_object_add(poJsonObject, "geoTransform",
poGeoTransform);
json_object_object_add(poStac, "proj:transform",
poStacGeoTransform);
}
else
{
if (adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0)
{
Concat(osStr, psOptions->bStdoutOutput,
"Origin = (%.15f,%.15f)\n", adfGeoTransform[0],
adfGeoTransform[3]);
Concat(osStr, psOptions->bStdoutOutput,
"Pixel Size = (%.15f,%.15f)\n", adfGeoTransform[1],
adfGeoTransform[5]);
}
else
{
Concat(osStr, psOptions->bStdoutOutput,
"GeoTransform =\n"
" %.16g, %.16g, %.16g\n"
" %.16g, %.16g, %.16g\n",
adfGeoTransform[0], adfGeoTransform[1],
adfGeoTransform[2], adfGeoTransform[3],
adfGeoTransform[4], adfGeoTransform[5]);
}
}
}
/* -------------------------------------------------------------------- */
/* Report GCPs. */
/* -------------------------------------------------------------------- */
if (psOptions->bShowGCPs && GDALGetGCPCount(hDataset) > 0)
{
json_object *const poGCPs = bJson ? json_object_new_object() : nullptr;
hSRS = GDALGetGCPSpatialRef(hDataset);
if (hSRS)
{
json_object *poGCPCoordinateSystem = nullptr;
char *pszPrettyWkt = nullptr;
int nAxesCount = 0;
const int *panAxes =
OSRGetDataAxisToSRSAxisMapping(hSRS, &nAxesCount);
OSRExportToWktEx(hSRS, &pszPrettyWkt, apszWKTOptions);
if (bJson)
{
json_object *poWkt = json_object_new_string(pszPrettyWkt);
poGCPCoordinateSystem = json_object_new_object();
json_object_object_add(poGCPCoordinateSystem, "wkt", poWkt);
json_object *poAxisMapping = json_object_new_array();
for (int i = 0; i < nAxesCount; i++)
{
json_object_array_add(poAxisMapping,
json_object_new_int(panAxes[i]));
}
json_object_object_add(poGCPCoordinateSystem,
"dataAxisToSRSAxisMapping",
poAxisMapping);
}
else
{
Concat(osStr, psOptions->bStdoutOutput,
"GCP Projection = \n%s\n", pszPrettyWkt);
Concat(osStr, psOptions->bStdoutOutput,
"Data axis to CRS axis mapping: ");
for (int i = 0; i < nAxesCount; i++)
{
if (i > 0)
{
Concat(osStr, psOptions->bStdoutOutput, ",");
}
Concat(osStr, psOptions->bStdoutOutput, "%d", panAxes[i]);
}
Concat(osStr, psOptions->bStdoutOutput, "\n");
}
CPLFree(pszPrettyWkt);
if (bJson)
json_object_object_add(poGCPs, "coordinateSystem",
poGCPCoordinateSystem);
}
json_object *const poGCPList =
bJson ? json_object_new_array() : nullptr;
for (int i = 0; i < GDALGetGCPCount(hDataset); i++)
{
const GDAL_GCP *psGCP = GDALGetGCPs(hDataset) + i;
if (bJson)
{
json_object *poGCP = json_object_new_object();
json_object *poId = json_object_new_string(psGCP->pszId);
json_object *poInfo = json_object_new_string(psGCP->pszInfo);
json_object *poPixel = json_object_new_double_with_precision(
psGCP->dfGCPPixel, 15);
json_object *poLine =
json_object_new_double_with_precision(psGCP->dfGCPLine, 15);
json_object *poX =
json_object_new_double_with_precision(psGCP->dfGCPX, 15);
json_object *poY =
json_object_new_double_with_precision(psGCP->dfGCPY, 15);
json_object *poZ =
json_object_new_double_with_precision(psGCP->dfGCPZ, 15);
json_object_object_add(poGCP, "id", poId);
json_object_object_add(poGCP, "info", poInfo);
json_object_object_add(poGCP, "pixel", poPixel);
json_object_object_add(poGCP, "line", poLine);
json_object_object_add(poGCP, "x", poX);
json_object_object_add(poGCP, "y", poY);
json_object_object_add(poGCP, "z", poZ);
json_object_array_add(poGCPList, poGCP);
}
else
{
Concat(osStr, psOptions->bStdoutOutput,
"GCP[%3d]: Id=%s, Info=%s\n"
" (%.15g,%.15g) -> (%.15g,%.15g,%.15g)\n",
i, psGCP->pszId, psGCP->pszInfo, psGCP->dfGCPPixel,
psGCP->dfGCPLine, psGCP->dfGCPX, psGCP->dfGCPY,
psGCP->dfGCPZ);
}
}
if (bJson)
{
json_object_object_add(poGCPs, "gcpList", poGCPList);
json_object_object_add(poJsonObject, "gcps", poGCPs);
}
}
/* -------------------------------------------------------------------- */
/* Report metadata. */
/* -------------------------------------------------------------------- */
GDALInfoReportMetadata(psOptions, hDataset, false, bJson, poMetadata,
osStr);
if (bJson)
{
if (psOptions->bShowMetadata)
json_object_object_add(poJsonObject, "metadata", poMetadata);
else
json_object_put(poMetadata);
// Include eo:cloud_cover in stac output
const char *pszCloudCover =
GDALGetMetadataItem(hDataset, "CLOUDCOVER", "IMAGERY");
json_object *poValue = nullptr;
if (pszCloudCover)
{
poValue = json_object_new_int(atoi(pszCloudCover));
json_object_object_add(poStac, "eo:cloud_cover", poValue);
}
}
/* -------------------------------------------------------------------- */
/* Setup projected to lat/long transform if appropriate. */
/* -------------------------------------------------------------------- */
OGRSpatialReferenceH hProj = nullptr;
if (GDALGetGeoTransform(hDataset, adfGeoTransform) == CE_None)
hProj = GDALGetSpatialRef(hDataset);
OGRCoordinateTransformationH hTransform = nullptr;
bool bTransformToWGS84 = false;
if (hProj)
{
OGRSpatialReferenceH hLatLong = nullptr;
if (bJson)
{
// Check that it looks like Earth before trying to reproject to wgs84...
// OSRGetSemiMajor() may raise an error on CRS like Engineering CRS
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
OGRErr eErr = OGRERR_NONE;
if (fabs(OSRGetSemiMajor(hProj, &eErr) - 6378137.0) < 10000.0 &&
eErr == OGRERR_NONE)
{
bTransformToWGS84 = true;
hLatLong = OSRNewSpatialReference(nullptr);
OSRSetWellKnownGeogCS(hLatLong, "WGS84");
}
}
else
{
hLatLong = OSRCloneGeogCS(hProj);
if (hLatLong)
{
// Override GEOGCS|UNIT child to be sure to output as degrees
OSRSetAngularUnits(hLatLong, SRS_UA_DEGREE,
CPLAtof(SRS_UA_DEGREE_CONV));
}
}
if (hLatLong != nullptr)
{
OSRSetAxisMappingStrategy(hLatLong, OAMS_TRADITIONAL_GIS_ORDER);
CPLPushErrorHandler(CPLQuietErrorHandler);
hTransform = OCTNewCoordinateTransformation(hProj, hLatLong);
CPLPopErrorHandler();
OSRDestroySpatialReference(hLatLong);
}
}
/* -------------------------------------------------------------------- */
/* Report corners. */
/* -------------------------------------------------------------------- */
if (bJson && GDALGetRasterXSize(hDataset))
{
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
json_object *poLinearRing = json_object_new_array();
json_object *poCornerCoordinates = json_object_new_object();
json_object *poLongLatExtent = json_object_new_object();
json_object *poLongLatExtentType = json_object_new_string("Polygon");
json_object *poLongLatExtentCoordinates = json_object_new_array();
GDALInfoReportCorner(psOptions, hDataset, hTransform, "upperLeft", 0.0,
0.0, bJson, poCornerCoordinates,
poLongLatExtentCoordinates, osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "lowerLeft", 0.0,
GDALGetRasterYSize(hDataset), bJson,
poCornerCoordinates, poLongLatExtentCoordinates,
osStr);
GDALInfoReportCorner(
psOptions, hDataset, hTransform, "lowerRight",
GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset), bJson,
poCornerCoordinates, poLongLatExtentCoordinates, osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "upperRight",
GDALGetRasterXSize(hDataset), 0.0, bJson,
poCornerCoordinates, poLongLatExtentCoordinates,
osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "center",
GDALGetRasterXSize(hDataset) / 2.0,
GDALGetRasterYSize(hDataset) / 2.0, bJson,
poCornerCoordinates, poLongLatExtentCoordinates,
osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "upperLeft", 0.0,
0.0, bJson, poCornerCoordinates,
poLongLatExtentCoordinates, osStr);
json_object_object_add(poJsonObject, "cornerCoordinates",
poCornerCoordinates);
json_object_object_add(poLongLatExtent, "type", poLongLatExtentType);
json_object_array_add(poLinearRing, poLongLatExtentCoordinates);
json_object_object_add(poLongLatExtent, "coordinates", poLinearRing);
json_object_object_add(poJsonObject,
bTransformToWGS84 ? "wgs84Extent" : "extent",
poLongLatExtent);
}
else if (GDALGetRasterXSize(hDataset))
{
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
Concat(osStr, psOptions->bStdoutOutput, "Corner Coordinates:\n");
GDALInfoReportCorner(psOptions, hDataset, hTransform, "Upper Left", 0.0,
0.0, bJson, nullptr, nullptr, osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "Lower Left", 0.0,
GDALGetRasterYSize(hDataset), bJson, nullptr,
nullptr, osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "Upper Right",
GDALGetRasterXSize(hDataset), 0.0, bJson, nullptr,
nullptr, osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "Lower Right",
GDALGetRasterXSize(hDataset),
GDALGetRasterYSize(hDataset), bJson, nullptr,
nullptr, osStr);
GDALInfoReportCorner(psOptions, hDataset, hTransform, "Center",
GDALGetRasterXSize(hDataset) / 2.0,
GDALGetRasterYSize(hDataset) / 2.0, bJson, nullptr,
nullptr, osStr);
}
if (hTransform != nullptr)
{
OCTDestroyCoordinateTransformation(hTransform);
hTransform = nullptr;
}
/* ==================================================================== */
/* Loop over bands. */
/* ==================================================================== */
for (int iBand = 0; iBand < GDALGetRasterCount(hDataset); iBand++)
{
json_object *poBand = nullptr;
json_object *poBandMetadata = nullptr;