File: ogrtindex.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (207 lines) | stat: -rw-r--r-- 6,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/******************************************************************************
 *
 * Project:  OpenGIS Simple Features Reference Implementation
 * Purpose:  Program to generate a UMN MapServer compatible tile index for a
 *           set of OGR data sources.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2002, Frank Warmerdam
 * Copyright (c) 2007-2010, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "cpl_port.h"

#include <vector>

#include "cpl_conv.h"
#include "cpl_string.h"
#include "gdalargumentparser.h"

#include "gdalalg_vector_index.h"
#include "commonutils.h"

/************************************************************************/
/*                                main()                                */
/************************************************************************/

MAIN_START(nArgc, papszArgv)

{
    EarlySetConfigOptions(nArgc, papszArgv);

    GDALAllRegister();

    nArgc = GDALGeneralCmdLineProcessor(nArgc, &papszArgv, 0);
    if (nArgc < 1)
        exit(-nArgc);

    CPLStringList aosArgv;
    for (int i = 0; i < nArgc; i++)
    {
        aosArgv.AddString(papszArgv[i]);
    }

    CSLDestroy(papszArgv);

    /* -------------------------------------------------------------------- */
    /*      Processing command line arguments.                              */
    /* -------------------------------------------------------------------- */
    std::string osOutputFormat;
    std::string osTileIndexField;
    std::string osOutputName;
    bool bWriteAbsolutePath{false};
    bool bSkipDifferentProjection{false};
    bool bAcceptDifferentSchemas{false};
    std::string osTargetSRS;
    std::string osSrcSRSName;
    std::string osSrcSRSFormat;
    std::vector<std::string> aosSrcDatasets;
    std::vector<std::string> aosLayerNames;
    std::vector<int> anLayerNumbers;

    GDALArgumentParser argParser{"ogrtindex", true};

    argParser.add_description(
        _("Program to generate a UMN MapServer compatible "
          "tile index for a set of OGR data sources."));

    argParser.add_epilog(
        _("For more details, see the full documentation for ogrtindex "
          "at\nhttps://gdal.org/programs/ogrtindex.html"));

    argParser.add_argument("-lnum")
        .metavar("<n>")
        .append()
        .scan<'i', int>()
        .store_into(anLayerNumbers)
        .help(
            _("Add layer number <n> from each source file in the tile index."));

    argParser.add_argument("-lname")
        .metavar("<name>")
        .append()
        .store_into(aosLayerNames)
        .help(_(
            "Add layer named <name> from each source file in the tile index."));

    argParser.add_output_format_argument(osOutputFormat);

    argParser.add_argument("-tileindex")
        .metavar("<tileindex>")
        .default_value("LOCATION")
        .nargs(1)
        .store_into(osTileIndexField)
        .help(_("Name to use for the dataset name."));

    argParser.add_argument("-write_absolute_path")
        .flag()
        .store_into(bWriteAbsolutePath)
        .help(_("Write absolute path of the source file in the tile index."));

    argParser.add_argument("-skip_different_projection")
        .flag()
        .store_into(bSkipDifferentProjection)
        .help(_("Skip layers that are not in the same projection as the first "
                "layer."));

    argParser.add_argument("-t_srs")
        .metavar("<srs_def>")
        .store_into(osTargetSRS)
        .help(
            _("Extent of input files will be transformed to the desired target "
              "coordinate reference system."));

    argParser.add_argument("-src_srs_name")
        .metavar("<field_name>")
        .store_into(osSrcSRSName)
        .help(_("Name of the field to store the SRS of each tile."));

    argParser.add_argument("-src_srs_format")
        .metavar("{AUTO|WKT|EPSG|PROJ}")
        .choices("AUTO", "WKT", "EPSG", "PROJ")
        .store_into(osSrcSRSFormat)
        .help(_("Format of the source SRS to store in the tile index file."));

    argParser.add_argument("-accept_different_schemas")
        .flag()
        .store_into(bAcceptDifferentSchemas)
        .help(_(
            "Disable check for identical schemas for layers in input files."));

    argParser.add_argument("output_dataset")
        .metavar("<output_dataset>")
        .store_into(osOutputName)
        .help(_("Name of the output dataset."));

    argParser.add_argument("src_dataset")
        .metavar("<src_dataset>")
        .nargs(nargs_pattern::at_least_one)
        .store_into(aosSrcDatasets)
        .help(_("Name of the source dataset(s)."));

    try
    {
        argParser.parse_args(aosArgv);
    }
    catch (const std::exception &e)
    {
        argParser.display_error_and_usage(e);
        GDALDestroy();
        return 1;
    }

    /* -------------------------------------------------------------------- */
    /*      Validate input                                                  */
    /* -------------------------------------------------------------------- */

    //srs_name must be specified when srs_format is specified.
    if (argParser.is_used("-src_srs_format") &&
        !argParser.is_used("-src_srs_name"))
    {
        fprintf(stderr, "-src_srs_name must be specified when -src_srs_format "
                        "is specified.\n");
        GDALDestroy();
        return 1;
    }

    GDALVectorIndexAlgorithm alg;
    alg["called-from-ogrtindex"] = true;
    alg["input"] = aosSrcDatasets;
    alg["output"] = osOutputName;
    if (!osOutputFormat.empty())
        alg["output-format"] = osOutputFormat;
    if (!aosLayerNames.empty())
        alg["source-layer-name"] = aosLayerNames;
    if (!anLayerNumbers.empty())
        alg["source-layer-index"] = anLayerNumbers;
    if (!osSrcSRSName.empty())
        alg["source-crs-field-name"] = osSrcSRSName;
    if (!osSrcSRSFormat.empty())
        alg["source-crs-format"] = osSrcSRSFormat;
    if (!osTargetSRS.empty())
        alg["dst-crs"] = osTargetSRS;
    alg["accept-different-schemas"] = bAcceptDifferentSchemas;
    if (bSkipDifferentProjection)
        alg["skip-different-crs"] = bSkipDifferentProjection;
    alg["absolute-path"] = bWriteAbsolutePath;
    alg["location-name"] = osTileIndexField;

    {
        CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
        if (std::unique_ptr<GDALDataset>(
                GDALDataset::Open(osOutputName.c_str(), GDAL_OF_VECTOR)))
            alg["append"] = true;
    }

    const int nRetCode =
        CPLGetLastErrorType() == CE_None && alg.Run() && alg.Finalize() ? 0 : 1;

    GDALDestroy();

    return nRetCode;
}

MAIN_END