Coverage Report

Created: 2026-04-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/ogrinfo_lib.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  Simple client for viewing OGR driver data.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1999, Frank Warmerdam
9
 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
#include "cpl_json.h"
16
#include "ogrlibjsonutils.h"
17
#include "cpl_string.h"
18
#include "gdal_utils.h"
19
#include "gdal_utils_priv.h"
20
#include "gdal_priv.h"
21
#include "ogr_feature.h"
22
#include "ogrsf_frmts.h"
23
#include "ogr_geometry.h"
24
#include "commonutils.h"
25
#include "gdalargumentparser.h"
26
27
#include <cmath>
28
#include <set>
29
30
/*! output format */
31
typedef enum
32
{
33
    /*! output in text format */ FORMAT_TEXT = 0,
34
    /*! output in json format */ FORMAT_JSON = 1
35
} GDALVectorInfoFormat;
36
37
struct GDALVectorInfoOptions
38
{
39
    GDALVectorInfoFormat eFormat = FORMAT_TEXT;
40
    std::string osWHERE{};
41
    CPLStringList aosLayers{};
42
    std::unique_ptr<OGRGeometry> poSpatialFilter{};
43
    bool bAllLayers = false;
44
    std::string osSQLStatement{};
45
    std::string osDialect{};
46
    std::string osGeomField{};
47
    CPLStringList aosExtraMDDomains{};
48
    bool bListMDD = false;
49
    bool bShowMetadata = true;
50
    bool bFeatureCount = true;
51
    bool bExtent = true;
52
    bool bExtent3D = false;
53
    bool bGeomType = true;
54
    bool bDatasetGetNextFeature = false;
55
    bool bVerbose = true;
56
    bool bSuperQuiet = false;
57
    bool bSummaryOnly = false;
58
    GIntBig nFetchFID = OGRNullFID;
59
    std::string osWKTFormat = "WKT2";
60
    std::string osFieldDomain{};
61
    CPLStringList aosOptions{};
62
    bool bStdoutOutput = false;  // only set by ogrinfo_bin
63
    int nRepeatCount = 1;
64
65
    /*! Maximum number of features, or -1 if no limit. */
66
    GIntBig nLimit = -1;
67
68
    // Only used during argument parsing
69
    bool bSummaryUserRequested = false;
70
    bool bFeaturesUserRequested = false;
71
72
    // Set by gdal vector info
73
    bool bIsCli = false;
74
75
    // Select the OGR_SCHEMA export
76
    bool bExportOgrSchema = false;
77
};
78
79
/************************************************************************/
80
/*                     GDALVectorInfoOptionsFree()                      */
81
/************************************************************************/
82
83
/**
84
 * Frees the GDALVectorInfoOptions struct.
85
 *
86
 * @param psOptions the options struct for GDALVectorInfo().
87
 *
88
 * @since GDAL 3.7
89
 */
90
91
void GDALVectorInfoOptionsFree(GDALVectorInfoOptions *psOptions)
92
0
{
93
0
    delete psOptions;
94
0
}
95
96
/************************************************************************/
97
/*                               Concat()                               */
98
/************************************************************************/
99
100
#ifndef Concat_defined
101
#define Concat_defined
102
static void Concat(CPLString &osRet, bool bStdoutOutput, const char *pszFormat,
103
                   ...) CPL_PRINT_FUNC_FORMAT(3, 4);
104
105
static void Concat(CPLString &osRet, bool bStdoutOutput, const char *pszFormat,
106
                   ...)
107
0
{
108
0
    va_list args;
109
0
    va_start(args, pszFormat);
110
111
0
    if (bStdoutOutput)
112
0
    {
113
0
        vfprintf(stdout, pszFormat, args);
114
0
    }
115
0
    else
116
0
    {
117
0
        try
118
0
        {
119
0
            CPLString osTarget;
120
0
            osTarget.vPrintf(pszFormat, args);
121
122
0
            osRet += osTarget;
123
0
        }
124
0
        catch (const std::bad_alloc &)
125
0
        {
126
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
127
0
        }
128
0
    }
129
130
0
    va_end(args);
131
0
}
132
#endif
133
134
static void ConcatStr(CPLString &osRet, bool bStdoutOutput, const char *pszStr)
135
0
{
136
0
    if (bStdoutOutput)
137
0
        fwrite(pszStr, 1, strlen(pszStr), stdout);
138
0
    else
139
0
        osRet += pszStr;
140
0
}
141
142
/************************************************************************/
143
/*                         ReportFieldDomain()                          */
144
/************************************************************************/
145
146
static void ReportFieldDomain(CPLString &osRet, CPLJSONObject &oDomains,
147
                              const GDALVectorInfoOptions *psOptions,
148
                              const OGRFieldDomain *poDomain)
149
0
{
150
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
151
0
    CPLJSONObject oDomain;
152
0
    oDomains.Add(poDomain->GetName(), oDomain);
153
0
    Concat(osRet, psOptions->bStdoutOutput, "Domain %s:\n",
154
0
           poDomain->GetName().c_str());
155
0
    const std::string &osDesc = poDomain->GetDescription();
156
0
    if (!osDesc.empty())
157
0
    {
158
0
        if (bJson)
159
0
            oDomain.Set("description", osDesc);
160
0
        else
161
0
            Concat(osRet, psOptions->bStdoutOutput, "  Description: %s\n",
162
0
                   osDesc.c_str());
163
0
    }
164
0
    const char *pszType = "";
165
0
    CPL_IGNORE_RET_VAL(pszType);  // Make CSA happy
166
0
    switch (poDomain->GetDomainType())
167
0
    {
168
0
        case OFDT_CODED:
169
0
            pszType = "coded";
170
0
            break;
171
0
        case OFDT_RANGE:
172
0
            pszType = "range";
173
0
            break;
174
0
        case OFDT_GLOB:
175
0
            pszType = "glob";
176
0
            break;
177
0
    }
178
0
    if (bJson)
179
0
    {
180
0
        oDomain.Set("type", pszType);
181
0
    }
182
0
    else
183
0
    {
184
0
        Concat(osRet, psOptions->bStdoutOutput, "  Type: %s\n", pszType);
185
0
    }
186
0
    const char *pszFieldType =
187
0
        OGRFieldDefn::GetFieldTypeName(poDomain->GetFieldType());
188
0
    const char *pszFieldSubType =
189
0
        OGRFieldDefn::GetFieldSubTypeName(poDomain->GetFieldSubType());
190
0
    if (bJson)
191
0
    {
192
0
        oDomain.Set("fieldType", pszFieldType);
193
0
        if (poDomain->GetFieldSubType() != OFSTNone)
194
0
            oDomain.Set("fieldSubType", pszFieldSubType);
195
0
    }
196
0
    else
197
0
    {
198
0
        const char *pszFieldTypeDisplay =
199
0
            (poDomain->GetFieldSubType() != OFSTNone)
200
0
                ? CPLSPrintf("%s(%s)", pszFieldType, pszFieldSubType)
201
0
                : pszFieldType;
202
0
        Concat(osRet, psOptions->bStdoutOutput, "  Field type: %s\n",
203
0
               pszFieldTypeDisplay);
204
0
    }
205
206
0
    const char *pszSplitPolicy = "";
207
0
    CPL_IGNORE_RET_VAL(pszSplitPolicy);  // Make CSA happy
208
0
    switch (poDomain->GetSplitPolicy())
209
0
    {
210
0
        case OFDSP_DEFAULT_VALUE:
211
0
            pszSplitPolicy = "default value";
212
0
            break;
213
0
        case OFDSP_DUPLICATE:
214
0
            pszSplitPolicy = "duplicate";
215
0
            break;
216
0
        case OFDSP_GEOMETRY_RATIO:
217
0
            pszSplitPolicy = "geometry ratio";
218
0
            break;
219
0
    }
220
0
    if (bJson)
221
0
    {
222
0
        oDomain.Set("splitPolicy", pszSplitPolicy);
223
0
    }
224
0
    else
225
0
    {
226
0
        Concat(osRet, psOptions->bStdoutOutput, "  Split policy: %s\n",
227
0
               pszSplitPolicy);
228
0
    }
229
230
0
    const char *pszMergePolicy = "";
231
0
    CPL_IGNORE_RET_VAL(pszMergePolicy);  // Make CSA happy
232
0
    switch (poDomain->GetMergePolicy())
233
0
    {
234
0
        case OFDMP_DEFAULT_VALUE:
235
0
            pszMergePolicy = "default value";
236
0
            break;
237
0
        case OFDMP_SUM:
238
0
            pszMergePolicy = "sum";
239
0
            break;
240
0
        case OFDMP_GEOMETRY_WEIGHTED:
241
0
            pszMergePolicy = "geometry weighted";
242
0
            break;
243
0
    }
244
0
    if (bJson)
245
0
    {
246
0
        oDomain.Set("mergePolicy", pszMergePolicy);
247
0
    }
248
0
    else
249
0
    {
250
0
        Concat(osRet, psOptions->bStdoutOutput, "  Merge policy: %s\n",
251
0
               pszMergePolicy);
252
0
    }
253
254
0
    switch (poDomain->GetDomainType())
255
0
    {
256
0
        case OFDT_CODED:
257
0
        {
258
0
            const auto poCodedFieldDomain =
259
0
                cpl::down_cast<const OGRCodedFieldDomain *>(poDomain);
260
0
            const OGRCodedValue *enumeration =
261
0
                poCodedFieldDomain->GetEnumeration();
262
0
            if (!bJson)
263
0
                Concat(osRet, psOptions->bStdoutOutput, "  Coded values:\n");
264
0
            CPLJSONObject oCodedValues;
265
0
            oDomain.Add("codedValues", oCodedValues);
266
0
            for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
267
0
            {
268
0
                if (enumeration[i].pszValue)
269
0
                {
270
0
                    if (bJson)
271
0
                    {
272
0
                        oCodedValues.Set(enumeration[i].pszCode,
273
0
                                         enumeration[i].pszValue);
274
0
                    }
275
0
                    else
276
0
                    {
277
0
                        Concat(osRet, psOptions->bStdoutOutput, "    %s: %s\n",
278
0
                               enumeration[i].pszCode, enumeration[i].pszValue);
279
0
                    }
280
0
                }
281
0
                else
282
0
                {
283
0
                    if (bJson)
284
0
                    {
285
0
                        oCodedValues.SetNull(enumeration[i].pszCode);
286
0
                    }
287
0
                    else
288
0
                    {
289
0
                        Concat(osRet, psOptions->bStdoutOutput, "    %s\n",
290
0
                               enumeration[i].pszCode);
291
0
                    }
292
0
                }
293
0
            }
294
0
            break;
295
0
        }
296
297
0
        case OFDT_RANGE:
298
0
        {
299
0
            const auto poRangeFieldDomain =
300
0
                cpl::down_cast<const OGRRangeFieldDomain *>(poDomain);
301
0
            bool bMinIsIncluded = false;
302
0
            const OGRField &sMin = poRangeFieldDomain->GetMin(bMinIsIncluded);
303
0
            bool bMaxIsIncluded = false;
304
0
            const OGRField &sMax = poRangeFieldDomain->GetMax(bMaxIsIncluded);
305
0
            if (poDomain->GetFieldType() == OFTInteger)
306
0
            {
307
0
                if (!OGR_RawField_IsUnset(&sMin))
308
0
                {
309
0
                    if (bJson)
310
0
                    {
311
0
                        oDomain.Set("minValue", sMin.Integer);
312
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
313
0
                    }
314
0
                    else
315
0
                    {
316
0
                        Concat(osRet, psOptions->bStdoutOutput,
317
0
                               "  Minimum value: %d%s\n", sMin.Integer,
318
0
                               bMinIsIncluded ? "" : " (excluded)");
319
0
                    }
320
0
                }
321
0
                if (!OGR_RawField_IsUnset(&sMax))
322
0
                {
323
0
                    if (bJson)
324
0
                    {
325
0
                        oDomain.Set("maxValue", sMax.Integer);
326
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
327
0
                    }
328
0
                    else
329
0
                    {
330
0
                        Concat(osRet, psOptions->bStdoutOutput,
331
0
                               "  Maximum value: %d%s\n", sMax.Integer,
332
0
                               bMaxIsIncluded ? "" : " (excluded)");
333
0
                    }
334
0
                }
335
0
            }
336
0
            else if (poDomain->GetFieldType() == OFTInteger64)
337
0
            {
338
0
                if (!OGR_RawField_IsUnset(&sMin))
339
0
                {
340
0
                    if (bJson)
341
0
                    {
342
0
                        oDomain.Set("minValue", sMin.Integer64);
343
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
344
0
                    }
345
0
                    else
346
0
                    {
347
0
                        Concat(osRet, psOptions->bStdoutOutput,
348
0
                               "  Minimum value: " CPL_FRMT_GIB "%s\n",
349
0
                               sMin.Integer64,
350
0
                               bMinIsIncluded ? "" : " (excluded)");
351
0
                    }
352
0
                }
353
0
                if (!OGR_RawField_IsUnset(&sMax))
354
0
                {
355
0
                    if (bJson)
356
0
                    {
357
0
                        oDomain.Set("maxValue", sMax.Integer64);
358
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
359
0
                    }
360
0
                    else
361
0
                    {
362
0
                        Concat(osRet, psOptions->bStdoutOutput,
363
0
                               "  Maximum value: " CPL_FRMT_GIB "%s\n",
364
0
                               sMax.Integer64,
365
0
                               bMaxIsIncluded ? "" : " (excluded)");
366
0
                    }
367
0
                }
368
0
            }
369
0
            else if (poDomain->GetFieldType() == OFTReal)
370
0
            {
371
0
                if (!OGR_RawField_IsUnset(&sMin))
372
0
                {
373
0
                    if (bJson)
374
0
                    {
375
0
                        oDomain.Set("minValue", sMin.Real);
376
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
377
0
                    }
378
0
                    else
379
0
                    {
380
0
                        Concat(osRet, psOptions->bStdoutOutput,
381
0
                               "  Minimum value: %g%s\n", sMin.Real,
382
0
                               bMinIsIncluded ? "" : " (excluded)");
383
0
                    }
384
0
                }
385
0
                if (!OGR_RawField_IsUnset(&sMax))
386
0
                {
387
0
                    if (bJson)
388
0
                    {
389
0
                        oDomain.Set("maxValue", sMax.Real);
390
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
391
0
                    }
392
0
                    else
393
0
                    {
394
0
                        Concat(osRet, psOptions->bStdoutOutput,
395
0
                               "  Maximum value: %g%s\n", sMax.Real,
396
0
                               bMaxIsIncluded ? "" : " (excluded)");
397
0
                    }
398
0
                }
399
0
            }
400
0
            else if (poDomain->GetFieldType() == OFTDateTime)
401
0
            {
402
0
                if (!OGR_RawField_IsUnset(&sMin))
403
0
                {
404
0
                    const char *pszVal = CPLSPrintf(
405
0
                        "%04d-%02d-%02dT%02d:%02d:%02d", sMin.Date.Year,
406
0
                        sMin.Date.Month, sMin.Date.Day, sMin.Date.Hour,
407
0
                        sMin.Date.Minute,
408
0
                        static_cast<int>(sMin.Date.Second + 0.5f));
409
0
                    if (bJson)
410
0
                    {
411
0
                        oDomain.Set("minValue", pszVal);
412
0
                        oDomain.Set("minValueIncluded", bMinIsIncluded);
413
0
                    }
414
0
                    else
415
0
                    {
416
0
                        Concat(osRet, psOptions->bStdoutOutput,
417
0
                               "  Minimum value: %s%s\n", pszVal,
418
0
                               bMinIsIncluded ? "" : " (excluded)");
419
0
                    }
420
0
                }
421
0
                if (!OGR_RawField_IsUnset(&sMax))
422
0
                {
423
0
                    const char *pszVal = CPLSPrintf(
424
0
                        "%04d-%02d-%02dT%02d:%02d:%02d", sMax.Date.Year,
425
0
                        sMax.Date.Month, sMax.Date.Day, sMax.Date.Hour,
426
0
                        sMax.Date.Minute,
427
0
                        static_cast<int>(sMax.Date.Second + 0.5f));
428
0
                    if (bJson)
429
0
                    {
430
0
                        oDomain.Set("maxValue", pszVal);
431
0
                        oDomain.Set("maxValueIncluded", bMaxIsIncluded);
432
0
                    }
433
0
                    else
434
0
                    {
435
0
                        Concat(osRet, psOptions->bStdoutOutput,
436
0
                               "  Maximum value: %s%s\n", pszVal,
437
0
                               bMaxIsIncluded ? "" : " (excluded)");
438
0
                    }
439
0
                }
440
0
            }
441
0
            break;
442
0
        }
443
444
0
        case OFDT_GLOB:
445
0
        {
446
0
            const auto poGlobFieldDomain =
447
0
                cpl::down_cast<const OGRGlobFieldDomain *>(poDomain);
448
0
            if (bJson)
449
0
                oDomain.Set("glob", poGlobFieldDomain->GetGlob());
450
0
            else
451
0
                Concat(osRet, psOptions->bStdoutOutput, "  Glob: %s\n",
452
0
                       poGlobFieldDomain->GetGlob().c_str());
453
0
            break;
454
0
        }
455
0
    }
456
0
}
457
458
/************************************************************************/
459
/*                        ReportRelationships()                         */
460
/************************************************************************/
461
462
static void ReportRelationships(CPLString &osRet, CPLJSONObject &oRoot,
463
                                const GDALVectorInfoOptions *psOptions,
464
                                const GDALDataset *poDS)
465
0
{
466
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
467
0
    CPLJSONObject oRelationships;
468
0
    if (bJson)
469
0
        oRoot.Add("relationships", oRelationships);
470
471
0
    const auto aosRelationshipNames = poDS->GetRelationshipNames();
472
0
    for (const std::string &osRelationshipName : aosRelationshipNames)
473
0
    {
474
0
        const auto poRelationship = poDS->GetRelationship(osRelationshipName);
475
0
        if (!poRelationship)
476
0
            continue;
477
478
0
        const char *pszType = "";
479
0
        CPL_IGNORE_RET_VAL(pszType);  // Make CSA happy
480
0
        switch (poRelationship->GetType())
481
0
        {
482
0
            case GRT_COMPOSITE:
483
0
                pszType = "Composite";
484
0
                break;
485
0
            case GRT_ASSOCIATION:
486
0
                pszType = "Association";
487
0
                break;
488
0
            case GRT_AGGREGATION:
489
0
                pszType = "Aggregation";
490
0
                break;
491
0
        }
492
493
0
        const char *pszCardinality = "";
494
0
        CPL_IGNORE_RET_VAL(pszCardinality);  // Make CSA happy
495
0
        switch (poRelationship->GetCardinality())
496
0
        {
497
0
            case GRC_ONE_TO_ONE:
498
0
                pszCardinality = "OneToOne";
499
0
                break;
500
0
            case GRC_ONE_TO_MANY:
501
0
                pszCardinality = "OneToMany";
502
0
                break;
503
0
            case GRC_MANY_TO_ONE:
504
0
                pszCardinality = "ManyToOne";
505
0
                break;
506
0
            case GRC_MANY_TO_MANY:
507
0
                pszCardinality = "ManyToMany";
508
0
                break;
509
0
        }
510
511
0
        const auto &aosLeftTableFields = poRelationship->GetLeftTableFields();
512
0
        const auto &aosRightTableFields = poRelationship->GetRightTableFields();
513
0
        const auto &osMappingTableName = poRelationship->GetMappingTableName();
514
0
        const auto &aosLeftMappingTableFields =
515
0
            poRelationship->GetLeftMappingTableFields();
516
0
        const auto &aosRightMappingTableFields =
517
0
            poRelationship->GetRightMappingTableFields();
518
519
0
        if (bJson)
520
0
        {
521
0
            CPLJSONObject oRelationship;
522
0
            oRelationships.Add(osRelationshipName, oRelationship);
523
524
0
            oRelationship.Add("type", pszType);
525
0
            oRelationship.Add("related_table_type",
526
0
                              poRelationship->GetRelatedTableType());
527
0
            oRelationship.Add("cardinality", pszCardinality);
528
0
            oRelationship.Add("left_table_name",
529
0
                              poRelationship->GetLeftTableName());
530
0
            oRelationship.Add("right_table_name",
531
0
                              poRelationship->GetRightTableName());
532
533
0
            CPLJSONArray oLeftTableFields;
534
0
            oRelationship.Add("left_table_fields", oLeftTableFields);
535
0
            for (const auto &osName : aosLeftTableFields)
536
0
                oLeftTableFields.Add(osName);
537
538
0
            CPLJSONArray oRightTableFields;
539
0
            oRelationship.Add("right_table_fields", oRightTableFields);
540
0
            for (const auto &osName : aosRightTableFields)
541
0
                oRightTableFields.Add(osName);
542
543
0
            if (!osMappingTableName.empty())
544
0
            {
545
0
                oRelationship.Add("mapping_table_name", osMappingTableName);
546
547
0
                CPLJSONArray oLeftMappingTableFields;
548
0
                oRelationship.Add("left_mapping_table_fields",
549
0
                                  oLeftMappingTableFields);
550
0
                for (const auto &osName : aosLeftMappingTableFields)
551
0
                    oLeftMappingTableFields.Add(osName);
552
553
0
                CPLJSONArray oRightMappingTableFields;
554
0
                oRelationship.Add("right_mapping_table_fields",
555
0
                                  oRightMappingTableFields);
556
0
                for (const auto &osName : aosRightMappingTableFields)
557
0
                    oRightMappingTableFields.Add(osName);
558
0
            }
559
560
0
            oRelationship.Add("forward_path_label",
561
0
                              poRelationship->GetForwardPathLabel());
562
0
            oRelationship.Add("backward_path_label",
563
0
                              poRelationship->GetBackwardPathLabel());
564
0
        }
565
0
        else
566
0
        {
567
0
            const auto ConcatStringList =
568
0
                [&osRet, psOptions](const std::vector<std::string> &aosList)
569
0
            {
570
0
                bool bFirstName = true;
571
0
                for (const auto &osName : aosList)
572
0
                {
573
0
                    if (!bFirstName)
574
0
                        ConcatStr(osRet, psOptions->bStdoutOutput, ", ");
575
0
                    bFirstName = false;
576
0
                    ConcatStr(osRet, psOptions->bStdoutOutput, osName.c_str());
577
0
                }
578
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
579
0
            };
580
581
0
            if (!psOptions->bAllLayers)
582
0
            {
583
0
                Concat(osRet, psOptions->bStdoutOutput,
584
0
                       "Relationship: %s (%s, %s, %s)\n",
585
0
                       osRelationshipName.c_str(), pszType,
586
0
                       poRelationship->GetLeftTableName().c_str(),
587
0
                       poRelationship->GetRightTableName().c_str());
588
0
                continue;
589
0
            }
590
0
            Concat(osRet, psOptions->bStdoutOutput, "\nRelationship: %s\n",
591
0
                   osRelationshipName.c_str());
592
0
            Concat(osRet, psOptions->bStdoutOutput, "  Type: %s\n", pszType);
593
0
            Concat(osRet, psOptions->bStdoutOutput,
594
0
                   "  Related table type: %s\n",
595
0
                   poRelationship->GetRelatedTableType().c_str());
596
0
            Concat(osRet, psOptions->bStdoutOutput, "  Cardinality: %s\n",
597
0
                   pszCardinality);
598
0
            Concat(osRet, psOptions->bStdoutOutput, "  Left table name: %s\n",
599
0
                   poRelationship->GetLeftTableName().c_str());
600
0
            Concat(osRet, psOptions->bStdoutOutput, "  Right table name: %s\n",
601
0
                   poRelationship->GetRightTableName().c_str());
602
0
            Concat(osRet, psOptions->bStdoutOutput, "  Left table fields: ");
603
0
            ConcatStringList(aosLeftTableFields);
604
0
            Concat(osRet, psOptions->bStdoutOutput, "  Right table fields: ");
605
0
            ConcatStringList(aosRightTableFields);
606
607
0
            if (!osMappingTableName.empty())
608
0
            {
609
0
                Concat(osRet, psOptions->bStdoutOutput,
610
0
                       "  Mapping table name: %s\n",
611
0
                       osMappingTableName.c_str());
612
613
0
                Concat(osRet, psOptions->bStdoutOutput,
614
0
                       "  Left mapping table fields: ");
615
0
                ConcatStringList(aosLeftMappingTableFields);
616
617
0
                Concat(osRet, psOptions->bStdoutOutput,
618
0
                       "  Right mapping table fields: ");
619
0
                ConcatStringList(aosRightMappingTableFields);
620
0
            }
621
622
0
            Concat(osRet, psOptions->bStdoutOutput,
623
0
                   "  Forward path label: %s\n",
624
0
                   poRelationship->GetForwardPathLabel().c_str());
625
0
            Concat(osRet, psOptions->bStdoutOutput,
626
0
                   "  Backward path label: %s\n",
627
0
                   poRelationship->GetBackwardPathLabel().c_str());
628
0
        }
629
0
    }
630
0
}
631
632
/************************************************************************/
633
/*                    GDALVectorInfoPrintMetadata()                     */
634
/************************************************************************/
635
636
static void
637
GDALVectorInfoPrintMetadata(CPLString &osRet, CPLJSONObject &oMetadata,
638
                            const GDALVectorInfoOptions *psOptions,
639
                            GDALMajorObjectH hObject, const char *pszDomain,
640
                            const char *pszDisplayedname, const char *pszIndent)
641
0
{
642
0
    const bool bJsonOutput = psOptions->eFormat == FORMAT_JSON;
643
0
    bool bIsxml = false;
644
0
    bool bMDIsJson = false;
645
646
0
    if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "xml:"))
647
0
        bIsxml = true;
648
0
    else if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "json:"))
649
0
        bMDIsJson = true;
650
651
0
    CSLConstList papszMetadata = GDALGetMetadata(hObject, pszDomain);
652
0
    if (CSLCount(papszMetadata) > 0)
653
0
    {
654
0
        CPLJSONObject oMetadataDomain;
655
0
        if (!bJsonOutput)
656
0
            Concat(osRet, psOptions->bStdoutOutput, "%s%s:\n", pszIndent,
657
0
                   pszDisplayedname);
658
0
        for (int i = 0; papszMetadata[i] != nullptr; i++)
659
0
        {
660
0
            if (bJsonOutput)
661
0
            {
662
0
                if (bIsxml)
663
0
                {
664
0
                    oMetadata.Add(pszDomain, papszMetadata[i]);
665
0
                    return;
666
0
                }
667
0
                else if (bMDIsJson)
668
0
                {
669
0
                    CPLJSONDocument oDoc;
670
0
                    if (oDoc.LoadMemory(papszMetadata[i]))
671
0
                        oMetadata.Add(pszDomain, oDoc.GetRoot());
672
0
                    return;
673
0
                }
674
0
                else
675
0
                {
676
0
                    char *pszKey = nullptr;
677
0
                    const char *pszValue =
678
0
                        CPLParseNameValue(papszMetadata[i], &pszKey);
679
0
                    if (pszKey)
680
0
                    {
681
0
                        oMetadataDomain.Add(pszKey, pszValue);
682
0
                        CPLFree(pszKey);
683
0
                    }
684
0
                }
685
0
            }
686
0
            else if (bIsxml)
687
0
                Concat(osRet, psOptions->bStdoutOutput, "%s%s\n", pszIndent,
688
0
                       papszMetadata[i]);
689
0
            else
690
0
                Concat(osRet, psOptions->bStdoutOutput, "%s  %s\n", pszIndent,
691
0
                       papszMetadata[i]);
692
0
        }
693
0
        if (bJsonOutput)
694
0
        {
695
0
            oMetadata.Add(pszDomain ? pszDomain : "", oMetadataDomain);
696
0
        }
697
0
    }
698
0
}
699
700
/************************************************************************/
701
/*                    GDALVectorInfoReportMetadata()                    */
702
/************************************************************************/
703
704
static void GDALVectorInfoReportMetadata(CPLString &osRet, CPLJSONObject &oRoot,
705
                                         const GDALVectorInfoOptions *psOptions,
706
                                         GDALMajorObject *poMajorObject,
707
                                         bool bListMDD, bool bShowMetadata,
708
                                         CSLConstList papszExtraMDDomains)
709
0
{
710
0
    const char *pszIndent = "";
711
0
    auto hObject = GDALMajorObject::ToHandle(poMajorObject);
712
713
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
714
    /* -------------------------------------------------------------------- */
715
    /*      Report list of Metadata domains                                 */
716
    /* -------------------------------------------------------------------- */
717
0
    if (bListMDD)
718
0
    {
719
0
        const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject));
720
721
0
        CPLJSONArray metadataDomains;
722
723
0
        if (!aosMDDList.empty() && !bJson)
724
0
            Concat(osRet, psOptions->bStdoutOutput, "%sMetadata domains:\n",
725
0
                   pszIndent);
726
0
        for (const char *pszDomain : aosMDDList)
727
0
        {
728
0
            if (EQUAL(pszDomain, ""))
729
0
            {
730
0
                if (bJson)
731
0
                    metadataDomains.Add("");
732
0
                else
733
0
                    Concat(osRet, psOptions->bStdoutOutput, "%s  (default)\n",
734
0
                           pszIndent);
735
0
            }
736
0
            else
737
0
            {
738
0
                if (bJson)
739
0
                    metadataDomains.Add(pszDomain);
740
0
                else
741
0
                    Concat(osRet, psOptions->bStdoutOutput, "%s  %s\n",
742
0
                           pszIndent, pszDomain);
743
0
            }
744
0
        }
745
746
0
        if (bJson)
747
0
            oRoot.Add("metadataDomains", metadataDomains);
748
0
    }
749
750
0
    if (!bShowMetadata)
751
0
        return;
752
753
    /* -------------------------------------------------------------------- */
754
    /*      Report default Metadata domain.                                 */
755
    /* -------------------------------------------------------------------- */
756
0
    CPLJSONObject oMetadata;
757
0
    oRoot.Add("metadata", oMetadata);
758
0
    GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject, nullptr,
759
0
                                "Metadata", pszIndent);
760
761
    /* -------------------------------------------------------------------- */
762
    /*      Report extra Metadata domains                                   */
763
    /* -------------------------------------------------------------------- */
764
0
    if (papszExtraMDDomains != nullptr)
765
0
    {
766
0
        CPLStringList aosExtraMDDomainsExpanded;
767
768
0
        if (EQUAL(papszExtraMDDomains[0], "all") &&
769
0
            papszExtraMDDomains[1] == nullptr)
770
0
        {
771
0
            const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject));
772
0
            for (const char *pszDomain : aosMDDList)
773
0
            {
774
0
                if (!EQUAL(pszDomain, "") && !EQUAL(pszDomain, "SUBDATASETS"))
775
0
                {
776
0
                    aosExtraMDDomainsExpanded.AddString(pszDomain);
777
0
                }
778
0
            }
779
0
        }
780
0
        else
781
0
        {
782
0
            aosExtraMDDomainsExpanded = CSLDuplicate(papszExtraMDDomains);
783
0
        }
784
785
0
        for (const char *pszDomain : aosExtraMDDomainsExpanded)
786
0
        {
787
0
            const std::string osDisplayedName =
788
0
                std::string("Metadata (").append(pszDomain).append(")");
789
0
            GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject,
790
0
                                        pszDomain, osDisplayedName.c_str(),
791
0
                                        pszIndent);
792
0
        }
793
0
    }
794
0
    GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject,
795
0
                                "SUBDATASETS", "Subdatasets", pszIndent);
796
0
}
797
798
/************************************************************************/
799
/*                           ReportOnLayer()                            */
800
/************************************************************************/
801
802
static void ReportOnLayer(CPLString &osRet, CPLJSONObject &oLayer,
803
                          const GDALVectorInfoOptions *psOptions,
804
                          OGRLayer *poLayer, bool bForceSummary,
805
                          bool bTakeIntoAccountWHERE,
806
                          bool bTakeIntoAccountSpatialFilter,
807
                          bool bTakeIntoAccountGeomField)
808
0
{
809
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
810
0
    const bool bIsSummaryCli =
811
0
        psOptions->bIsCli && psOptions->bSummaryUserRequested;
812
0
    const bool bExportOgrSchema = psOptions->bExportOgrSchema;
813
0
    OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
814
815
0
    oLayer.Set("name", poLayer->GetName());
816
0
    if (bExportOgrSchema)
817
0
    {
818
0
        oLayer.Set("schemaType", "Full");
819
0
    }
820
0
    const int nGeomFieldCount =
821
0
        psOptions->bGeomType ? poLayer->GetLayerDefn()->GetGeomFieldCount() : 0;
822
823
    /* -------------------------------------------------------------------- */
824
    /*      Set filters if provided.                                        */
825
    /* -------------------------------------------------------------------- */
826
0
    if (bTakeIntoAccountWHERE && !psOptions->osWHERE.empty())
827
0
    {
828
0
        if (poLayer->SetAttributeFilter(psOptions->osWHERE.c_str()) !=
829
0
            OGRERR_NONE)
830
0
        {
831
0
            CPLError(CE_Failure, CPLE_AppDefined,
832
0
                     "SetAttributeFilter(%s) failed.",
833
0
                     psOptions->osWHERE.c_str());
834
0
            return;
835
0
        }
836
0
    }
837
838
0
    if (bTakeIntoAccountSpatialFilter && psOptions->poSpatialFilter != nullptr)
839
0
    {
840
0
        if (bTakeIntoAccountGeomField && !psOptions->osGeomField.empty())
841
0
        {
842
0
            const int iGeomField =
843
0
                poDefn->GetGeomFieldIndex(psOptions->osGeomField.c_str());
844
0
            if (iGeomField >= 0)
845
0
                poLayer->SetSpatialFilter(iGeomField,
846
0
                                          psOptions->poSpatialFilter.get());
847
0
            else
848
0
                CPLError(CE_Warning, CPLE_AppDefined,
849
0
                         "Cannot find geometry field %s.",
850
0
                         psOptions->osGeomField.c_str());
851
0
        }
852
0
        else
853
0
        {
854
0
            poLayer->SetSpatialFilter(psOptions->poSpatialFilter.get());
855
0
        }
856
0
    }
857
858
    /* -------------------------------------------------------------------- */
859
    /*      Report various overall information.                             */
860
    /* -------------------------------------------------------------------- */
861
0
    if (!bJson && !psOptions->bSuperQuiet)
862
0
    {
863
0
        Concat(osRet, psOptions->bStdoutOutput, "\n");
864
0
        Concat(osRet, psOptions->bStdoutOutput, "Layer name: %s\n",
865
0
               poLayer->GetName());
866
0
    }
867
868
0
    GDALVectorInfoReportMetadata(osRet, oLayer, psOptions, poLayer,
869
0
                                 !bIsSummaryCli && psOptions->bListMDD,
870
0
                                 !bIsSummaryCli && psOptions->bShowMetadata,
871
0
                                 psOptions->aosExtraMDDomains.List());
872
873
0
    if (psOptions->bVerbose)
874
0
    {
875
876
0
        CPLString osWKTFormat("FORMAT=");
877
0
        osWKTFormat += psOptions->osWKTFormat;
878
0
        const char *const apszWKTOptions[] = {osWKTFormat.c_str(),
879
0
                                              "MULTILINE=YES", nullptr};
880
881
0
        if (bJson || nGeomFieldCount > 1)
882
0
        {
883
0
            CPLJSONArray oGeometryFields;
884
0
            if (bJson)
885
0
                oLayer.Add("geometryFields", oGeometryFields);
886
0
            for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
887
0
            {
888
0
                const OGRGeomFieldDefn *poGFldDefn =
889
0
                    poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
890
0
                if (bJson)
891
0
                {
892
0
                    CPLJSONObject oGeometryField;
893
0
                    oGeometryFields.Add(oGeometryField);
894
0
                    oGeometryField.Set("name", poGFldDefn->GetNameRef());
895
0
                    oGeometryField.Set(
896
0
                        "type", OGRToOGCGeomType(poGFldDefn->GetType(),
897
0
                                                 /*bCamelCase=*/true,
898
0
                                                 /*bAddZm=*/true,
899
0
                                                 /*bSpaceBeforeZM=*/false));
900
0
                    oGeometryField.Set("nullable",
901
0
                                       CPL_TO_BOOL(poGFldDefn->IsNullable()));
902
0
                    if (psOptions->bExtent3D)
903
0
                    {
904
0
                        OGREnvelope3D oExt;
905
0
                        if (poLayer->GetExtent3D(iGeom, &oExt, TRUE) ==
906
0
                            OGRERR_NONE)
907
0
                        {
908
0
                            {
909
0
                                CPLJSONArray oBbox;
910
0
                                oBbox.Add(oExt.MinX);
911
0
                                oBbox.Add(oExt.MinY);
912
0
                                oBbox.Add(oExt.MaxX);
913
0
                                oBbox.Add(oExt.MaxY);
914
0
                                oGeometryField.Add("extent", oBbox);
915
0
                            }
916
0
                            {
917
0
                                CPLJSONArray oBbox;
918
0
                                oBbox.Add(oExt.MinX);
919
0
                                oBbox.Add(oExt.MinY);
920
0
                                if (std::isfinite(oExt.MinZ))
921
0
                                    oBbox.Add(oExt.MinZ);
922
0
                                else
923
0
                                    oBbox.AddNull();
924
0
                                oBbox.Add(oExt.MaxX);
925
0
                                oBbox.Add(oExt.MaxY);
926
0
                                if (std::isfinite(oExt.MaxZ))
927
0
                                    oBbox.Add(oExt.MaxZ);
928
0
                                else
929
0
                                    oBbox.AddNull();
930
0
                                oGeometryField.Add("extent3D", oBbox);
931
0
                            }
932
0
                        }
933
0
                    }
934
0
                    else if (psOptions->bExtent)
935
0
                    {
936
0
                        OGREnvelope oExt;
937
0
                        if (poLayer->GetExtent(iGeom, &oExt, TRUE) ==
938
0
                            OGRERR_NONE)
939
0
                        {
940
0
                            CPLJSONArray oBbox;
941
0
                            oBbox.Add(oExt.MinX);
942
0
                            oBbox.Add(oExt.MinY);
943
0
                            oBbox.Add(oExt.MaxX);
944
0
                            oBbox.Add(oExt.MaxY);
945
0
                            oGeometryField.Add("extent", oBbox);
946
0
                        }
947
0
                    }
948
0
                    const OGRSpatialReference *poSRS =
949
0
                        poGFldDefn->GetSpatialRef();
950
0
                    if (poSRS)
951
0
                    {
952
0
                        CPLJSONObject oCRS;
953
0
                        oGeometryField.Add("coordinateSystem", oCRS);
954
955
                        // When exporting the schema give priority
956
                        // to the compact <authority:code> form
957
0
                        bool authIdSet{false};
958
0
                        if (psOptions->bExportOgrSchema)
959
0
                        {
960
0
                            const char *pszAuthCode =
961
0
                                poSRS->GetAuthorityCode(nullptr);
962
0
                            const char *pszAuthName =
963
0
                                poSRS->GetAuthorityName(nullptr);
964
0
                            if (pszAuthName && pszAuthCode)
965
0
                            {
966
0
                                std::string oSRS{pszAuthName};
967
0
                                oSRS += ':';
968
0
                                oSRS += pszAuthCode;
969
0
                                oCRS.Set("authid", oSRS);
970
0
                                authIdSet = true;
971
0
                            }
972
0
                        }
973
974
0
                        if (!authIdSet)
975
0
                        {
976
0
                            char *pszWKT = nullptr;
977
0
                            poSRS->exportToWkt(&pszWKT, apszWKTOptions);
978
0
                            if (pszWKT)
979
0
                            {
980
0
                                oCRS.Set("wkt", pszWKT);
981
0
                                CPLFree(pszWKT);
982
0
                            }
983
984
0
                            {
985
0
                                char *pszProjJson = nullptr;
986
                                // PROJJSON requires PROJ >= 6.2
987
0
                                CPLErrorStateBackuper oCPLErrorHandlerPusher(
988
0
                                    CPLQuietErrorHandler);
989
0
                                CPL_IGNORE_RET_VAL(poSRS->exportToPROJJSON(
990
0
                                    &pszProjJson, nullptr));
991
0
                                if (pszProjJson)
992
0
                                {
993
0
                                    CPLJSONDocument oDoc;
994
0
                                    if (oDoc.LoadMemory(pszProjJson))
995
0
                                    {
996
0
                                        oCRS.Add("projjson", oDoc.GetRoot());
997
0
                                    }
998
0
                                    CPLFree(pszProjJson);
999
0
                                }
1000
0
                            }
1001
1002
0
                            const auto &anAxes =
1003
0
                                poSRS->GetDataAxisToSRSAxisMapping();
1004
0
                            CPLJSONArray oAxisMapping;
1005
0
                            for (const auto nAxis : anAxes)
1006
0
                            {
1007
0
                                oAxisMapping.Add(nAxis);
1008
0
                            }
1009
0
                            oCRS.Add("dataAxisToSRSAxisMapping", oAxisMapping);
1010
1011
0
                            const double dfCoordinateEpoch =
1012
0
                                poSRS->GetCoordinateEpoch();
1013
0
                            if (dfCoordinateEpoch > 0)
1014
0
                                oCRS.Set("coordinateEpoch", dfCoordinateEpoch);
1015
0
                        }
1016
0
                    }
1017
0
                    else
1018
0
                    {
1019
0
                        oGeometryField.SetNull("coordinateSystem");
1020
0
                    }
1021
1022
0
                    const auto &srsList = poLayer->GetSupportedSRSList(iGeom);
1023
0
                    if (!srsList.empty())
1024
0
                    {
1025
0
                        CPLJSONArray oSupportedSRSList;
1026
0
                        for (const auto &poSupportedSRS : srsList)
1027
0
                        {
1028
0
                            const char *pszAuthName =
1029
0
                                poSupportedSRS->GetAuthorityName(nullptr);
1030
0
                            const char *pszAuthCode =
1031
0
                                poSupportedSRS->GetAuthorityCode(nullptr);
1032
0
                            CPLJSONObject oSupportedSRS;
1033
0
                            if (pszAuthName && pszAuthCode)
1034
0
                            {
1035
0
                                CPLJSONObject id;
1036
0
                                id.Set("authority", pszAuthName);
1037
0
                                id.Set("code", pszAuthCode);
1038
0
                                oSupportedSRS.Add("id", id);
1039
0
                                oSupportedSRSList.Add(oSupportedSRS);
1040
0
                            }
1041
0
                            else
1042
0
                            {
1043
0
                                char *pszWKT = nullptr;
1044
0
                                poSupportedSRS->exportToWkt(&pszWKT,
1045
0
                                                            apszWKTOptions);
1046
0
                                if (pszWKT)
1047
0
                                {
1048
0
                                    oSupportedSRS.Add("wkt", pszWKT);
1049
0
                                    oSupportedSRSList.Add(oSupportedSRS);
1050
0
                                }
1051
0
                                CPLFree(pszWKT);
1052
0
                            }
1053
0
                        }
1054
0
                        oGeometryField.Add("supportedSRSList",
1055
0
                                           oSupportedSRSList);
1056
0
                    }
1057
1058
0
                    const auto &oCoordPrec =
1059
0
                        poGFldDefn->GetCoordinatePrecision();
1060
0
                    if (oCoordPrec.dfXYResolution !=
1061
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1062
0
                    {
1063
0
                        oGeometryField.Add("xyCoordinateResolution",
1064
0
                                           oCoordPrec.dfXYResolution);
1065
0
                    }
1066
0
                    if (oCoordPrec.dfZResolution !=
1067
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1068
0
                    {
1069
0
                        oGeometryField.Add("zCoordinateResolution",
1070
0
                                           oCoordPrec.dfZResolution);
1071
0
                    }
1072
0
                    if (oCoordPrec.dfMResolution !=
1073
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1074
0
                    {
1075
0
                        oGeometryField.Add("mCoordinateResolution",
1076
0
                                           oCoordPrec.dfMResolution);
1077
0
                    }
1078
1079
                    // For example set by OpenFileGDB driver
1080
0
                    if (!oCoordPrec.oFormatSpecificOptions.empty())
1081
0
                    {
1082
0
                        CPLJSONObject oFormatSpecificOptions;
1083
0
                        for (const auto &formatOptionsPair :
1084
0
                             oCoordPrec.oFormatSpecificOptions)
1085
0
                        {
1086
0
                            CPLJSONObject oThisFormatSpecificOptions;
1087
0
                            for (const auto &[pszKey, pszValue] :
1088
0
                                 cpl::IterateNameValue(
1089
0
                                     formatOptionsPair.second))
1090
0
                            {
1091
0
                                const auto eValueType =
1092
0
                                    CPLGetValueType(pszValue);
1093
0
                                if (eValueType == CPL_VALUE_INTEGER)
1094
0
                                {
1095
0
                                    oThisFormatSpecificOptions.Add(
1096
0
                                        pszKey, CPLAtoGIntBig(pszValue));
1097
0
                                }
1098
0
                                else if (eValueType == CPL_VALUE_REAL)
1099
0
                                {
1100
0
                                    oThisFormatSpecificOptions.Add(
1101
0
                                        pszKey, CPLAtof(pszValue));
1102
0
                                }
1103
0
                                else
1104
0
                                {
1105
0
                                    oThisFormatSpecificOptions.Add(pszKey,
1106
0
                                                                   pszValue);
1107
0
                                }
1108
0
                            }
1109
0
                            oFormatSpecificOptions.Add(
1110
0
                                formatOptionsPair.first,
1111
0
                                oThisFormatSpecificOptions);
1112
0
                        }
1113
0
                        oGeometryField.Add(
1114
0
                            "coordinatePrecisionFormatSpecificOptions",
1115
0
                            oFormatSpecificOptions);
1116
0
                    }
1117
0
                }
1118
0
                else
1119
0
                {
1120
0
                    Concat(osRet, psOptions->bStdoutOutput,
1121
0
                           "Geometry (%s): %s\n", poGFldDefn->GetNameRef(),
1122
0
                           OGRGeometryTypeToName(poGFldDefn->GetType()));
1123
0
                }
1124
0
            }
1125
0
        }
1126
0
        else if (psOptions->bGeomType)
1127
0
        {
1128
0
            Concat(osRet, psOptions->bStdoutOutput, "Geometry: %s\n",
1129
0
                   OGRGeometryTypeToName(poLayer->GetGeomType()));
1130
0
        }
1131
1132
0
        if (psOptions->bFeatureCount)
1133
0
        {
1134
0
            if (bJson)
1135
0
                oLayer.Set("featureCount", poLayer->GetFeatureCount());
1136
0
            else
1137
0
            {
1138
0
                Concat(osRet, psOptions->bStdoutOutput,
1139
0
                       "Feature Count: " CPL_FRMT_GIB "\n",
1140
0
                       poLayer->GetFeatureCount());
1141
0
            }
1142
0
        }
1143
1144
0
        if (!bJson && psOptions->bExtent && nGeomFieldCount > 1)
1145
0
        {
1146
0
            for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1147
0
            {
1148
0
                if (psOptions->bExtent3D)
1149
0
                {
1150
0
                    OGREnvelope3D oExt;
1151
0
                    if (poLayer->GetExtent3D(iGeom, &oExt, TRUE) == OGRERR_NONE)
1152
0
                    {
1153
0
                        OGRGeomFieldDefn *poGFldDefn =
1154
0
                            poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1155
0
                        Concat(osRet, psOptions->bStdoutOutput,
1156
0
                               "Extent (%s): (%f, %f, %s) - (%f, %f, %s)\n",
1157
0
                               poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY,
1158
0
                               std::isfinite(oExt.MinZ)
1159
0
                                   ? CPLSPrintf("%f", oExt.MinZ)
1160
0
                                   : "none",
1161
0
                               oExt.MaxX, oExt.MaxY,
1162
0
                               std::isfinite(oExt.MaxZ)
1163
0
                                   ? CPLSPrintf("%f", oExt.MaxZ)
1164
0
                                   : "none");
1165
0
                    }
1166
0
                }
1167
0
                else
1168
0
                {
1169
0
                    OGREnvelope oExt;
1170
0
                    if (poLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE)
1171
0
                    {
1172
0
                        OGRGeomFieldDefn *poGFldDefn =
1173
0
                            poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1174
0
                        Concat(osRet, psOptions->bStdoutOutput,
1175
0
                               "Extent (%s): (%f, %f) - (%f, %f)\n",
1176
0
                               poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY,
1177
0
                               oExt.MaxX, oExt.MaxY);
1178
0
                    }
1179
0
                }
1180
0
            }
1181
0
        }
1182
0
        else if (!bJson && psOptions->bExtent)
1183
0
        {
1184
0
            if (psOptions->bExtent3D)
1185
0
            {
1186
0
                OGREnvelope3D oExt;
1187
0
                if (poLayer->GetExtent3D(0, &oExt, TRUE) == OGRERR_NONE)
1188
0
                {
1189
0
                    Concat(
1190
0
                        osRet, psOptions->bStdoutOutput,
1191
0
                        "Extent: (%f, %f, %s) - (%f, %f, %s)\n", oExt.MinX,
1192
0
                        oExt.MinY,
1193
0
                        std::isfinite(oExt.MinZ) ? CPLSPrintf("%f", oExt.MinZ)
1194
0
                                                 : "none",
1195
0
                        oExt.MaxX, oExt.MaxY,
1196
0
                        std::isfinite(oExt.MaxZ) ? CPLSPrintf("%f", oExt.MaxZ)
1197
0
                                                 : "none");
1198
0
                }
1199
0
            }
1200
0
            else
1201
0
            {
1202
0
                OGREnvelope oExt;
1203
0
                if (poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE)
1204
0
                {
1205
0
                    Concat(osRet, psOptions->bStdoutOutput,
1206
0
                           "Extent: (%f, %f) - (%f, %f)\n", oExt.MinX,
1207
0
                           oExt.MinY, oExt.MaxX, oExt.MaxY);
1208
0
                }
1209
0
            }
1210
0
        }
1211
1212
0
        const auto displayExtraInfoSRS =
1213
0
            [&osRet, &psOptions](const OGRSpatialReference *poSRS)
1214
0
        {
1215
0
            const double dfCoordinateEpoch = poSRS->GetCoordinateEpoch();
1216
0
            if (dfCoordinateEpoch > 0)
1217
0
            {
1218
0
                std::string osCoordinateEpoch =
1219
0
                    CPLSPrintf("%f", dfCoordinateEpoch);
1220
0
                const size_t nDotPos = osCoordinateEpoch.find('.');
1221
0
                if (nDotPos != std::string::npos)
1222
0
                {
1223
0
                    while (osCoordinateEpoch.size() > nDotPos + 2 &&
1224
0
                           osCoordinateEpoch.back() == '0')
1225
0
                        osCoordinateEpoch.pop_back();
1226
0
                }
1227
0
                Concat(osRet, psOptions->bStdoutOutput,
1228
0
                       "Coordinate epoch: %s\n", osCoordinateEpoch.c_str());
1229
0
            }
1230
1231
0
            const auto &mapping = poSRS->GetDataAxisToSRSAxisMapping();
1232
0
            Concat(osRet, psOptions->bStdoutOutput,
1233
0
                   "Data axis to CRS axis mapping: ");
1234
0
            for (size_t i = 0; i < mapping.size(); i++)
1235
0
            {
1236
0
                if (i > 0)
1237
0
                {
1238
0
                    Concat(osRet, psOptions->bStdoutOutput, ",");
1239
0
                }
1240
0
                Concat(osRet, psOptions->bStdoutOutput, "%d", mapping[i]);
1241
0
            }
1242
0
            Concat(osRet, psOptions->bStdoutOutput, "\n");
1243
0
        };
1244
1245
0
        const auto DisplaySupportedCRSList = [&](int iGeomField)
1246
0
        {
1247
0
            const auto &srsList = poLayer->GetSupportedSRSList(iGeomField);
1248
0
            if (!srsList.empty())
1249
0
            {
1250
0
                Concat(osRet, psOptions->bStdoutOutput, "Supported SRS: ");
1251
0
                bool bFirst = true;
1252
0
                for (const auto &poSupportedSRS : srsList)
1253
0
                {
1254
0
                    const char *pszAuthName =
1255
0
                        poSupportedSRS->GetAuthorityName(nullptr);
1256
0
                    const char *pszAuthCode =
1257
0
                        poSupportedSRS->GetAuthorityCode(nullptr);
1258
0
                    if (!bFirst)
1259
0
                        Concat(osRet, psOptions->bStdoutOutput, ", ");
1260
0
                    bFirst = false;
1261
0
                    if (pszAuthName && pszAuthCode)
1262
0
                    {
1263
0
                        Concat(osRet, psOptions->bStdoutOutput, "%s:%s",
1264
0
                               pszAuthName, pszAuthCode);
1265
0
                    }
1266
0
                    else
1267
0
                    {
1268
0
                        ConcatStr(osRet, psOptions->bStdoutOutput,
1269
0
                                  poSupportedSRS->GetName());
1270
0
                    }
1271
0
                }
1272
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1273
0
            }
1274
0
        };
1275
1276
0
        if (!bJson && nGeomFieldCount > 1)
1277
0
        {
1278
1279
0
            for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1280
0
            {
1281
0
                OGRGeomFieldDefn *poGFldDefn =
1282
0
                    poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1283
0
                const OGRSpatialReference *poSRS = poGFldDefn->GetSpatialRef();
1284
0
                char *pszWKT = nullptr;
1285
0
                if (poSRS == nullptr)
1286
0
                {
1287
0
                    pszWKT = CPLStrdup("(unknown)");
1288
0
                }
1289
0
                else
1290
0
                {
1291
0
                    poSRS->exportToWkt(&pszWKT, apszWKTOptions);
1292
0
                }
1293
1294
0
                Concat(osRet, psOptions->bStdoutOutput, "SRS WKT (%s):\n%s\n",
1295
0
                       poGFldDefn->GetNameRef(), pszWKT);
1296
0
                CPLFree(pszWKT);
1297
0
                if (poSRS)
1298
0
                {
1299
0
                    displayExtraInfoSRS(poSRS);
1300
0
                }
1301
0
                DisplaySupportedCRSList(iGeom);
1302
0
            }
1303
0
        }
1304
0
        else if (!bJson)
1305
0
        {
1306
0
            char *pszWKT = nullptr;
1307
0
            auto poSRS = poLayer->GetSpatialRef();
1308
0
            if (poSRS == nullptr)
1309
0
            {
1310
0
                pszWKT = CPLStrdup("(unknown)");
1311
0
            }
1312
0
            else
1313
0
            {
1314
0
                poSRS->exportToWkt(&pszWKT, apszWKTOptions);
1315
0
            }
1316
1317
0
            Concat(osRet, psOptions->bStdoutOutput, "Layer SRS WKT:\n%s\n",
1318
0
                   pszWKT);
1319
0
            CPLFree(pszWKT);
1320
0
            if (poSRS)
1321
0
            {
1322
0
                displayExtraInfoSRS(poSRS);
1323
0
            }
1324
0
            DisplaySupportedCRSList(0);
1325
0
        }
1326
1327
0
        const char *pszFIDColumn = poLayer->GetFIDColumn();
1328
0
        if (pszFIDColumn[0] != '\0')
1329
0
        {
1330
0
            if (bJson)
1331
0
                oLayer.Set("fidColumnName", pszFIDColumn);
1332
0
            else
1333
0
            {
1334
0
                Concat(osRet, psOptions->bStdoutOutput, "FID Column = %s\n",
1335
0
                       pszFIDColumn);
1336
0
            }
1337
0
        }
1338
1339
0
        for (int iGeom = 0; !bJson && iGeom < nGeomFieldCount; iGeom++)
1340
0
        {
1341
0
            OGRGeomFieldDefn *poGFldDefn =
1342
0
                poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1343
0
            if (nGeomFieldCount == 1 && EQUAL(poGFldDefn->GetNameRef(), "") &&
1344
0
                poGFldDefn->IsNullable())
1345
0
                break;
1346
0
            Concat(osRet, psOptions->bStdoutOutput, "Geometry Column ");
1347
0
            if (nGeomFieldCount > 1)
1348
0
                Concat(osRet, psOptions->bStdoutOutput, "%d ", iGeom + 1);
1349
0
            if (!poGFldDefn->IsNullable())
1350
0
                Concat(osRet, psOptions->bStdoutOutput, "NOT NULL ");
1351
0
            Concat(osRet, psOptions->bStdoutOutput, "= %s\n",
1352
0
                   poGFldDefn->GetNameRef());
1353
0
        }
1354
1355
0
        CPLJSONArray oFields;
1356
0
        if (bJson)
1357
0
            oLayer.Add("fields", oFields);
1358
0
        for (int iAttr = 0; iAttr < poDefn->GetFieldCount(); iAttr++)
1359
0
        {
1360
0
            const OGRFieldDefn *poField = poDefn->GetFieldDefn(iAttr);
1361
0
            const char *pszAlias = poField->GetAlternativeNameRef();
1362
0
            const std::string &osDomain = poField->GetDomainName();
1363
0
            const std::string &osComment = poField->GetComment();
1364
0
            const auto eType = poField->GetType();
1365
0
            std::string osTimeZone;
1366
0
            if (eType == OFTTime || eType == OFTDate || eType == OFTDateTime)
1367
0
            {
1368
0
                const int nTZFlag = poField->GetTZFlag();
1369
0
                if (nTZFlag == OGR_TZFLAG_LOCALTIME)
1370
0
                {
1371
0
                    osTimeZone = "localtime";
1372
0
                }
1373
0
                else if (nTZFlag == OGR_TZFLAG_MIXED_TZ)
1374
0
                {
1375
0
                    osTimeZone = "mixed timezones";
1376
0
                }
1377
0
                else if (nTZFlag == OGR_TZFLAG_UTC)
1378
0
                {
1379
0
                    osTimeZone = "UTC";
1380
0
                }
1381
0
                else if (nTZFlag > 0)
1382
0
                {
1383
0
                    char chSign;
1384
0
                    const int nOffset = (nTZFlag - OGR_TZFLAG_UTC) * 15;
1385
0
                    int nHours =
1386
0
                        static_cast<int>(nOffset / 60);  // Round towards zero.
1387
0
                    const int nMinutes = std::abs(nOffset - nHours * 60);
1388
1389
0
                    if (nOffset < 0)
1390
0
                    {
1391
0
                        chSign = '-';
1392
0
                        nHours = std::abs(nHours);
1393
0
                    }
1394
0
                    else
1395
0
                    {
1396
0
                        chSign = '+';
1397
0
                    }
1398
0
                    osTimeZone =
1399
0
                        CPLSPrintf("%c%02d:%02d", chSign, nHours, nMinutes);
1400
0
                }
1401
0
            }
1402
1403
0
            if (bJson)
1404
0
            {
1405
0
                CPLJSONObject oField;
1406
0
                oFields.Add(oField);
1407
0
                oField.Set("name", poField->GetNameRef());
1408
0
                oField.Set("type", OGRFieldDefn::GetFieldTypeName(eType));
1409
0
                if (poField->GetSubType() != OFSTNone)
1410
0
                    oField.Set("subType", OGRFieldDefn::GetFieldSubTypeName(
1411
0
                                              poField->GetSubType()));
1412
0
                if (poField->GetWidth() > 0)
1413
0
                    oField.Set("width", poField->GetWidth());
1414
0
                if (poField->GetPrecision() > 0)
1415
0
                    oField.Set("precision", poField->GetPrecision());
1416
0
                oField.Set("nullable", CPL_TO_BOOL(poField->IsNullable()));
1417
0
                oField.Set("uniqueConstraint",
1418
0
                           CPL_TO_BOOL(poField->IsUnique()));
1419
0
                if (poField->GetDefault() != nullptr)
1420
0
                    oField.Set("defaultValue", poField->GetDefault());
1421
0
                if (pszAlias != nullptr && pszAlias[0])
1422
0
                    oField.Set("alias", pszAlias);
1423
0
                if (!osDomain.empty())
1424
0
                    oField.Set("domainName", osDomain);
1425
0
                if (!osComment.empty())
1426
0
                    oField.Set("comment", osComment);
1427
0
                if (!osTimeZone.empty())
1428
0
                    oField.Set("timezone", osTimeZone);
1429
0
            }
1430
0
            else
1431
0
            {
1432
0
                const char *pszType =
1433
0
                    (poField->GetSubType() != OFSTNone)
1434
0
                        ? CPLSPrintf("%s(%s)",
1435
0
                                     OGRFieldDefn::GetFieldTypeName(
1436
0
                                         poField->GetType()),
1437
0
                                     OGRFieldDefn::GetFieldSubTypeName(
1438
0
                                         poField->GetSubType()))
1439
0
                        : OGRFieldDefn::GetFieldTypeName(poField->GetType());
1440
0
                Concat(osRet, psOptions->bStdoutOutput, "%s: %s",
1441
0
                       poField->GetNameRef(), pszType);
1442
0
                if (eType == OFTTime || eType == OFTDate ||
1443
0
                    eType == OFTDateTime)
1444
0
                {
1445
0
                    if (!osTimeZone.empty())
1446
0
                        Concat(osRet, psOptions->bStdoutOutput, " (%s)",
1447
0
                               osTimeZone.c_str());
1448
0
                }
1449
0
                else
1450
0
                {
1451
0
                    Concat(osRet, psOptions->bStdoutOutput, " (%d.%d)",
1452
0
                           poField->GetWidth(), poField->GetPrecision());
1453
0
                }
1454
0
                if (poField->IsUnique())
1455
0
                    Concat(osRet, psOptions->bStdoutOutput, " UNIQUE");
1456
0
                if (!poField->IsNullable())
1457
0
                    Concat(osRet, psOptions->bStdoutOutput, " NOT NULL");
1458
0
                if (poField->GetDefault() != nullptr)
1459
0
                    Concat(osRet, psOptions->bStdoutOutput, " DEFAULT %s",
1460
0
                           poField->GetDefault());
1461
0
                if (pszAlias != nullptr && pszAlias[0])
1462
0
                    Concat(osRet, psOptions->bStdoutOutput,
1463
0
                           ", alternative name=\"%s\"", pszAlias);
1464
0
                if (!osDomain.empty())
1465
0
                    Concat(osRet, psOptions->bStdoutOutput, ", domain name=%s",
1466
0
                           osDomain.c_str());
1467
0
                if (!osComment.empty())
1468
0
                    Concat(osRet, psOptions->bStdoutOutput, ", comment=%s",
1469
0
                           osComment.c_str());
1470
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1471
0
            }
1472
0
        }
1473
0
    }
1474
1475
    /* -------------------------------------------------------------------- */
1476
    /*      Read, and dump features.                                        */
1477
    /* -------------------------------------------------------------------- */
1478
1479
0
    if ((psOptions->nFetchFID == OGRNullFID || bJson) && !bForceSummary &&
1480
0
        ((psOptions->bIsCli && psOptions->bFeaturesUserRequested) ||
1481
0
         (!psOptions->bIsCli && !psOptions->bSummaryOnly)))
1482
0
    {
1483
0
        if (!psOptions->bSuperQuiet)
1484
0
        {
1485
0
            CPLJSONArray oFeatures;
1486
0
            const bool bDisplayFields =
1487
0
                CPLTestBool(psOptions->aosOptions.FetchNameValueDef(
1488
0
                    "DISPLAY_FIELDS", "YES"));
1489
0
            const int nFields =
1490
0
                bDisplayFields ? poLayer->GetLayerDefn()->GetFieldCount() : 0;
1491
0
            const bool bDisplayGeometry =
1492
0
                CPLTestBool(psOptions->aosOptions.FetchNameValueDef(
1493
0
                    "DISPLAY_GEOMETRY", "YES"));
1494
0
            const int nGeomFields =
1495
0
                bDisplayGeometry ? poLayer->GetLayerDefn()->GetGeomFieldCount()
1496
0
                                 : 0;
1497
0
            if (bJson)
1498
0
                oLayer.Add("features", oFeatures);
1499
1500
0
            const auto EmitFeatureJSON =
1501
0
                [poLayer, nFields, nGeomFields,
1502
0
                 &oFeatures](const OGRFeature *poFeature)
1503
0
            {
1504
0
                CPLJSONObject oFeature;
1505
0
                CPLJSONObject oProperties;
1506
0
                oFeatures.Add(oFeature);
1507
0
                oFeature.Add("type", "Feature");
1508
0
                oFeature.Add("properties", oProperties);
1509
0
                oFeature.Add("fid", poFeature->GetFID());
1510
0
                for (int i = 0; i < nFields; ++i)
1511
0
                {
1512
0
                    const auto poFDefn = poFeature->GetFieldDefnRef(i);
1513
0
                    const auto eType = poFDefn->GetType();
1514
0
                    if (!poFeature->IsFieldSet(i))
1515
0
                        continue;
1516
0
                    if (poFeature->IsFieldNull(i))
1517
0
                    {
1518
0
                        oProperties.SetNull(poFDefn->GetNameRef());
1519
0
                    }
1520
0
                    else if (eType == OFTInteger)
1521
0
                    {
1522
0
                        if (poFDefn->GetSubType() == OFSTBoolean)
1523
0
                            oProperties.Add(
1524
0
                                poFDefn->GetNameRef(),
1525
0
                                CPL_TO_BOOL(poFeature->GetFieldAsInteger(i)));
1526
0
                        else
1527
0
                            oProperties.Add(poFDefn->GetNameRef(),
1528
0
                                            poFeature->GetFieldAsInteger(i));
1529
0
                    }
1530
0
                    else if (eType == OFTInteger64)
1531
0
                    {
1532
0
                        oProperties.Add(poFDefn->GetNameRef(),
1533
0
                                        poFeature->GetFieldAsInteger64(i));
1534
0
                    }
1535
0
                    else if (eType == OFTReal)
1536
0
                    {
1537
0
                        oProperties.Add(poFDefn->GetNameRef(),
1538
0
                                        poFeature->GetFieldAsDouble(i));
1539
0
                    }
1540
0
                    else if ((eType == OFTString &&
1541
0
                              poFDefn->GetSubType() != OFSTJSON) ||
1542
0
                             eType == OFTDate || eType == OFTTime ||
1543
0
                             eType == OFTDateTime)
1544
0
                    {
1545
0
                        oProperties.Add(poFDefn->GetNameRef(),
1546
0
                                        poFeature->GetFieldAsString(i));
1547
0
                    }
1548
0
                    else
1549
0
                    {
1550
0
                        char *pszSerialized =
1551
0
                            poFeature->GetFieldAsSerializedJSon(i);
1552
0
                        if (pszSerialized)
1553
0
                        {
1554
0
                            const auto eStrType =
1555
0
                                CPLGetValueType(pszSerialized);
1556
0
                            if (eStrType == CPL_VALUE_INTEGER)
1557
0
                            {
1558
0
                                oProperties.Add(poFDefn->GetNameRef(),
1559
0
                                                CPLAtoGIntBig(pszSerialized));
1560
0
                            }
1561
0
                            else if (eStrType == CPL_VALUE_REAL)
1562
0
                            {
1563
0
                                oProperties.Add(poFDefn->GetNameRef(),
1564
0
                                                CPLAtof(pszSerialized));
1565
0
                            }
1566
0
                            else
1567
0
                            {
1568
0
                                CPLJSONDocument oDoc;
1569
0
                                if (oDoc.LoadMemory(pszSerialized))
1570
0
                                    oProperties.Add(poFDefn->GetNameRef(),
1571
0
                                                    oDoc.GetRoot());
1572
0
                            }
1573
0
                            CPLFree(pszSerialized);
1574
0
                        }
1575
0
                    }
1576
0
                }
1577
1578
0
                const auto GetGeoJSONOptions = [poLayer](int iGeomField)
1579
0
                {
1580
0
                    CPLStringList aosGeoJSONOptions;
1581
0
                    const auto &oCoordPrec = poLayer->GetLayerDefn()
1582
0
                                                 ->GetGeomFieldDefn(iGeomField)
1583
0
                                                 ->GetCoordinatePrecision();
1584
0
                    if (oCoordPrec.dfXYResolution !=
1585
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1586
0
                    {
1587
0
                        aosGeoJSONOptions.SetNameValue(
1588
0
                            "XY_COORD_PRECISION",
1589
0
                            CPLSPrintf("%d",
1590
0
                                       OGRGeomCoordinatePrecision::
1591
0
                                           ResolutionToPrecision(
1592
0
                                               oCoordPrec.dfXYResolution)));
1593
0
                    }
1594
0
                    if (oCoordPrec.dfZResolution !=
1595
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
1596
0
                    {
1597
0
                        aosGeoJSONOptions.SetNameValue(
1598
0
                            "Z_COORD_PRECISION",
1599
0
                            CPLSPrintf("%d",
1600
0
                                       OGRGeomCoordinatePrecision::
1601
0
                                           ResolutionToPrecision(
1602
0
                                               oCoordPrec.dfZResolution)));
1603
0
                    }
1604
0
                    return aosGeoJSONOptions;
1605
0
                };
1606
1607
0
                if (nGeomFields == 0)
1608
0
                    oFeature.SetNull("geometry");
1609
0
                else
1610
0
                {
1611
0
                    if (const auto poGeom = poFeature->GetGeometryRef())
1612
0
                    {
1613
0
                        char *pszSerialized =
1614
0
                            wkbFlatten(poGeom->getGeometryType()) <=
1615
0
                                    wkbGeometryCollection
1616
0
                                ? poGeom->exportToJson(
1617
0
                                      GetGeoJSONOptions(0).List())
1618
0
                                : nullptr;
1619
0
                        if (pszSerialized)
1620
0
                        {
1621
0
                            CPLJSONDocument oDoc;
1622
0
                            if (oDoc.LoadMemory(pszSerialized))
1623
0
                                oFeature.Add("geometry", oDoc.GetRoot());
1624
0
                            CPLFree(pszSerialized);
1625
0
                        }
1626
0
                        else
1627
0
                        {
1628
0
                            CPLJSONObject oGeometry;
1629
0
                            oFeature.SetNull("geometry");
1630
0
                            oFeature.Add("wkt_geometry", poGeom->exportToWkt());
1631
0
                        }
1632
0
                    }
1633
0
                    else
1634
0
                        oFeature.SetNull("geometry");
1635
1636
0
                    if (nGeomFields > 1)
1637
0
                    {
1638
0
                        CPLJSONArray oGeometries;
1639
0
                        oFeature.Add("geometries", oGeometries);
1640
0
                        for (int i = 0; i < nGeomFields; ++i)
1641
0
                        {
1642
0
                            auto poGeom = poFeature->GetGeomFieldRef(i);
1643
0
                            if (poGeom)
1644
0
                            {
1645
0
                                char *pszSerialized =
1646
0
                                    wkbFlatten(poGeom->getGeometryType()) <=
1647
0
                                            wkbGeometryCollection
1648
0
                                        ? poGeom->exportToJson(
1649
0
                                              GetGeoJSONOptions(i).List())
1650
0
                                        : nullptr;
1651
0
                                if (pszSerialized)
1652
0
                                {
1653
0
                                    CPLJSONDocument oDoc;
1654
0
                                    if (oDoc.LoadMemory(pszSerialized))
1655
0
                                        oGeometries.Add(oDoc.GetRoot());
1656
0
                                    CPLFree(pszSerialized);
1657
0
                                }
1658
0
                                else
1659
0
                                {
1660
0
                                    CPLJSONObject oGeometry;
1661
0
                                    oGeometries.Add(poGeom->exportToWkt());
1662
0
                                }
1663
0
                            }
1664
0
                            else
1665
0
                                oGeometries.AddNull();
1666
0
                        }
1667
0
                    }
1668
0
                }
1669
0
            };
1670
1671
0
            if (psOptions->nFetchFID != OGRNullFID)
1672
0
            {
1673
0
                auto poFeature = std::unique_ptr<OGRFeature>(
1674
0
                    poLayer->GetFeature(psOptions->nFetchFID));
1675
0
                if (poFeature)
1676
0
                {
1677
0
                    EmitFeatureJSON(poFeature.get());
1678
0
                }
1679
0
            }
1680
0
            else if (psOptions->nLimit < 0 || psOptions->nLimit > 0)
1681
0
            {
1682
0
                GIntBig nFeatureCount = 0;
1683
0
                for (auto &poFeature : poLayer)
1684
0
                {
1685
0
                    if (bJson)
1686
0
                    {
1687
0
                        EmitFeatureJSON(poFeature.get());
1688
0
                    }
1689
0
                    else
1690
0
                    {
1691
0
                        ConcatStr(osRet, psOptions->bStdoutOutput,
1692
0
                                  poFeature
1693
0
                                      ->DumpReadableAsString(
1694
0
                                          psOptions->aosOptions.List())
1695
0
                                      .c_str());
1696
0
                    }
1697
1698
0
                    ++nFeatureCount;
1699
0
                    if (psOptions->nLimit >= 0 &&
1700
0
                        nFeatureCount >= psOptions->nLimit)
1701
0
                    {
1702
0
                        break;
1703
0
                    }
1704
0
                }
1705
0
            }
1706
0
        }
1707
0
    }
1708
0
    else if (!bJson && psOptions->nFetchFID != OGRNullFID)
1709
0
    {
1710
0
        auto poFeature = std::unique_ptr<OGRFeature>(
1711
0
            poLayer->GetFeature(psOptions->nFetchFID));
1712
0
        if (poFeature == nullptr)
1713
0
        {
1714
0
            Concat(osRet, psOptions->bStdoutOutput,
1715
0
                   "Unable to locate feature id " CPL_FRMT_GIB
1716
0
                   " on this layer.\n",
1717
0
                   psOptions->nFetchFID);
1718
0
        }
1719
0
        else
1720
0
        {
1721
0
            ConcatStr(
1722
0
                osRet, psOptions->bStdoutOutput,
1723
0
                poFeature->DumpReadableAsString(psOptions->aosOptions.List())
1724
0
                    .c_str());
1725
0
        }
1726
0
    }
1727
0
}
1728
1729
/************************************************************************/
1730
/*                         PrintLayerSummary()                          */
1731
/************************************************************************/
1732
1733
static void PrintLayerSummary(CPLString &osRet, CPLJSONObject &oLayer,
1734
                              const GDALVectorInfoOptions *psOptions,
1735
                              OGRLayer *poLayer, bool bIsPrivate)
1736
0
{
1737
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
1738
0
    const bool bIsSummaryCli = psOptions->bIsCli && psOptions->bSummaryOnly;
1739
0
    if (bJson)
1740
0
    {
1741
0
        oLayer.Set("name", poLayer->GetName());
1742
0
    }
1743
0
    else
1744
0
        ConcatStr(osRet, psOptions->bStdoutOutput, poLayer->GetName());
1745
1746
0
    const char *pszTitle = poLayer->GetMetadataItem("TITLE");
1747
0
    if (pszTitle)
1748
0
    {
1749
0
        if (bJson)
1750
0
            oLayer.Set("title", pszTitle);
1751
0
        else
1752
0
            Concat(osRet, psOptions->bStdoutOutput, " (title: %s)", pszTitle);
1753
0
    }
1754
1755
0
    const int nGeomFieldCount =
1756
0
        psOptions->bGeomType ? poLayer->GetLayerDefn()->GetGeomFieldCount() : 0;
1757
1758
0
    if (bIsSummaryCli && bJson)
1759
0
    {
1760
0
        CPLJSONArray oGeometryTypes;
1761
0
        for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1762
0
        {
1763
0
            OGRGeomFieldDefn *poGFldDefn =
1764
0
                poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1765
0
            oGeometryTypes.Add(OGRGeometryTypeToName(poGFldDefn->GetType()));
1766
0
        }
1767
0
        oLayer.Add("geometryType", oGeometryTypes);
1768
0
        return;
1769
0
    }
1770
1771
0
    if (bJson || nGeomFieldCount > 1)
1772
0
    {
1773
0
        if (!bJson)
1774
0
            Concat(osRet, psOptions->bStdoutOutput, " (");
1775
0
        CPLJSONArray oGeometryFields;
1776
0
        oLayer.Add("geometryFields", oGeometryFields);
1777
0
        for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
1778
0
        {
1779
0
            OGRGeomFieldDefn *poGFldDefn =
1780
0
                poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
1781
0
            if (bJson)
1782
0
            {
1783
0
                oGeometryFields.Add(
1784
0
                    OGRGeometryTypeToName(poGFldDefn->GetType()));
1785
0
            }
1786
0
            else
1787
0
            {
1788
0
                if (iGeom > 0)
1789
0
                    Concat(osRet, psOptions->bStdoutOutput, ", ");
1790
0
                ConcatStr(osRet, psOptions->bStdoutOutput,
1791
0
                          OGRGeometryTypeToName(poGFldDefn->GetType()));
1792
0
            }
1793
0
        }
1794
0
        if (!bJson)
1795
0
            Concat(osRet, psOptions->bStdoutOutput, ")");
1796
0
    }
1797
0
    else if (psOptions->bGeomType && poLayer->GetGeomType() != wkbUnknown)
1798
0
        Concat(osRet, psOptions->bStdoutOutput, " (%s)",
1799
0
               OGRGeometryTypeToName(poLayer->GetGeomType()));
1800
1801
0
    if (bIsPrivate)
1802
0
    {
1803
0
        if (bJson)
1804
0
            oLayer.Set("isPrivate", true);
1805
0
        else
1806
0
            Concat(osRet, psOptions->bStdoutOutput, " [private]");
1807
0
    }
1808
1809
0
    if (!bJson)
1810
0
        Concat(osRet, psOptions->bStdoutOutput, "\n");
1811
0
}
1812
1813
/************************************************************************/
1814
/*                      ReportHiearchicalLayers()                       */
1815
/************************************************************************/
1816
1817
static void ReportHiearchicalLayers(CPLString &osRet, CPLJSONObject &oRoot,
1818
                                    const GDALVectorInfoOptions *psOptions,
1819
                                    const GDALGroup *group,
1820
                                    const std::string &indent, bool bGeomType)
1821
0
{
1822
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
1823
0
    const auto aosVectorLayerNames = group->GetVectorLayerNames();
1824
0
    CPLJSONArray oLayerNames;
1825
0
    oRoot.Add("layerNames", oLayerNames);
1826
0
    for (const auto &osVectorLayerName : aosVectorLayerNames)
1827
0
    {
1828
0
        OGRLayer *poLayer = group->OpenVectorLayer(osVectorLayerName);
1829
0
        if (poLayer)
1830
0
        {
1831
0
            CPLJSONObject oLayer;
1832
0
            if (!bJson)
1833
0
            {
1834
0
                Concat(osRet, psOptions->bStdoutOutput,
1835
0
                       "%sLayer: ", indent.c_str());
1836
0
                PrintLayerSummary(osRet, oLayer, psOptions, poLayer,
1837
0
                                  /* bIsPrivate=*/false);
1838
0
            }
1839
0
            else
1840
0
            {
1841
0
                oLayerNames.Add(poLayer->GetName());
1842
0
            }
1843
0
        }
1844
0
    }
1845
1846
0
    const std::string subIndent(indent + "  ");
1847
0
    auto aosSubGroupNames = group->GetGroupNames();
1848
0
    CPLJSONArray oGroupArray;
1849
0
    oRoot.Add("groups", oGroupArray);
1850
0
    for (const auto &osSubGroupName : aosSubGroupNames)
1851
0
    {
1852
0
        auto poSubGroup = group->OpenGroup(osSubGroupName);
1853
0
        if (poSubGroup)
1854
0
        {
1855
0
            CPLJSONObject oGroup;
1856
0
            if (!bJson)
1857
0
            {
1858
0
                Concat(osRet, psOptions->bStdoutOutput, "Group %s",
1859
0
                       indent.c_str());
1860
0
                Concat(osRet, psOptions->bStdoutOutput, "%s:\n",
1861
0
                       osSubGroupName.c_str());
1862
0
            }
1863
0
            else
1864
0
            {
1865
0
                oGroupArray.Add(oGroup);
1866
0
                oGroup.Set("name", osSubGroupName);
1867
0
            }
1868
0
            ReportHiearchicalLayers(osRet, oGroup, psOptions, poSubGroup.get(),
1869
0
                                    subIndent, bGeomType);
1870
0
        }
1871
0
    }
1872
0
}
1873
1874
/************************************************************************/
1875
/*                           GDALVectorInfo()                           */
1876
/************************************************************************/
1877
1878
/**
1879
 * Lists various information about a GDAL supported vector dataset.
1880
 *
1881
 * This is the equivalent of the <a href="/programs/ogrinfo.html">ogrinfo</a>
1882
 * utility.
1883
 *
1884
 * GDALVectorInfoOptions* must be allocated and freed with
1885
 * GDALVectorInfoOptionsNew() and GDALVectorInfoOptionsFree() respectively.
1886
 *
1887
 * @param hDataset the dataset handle.
1888
 * @param psOptions the options structure returned by GDALVectorInfoOptionsNew()
1889
 * or NULL.
1890
 * @return string corresponding to the information about the raster dataset
1891
 * (must be freed with CPLFree()), or NULL in case of error.
1892
 *
1893
 * @since GDAL 3.7
1894
 */
1895
char *GDALVectorInfo(GDALDatasetH hDataset,
1896
                     const GDALVectorInfoOptions *psOptions)
1897
0
{
1898
0
    auto poDS = GDALDataset::FromHandle(hDataset);
1899
0
    if (poDS == nullptr)
1900
0
        return nullptr;
1901
1902
0
    const GDALVectorInfoOptions sDefaultOptions;
1903
0
    if (!psOptions)
1904
0
        psOptions = &sDefaultOptions;
1905
1906
0
    GDALDriver *poDriver = poDS->GetDriver();
1907
1908
0
    CPLString osRet;
1909
0
    CPLJSONObject oRoot;
1910
0
    const std::string osFilename(poDS->GetDescription());
1911
1912
0
    const bool bExportOgrSchema = psOptions->bExportOgrSchema;
1913
0
    const bool bJson = psOptions->eFormat == FORMAT_JSON;
1914
0
    const bool bIsSummaryCli =
1915
0
        (psOptions->bIsCli && psOptions->bSummaryUserRequested);
1916
1917
0
    CPLJSONArray oLayerArray;
1918
0
    if (bJson)
1919
0
    {
1920
0
        if (!bExportOgrSchema)
1921
0
        {
1922
0
            oRoot.Set("description", poDS->GetDescription());
1923
0
            if (poDriver)
1924
0
            {
1925
0
                oRoot.Set("driverShortName", poDriver->GetDescription());
1926
0
                oRoot.Set("driverLongName",
1927
0
                          poDriver->GetMetadataItem(GDAL_DMD_LONGNAME));
1928
0
            }
1929
0
        }
1930
0
        oRoot.Add("layers", oLayerArray);
1931
0
    }
1932
1933
    /* -------------------------------------------------------------------- */
1934
    /*      Some information messages.                                      */
1935
    /* -------------------------------------------------------------------- */
1936
0
    if (!bJson && psOptions->bVerbose)
1937
0
    {
1938
0
        Concat(osRet, psOptions->bStdoutOutput,
1939
0
               "INFO: Open of `%s'\n"
1940
0
               "      using driver `%s' successful.\n",
1941
0
               osFilename.c_str(),
1942
0
               poDriver ? poDriver->GetDescription() : "(null)");
1943
0
    }
1944
1945
0
    if (!bJson && psOptions->bVerbose &&
1946
0
        !EQUAL(osFilename.c_str(), poDS->GetDescription()))
1947
0
    {
1948
0
        Concat(osRet, psOptions->bStdoutOutput,
1949
0
               "INFO: Internal data source name `%s'\n"
1950
0
               "      different from user name `%s'.\n",
1951
0
               poDS->GetDescription(), osFilename.c_str());
1952
0
    }
1953
1954
0
    int nRepeatCount = psOptions->nRepeatCount;
1955
1956
0
    if (!bIsSummaryCli && !bExportOgrSchema)
1957
0
    {
1958
0
        GDALVectorInfoReportMetadata(
1959
0
            osRet, oRoot, psOptions, poDS, psOptions->bListMDD,
1960
0
            psOptions->bShowMetadata, psOptions->aosExtraMDDomains.List());
1961
1962
0
        CPLJSONObject oDomains;
1963
0
        oRoot.Add("domains", oDomains);
1964
0
        if (!psOptions->osFieldDomain.empty())
1965
0
        {
1966
0
            auto poDomain = poDS->GetFieldDomain(psOptions->osFieldDomain);
1967
0
            if (poDomain == nullptr)
1968
0
            {
1969
0
                CPLError(CE_Failure, CPLE_AppDefined,
1970
0
                         "Domain %s cannot be found.",
1971
0
                         psOptions->osFieldDomain.c_str());
1972
0
                return nullptr;
1973
0
            }
1974
0
            if (!bJson)
1975
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1976
0
            ReportFieldDomain(osRet, oDomains, psOptions, poDomain);
1977
0
            if (!bJson)
1978
0
                Concat(osRet, psOptions->bStdoutOutput, "\n");
1979
0
        }
1980
0
        else if (bJson)
1981
0
        {
1982
0
            for (const auto &osDomainName : poDS->GetFieldDomainNames())
1983
0
            {
1984
0
                auto poDomain = poDS->GetFieldDomain(osDomainName);
1985
0
                if (poDomain)
1986
0
                {
1987
0
                    ReportFieldDomain(osRet, oDomains, psOptions, poDomain);
1988
0
                }
1989
0
            }
1990
0
        }
1991
1992
0
        if (psOptions->bDatasetGetNextFeature)
1993
0
        {
1994
0
            nRepeatCount = 0;  // skip layer reporting.
1995
1996
            /* --------------------------------------------------------------------
1997
             */
1998
            /*      Set filters if provided. */
1999
            /* --------------------------------------------------------------------
2000
             */
2001
0
            if (!psOptions->osWHERE.empty() ||
2002
0
                psOptions->poSpatialFilter != nullptr)
2003
0
            {
2004
0
                for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
2005
0
                {
2006
0
                    OGRLayer *poLayer = poDS->GetLayer(iLayer);
2007
2008
0
                    if (poLayer == nullptr)
2009
0
                    {
2010
0
                        CPLError(CE_Failure, CPLE_AppDefined,
2011
0
                                 "Couldn't fetch advertised layer %d.", iLayer);
2012
0
                        return nullptr;
2013
0
                    }
2014
2015
0
                    if (!psOptions->osWHERE.empty())
2016
0
                    {
2017
0
                        if (poLayer->SetAttributeFilter(
2018
0
                                psOptions->osWHERE.c_str()) != OGRERR_NONE)
2019
0
                        {
2020
0
                            CPLError(
2021
0
                                CE_Warning, CPLE_AppDefined,
2022
0
                                "SetAttributeFilter(%s) failed on layer %s.",
2023
0
                                psOptions->osWHERE.c_str(), poLayer->GetName());
2024
0
                        }
2025
0
                    }
2026
2027
0
                    if (psOptions->poSpatialFilter != nullptr)
2028
0
                    {
2029
0
                        if (!psOptions->osGeomField.empty())
2030
0
                        {
2031
0
                            OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
2032
0
                            const int iGeomField = poDefn->GetGeomFieldIndex(
2033
0
                                psOptions->osGeomField.c_str());
2034
0
                            if (iGeomField >= 0)
2035
0
                                poLayer->SetSpatialFilter(
2036
0
                                    iGeomField,
2037
0
                                    psOptions->poSpatialFilter.get());
2038
0
                            else
2039
0
                                CPLError(CE_Warning, CPLE_AppDefined,
2040
0
                                         "Cannot find geometry field %s.",
2041
0
                                         psOptions->osGeomField.c_str());
2042
0
                        }
2043
0
                        else
2044
0
                        {
2045
0
                            poLayer->SetSpatialFilter(
2046
0
                                psOptions->poSpatialFilter.get());
2047
0
                        }
2048
0
                    }
2049
0
                }
2050
0
            }
2051
2052
0
            std::set<OGRLayer *> oSetLayers;
2053
0
            while (true)
2054
0
            {
2055
0
                OGRLayer *poLayer = nullptr;
2056
0
                OGRFeature *poFeature =
2057
0
                    poDS->GetNextFeature(&poLayer, nullptr, nullptr, nullptr);
2058
0
                if (poFeature == nullptr)
2059
0
                    break;
2060
0
                if (psOptions->aosLayers.empty() || poLayer == nullptr ||
2061
0
                    CSLFindString(psOptions->aosLayers.List(),
2062
0
                                  poLayer->GetName()) >= 0)
2063
0
                {
2064
0
                    if (psOptions->bVerbose && poLayer != nullptr &&
2065
0
                        oSetLayers.find(poLayer) == oSetLayers.end())
2066
0
                    {
2067
0
                        oSetLayers.insert(poLayer);
2068
0
                        CPLJSONObject oLayer;
2069
0
                        oLayerArray.Add(oLayer);
2070
0
                        ReportOnLayer(
2071
0
                            osRet, oLayer, psOptions, poLayer,
2072
0
                            /*bForceSummary = */ true,
2073
0
                            /*bTakeIntoAccountWHERE = */ false,
2074
0
                            /*bTakeIntoAccountSpatialFilter = */ false,
2075
0
                            /*bTakeIntoAccountGeomField = */ false);
2076
0
                    }
2077
0
                    if (!psOptions->bSuperQuiet && !psOptions->bSummaryOnly)
2078
0
                        poFeature->DumpReadable(
2079
0
                            nullptr,
2080
0
                            const_cast<char **>(psOptions->aosOptions.List()));
2081
0
                }
2082
0
                OGRFeature::DestroyFeature(poFeature);
2083
0
            }
2084
0
        }
2085
2086
        /* -------------------------------------------------------------------- */
2087
        /*      Special case for -sql clause.  No source layers required.       */
2088
        /* -------------------------------------------------------------------- */
2089
0
        else if (!psOptions->osSQLStatement.empty())
2090
0
        {
2091
0
            nRepeatCount = 0;  // skip layer reporting.
2092
2093
0
            if (!bJson && !psOptions->aosLayers.empty())
2094
0
                Concat(osRet, psOptions->bStdoutOutput,
2095
0
                       "layer names ignored in combination with -sql.\n");
2096
2097
0
            CPLErrorReset();
2098
0
            OGRLayer *poResultSet = poDS->ExecuteSQL(
2099
0
                psOptions->osSQLStatement.c_str(),
2100
0
                psOptions->osGeomField.empty()
2101
0
                    ? psOptions->poSpatialFilter.get()
2102
0
                    : nullptr,
2103
0
                psOptions->osDialect.empty() ? nullptr
2104
0
                                             : psOptions->osDialect.c_str());
2105
2106
0
            if (poResultSet != nullptr)
2107
0
            {
2108
0
                if (!psOptions->osWHERE.empty())
2109
0
                {
2110
0
                    if (poResultSet->SetAttributeFilter(
2111
0
                            psOptions->osWHERE.c_str()) != OGRERR_NONE)
2112
0
                    {
2113
0
                        CPLError(CE_Failure, CPLE_AppDefined,
2114
0
                                 "SetAttributeFilter(%s) failed.",
2115
0
                                 psOptions->osWHERE.c_str());
2116
0
                        return nullptr;
2117
0
                    }
2118
0
                }
2119
2120
0
                CPLJSONObject oLayer;
2121
0
                oLayerArray.Add(oLayer);
2122
0
                if (!psOptions->osGeomField.empty())
2123
0
                    ReportOnLayer(osRet, oLayer, psOptions, poResultSet,
2124
0
                                  /*bForceSummary = */ false,
2125
0
                                  /*bTakeIntoAccountWHERE = */ false,
2126
0
                                  /*bTakeIntoAccountSpatialFilter = */ true,
2127
0
                                  /*bTakeIntoAccountGeomField = */ true);
2128
0
                else
2129
0
                    ReportOnLayer(osRet, oLayer, psOptions, poResultSet,
2130
0
                                  /*bForceSummary = */ false,
2131
0
                                  /*bTakeIntoAccountWHERE = */ false,
2132
0
                                  /*bTakeIntoAccountSpatialFilter = */ false,
2133
0
                                  /*bTakeIntoAccountGeomField = */ false);
2134
2135
0
                poDS->ReleaseResultSet(poResultSet);
2136
0
            }
2137
0
            else if (CPLGetLastErrorType() != CE_None)
2138
0
            {
2139
                // sqlite3 emits messages with "readonly" and GDAL with "read-only"
2140
0
                if (psOptions->bIsCli &&
2141
0
                    (strstr(CPLGetLastErrorMsg(), "readonly") ||
2142
0
                     strstr(CPLGetLastErrorMsg(), "read-only")))
2143
0
                {
2144
0
                    CPLError(CE_Warning, CPLE_AppDefined,
2145
0
                             "Perhaps you want to run \"gdal vector sql "
2146
0
                             "--update\" instead?");
2147
0
                }
2148
0
                return nullptr;
2149
0
            }
2150
0
        }
2151
0
    }
2152
2153
    // coverity[tainted_data]
2154
0
    auto papszLayers = psOptions->aosLayers.List();
2155
0
    for (int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++)
2156
0
    {
2157
0
        if (papszLayers == nullptr || papszLayers[0] == nullptr)
2158
0
        {
2159
0
            const int nLayerCount = poDS->GetLayerCount();
2160
0
            if (iRepeat == 0)
2161
0
                CPLDebug("OGR", "GetLayerCount() = %d\n", nLayerCount);
2162
2163
0
            bool bDone = false;
2164
0
            auto poRootGroup = poDS->GetRootGroup();
2165
0
            if ((bJson || !psOptions->bAllLayers) && poRootGroup &&
2166
0
                (!poRootGroup->GetGroupNames().empty() ||
2167
0
                 !poRootGroup->GetVectorLayerNames().empty()))
2168
0
            {
2169
0
                CPLJSONObject oGroup;
2170
0
                oRoot.Add("rootGroup", oGroup);
2171
0
                ReportHiearchicalLayers(osRet, oGroup, psOptions,
2172
0
                                        poRootGroup.get(), std::string(),
2173
0
                                        psOptions->bGeomType);
2174
0
                if (!bJson)
2175
0
                    bDone = true;
2176
0
            }
2177
2178
            /* --------------------------------------------------------------------
2179
             */
2180
            /*      Process each data source layer. */
2181
            /* --------------------------------------------------------------------
2182
             */
2183
0
            for (int iLayer = 0; !bDone && iLayer < nLayerCount; iLayer++)
2184
0
            {
2185
0
                OGRLayer *poLayer = poDS->GetLayer(iLayer);
2186
2187
0
                if (poLayer == nullptr)
2188
0
                {
2189
0
                    CPLError(CE_Failure, CPLE_AppDefined,
2190
0
                             "Couldn't fetch advertised layer %d.", iLayer);
2191
0
                    return nullptr;
2192
0
                }
2193
2194
0
                CPLJSONObject oLayer;
2195
0
                oLayerArray.Add(oLayer);
2196
0
                if (!psOptions->bAllLayers || bIsSummaryCli)
2197
0
                {
2198
0
                    if (!bJson)
2199
0
                        Concat(osRet, psOptions->bStdoutOutput,
2200
0
                               "%d: ", iLayer + 1);
2201
0
                    PrintLayerSummary(osRet, oLayer, psOptions, poLayer,
2202
0
                                      poDS->IsLayerPrivate(iLayer));
2203
0
                }
2204
0
                else
2205
0
                {
2206
0
                    if (iRepeat != 0)
2207
0
                        poLayer->ResetReading();
2208
2209
0
                    ReportOnLayer(osRet, oLayer, psOptions, poLayer,
2210
0
                                  /*bForceSummary = */ false,
2211
0
                                  /*bTakeIntoAccountWHERE = */ true,
2212
0
                                  /*bTakeIntoAccountSpatialFilter = */ true,
2213
0
                                  /*bTakeIntoAccountGeomField = */ true);
2214
0
                }
2215
0
            }
2216
0
        }
2217
0
        else
2218
0
        {
2219
            /* --------------------------------------------------------------------
2220
             */
2221
            /*      Process specified data source layers. */
2222
            /* --------------------------------------------------------------------
2223
             */
2224
2225
0
            for (const char *pszLayer : cpl::Iterate(papszLayers))
2226
0
            {
2227
0
                OGRLayer *poLayer = poDS->GetLayerByName(pszLayer);
2228
2229
0
                if (poLayer == nullptr)
2230
0
                {
2231
0
                    CPLError(CE_Failure, CPLE_AppDefined,
2232
0
                             "Couldn't fetch requested layer %s.", pszLayer);
2233
0
                    return nullptr;
2234
0
                }
2235
2236
0
                if (iRepeat != 0)
2237
0
                    poLayer->ResetReading();
2238
2239
0
                CPLJSONObject oLayer;
2240
0
                oLayerArray.Add(oLayer);
2241
0
                ReportOnLayer(osRet, oLayer, psOptions, poLayer,
2242
0
                              /*bForceSummary = */ false,
2243
0
                              /*bTakeIntoAccountWHERE = */ true,
2244
0
                              /*bTakeIntoAccountSpatialFilter = */ true,
2245
0
                              /*bTakeIntoAccountGeomField = */ true);
2246
0
            }
2247
0
        }
2248
0
    }
2249
2250
0
    if (!papszLayers && !bIsSummaryCli && !bExportOgrSchema)
2251
0
    {
2252
0
        ReportRelationships(osRet, oRoot, psOptions, poDS);
2253
0
    }
2254
2255
0
    if (bJson)
2256
0
    {
2257
0
        osRet.clear();
2258
0
        ConcatStr(
2259
0
            osRet, psOptions->bStdoutOutput,
2260
0
            json_object_to_json_string_ext(
2261
0
                static_cast<struct json_object *>(oRoot.GetInternalHandle()),
2262
0
                JSON_C_TO_STRING_PRETTY
2263
0
#ifdef JSON_C_TO_STRING_NOSLASHESCAPE
2264
0
                    | JSON_C_TO_STRING_NOSLASHESCAPE
2265
0
#endif
2266
0
                ));
2267
0
        ConcatStr(osRet, psOptions->bStdoutOutput, "\n");
2268
0
    }
2269
2270
0
    return VSI_STRDUP_VERBOSE(osRet);
2271
0
}
2272
2273
/************************************************************************/
2274
/*                   GDALVectorInfoOptionsGetParser()                   */
2275
/************************************************************************/
2276
2277
static std::unique_ptr<GDALArgumentParser> GDALVectorInfoOptionsGetParser(
2278
    GDALVectorInfoOptions *psOptions,
2279
    GDALVectorInfoOptionsForBinary *psOptionsForBinary)
2280
0
{
2281
0
    auto argParser = std::make_unique<GDALArgumentParser>(
2282
0
        "ogrinfo", /* bForBinary=*/psOptionsForBinary != nullptr);
2283
2284
0
    argParser->add_description(
2285
0
        _("Lists information about an OGR-supported data source."));
2286
2287
0
    argParser->add_epilog(
2288
0
        _("For more details, consult https://gdal.org/programs/ogrinfo.html"));
2289
2290
0
    argParser->add_argument("-json")
2291
0
        .flag()
2292
0
        .action([psOptions](const std::string &)
2293
0
                { psOptions->eFormat = FORMAT_JSON; })
2294
0
        .help(_("Display the output in json format."));
2295
2296
    // Hidden argument to select OGR_SCHEMA output
2297
0
    argParser->add_argument("-schema")
2298
0
        .flag()
2299
0
        .hidden()
2300
0
        .action([psOptions](const std::string &)
2301
0
                { psOptions->bExportOgrSchema = true; })
2302
0
        .help(_("Export the OGR_SCHEMA in json format."));
2303
2304
0
    argParser->add_argument("-ro")
2305
0
        .flag()
2306
0
        .action(
2307
0
            [psOptionsForBinary](const std::string &)
2308
0
            {
2309
0
                if (psOptionsForBinary)
2310
0
                    psOptionsForBinary->bReadOnly = true;
2311
0
            })
2312
0
        .help(_("Open the data source in read-only mode."));
2313
2314
0
    argParser->add_argument("-update")
2315
0
        .flag()
2316
0
        .action(
2317
0
            [psOptionsForBinary](const std::string &)
2318
0
            {
2319
0
                if (psOptionsForBinary)
2320
0
                    psOptionsForBinary->bUpdate = true;
2321
0
            })
2322
0
        .help(_("Open the data source in update mode."));
2323
2324
0
    argParser->add_argument("-q", "--quiet")
2325
0
        .flag()
2326
0
        .action(
2327
0
            [psOptions, psOptionsForBinary](const std::string &)
2328
0
            {
2329
0
                psOptions->bVerbose = false;
2330
0
                if (psOptionsForBinary)
2331
0
                    psOptionsForBinary->bVerbose = false;
2332
0
            })
2333
0
        .help(_("Quiet mode. No progress message is emitted on the standard "
2334
0
                "output."));
2335
2336
#ifdef __AFL_HAVE_MANUAL_CONTROL
2337
    /* Undocumented: mainly only useful for AFL testing */
2338
    argParser->add_argument("-qq")
2339
        .flag()
2340
        .hidden()
2341
        .action(
2342
            [psOptions, psOptionsForBinary](const std::string &)
2343
            {
2344
                psOptions->bVerbose = false;
2345
                if (psOptionsForBinary)
2346
                    psOptionsForBinary->bVerbose = false;
2347
                psOptions->bSuperQuiet = true;
2348
            })
2349
        .help(_("Super quiet mode."));
2350
#endif
2351
2352
0
    argParser->add_argument("-fid")
2353
0
        .metavar("<FID>")
2354
0
        .store_into(psOptions->nFetchFID)
2355
0
        .help(_("Only the feature with this feature id will be reported."));
2356
2357
0
    argParser->add_argument("-spat")
2358
0
        .metavar("<xmin> <ymin> <xmax> <ymax>")
2359
0
        .nargs(4)
2360
0
        .scan<'g', double>()
2361
0
        .help(_("The area of interest. Only features within the rectangle will "
2362
0
                "be reported."));
2363
2364
0
    argParser->add_argument("-geomfield")
2365
0
        .metavar("<field>")
2366
0
        .store_into(psOptions->osGeomField)
2367
0
        .help(_("Name of the geometry field on which the spatial filter "
2368
0
                "operates."));
2369
2370
0
    argParser->add_argument("-where")
2371
0
        .metavar("<restricted_where>")
2372
0
        .store_into(psOptions->osWHERE)
2373
0
        .help(_("An attribute query in a restricted form of the queries used "
2374
0
                "in the SQL WHERE statement."));
2375
2376
0
    {
2377
0
        auto &group = argParser->add_mutually_exclusive_group();
2378
0
        group.add_argument("-sql")
2379
0
            .metavar("<statement|@filename>")
2380
0
            .store_into(psOptions->osSQLStatement)
2381
0
            .help(_(
2382
0
                "Execute the indicated SQL statement and return the result."));
2383
2384
0
        group.add_argument("-rl")
2385
0
            .store_into(psOptions->bDatasetGetNextFeature)
2386
0
            .help(_("Enable random layer reading mode."));
2387
0
    }
2388
2389
0
    argParser->add_argument("-dialect")
2390
0
        .metavar("<dialect>")
2391
0
        .store_into(psOptions->osDialect)
2392
0
        .help(_("SQL dialect."));
2393
2394
    // Only for fuzzing
2395
0
    argParser->add_argument("-rc")
2396
0
        .hidden()
2397
0
        .metavar("<count>")
2398
0
        .store_into(psOptions->nRepeatCount)
2399
0
        .help(_("Repeat count"));
2400
2401
0
    argParser->add_argument("-al")
2402
0
        .store_into(psOptions->bAllLayers)
2403
0
        .help(_("List all layers (used instead of having to give layer names "
2404
0
                "as arguments)."));
2405
2406
0
    {
2407
0
        auto &group = argParser->add_mutually_exclusive_group();
2408
0
        group.add_argument("-so", "-summary")
2409
0
            .store_into(psOptions->bSummaryUserRequested)
2410
0
            .help(_("Summary only: show only summary information like "
2411
0
                    "projection, schema, feature count and extents."));
2412
2413
0
        group.add_argument("-features")
2414
0
            .store_into(psOptions->bFeaturesUserRequested)
2415
0
            .help(_("Enable listing of features."));
2416
0
    }
2417
2418
0
    argParser->add_argument("-limit")
2419
0
        .metavar("<nb_features>")
2420
0
        .store_into(psOptions->nLimit)
2421
0
        .help(_("Limit the number of features per layer."));
2422
2423
0
    argParser->add_argument("-fields")
2424
0
        .choices("YES", "NO")
2425
0
        .metavar("YES|NO")
2426
0
        .action(
2427
0
            [psOptions](const std::string &s)
2428
0
            {
2429
0
                psOptions->aosOptions.SetNameValue("DISPLAY_FIELDS", s.c_str());
2430
0
            })
2431
0
        .help(
2432
0
            _("If set to NO, the feature dump will not display field values."));
2433
2434
0
    argParser->add_argument("-geom")
2435
0
        .choices("YES", "NO", "SUMMARY", "WKT", "ISO_WKT")
2436
0
        .metavar("YES|NO|SUMMARY|WKT|ISO_WKT")
2437
0
        .action(
2438
0
            [psOptions](const std::string &s)
2439
0
            {
2440
0
                psOptions->aosOptions.SetNameValue("DISPLAY_GEOMETRY",
2441
0
                                                   s.c_str());
2442
0
            })
2443
0
        .help(_("How to display geometries in feature dump."));
2444
2445
0
    argParser->add_argument("-oo")
2446
0
        .append()
2447
0
        .metavar("<NAME=VALUE>")
2448
0
        .action(
2449
0
            [psOptionsForBinary](const std::string &s)
2450
0
            {
2451
0
                if (psOptionsForBinary)
2452
0
                    psOptionsForBinary->aosOpenOptions.AddString(s.c_str());
2453
0
            })
2454
0
        .help(_("Dataset open option (format-specific)."));
2455
2456
0
    argParser->add_argument("-nomd")
2457
0
        .flag()
2458
0
        .action([psOptions](const std::string &)
2459
0
                { psOptions->bShowMetadata = false; })
2460
0
        .help(_("Suppress metadata printing."));
2461
2462
0
    argParser->add_argument("-listmdd")
2463
0
        .store_into(psOptions->bListMDD)
2464
0
        .help(_("List all metadata domains available for the dataset."));
2465
2466
0
    argParser->add_argument("-mdd")
2467
0
        .append()
2468
0
        .metavar("<domain>")
2469
0
        .action([psOptions](const std::string &s)
2470
0
                { psOptions->aosExtraMDDomains.AddString(s.c_str()); })
2471
0
        .help(_("List metadata in the specified domain."));
2472
2473
0
    argParser->add_argument("-nocount")
2474
0
        .flag()
2475
0
        .action([psOptions](const std::string &)
2476
0
                { psOptions->bFeatureCount = false; })
2477
0
        .help(_("Suppress feature count printing."));
2478
2479
0
    argParser->add_argument("-noextent")
2480
0
        .flag()
2481
0
        .action([psOptions](const std::string &)
2482
0
                { psOptions->bExtent = false; })
2483
0
        .help(_("Suppress spatial extent printing."));
2484
2485
0
    argParser->add_argument("-extent3D")
2486
0
        .store_into(psOptions->bExtent3D)
2487
0
        .help(_("Request a 3D extent to be reported."));
2488
2489
0
    argParser->add_argument("-nogeomtype")
2490
0
        .flag()
2491
0
        .action([psOptions](const std::string &)
2492
0
                { psOptions->bGeomType = false; })
2493
0
        .help(_("Suppress layer geometry type printing."));
2494
2495
0
    argParser->add_argument("-wkt_format")
2496
0
        .store_into(psOptions->osWKTFormat)
2497
0
        .metavar("WKT1|WKT2|WKT2_2015|WKT2_2019")
2498
0
        .help(_("The WKT format used to display the SRS."));
2499
2500
0
    argParser->add_argument("-fielddomain")
2501
0
        .store_into(psOptions->osFieldDomain)
2502
0
        .metavar("<name>")
2503
0
        .help(_("Display details about a field domain."));
2504
2505
0
    argParser->add_argument("-if")
2506
0
        .append()
2507
0
        .metavar("<format>")
2508
0
        .action(
2509
0
            [psOptionsForBinary](const std::string &s)
2510
0
            {
2511
0
                if (psOptionsForBinary)
2512
0
                {
2513
0
                    if (GDALGetDriverByName(s.c_str()) == nullptr)
2514
0
                    {
2515
0
                        CPLError(CE_Warning, CPLE_AppDefined,
2516
0
                                 "%s is not a recognized driver", s.c_str());
2517
0
                    }
2518
0
                    psOptionsForBinary->aosAllowInputDrivers.AddString(
2519
0
                        s.c_str());
2520
0
                }
2521
0
            })
2522
0
        .help(_("Format/driver name(s) to try when opening the input file."));
2523
2524
0
    argParser->add_argument("-stdout")
2525
0
        .flag()
2526
0
        .store_into(psOptions->bStdoutOutput)
2527
0
        .hidden()
2528
0
        .help(_("Directly output on stdout (format=text mode only)"));
2529
2530
0
    argParser->add_argument("--cli")
2531
0
        .hidden()
2532
0
        .store_into(psOptions->bIsCli)
2533
0
        .help(_("Indicates that this is called from the gdal vector info CLI "
2534
0
                "utility."));
2535
2536
0
    auto &argFilename = argParser->add_argument("filename")
2537
0
                            .action(
2538
0
                                [psOptionsForBinary](const std::string &s)
2539
0
                                {
2540
0
                                    if (psOptionsForBinary)
2541
0
                                        psOptionsForBinary->osFilename = s;
2542
0
                                })
2543
0
                            .help(_("The data source to open."));
2544
0
    if (!psOptionsForBinary)
2545
0
        argFilename.nargs(argparse::nargs_pattern::optional);
2546
2547
0
    argParser->add_argument("layer")
2548
0
        .remaining()
2549
0
        .metavar("<layer_name>")
2550
0
        .help(_("Layer name."));
2551
2552
0
    return argParser;
2553
0
}
2554
2555
/************************************************************************/
2556
/*                    GDALVectorInfoGetParserUsage()                    */
2557
/************************************************************************/
2558
2559
std::string GDALVectorInfoGetParserUsage()
2560
0
{
2561
0
    try
2562
0
    {
2563
0
        GDALVectorInfoOptions sOptions;
2564
0
        GDALVectorInfoOptionsForBinary sOptionsForBinary;
2565
0
        auto argParser =
2566
0
            GDALVectorInfoOptionsGetParser(&sOptions, &sOptionsForBinary);
2567
0
        return argParser->usage();
2568
0
    }
2569
0
    catch (const std::exception &err)
2570
0
    {
2571
0
        CPLError(CE_Failure, CPLE_AppDefined, "Unexpected exception: %s",
2572
0
                 err.what());
2573
0
        return std::string();
2574
0
    }
2575
0
}
2576
2577
/************************************************************************/
2578
/*                      GDALVectorInfoOptionsNew()                      */
2579
/************************************************************************/
2580
2581
/**
2582
 * Allocates a GDALVectorInfoOptions struct.
2583
 *
2584
 * Note that  when this function is used a library function, and not from the
2585
 * ogrinfo utility, a dataset name must be specified if any layer names(s) are
2586
 * specified (if no layer name is specific, passing a dataset name is not
2587
 * needed). That dataset name may be a dummy one, as the dataset taken into
2588
 * account is the hDS parameter passed to GDALVectorInfo().
2589
 * Similarly the -oo switch in a non-ogrinfo context will be ignored, and it
2590
 * is the responsibility of the user to apply them when opening the hDS parameter
2591
 * passed to GDALVectorInfo().
2592
 *
2593
 * @param papszArgv NULL terminated list of options (potentially including
2594
 * filename and open options too), or NULL. The accepted options are the ones of
2595
 * the <a href="/programs/ogrinfo.html">ogrinfo</a> utility.
2596
 * @param psOptionsForBinary (output) may be NULL (and should generally be
2597
 * NULL), otherwise (ogrinfo_bin.cpp use case) must be allocated with
2598
 * GDALVectorInfoOptionsForBinaryNew() prior to this
2599
 * function. Will be filled with potentially present filename, open options,
2600
 * subdataset number...
2601
 * @return pointer to the allocated GDALVectorInfoOptions struct. Must be freed
2602
 * with GDALVectorInfoOptionsFree().
2603
 *
2604
 * @since GDAL 3.7
2605
 */
2606
2607
GDALVectorInfoOptions *
2608
GDALVectorInfoOptionsNew(char **papszArgv,
2609
                         GDALVectorInfoOptionsForBinary *psOptionsForBinary)
2610
0
{
2611
0
    auto psOptions = std::make_unique<GDALVectorInfoOptions>();
2612
2613
0
    try
2614
0
    {
2615
0
        auto argParser =
2616
0
            GDALVectorInfoOptionsGetParser(psOptions.get(), psOptionsForBinary);
2617
2618
        /* Special pre-processing to rewrite -fields=foo as "-fields" "FOO", and
2619
     * same for -geom=foo. */
2620
0
        CPLStringList aosArgv;
2621
0
        for (CSLConstList papszIter = papszArgv; papszIter && *papszIter;
2622
0
             ++papszIter)
2623
0
        {
2624
0
            if (STARTS_WITH(*papszIter, "-fields="))
2625
0
            {
2626
0
                aosArgv.AddString("-fields");
2627
0
                aosArgv.AddString(
2628
0
                    CPLString(*papszIter + strlen("-fields=")).toupper());
2629
0
            }
2630
0
            else if (STARTS_WITH(*papszIter, "-geom="))
2631
0
            {
2632
0
                aosArgv.AddString("-geom");
2633
0
                aosArgv.AddString(
2634
0
                    CPLString(*papszIter + strlen("-geom=")).toupper());
2635
0
            }
2636
0
            else
2637
0
            {
2638
0
                aosArgv.AddString(*papszIter);
2639
0
            }
2640
0
        }
2641
2642
0
        argParser->parse_args_without_binary_name(aosArgv.List());
2643
2644
0
        auto layers = argParser->present<std::vector<std::string>>("layer");
2645
0
        if (layers)
2646
0
        {
2647
0
            for (const auto &layer : *layers)
2648
0
            {
2649
0
                psOptions->aosLayers.AddString(layer.c_str());
2650
0
                psOptions->bAllLayers = false;
2651
0
            }
2652
0
        }
2653
2654
0
        if (auto oSpat = argParser->present<std::vector<double>>("-spat"))
2655
0
        {
2656
0
            const double dfMinX = (*oSpat)[0];
2657
0
            const double dfMinY = (*oSpat)[1];
2658
0
            const double dfMaxX = (*oSpat)[2];
2659
0
            const double dfMaxY = (*oSpat)[3];
2660
2661
0
            auto poPolygon =
2662
0
                std::make_unique<OGRPolygon>(dfMinX, dfMinY, dfMaxX, dfMaxY);
2663
0
            psOptions->poSpatialFilter.reset(poPolygon.release());
2664
0
        }
2665
2666
0
        if (!psOptions->osWHERE.empty() && psOptions->osWHERE[0] == '@')
2667
0
        {
2668
0
            GByte *pabyRet = nullptr;
2669
0
            if (VSIIngestFile(nullptr, psOptions->osWHERE.substr(1).c_str(),
2670
0
                              &pabyRet, nullptr, 10 * 1024 * 1024))
2671
0
            {
2672
0
                GDALRemoveBOM(pabyRet);
2673
0
                psOptions->osWHERE = reinterpret_cast<const char *>(pabyRet);
2674
0
                VSIFree(pabyRet);
2675
0
            }
2676
0
            else
2677
0
            {
2678
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
2679
0
                         psOptions->osWHERE.substr(1).c_str());
2680
0
                return nullptr;
2681
0
            }
2682
0
        }
2683
2684
0
        if (!psOptions->osSQLStatement.empty() &&
2685
0
            psOptions->osSQLStatement[0] == '@')
2686
0
        {
2687
0
            GByte *pabyRet = nullptr;
2688
0
            if (VSIIngestFile(nullptr,
2689
0
                              psOptions->osSQLStatement.substr(1).c_str(),
2690
0
                              &pabyRet, nullptr, 10 * 1024 * 1024))
2691
0
            {
2692
0
                GDALRemoveBOM(pabyRet);
2693
0
                char *pszSQLStatement = reinterpret_cast<char *>(pabyRet);
2694
0
                psOptions->osSQLStatement =
2695
0
                    CPLRemoveSQLComments(pszSQLStatement);
2696
0
                VSIFree(pabyRet);
2697
0
            }
2698
0
            else
2699
0
            {
2700
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
2701
0
                         psOptions->osSQLStatement.substr(1).c_str());
2702
0
                return nullptr;
2703
0
            }
2704
0
        }
2705
2706
0
        if (psOptionsForBinary)
2707
0
        {
2708
0
            psOptions->bStdoutOutput = true;
2709
0
            psOptionsForBinary->osSQLStatement = psOptions->osSQLStatement;
2710
0
        }
2711
2712
0
        if (psOptions->eFormat == FORMAT_JSON)
2713
0
        {
2714
0
            psOptions->bAllLayers = true;
2715
0
            psOptions->bSummaryOnly = true;
2716
0
            if (psOptions->aosExtraMDDomains.empty())
2717
0
                psOptions->aosExtraMDDomains.AddString("all");
2718
0
            psOptions->bStdoutOutput = false;
2719
0
        }
2720
2721
0
        if (psOptions->bSummaryUserRequested)
2722
0
            psOptions->bSummaryOnly = true;
2723
0
        else if (psOptions->bFeaturesUserRequested)
2724
0
            psOptions->bSummaryOnly = false;
2725
2726
0
        if (!psOptions->osDialect.empty() && !psOptions->osWHERE.empty() &&
2727
0
            psOptions->osSQLStatement.empty())
2728
0
        {
2729
0
            CPLError(CE_Warning, CPLE_AppDefined,
2730
0
                     "-dialect is ignored with -where. Use -sql instead");
2731
0
        }
2732
2733
        // Patch options when -schema is set
2734
0
        if (psOptions->bExportOgrSchema)
2735
0
        {
2736
            // TODO: validate and raise an error if incompatible options are set?
2737
            //       not strictly necessary given that -schema is an hidden option
2738
0
            psOptions->eFormat = FORMAT_JSON;
2739
0
            psOptions->bAllLayers = true;
2740
0
            psOptions->bShowMetadata = false;
2741
0
            psOptions->bListMDD = false;
2742
0
            psOptions->bFeatureCount = false;
2743
0
            psOptions->bIsCli = true;
2744
0
            psOptions->bSummaryOnly = false;
2745
0
            psOptions->bExtent = false;
2746
0
            psOptions->bExtent3D = false;
2747
0
        }
2748
2749
0
        return psOptions.release();
2750
0
    }
2751
0
    catch (const std::exception &err)
2752
0
    {
2753
0
        CPLError(CE_Failure, CPLE_AppDefined, "%s", err.what());
2754
0
        return nullptr;
2755
0
    }
2756
0
}