LCOV - code coverage report
Current view: top level - apps - ogr2ogr_lib.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 3217 3996 80.5 %
Date: 2026-04-18 22:07:07 Functions: 104 140 74.3 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Simple client for translating between formats.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 1999, Frank Warmerdam
       9             :  * Copyright (c) 2008-2015, Even Rouault <even dot rouault at spatialys.com>
      10             :  * Copyright (c) 2015, Faza Mahamood
      11             :  *
      12             :  * SPDX-License-Identifier: MIT
      13             :  ****************************************************************************/
      14             : 
      15             : #include "cpl_port.h"
      16             : #include "gdal_utils.h"
      17             : #include "gdal_utils_priv.h"
      18             : #include "gdalargumentparser.h"
      19             : 
      20             : #include <cassert>
      21             : #include <climits>
      22             : #include <cstdio>
      23             : #include <cstdlib>
      24             : #include <cstring>
      25             : 
      26             : #include <algorithm>
      27             : #include <atomic>
      28             : #include <future>
      29             : #include <limits>
      30             : #include <map>
      31             : #include <memory>
      32             : #include <mutex>
      33             : #include <set>
      34             : #include <unordered_set>
      35             : #include <string>
      36             : #include <utility>
      37             : #include <vector>
      38             : 
      39             : #include "commonutils.h"
      40             : #include "cpl_conv.h"
      41             : #include "cpl_error.h"
      42             : #include "cpl_multiproc.h"
      43             : #include "cpl_progress.h"
      44             : #include "cpl_string.h"
      45             : #include "cpl_time.h"
      46             : #include "cpl_vsi.h"
      47             : #include "gdal.h"
      48             : #include "gdal_alg.h"
      49             : #include "gdal_alg_priv.h"
      50             : #include "gdal_priv.h"
      51             : #include "gdal_thread_pool.h"
      52             : #include "ogr_api.h"
      53             : #include "ogr_core.h"
      54             : #include "ogr_feature.h"
      55             : #include "ogr_featurestyle.h"
      56             : #include "ogr_geometry.h"
      57             : #include "ogr_p.h"
      58             : #include "ogr_recordbatch.h"
      59             : #include "ogr_spatialref.h"
      60             : #include "ogrlayerarrow.h"
      61             : #include "ogrlayerdecorator.h"
      62             : #include "ogrsf_frmts.h"
      63             : #include "ogr_wkb.h"
      64             : #include "ogrct_priv.h"
      65             : 
      66             : typedef enum
      67             : {
      68             :     GEOMOP_NONE,
      69             :     GEOMOP_SEGMENTIZE,
      70             :     GEOMOP_SIMPLIFY_PRESERVE_TOPOLOGY,
      71             : } GeomOperation;
      72             : 
      73             : typedef enum
      74             : {
      75             :     GTC_DEFAULT,
      76             :     GTC_PROMOTE_TO_MULTI,
      77             :     GTC_CONVERT_TO_LINEAR,
      78             :     GTC_CONVERT_TO_CURVE,
      79             :     GTC_PROMOTE_TO_MULTI_AND_CONVERT_TO_LINEAR,
      80             : } GeomTypeConversion;
      81             : 
      82             : #define GEOMTYPE_UNCHANGED -2
      83             : 
      84             : #define COORD_DIM_UNCHANGED -1
      85             : #define COORD_DIM_LAYER_DIM -2
      86             : #define COORD_DIM_XYM -3
      87             : 
      88             : #define TZ_OFFSET_INVALID INT_MIN
      89             : 
      90             : /************************************************************************/
      91             : /*                      GDALVectorTranslateOptions                      */
      92             : /************************************************************************/
      93             : 
      94             : /** Options for use with GDALVectorTranslate(). GDALVectorTranslateOptions* must
      95             :  * be allocated and freed with GDALVectorTranslateOptionsNew() and
      96             :  * GDALVectorTranslateOptionsFree() respectively.
      97             :  */
      98             : struct GDALVectorTranslateOptions
      99             : {
     100             :     // All arguments passed to GDALVectorTranslate() except the positional
     101             :     // ones (that is dataset names and layer names)
     102             :     CPLStringList aosArguments{};
     103             : 
     104             :     /*! continue after a failure, skipping the failed feature */
     105             :     bool bSkipFailures = false;
     106             : 
     107             :     /*! use layer level transaction. If set to FALSE, then it is interpreted as
     108             :      * dataset level transaction. */
     109             :     int nLayerTransaction = -1;
     110             : 
     111             :     /*! force the use of particular transaction type based on
     112             :      * GDALVectorTranslate::nLayerTransaction */
     113             :     bool bForceTransaction = false;
     114             : 
     115             :     /*! group nGroupTransactions features per transaction.
     116             :        Increase the value for better performance when writing into DBMS drivers
     117             :        that have transaction support. nGroupTransactions can be set to -1 to
     118             :        load the data into a single transaction */
     119             :     int nGroupTransactions = 100 * 1000;
     120             : 
     121             :     /*! If provided, only the feature with this feature id will be reported.
     122             :        Operates exclusive of the spatial or attribute queries. Note: if you want
     123             :        to select several features based on their feature id, you can also use
     124             :        the fact the 'fid' is a special field recognized by OGR SQL. So
     125             :        GDALVectorTranslateOptions::pszWHERE = "fid in (1,3,5)" would select
     126             :        features 1, 3 and 5. */
     127             :     GIntBig nFIDToFetch = OGRNullFID;
     128             : 
     129             :     /*! allow or suppress progress monitor and other non-error output */
     130             :     bool bQuiet = false;
     131             : 
     132             :     /*! output file format name */
     133             :     std::string osFormat{};
     134             : 
     135             :     /*! list of layers of the source dataset which needs to be selected */
     136             :     CPLStringList aosLayers{};
     137             : 
     138             :     /*! dataset creation option (format specific) */
     139             :     CPLStringList aosDSCO{};
     140             : 
     141             :     /*! layer creation option (format specific) */
     142             :     CPLStringList aosLCO{};
     143             : 
     144             :     /*! access modes */
     145             :     GDALVectorTranslateAccessMode eAccessMode = ACCESS_CREATION;
     146             : 
     147             :     /*! whether to use UpsertFeature() instead of CreateFeature() */
     148             :     bool bUpsert = false;
     149             : 
     150             :     /*! It has the effect of adding, to existing target layers, the new fields
     151             :        found in source layers. This option is useful when merging files that
     152             :        have non-strictly identical structures. This might not work for output
     153             :        formats that don't support adding fields to existing non-empty layers. */
     154             :     bool bAddMissingFields = false;
     155             : 
     156             :     /*! It must be set to true to trigger reprojection, otherwise only SRS
     157             :      * assignment is done. */
     158             :     bool bTransform = false;
     159             : 
     160             :     /*! output SRS. GDALVectorTranslateOptions::bTransform must be set to true
     161             :        to trigger reprojection, otherwise only SRS assignment is done. */
     162             :     std::string osOutputSRSDef{};
     163             : 
     164             :     /*! Coordinate epoch of source SRS */
     165             :     double dfSourceCoordinateEpoch = 0;
     166             : 
     167             :     /*! Coordinate epoch of output SRS */
     168             :     double dfOutputCoordinateEpoch = 0;
     169             : 
     170             :     /*! override source SRS */
     171             :     std::string osSourceSRSDef{};
     172             : 
     173             :     /*! PROJ pipeline */
     174             :     std::string osCTPipeline{};
     175             : 
     176             :     /*! Transform options. */
     177             :     CPLStringList aosCTOptions{};
     178             : 
     179             :     bool bNullifyOutputSRS = false;
     180             : 
     181             :     /*! If set to false, then field name matching between source and existing
     182             :        target layer is done in a more relaxed way if the target driver has an
     183             :        implementation for it. */
     184             :     bool bExactFieldNameMatch = true;
     185             : 
     186             :     /*! an alternate name to the new layer */
     187             :     std::string osNewLayerName{};
     188             : 
     189             :     /*! attribute query (like SQL WHERE) */
     190             :     std::string osWHERE{};
     191             : 
     192             :     /*! name of the geometry field on which the spatial filter operates on. */
     193             :     std::string osGeomField{};
     194             : 
     195             :     /*! whether osGeomField is set (useful for empty strings) */
     196             :     bool bGeomFieldSet = false;
     197             : 
     198             :     /*! whether -select has been specified. This is of course true when
     199             :      * !aosSelFields.empty(), but this can also be set when an empty string
     200             :      * has been to disable fields. */
     201             :     bool bSelFieldsSet = false;
     202             : 
     203             :     /*! list of fields from input layer to copy to the new layer.
     204             :      * Geometry fields can also be specified in the list. */
     205             :     CPLStringList aosSelFields{};
     206             : 
     207             :     /*! SQL statement to execute. The resulting table/layer will be saved to the
     208             :      * output. */
     209             :     std::string osSQLStatement{};
     210             : 
     211             :     /*! SQL dialect. In some cases can be used to use (unoptimized) OGR SQL
     212             :        instead of the native SQL of an RDBMS by using "OGRSQL". The "SQLITE"
     213             :        dialect can also be used with any datasource. */
     214             :     std::string osDialect{};
     215             : 
     216             :     /*! the geometry type for the created layer */
     217             :     int eGType = GEOMTYPE_UNCHANGED;
     218             : 
     219             :     GeomTypeConversion eGeomTypeConversion = GTC_DEFAULT;
     220             : 
     221             :     /*! Geometric operation to perform */
     222             :     GeomOperation eGeomOp = GEOMOP_NONE;
     223             : 
     224             :     /*! the parameter to geometric operation */
     225             :     double dfGeomOpParam = 0;
     226             : 
     227             :     /*! Whether to run MakeValid */
     228             :     bool bMakeValid = false;
     229             : 
     230             :     /*! Whether to run OGRGeometry::IsValid */
     231             :     bool bSkipInvalidGeom = false;
     232             : 
     233             :     /*! list of field types to convert to a field of type string in the
     234             :        destination layer. Valid types are: Integer, Integer64, Real, String,
     235             :        Date, Time, DateTime, Binary, IntegerList, Integer64List, RealList,
     236             :        StringList. Special value "All" can be used to convert all fields to
     237             :        strings. This is an alternate way to using the CAST operator of OGR SQL,
     238             :        that may avoid typing a long SQL query. Note that this does not influence
     239             :        the field types used by the source driver, and is only an afterwards
     240             :         conversion. */
     241             :     CPLStringList aosFieldTypesToString{};
     242             : 
     243             :     /*! list of field types and the field type after conversion in the
     244             :        destination layer.
     245             :         ("srctype1=dsttype1","srctype2=dsttype2",...).
     246             :         Valid types are : Integer, Integer64, Real, String, Date, Time,
     247             :        DateTime, Binary, IntegerList, Integer64List, RealList, StringList. Types
     248             :        can also include subtype between parenthesis, such as Integer(Boolean),
     249             :        Real(Float32), ... Special value "All" can be used to convert all fields
     250             :        to another type. This is an alternate way to using the CAST operator of
     251             :        OGR SQL, that may avoid typing a long SQL query. This is a generalization
     252             :        of GDALVectorTranslateOptions::papszFieldTypeToString. Note that this
     253             :        does not influence the field types used by the source driver, and is only
     254             :        an afterwards conversion. */
     255             :     CPLStringList aosMapFieldType{};
     256             : 
     257             :     /*! set field width and precision to 0 */
     258             :     bool bUnsetFieldWidth = false;
     259             : 
     260             :     /*! display progress on terminal. Only works if input layers have the "fast
     261             :     feature count" capability */
     262             :     bool bDisplayProgress = false;
     263             : 
     264             :     /*! split geometries crossing the dateline meridian */
     265             :     bool bWrapDateline = false;
     266             : 
     267             :     /*! offset from dateline in degrees (default long. = +/- 10deg, geometries
     268             :     within 170deg to -170deg will be split) */
     269             :     double dfDateLineOffset = 10.0;
     270             : 
     271             :     /*! clip geometries when it is set to true */
     272             :     bool bClipSrc = false;
     273             : 
     274             :     std::shared_ptr<OGRGeometry> poClipSrc{};
     275             : 
     276             :     /*! clip datasource */
     277             :     std::string osClipSrcDS{};
     278             : 
     279             :     /*! select desired geometries using an SQL query */
     280             :     std::string osClipSrcSQL{};
     281             : 
     282             :     /*! selected named layer from the source clip datasource */
     283             :     std::string osClipSrcLayer{};
     284             : 
     285             :     /*! restrict desired geometries based on attribute query */
     286             :     std::string osClipSrcWhere{};
     287             : 
     288             :     std::shared_ptr<OGRGeometry> poClipDst{};
     289             : 
     290             :     /*! destination clip datasource */
     291             :     std::string osClipDstDS{};
     292             : 
     293             :     /*! select desired geometries using an SQL query */
     294             :     std::string osClipDstSQL{};
     295             : 
     296             :     /*! selected named layer from the destination clip datasource */
     297             :     std::string osClipDstLayer{};
     298             : 
     299             :     /*! restrict desired geometries based on attribute query */
     300             :     std::string osClipDstWhere{};
     301             : 
     302             :     /*! split fields of type StringList, RealList or IntegerList into as many
     303             :        fields of type String, Real or Integer as necessary. */
     304             :     bool bSplitListFields = false;
     305             : 
     306             :     /*! limit the number of subfields created for each split field. */
     307             :     int nMaxSplitListSubFields = -1;
     308             : 
     309             :     /*! produce one feature for each geometry in any kind of geometry collection
     310             :        in the source file */
     311             :     bool bExplodeCollections = false;
     312             : 
     313             :     /*! uses the specified field to fill the Z coordinates of geometries */
     314             :     std::string osZField{};
     315             : 
     316             :     /*! the list of field indexes to be copied from the source to the
     317             :        destination. The (n)th value specified in the list is the index of the
     318             :        field in the target layer definition in which the n(th) field of the
     319             :        source layer must be copied. Index count starts at zero. There must be
     320             :         exactly as many values in the list as the count of the fields in the
     321             :        source layer. We can use the "identity" option to specify that the fields
     322             :        should be transferred by using the same order. This option should be used
     323             :        along with the GDALVectorTranslateOptions::eAccessMode = ACCESS_APPEND
     324             :        option. */
     325             :     CPLStringList aosFieldMap{};
     326             : 
     327             :     /*! force the coordinate dimension to nCoordDim (valid values are 2 or 3).
     328             :        This affects both the layer geometry type, and feature geometries. */
     329             :     int nCoordDim = COORD_DIM_UNCHANGED;
     330             : 
     331             :     /*! destination dataset open option (format specific), only valid in update
     332             :      * mode */
     333             :     CPLStringList aosDestOpenOptions{};
     334             : 
     335             :     /*! If set to true, does not propagate not-nullable constraints to target
     336             :        layer if they exist in source layer */
     337             :     bool bForceNullable = false;
     338             : 
     339             :     /*! If set to true, for each field with a coded field domains, create a
     340             :        field that contains the description of the coded value. */
     341             :     bool bResolveDomains = false;
     342             : 
     343             :     /*! If set to true, empty string values will be treated as null */
     344             :     bool bEmptyStrAsNull = false;
     345             : 
     346             :     /*! If set to true, does not propagate default field values to target layer
     347             :        if they exist in source layer */
     348             :     bool bUnsetDefault = false;
     349             : 
     350             :     /*! to prevent the new default behavior that consists in, if the output
     351             :        driver has a FID layer creation option and we are not in append mode, to
     352             :        preserve the name of the source FID column and source feature IDs */
     353             :     bool bUnsetFid = false;
     354             : 
     355             :     /*! use the FID of the source features instead of letting the output driver
     356             :        to automatically assign a new one. If not in append mode, this behavior
     357             :        becomes the default if the output driver has a FID layer creation option.
     358             :        In which case the name of the source FID column will be used and source
     359             :        feature IDs will be attempted to be preserved. This behavior can be
     360             :         disabled by option GDALVectorTranslateOptions::bUnsetFid */
     361             :     bool bPreserveFID = false;
     362             : 
     363             :     /*! set it to false to disable copying of metadata from source dataset and
     364             :        layers into target dataset and layers, when supported by output driver.
     365             :      */
     366             :     bool bCopyMD = true;
     367             : 
     368             :     /*! list of metadata key and value to set on the output dataset, when
     369             :        supported by output driver.
     370             :         ("META-TAG1=VALUE1","META-TAG2=VALUE2") */
     371             :     CPLStringList aosMetadataOptions{};
     372             : 
     373             :     /*! override spatial filter SRS */
     374             :     std::string osSpatSRSDef{};
     375             : 
     376             :     /*! list of ground control points to be added */
     377             :     std::vector<gdal::GCP> asGCPs{};
     378             : 
     379             :     /*! order of polynomial used for warping (1 to 3). The default is to select
     380             :        a polynomial order based on the number of GCPs */
     381             :     int nTransformOrder = 0;
     382             : 
     383             :     /*! spatial query extents, in the SRS of the source layer(s) (or the one
     384             :        specified with GDALVectorTranslateOptions::pszSpatSRSDef). Only features
     385             :        whose geometry intersects the extents will be selected. The geometries
     386             :        will not be clipped unless GDALVectorTranslateOptions::bClipSrc is true.
     387             :      */
     388             :     std::shared_ptr<OGRGeometry> poSpatialFilter{};
     389             : 
     390             :     /*! the progress function to use */
     391             :     GDALProgressFunc pfnProgress = nullptr;
     392             : 
     393             :     /*! pointer to the progress data variable */
     394             :     void *pProgressData = nullptr;
     395             : 
     396             :     /*! Whether layer and feature native data must be transferred. */
     397             :     bool bNativeData = true;
     398             : 
     399             :     /*! Maximum number of features, or -1 if no limit. */
     400             :     GIntBig nLimit = -1;
     401             : 
     402             :     /*! Wished offset w.r.t UTC of dateTime */
     403             :     int nTZOffsetInSec = TZ_OFFSET_INVALID;
     404             : 
     405             :     /*! Geometry X,Y coordinate resolution */
     406             :     double dfXYRes = OGRGeomCoordinatePrecision::UNKNOWN;
     407             : 
     408             :     /*! Unit of dXYRes. empty string, "m", "mm" or "deg" */
     409             :     std::string osXYResUnit{};
     410             : 
     411             :     /*! Geometry Z coordinate resolution */
     412             :     double dfZRes = OGRGeomCoordinatePrecision::UNKNOWN;
     413             : 
     414             :     /*! Unit of dfZRes. empty string, "m" or "mm" */
     415             :     std::string osZResUnit{};
     416             : 
     417             :     /*! Geometry M coordinate resolution */
     418             :     double dfMRes = OGRGeomCoordinatePrecision::UNKNOWN;
     419             : 
     420             :     /*! Whether to unset geometry coordinate precision */
     421             :     bool bUnsetCoordPrecision = false;
     422             : 
     423             :     /*! set to true to prevent overwriting existing dataset */
     424             :     bool bNoOverwrite = false;
     425             : 
     426             :     /*! set to true to customize error messages when called from "new" (GDAL 3.11) CLI or Algorithm API */
     427             :     bool bInvokedFromGdalAlgorithm = false;
     428             : };
     429             : 
     430             : struct TargetLayerInfo
     431             : {
     432             :     OGRLayer *m_poSrcLayer = nullptr;
     433             :     GIntBig m_nFeaturesRead = 0;
     434             :     bool m_bPerFeatureCT = 0;
     435             :     OGRLayer *m_poDstLayer = nullptr;
     436             :     bool m_bUseWriteArrowBatch = false;
     437             : 
     438             :     struct ReprojectionInfo
     439             :     {
     440             :         std::unique_ptr<OGRCoordinateTransformation> m_poCT{};
     441             :         CPLStringList m_aosTransformOptions{};
     442             :         bool m_bCanInvalidateValidity = true;
     443             :         bool m_bWarnAboutDifferentCoordinateOperations = false;
     444             :         double m_dfLeftX = std::numeric_limits<double>::max();
     445             :         double m_dfLeftY = 0;
     446             :         double m_dfLeftZ = 0;
     447             :         double m_dfRightX = -std::numeric_limits<double>::max();
     448             :         double m_dfRightY = 0;
     449             :         double m_dfRightZ = 0;
     450             :         double m_dfBottomX = 0;
     451             :         double m_dfBottomY = std::numeric_limits<double>::max();
     452             :         double m_dfBottomZ = 0;
     453             :         double m_dfTopX = 0;
     454             :         double m_dfTopY = -std::numeric_limits<double>::max();
     455             :         double m_dfTopZ = 0;
     456             : 
     457       13076 :         void UpdateExtremePoints(double dfX, double dfY, double dfZ)
     458             :         {
     459       13076 :             if (dfX < m_dfLeftX)
     460             :             {
     461         237 :                 m_dfLeftX = dfX;
     462         237 :                 m_dfLeftY = dfY;
     463         237 :                 m_dfLeftZ = dfZ;
     464             :             }
     465       13076 :             if (dfX > m_dfRightX)
     466             :             {
     467       10373 :                 m_dfRightX = dfX;
     468       10373 :                 m_dfRightY = dfY;
     469       10373 :                 m_dfRightZ = dfZ;
     470             :             }
     471       13076 :             if (dfY < m_dfBottomY)
     472             :             {
     473         411 :                 m_dfBottomX = dfX;
     474         411 :                 m_dfBottomY = dfY;
     475         411 :                 m_dfBottomZ = dfZ;
     476             :             }
     477       13076 :             if (dfY > m_dfTopY)
     478             :             {
     479       10219 :                 m_dfTopX = dfX;
     480       10219 :                 m_dfTopY = dfY;
     481       10219 :                 m_dfTopZ = 0;
     482             :             }
     483       13076 :         }
     484             :     };
     485             : 
     486             :     std::vector<ReprojectionInfo> m_aoReprojectionInfo{};
     487             : 
     488             :     std::vector<int> m_anMap{};
     489             : 
     490             :     struct ResolvedInfo
     491             :     {
     492             :         int nSrcField;
     493             :         const OGRFieldDomain *poDomain;
     494             :     };
     495             : 
     496             :     std::map<int, ResolvedInfo> m_oMapResolved{};
     497             :     std::map<const OGRFieldDomain *, std::map<std::string, std::string>>
     498             :         m_oMapDomainToKV{};
     499             :     int m_iSrcZField = -1;
     500             :     int m_iSrcFIDField = -1;
     501             :     int m_iRequestedSrcGeomField = -1;
     502             :     bool m_bPreserveFID = false;
     503             :     const char *m_pszCTPipeline = nullptr;
     504             :     CPLStringList m_aosCTOptions{};
     505             :     bool m_bCanAvoidSetFrom = false;
     506             :     const char *m_pszSpatSRSDef = nullptr;
     507             :     OGRGeometryH m_hSpatialFilter = nullptr;
     508             :     const char *m_pszGeomField = nullptr;
     509             :     std::vector<int> m_anDateTimeFieldIdx{};
     510             :     bool m_bSupportCurves = false;
     511             :     bool m_bSupportZ = false;
     512             :     bool m_bSupportM = false;
     513             :     bool m_bHasWarnedAboutCurves = false;
     514             :     bool m_bHasWarnedAboutZ = false;
     515             :     bool m_bHasWarnedAboutM = false;
     516             :     OGRArrowArrayStream m_sArrowArrayStream{};
     517             : 
     518             :     void CheckSameCoordinateOperation() const;
     519             : };
     520             : 
     521             : struct AssociatedLayers
     522             : {
     523             :     OGRLayer *poSrcLayer = nullptr;
     524             :     std::unique_ptr<TargetLayerInfo> psInfo{};
     525             : };
     526             : 
     527             : class SetupTargetLayer
     528             : {
     529             :   public:
     530             :     GDALDataset *m_poSrcDS = nullptr;
     531             :     GDALDataset *m_poDstDS = nullptr;
     532             :     CSLConstList m_papszLCO = nullptr;
     533             :     const OGRSpatialReference *m_poUserSourceSRS = nullptr;
     534             :     const OGRSpatialReference *m_poOutputSRS = nullptr;
     535             :     bool m_bTransform = false;
     536             :     bool m_bNullifyOutputSRS = false;
     537             :     bool m_bSelFieldsSet = false;
     538             :     CSLConstList m_papszSelFields = nullptr;
     539             :     bool m_bAppend = false;
     540             :     bool m_bAddMissingFields = false;
     541             :     int m_eGType = 0;
     542             :     GeomTypeConversion m_eGeomTypeConversion = GTC_DEFAULT;
     543             :     int m_nCoordDim = 0;
     544             :     bool m_bOverwrite = false;
     545             :     CSLConstList m_papszFieldTypesToString = nullptr;
     546             :     CSLConstList m_papszMapFieldType = nullptr;
     547             :     bool m_bUnsetFieldWidth = false;
     548             :     bool m_bExplodeCollections = false;
     549             :     const char *m_pszZField = nullptr;
     550             :     CSLConstList m_papszFieldMap = nullptr;
     551             :     const char *m_pszWHERE = nullptr;
     552             :     bool m_bExactFieldNameMatch = false;
     553             :     bool m_bQuiet = false;
     554             :     bool m_bForceNullable = false;
     555             :     bool m_bResolveDomains = false;
     556             :     bool m_bUnsetDefault = false;
     557             :     bool m_bUnsetFid = false;
     558             :     bool m_bPreserveFID = false;
     559             :     bool m_bCopyMD = false;
     560             :     bool m_bNativeData = false;
     561             :     bool m_bNewDataSource = false;
     562             :     const char *m_pszCTPipeline = nullptr;
     563             :     CPLStringList m_aosCTOptions{};
     564             : 
     565             :     std::unique_ptr<TargetLayerInfo>
     566             :     Setup(OGRLayer *poSrcLayer, const char *pszNewLayerName,
     567             :           GDALVectorTranslateOptions *psOptions, GIntBig &nTotalEventsDone);
     568             : 
     569             :   private:
     570             :     bool CanUseWriteArrowBatch(OGRLayer *poSrcLayer, OGRLayer *poDstLayer,
     571             :                                bool bJustCreatedLayer,
     572             :                                const GDALVectorTranslateOptions *psOptions,
     573             :                                bool bPreserveFID, bool &bError,
     574             :                                OGRArrowArrayStream &streamSrc);
     575             : 
     576             :     void SetIgnoredFields(OGRLayer *poSrcLayer);
     577             : };
     578             : 
     579             : class LayerTranslator
     580             : {
     581             :     bool TranslateArrow(TargetLayerInfo *psInfo, GIntBig nCountLayerFeatures,
     582             :                         GIntBig *pnReadFeatureCount,
     583             :                         GDALProgressFunc pfnProgress, void *pProgressArg,
     584             :                         const GDALVectorTranslateOptions *psOptions);
     585             : 
     586             :   public:
     587             :     GDALDataset *m_poSrcDS = nullptr;
     588             :     GDALDataset *m_poODS = nullptr;
     589             :     bool m_bTransform = false;
     590             :     bool m_bWrapDateline = false;
     591             :     CPLString m_osDateLineOffset{};
     592             :     const OGRSpatialReference *m_poOutputSRS = nullptr;
     593             :     bool m_bNullifyOutputSRS = false;
     594             :     const OGRSpatialReference *m_poUserSourceSRS = nullptr;
     595             :     OGRCoordinateTransformation *m_poGCPCoordTrans = nullptr;
     596             :     int m_eGType = -1;
     597             :     GeomTypeConversion m_eGeomTypeConversion = GTC_DEFAULT;
     598             :     bool m_bMakeValid = false;
     599             :     bool m_bSkipInvalidGeom = false;
     600             :     int m_nCoordDim = 0;
     601             :     GeomOperation m_eGeomOp = GEOMOP_NONE;
     602             :     double m_dfGeomOpParam = 0;
     603             : 
     604             :     OGRGeometry *m_poClipSrcOri = nullptr;
     605             :     bool m_bWarnedClipSrcSRS = false;
     606             :     std::unique_ptr<OGRGeometry> m_poClipSrcReprojectedToSrcSRS{};
     607             :     const OGRSpatialReference *m_poClipSrcReprojectedToSrcSRS_SRS = nullptr;
     608             :     OGREnvelope m_oClipSrcEnv{};
     609             :     bool m_bClipSrcIsRectangle = false;
     610             : 
     611             :     OGRGeometry *m_poClipDstOri = nullptr;
     612             :     bool m_bWarnedClipDstSRS = false;
     613             :     std::unique_ptr<OGRGeometry> m_poClipDstReprojectedToDstSRS{};
     614             :     const OGRSpatialReference *m_poClipDstReprojectedToDstSRS_SRS = nullptr;
     615             :     OGREnvelope m_oClipDstEnv{};
     616             :     bool m_bClipDstIsRectangle = false;
     617             : 
     618             :     bool m_bExplodeCollections = false;
     619             :     bool m_bNativeData = false;
     620             :     GIntBig m_nLimit = -1;
     621             :     OGRGeometryFactory::TransformWithOptionsCache m_transformWithOptionsCache{};
     622             : 
     623             :     bool Translate(std::unique_ptr<OGRFeature> poFeatureIn,
     624             :                    TargetLayerInfo *psInfo, GIntBig nCountLayerFeatures,
     625             :                    GIntBig *pnReadFeatureCount, GIntBig &nTotalEventsDone,
     626             :                    GDALProgressFunc pfnProgress, void *pProgressArg,
     627             :                    const GDALVectorTranslateOptions *psOptions);
     628             : 
     629             :   private:
     630             :     struct ClipGeomDesc
     631             :     {
     632             :         const OGRGeometry *poGeom = nullptr;
     633             :         const OGREnvelope *poEnv = nullptr;
     634             :         bool bGeomIsRectangle = false;
     635             :     };
     636             : 
     637             :     ClipGeomDesc GetDstClipGeom(const OGRSpatialReference *poGeomSRS);
     638             :     ClipGeomDesc GetSrcClipGeom(const OGRSpatialReference *poGeomSRS);
     639             : };
     640             : 
     641             : static OGRLayer *GetLayerAndOverwriteIfNecessary(GDALDataset *poDstDS,
     642             :                                                  const char *pszNewLayerName,
     643             :                                                  bool bOverwrite,
     644             :                                                  bool *pbErrorOccurred,
     645             :                                                  bool *pbOverwriteActuallyDone,
     646             :                                                  bool *pbAddOverwriteLCO);
     647             : 
     648             : /************************************************************************/
     649             : /*                            LoadGeometry()                            */
     650             : /************************************************************************/
     651             : 
     652          20 : static std::unique_ptr<OGRGeometry> LoadGeometry(const std::string &osDS,
     653             :                                                  const std::string &osSQL,
     654             :                                                  const std::string &osLyr,
     655             :                                                  const std::string &osWhere,
     656             :                                                  bool bMakeValid)
     657             : {
     658             :     auto poDS = std::unique_ptr<GDALDataset>(
     659          40 :         GDALDataset::Open(osDS.c_str(), GDAL_OF_VECTOR));
     660          20 :     if (poDS == nullptr)
     661           3 :         return nullptr;
     662             : 
     663          17 :     OGRLayer *poLyr = nullptr;
     664          17 :     if (!osSQL.empty())
     665           3 :         poLyr = poDS->ExecuteSQL(osSQL.c_str(), nullptr, nullptr);
     666          14 :     else if (!osLyr.empty())
     667           2 :         poLyr = poDS->GetLayerByName(osLyr.c_str());
     668             :     else
     669          12 :         poLyr = poDS->GetLayer(0);
     670             : 
     671          17 :     if (poLyr == nullptr)
     672             :     {
     673           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     674             :                  "Failed to identify source layer from datasource.");
     675           0 :         return nullptr;
     676             :     }
     677             : 
     678          17 :     if (!osWhere.empty())
     679           7 :         poLyr->SetAttributeFilter(osWhere.c_str());
     680             : 
     681          34 :     OGRGeometryCollection oGC;
     682             : 
     683          17 :     const auto poSRSSrc = poLyr->GetSpatialRef();
     684          17 :     if (poSRSSrc)
     685             :     {
     686           4 :         auto poSRSClone = OGRSpatialReferenceRefCountedPtr::makeClone(poSRSSrc);
     687           2 :         oGC.assignSpatialReference(poSRSClone.get());
     688             :     }
     689             : 
     690          34 :     for (auto &poFeat : poLyr)
     691             :     {
     692          17 :         auto poSrcGeom = std::unique_ptr<OGRGeometry>(poFeat->StealGeometry());
     693          17 :         if (poSrcGeom)
     694             :         {
     695             :             // Only take into account areal geometries.
     696          17 :             if (poSrcGeom->getDimension() == 2)
     697             :             {
     698          17 :                 std::string osReason;
     699          17 :                 if (!poSrcGeom->IsValid(&osReason))
     700             :                 {
     701           4 :                     if (!bMakeValid)
     702             :                     {
     703           2 :                         CPLError(
     704             :                             CE_Failure, CPLE_AppDefined,
     705             :                             "Geometry of feature " CPL_FRMT_GIB " of %s "
     706             :                             "is invalid (%s). You can try to make it valid by "
     707             :                             "specifying -makevalid, but the results of "
     708             :                             "the operation should be manually inspected.",
     709             :                             poFeat->GetFID(), osDS.c_str(), osReason.c_str());
     710           2 :                         oGC.empty();
     711           2 :                         break;
     712             :                     }
     713             :                     auto poValid =
     714           2 :                         std::unique_ptr<OGRGeometry>(poSrcGeom->MakeValid());
     715           2 :                     if (poValid)
     716             :                     {
     717           2 :                         CPLError(CE_Warning, CPLE_AppDefined,
     718             :                                  "Geometry of feature " CPL_FRMT_GIB " of %s "
     719             :                                  "was invalid (%s) and has been made valid, "
     720             :                                  "but the results of the operation "
     721             :                                  "should be manually inspected.",
     722             :                                  poFeat->GetFID(), osDS.c_str(),
     723             :                                  osReason.c_str());
     724             : 
     725           2 :                         oGC.addGeometry(std::move(poValid));
     726             :                     }
     727             :                     else
     728             :                     {
     729           0 :                         CPLError(
     730             :                             CE_Failure, CPLE_AppDefined,
     731             :                             "Geometry of feature " CPL_FRMT_GIB " of %s "
     732             :                             "is invalid (%s), and could not be made valid.",
     733             :                             poFeat->GetFID(), osDS.c_str(), osReason.c_str());
     734           0 :                         oGC.empty();
     735           0 :                         break;
     736             :                     }
     737             :                 }
     738             :                 else
     739             :                 {
     740          13 :                     oGC.addGeometry(std::move(poSrcGeom));
     741             :                 }
     742             :             }
     743             :         }
     744             :     }
     745             : 
     746          17 :     if (!osSQL.empty())
     747           3 :         poDS->ReleaseResultSet(poLyr);
     748             : 
     749          17 :     if (oGC.IsEmpty())
     750           2 :         return nullptr;
     751             : 
     752          15 :     return std::unique_ptr<OGRGeometry>(oGC.UnaryUnion());
     753             : }
     754             : 
     755             : /************************************************************************/
     756             : /*                        OGRSplitListFieldLayer                        */
     757             : /************************************************************************/
     758             : 
     759             : class OGRSplitListFieldLayer : public OGRLayer
     760             : {
     761             :     struct ListFieldDesc
     762             :     {
     763             :         int iSrcIndex = -1;
     764             :         OGRFieldType eType = OFTMaxType;
     765             :         int nMaxOccurrences = 0;
     766             :         int nWidth = 0;
     767             :     };
     768             : 
     769             :     OGRLayer *poSrcLayer = nullptr;
     770             :     OGRFeatureDefnRefCountedPtr poFeatureDefn{};
     771             :     std::vector<ListFieldDesc> asListFields{};
     772             :     const int nMaxSplitListSubFields;
     773             : 
     774             :     std::unique_ptr<OGRFeature>
     775             :     TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeature) const;
     776             : 
     777             :     CPL_DISALLOW_COPY_ASSIGN(OGRSplitListFieldLayer)
     778             : 
     779             :   public:
     780             :     OGRSplitListFieldLayer(OGRLayer *poSrcLayer, int nMaxSplitListSubFields);
     781             : 
     782             :     bool BuildLayerDefn(GDALProgressFunc pfnProgress, void *pProgressArg);
     783             : 
     784             :     OGRFeature *GetNextFeature() override;
     785             :     OGRFeature *GetFeature(GIntBig nFID) override;
     786             :     const OGRFeatureDefn *GetLayerDefn() const override;
     787             : 
     788           1 :     void ResetReading() override
     789             :     {
     790           1 :         poSrcLayer->ResetReading();
     791           1 :     }
     792             : 
     793           1 :     int TestCapability(const char *) const override
     794             :     {
     795           1 :         return FALSE;
     796             :     }
     797             : 
     798           0 :     GIntBig GetFeatureCount(int bForce = TRUE) override
     799             :     {
     800           0 :         return poSrcLayer->GetFeatureCount(bForce);
     801             :     }
     802             : 
     803           1 :     const OGRSpatialReference *GetSpatialRef() const override
     804             :     {
     805           1 :         return poSrcLayer->GetSpatialRef();
     806             :     }
     807             : 
     808           0 :     OGRGeometry *GetSpatialFilter() override
     809             :     {
     810           0 :         return poSrcLayer->GetSpatialFilter();
     811             :     }
     812             : 
     813           1 :     OGRStyleTable *GetStyleTable() override
     814             :     {
     815           1 :         return poSrcLayer->GetStyleTable();
     816             :     }
     817             : 
     818           0 :     virtual OGRErr ISetSpatialFilter(int iGeom,
     819             :                                      const OGRGeometry *poGeom) override
     820             :     {
     821           0 :         return poSrcLayer->SetSpatialFilter(iGeom, poGeom);
     822             :     }
     823             : 
     824           0 :     OGRErr SetAttributeFilter(const char *pszFilter) override
     825             :     {
     826           0 :         return poSrcLayer->SetAttributeFilter(pszFilter);
     827             :     }
     828             : };
     829             : 
     830             : /************************************************************************/
     831             : /*                       OGRSplitListFieldLayer()                       */
     832             : /************************************************************************/
     833             : 
     834           1 : OGRSplitListFieldLayer::OGRSplitListFieldLayer(OGRLayer *poSrcLayerIn,
     835           1 :                                                int nMaxSplitListSubFieldsIn)
     836             :     : poSrcLayer(poSrcLayerIn),
     837             :       nMaxSplitListSubFields(
     838           1 :           nMaxSplitListSubFieldsIn < 0 ? INT_MAX : nMaxSplitListSubFieldsIn)
     839             : {
     840           1 : }
     841             : 
     842             : /************************************************************************/
     843             : /*                           BuildLayerDefn()                           */
     844             : /************************************************************************/
     845             : 
     846           1 : bool OGRSplitListFieldLayer::BuildLayerDefn(GDALProgressFunc pfnProgress,
     847             :                                             void *pProgressArg)
     848             : {
     849           1 :     CPLAssert(poFeatureDefn == nullptr);
     850             : 
     851           1 :     const OGRFeatureDefn *poSrcFeatureDefn = poSrcLayer->GetLayerDefn();
     852             : 
     853           1 :     const int nSrcFields = poSrcFeatureDefn->GetFieldCount();
     854           1 :     asListFields.reserve(nSrcFields);
     855             : 
     856             :     /* Establish the list of fields of list type */
     857           6 :     for (int i = 0; i < nSrcFields; ++i)
     858             :     {
     859           5 :         OGRFieldType eType = poSrcFeatureDefn->GetFieldDefn(i)->GetType();
     860           5 :         if (eType == OFTIntegerList || eType == OFTInteger64List ||
     861           3 :             eType == OFTRealList || eType == OFTStringList)
     862             :         {
     863           3 :             asListFields.resize(asListFields.size() + 1);
     864           3 :             asListFields.back().iSrcIndex = i;
     865           3 :             asListFields.back().eType = eType;
     866           3 :             if (nMaxSplitListSubFields == 1)
     867           0 :                 asListFields.back().nMaxOccurrences = 1;
     868             :         }
     869             :     }
     870             : 
     871           1 :     if (asListFields.empty())
     872           0 :         return false;
     873             : 
     874             :     /* No need for full scan if the limit is 1. We just to have to create */
     875             :     /* one and a single one field */
     876           1 :     if (nMaxSplitListSubFields != 1)
     877             :     {
     878           1 :         poSrcLayer->ResetReading();
     879             : 
     880             :         const GIntBig nFeatureCount =
     881           1 :             poSrcLayer->TestCapability(OLCFastFeatureCount)
     882           1 :                 ? poSrcLayer->GetFeatureCount()
     883           1 :                 : 0;
     884           1 :         GIntBig nFeatureIndex = 0;
     885             : 
     886             :         /* Scan the whole layer to compute the maximum number of */
     887             :         /* items for each field of list type */
     888           2 :         for (const auto &poSrcFeature : poSrcLayer)
     889             :         {
     890           4 :             for (auto &sListField : asListFields)
     891             :             {
     892           3 :                 int nCount = 0;
     893             :                 const OGRField *psField =
     894           3 :                     poSrcFeature->GetRawFieldRef(sListField.iSrcIndex);
     895           3 :                 switch (sListField.eType)
     896             :                 {
     897           1 :                     case OFTIntegerList:
     898           1 :                         nCount = psField->IntegerList.nCount;
     899           1 :                         break;
     900           1 :                     case OFTRealList:
     901           1 :                         nCount = psField->RealList.nCount;
     902           1 :                         break;
     903           1 :                     case OFTStringList:
     904             :                     {
     905           1 :                         nCount = psField->StringList.nCount;
     906           1 :                         char **paList = psField->StringList.paList;
     907           3 :                         for (int j = 0; j < nCount; j++)
     908             :                         {
     909           2 :                             int nWidth = static_cast<int>(strlen(paList[j]));
     910           2 :                             if (nWidth > sListField.nWidth)
     911           1 :                                 sListField.nWidth = nWidth;
     912             :                         }
     913           1 :                         break;
     914             :                     }
     915           0 :                     default:
     916             :                         // cppcheck-suppress knownConditionTrueFalse
     917           0 :                         CPLAssert(false);
     918             :                         break;
     919             :                 }
     920           3 :                 if (nCount > sListField.nMaxOccurrences)
     921             :                 {
     922           3 :                     if (nCount > nMaxSplitListSubFields)
     923           0 :                         nCount = nMaxSplitListSubFields;
     924           3 :                     sListField.nMaxOccurrences = nCount;
     925             :                 }
     926             :             }
     927             : 
     928           1 :             nFeatureIndex++;
     929           1 :             if (pfnProgress != nullptr && nFeatureCount != 0)
     930           0 :                 pfnProgress(nFeatureIndex * 1.0 / nFeatureCount, "",
     931             :                             pProgressArg);
     932             :         }
     933             :     }
     934             : 
     935             :     /* Now let's build the target feature definition */
     936             : 
     937           1 :     poFeatureDefn.reset(
     938           1 :         OGRFeatureDefn::CreateFeatureDefn(poSrcFeatureDefn->GetName()));
     939           1 :     poFeatureDefn->SetGeomType(wkbNone);
     940             : 
     941           1 :     for (const auto poSrcGeomFieldDefn : poSrcFeatureDefn->GetGeomFields())
     942             :     {
     943           0 :         poFeatureDefn->AddGeomFieldDefn(poSrcGeomFieldDefn);
     944             :     }
     945             : 
     946           1 :     int iListField = 0;
     947           6 :     for (const auto poSrcFieldDefn : poSrcFeatureDefn->GetFields())
     948             :     {
     949           5 :         const OGRFieldType eType = poSrcFieldDefn->GetType();
     950           5 :         if (eType == OFTIntegerList || eType == OFTInteger64List ||
     951           3 :             eType == OFTRealList || eType == OFTStringList)
     952             :         {
     953             :             const int nMaxOccurrences =
     954           3 :                 asListFields[iListField].nMaxOccurrences;
     955           3 :             const int nWidth = asListFields[iListField].nWidth;
     956           3 :             iListField++;
     957           3 :             if (nMaxOccurrences == 1)
     958             :             {
     959             :                 OGRFieldDefn oFieldDefn(poSrcFieldDefn->GetNameRef(),
     960             :                                         (eType == OFTIntegerList) ? OFTInteger
     961             :                                         : (eType == OFTInteger64List)
     962           0 :                                             ? OFTInteger64
     963           0 :                                         : (eType == OFTRealList) ? OFTReal
     964           0 :                                                                  : OFTString);
     965           0 :                 poFeatureDefn->AddFieldDefn(&oFieldDefn);
     966             :             }
     967             :             else
     968             :             {
     969           9 :                 for (int j = 0; j < nMaxOccurrences; j++)
     970             :                 {
     971          12 :                     CPLString osFieldName;
     972             :                     osFieldName.Printf("%s%d", poSrcFieldDefn->GetNameRef(),
     973           6 :                                        j + 1);
     974             :                     OGRFieldDefn oFieldDefn(
     975             :                         osFieldName.c_str(),
     976             :                         (eType == OFTIntegerList)     ? OFTInteger
     977           8 :                         : (eType == OFTInteger64List) ? OFTInteger64
     978           4 :                         : (eType == OFTRealList)      ? OFTReal
     979          16 :                                                       : OFTString);
     980           6 :                     oFieldDefn.SetWidth(nWidth);
     981           6 :                     poFeatureDefn->AddFieldDefn(&oFieldDefn);
     982             :                 }
     983           3 :             }
     984             :         }
     985             :         else
     986             :         {
     987           2 :             poFeatureDefn->AddFieldDefn(poSrcFieldDefn);
     988             :         }
     989             :     }
     990             : 
     991           1 :     return true;
     992             : }
     993             : 
     994             : /************************************************************************/
     995             : /*                          TranslateFeature()                          */
     996             : /************************************************************************/
     997             : 
     998           2 : std::unique_ptr<OGRFeature> OGRSplitListFieldLayer::TranslateFeature(
     999             :     std::unique_ptr<OGRFeature> poSrcFeature) const
    1000             : {
    1001           2 :     if (poSrcFeature == nullptr)
    1002           1 :         return nullptr;
    1003           1 :     if (poFeatureDefn == nullptr)
    1004           0 :         return poSrcFeature;
    1005             : 
    1006           2 :     auto poFeature = std::make_unique<OGRFeature>(poFeatureDefn.get());
    1007           1 :     poFeature->SetFID(poSrcFeature->GetFID());
    1008           1 :     for (int i = 0; i < poFeature->GetGeomFieldCount(); i++)
    1009             :     {
    1010           0 :         poFeature->SetGeomFieldDirectly(i, poSrcFeature->StealGeometry(i));
    1011             :     }
    1012           1 :     poFeature->SetStyleString(poFeature->GetStyleString());
    1013             : 
    1014           1 :     const OGRFeatureDefn *poSrcFieldDefn = poSrcLayer->GetLayerDefn();
    1015           1 :     const int nSrcFields = poSrcFeature->GetFieldCount();
    1016           1 :     int iDstField = 0;
    1017           1 :     int iListField = 0;
    1018             : 
    1019           6 :     for (int iSrcField = 0; iSrcField < nSrcFields; ++iSrcField)
    1020             :     {
    1021             :         const OGRFieldType eType =
    1022           5 :             poSrcFieldDefn->GetFieldDefn(iSrcField)->GetType();
    1023           5 :         const OGRField *psField = poSrcFeature->GetRawFieldRef(iSrcField);
    1024           5 :         switch (eType)
    1025             :         {
    1026           1 :             case OFTIntegerList:
    1027             :             {
    1028           1 :                 const int nCount = std::min(nMaxSplitListSubFields,
    1029           1 :                                             psField->IntegerList.nCount);
    1030           1 :                 const int *paList = psField->IntegerList.paList;
    1031           3 :                 for (int j = 0; j < nCount; ++j)
    1032           2 :                     poFeature->SetField(iDstField + j, paList[j]);
    1033           1 :                 iDstField += asListFields[iListField].nMaxOccurrences;
    1034           1 :                 iListField++;
    1035           1 :                 break;
    1036             :             }
    1037           0 :             case OFTInteger64List:
    1038             :             {
    1039           0 :                 const int nCount = std::min(nMaxSplitListSubFields,
    1040           0 :                                             psField->Integer64List.nCount);
    1041           0 :                 const GIntBig *paList = psField->Integer64List.paList;
    1042           0 :                 for (int j = 0; j < nCount; ++j)
    1043           0 :                     poFeature->SetField(iDstField + j, paList[j]);
    1044           0 :                 iDstField += asListFields[iListField].nMaxOccurrences;
    1045           0 :                 iListField++;
    1046           0 :                 break;
    1047             :             }
    1048           1 :             case OFTRealList:
    1049             :             {
    1050             :                 const int nCount =
    1051           1 :                     std::min(nMaxSplitListSubFields, psField->RealList.nCount);
    1052           1 :                 const double *paList = psField->RealList.paList;
    1053           3 :                 for (int j = 0; j < nCount; ++j)
    1054           2 :                     poFeature->SetField(iDstField + j, paList[j]);
    1055           1 :                 iDstField += asListFields[iListField].nMaxOccurrences;
    1056           1 :                 iListField++;
    1057           1 :                 break;
    1058             :             }
    1059           1 :             case OFTStringList:
    1060             :             {
    1061           1 :                 const int nCount = std::min(nMaxSplitListSubFields,
    1062           1 :                                             psField->StringList.nCount);
    1063           1 :                 CSLConstList paList = psField->StringList.paList;
    1064           3 :                 for (int j = 0; j < nCount; ++j)
    1065           2 :                     poFeature->SetField(iDstField + j, paList[j]);
    1066           1 :                 iDstField += asListFields[iListField].nMaxOccurrences;
    1067           1 :                 iListField++;
    1068           1 :                 break;
    1069             :             }
    1070           2 :             default:
    1071             :             {
    1072           2 :                 poFeature->SetField(iDstField, psField);
    1073           2 :                 iDstField++;
    1074           2 :                 break;
    1075             :             }
    1076             :         }
    1077             :     }
    1078             : 
    1079           1 :     return poFeature;
    1080             : }
    1081             : 
    1082             : /************************************************************************/
    1083             : /*                           GetNextFeature()                           */
    1084             : /************************************************************************/
    1085             : 
    1086           2 : OGRFeature *OGRSplitListFieldLayer::GetNextFeature()
    1087             : {
    1088           4 :     return TranslateFeature(
    1089           4 :                std::unique_ptr<OGRFeature>(poSrcLayer->GetNextFeature()))
    1090           4 :         .release();
    1091             : }
    1092             : 
    1093             : /************************************************************************/
    1094             : /*                             GetFeature()                             */
    1095             : /************************************************************************/
    1096             : 
    1097           0 : OGRFeature *OGRSplitListFieldLayer::GetFeature(GIntBig nFID)
    1098             : {
    1099           0 :     return TranslateFeature(
    1100           0 :                std::unique_ptr<OGRFeature>(poSrcLayer->GetFeature(nFID)))
    1101           0 :         .release();
    1102             : }
    1103             : 
    1104             : /************************************************************************/
    1105             : /*                            GetLayerDefn()                            */
    1106             : /************************************************************************/
    1107             : 
    1108           3 : const OGRFeatureDefn *OGRSplitListFieldLayer::GetLayerDefn() const
    1109             : {
    1110           3 :     if (poFeatureDefn == nullptr)
    1111           0 :         return poSrcLayer->GetLayerDefn();
    1112           3 :     return poFeatureDefn.get();
    1113             : }
    1114             : 
    1115             : /************************************************************************/
    1116             : /*                            GCPCoordTransformation()                  */
    1117             : /*                                                                      */
    1118             : /*      Apply GCP Transform to points                                   */
    1119             : /************************************************************************/
    1120             : namespace
    1121             : {
    1122             : class GCPCoordTransformation final : public OGRCoordinateTransformation
    1123             : {
    1124           0 :     GCPCoordTransformation(const GCPCoordTransformation &other)
    1125           0 :         : bUseTPS(other.bUseTPS), poSRS(other.poSRS)
    1126             :     {
    1127           0 :         hTransformArg.reset(GDALCloneTransformer(other.hTransformArg.get()));
    1128           0 :     }
    1129             : 
    1130             :     GCPCoordTransformation &operator=(const GCPCoordTransformation &) = delete;
    1131             : 
    1132             :   public:
    1133             :     std::unique_ptr<void, decltype(&GDALDestroyTransformer)> hTransformArg{
    1134           0 :         nullptr, GDALDestroyTransformer};
    1135             :     const bool bUseTPS;
    1136             :     const OGRSpatialReferenceRefCountedPtr poSRS;
    1137             : 
    1138           7 :     GCPCoordTransformation(int nGCPCount, const GDAL_GCP *pasGCPList,
    1139             :                            int nReqOrder, const OGRSpatialReference *poSRSIn)
    1140          14 :         : bUseTPS(nReqOrder < 0),
    1141             :           poSRS(const_cast<OGRSpatialReference *>(poSRSIn),
    1142           7 :                 /* add_ref = */ true)
    1143             :     {
    1144           7 :         if (nReqOrder < 0)
    1145             :         {
    1146           1 :             hTransformArg.reset(
    1147             :                 GDALCreateTPSTransformer(nGCPCount, pasGCPList, FALSE));
    1148             :         }
    1149             :         else
    1150             :         {
    1151           6 :             hTransformArg.reset(GDALCreateGCPTransformer(nGCPCount, pasGCPList,
    1152             :                                                          nReqOrder, FALSE));
    1153             :         }
    1154           7 :     }
    1155             : 
    1156           0 :     OGRCoordinateTransformation *Clone() const override
    1157             :     {
    1158           0 :         return new GCPCoordTransformation(*this);
    1159             :     }
    1160             : 
    1161           7 :     bool IsValid() const
    1162             :     {
    1163           7 :         return hTransformArg != nullptr;
    1164             :     }
    1165             : 
    1166          11 :     const OGRSpatialReference *GetSourceCS() const override
    1167             :     {
    1168          11 :         return poSRS.get();
    1169             :     }
    1170             : 
    1171          18 :     const OGRSpatialReference *GetTargetCS() const override
    1172             :     {
    1173          18 :         return poSRS.get();
    1174             :     }
    1175             : 
    1176          11 :     virtual int Transform(size_t nCount, double *x, double *y, double *z,
    1177             :                           double * /* t */, int *pabSuccess) override
    1178             :     {
    1179          11 :         CPLAssert(nCount <=
    1180             :                   static_cast<size_t>(std::numeric_limits<int>::max()));
    1181          11 :         if (bUseTPS)
    1182           2 :             return GDALTPSTransform(hTransformArg.get(), FALSE,
    1183             :                                     static_cast<int>(nCount), x, y, z,
    1184           2 :                                     pabSuccess);
    1185             :         else
    1186           9 :             return GDALGCPTransform(hTransformArg.get(), FALSE,
    1187             :                                     static_cast<int>(nCount), x, y, z,
    1188           9 :                                     pabSuccess);
    1189             :     }
    1190             : 
    1191           0 :     OGRCoordinateTransformation *GetInverse() const override
    1192             :     {
    1193             :         static std::once_flag flag;
    1194           0 :         std::call_once(flag,
    1195           0 :                        []()
    1196             :                        {
    1197           0 :                            CPLDebug("OGR2OGR",
    1198             :                                     "GCPCoordTransformation::GetInverse() "
    1199             :                                     "called, but not implemented");
    1200           0 :                        });
    1201           0 :         return nullptr;
    1202             :     }
    1203             : };
    1204             : }  // namespace
    1205             : 
    1206             : /************************************************************************/
    1207             : /*                             CompositeCT                              */
    1208             : /************************************************************************/
    1209             : 
    1210          12 : class CompositeCT final : public OGRCoordinateTransformation
    1211             : {
    1212             :     std::unique_ptr<OGRCoordinateTransformation> poOwnedCT1{};
    1213             :     OGRCoordinateTransformation *const poCT1;
    1214             :     std::unique_ptr<OGRCoordinateTransformation> poOwnedCT2{};
    1215             :     OGRCoordinateTransformation *const poCT2;
    1216             : 
    1217             :     // Working buffer
    1218             :     std::vector<int> m_anErrorCode{};
    1219             : 
    1220             :     CompositeCT &operator=(const CompositeCT &) = delete;
    1221             : 
    1222             :   public:
    1223           5 :     CompositeCT(OGRCoordinateTransformation *poCT1In,
    1224             :                 OGRCoordinateTransformation *poCT2In)
    1225           5 :         : poCT1(poCT1In), poCT2(poCT2In)
    1226             :     {
    1227           5 :     }
    1228             : 
    1229           0 :     CompositeCT(std::unique_ptr<OGRCoordinateTransformation> poCT1In,
    1230             :                 OGRCoordinateTransformation *poCT2In)
    1231           0 :         : poOwnedCT1(std::move(poCT1In)), poCT1(poOwnedCT1.get()),
    1232           0 :           poCT2(poCT2In)
    1233             :     {
    1234           0 :     }
    1235             : 
    1236           0 :     CompositeCT(std::unique_ptr<OGRCoordinateTransformation> poCT1In,
    1237             :                 std::unique_ptr<OGRCoordinateTransformation> poCT2In)
    1238           0 :         : poOwnedCT1(std::move(poCT1In)), poCT1(poOwnedCT1.get()),
    1239           0 :           poOwnedCT2(std::move(poCT2In)), poCT2(poOwnedCT2.get())
    1240             :     {
    1241           0 :     }
    1242             : 
    1243           1 :     CompositeCT(OGRCoordinateTransformation *poCT1In,
    1244             :                 std::unique_ptr<OGRCoordinateTransformation> poCT2In)
    1245           2 :         : poCT1(poCT1In), poOwnedCT2(std::move(poCT2In)),
    1246           1 :           poCT2(poOwnedCT2.get())
    1247             :     {
    1248           1 :     }
    1249             : 
    1250           0 :     CompositeCT(const CompositeCT &other)
    1251           0 :         : poOwnedCT1(other.poCT1 ? other.poCT1->Clone() : nullptr),
    1252           0 :           poCT1(poOwnedCT1.get()),
    1253           0 :           poOwnedCT2(other.poCT2 ? other.poCT2->Clone() : nullptr),
    1254           0 :           poCT2(poOwnedCT2.get()), m_anErrorCode({})
    1255             :     {
    1256           0 :     }
    1257             : 
    1258             :     ~CompositeCT() override;
    1259             : 
    1260           0 :     OGRCoordinateTransformation *Clone() const override
    1261             :     {
    1262           0 :         return std::make_unique<CompositeCT>(*this).release();
    1263             :     }
    1264             : 
    1265          11 :     const OGRSpatialReference *GetSourceCS() const override
    1266             :     {
    1267          11 :         return poCT1   ? poCT1->GetSourceCS()
    1268           0 :                : poCT2 ? poCT2->GetSourceCS()
    1269          11 :                        : nullptr;
    1270             :     }
    1271             : 
    1272          22 :     const OGRSpatialReference *GetTargetCS() const override
    1273             :     {
    1274          40 :         return poCT2   ? poCT2->GetTargetCS()
    1275          18 :                : poCT1 ? poCT1->GetTargetCS()
    1276          22 :                        : nullptr;
    1277             :     }
    1278             : 
    1279           0 :     bool GetEmitErrors() const override
    1280             :     {
    1281           0 :         if (poCT1)
    1282           0 :             return poCT1->GetEmitErrors();
    1283           0 :         if (poCT2)
    1284           0 :             return poCT2->GetEmitErrors();
    1285           0 :         return true;
    1286             :     }
    1287             : 
    1288           0 :     void SetEmitErrors(bool bEmitErrors) override
    1289             :     {
    1290           0 :         if (poCT1)
    1291           0 :             poCT1->SetEmitErrors(bEmitErrors);
    1292           0 :         if (poCT2)
    1293           0 :             poCT2->SetEmitErrors(bEmitErrors);
    1294           0 :     }
    1295             : 
    1296          11 :     virtual int Transform(size_t nCount, double *x, double *y, double *z,
    1297             :                           double *t, int *pabSuccess) override
    1298             :     {
    1299          11 :         int nResult = TRUE;
    1300          11 :         if (poCT1)
    1301          11 :             nResult = poCT1->Transform(nCount, x, y, z, t, pabSuccess);
    1302          11 :         if (nResult && poCT2)
    1303           2 :             nResult = poCT2->Transform(nCount, x, y, z, t, pabSuccess);
    1304          11 :         return nResult;
    1305             :     }
    1306             : 
    1307           0 :     virtual int TransformWithErrorCodes(size_t nCount, double *x, double *y,
    1308             :                                         double *z, double *t,
    1309             :                                         int *panErrorCodes) override
    1310             :     {
    1311           0 :         if (poCT1 && poCT2 && panErrorCodes)
    1312             :         {
    1313           0 :             m_anErrorCode.resize(nCount);
    1314           0 :             int nResult = poCT1->TransformWithErrorCodes(nCount, x, y, z, t,
    1315           0 :                                                          m_anErrorCode.data());
    1316           0 :             if (nResult)
    1317           0 :                 nResult = poCT2->TransformWithErrorCodes(nCount, x, y, z, t,
    1318           0 :                                                          panErrorCodes);
    1319           0 :             for (size_t i = 0; i < nCount; ++i)
    1320             :             {
    1321           0 :                 if (m_anErrorCode[i])
    1322           0 :                     panErrorCodes[i] = m_anErrorCode[i];
    1323             :             }
    1324           0 :             return nResult;
    1325             :         }
    1326           0 :         int nResult = TRUE;
    1327           0 :         if (poCT1)
    1328           0 :             nResult = poCT1->TransformWithErrorCodes(nCount, x, y, z, t,
    1329           0 :                                                      panErrorCodes);
    1330           0 :         if (nResult && poCT2)
    1331           0 :             nResult = poCT2->TransformWithErrorCodes(nCount, x, y, z, t,
    1332           0 :                                                      panErrorCodes);
    1333           0 :         return nResult;
    1334             :     }
    1335             : 
    1336           0 :     OGRCoordinateTransformation *GetInverse() const override
    1337             :     {
    1338           0 :         if (!poCT1 && !poCT2)
    1339           0 :             return nullptr;
    1340           0 :         if (!poCT2)
    1341           0 :             return poCT1->GetInverse();
    1342           0 :         if (!poCT1)
    1343           0 :             return poCT2->GetInverse();
    1344             :         auto poInvCT1 =
    1345           0 :             std::unique_ptr<OGRCoordinateTransformation>(poCT1->GetInverse());
    1346             :         auto poInvCT2 =
    1347           0 :             std::unique_ptr<OGRCoordinateTransformation>(poCT2->GetInverse());
    1348           0 :         if (!poInvCT1 || !poInvCT2)
    1349           0 :             return nullptr;
    1350           0 :         return std::make_unique<CompositeCT>(std::move(poInvCT2),
    1351           0 :                                              std::move(poInvCT1))
    1352           0 :             .release();
    1353             :     }
    1354             : };
    1355             : 
    1356             : CompositeCT::~CompositeCT() = default;
    1357             : 
    1358             : /************************************************************************/
    1359             : /*                 AxisMappingCoordinateTransformation                  */
    1360             : /************************************************************************/
    1361             : 
    1362           0 : class AxisMappingCoordinateTransformation : public OGRCoordinateTransformation
    1363             : {
    1364             :     bool bSwapXY = false;
    1365             : 
    1366           0 :     AxisMappingCoordinateTransformation(
    1367             :         const AxisMappingCoordinateTransformation &) = default;
    1368             :     AxisMappingCoordinateTransformation &
    1369             :     operator=(const AxisMappingCoordinateTransformation &) = delete;
    1370             :     AxisMappingCoordinateTransformation(
    1371             :         AxisMappingCoordinateTransformation &&) = delete;
    1372             :     AxisMappingCoordinateTransformation &
    1373             :     operator=(AxisMappingCoordinateTransformation &&) = delete;
    1374             : 
    1375             :   public:
    1376           0 :     explicit AxisMappingCoordinateTransformation(bool bSwapXYIn)
    1377           0 :         : bSwapXY(bSwapXYIn)
    1378             :     {
    1379           0 :     }
    1380             : 
    1381           0 :     AxisMappingCoordinateTransformation(const std::vector<int> &mappingIn,
    1382             :                                         const std::vector<int> &mappingOut)
    1383           0 :     {
    1384           0 :         if (mappingIn.size() >= 2 && mappingIn[0] == 1 && mappingIn[1] == 2 &&
    1385           0 :             mappingOut.size() >= 2 && mappingOut[0] == 2 && mappingOut[1] == 1)
    1386             :         {
    1387           0 :             bSwapXY = true;
    1388             :         }
    1389           0 :         else if (mappingIn.size() >= 2 && mappingIn[0] == 2 &&
    1390           0 :                  mappingIn[1] == 1 && mappingOut.size() >= 2 &&
    1391           0 :                  mappingOut[0] == 1 && mappingOut[1] == 2)
    1392             :         {
    1393           0 :             bSwapXY = true;
    1394             :         }
    1395             :         else
    1396             :         {
    1397           0 :             CPLError(CE_Failure, CPLE_NotSupported,
    1398             :                      "Unsupported axis transformation");
    1399             :         }
    1400           0 :     }
    1401             : 
    1402             :     ~AxisMappingCoordinateTransformation() override;
    1403             : 
    1404           0 :     OGRCoordinateTransformation *Clone() const override
    1405             :     {
    1406           0 :         return new AxisMappingCoordinateTransformation(*this);
    1407             :     }
    1408             : 
    1409           0 :     const OGRSpatialReference *GetSourceCS() const override
    1410             :     {
    1411           0 :         return nullptr;
    1412             :     }
    1413             : 
    1414           0 :     const OGRSpatialReference *GetTargetCS() const override
    1415             :     {
    1416           0 :         return nullptr;
    1417             :     }
    1418             : 
    1419           0 :     virtual int Transform(size_t nCount, double *x, double *y, double * /*z*/,
    1420             :                           double * /*t*/, int *pabSuccess) override
    1421             :     {
    1422           0 :         for (size_t i = 0; i < nCount; i++)
    1423             :         {
    1424           0 :             if (pabSuccess)
    1425           0 :                 pabSuccess[i] = true;
    1426           0 :             if (bSwapXY)
    1427           0 :                 std::swap(x[i], y[i]);
    1428             :         }
    1429           0 :         return true;
    1430             :     }
    1431             : 
    1432           0 :     virtual int TransformWithErrorCodes(size_t nCount, double *x, double *y,
    1433             :                                         double * /*z*/, double * /*t*/,
    1434             :                                         int *panErrorCodes) override
    1435             :     {
    1436           0 :         for (size_t i = 0; i < nCount; i++)
    1437             :         {
    1438           0 :             if (panErrorCodes)
    1439           0 :                 panErrorCodes[i] = 0;
    1440           0 :             if (bSwapXY)
    1441           0 :                 std::swap(x[i], y[i]);
    1442             :         }
    1443           0 :         return true;
    1444             :     }
    1445             : 
    1446           0 :     OGRCoordinateTransformation *GetInverse() const override
    1447             :     {
    1448           0 :         return std::make_unique<AxisMappingCoordinateTransformation>(bSwapXY)
    1449           0 :             .release();
    1450             :     }
    1451             : };
    1452             : 
    1453             : AxisMappingCoordinateTransformation::~AxisMappingCoordinateTransformation() =
    1454             :     default;
    1455             : 
    1456             : /************************************************************************/
    1457             : /*                         ApplySpatialFilter()                         */
    1458             : /************************************************************************/
    1459             : 
    1460        1179 : static void ApplySpatialFilter(OGRLayer *poLayer, OGRGeometry *poSpatialFilter,
    1461             :                                const OGRSpatialReference *poSpatSRS,
    1462             :                                const char *pszGeomField,
    1463             :                                const OGRSpatialReference *poSourceSRS)
    1464             : {
    1465        1179 :     if (poSpatialFilter == nullptr)
    1466        1171 :         return;
    1467             : 
    1468           8 :     std::unique_ptr<OGRGeometry> poSpatialFilterReprojected;
    1469           8 :     if (poSpatSRS)
    1470             :     {
    1471           4 :         poSpatialFilterReprojected.reset(poSpatialFilter->clone());
    1472           4 :         poSpatialFilterReprojected->assignSpatialReference(poSpatSRS);
    1473             :         const OGRSpatialReference *poSpatialFilterTargetSRS =
    1474           4 :             poSourceSRS ? poSourceSRS : poLayer->GetSpatialRef();
    1475           4 :         if (poSpatialFilterTargetSRS)
    1476             :         {
    1477             :             // When transforming the spatial filter from its spat_srs to the
    1478             :             // layer SRS, make sure to densify it sufficiently to avoid issues
    1479           4 :             constexpr double SEGMENT_DISTANCE_METRE = 10 * 1000;
    1480           4 :             if (poSpatSRS->IsGeographic())
    1481             :             {
    1482             :                 const double LENGTH_OF_ONE_DEGREE =
    1483           1 :                     poSpatSRS->GetSemiMajor(nullptr) * M_PI / 180.0;
    1484           1 :                 poSpatialFilterReprojected->segmentize(SEGMENT_DISTANCE_METRE /
    1485           1 :                                                        LENGTH_OF_ONE_DEGREE);
    1486             :             }
    1487           3 :             else if (poSpatSRS->IsProjected())
    1488             :             {
    1489           3 :                 poSpatialFilterReprojected->segmentize(
    1490             :                     SEGMENT_DISTANCE_METRE /
    1491           3 :                     poSpatSRS->GetLinearUnits(nullptr));
    1492             :             }
    1493           4 :             poSpatialFilterReprojected->transformTo(poSpatialFilterTargetSRS);
    1494             :         }
    1495             :         else
    1496           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    1497             :                      "cannot determine layer SRS for %s.",
    1498           0 :                      poLayer->GetDescription());
    1499             :     }
    1500             : 
    1501           8 :     if (pszGeomField != nullptr)
    1502             :     {
    1503             :         const int iGeomField =
    1504           1 :             poLayer->GetLayerDefn()->GetGeomFieldIndex(pszGeomField);
    1505           1 :         if (iGeomField >= 0)
    1506           1 :             poLayer->SetSpatialFilter(iGeomField,
    1507             :                                       poSpatialFilterReprojected
    1508           0 :                                           ? poSpatialFilterReprojected.get()
    1509           1 :                                           : poSpatialFilter);
    1510             :         else
    1511           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    1512             :                      "Cannot find geometry field %s.", pszGeomField);
    1513             :     }
    1514             :     else
    1515             :     {
    1516          11 :         poLayer->SetSpatialFilter(poSpatialFilterReprojected
    1517           4 :                                       ? poSpatialFilterReprojected.get()
    1518           7 :                                       : poSpatialFilter);
    1519             :     }
    1520             : }
    1521             : 
    1522             : /************************************************************************/
    1523             : /*                            GetFieldType()                            */
    1524             : /************************************************************************/
    1525             : 
    1526          12 : static int GetFieldType(const char *pszArg, int *pnSubFieldType)
    1527             : {
    1528          12 :     *pnSubFieldType = OFSTNone;
    1529          12 :     const char *pszOpenParenthesis = strchr(pszArg, '(');
    1530          12 :     const int nLengthBeforeParenthesis =
    1531          12 :         pszOpenParenthesis ? static_cast<int>(pszOpenParenthesis - pszArg)
    1532          11 :                            : static_cast<int>(strlen(pszArg));
    1533          72 :     for (int iType = 0; iType <= static_cast<int>(OFTMaxType); iType++)
    1534             :     {
    1535             :         const char *pszFieldTypeName =
    1536          72 :             OGRFieldDefn::GetFieldTypeName(static_cast<OGRFieldType>(iType));
    1537          72 :         if (EQUALN(pszArg, pszFieldTypeName, nLengthBeforeParenthesis) &&
    1538          12 :             pszFieldTypeName[nLengthBeforeParenthesis] == '\0')
    1539             :         {
    1540          12 :             if (pszOpenParenthesis != nullptr)
    1541             :             {
    1542           1 :                 *pnSubFieldType = -1;
    1543           2 :                 CPLString osArgSubType = pszOpenParenthesis + 1;
    1544           1 :                 if (!osArgSubType.empty() && osArgSubType.back() == ')')
    1545           1 :                     osArgSubType.pop_back();
    1546           2 :                 for (int iSubType = 0;
    1547           2 :                      iSubType <= static_cast<int>(OFSTMaxSubType); iSubType++)
    1548             :                 {
    1549             :                     const char *pszFieldSubTypeName =
    1550           2 :                         OGRFieldDefn::GetFieldSubTypeName(
    1551             :                             static_cast<OGRFieldSubType>(iSubType));
    1552           2 :                     if (EQUAL(pszFieldSubTypeName, osArgSubType))
    1553             :                     {
    1554           1 :                         *pnSubFieldType = iSubType;
    1555           1 :                         break;
    1556             :                     }
    1557             :                 }
    1558             :             }
    1559          12 :             return iType;
    1560             :         }
    1561             :     }
    1562           0 :     return -1;
    1563             : }
    1564             : 
    1565             : /************************************************************************/
    1566             : /*                            IsFieldType()                             */
    1567             : /************************************************************************/
    1568             : 
    1569           8 : static bool IsFieldType(const char *pszArg)
    1570             : {
    1571             :     int iSubType;
    1572           8 :     return GetFieldType(pszArg, &iSubType) >= 0 && iSubType >= 0;
    1573             : }
    1574             : 
    1575             : class GDALVectorTranslateWrappedDataset final : public GDALDataset
    1576             : {
    1577             :     std::unique_ptr<GDALDriver> m_poDriverToFree{};
    1578             :     GDALDataset *m_poBase = nullptr;
    1579             :     OGRSpatialReference *m_poOutputSRS = nullptr;
    1580             :     const bool m_bTransform = false;
    1581             : 
    1582             :     std::vector<std::unique_ptr<OGRLayer>> m_apoLayers{};
    1583             :     std::vector<std::unique_ptr<OGRLayer>> m_apoHiddenLayers{};
    1584             : 
    1585             :     GDALVectorTranslateWrappedDataset(GDALDataset *poBase,
    1586             :                                       OGRSpatialReference *poOutputSRS,
    1587             :                                       bool bTransform);
    1588             : 
    1589             :     CPL_DISALLOW_COPY_ASSIGN(GDALVectorTranslateWrappedDataset)
    1590             : 
    1591             :   public:
    1592           3 :     int GetLayerCount() const override
    1593             :     {
    1594           3 :         return static_cast<int>(m_apoLayers.size());
    1595             :     }
    1596             : 
    1597             :     OGRLayer *GetLayer(int nIdx) const override;
    1598             :     OGRLayer *GetLayerByName(const char *pszName) override;
    1599             : 
    1600             :     OGRLayer *ExecuteSQL(const char *pszStatement, OGRGeometry *poSpatialFilter,
    1601             :                          const char *pszDialect) override;
    1602             :     void ReleaseResultSet(OGRLayer *poResultsSet) override;
    1603             : 
    1604             :     static std::unique_ptr<GDALVectorTranslateWrappedDataset>
    1605             :     New(GDALDataset *poBase, OGRSpatialReference *poOutputSRS, bool bTransform);
    1606             : };
    1607             : 
    1608             : class GDALVectorTranslateWrappedLayer final : public OGRLayerDecorator
    1609             : {
    1610             :     std::vector<std::unique_ptr<OGRCoordinateTransformation>> m_apoCT{};
    1611             :     OGRFeatureDefnRefCountedPtr m_poFDefn{};
    1612             : 
    1613             :     GDALVectorTranslateWrappedLayer(OGRLayer *poBaseLayer, bool bOwnBaseLayer);
    1614             :     std::unique_ptr<OGRFeature>
    1615             :     TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeat);
    1616             : 
    1617             :     CPL_DISALLOW_COPY_ASSIGN(GDALVectorTranslateWrappedLayer)
    1618             : 
    1619             :   public:
    1620         379 :     const OGRFeatureDefn *GetLayerDefn() const override
    1621             :     {
    1622         379 :         return m_poFDefn.get();
    1623             :     }
    1624             : 
    1625             :     OGRFeature *GetNextFeature() override;
    1626             :     OGRFeature *GetFeature(GIntBig nFID) override;
    1627             : 
    1628             :     static std::unique_ptr<GDALVectorTranslateWrappedLayer>
    1629             :     New(OGRLayer *poBaseLayer, bool bOwnBaseLayer,
    1630             :         OGRSpatialReference *poOutputSRS, bool bTransform);
    1631             : };
    1632             : 
    1633          79 : GDALVectorTranslateWrappedLayer::GDALVectorTranslateWrappedLayer(
    1634          79 :     OGRLayer *poBaseLayer, bool bOwnBaseLayer)
    1635             :     : OGRLayerDecorator(poBaseLayer, bOwnBaseLayer),
    1636          79 :       m_apoCT(poBaseLayer->GetLayerDefn()->GetGeomFieldCount())
    1637             : {
    1638          79 : }
    1639             : 
    1640             : std::unique_ptr<GDALVectorTranslateWrappedLayer>
    1641          79 : GDALVectorTranslateWrappedLayer::New(OGRLayer *poBaseLayer, bool bOwnBaseLayer,
    1642             :                                      OGRSpatialReference *poOutputSRS,
    1643             :                                      bool bTransform)
    1644             : {
    1645             :     auto poNew = std::unique_ptr<GDALVectorTranslateWrappedLayer>(
    1646         158 :         new GDALVectorTranslateWrappedLayer(poBaseLayer, bOwnBaseLayer));
    1647          79 :     poNew->m_poFDefn.reset(poBaseLayer->GetLayerDefn()->Clone());
    1648          79 :     if (!poOutputSRS)
    1649           0 :         return poNew;
    1650             : 
    1651          94 :     for (int i = 0; i < poNew->m_poFDefn->GetGeomFieldCount(); i++)
    1652             :     {
    1653          15 :         if (bTransform)
    1654             :         {
    1655           0 :             const OGRSpatialReference *poSourceSRS = poBaseLayer->GetLayerDefn()
    1656           0 :                                                          ->GetGeomFieldDefn(i)
    1657           0 :                                                          ->GetSpatialRef();
    1658           0 :             if (poSourceSRS == nullptr)
    1659             :             {
    1660           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    1661             :                          "Layer %s has no source SRS for geometry field %s",
    1662           0 :                          poBaseLayer->GetName(),
    1663           0 :                          poBaseLayer->GetLayerDefn()
    1664           0 :                              ->GetGeomFieldDefn(i)
    1665             :                              ->GetNameRef());
    1666           0 :                 return nullptr;
    1667             :             }
    1668             :             else
    1669             :             {
    1670           0 :                 poNew->m_apoCT[i] =
    1671           0 :                     std::unique_ptr<OGRCoordinateTransformation>(
    1672             :                         OGRCreateCoordinateTransformation(poSourceSRS,
    1673           0 :                                                           poOutputSRS));
    1674           0 :                 if (poNew->m_apoCT[i] == nullptr)
    1675             :                 {
    1676           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    1677             :                              "Failed to create coordinate transformation "
    1678             :                              "between the\n"
    1679             :                              "following coordinate systems.  This may be "
    1680             :                              "because they\n"
    1681             :                              "are not transformable.");
    1682             : 
    1683           0 :                     char *pszWKT = nullptr;
    1684           0 :                     poSourceSRS->exportToPrettyWkt(&pszWKT, FALSE);
    1685           0 :                     CPLError(CE_Failure, CPLE_AppDefined, "Source:\n%s",
    1686             :                              pszWKT);
    1687           0 :                     CPLFree(pszWKT);
    1688             : 
    1689           0 :                     poOutputSRS->exportToPrettyWkt(&pszWKT, FALSE);
    1690           0 :                     CPLError(CE_Failure, CPLE_AppDefined, "Target:\n%s",
    1691             :                              pszWKT);
    1692           0 :                     CPLFree(pszWKT);
    1693             : 
    1694           0 :                     return nullptr;
    1695             :                 }
    1696             :             }
    1697             :         }
    1698          15 :         poNew->m_poFDefn->GetGeomFieldDefn(i)->SetSpatialRef(poOutputSRS);
    1699             :     }
    1700             : 
    1701          79 :     return poNew;
    1702             : }
    1703             : 
    1704         641 : OGRFeature *GDALVectorTranslateWrappedLayer::GetNextFeature()
    1705             : {
    1706        1282 :     return TranslateFeature(
    1707        1282 :                std::unique_ptr<OGRFeature>(OGRLayerDecorator::GetNextFeature()))
    1708        1282 :         .release();
    1709             : }
    1710             : 
    1711           0 : OGRFeature *GDALVectorTranslateWrappedLayer::GetFeature(GIntBig nFID)
    1712             : {
    1713           0 :     return TranslateFeature(
    1714           0 :                std::unique_ptr<OGRFeature>(OGRLayerDecorator::GetFeature(nFID)))
    1715           0 :         .release();
    1716             : }
    1717             : 
    1718         641 : std::unique_ptr<OGRFeature> GDALVectorTranslateWrappedLayer::TranslateFeature(
    1719             :     std::unique_ptr<OGRFeature> poSrcFeat)
    1720             : {
    1721         641 :     if (poSrcFeat == nullptr)
    1722          93 :         return nullptr;
    1723        1096 :     auto poNewFeat = std::make_unique<OGRFeature>(m_poFDefn.get());
    1724         548 :     poNewFeat->SetFrom(poSrcFeat.get());
    1725         548 :     poNewFeat->SetFID(poSrcFeat->GetFID());
    1726         563 :     for (int i = 0; i < poNewFeat->GetGeomFieldCount(); i++)
    1727             :     {
    1728          15 :         OGRGeometry *poGeom = poNewFeat->GetGeomFieldRef(i);
    1729          15 :         if (poGeom)
    1730             :         {
    1731          13 :             if (m_apoCT[i])
    1732           0 :                 poGeom->transform(m_apoCT[i].get());
    1733          13 :             poGeom->assignSpatialReference(
    1734          13 :                 m_poFDefn->GetGeomFieldDefn(i)->GetSpatialRef());
    1735             :         }
    1736             :     }
    1737         548 :     return poNewFeat;
    1738             : }
    1739             : 
    1740           3 : GDALVectorTranslateWrappedDataset::GDALVectorTranslateWrappedDataset(
    1741           3 :     GDALDataset *poBase, OGRSpatialReference *poOutputSRS, bool bTransform)
    1742           3 :     : m_poBase(poBase), m_poOutputSRS(poOutputSRS), m_bTransform(bTransform)
    1743             : {
    1744           3 :     SetDescription(poBase->GetDescription());
    1745           3 :     if (poBase->GetDriver())
    1746             :     {
    1747           6 :         auto poNewDriver = std::make_unique<GDALDriver>();
    1748           3 :         poNewDriver->SetDescription(poBase->GetDriver()->GetDescription());
    1749           3 :         m_poDriverToFree = std::move(poNewDriver);
    1750             :     }
    1751           3 : }
    1752             : 
    1753             : std::unique_ptr<GDALVectorTranslateWrappedDataset>
    1754           3 : GDALVectorTranslateWrappedDataset::New(GDALDataset *poBase,
    1755             :                                        OGRSpatialReference *poOutputSRS,
    1756             :                                        bool bTransform)
    1757             : {
    1758             :     auto poNew = std::unique_ptr<GDALVectorTranslateWrappedDataset>(
    1759           6 :         new GDALVectorTranslateWrappedDataset(poBase, poOutputSRS, bTransform));
    1760          70 :     for (int i = 0; i < poBase->GetLayerCount(); i++)
    1761             :     {
    1762             :         auto poLayer = GDALVectorTranslateWrappedLayer::New(
    1763             :             poBase->GetLayer(i), /* bOwnBaseLayer = */ false, poOutputSRS,
    1764          67 :             bTransform);
    1765          67 :         if (poLayer == nullptr)
    1766             :         {
    1767           0 :             return nullptr;
    1768             :         }
    1769          67 :         poNew->m_apoLayers.push_back(std::move(poLayer));
    1770             :     }
    1771           3 :     return poNew;
    1772             : }
    1773             : 
    1774           0 : OGRLayer *GDALVectorTranslateWrappedDataset::GetLayer(int i) const
    1775             : {
    1776           0 :     if (i < 0 || i >= static_cast<int>(m_apoLayers.size()))
    1777           0 :         return nullptr;
    1778           0 :     return m_apoLayers[i].get();
    1779             : }
    1780             : 
    1781          68 : OGRLayer *GDALVectorTranslateWrappedDataset::GetLayerByName(const char *pszName)
    1782             : {
    1783        1008 :     for (const auto &poLayer : m_apoLayers)
    1784             :     {
    1785        1008 :         if (strcmp(poLayer->GetName(), pszName) == 0)
    1786          68 :             return poLayer.get();
    1787             :     }
    1788           0 :     for (const auto &poLayer : m_apoHiddenLayers)
    1789             :     {
    1790           0 :         if (strcmp(poLayer->GetName(), pszName) == 0)
    1791           0 :             return poLayer.get();
    1792             :     }
    1793           0 :     for (const auto &poLayer : m_apoLayers)
    1794             :     {
    1795           0 :         if (EQUAL(poLayer->GetName(), pszName))
    1796           0 :             return poLayer.get();
    1797             :     }
    1798           0 :     for (const auto &poLayer : m_apoHiddenLayers)
    1799             :     {
    1800           0 :         if (EQUAL(poLayer->GetName(), pszName))
    1801           0 :             return poLayer.get();
    1802             :     }
    1803             : 
    1804           0 :     OGRLayer *poLayer = m_poBase->GetLayerByName(pszName);
    1805           0 :     if (poLayer == nullptr)
    1806           0 :         return nullptr;
    1807             : 
    1808             :     auto poNewLayer = GDALVectorTranslateWrappedLayer::New(
    1809           0 :         poLayer, /* bOwnBaseLayer = */ false, m_poOutputSRS, m_bTransform);
    1810           0 :     if (poNewLayer == nullptr)
    1811           0 :         return nullptr;
    1812             : 
    1813             :     // Replicate source dataset behavior: if the fact of calling
    1814             :     // GetLayerByName() on a initially hidden layer makes it visible through
    1815             :     // GetLayerCount()/GetLayer(), do the same. Otherwise we are going to
    1816             :     // maintain it hidden as well.
    1817           0 :     for (int i = 0; i < m_poBase->GetLayerCount(); i++)
    1818             :     {
    1819           0 :         if (m_poBase->GetLayer(i) == poLayer)
    1820             :         {
    1821           0 :             m_apoLayers.push_back(std::move(poNewLayer));
    1822           0 :             return m_apoLayers.back().get();
    1823             :         }
    1824             :     }
    1825           0 :     m_apoHiddenLayers.push_back(std::move(poNewLayer));
    1826           0 :     return m_apoHiddenLayers.back().get();
    1827             : }
    1828             : 
    1829             : OGRLayer *
    1830          12 : GDALVectorTranslateWrappedDataset::ExecuteSQL(const char *pszStatement,
    1831             :                                               OGRGeometry *poSpatialFilter,
    1832             :                                               const char *pszDialect)
    1833             : {
    1834             :     OGRLayer *poLayer =
    1835          12 :         m_poBase->ExecuteSQL(pszStatement, poSpatialFilter, pszDialect);
    1836          12 :     if (poLayer == nullptr)
    1837           0 :         return nullptr;
    1838          12 :     return GDALVectorTranslateWrappedLayer::New(
    1839          12 :                poLayer, /* bOwnBaseLayer = */ true, m_poOutputSRS, m_bTransform)
    1840          12 :         .release();
    1841             : }
    1842             : 
    1843          12 : void GDALVectorTranslateWrappedDataset::ReleaseResultSet(OGRLayer *poResultsSet)
    1844             : {
    1845          12 :     delete poResultsSet;
    1846          12 : }
    1847             : 
    1848             : /************************************************************************/
    1849             : /*                   GDALVectorTranslateCreateCopy()                    */
    1850             : /************************************************************************/
    1851             : 
    1852             : static GDALDataset *
    1853          22 : GDALVectorTranslateCreateCopy(GDALDriver *poDriver, const char *pszDest,
    1854             :                               GDALDataset *poDS,
    1855             :                               const GDALVectorTranslateOptions *psOptions)
    1856             : {
    1857          22 :     const char *const szErrorMsg = "%s not supported by this output driver";
    1858             : 
    1859          22 :     if (psOptions->bSkipFailures)
    1860             :     {
    1861           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-skipfailures");
    1862           0 :         return nullptr;
    1863             :     }
    1864          22 :     if (psOptions->nLayerTransaction >= 0)
    1865             :     {
    1866           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg,
    1867             :                  "-lyr_transaction or -ds_transaction");
    1868           0 :         return nullptr;
    1869             :     }
    1870          22 :     if (psOptions->nFIDToFetch >= 0)
    1871             :     {
    1872           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-fid");
    1873           0 :         return nullptr;
    1874             :     }
    1875          22 :     if (!psOptions->aosLCO.empty())
    1876             :     {
    1877           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-lco");
    1878           0 :         return nullptr;
    1879             :     }
    1880          22 :     if (psOptions->bAddMissingFields)
    1881             :     {
    1882           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-addfields");
    1883           0 :         return nullptr;
    1884             :     }
    1885          22 :     if (!psOptions->osSourceSRSDef.empty())
    1886             :     {
    1887           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-s_srs");
    1888           0 :         return nullptr;
    1889             :     }
    1890          22 :     if (!psOptions->bExactFieldNameMatch)
    1891             :     {
    1892           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg,
    1893             :                  "-relaxedFieldNameMatch");
    1894           0 :         return nullptr;
    1895             :     }
    1896          22 :     if (!psOptions->osNewLayerName.empty())
    1897             :     {
    1898           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-nln");
    1899           0 :         return nullptr;
    1900             :     }
    1901          22 :     if (psOptions->bSelFieldsSet)
    1902             :     {
    1903           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-select");
    1904           0 :         return nullptr;
    1905             :     }
    1906          22 :     if (!psOptions->osSQLStatement.empty())
    1907             :     {
    1908           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-sql");
    1909           0 :         return nullptr;
    1910             :     }
    1911          22 :     if (!psOptions->osDialect.empty())
    1912             :     {
    1913           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-dialect");
    1914           0 :         return nullptr;
    1915             :     }
    1916          22 :     if (psOptions->eGType != GEOMTYPE_UNCHANGED ||
    1917          22 :         psOptions->eGeomTypeConversion != GTC_DEFAULT)
    1918             :     {
    1919           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-nlt");
    1920           0 :         return nullptr;
    1921             :     }
    1922          22 :     if (!psOptions->aosFieldTypesToString.empty())
    1923             :     {
    1924           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg,
    1925             :                  "-fieldTypeToString");
    1926           0 :         return nullptr;
    1927             :     }
    1928          22 :     if (!psOptions->aosMapFieldType.empty())
    1929             :     {
    1930           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-mapFieldType");
    1931           0 :         return nullptr;
    1932             :     }
    1933          22 :     if (psOptions->bUnsetFieldWidth)
    1934             :     {
    1935           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-unsetFieldWidth");
    1936           0 :         return nullptr;
    1937             :     }
    1938          22 :     if (psOptions->bWrapDateline)
    1939             :     {
    1940           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-wrapdateline");
    1941           0 :         return nullptr;
    1942             :     }
    1943          22 :     if (psOptions->bClipSrc)
    1944             :     {
    1945           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipsrc");
    1946           0 :         return nullptr;
    1947             :     }
    1948          22 :     if (!psOptions->osClipSrcSQL.empty())
    1949             :     {
    1950           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipsrcsql");
    1951           0 :         return nullptr;
    1952             :     }
    1953          22 :     if (!psOptions->osClipSrcLayer.empty())
    1954             :     {
    1955           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipsrclayer");
    1956           0 :         return nullptr;
    1957             :     }
    1958          22 :     if (!psOptions->osClipSrcWhere.empty())
    1959             :     {
    1960           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipsrcwhere");
    1961           0 :         return nullptr;
    1962             :     }
    1963          22 :     if (!psOptions->osClipDstDS.empty() || psOptions->poClipDst)
    1964             :     {
    1965           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipdst");
    1966           0 :         return nullptr;
    1967             :     }
    1968          22 :     if (!psOptions->osClipDstSQL.empty())
    1969             :     {
    1970           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipdstsql");
    1971           0 :         return nullptr;
    1972             :     }
    1973          22 :     if (!psOptions->osClipDstLayer.empty())
    1974             :     {
    1975           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipdstlayer");
    1976           0 :         return nullptr;
    1977             :     }
    1978          22 :     if (!psOptions->osClipDstWhere.empty())
    1979             :     {
    1980           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-clipdstwhere");
    1981           0 :         return nullptr;
    1982             :     }
    1983          22 :     if (psOptions->bSplitListFields)
    1984             :     {
    1985           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-splitlistfields");
    1986           0 :         return nullptr;
    1987             :     }
    1988          22 :     if (psOptions->nMaxSplitListSubFields >= 0)
    1989             :     {
    1990           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-maxsubfields");
    1991           0 :         return nullptr;
    1992             :     }
    1993          22 :     if (psOptions->bExplodeCollections)
    1994             :     {
    1995           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg,
    1996             :                  "-explodecollections");
    1997           0 :         return nullptr;
    1998             :     }
    1999          22 :     if (!psOptions->osZField.empty())
    2000             :     {
    2001           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-zfield");
    2002           0 :         return nullptr;
    2003             :     }
    2004          22 :     if (!psOptions->asGCPs.empty())
    2005             :     {
    2006           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-gcp");
    2007           0 :         return nullptr;
    2008             :     }
    2009          22 :     if (!psOptions->aosFieldMap.empty())
    2010             :     {
    2011           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-fieldmap");
    2012           0 :         return nullptr;
    2013             :     }
    2014          22 :     if (psOptions->bForceNullable)
    2015             :     {
    2016           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-forceNullable");
    2017           0 :         return nullptr;
    2018             :     }
    2019          22 :     if (psOptions->bResolveDomains)
    2020             :     {
    2021           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-forceNullable");
    2022           0 :         return nullptr;
    2023             :     }
    2024          22 :     if (psOptions->bEmptyStrAsNull)
    2025             :     {
    2026           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-emptyStrAsNull");
    2027           0 :         return nullptr;
    2028             :     }
    2029          22 :     if (psOptions->bUnsetDefault)
    2030             :     {
    2031           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-unsetDefault");
    2032           0 :         return nullptr;
    2033             :     }
    2034          22 :     if (psOptions->bUnsetFid)
    2035             :     {
    2036           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-unsetFid");
    2037           0 :         return nullptr;
    2038             :     }
    2039          22 :     if (!psOptions->bCopyMD)
    2040             :     {
    2041           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-nomd");
    2042           0 :         return nullptr;
    2043             :     }
    2044          22 :     if (!psOptions->bNativeData)
    2045             :     {
    2046           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-noNativeData");
    2047           0 :         return nullptr;
    2048             :     }
    2049          22 :     if (psOptions->nLimit >= 0)
    2050             :     {
    2051           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-limit");
    2052           0 :         return nullptr;
    2053             :     }
    2054          22 :     if (!psOptions->aosMetadataOptions.empty())
    2055             :     {
    2056           0 :         CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-mo");
    2057           0 :         return nullptr;
    2058             :     }
    2059             : 
    2060          22 :     GDALDataset *poWrkSrcDS = poDS;
    2061          22 :     std::unique_ptr<GDALDataset> poWrkSrcDSToFree;
    2062          44 :     OGRSpatialReferenceRefCountedPtr poOutputSRS;
    2063             : 
    2064          22 :     if (!psOptions->osOutputSRSDef.empty())
    2065             :     {
    2066           3 :         poOutputSRS = OGRSpatialReferenceRefCountedPtr::makeInstance();
    2067           3 :         poOutputSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2068           3 :         if (poOutputSRS->SetFromUserInput(psOptions->osOutputSRSDef.c_str()) !=
    2069             :             OGRERR_NONE)
    2070             :         {
    2071           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2072             :                      "Failed to process SRS definition: %s",
    2073             :                      psOptions->osOutputSRSDef.c_str());
    2074           0 :             return nullptr;
    2075             :         }
    2076           3 :         poOutputSRS->SetCoordinateEpoch(psOptions->dfOutputCoordinateEpoch);
    2077             : 
    2078           6 :         poWrkSrcDSToFree = GDALVectorTranslateWrappedDataset::New(
    2079           6 :             poDS, poOutputSRS.get(), psOptions->bTransform);
    2080           3 :         if (poWrkSrcDSToFree == nullptr)
    2081           0 :             return nullptr;
    2082           3 :         poWrkSrcDS = poWrkSrcDSToFree.get();
    2083             :     }
    2084             : 
    2085          22 :     if (!psOptions->osWHERE.empty())
    2086             :     {
    2087             :         // Hack for GMLAS driver
    2088           0 :         if (EQUAL(poDriver->GetDescription(), "GMLAS"))
    2089             :         {
    2090           0 :             if (psOptions->aosLayers.empty())
    2091             :             {
    2092           0 :                 CPLError(CE_Failure, CPLE_NotSupported,
    2093             :                          "-where not supported by this output driver "
    2094             :                          "without explicit layer name(s)");
    2095           0 :                 return nullptr;
    2096             :             }
    2097             :             else
    2098             :             {
    2099           0 :                 for (const char *pszLayer : psOptions->aosLayers)
    2100             :                 {
    2101           0 :                     OGRLayer *poSrcLayer = poDS->GetLayerByName(pszLayer);
    2102           0 :                     if (poSrcLayer != nullptr)
    2103             :                     {
    2104           0 :                         poSrcLayer->SetAttributeFilter(
    2105           0 :                             psOptions->osWHERE.c_str());
    2106             :                     }
    2107             :                 }
    2108             :             }
    2109             :         }
    2110             :         else
    2111             :         {
    2112           0 :             CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg, "-where");
    2113           0 :             return nullptr;
    2114             :         }
    2115             :     }
    2116             : 
    2117          22 :     if (psOptions->poSpatialFilter)
    2118             :     {
    2119           0 :         for (int i = 0; i < poWrkSrcDS->GetLayerCount(); ++i)
    2120             :         {
    2121           0 :             OGRLayer *poSrcLayer = poWrkSrcDS->GetLayer(i);
    2122           0 :             if (poSrcLayer &&
    2123           0 :                 poSrcLayer->GetLayerDefn()->GetGeomFieldCount() > 0 &&
    2124           0 :                 (psOptions->aosLayers.empty() ||
    2125           0 :                  psOptions->aosLayers.FindString(poSrcLayer->GetName()) >= 0))
    2126             :             {
    2127           0 :                 if (psOptions->bGeomFieldSet)
    2128             :                 {
    2129             :                     const int iGeomField =
    2130           0 :                         poSrcLayer->GetLayerDefn()->GetGeomFieldIndex(
    2131           0 :                             psOptions->osGeomField.c_str());
    2132           0 :                     if (iGeomField >= 0)
    2133           0 :                         poSrcLayer->SetSpatialFilter(
    2134           0 :                             iGeomField, psOptions->poSpatialFilter.get());
    2135             :                     else
    2136           0 :                         CPLError(CE_Warning, CPLE_AppDefined,
    2137             :                                  "Cannot find geometry field %s in layer %s. "
    2138             :                                  "Applying to first geometry field",
    2139             :                                  psOptions->osGeomField.c_str(),
    2140           0 :                                  poSrcLayer->GetName());
    2141             :                 }
    2142             :                 else
    2143             :                 {
    2144           0 :                     poSrcLayer->SetSpatialFilter(
    2145           0 :                         psOptions->poSpatialFilter.get());
    2146             :                 }
    2147             :             }
    2148             :         }
    2149             :     }
    2150             : 
    2151          44 :     CPLStringList aosDSCO(psOptions->aosDSCO);
    2152          22 :     if (!psOptions->aosLayers.empty())
    2153             :     {
    2154             :         // Hack for GMLAS driver
    2155           0 :         if (EQUAL(poDriver->GetDescription(), "GMLAS"))
    2156             :         {
    2157           0 :             CPLString osLayers;
    2158           0 :             for (const char *pszLayer : psOptions->aosLayers)
    2159             :             {
    2160           0 :                 if (!osLayers.empty())
    2161           0 :                     osLayers += ",";
    2162           0 :                 osLayers += pszLayer;
    2163             :             }
    2164           0 :             aosDSCO.SetNameValue("LAYERS", osLayers);
    2165             :         }
    2166             :         else
    2167             :         {
    2168           0 :             CPLError(CE_Failure, CPLE_NotSupported, szErrorMsg,
    2169             :                      "Specifying layers");
    2170           0 :             return nullptr;
    2171             :         }
    2172             :     }
    2173             : 
    2174             :     // Hack for GMLAS driver (this speed up deletion by avoiding the GML
    2175             :     // driver to try parsing a pre-existing file). Could be potentially
    2176             :     // removed if the GML driver implemented fast dataset opening (ie
    2177             :     // without parsing) and GetFileList()
    2178          22 :     if (EQUAL(poDriver->GetDescription(), "GMLAS"))
    2179             :     {
    2180          22 :         GDALDriverH hIdentifyingDriver = GDALIdentifyDriver(pszDest, nullptr);
    2181          23 :         if (hIdentifyingDriver != nullptr &&
    2182           1 :             EQUAL(GDALGetDescription(hIdentifyingDriver), "GML"))
    2183             :         {
    2184           0 :             VSIUnlink(pszDest);
    2185           0 :             VSIUnlink(CPLResetExtensionSafe(pszDest, "gfs").c_str());
    2186             :         }
    2187             :     }
    2188             : 
    2189             :     GDALDataset *poOut =
    2190          22 :         poDriver->CreateCopy(pszDest, poWrkSrcDS, FALSE, aosDSCO.List(),
    2191          22 :                              psOptions->pfnProgress, psOptions->pProgressData);
    2192             : 
    2193          22 :     return poOut;
    2194             : }
    2195             : 
    2196             : /************************************************************************/
    2197             : /*                         CopyRelationships()                          */
    2198             : /************************************************************************/
    2199             : 
    2200         988 : static void CopyRelationships(GDALDataset *poODS, GDALDataset *poDS)
    2201             : {
    2202         988 :     if (!poODS->GetDriver()->GetMetadataItem(GDAL_DCAP_CREATE_RELATIONSHIP))
    2203         983 :         return;
    2204             : 
    2205         401 :     const auto aosRelationshipNames = poDS->GetRelationshipNames();
    2206         401 :     if (aosRelationshipNames.empty())
    2207         396 :         return;
    2208             : 
    2209             :     // Collect target layer names
    2210          10 :     std::set<std::string> oSetDestLayerNames;
    2211          30 :     for (const auto &poLayer : poDS->GetLayers())
    2212             :     {
    2213          25 :         oSetDestLayerNames.insert(poLayer->GetName());
    2214             :     }
    2215             : 
    2216             :     // Iterate over all source relationships
    2217          20 :     for (const auto &osRelationshipName : aosRelationshipNames)
    2218             :     {
    2219             :         const auto poSrcRelationship =
    2220          15 :             poDS->GetRelationship(osRelationshipName);
    2221          15 :         if (!poSrcRelationship)
    2222           0 :             continue;
    2223             : 
    2224             :         // Skip existing relationship of the same name
    2225          15 :         if (poODS->GetRelationship(osRelationshipName))
    2226           0 :             continue;
    2227             : 
    2228          15 :         bool canAdd = true;
    2229          15 :         const auto &osLeftTableName = poSrcRelationship->GetLeftTableName();
    2230          30 :         if (!osLeftTableName.empty() &&
    2231          15 :             !cpl::contains(oSetDestLayerNames, osLeftTableName))
    2232             :         {
    2233           1 :             CPLDebug("GDALVectorTranslate",
    2234             :                      "Skipping relationship %s because its left table (%s) "
    2235             :                      "does not exist in target dataset",
    2236             :                      osRelationshipName.c_str(), osLeftTableName.c_str());
    2237           1 :             canAdd = false;
    2238             :         }
    2239             : 
    2240          15 :         const auto &osRightTableName = poSrcRelationship->GetRightTableName();
    2241          30 :         if (!osRightTableName.empty() &&
    2242          15 :             !cpl::contains(oSetDestLayerNames, osRightTableName))
    2243             :         {
    2244           0 :             CPLDebug("GDALVectorTranslate",
    2245             :                      "Skipping relationship %s because its right table (%s) "
    2246             :                      "does not exist in target dataset",
    2247             :                      osRelationshipName.c_str(), osRightTableName.c_str());
    2248           0 :             canAdd = false;
    2249             :         }
    2250             : 
    2251             :         const auto &osMappingTableName =
    2252          15 :             poSrcRelationship->GetMappingTableName();
    2253          21 :         if (!osMappingTableName.empty() &&
    2254           6 :             !cpl::contains(oSetDestLayerNames, osMappingTableName))
    2255             :         {
    2256           0 :             CPLDebug("GDALVectorTranslate",
    2257             :                      "Skipping relationship %s because its mapping table (%s) "
    2258             :                      "does not exist in target dataset",
    2259             :                      osRelationshipName.c_str(), osMappingTableName.c_str());
    2260           0 :             canAdd = false;
    2261             :         }
    2262             : 
    2263          15 :         if (canAdd)
    2264             :         {
    2265          28 :             std::string osFailureReason;
    2266          14 :             if (!poODS->AddRelationship(
    2267          14 :                     std::make_unique<GDALRelationship>(*poSrcRelationship),
    2268          14 :                     osFailureReason))
    2269             :             {
    2270           3 :                 CPLDebug("GDALVectorTranslate",
    2271             :                          "Cannot add relationship %s: %s",
    2272             :                          osRelationshipName.c_str(), osFailureReason.c_str());
    2273             :             }
    2274             :         }
    2275             :     }
    2276             : }
    2277             : 
    2278             : /************************************************************************/
    2279             : /*                        GDALVectorTranslate()                         */
    2280             : /************************************************************************/
    2281             : /**
    2282             :  * Converts vector data between file formats.
    2283             :  *
    2284             :  * This is the equivalent of the <a href="/programs/ogr2ogr.html">ogr2ogr</a>
    2285             :  * utility.
    2286             :  *
    2287             :  * GDALVectorTranslateOptions* must be allocated and freed with
    2288             :  * GDALVectorTranslateOptionsNew() and GDALVectorTranslateOptionsFree()
    2289             :  * respectively. pszDest and hDstDS cannot be used at the same time.
    2290             :  *
    2291             :  * @param pszDest the destination dataset path or NULL.
    2292             :  * @param hDstDS the destination dataset or NULL.
    2293             :  * @param nSrcCount the number of input datasets (only 1 supported currently)
    2294             :  * @param pahSrcDS the list of input datasets.
    2295             :  * @param psOptionsIn the options struct returned by
    2296             :  * GDALVectorTranslateOptionsNew() or NULL.
    2297             :  * @param pbUsageError pointer to a integer output variable to store if any
    2298             :  * usage error has occurred, or NULL.
    2299             :  * @return the output dataset (new dataset that must be closed using
    2300             :  * GDALClose(), or hDstDS is not NULL) or NULL in case of error.
    2301             :  *
    2302             :  * @since GDAL 2.1
    2303             :  */
    2304             : 
    2305        1035 : GDALDatasetH GDALVectorTranslate(const char *pszDest, GDALDatasetH hDstDS,
    2306             :                                  int nSrcCount, GDALDatasetH *pahSrcDS,
    2307             :                                  const GDALVectorTranslateOptions *psOptionsIn,
    2308             :                                  int *pbUsageError)
    2309             : 
    2310             : {
    2311        1035 :     if (pszDest == nullptr && hDstDS == nullptr)
    2312             :     {
    2313           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    2314             :                  "pszDest == NULL && hDstDS == NULL");
    2315             : 
    2316           0 :         if (pbUsageError)
    2317           0 :             *pbUsageError = TRUE;
    2318           0 :         return nullptr;
    2319             :     }
    2320        1035 :     if (nSrcCount != 1)
    2321             :     {
    2322           0 :         CPLError(CE_Failure, CPLE_AppDefined, "nSrcCount != 1");
    2323             : 
    2324           0 :         if (pbUsageError)
    2325           0 :             *pbUsageError = TRUE;
    2326           0 :         return nullptr;
    2327             :     }
    2328             : 
    2329        1035 :     GDALDatasetH hSrcDS = pahSrcDS[0];
    2330        1035 :     if (hSrcDS == nullptr)
    2331             :     {
    2332           0 :         CPLError(CE_Failure, CPLE_AppDefined, "hSrcDS == NULL");
    2333             : 
    2334           0 :         if (pbUsageError)
    2335           0 :             *pbUsageError = TRUE;
    2336           0 :         return nullptr;
    2337             :     }
    2338             : 
    2339             :     auto psOptions =
    2340             :         psOptionsIn ? std::make_unique<GDALVectorTranslateOptions>(*psOptionsIn)
    2341        2070 :                     : std::make_unique<GDALVectorTranslateOptions>();
    2342             : 
    2343        1035 :     bool bAppend = false;
    2344        1035 :     bool bUpdate = false;
    2345        1035 :     bool bOverwrite = false;
    2346             : 
    2347        1035 :     if (psOptions->eAccessMode == ACCESS_UPDATE)
    2348             :     {
    2349           5 :         bUpdate = true;
    2350             :     }
    2351        1030 :     else if (psOptions->eAccessMode == ACCESS_APPEND)
    2352             :     {
    2353          44 :         bAppend = true;
    2354          44 :         bUpdate = true;
    2355             :     }
    2356         986 :     else if (psOptions->eAccessMode == ACCESS_OVERWRITE)
    2357             :     {
    2358          18 :         bOverwrite = true;
    2359          18 :         bUpdate = true;
    2360             :     }
    2361         968 :     else if (hDstDS != nullptr)
    2362             :     {
    2363           9 :         bUpdate = true;
    2364             :     }
    2365             : 
    2366        1035 :     if (psOptions->bPreserveFID && psOptions->bExplodeCollections)
    2367             :     {
    2368           1 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2369             :                  "cannot use -preserve_fid and -explodecollections at the same "
    2370             :                  "time.");
    2371           1 :         if (pbUsageError)
    2372           1 :             *pbUsageError = TRUE;
    2373           1 :         return nullptr;
    2374             :     }
    2375             : 
    2376        1034 :     if (!psOptions->aosFieldMap.empty() && !bAppend)
    2377             :     {
    2378           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2379             :                  "if -fieldmap is specified, -append must also be specified");
    2380           0 :         if (pbUsageError)
    2381           0 :             *pbUsageError = TRUE;
    2382           0 :         return nullptr;
    2383             :     }
    2384             : 
    2385        1034 :     if (!psOptions->aosFieldMap.empty() && psOptions->bAddMissingFields)
    2386             :     {
    2387           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2388             :                  "if -addfields is specified, -fieldmap cannot be used.");
    2389           0 :         if (pbUsageError)
    2390           0 :             *pbUsageError = TRUE;
    2391           0 :         return nullptr;
    2392             :     }
    2393             : 
    2394        1034 :     if (psOptions->bSelFieldsSet && bAppend && !psOptions->bAddMissingFields)
    2395             :     {
    2396           1 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2397             :                  "if -append is specified, -select cannot be used "
    2398             :                  "(use -fieldmap or -sql instead).");
    2399           1 :         if (pbUsageError)
    2400           1 :             *pbUsageError = TRUE;
    2401           1 :         return nullptr;
    2402             :     }
    2403             : 
    2404        1033 :     if (!psOptions->aosFieldTypesToString.empty() &&
    2405           0 :         !psOptions->aosMapFieldType.empty())
    2406             :     {
    2407           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2408             :                  "-fieldTypeToString and -mapFieldType are exclusive.");
    2409           0 :         if (pbUsageError)
    2410           0 :             *pbUsageError = TRUE;
    2411           0 :         return nullptr;
    2412             :     }
    2413             : 
    2414        1041 :     if (!psOptions->osSourceSRSDef.empty() &&
    2415        1041 :         psOptions->osOutputSRSDef.empty() && psOptions->osSpatSRSDef.empty())
    2416             :     {
    2417           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2418             :                  "if -s_srs is specified, -t_srs and/or -spat_srs must also be "
    2419             :                  "specified.");
    2420           0 :         if (pbUsageError)
    2421           0 :             *pbUsageError = TRUE;
    2422           0 :         return nullptr;
    2423             :     }
    2424             : 
    2425             :     /* -------------------------------------------------------------------- */
    2426             :     /*      Parse spatial filter SRS if needed.                             */
    2427             :     /* -------------------------------------------------------------------- */
    2428        2066 :     OGRSpatialReferenceRefCountedPtr poSpatSRS;
    2429        1033 :     if (psOptions->poSpatialFilter && !psOptions->osSpatSRSDef.empty())
    2430             :     {
    2431           4 :         if (!psOptions->osSQLStatement.empty())
    2432             :         {
    2433           0 :             CPLError(CE_Failure, CPLE_IllegalArg,
    2434             :                      "-spat_srs not compatible with -sql.");
    2435           0 :             return nullptr;
    2436             :         }
    2437           4 :         OGREnvelope sEnvelope;
    2438           4 :         psOptions->poSpatialFilter->getEnvelope(&sEnvelope);
    2439           4 :         poSpatSRS = OGRSpatialReferenceRefCountedPtr::makeInstance();
    2440           4 :         poSpatSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2441           4 :         if (poSpatSRS->SetFromUserInput(psOptions->osSpatSRSDef.c_str()) !=
    2442             :             OGRERR_NONE)
    2443             :         {
    2444           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2445             :                      "Failed to process SRS definition: %s",
    2446           0 :                      psOptions->osSpatSRSDef.c_str());
    2447           0 :             return nullptr;
    2448             :         }
    2449             :     }
    2450             : 
    2451        1033 :     if (!psOptions->poClipSrc && !psOptions->osClipSrcDS.empty())
    2452             :     {
    2453          10 :         psOptions->poClipSrc =
    2454          20 :             LoadGeometry(psOptions->osClipSrcDS, psOptions->osClipSrcSQL,
    2455          10 :                          psOptions->osClipSrcLayer, psOptions->osClipSrcWhere,
    2456          20 :                          psOptions->bMakeValid);
    2457          10 :         if (psOptions->poClipSrc == nullptr)
    2458             :         {
    2459           2 :             CPLError(CE_Failure, CPLE_IllegalArg,
    2460             :                      "cannot load source clip geometry");
    2461           2 :             return nullptr;
    2462             :         }
    2463             :     }
    2464        1024 :     else if (psOptions->bClipSrc && !psOptions->poClipSrc &&
    2465           1 :              psOptions->poSpatialFilter)
    2466             :     {
    2467           1 :         psOptions->poClipSrc.reset(psOptions->poSpatialFilter->clone());
    2468           1 :         if (poSpatSRS)
    2469             :         {
    2470           0 :             psOptions->poClipSrc->assignSpatialReference(poSpatSRS.get());
    2471             :         }
    2472             :     }
    2473        1022 :     else if (psOptions->bClipSrc && !psOptions->poClipSrc)
    2474             :     {
    2475           0 :         CPLError(CE_Failure, CPLE_IllegalArg,
    2476             :                  "-clipsrc must be used with -spat option or a\n"
    2477             :                  "bounding box, WKT string or datasource must be specified");
    2478           0 :         if (pbUsageError)
    2479           0 :             *pbUsageError = TRUE;
    2480           0 :         return nullptr;
    2481             :     }
    2482             : 
    2483        2062 :     std::string osReason;
    2484        1031 :     if (psOptions->poClipSrc && !psOptions->poClipSrc->IsValid(&osReason))
    2485             :     {
    2486           2 :         if (!psOptions->bMakeValid)
    2487             :         {
    2488           1 :             CPLError(
    2489             :                 CE_Failure, CPLE_IllegalArg,
    2490             :                 "-clipsrc geometry is invalid (%s). You can try to make it "
    2491             :                 "valid with -makevalid, but the results of the operation "
    2492             :                 "should be manually inspected.",
    2493             :                 osReason.c_str());
    2494           1 :             return nullptr;
    2495             :         }
    2496             :         auto poValid =
    2497           1 :             std::unique_ptr<OGRGeometry>(psOptions->poClipSrc->MakeValid());
    2498           1 :         if (!poValid)
    2499             :         {
    2500           0 :             CPLError(
    2501             :                 CE_Failure, CPLE_IllegalArg,
    2502             :                 "-clipsrc geometry is invalid (%s) and cannot be made valid.",
    2503             :                 osReason.c_str());
    2504           0 :             return nullptr;
    2505             :         }
    2506           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    2507             :                  "-clipsrc geometry was invalid (%s) and has been made valid, "
    2508             :                  "but the results of the operation "
    2509             :                  "should be manually inspected.",
    2510             :                  osReason.c_str());
    2511           1 :         psOptions->poClipSrc = std::move(poValid);
    2512             :     }
    2513             : 
    2514        1030 :     if (!psOptions->osClipDstDS.empty())
    2515             :     {
    2516          10 :         psOptions->poClipDst =
    2517          20 :             LoadGeometry(psOptions->osClipDstDS, psOptions->osClipDstSQL,
    2518          10 :                          psOptions->osClipDstLayer, psOptions->osClipDstWhere,
    2519          20 :                          psOptions->bMakeValid);
    2520          10 :         if (psOptions->poClipDst == nullptr)
    2521             :         {
    2522           3 :             CPLError(CE_Failure, CPLE_IllegalArg,
    2523             :                      "cannot load dest clip geometry");
    2524           3 :             return nullptr;
    2525             :         }
    2526             :     }
    2527             : 
    2528        1027 :     if (psOptions->poClipDst && !psOptions->poClipDst->IsValid(&osReason))
    2529             :     {
    2530           2 :         if (!psOptions->bMakeValid)
    2531             :         {
    2532           1 :             CPLError(
    2533             :                 CE_Failure, CPLE_IllegalArg,
    2534             :                 "-clipdst geometry is invalid (%s). You can try to make it "
    2535             :                 "valid with -makevalid, but the results of the operation "
    2536             :                 "should be manually inspected.",
    2537             :                 osReason.c_str());
    2538           1 :             return nullptr;
    2539             :         }
    2540             :         auto poValid =
    2541           1 :             std::unique_ptr<OGRGeometry>(psOptions->poClipDst->MakeValid());
    2542           1 :         if (!poValid)
    2543             :         {
    2544           0 :             CPLError(
    2545             :                 CE_Failure, CPLE_IllegalArg,
    2546             :                 "-clipdst geometry is invalid (%s) and cannot be made valid.",
    2547             :                 osReason.c_str());
    2548           0 :             return nullptr;
    2549             :         }
    2550           1 :         CPLError(CE_Warning, CPLE_AppDefined,
    2551             :                  "-clipdst geometry was invalid (%s) and has been made valid, "
    2552             :                  "but the results of the operation "
    2553             :                  "should be manually inspected.",
    2554             :                  osReason.c_str());
    2555           1 :         psOptions->poClipDst = std::move(poValid);
    2556             :     }
    2557             : 
    2558        1026 :     GDALDataset *poDS = GDALDataset::FromHandle(hSrcDS);
    2559        1026 :     GDALDataset *poODS = nullptr;
    2560        1026 :     GDALDriver *poDriver = nullptr;
    2561        2052 :     CPLString osDestFilename;
    2562             : 
    2563        1026 :     if (hDstDS)
    2564             :     {
    2565          31 :         poODS = GDALDataset::FromHandle(hDstDS);
    2566          31 :         osDestFilename = poODS->GetDescription();
    2567             :     }
    2568             :     else
    2569             :     {
    2570         995 :         osDestFilename = pszDest;
    2571             :     }
    2572             : 
    2573             :     /* Various tests to avoid overwriting the source layer(s) */
    2574             :     /* or to avoid appending a layer to itself */
    2575          75 :     if (bUpdate && strcmp(osDestFilename, poDS->GetDescription()) == 0 &&
    2576           6 :         !EQUAL(poDS->GetDriverName(), "MEM") &&
    2577        1101 :         !EQUAL(poDS->GetDriverName(), "Memory") && (bOverwrite || bAppend))
    2578             :     {
    2579           2 :         bool bError = false;
    2580           2 :         if (psOptions->osNewLayerName.empty())
    2581           1 :             bError = true;
    2582           1 :         else if (psOptions->aosLayers.size() == 1)
    2583           1 :             bError = strcmp(psOptions->osNewLayerName.c_str(),
    2584           1 :                             psOptions->aosLayers[0]) == 0;
    2585           0 :         else if (psOptions->osSQLStatement.empty())
    2586             :         {
    2587           0 :             if (psOptions->aosLayers.empty() && poDS->GetLayerCount() == 1)
    2588             :             {
    2589           0 :                 bError = strcmp(psOptions->osNewLayerName.c_str(),
    2590           0 :                                 poDS->GetLayer(0)->GetName()) == 0;
    2591             :             }
    2592             :             else
    2593             :             {
    2594           0 :                 bError = true;
    2595             :             }
    2596             :         }
    2597           2 :         if (bError)
    2598             :         {
    2599           1 :             if (psOptions->bInvokedFromGdalAlgorithm)
    2600             :             {
    2601           1 :                 CPLError(CE_Failure, CPLE_IllegalArg,
    2602             :                          "--output-layer name must be specified combined with "
    2603             :                          "a single source layer name and it "
    2604             :                          "must be different from an existing layer.");
    2605             :             }
    2606             :             else
    2607             :             {
    2608           0 :                 CPLError(
    2609             :                     CE_Failure, CPLE_IllegalArg,
    2610             :                     "-nln name must be specified combined with "
    2611             :                     "a single source layer name,\nor a -sql statement, and "
    2612             :                     "name must be different from an existing layer.");
    2613             :             }
    2614           1 :             return nullptr;
    2615             :         }
    2616             :     }
    2617        1275 :     else if (!bUpdate && strcmp(osDestFilename, poDS->GetDescription()) == 0 &&
    2618         126 :              (psOptions->osFormat.empty() ||
    2619         125 :               (!EQUAL(psOptions->osFormat.c_str(), "MEM") &&
    2620           0 :                !EQUAL(psOptions->osFormat.c_str(), "Memory"))))
    2621             :     {
    2622           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    2623             :                  "Source and destination datasets must be different "
    2624             :                  "in non-update mode.");
    2625           1 :         return nullptr;
    2626             :     }
    2627             : 
    2628             :     /* -------------------------------------------------------------------- */
    2629             :     /*      Try opening the output datasource as an existing, writable      */
    2630             :     /* -------------------------------------------------------------------- */
    2631        2048 :     std::vector<std::string> aoDrivers;
    2632        1024 :     if (poODS == nullptr && psOptions->osFormat.empty())
    2633             :     {
    2634         415 :         const auto nErrorCount = CPLGetErrorCounter();
    2635         415 :         aoDrivers = CPLStringList(GDALGetOutputDriversForDatasetName(
    2636             :             pszDest, GDAL_OF_VECTOR, /* bSingleMatch = */ true,
    2637         415 :             /* bWarn = */ true));
    2638         415 :         if (!bUpdate && aoDrivers.size() == 1)
    2639             :         {
    2640         368 :             GDALDriverH hDriver = GDALGetDriverByName(aoDrivers[0].c_str());
    2641         368 :             const char *pszPrefix = GDALGetMetadataItem(
    2642             :                 hDriver, GDAL_DMD_CONNECTION_PREFIX, nullptr);
    2643         368 :             if (pszPrefix && STARTS_WITH_CI(pszDest, pszPrefix))
    2644             :             {
    2645           4 :                 bUpdate = true;
    2646             :             }
    2647             :         }
    2648          48 :         else if (aoDrivers.empty() && CPLGetErrorCounter() > nErrorCount &&
    2649           1 :                  CPLGetLastErrorType() == CE_Failure)
    2650             :         {
    2651           1 :             return nullptr;
    2652             :         }
    2653             :     }
    2654             : 
    2655        1023 :     if (bUpdate && poODS == nullptr)
    2656             :     {
    2657          48 :         poODS = GDALDataset::Open(
    2658             :             osDestFilename, GDAL_OF_UPDATE | GDAL_OF_VECTOR, nullptr,
    2659          48 :             psOptions->aosDestOpenOptions.List(), nullptr);
    2660             : 
    2661          48 :         if (poODS == nullptr)
    2662             :         {
    2663           3 :             if (bOverwrite || bAppend)
    2664             :             {
    2665           3 :                 poODS = GDALDataset::Open(
    2666             :                     osDestFilename, GDAL_OF_VECTOR, nullptr,
    2667           3 :                     psOptions->aosDestOpenOptions.List(), nullptr);
    2668           3 :                 if (poODS == nullptr)
    2669             :                 {
    2670             :                     /* OK the datasource doesn't exist at all */
    2671           3 :                     bUpdate = false;
    2672             :                 }
    2673             :                 else
    2674             :                 {
    2675           0 :                     poDriver = poODS->GetDriver();
    2676           0 :                     GDALClose(poODS);
    2677           0 :                     poODS = nullptr;
    2678             :                 }
    2679             :             }
    2680             : 
    2681           3 :             if (bUpdate)
    2682             :             {
    2683           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    2684             :                          "Unable to open existing output datasource `%s'.",
    2685             :                          osDestFilename.c_str());
    2686           0 :                 return nullptr;
    2687             :             }
    2688             :         }
    2689          45 :         else if (psOptions->aosDSCO.size() > 0)
    2690             :         {
    2691           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2692             :                      "Datasource creation options ignored since an existing "
    2693             :                      "datasource\n"
    2694             :                      "         being updated.");
    2695             :         }
    2696             :     }
    2697             : 
    2698        1023 :     if (poODS)
    2699          75 :         poDriver = poODS->GetDriver();
    2700             : 
    2701             :     /* -------------------------------------------------------------------- */
    2702             :     /*      Find the output driver.                                         */
    2703             :     /* -------------------------------------------------------------------- */
    2704        1023 :     bool bNewDataSource = false;
    2705        1023 :     if (!bUpdate)
    2706             :     {
    2707         948 :         GDALDriverManager *poDM = GetGDALDriverManager();
    2708             : 
    2709         948 :         if (psOptions->bNoOverwrite && !EQUAL(pszDest, ""))
    2710             :         {
    2711         155 :             const char *pszType = "";
    2712         155 :             if (GDALDoesFileOrDatasetExist(pszDest, &pszType))
    2713             :             {
    2714           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    2715             :                          "%s '%s' already exists. Specify the --overwrite "
    2716             :                          "option to overwrite it.",
    2717             :                          pszType, pszDest);
    2718           0 :                 return nullptr;
    2719             :             }
    2720             :         }
    2721             : 
    2722         948 :         if (psOptions->osFormat.empty())
    2723             :         {
    2724         379 :             if (aoDrivers.empty())
    2725             :             {
    2726          23 :                 if (CPLGetExtensionSafe(pszDest).empty() &&
    2727          11 :                     !psOptions->bInvokedFromGdalAlgorithm)
    2728             :                 {
    2729          10 :                     psOptions->osFormat = "ESRI Shapefile";
    2730             :                 }
    2731             :                 else
    2732             :                 {
    2733           2 :                     CPLError(CE_Failure, CPLE_AppDefined,
    2734             :                              "Cannot guess driver for %s", pszDest);
    2735           2 :                     return nullptr;
    2736             :                 }
    2737             :             }
    2738             :             else
    2739             :             {
    2740         367 :                 psOptions->osFormat = aoDrivers[0];
    2741             :             }
    2742         377 :             CPLDebug("GDAL", "Using %s driver", psOptions->osFormat.c_str());
    2743             :         }
    2744             : 
    2745         946 :         CPLString osOGRCompatFormat(psOptions->osFormat);
    2746             :         // Special processing for non-unified drivers that have the same name
    2747             :         // as GDAL and OGR drivers. GMT should become OGR_GMT.
    2748             :         // Other candidates could be VRT, SDTS and PDS, but they don't
    2749             :         // have write capabilities. But do the substitution to get a sensible
    2750             :         // error message
    2751         946 :         if (EQUAL(osOGRCompatFormat, "GMT") ||
    2752         945 :             EQUAL(osOGRCompatFormat, "VRT") ||
    2753        1891 :             EQUAL(osOGRCompatFormat, "SDTS") || EQUAL(osOGRCompatFormat, "PDS"))
    2754             :         {
    2755           1 :             osOGRCompatFormat = "OGR_" + osOGRCompatFormat;
    2756             :         }
    2757         946 :         poDriver = poDM->GetDriverByName(osOGRCompatFormat);
    2758         946 :         if (poDriver == nullptr)
    2759             :         {
    2760           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Unable to find driver `%s'.",
    2761           0 :                      psOptions->osFormat.c_str());
    2762           0 :             return nullptr;
    2763             :         }
    2764             : 
    2765         946 :         CSLConstList papszDriverMD = poDriver->GetMetadata();
    2766         946 :         if (!CPLTestBool(
    2767             :                 CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_VECTOR, "FALSE")))
    2768             :         {
    2769           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2770             :                      "%s driver has no vector capabilities.",
    2771           0 :                      psOptions->osFormat.c_str());
    2772           0 :             return nullptr;
    2773             :         }
    2774             : 
    2775         946 :         if (poDriver->CanVectorTranslateFrom(
    2776         946 :                 pszDest, poDS, psOptions->aosArguments.List(), nullptr))
    2777             :         {
    2778           4 :             return poDriver->VectorTranslateFrom(
    2779           4 :                 pszDest, poDS, psOptions->aosArguments.List(),
    2780           8 :                 psOptions->pfnProgress, psOptions->pProgressData);
    2781             :         }
    2782             : 
    2783         942 :         if (!CPLTestBool(
    2784             :                 CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
    2785             :         {
    2786          22 :             if (CPLTestBool(CSLFetchNameValueDef(
    2787             :                     papszDriverMD, GDAL_DCAP_CREATECOPY, "FALSE")))
    2788             :             {
    2789          22 :                 poODS = GDALVectorTranslateCreateCopy(poDriver, pszDest, poDS,
    2790          22 :                                                       psOptions.get());
    2791          22 :                 return poODS;
    2792             :             }
    2793             : 
    2794           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2795             :                      "%s driver does not support data source creation.",
    2796           0 :                      psOptions->osFormat.c_str());
    2797           0 :             return nullptr;
    2798             :         }
    2799             : 
    2800         920 :         if (!psOptions->aosDestOpenOptions.empty())
    2801             :         {
    2802           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    2803             :                      "-doo ignored when creating the output datasource.");
    2804             :         }
    2805             : 
    2806             :         const bool bSingleLayer =
    2807         920 :             (!psOptions->osSQLStatement.empty() ||
    2808        1820 :              psOptions->aosLayers.size() == 1 ||
    2809         900 :              (psOptions->aosLayers.empty() && poDS->GetLayerCount() == 1));
    2810             : 
    2811             :         bool bOutputDirectory =
    2812        1002 :             !bSingleLayer && CPLGetExtensionSafe(osDestFilename).empty() &&
    2813          30 :             poDriver->GetMetadataItem(
    2814         950 :                 GDAL_DCAP_MULTIPLE_VECTOR_LAYERS_IN_DIRECTORY);
    2815             : 
    2816             :         /* ------------------------------------------------------------------ */
    2817             :         /*   Special case to improve user experience when translating         */
    2818             :         /*   a datasource with multiple layers into a shapefile. If the       */
    2819             :         /*   user gives a target datasource with .shp and it does not exist,  */
    2820             :         /*   the shapefile driver will try to create a file, but this is not  */
    2821             :         /*   appropriate because here we have several layers, so create       */
    2822             :         /*   a directory instead.                                             */
    2823             :         /* ------------------------------------------------------------------ */
    2824             : 
    2825             :         VSIStatBufL sStat;
    2826        2893 :         if (EQUAL(poDriver->GetDescription(), "ESRI Shapefile") &&
    2827         133 :             !bSingleLayer && psOptions->osNewLayerName.empty() &&
    2828        1054 :             EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "SHP") &&
    2829           1 :             VSIStatL(osDestFilename, &sStat) != 0)
    2830             :         {
    2831           1 :             if (VSIMkdir(osDestFilename, 0755) != 0)
    2832             :             {
    2833           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    2834             :                          "Failed to create directory %s\n"
    2835             :                          "for shapefile datastore.",
    2836             :                          osDestFilename.c_str());
    2837           0 :                 return nullptr;
    2838             :             }
    2839           1 :             bOutputDirectory = true;
    2840             :         }
    2841             : 
    2842        1145 :         if (psOptions->bInvokedFromGdalAlgorithm && !bSingleLayer &&
    2843        1160 :             !bOutputDirectory &&
    2844          15 :             !poDriver->GetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS))
    2845             :         {
    2846           1 :             CPLError(CE_Failure, CPLE_AppDefined,
    2847             :                      "%s driver does not support multiple layers.",
    2848           1 :                      poDriver->GetDescription());
    2849           1 :             return nullptr;
    2850             :         }
    2851             : 
    2852         919 :         CPLStringList aosDSCO(psOptions->aosDSCO);
    2853             : 
    2854         919 :         if (!aosDSCO.FetchNameValue("SINGLE_LAYER"))
    2855             :         {
    2856             :             // Informs the target driver (e.g. JSONFG) if a single layer
    2857             :             // will be created
    2858             :             const char *pszCOList =
    2859         919 :                 poDriver->GetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST);
    2860         919 :             if (bSingleLayer && pszCOList && strstr(pszCOList, "SINGLE_LAYER"))
    2861             :             {
    2862           4 :                 aosDSCO.SetNameValue("SINGLE_LAYER", "YES");
    2863             :             }
    2864             :         }
    2865             : 
    2866             :         /* --------------------------------------------------------------------
    2867             :          */
    2868             :         /*      Create the output data source. */
    2869             :         /* --------------------------------------------------------------------
    2870             :          */
    2871         919 :         poODS = poDriver->Create(osDestFilename, 0, 0, 0, GDT_Unknown,
    2872         919 :                                  aosDSCO.List());
    2873         919 :         if (poODS == nullptr)
    2874             :         {
    2875           6 :             CPLError(CE_Failure, CPLE_AppDefined,
    2876             :                      "%s driver failed to create %s",
    2877           3 :                      psOptions->osFormat.c_str(), osDestFilename.c_str());
    2878           3 :             return nullptr;
    2879             :         }
    2880         916 :         bNewDataSource = true;
    2881             : 
    2882         916 :         if (psOptions->bCopyMD)
    2883             :         {
    2884        1824 :             const CPLStringList aosDomains(poDS->GetMetadataDomainList());
    2885         954 :             for (const char *pszMD : aosDomains)
    2886             :             {
    2887          42 :                 if (CSLConstList papszMD = poDS->GetMetadata(pszMD))
    2888          10 :                     poODS->SetMetadata(papszMD, pszMD);
    2889             :             }
    2890             :         }
    2891           2 :         for (const auto &[pszKey, pszValue] :
    2892         918 :              cpl::IterateNameValue(psOptions->aosMetadataOptions))
    2893             :         {
    2894           1 :             poODS->SetMetadataItem(pszKey, pszValue);
    2895             :         }
    2896             : 
    2897             :         // When writing to GeoJSON and using -nln, set the @NAME layer
    2898             :         // creation option to avoid the GeoJSON driver to potentially reuse
    2899             :         // the source feature collection name if the input is also GeoJSON.
    2900         928 :         if (!psOptions->osNewLayerName.empty() &&
    2901          12 :             EQUAL(psOptions->osFormat.c_str(), "GeoJSON"))
    2902             :         {
    2903           1 :             psOptions->aosLCO.SetNameValue("@NAME",
    2904           1 :                                            psOptions->osNewLayerName.c_str());
    2905             :         }
    2906             :     }
    2907             :     else
    2908             :     {
    2909          79 :         if (psOptions->bUpsert &&
    2910           4 :             poDriver->GetMetadataItem(GDAL_DCAP_UPSERT) == nullptr)
    2911             :         {
    2912           2 :             CPLError(CE_Failure, CPLE_NotSupported,
    2913             :                      "%s driver does not support upsert",
    2914           2 :                      poODS->GetDriver()->GetDescription());
    2915           2 :             return nullptr;
    2916             :         }
    2917             :     }
    2918             : 
    2919             :     // Automatically close poODS on error, if it has been created by this
    2920             :     // method.
    2921        1978 :     GDALDatasetUniquePtr poODSUniquePtr(hDstDS == nullptr ? poODS : nullptr);
    2922             : 
    2923             :     // Some syntactic sugar to make "ogr2ogr [-f PostgreSQL] PG:dbname=....
    2924             :     // source [srclayer] -lco OVERWRITE=YES" work like "ogr2ogr -overwrite
    2925             :     // PG:dbname=.... source [srclayer]" The former syntax used to work at
    2926             :     // GDAL 1.1.8 time when it was documented in the PG driver, but was broken
    2927             :     // starting with GDAL 1.3.2
    2928             :     // (https://github.com/OSGeo/gdal/commit/29c108a6c9f651dfebae6d1313ba0e707a77c1aa)
    2929             :     // This could probably be generalized to other drivers that support the
    2930             :     // OVERWRITE layer creation option, but we'd need to make sure that they
    2931             :     // just do a DeleteLayer() call. The CARTO driver is an exception regarding
    2932             :     // that.
    2933        1007 :     if (EQUAL(poODS->GetDriver()->GetDescription(), "PostgreSQL") &&
    2934          18 :         CPLTestBool(psOptions->aosLCO.FetchNameValueDef("OVERWRITE", "NO")))
    2935             :     {
    2936           0 :         if (bAppend)
    2937             :         {
    2938           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2939             :                      "-append and -lco OVERWRITE=YES are mutually exclusive");
    2940           0 :             return nullptr;
    2941             :         }
    2942           0 :         bOverwrite = true;
    2943             :     }
    2944             : 
    2945             :     /* -------------------------------------------------------------------- */
    2946             :     /*      For random reading                                              */
    2947             :     /* -------------------------------------------------------------------- */
    2948             :     const bool bRandomLayerReading =
    2949         989 :         CPL_TO_BOOL(poDS->TestCapability(ODsCRandomLayerRead));
    2950          17 :     if (bRandomLayerReading && !poODS->TestCapability(ODsCRandomLayerWrite) &&
    2951        1006 :         psOptions->aosLayers.size() != 1 && psOptions->osSQLStatement.empty() &&
    2952           0 :         !psOptions->bQuiet)
    2953             :     {
    2954           0 :         CPLError(CE_Warning, CPLE_AppDefined,
    2955             :                  "Input datasource uses random layer reading, but "
    2956             :                  "output datasource does not support random layer writing");
    2957             :     }
    2958             : 
    2959         989 :     if (psOptions->nLayerTransaction < 0)
    2960             :     {
    2961         988 :         if (bRandomLayerReading)
    2962          17 :             psOptions->nLayerTransaction = FALSE;
    2963             :         else
    2964         971 :             psOptions->nLayerTransaction =
    2965         971 :                 !poODS->TestCapability(ODsCTransactions);
    2966             :     }
    2967           1 :     else if (psOptions->nLayerTransaction && bRandomLayerReading)
    2968             :     {
    2969           0 :         psOptions->nLayerTransaction = false;
    2970             :     }
    2971             : 
    2972             :     /* -------------------------------------------------------------------- */
    2973             :     /*      Parse the output SRS definition if possible.                    */
    2974             :     /* -------------------------------------------------------------------- */
    2975        1978 :     OGRSpatialReferenceRefCountedPtr poOutputSRS;
    2976         989 :     if (!psOptions->osOutputSRSDef.empty())
    2977             :     {
    2978         144 :         poOutputSRS = OGRSpatialReferenceRefCountedPtr::makeInstance();
    2979         144 :         poOutputSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2980         144 :         if (poOutputSRS->SetFromUserInput(psOptions->osOutputSRSDef.c_str()) !=
    2981             :             OGRERR_NONE)
    2982             :         {
    2983           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2984             :                      "Failed to process SRS definition: %s",
    2985           0 :                      psOptions->osOutputSRSDef.c_str());
    2986           0 :             return nullptr;
    2987             :         }
    2988         144 :         poOutputSRS->SetCoordinateEpoch(psOptions->dfOutputCoordinateEpoch);
    2989             :     }
    2990             : 
    2991             :     /* -------------------------------------------------------------------- */
    2992             :     /*      Parse the source SRS definition if possible.                    */
    2993             :     /* -------------------------------------------------------------------- */
    2994        1978 :     OGRSpatialReference oSourceSRS;
    2995         989 :     OGRSpatialReference *poSourceSRS = nullptr;
    2996         989 :     if (!psOptions->osSourceSRSDef.empty())
    2997             :     {
    2998           8 :         oSourceSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2999           8 :         if (oSourceSRS.SetFromUserInput(psOptions->osSourceSRSDef.c_str()) !=
    3000             :             OGRERR_NONE)
    3001             :         {
    3002           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3003             :                      "Failed to process SRS definition: %s",
    3004           0 :                      psOptions->osSourceSRSDef.c_str());
    3005           0 :             return nullptr;
    3006             :         }
    3007           8 :         oSourceSRS.SetCoordinateEpoch(psOptions->dfSourceCoordinateEpoch);
    3008           8 :         poSourceSRS = &oSourceSRS;
    3009             :     }
    3010             : 
    3011             :     /* -------------------------------------------------------------------- */
    3012             :     /*      Create a transformation object from the source to               */
    3013             :     /*      destination coordinate system.                                  */
    3014             :     /* -------------------------------------------------------------------- */
    3015         989 :     std::unique_ptr<GCPCoordTransformation> poGCPCoordTrans;
    3016         989 :     if (!psOptions->asGCPs.empty())
    3017             :     {
    3018           7 :         poGCPCoordTrans = std::make_unique<GCPCoordTransformation>(
    3019           7 :             static_cast<int>(psOptions->asGCPs.size()),
    3020           7 :             gdal::GCP::c_ptr(psOptions->asGCPs), psOptions->nTransformOrder,
    3021          14 :             poSourceSRS ? poSourceSRS : poOutputSRS.get());
    3022           7 :         if (!(poGCPCoordTrans->IsValid()))
    3023             :         {
    3024           1 :             return nullptr;
    3025             :         }
    3026             :     }
    3027             : 
    3028             :     /* -------------------------------------------------------------------- */
    3029             :     /*      Create layer setup and transformer objects.                     */
    3030             :     /* -------------------------------------------------------------------- */
    3031        1976 :     SetupTargetLayer oSetup;
    3032         988 :     oSetup.m_poSrcDS = poDS;
    3033         988 :     oSetup.m_poDstDS = poODS;
    3034         988 :     oSetup.m_papszLCO = psOptions->aosLCO.List();
    3035         988 :     oSetup.m_poOutputSRS = poOutputSRS.get();
    3036         988 :     oSetup.m_bTransform = psOptions->bTransform;
    3037         988 :     oSetup.m_bNullifyOutputSRS = psOptions->bNullifyOutputSRS;
    3038         988 :     oSetup.m_poUserSourceSRS = poSourceSRS;
    3039         988 :     oSetup.m_bSelFieldsSet = psOptions->bSelFieldsSet;
    3040         988 :     oSetup.m_papszSelFields = psOptions->aosSelFields.List();
    3041         988 :     oSetup.m_bAppend = bAppend;
    3042         988 :     oSetup.m_bAddMissingFields = psOptions->bAddMissingFields;
    3043         988 :     oSetup.m_eGType = psOptions->eGType;
    3044         988 :     oSetup.m_eGeomTypeConversion = psOptions->eGeomTypeConversion;
    3045         988 :     oSetup.m_nCoordDim = psOptions->nCoordDim;
    3046         988 :     oSetup.m_bOverwrite = bOverwrite;
    3047         988 :     oSetup.m_papszFieldTypesToString = psOptions->aosFieldTypesToString.List();
    3048         988 :     oSetup.m_papszMapFieldType = psOptions->aosMapFieldType.List();
    3049         988 :     oSetup.m_bUnsetFieldWidth = psOptions->bUnsetFieldWidth;
    3050         988 :     oSetup.m_bExplodeCollections = psOptions->bExplodeCollections;
    3051         988 :     oSetup.m_pszZField =
    3052         988 :         psOptions->osZField.empty() ? nullptr : psOptions->osZField.c_str();
    3053         988 :     oSetup.m_papszFieldMap = psOptions->aosFieldMap.List();
    3054         988 :     oSetup.m_pszWHERE =
    3055         988 :         psOptions->osWHERE.empty() ? nullptr : psOptions->osWHERE.c_str();
    3056         988 :     oSetup.m_bExactFieldNameMatch = psOptions->bExactFieldNameMatch;
    3057         988 :     oSetup.m_bQuiet = psOptions->bQuiet;
    3058         988 :     oSetup.m_bForceNullable = psOptions->bForceNullable;
    3059         988 :     oSetup.m_bResolveDomains = psOptions->bResolveDomains;
    3060         988 :     oSetup.m_bUnsetDefault = psOptions->bUnsetDefault;
    3061         988 :     oSetup.m_bUnsetFid = psOptions->bUnsetFid;
    3062         988 :     oSetup.m_bPreserveFID = psOptions->bPreserveFID;
    3063         988 :     oSetup.m_bCopyMD = psOptions->bCopyMD;
    3064         988 :     oSetup.m_bNativeData = psOptions->bNativeData;
    3065         988 :     oSetup.m_bNewDataSource = bNewDataSource;
    3066         988 :     oSetup.m_pszCTPipeline = psOptions->osCTPipeline.empty()
    3067         988 :                                  ? nullptr
    3068           4 :                                  : psOptions->osCTPipeline.c_str();
    3069         988 :     oSetup.m_aosCTOptions = psOptions->aosCTOptions;
    3070             : 
    3071        1976 :     LayerTranslator oTranslator;
    3072         988 :     oTranslator.m_poSrcDS = poDS;
    3073         988 :     oTranslator.m_poODS = poODS;
    3074         988 :     oTranslator.m_bTransform = psOptions->bTransform;
    3075         988 :     oTranslator.m_bWrapDateline = psOptions->bWrapDateline;
    3076             :     oTranslator.m_osDateLineOffset =
    3077         988 :         CPLOPrintf("%g", psOptions->dfDateLineOffset);
    3078         988 :     oTranslator.m_poOutputSRS = poOutputSRS.get();
    3079         988 :     oTranslator.m_bNullifyOutputSRS = psOptions->bNullifyOutputSRS;
    3080         988 :     oTranslator.m_poUserSourceSRS = poSourceSRS;
    3081         988 :     oTranslator.m_poGCPCoordTrans = poGCPCoordTrans.get();
    3082         988 :     oTranslator.m_eGType = psOptions->eGType;
    3083         988 :     oTranslator.m_eGeomTypeConversion = psOptions->eGeomTypeConversion;
    3084         988 :     oTranslator.m_bMakeValid = psOptions->bMakeValid;
    3085         988 :     oTranslator.m_bSkipInvalidGeom = psOptions->bSkipInvalidGeom;
    3086         988 :     oTranslator.m_nCoordDim = psOptions->nCoordDim;
    3087         988 :     oTranslator.m_eGeomOp = psOptions->eGeomOp;
    3088         988 :     oTranslator.m_dfGeomOpParam = psOptions->dfGeomOpParam;
    3089             :     // Do not emit warning if the user specified directly the clip source geom
    3090         988 :     if (psOptions->osClipSrcDS.empty())
    3091         980 :         oTranslator.m_bWarnedClipSrcSRS = true;
    3092         988 :     oTranslator.m_poClipSrcOri = psOptions->poClipSrc.get();
    3093             :     // Do not emit warning if the user specified directly the clip dest geom
    3094         988 :     if (psOptions->osClipDstDS.empty())
    3095         981 :         oTranslator.m_bWarnedClipDstSRS = true;
    3096         988 :     oTranslator.m_poClipDstOri = psOptions->poClipDst.get();
    3097         988 :     oTranslator.m_bExplodeCollections = psOptions->bExplodeCollections;
    3098         988 :     oTranslator.m_bNativeData = psOptions->bNativeData;
    3099         988 :     oTranslator.m_nLimit = psOptions->nLimit;
    3100             : 
    3101         988 :     if (psOptions->nGroupTransactions)
    3102             :     {
    3103         987 :         if (!psOptions->nLayerTransaction)
    3104         174 :             poODS->StartTransaction(psOptions->bForceTransaction);
    3105             :     }
    3106             : 
    3107         988 :     GIntBig nTotalEventsDone = 0;
    3108             : 
    3109             :     /* -------------------------------------------------------------------- */
    3110             :     /*      Special case for -sql clause.  No source layers required.       */
    3111             :     /* -------------------------------------------------------------------- */
    3112         988 :     int nRetCode = 0;
    3113             : 
    3114         988 :     if (!psOptions->osSQLStatement.empty())
    3115             :     {
    3116             :         /* Special case: if output=input, then we must likely destroy the */
    3117             :         /* old table before to avoid transaction issues. */
    3118          17 :         if (poDS == poODS && !psOptions->osNewLayerName.empty() && bOverwrite)
    3119           0 :             GetLayerAndOverwriteIfNecessary(
    3120           0 :                 poODS, psOptions->osNewLayerName.c_str(), bOverwrite, nullptr,
    3121             :                 nullptr, nullptr);
    3122             : 
    3123          17 :         if (!psOptions->osWHERE.empty())
    3124           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    3125             :                      "-where clause ignored in combination with -sql.");
    3126          17 :         if (psOptions->aosLayers.size() > 0)
    3127           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    3128             :                      "layer names ignored in combination with -sql.");
    3129             : 
    3130          34 :         OGRLayer *poResultSet = poDS->ExecuteSQL(
    3131          17 :             psOptions->osSQLStatement.c_str(),
    3132          17 :             (!psOptions->bGeomFieldSet) ? psOptions->poSpatialFilter.get()
    3133             :                                         : nullptr,
    3134          17 :             psOptions->osDialect.empty() ? nullptr
    3135          23 :                                          : psOptions->osDialect.c_str());
    3136             : 
    3137          17 :         if (poResultSet != nullptr)
    3138             :         {
    3139          17 :             if (psOptions->poSpatialFilter && psOptions->bGeomFieldSet)
    3140             :             {
    3141           0 :                 int iGeomField = poResultSet->GetLayerDefn()->GetGeomFieldIndex(
    3142           0 :                     psOptions->osGeomField.c_str());
    3143           0 :                 if (iGeomField >= 0)
    3144           0 :                     poResultSet->SetSpatialFilter(
    3145           0 :                         iGeomField, psOptions->poSpatialFilter.get());
    3146             :                 else
    3147           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    3148             :                              "Cannot find geometry field %s.",
    3149           0 :                              psOptions->osGeomField.c_str());
    3150             :             }
    3151             : 
    3152          17 :             GIntBig nCountLayerFeatures = 0;
    3153          17 :             GDALProgressFunc pfnProgress = nullptr;
    3154          17 :             void *pProgressArg = nullptr;
    3155          17 :             if (psOptions->bDisplayProgress)
    3156             :             {
    3157           1 :                 if (bRandomLayerReading)
    3158             :                 {
    3159           1 :                     pfnProgress = psOptions->pfnProgress;
    3160           1 :                     pProgressArg = psOptions->pProgressData;
    3161             :                 }
    3162           0 :                 else if (!poResultSet->TestCapability(OLCFastFeatureCount))
    3163             :                 {
    3164           0 :                     if (!psOptions->bInvokedFromGdalAlgorithm)
    3165             :                     {
    3166           0 :                         CPLError(
    3167             :                             CE_Warning, CPLE_AppDefined,
    3168             :                             "Progress turned off as fast feature count is not "
    3169             :                             "available.");
    3170             :                     }
    3171           0 :                     psOptions->bDisplayProgress = false;
    3172             :                 }
    3173             :                 else
    3174             :                 {
    3175           0 :                     nCountLayerFeatures = poResultSet->GetFeatureCount();
    3176           0 :                     pfnProgress = psOptions->pfnProgress;
    3177           0 :                     pProgressArg = psOptions->pProgressData;
    3178             :                 }
    3179             :             }
    3180             : 
    3181          17 :             std::unique_ptr<OGRLayer> poLayerToFree;
    3182          17 :             OGRLayer *poPassedLayer = poResultSet;
    3183          17 :             if (psOptions->bSplitListFields)
    3184             :             {
    3185             :                 auto poLayer = std::make_unique<OGRSplitListFieldLayer>(
    3186           0 :                     poPassedLayer, psOptions->nMaxSplitListSubFields);
    3187           0 :                 int nRet = poLayer->BuildLayerDefn(nullptr, nullptr);
    3188           0 :                 if (nRet)
    3189             :                 {
    3190           0 :                     poLayerToFree = std::move(poLayer);
    3191           0 :                     poPassedLayer = poLayerToFree.get();
    3192             :                 }
    3193             :             }
    3194             : 
    3195             :             /* --------------------------------------------------------------------
    3196             :              */
    3197             :             /*      Special case to improve user experience when translating
    3198             :              * into   */
    3199             :             /*      single file shapefile and source has only one layer, and
    3200             :              * that   */
    3201             :             /*      the layer name isn't specified */
    3202             :             /* --------------------------------------------------------------------
    3203             :              */
    3204             :             VSIStatBufL sStat;
    3205           3 :             if (EQUAL(poDriver->GetDescription(), "ESRI Shapefile") &&
    3206           3 :                 psOptions->osNewLayerName.empty() &&
    3207           3 :                 VSIStatL(osDestFilename, &sStat) == 0 &&
    3208          20 :                 VSI_ISREG(sStat.st_mode) &&
    3209          17 :                 (EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "shp") ||
    3210          17 :                  EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "shz") ||
    3211          17 :                  EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "dbf")))
    3212             :             {
    3213           0 :                 psOptions->osNewLayerName = CPLGetBasenameSafe(osDestFilename);
    3214             :             }
    3215             : 
    3216             :             auto psInfo = oSetup.Setup(poPassedLayer,
    3217          17 :                                        psOptions->osNewLayerName.empty()
    3218             :                                            ? nullptr
    3219           3 :                                            : psOptions->osNewLayerName.c_str(),
    3220          54 :                                        psOptions.get(), nTotalEventsDone);
    3221             : 
    3222          17 :             poPassedLayer->ResetReading();
    3223             : 
    3224          32 :             if (psInfo == nullptr ||
    3225          32 :                 !oTranslator.Translate(nullptr, psInfo.get(),
    3226             :                                        nCountLayerFeatures, nullptr,
    3227             :                                        nTotalEventsDone, pfnProgress,
    3228          15 :                                        pProgressArg, psOptions.get()))
    3229             :             {
    3230           2 :                 CPLError(CE_Failure, CPLE_AppDefined,
    3231             :                          "Terminating translation prematurely after failed\n"
    3232             :                          "translation from sql statement.");
    3233             : 
    3234           2 :                 nRetCode = 1;
    3235             :             }
    3236             :             else
    3237             :             {
    3238          15 :                 psInfo->CheckSameCoordinateOperation();
    3239             :             }
    3240             : 
    3241          17 :             poDS->ReleaseResultSet(poResultSet);
    3242             :         }
    3243             :         else
    3244             :         {
    3245           0 :             if (CPLGetLastErrorNo() != 0)
    3246           0 :                 nRetCode = 1;
    3247             :         }
    3248             :     }
    3249             : 
    3250             :     /* -------------------------------------------------------------------- */
    3251             :     /*      Special case for layer interleaving mode.                       */
    3252             :     /* -------------------------------------------------------------------- */
    3253         971 :     else if (bRandomLayerReading)
    3254             :     {
    3255          16 :         if (psOptions->bSplitListFields)
    3256             :         {
    3257           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3258             :                      "-splitlistfields not supported in this mode");
    3259           0 :             return nullptr;
    3260             :         }
    3261             : 
    3262             :         // Make sure to probe all layers in case some are by default invisible
    3263          28 :         for (const char *pszLayer : psOptions->aosLayers)
    3264             :         {
    3265          12 :             OGRLayer *poLayer = poDS->GetLayerByName(pszLayer);
    3266             : 
    3267          12 :             if (poLayer == nullptr)
    3268             :             {
    3269           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    3270             :                          "Couldn't fetch requested layer %s!", pszLayer);
    3271           0 :                 return nullptr;
    3272             :             }
    3273             :         }
    3274             : 
    3275          16 :         const int nSrcLayerCount = poDS->GetLayerCount();
    3276          16 :         std::vector<AssociatedLayers> pasAssocLayers(nSrcLayerCount);
    3277             : 
    3278             :         /* --------------------------------------------------------------------
    3279             :          */
    3280             :         /*      Special case to improve user experience when translating into */
    3281             :         /*      single file shapefile and source has only one layer, and that */
    3282             :         /*      the layer name isn't specified */
    3283             :         /* --------------------------------------------------------------------
    3284             :          */
    3285             :         VSIStatBufL sStat;
    3286          53 :         if (EQUAL(poDriver->GetDescription(), "ESRI Shapefile") &&
    3287           5 :             (psOptions->aosLayers.size() == 1 || nSrcLayerCount == 1) &&
    3288           0 :             psOptions->osNewLayerName.empty() &&
    3289          21 :             VSIStatL(osDestFilename, &sStat) == 0 && VSI_ISREG(sStat.st_mode) &&
    3290          16 :             (EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "shp") ||
    3291          16 :              EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "shz") ||
    3292          16 :              EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "dbf")))
    3293             :         {
    3294           0 :             psOptions->osNewLayerName = CPLGetBasenameSafe(osDestFilename);
    3295             :         }
    3296             : 
    3297          16 :         GDALProgressFunc pfnProgress = nullptr;
    3298          16 :         void *pProgressArg = nullptr;
    3299          16 :         if (!psOptions->bQuiet)
    3300             :         {
    3301          16 :             pfnProgress = psOptions->pfnProgress;
    3302          16 :             pProgressArg = psOptions->pProgressData;
    3303             :         }
    3304             : 
    3305             :         /* --------------------------------------------------------------------
    3306             :          */
    3307             :         /*      If no target layer specified, use all source layers. */
    3308             :         /* --------------------------------------------------------------------
    3309             :          */
    3310          16 :         if (psOptions->aosLayers.empty())
    3311             :         {
    3312         148 :             for (int iLayer = 0; iLayer < nSrcLayerCount; iLayer++)
    3313             :             {
    3314         135 :                 OGRLayer *poLayer = poDS->GetLayer(iLayer);
    3315             : 
    3316         135 :                 if (poLayer == nullptr)
    3317             :                 {
    3318           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    3319             :                              "Couldn't fetch advertised layer %d!", iLayer);
    3320           0 :                     return nullptr;
    3321             :                 }
    3322             : 
    3323         135 :                 psOptions->aosLayers.AddString(poLayer->GetName());
    3324             :             }
    3325             :         }
    3326             :         else
    3327             :         {
    3328           3 :             const bool bSrcIsOSM = (strcmp(poDS->GetDriverName(), "OSM") == 0);
    3329           3 :             if (bSrcIsOSM)
    3330             :             {
    3331           6 :                 CPLString osInterestLayers = "SET interest_layers =";
    3332          15 :                 for (int iLayer = 0; iLayer < psOptions->aosLayers.size();
    3333             :                      iLayer++)
    3334             :                 {
    3335          12 :                     if (iLayer != 0)
    3336           9 :                         osInterestLayers += ",";
    3337          12 :                     osInterestLayers += psOptions->aosLayers[iLayer];
    3338             :                 }
    3339             : 
    3340           3 :                 poDS->ExecuteSQL(osInterestLayers.c_str(), nullptr, nullptr);
    3341             :             }
    3342             :         }
    3343             : 
    3344             :         /* --------------------------------------------------------------------
    3345             :          */
    3346             :         /*      First pass to set filters. */
    3347             :         /* --------------------------------------------------------------------
    3348             :          */
    3349          16 :         std::map<OGRLayer *, int> oMapLayerToIdx;
    3350             : 
    3351         166 :         for (int iLayer = 0; iLayer < nSrcLayerCount; iLayer++)
    3352             :         {
    3353         150 :             OGRLayer *poLayer = poDS->GetLayer(iLayer);
    3354         150 :             if (poLayer == nullptr)
    3355             :             {
    3356           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    3357             :                          "Couldn't fetch advertised layer %d!", iLayer);
    3358           0 :                 return nullptr;
    3359             :             }
    3360             : 
    3361         150 :             pasAssocLayers[iLayer].poSrcLayer = poLayer;
    3362             : 
    3363         150 :             if (psOptions->aosLayers.FindString(poLayer->GetName()) >= 0)
    3364             :             {
    3365         147 :                 if (!psOptions->osWHERE.empty())
    3366             :                 {
    3367           0 :                     if (poLayer->SetAttributeFilter(
    3368           0 :                             psOptions->osWHERE.c_str()) != OGRERR_NONE)
    3369             :                     {
    3370           0 :                         CPLError(CE_Failure, CPLE_AppDefined,
    3371             :                                  "SetAttributeFilter(%s) on layer '%s' failed.",
    3372           0 :                                  psOptions->osWHERE.c_str(),
    3373           0 :                                  poLayer->GetName());
    3374           0 :                         if (!psOptions->bSkipFailures)
    3375             :                         {
    3376           0 :                             return nullptr;
    3377             :                         }
    3378             :                     }
    3379             :                 }
    3380             : 
    3381         294 :                 ApplySpatialFilter(
    3382         147 :                     poLayer, psOptions->poSpatialFilter.get(), poSpatSRS.get(),
    3383         147 :                     psOptions->bGeomFieldSet ? psOptions->osGeomField.c_str()
    3384             :                                              : nullptr,
    3385             :                     poSourceSRS);
    3386             : 
    3387         147 :                 oMapLayerToIdx[poLayer] = iLayer;
    3388             :             }
    3389             :         }
    3390             : 
    3391             :         /* --------------------------------------------------------------------
    3392             :          */
    3393             :         /*      Second pass to process features in a interleaved layer mode. */
    3394             :         /* --------------------------------------------------------------------
    3395             :          */
    3396          16 :         bool bTargetLayersHaveBeenCreated = false;
    3397             :         while (true)
    3398             :         {
    3399         991 :             OGRLayer *poFeatureLayer = nullptr;
    3400             :             auto poFeature = std::unique_ptr<OGRFeature>(poDS->GetNextFeature(
    3401         991 :                 &poFeatureLayer, nullptr, pfnProgress, pProgressArg));
    3402         991 :             if (poFeature == nullptr)
    3403          16 :                 break;
    3404             :             std::map<OGRLayer *, int>::const_iterator oIter =
    3405         975 :                 oMapLayerToIdx.find(poFeatureLayer);
    3406         975 :             if (oIter == oMapLayerToIdx.end())
    3407             :             {
    3408             :                 // Feature in a layer that is not a layer of interest.
    3409             :                 // nothing to do
    3410             :             }
    3411             :             else
    3412             :             {
    3413         975 :                 if (!bTargetLayersHaveBeenCreated)
    3414             :                 {
    3415             :                     // We defer target layer creation at the first feature
    3416             :                     // retrieved since getting the layer definition can be
    3417             :                     // costly (case of the GMLAS driver) and thus we'd better
    3418             :                     // taking advantage from the progress callback of
    3419             :                     // GetNextFeature.
    3420          15 :                     bTargetLayersHaveBeenCreated = true;
    3421         160 :                     for (int iLayer = 0; iLayer < nSrcLayerCount; iLayer++)
    3422             :                     {
    3423         145 :                         OGRLayer *poLayer = poDS->GetLayer(iLayer);
    3424         290 :                         if (psOptions->aosLayers.FindString(
    3425         290 :                                 poLayer->GetName()) < 0)
    3426           3 :                             continue;
    3427             : 
    3428             :                         auto psInfo = oSetup.Setup(
    3429             :                             poLayer,
    3430         142 :                             psOptions->osNewLayerName.empty()
    3431             :                                 ? nullptr
    3432           0 :                                 : psOptions->osNewLayerName.c_str(),
    3433         284 :                             psOptions.get(), nTotalEventsDone);
    3434             : 
    3435         142 :                         if (psInfo == nullptr && !psOptions->bSkipFailures)
    3436             :                         {
    3437           0 :                             return nullptr;
    3438             :                         }
    3439             : 
    3440         142 :                         pasAssocLayers[iLayer].psInfo = std::move(psInfo);
    3441             :                     }
    3442          15 :                     if (nRetCode)
    3443           0 :                         break;
    3444             :                 }
    3445             : 
    3446         975 :                 int iLayer = oIter->second;
    3447         975 :                 TargetLayerInfo *psInfo = pasAssocLayers[iLayer].psInfo.get();
    3448        2923 :                 if ((psInfo == nullptr ||
    3449        1949 :                      !oTranslator.Translate(std::move(poFeature), psInfo, 0,
    3450             :                                             nullptr, nTotalEventsDone, nullptr,
    3451        1950 :                                             nullptr, psOptions.get())) &&
    3452           1 :                     !psOptions->bSkipFailures)
    3453             :                 {
    3454           0 :                     if (psOptions->bInvokedFromGdalAlgorithm)
    3455             :                     {
    3456           0 :                         CPLError(
    3457             :                             CE_Failure, CPLE_AppDefined,
    3458             :                             "Failed to write layer '%s'. Use --skip-errors to "
    3459             :                             "ignore errors and continue writing.",
    3460           0 :                             poFeatureLayer->GetName());
    3461             :                     }
    3462             :                     else
    3463             :                     {
    3464           0 :                         CPLError(
    3465             :                             CE_Failure, CPLE_AppDefined,
    3466             :                             "Terminating translation prematurely after failed\n"
    3467             :                             "translation of layer %s (use -skipfailures to "
    3468             :                             "skip "
    3469             :                             "errors)",
    3470           0 :                             poFeatureLayer->GetName());
    3471             :                     }
    3472             : 
    3473           0 :                     nRetCode = 1;
    3474           0 :                     break;
    3475             :                 }
    3476             :             }
    3477         975 :         }  // while true
    3478             : 
    3479          16 :         if (pfnProgress)
    3480             :         {
    3481           0 :             pfnProgress(1.0, "", pProgressArg);
    3482             :         }
    3483             : 
    3484         166 :         for (int iLayer = 0; iLayer < nSrcLayerCount; iLayer++)
    3485             :         {
    3486         150 :             if (pasAssocLayers[iLayer].psInfo)
    3487         141 :                 pasAssocLayers[iLayer].psInfo->CheckSameCoordinateOperation();
    3488             :         }
    3489             : 
    3490          16 :         if (!bTargetLayersHaveBeenCreated)
    3491             :         {
    3492             :             // bTargetLayersHaveBeenCreated not used after here.
    3493             :             // bTargetLayersHaveBeenCreated = true;
    3494           6 :             for (int iLayer = 0; iLayer < nSrcLayerCount; iLayer++)
    3495             :             {
    3496           5 :                 OGRLayer *poLayer = poDS->GetLayer(iLayer);
    3497           5 :                 if (psOptions->aosLayers.FindString(poLayer->GetName()) < 0)
    3498           0 :                     continue;
    3499             : 
    3500             :                 auto psInfo =
    3501             :                     oSetup.Setup(poLayer,
    3502           5 :                                  psOptions->osNewLayerName.empty()
    3503             :                                      ? nullptr
    3504           0 :                                      : psOptions->osNewLayerName.c_str(),
    3505          10 :                                  psOptions.get(), nTotalEventsDone);
    3506             : 
    3507           5 :                 if (psInfo == nullptr && !psOptions->bSkipFailures)
    3508             :                 {
    3509           0 :                     return nullptr;
    3510             :                 }
    3511             : 
    3512           5 :                 pasAssocLayers[iLayer].psInfo = std::move(psInfo);
    3513             :             }
    3514             :         }
    3515             :     }
    3516             : 
    3517             :     else
    3518             :     {
    3519         955 :         std::vector<OGRLayer *> apoLayers;
    3520             : 
    3521             :         /* --------------------------------------------------------------------
    3522             :          */
    3523             :         /*      Process each data source layer. */
    3524             :         /* --------------------------------------------------------------------
    3525             :          */
    3526         955 :         if (psOptions->aosLayers.empty())
    3527             :         {
    3528         943 :             const int nLayerCount = poDS->GetLayerCount();
    3529             : 
    3530        1954 :             for (int iLayer = 0; iLayer < nLayerCount; iLayer++)
    3531             :             {
    3532        1011 :                 OGRLayer *poLayer = poDS->GetLayer(iLayer);
    3533             : 
    3534        1011 :                 if (poLayer == nullptr)
    3535             :                 {
    3536           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    3537             :                              "Couldn't fetch advertised layer %d!", iLayer);
    3538           0 :                     return nullptr;
    3539             :                 }
    3540        1011 :                 if (!poDS->IsLayerPrivate(iLayer))
    3541             :                 {
    3542        1011 :                     apoLayers.push_back(poLayer);
    3543             :                 }
    3544             :             }
    3545             :         }
    3546             :         /* --------------------------------------------------------------------
    3547             :          */
    3548             :         /*      Process specified data source layers. */
    3549             :         /* --------------------------------------------------------------------
    3550             :          */
    3551             :         else
    3552             :         {
    3553             : 
    3554          33 :             for (int iLayer = 0; psOptions->aosLayers[iLayer] != nullptr;
    3555             :                  iLayer++)
    3556             :             {
    3557             :                 OGRLayer *poLayer =
    3558          21 :                     poDS->GetLayerByName(psOptions->aosLayers[iLayer]);
    3559             : 
    3560          21 :                 if (poLayer == nullptr)
    3561             :                 {
    3562           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    3563             :                              "Couldn't fetch requested layer '%s'!",
    3564           0 :                              psOptions->aosLayers[iLayer]);
    3565           0 :                     if (!psOptions->bSkipFailures)
    3566             :                     {
    3567           0 :                         return nullptr;
    3568             :                     }
    3569             :                 }
    3570             : 
    3571          21 :                 apoLayers.emplace_back(poLayer);
    3572             :             }
    3573             :         }
    3574             : 
    3575             :         /* --------------------------------------------------------------------
    3576             :          */
    3577             :         /*      Special case to improve user experience when translating into */
    3578             :         /*      single file shapefile and source has only one layer, and that */
    3579             :         /*      the layer name isn't specified */
    3580             :         /* --------------------------------------------------------------------
    3581             :          */
    3582             :         VSIStatBufL sStat;
    3583         955 :         const int nLayerCount = static_cast<int>(apoLayers.size());
    3584         147 :         if (EQUAL(poDriver->GetDescription(), "ESRI Shapefile") &&
    3585         143 :             nLayerCount == 1 && psOptions->osNewLayerName.empty() &&
    3586        1113 :             VSIStatL(osDestFilename, &sStat) == 0 && VSI_ISREG(sStat.st_mode) &&
    3587         966 :             (EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "shp") ||
    3588         957 :              EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "shz") ||
    3589         956 :              EQUAL(CPLGetExtensionSafe(osDestFilename).c_str(), "dbf")))
    3590             :         {
    3591          10 :             psOptions->osNewLayerName = CPLGetBasenameSafe(osDestFilename);
    3592             :         }
    3593             : 
    3594         955 :         std::vector<GIntBig> anLayerCountFeatures(nLayerCount);
    3595         955 :         GIntBig nCountLayersFeatures = 0;
    3596         955 :         GIntBig nAccCountFeatures = 0;
    3597             : 
    3598             :         /* First pass to apply filters and count all features if necessary */
    3599        1987 :         for (int iLayer = 0; iLayer < nLayerCount; iLayer++)
    3600             :         {
    3601        1032 :             OGRLayer *poLayer = apoLayers[iLayer];
    3602        1032 :             if (poLayer == nullptr)
    3603           0 :                 continue;
    3604             : 
    3605        1032 :             if (!psOptions->osWHERE.empty())
    3606             :             {
    3607           8 :                 if (poLayer->SetAttributeFilter(psOptions->osWHERE.c_str()) !=
    3608             :                     OGRERR_NONE)
    3609             :                 {
    3610           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    3611             :                              "SetAttributeFilter(%s) on layer '%s' failed.",
    3612           0 :                              psOptions->osWHERE.c_str(), poLayer->GetName());
    3613           0 :                     if (!psOptions->bSkipFailures)
    3614             :                     {
    3615           0 :                         return nullptr;
    3616             :                     }
    3617             :                 }
    3618             :             }
    3619             : 
    3620        2063 :             ApplySpatialFilter(
    3621        1032 :                 poLayer, psOptions->poSpatialFilter.get(), poSpatSRS.get(),
    3622        1032 :                 psOptions->bGeomFieldSet ? psOptions->osGeomField.c_str()
    3623             :                                          : nullptr,
    3624             :                 poSourceSRS);
    3625             : 
    3626        1032 :             if (psOptions->bDisplayProgress)
    3627             :             {
    3628          16 :                 if (!poLayer->TestCapability(OLCFastFeatureCount))
    3629             :                 {
    3630           1 :                     if (!psOptions->bInvokedFromGdalAlgorithm)
    3631             :                     {
    3632           0 :                         CPLError(
    3633             :                             CE_Warning, CPLE_NotSupported,
    3634             :                             "Progress turned off as fast feature count is not "
    3635             :                             "available.");
    3636             :                     }
    3637           1 :                     psOptions->bDisplayProgress = false;
    3638             :                 }
    3639             :                 else
    3640             :                 {
    3641          15 :                     anLayerCountFeatures[iLayer] = poLayer->GetFeatureCount();
    3642          15 :                     if (psOptions->nLimit >= 0)
    3643           0 :                         anLayerCountFeatures[iLayer] = std::min(
    3644           0 :                             anLayerCountFeatures[iLayer], psOptions->nLimit);
    3645          30 :                     if (anLayerCountFeatures[iLayer] >= 0 &&
    3646          15 :                         anLayerCountFeatures[iLayer] <=
    3647          15 :                             std::numeric_limits<GIntBig>::max() -
    3648             :                                 nCountLayersFeatures)
    3649             :                     {
    3650          14 :                         nCountLayersFeatures += anLayerCountFeatures[iLayer];
    3651             :                     }
    3652             :                     else
    3653             :                     {
    3654           1 :                         nCountLayersFeatures = 0;
    3655           1 :                         psOptions->bDisplayProgress = false;
    3656             :                     }
    3657             :                 }
    3658             :             }
    3659             :         }
    3660             : 
    3661             :         /* Second pass to do the real job */
    3662        1987 :         for (int iLayer = 0; iLayer < nLayerCount && nRetCode == 0; iLayer++)
    3663             :         {
    3664        1032 :             OGRLayer *poLayer = apoLayers[iLayer];
    3665        1032 :             if (poLayer == nullptr)
    3666           0 :                 continue;
    3667             : 
    3668        1032 :             std::unique_ptr<OGRLayer> poLayerToFree;
    3669        1032 :             OGRLayer *poPassedLayer = poLayer;
    3670        1032 :             if (psOptions->bSplitListFields)
    3671             :             {
    3672             :                 auto poSLFLayer = std::make_unique<OGRSplitListFieldLayer>(
    3673           2 :                     poPassedLayer, psOptions->nMaxSplitListSubFields);
    3674             : 
    3675           1 :                 GDALProgressFunc pfnProgress = nullptr;
    3676             :                 std::unique_ptr<void, decltype(&GDALDestroyScaledProgress)>
    3677           2 :                     pProgressArg(nullptr, GDALDestroyScaledProgress);
    3678             : 
    3679           1 :                 if (psOptions->bDisplayProgress &&
    3680           1 :                     psOptions->nMaxSplitListSubFields != 1 &&
    3681             :                     nCountLayersFeatures != 0)
    3682             :                 {
    3683           0 :                     pfnProgress = GDALScaledProgress;
    3684           0 :                     pProgressArg.reset(GDALCreateScaledProgress(
    3685             :                         nAccCountFeatures * 1.0 / nCountLayersFeatures,
    3686           0 :                         (nAccCountFeatures + anLayerCountFeatures[iLayer] / 2) *
    3687             :                             1.0 / nCountLayersFeatures,
    3688           0 :                         psOptions->pfnProgress, psOptions->pProgressData));
    3689             :                 }
    3690             : 
    3691             :                 int nRet =
    3692           1 :                     poSLFLayer->BuildLayerDefn(pfnProgress, pProgressArg.get());
    3693           1 :                 if (nRet)
    3694             :                 {
    3695           1 :                     poLayerToFree = std::move(poSLFLayer);
    3696           1 :                     poPassedLayer = poLayerToFree.get();
    3697             :                 }
    3698             :             }
    3699             : 
    3700        1032 :             GDALProgressFunc pfnProgress = nullptr;
    3701             :             std::unique_ptr<void, decltype(&GDALDestroyScaledProgress)>
    3702        2064 :                 pProgressArg(nullptr, GDALDestroyScaledProgress);
    3703             : 
    3704        1032 :             if (psOptions->bDisplayProgress)
    3705             :             {
    3706          13 :                 if (nCountLayersFeatures != 0)
    3707             :                 {
    3708          13 :                     pfnProgress = GDALScaledProgress;
    3709          13 :                     GIntBig nStart = 0;
    3710          13 :                     if (poPassedLayer != poLayer &&
    3711           0 :                         psOptions->nMaxSplitListSubFields != 1)
    3712           0 :                         nStart = anLayerCountFeatures[iLayer] / 2;
    3713          13 :                     pProgressArg.reset(GDALCreateScaledProgress(
    3714          13 :                         (nAccCountFeatures + nStart) * 1.0 /
    3715             :                             nCountLayersFeatures,
    3716          13 :                         (nAccCountFeatures + anLayerCountFeatures[iLayer]) *
    3717             :                             1.0 / nCountLayersFeatures,
    3718          13 :                         psOptions->pfnProgress, psOptions->pProgressData));
    3719             :                 }
    3720             : 
    3721          13 :                 nAccCountFeatures += anLayerCountFeatures[iLayer];
    3722             :             }
    3723             : 
    3724             :             auto psInfo = oSetup.Setup(poPassedLayer,
    3725        1032 :                                        psOptions->osNewLayerName.empty()
    3726             :                                            ? nullptr
    3727          37 :                                            : psOptions->osNewLayerName.c_str(),
    3728        3133 :                                        psOptions.get(), nTotalEventsDone);
    3729             : 
    3730        1032 :             poPassedLayer->ResetReading();
    3731             : 
    3732        4124 :             if ((psInfo == nullptr ||
    3733        3092 :                  !oTranslator.Translate(nullptr, psInfo.get(),
    3734        1030 :                                         anLayerCountFeatures[iLayer], nullptr,
    3735             :                                         nTotalEventsDone, pfnProgress,
    3736        2064 :                                         pProgressArg.get(), psOptions.get())) &&
    3737          17 :                 !psOptions->bSkipFailures)
    3738             :             {
    3739          16 :                 if (psOptions->bInvokedFromGdalAlgorithm)
    3740             :                 {
    3741           9 :                     CPLError(CE_Failure, CPLE_AppDefined,
    3742             :                              "Failed to write layer '%s'. Use --skip-errors to "
    3743             :                              "ignore errors and continue writing.",
    3744           9 :                              poLayer->GetName());
    3745             :                 }
    3746             :                 else
    3747             :                 {
    3748           7 :                     CPLError(
    3749             :                         CE_Failure, CPLE_AppDefined,
    3750             :                         "Terminating translation prematurely after failed\n"
    3751             :                         "translation of layer %s (use -skipfailures to skip "
    3752             :                         "errors)",
    3753           7 :                         poLayer->GetName());
    3754             :                 }
    3755             : 
    3756          16 :                 nRetCode = 1;
    3757             :             }
    3758             : 
    3759        1032 :             if (psInfo)
    3760        1030 :                 psInfo->CheckSameCoordinateOperation();
    3761             :         }
    3762             :     }
    3763             : 
    3764         988 :     CopyRelationships(poODS, poDS);
    3765             : 
    3766             :     /* -------------------------------------------------------------------- */
    3767             :     /*      Process DS style table                                          */
    3768             :     /* -------------------------------------------------------------------- */
    3769             : 
    3770         988 :     poODS->SetStyleTable(poDS->GetStyleTable());
    3771             : 
    3772         988 :     if (psOptions->nGroupTransactions)
    3773             :     {
    3774         987 :         if (!psOptions->nLayerTransaction)
    3775             :         {
    3776         174 :             if (nRetCode != 0 && !psOptions->bSkipFailures)
    3777           8 :                 poODS->RollbackTransaction();
    3778             :             else
    3779             :             {
    3780         166 :                 OGRErr eRet = poODS->CommitTransaction();
    3781         166 :                 if (eRet != OGRERR_NONE && eRet != OGRERR_UNSUPPORTED_OPERATION)
    3782             :                 {
    3783           1 :                     nRetCode = 1;
    3784             :                 }
    3785             :             }
    3786             :         }
    3787             :     }
    3788             : 
    3789             :     // Note: this guarantees that the file can be opened in a consistent state,
    3790             :     // without requiring to close poODS, only if the driver declares
    3791             :     // DCAP_FLUSHCACHE_CONSISTENT_STATE
    3792         988 :     if (poODS->FlushCache() != CE_None)
    3793           0 :         nRetCode = 1;
    3794             : 
    3795         988 :     if (nRetCode == 0)
    3796             :     {
    3797         969 :         if (hDstDS)
    3798          28 :             return hDstDS;
    3799             :         else
    3800         941 :             return GDALDataset::ToHandle(poODSUniquePtr.release());
    3801             :     }
    3802             : 
    3803          19 :     return nullptr;
    3804             : }
    3805             : 
    3806             : /************************************************************************/
    3807             : /*                                SetZ()                                */
    3808             : /************************************************************************/
    3809             : 
    3810             : namespace
    3811             : {
    3812             : class SetZVisitor : public OGRDefaultGeometryVisitor
    3813             : {
    3814             :     double m_dfZ;
    3815             : 
    3816             :   public:
    3817          30 :     explicit SetZVisitor(double dfZ) : m_dfZ(dfZ)
    3818             :     {
    3819          30 :     }
    3820             : 
    3821             :     using OGRDefaultGeometryVisitor::visit;
    3822             : 
    3823         735 :     void visit(OGRPoint *poPoint) override
    3824             :     {
    3825         735 :         poPoint->setZ(m_dfZ);
    3826         735 :     }
    3827             : };
    3828             : }  // namespace
    3829             : 
    3830          30 : static void SetZ(OGRGeometry *poGeom, double dfZ)
    3831             : {
    3832          30 :     if (poGeom == nullptr)
    3833           0 :         return;
    3834          60 :     SetZVisitor visitor(dfZ);
    3835          30 :     poGeom->set3D(true);
    3836          30 :     poGeom->accept(&visitor);
    3837             : }
    3838             : 
    3839             : /************************************************************************/
    3840             : /*                        ForceCoordDimension()                         */
    3841             : /************************************************************************/
    3842             : 
    3843        1328 : static int ForceCoordDimension(int eGType, int nCoordDim)
    3844             : {
    3845        1328 :     if (nCoordDim == 2 && eGType != wkbNone)
    3846           3 :         return wkbFlatten(eGType);
    3847        1325 :     else if (nCoordDim == 3 && eGType != wkbNone)
    3848           3 :         return wkbSetZ(wkbFlatten(eGType));
    3849        1322 :     else if (nCoordDim == COORD_DIM_XYM && eGType != wkbNone)
    3850           2 :         return wkbSetM(wkbFlatten(eGType));
    3851        1320 :     else if (nCoordDim == 4 && eGType != wkbNone)
    3852           2 :         return OGR_GT_SetModifier(static_cast<OGRwkbGeometryType>(eGType), TRUE,
    3853           2 :                                   TRUE);
    3854             :     else
    3855        1318 :         return eGType;
    3856             : }
    3857             : 
    3858             : /************************************************************************/
    3859             : /*                  GetLayerAndOverwriteIfNecessary()                   */
    3860             : /************************************************************************/
    3861             : 
    3862        1196 : static OGRLayer *GetLayerAndOverwriteIfNecessary(GDALDataset *poDstDS,
    3863             :                                                  const char *pszNewLayerName,
    3864             :                                                  bool bOverwrite,
    3865             :                                                  bool *pbErrorOccurred,
    3866             :                                                  bool *pbOverwriteActuallyDone,
    3867             :                                                  bool *pbAddOverwriteLCO)
    3868             : {
    3869        1196 :     if (pbErrorOccurred)
    3870        1196 :         *pbErrorOccurred = false;
    3871        1196 :     if (pbOverwriteActuallyDone)
    3872        1196 :         *pbOverwriteActuallyDone = false;
    3873        1196 :     if (pbAddOverwriteLCO)
    3874        1196 :         *pbAddOverwriteLCO = false;
    3875             : 
    3876             :     /* GetLayerByName() can instantiate layers that would have been */
    3877             :     /* 'hidden' otherwise, for example, non-spatial tables in a */
    3878             :     /* PostGIS-enabled database, so this apparently useless command is */
    3879             :     /* not useless. (#4012) */
    3880        1196 :     CPLPushErrorHandler(CPLQuietErrorHandler);
    3881        1196 :     OGRLayer *poDstLayer = poDstDS->GetLayerByName(pszNewLayerName);
    3882        1196 :     CPLPopErrorHandler();
    3883        1196 :     CPLErrorReset();
    3884             : 
    3885        1196 :     int iLayer = -1;
    3886        1196 :     if (poDstLayer != nullptr)
    3887             :     {
    3888          65 :         const int nLayerCount = poDstDS->GetLayerCount();
    3889         379 :         for (iLayer = 0; iLayer < nLayerCount; iLayer++)
    3890             :         {
    3891         379 :             OGRLayer *poLayer = poDstDS->GetLayer(iLayer);
    3892         379 :             if (poLayer == poDstLayer)
    3893          65 :                 break;
    3894             :         }
    3895             : 
    3896          65 :         if (iLayer == nLayerCount)
    3897             :             /* should not happen with an ideal driver */
    3898           0 :             poDstLayer = nullptr;
    3899             :     }
    3900             : 
    3901             :     /* -------------------------------------------------------------------- */
    3902             :     /*      If the user requested overwrite, and we have the layer in       */
    3903             :     /*      question we need to delete it now so it will get recreated      */
    3904             :     /*      (overwritten).                                                  */
    3905             :     /* -------------------------------------------------------------------- */
    3906        1196 :     if (poDstLayer != nullptr && bOverwrite)
    3907             :     {
    3908             :         /* When using the CARTO driver we don't want to delete the layer if */
    3909             :         /* it's going to be recreated. Instead we mark it to be overwritten */
    3910             :         /* when the new creation is requested */
    3911          16 :         if (poDstDS->GetDriver()->GetMetadataItem(
    3912          32 :                 GDAL_DS_LAYER_CREATIONOPTIONLIST) != nullptr &&
    3913          32 :             strstr(poDstDS->GetDriver()->GetMetadataItem(
    3914          16 :                        GDAL_DS_LAYER_CREATIONOPTIONLIST),
    3915             :                    "CARTODBFY") != nullptr)
    3916             :         {
    3917           0 :             if (pbAddOverwriteLCO)
    3918           0 :                 *pbAddOverwriteLCO = true;
    3919           0 :             if (pbOverwriteActuallyDone)
    3920           0 :                 *pbOverwriteActuallyDone = true;
    3921             :         }
    3922          16 :         else if (poDstDS->DeleteLayer(iLayer) != OGRERR_NONE)
    3923             :         {
    3924           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3925             :                      "DeleteLayer() failed when overwrite requested.");
    3926           0 :             if (pbErrorOccurred)
    3927           0 :                 *pbErrorOccurred = true;
    3928             :         }
    3929             :         else
    3930             :         {
    3931          16 :             if (pbOverwriteActuallyDone)
    3932          16 :                 *pbOverwriteActuallyDone = true;
    3933             :         }
    3934          16 :         poDstLayer = nullptr;
    3935             :     }
    3936             : 
    3937        1196 :     return poDstLayer;
    3938             : }
    3939             : 
    3940             : /************************************************************************/
    3941             : /*                            ConvertType()                             */
    3942             : /************************************************************************/
    3943             : 
    3944        1319 : static OGRwkbGeometryType ConvertType(GeomTypeConversion eGeomTypeConversion,
    3945             :                                       OGRwkbGeometryType eGType)
    3946             : {
    3947        1319 :     OGRwkbGeometryType eRetType = eGType;
    3948             : 
    3949        1319 :     if (eGeomTypeConversion == GTC_CONVERT_TO_LINEAR ||
    3950             :         eGeomTypeConversion == GTC_PROMOTE_TO_MULTI_AND_CONVERT_TO_LINEAR)
    3951             :     {
    3952          13 :         eRetType = OGR_GT_GetLinear(eRetType);
    3953             :     }
    3954             : 
    3955        1319 :     if (eGeomTypeConversion == GTC_PROMOTE_TO_MULTI ||
    3956             :         eGeomTypeConversion == GTC_PROMOTE_TO_MULTI_AND_CONVERT_TO_LINEAR)
    3957             :     {
    3958          12 :         if (eRetType == wkbTriangle || eRetType == wkbTIN ||
    3959             :             eRetType == wkbPolyhedralSurface)
    3960             :         {
    3961           0 :             eRetType = wkbMultiPolygon;
    3962             :         }
    3963          12 :         else if (!OGR_GT_IsSubClassOf(eRetType, wkbGeometryCollection))
    3964             :         {
    3965           8 :             eRetType = OGR_GT_GetCollection(eRetType);
    3966             :         }
    3967             :     }
    3968             : 
    3969        1319 :     if (eGeomTypeConversion == GTC_CONVERT_TO_CURVE)
    3970           2 :         eRetType = OGR_GT_GetCurve(eRetType);
    3971             : 
    3972        1319 :     return eRetType;
    3973             : }
    3974             : 
    3975             : /************************************************************************/
    3976             : /*                       DoFieldTypeConversion()                        */
    3977             : /************************************************************************/
    3978             : 
    3979        3758 : static void DoFieldTypeConversion(GDALDataset *poDstDS,
    3980             :                                   OGRFieldDefn &oFieldDefn,
    3981             :                                   CSLConstList papszFieldTypesToString,
    3982             :                                   CSLConstList papszMapFieldType,
    3983             :                                   bool bUnsetFieldWidth, bool bQuiet,
    3984             :                                   bool bForceNullable, bool bUnsetDefault)
    3985             : {
    3986        3758 :     if (papszFieldTypesToString != nullptr)
    3987             :     {
    3988           0 :         CPLString osLookupString;
    3989             :         osLookupString.Printf(
    3990             :             "%s(%s)", OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()),
    3991           0 :             OGRFieldDefn::GetFieldSubTypeName(oFieldDefn.GetSubType()));
    3992             : 
    3993           0 :         int iIdx = CSLFindString(papszFieldTypesToString, osLookupString);
    3994           0 :         if (iIdx < 0)
    3995           0 :             iIdx = CSLFindString(
    3996             :                 papszFieldTypesToString,
    3997             :                 OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()));
    3998           0 :         if (iIdx < 0)
    3999           0 :             iIdx = CSLFindString(papszFieldTypesToString, "All");
    4000           0 :         if (iIdx >= 0)
    4001             :         {
    4002           0 :             oFieldDefn.SetSubType(OFSTNone);
    4003           0 :             oFieldDefn.SetType(OFTString);
    4004             :         }
    4005             :     }
    4006        3758 :     else if (papszMapFieldType != nullptr)
    4007             :     {
    4008          28 :         CPLString osLookupString;
    4009             :         osLookupString.Printf(
    4010             :             "%s(%s)", OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()),
    4011          14 :             OGRFieldDefn::GetFieldSubTypeName(oFieldDefn.GetSubType()));
    4012             : 
    4013             :         const char *pszType =
    4014          14 :             CSLFetchNameValue(papszMapFieldType, osLookupString);
    4015          14 :         if (pszType == nullptr)
    4016          13 :             pszType = CSLFetchNameValue(
    4017             :                 papszMapFieldType,
    4018             :                 OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()));
    4019          14 :         if (pszType == nullptr)
    4020          10 :             pszType = CSLFetchNameValue(papszMapFieldType, "All");
    4021          14 :         if (pszType != nullptr)
    4022             :         {
    4023             :             int iSubType;
    4024           4 :             int iType = GetFieldType(pszType, &iSubType);
    4025           4 :             if (iType >= 0 && iSubType >= 0)
    4026             :             {
    4027           4 :                 oFieldDefn.SetSubType(OFSTNone);
    4028           4 :                 oFieldDefn.SetType(static_cast<OGRFieldType>(iType));
    4029           4 :                 oFieldDefn.SetSubType(static_cast<OGRFieldSubType>(iSubType));
    4030           4 :                 if (iType == OFTInteger)
    4031           1 :                     oFieldDefn.SetWidth(0);
    4032             :             }
    4033             :         }
    4034             :     }
    4035        3758 :     if (bUnsetFieldWidth)
    4036             :     {
    4037           6 :         oFieldDefn.SetWidth(0);
    4038           6 :         oFieldDefn.SetPrecision(0);
    4039             :     }
    4040        3758 :     if (bForceNullable)
    4041           4 :         oFieldDefn.SetNullable(TRUE);
    4042        3758 :     if (bUnsetDefault)
    4043           2 :         oFieldDefn.SetDefault(nullptr);
    4044             : 
    4045        3758 :     const auto poDstDriver = poDstDS->GetDriver();
    4046             :     const char *pszCreationFieldDataTypes =
    4047             :         poDstDriver
    4048        3758 :             ? poDstDriver->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATATYPES)
    4049        3758 :             : nullptr;
    4050             :     const char *pszCreationFieldDataSubtypes =
    4051             :         poDstDriver
    4052        3758 :             ? poDstDriver->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATASUBTYPES)
    4053        3758 :             : nullptr;
    4054        7401 :     if (pszCreationFieldDataTypes &&
    4055        3643 :         strstr(pszCreationFieldDataTypes,
    4056             :                OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType())) == nullptr)
    4057             :     {
    4058          30 :         if (pszCreationFieldDataSubtypes &&
    4059          58 :             (oFieldDefn.GetType() == OFTIntegerList ||
    4060          55 :              oFieldDefn.GetType() == OFTInteger64List ||
    4061          53 :              oFieldDefn.GetType() == OFTRealList ||
    4062          88 :              oFieldDefn.GetType() == OFTStringList) &&
    4063          25 :             strstr(pszCreationFieldDataSubtypes, "JSON"))
    4064             :         {
    4065           5 :             if (!bQuiet)
    4066             :             {
    4067           5 :                 CPLError(
    4068             :                     CE_Warning, CPLE_AppDefined,
    4069             :                     "The output driver does not seem to natively support %s "
    4070             :                     "type for field %s. Converting it to String(JSON) instead. "
    4071             :                     "-mapFieldType can be used to control field type "
    4072             :                     "conversion.",
    4073             :                     OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()),
    4074             :                     oFieldDefn.GetNameRef());
    4075             :             }
    4076           5 :             oFieldDefn.SetSubType(OFSTNone);
    4077           5 :             oFieldDefn.SetType(OFTString);
    4078           5 :             oFieldDefn.SetSubType(OFSTJSON);
    4079             :         }
    4080          27 :         else if (oFieldDefn.GetType() == OFTInteger64)
    4081             :         {
    4082           1 :             if (!bQuiet)
    4083             :             {
    4084           1 :                 CPLError(
    4085             :                     CE_Warning, CPLE_AppDefined,
    4086             :                     "The output driver does not seem to natively support %s "
    4087             :                     "type for field %s. Converting it to Real instead. "
    4088             :                     "-mapFieldType can be used to control field type "
    4089             :                     "conversion.",
    4090             :                     OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()),
    4091             :                     oFieldDefn.GetNameRef());
    4092             :             }
    4093           1 :             oFieldDefn.SetType(OFTReal);
    4094             :         }
    4095          30 :         else if (oFieldDefn.GetType() == OFTDateTime && poDstDriver &&
    4096           4 :                  EQUAL(poDstDriver->GetDescription(), "ESRI Shapefile"))
    4097             :         {
    4098             :             // Just be silent. The shapefile driver will itself emit a
    4099             :             // warning mentioning it converts DateTime to String.
    4100             :         }
    4101          25 :         else if (!bQuiet)
    4102             :         {
    4103          25 :             CPLError(
    4104             :                 CE_Warning, CPLE_AppDefined,
    4105             :                 "The output driver does not natively support %s type for "
    4106             :                 "field %s. Misconversion can happen. "
    4107             :                 "-mapFieldType can be used to control field type conversion.",
    4108             :                 OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()),
    4109             :                 oFieldDefn.GetNameRef());
    4110             :         }
    4111             :     }
    4112        3726 :     else if (!pszCreationFieldDataTypes)
    4113             :     {
    4114             :         // All drivers supporting OFTInteger64 should advertise it theoretically
    4115         115 :         if (oFieldDefn.GetType() == OFTInteger64)
    4116             :         {
    4117           1 :             if (!bQuiet)
    4118             :             {
    4119           1 :                 CPLError(CE_Warning, CPLE_AppDefined,
    4120             :                          "The output driver does not seem to natively support "
    4121             :                          "%s type "
    4122             :                          "for field %s. Converting it to Real instead. "
    4123             :                          "-mapFieldType can be used to control field type "
    4124             :                          "conversion.",
    4125             :                          OGRFieldDefn::GetFieldTypeName(oFieldDefn.GetType()),
    4126             :                          oFieldDefn.GetNameRef());
    4127             :             }
    4128           1 :             oFieldDefn.SetType(OFTReal);
    4129             :         }
    4130             :     }
    4131        3758 : }
    4132             : 
    4133             : /************************************************************************/
    4134             : /*                       GetArrowGeomFieldIndex()                       */
    4135             : /************************************************************************/
    4136             : 
    4137          24 : static int GetArrowGeomFieldIndex(const struct ArrowSchema *psLayerSchema,
    4138             :                                   const char *pszFieldName)
    4139             : {
    4140          24 :     if (strcmp(psLayerSchema->format, "+s") == 0)  // struct
    4141             :     {
    4142          82 :         for (int i = 0; i < psLayerSchema->n_children; ++i)
    4143             :         {
    4144          82 :             const auto psSchema = psLayerSchema->children[i];
    4145          82 :             if (strcmp(psSchema->format, "z") == 0)  // binary
    4146             :             {
    4147          24 :                 if (strcmp(psSchema->name, pszFieldName) == 0)
    4148             :                 {
    4149          22 :                     return i;
    4150             :                 }
    4151             :                 else
    4152             :                 {
    4153             :                     // Check if ARROW:extension:name = ogc.wkb or geoarrow.wkb
    4154           2 :                     const char *pabyMetadata = psSchema->metadata;
    4155           2 :                     if (pabyMetadata)
    4156             :                     {
    4157             :                         const auto oMetadata =
    4158           2 :                             OGRParseArrowMetadata(pabyMetadata);
    4159           2 :                         auto oIter = oMetadata.find(ARROW_EXTENSION_NAME_KEY);
    4160           4 :                         if (oIter != oMetadata.end() &&
    4161           2 :                             (oIter->second == EXTENSION_NAME_OGC_WKB ||
    4162           0 :                              oIter->second == EXTENSION_NAME_GEOARROW_WKB))
    4163             :                         {
    4164           2 :                             return i;
    4165             :                         }
    4166             :                     }
    4167             :                 }
    4168             :             }
    4169             :         }
    4170             :     }
    4171           0 :     return -1;
    4172             : }
    4173             : 
    4174             : /************************************************************************/
    4175             : /*                     BuildGetArrowStreamOptions()                     */
    4176             : /************************************************************************/
    4177             : 
    4178             : static CPLStringList
    4179         167 : BuildGetArrowStreamOptions(OGRLayer *poSrcLayer, OGRLayer *poDstLayer,
    4180             :                            const GDALVectorTranslateOptions *psOptions,
    4181             :                            bool bPreserveFID)
    4182             : {
    4183         167 :     CPLStringList aosOptionsGetArrowStream;
    4184         167 :     aosOptionsGetArrowStream.SetNameValue("SILENCE_GET_SCHEMA_ERROR", "YES");
    4185         167 :     aosOptionsGetArrowStream.SetNameValue("GEOMETRY_ENCODING", "WKB");
    4186         167 :     if (!bPreserveFID)
    4187         137 :         aosOptionsGetArrowStream.SetNameValue("INCLUDE_FID", "NO");
    4188         167 :     if (psOptions->nLimit >= 0)
    4189             :     {
    4190             :         aosOptionsGetArrowStream.SetNameValue(
    4191             :             "MAX_FEATURES_IN_BATCH",
    4192             :             CPLSPrintf(CPL_FRMT_GIB,
    4193           2 :                        std::min<GIntBig>(psOptions->nLimit,
    4194           2 :                                          (psOptions->nGroupTransactions > 0
    4195           4 :                                               ? psOptions->nGroupTransactions
    4196           2 :                                               : 65536))));
    4197             :     }
    4198         165 :     else if (psOptions->nGroupTransactions > 0)
    4199             :     {
    4200             :         aosOptionsGetArrowStream.SetNameValue(
    4201             :             "MAX_FEATURES_IN_BATCH",
    4202         165 :             CPLSPrintf("%d", psOptions->nGroupTransactions));
    4203             :     }
    4204             : 
    4205         167 :     auto poSrcDS = poSrcLayer->GetDataset();
    4206         167 :     auto poDstDS = poDstLayer->GetDataset();
    4207         167 :     if (poSrcDS && poDstDS)
    4208             :     {
    4209         163 :         auto poSrcDriver = poSrcDS->GetDriver();
    4210         163 :         auto poDstDriver = poDstDS->GetDriver();
    4211             : 
    4212         206 :         const auto IsArrowNativeDriver = [](GDALDriver *poDriver)
    4213             :         {
    4214         206 :             return EQUAL(poDriver->GetDescription(), "ARROW") ||
    4215         289 :                    EQUAL(poDriver->GetDescription(), "PARQUET") ||
    4216         289 :                    EQUAL(poDriver->GetDescription(), "ADBC");
    4217             :         };
    4218             : 
    4219         206 :         if (poSrcDriver && poDstDriver && !IsArrowNativeDriver(poSrcDriver) &&
    4220          43 :             !IsArrowNativeDriver(poDstDriver))
    4221             :         {
    4222             :             // For non-Arrow-native drivers, request DateTime as string, to
    4223             :             // allow mix of timezones
    4224             :             aosOptionsGetArrowStream.SetNameValue(GAS_OPT_DATETIME_AS_STRING,
    4225          40 :                                                   "YES");
    4226             :         }
    4227             :     }
    4228             : 
    4229         167 :     return aosOptionsGetArrowStream;
    4230             : }
    4231             : 
    4232             : /************************************************************************/
    4233             : /*              SetupTargetLayer::CanUseWriteArrowBatch()               */
    4234             : /************************************************************************/
    4235             : 
    4236        1192 : bool SetupTargetLayer::CanUseWriteArrowBatch(
    4237             :     OGRLayer *poSrcLayer, OGRLayer *poDstLayer, bool bJustCreatedLayer,
    4238             :     const GDALVectorTranslateOptions *psOptions, bool bPreserveFID,
    4239             :     bool &bError, OGRArrowArrayStream &streamSrc)
    4240             : {
    4241        1192 :     bError = false;
    4242             : 
    4243             :     // Check if we can use the Arrow interface to get and write features
    4244             :     // as it will be faster if the input driver has a fast
    4245             :     // implementation of GetArrowStream().
    4246             :     // We also can only do that only if using ogr2ogr without options that
    4247             :     // alter features.
    4248             :     // OGR2OGR_USE_ARROW_API config option is mostly for testing purposes
    4249             :     // or as a safety belt if things turned bad...
    4250        1192 :     bool bUseWriteArrowBatch = false;
    4251        1192 :     if (((poSrcLayer->TestCapability(OLCFastGetArrowStream) &&
    4252             :           // As we don't control the input array size when the input or output
    4253             :           // drivers are Arrow/Parquet (as they don't use the generic
    4254             :           // implementation), we can't guarantee that ROW_GROUP_SIZE/BATCH_SIZE
    4255             :           // layer creation options will be honored.
    4256         175 :           !psOptions->aosLCO.FetchNameValue("ROW_GROUP_SIZE") &&
    4257         173 :           !psOptions->aosLCO.FetchNameValue("BATCH_SIZE") &&
    4258         170 :           CPLTestBool(CPLGetConfigOption("OGR2OGR_USE_ARROW_API", "YES"))) ||
    4259        1023 :          CPLTestBool(CPLGetConfigOption("OGR2OGR_USE_ARROW_API", "NO"))) &&
    4260         180 :         !psOptions->bUpsert && !psOptions->bSkipFailures &&
    4261         348 :         !psOptions->poClipSrc && !psOptions->poClipDst &&
    4262         340 :         psOptions->asGCPs.empty() && !psOptions->bWrapDateline &&
    4263         170 :         !m_bAddMissingFields && m_eGType == GEOMTYPE_UNCHANGED &&
    4264         169 :         psOptions->eGeomOp == GEOMOP_NONE &&
    4265         169 :         m_eGeomTypeConversion == GTC_DEFAULT && m_nCoordDim < 0 &&
    4266         169 :         !m_papszFieldTypesToString && !m_papszMapFieldType &&
    4267         169 :         !m_bUnsetFieldWidth && !m_bExplodeCollections && !m_pszZField &&
    4268         168 :         m_bExactFieldNameMatch && !m_bForceNullable && !m_bResolveDomains &&
    4269         168 :         !m_bUnsetDefault && psOptions->nFIDToFetch == OGRNullFID &&
    4270         168 :         psOptions->dfXYRes == OGRGeomCoordinatePrecision::UNKNOWN &&
    4271        2384 :         !psOptions->bMakeValid && !psOptions->bSkipInvalidGeom)
    4272             :     {
    4273         168 :         if (psOptions->bTransform)
    4274             :         {
    4275             :             // To simplify implementation for now
    4276          26 :             if (poSrcLayer->GetLayerDefn()->GetGeomFieldCount() != 1 ||
    4277          13 :                 poDstLayer->GetLayerDefn()->GetGeomFieldCount() != 1)
    4278             :             {
    4279           1 :                 return false;
    4280             :             }
    4281          13 :             const auto poSrcSRS = m_poUserSourceSRS ? m_poUserSourceSRS
    4282          12 :                                                     : poSrcLayer->GetLayerDefn()
    4283          12 :                                                           ->GetGeomFieldDefn(0)
    4284          12 :                                                           ->GetSpatialRef();
    4285          13 :             if (!OGRGeometryFactory::isTransformWithOptionsRegularTransform(
    4286             :                     poSrcSRS, m_poOutputSRS, nullptr))
    4287             :             {
    4288           1 :                 return false;
    4289             :             }
    4290             :         }
    4291             : 
    4292         167 :         if (m_bSelFieldsSet)
    4293             :         {
    4294           2 :             SetIgnoredFields(poSrcLayer);
    4295             :         }
    4296             : 
    4297             :         const CPLStringList aosGetArrowStreamOptions(BuildGetArrowStreamOptions(
    4298         167 :             poSrcLayer, poDstLayer, psOptions, bPreserveFID));
    4299         167 :         if (poSrcLayer->GetArrowStream(streamSrc.get(),
    4300         167 :                                        aosGetArrowStreamOptions.List()))
    4301             :         {
    4302             :             struct ArrowSchema schemaSrc;
    4303         167 :             if (streamSrc.get_schema(&schemaSrc) == 0)
    4304             :             {
    4305         179 :                 if (psOptions->bTransform &&
    4306          12 :                     GetArrowGeomFieldIndex(&schemaSrc,
    4307          12 :                                            poSrcLayer->GetGeometryColumn()) < 0)
    4308             :                 {
    4309           0 :                     schemaSrc.release(&schemaSrc);
    4310           0 :                     streamSrc.clear();
    4311           0 :                     return false;
    4312             :                 }
    4313             : 
    4314         167 :                 std::string osErrorMsg;
    4315         167 :                 if (poDstLayer->IsArrowSchemaSupported(&schemaSrc, nullptr,
    4316         167 :                                                        osErrorMsg))
    4317             :                 {
    4318             :                     const OGRFeatureDefn *poSrcFDefn =
    4319         167 :                         poSrcLayer->GetLayerDefn();
    4320             :                     const OGRFeatureDefn *poDstFDefn =
    4321         167 :                         poDstLayer->GetLayerDefn();
    4322         165 :                     if (bJustCreatedLayer && poDstFDefn &&
    4323         497 :                         poDstFDefn->GetFieldCount() == 0 &&
    4324         165 :                         poDstFDefn->GetGeomFieldCount() ==
    4325         165 :                             poSrcFDefn->GetGeomFieldCount())
    4326             :                     {
    4327             :                         // Create output fields using CreateFieldFromArrowSchema()
    4328        1273 :                         for (int i = 0; i < schemaSrc.n_children; ++i)
    4329             :                         {
    4330        1108 :                             const char *pszFieldName =
    4331        1108 :                                 schemaSrc.children[i]->name;
    4332             : 
    4333             :                             const auto iSrcField =
    4334        1108 :                                 poSrcFDefn->GetFieldIndex(pszFieldName);
    4335        1108 :                             if (iSrcField >= 0)
    4336             :                             {
    4337             :                                 const auto poSrcFieldDefn =
    4338         910 :                                     poSrcFDefn->GetFieldDefn(iSrcField);
    4339             :                                 // Create field domain in output dataset if not already existing.
    4340             :                                 const std::string osDomainName(
    4341        1820 :                                     poSrcFieldDefn->GetDomainName());
    4342         910 :                                 if (!osDomainName.empty())
    4343             :                                 {
    4344          33 :                                     if (m_poDstDS->TestCapability(
    4345          22 :                                             ODsCAddFieldDomain) &&
    4346          11 :                                         m_poDstDS->GetFieldDomain(
    4347          11 :                                             osDomainName) == nullptr)
    4348             :                                     {
    4349             :                                         const auto poSrcDomain =
    4350          22 :                                             m_poSrcDS->GetFieldDomain(
    4351          11 :                                                 osDomainName);
    4352          11 :                                         if (poSrcDomain)
    4353             :                                         {
    4354          22 :                                             std::string failureReason;
    4355          11 :                                             if (!m_poDstDS->AddFieldDomain(
    4356          22 :                                                     std::unique_ptr<
    4357             :                                                         OGRFieldDomain>(
    4358          11 :                                                         poSrcDomain->Clone()),
    4359          11 :                                                     failureReason))
    4360             :                                             {
    4361           0 :                                                 CPLDebug("OGR2OGR",
    4362             :                                                          "Cannot create domain "
    4363             :                                                          "%s: %s",
    4364             :                                                          osDomainName.c_str(),
    4365             :                                                          failureReason.c_str());
    4366             :                                             }
    4367             :                                         }
    4368             :                                         else
    4369             :                                         {
    4370           0 :                                             CPLDebug("OGR2OGR",
    4371             :                                                      "Cannot find domain %s in "
    4372             :                                                      "source dataset",
    4373             :                                                      osDomainName.c_str());
    4374             :                                         }
    4375             :                                     }
    4376             :                                 }
    4377             :                             }
    4378             : 
    4379        3324 :                             if (!EQUAL(pszFieldName, "OGC_FID") &&
    4380        1108 :                                 !EQUAL(pszFieldName, "wkb_geometry") &&
    4381        1100 :                                 !EQUAL(pszFieldName,
    4382        1070 :                                        poSrcLayer->GetFIDColumn()) &&
    4383        1070 :                                 poSrcFDefn->GetGeomFieldIndex(pszFieldName) <
    4384        2216 :                                     0 &&
    4385         919 :                                 !poDstLayer->CreateFieldFromArrowSchema(
    4386         919 :                                     schemaSrc.children[i], nullptr))
    4387             :                             {
    4388           0 :                                 CPLError(CE_Failure, CPLE_AppDefined,
    4389             :                                          "Cannot create field %s",
    4390             :                                          pszFieldName);
    4391           0 :                                 schemaSrc.release(&schemaSrc);
    4392           0 :                                 streamSrc.clear();
    4393           0 :                                 return false;
    4394             :                             }
    4395             :                         }
    4396         165 :                         bUseWriteArrowBatch = true;
    4397             :                     }
    4398           2 :                     else if (!bJustCreatedLayer)
    4399             :                     {
    4400             :                         // If the layer already exist, get its schema, and
    4401             :                         // check that it looks to be the same as the source
    4402             :                         // one
    4403             :                         struct ArrowArrayStream streamDst;
    4404           2 :                         if (poDstLayer->GetArrowStream(
    4405           2 :                                 &streamDst, aosGetArrowStreamOptions.List()))
    4406             :                         {
    4407             :                             struct ArrowSchema schemaDst;
    4408           2 :                             if (streamDst.get_schema(&streamDst, &schemaDst) ==
    4409             :                                 0)
    4410             :                             {
    4411           2 :                                 if (schemaDst.n_children ==
    4412           2 :                                     schemaSrc.n_children)
    4413             :                                 {
    4414           2 :                                     bUseWriteArrowBatch = true;
    4415             :                                 }
    4416           2 :                                 schemaDst.release(&schemaDst);
    4417             :                             }
    4418           2 :                             streamDst.release(&streamDst);
    4419             :                         }
    4420             :                     }
    4421         167 :                     if (bUseWriteArrowBatch)
    4422             :                     {
    4423         167 :                         CPLDebug("OGR2OGR", "Using WriteArrowBatch()");
    4424             :                     }
    4425             :                 }
    4426             :                 else
    4427             :                 {
    4428           0 :                     CPLDebug("OGR2OGR",
    4429             :                              "Cannot use WriteArrowBatch() because "
    4430             :                              "input layer schema is not supported by output "
    4431             :                              "layer: %s",
    4432             :                              osErrorMsg.c_str());
    4433             :                 }
    4434         167 :                 schemaSrc.release(&schemaSrc);
    4435             :             }
    4436         167 :             if (!bUseWriteArrowBatch)
    4437           0 :                 streamSrc.clear();
    4438             :         }
    4439             :     }
    4440        1191 :     return bUseWriteArrowBatch;
    4441             : }
    4442             : 
    4443             : /************************************************************************/
    4444             : /*                 SetupTargetLayer::SetIgnoredFields()                 */
    4445             : /************************************************************************/
    4446             : 
    4447          12 : void SetupTargetLayer::SetIgnoredFields(OGRLayer *poSrcLayer)
    4448             : {
    4449          12 :     bool bUseIgnoredFields = true;
    4450          24 :     CPLStringList aosWHEREUsedFields;
    4451          12 :     const auto poSrcFDefn = poSrcLayer->GetLayerDefn();
    4452             : 
    4453          12 :     if (m_pszWHERE)
    4454             :     {
    4455             :         /* We must not ignore fields used in the -where expression
    4456             :          * (#4015) */
    4457           4 :         OGRFeatureQuery oFeatureQuery;
    4458           2 :         if (oFeatureQuery.Compile(poSrcFDefn, m_pszWHERE, FALSE, nullptr) ==
    4459             :             OGRERR_NONE)
    4460             :         {
    4461           0 :             aosWHEREUsedFields = oFeatureQuery.GetUsedFields();
    4462             :         }
    4463             :         else
    4464             :         {
    4465           2 :             bUseIgnoredFields = false;
    4466             :         }
    4467             :     }
    4468             : 
    4469          24 :     CPLStringList aosIgnoredFields;
    4470          40 :     for (int iSrcField = 0;
    4471          40 :          bUseIgnoredFields && iSrcField < poSrcFDefn->GetFieldCount();
    4472             :          iSrcField++)
    4473             :     {
    4474             :         const char *pszFieldName =
    4475          28 :             poSrcFDefn->GetFieldDefn(iSrcField)->GetNameRef();
    4476             :         bool bFieldRequested =
    4477          28 :             CSLFindString(m_papszSelFields, pszFieldName) >= 0;
    4478          28 :         bFieldRequested |= aosWHEREUsedFields.FindString(pszFieldName) >= 0;
    4479          28 :         bFieldRequested |=
    4480          28 :             (m_pszZField != nullptr && EQUAL(pszFieldName, m_pszZField));
    4481             : 
    4482             :         // If the source field is not requested, add it to the list of ignored
    4483             :         // fields.
    4484          28 :         if (!bFieldRequested)
    4485          15 :             aosIgnoredFields.push_back(pszFieldName);
    4486             :     }
    4487          12 :     if (bUseIgnoredFields)
    4488          10 :         poSrcLayer->SetIgnoredFields(
    4489          10 :             const_cast<const char **>(aosIgnoredFields.List()));
    4490          12 : }
    4491             : 
    4492             : /************************************************************************/
    4493             : /*                      SetupTargetLayer::Setup()                       */
    4494             : /************************************************************************/
    4495             : 
    4496             : std::unique_ptr<TargetLayerInfo>
    4497        1196 : SetupTargetLayer::Setup(OGRLayer *poSrcLayer, const char *pszNewLayerName,
    4498             :                         GDALVectorTranslateOptions *psOptions,
    4499             :                         GIntBig &nTotalEventsDone)
    4500             : {
    4501        1196 :     int eGType = m_eGType;
    4502        1196 :     bool bPreserveFID = m_bPreserveFID;
    4503        1196 :     bool bAppend = m_bAppend;
    4504             : 
    4505        1196 :     if (pszNewLayerName == nullptr)
    4506        1156 :         pszNewLayerName = poSrcLayer->GetName();
    4507             : 
    4508             :     /* -------------------------------------------------------------------- */
    4509             :     /*      Get other info.                                                 */
    4510             :     /* -------------------------------------------------------------------- */
    4511        1196 :     const OGRFeatureDefn *poSrcFDefn = poSrcLayer->GetLayerDefn();
    4512             : 
    4513             :     /* -------------------------------------------------------------------- */
    4514             :     /*      Find requested geometry fields.                                 */
    4515             :     /* -------------------------------------------------------------------- */
    4516        2392 :     std::vector<int> anRequestedGeomFields;
    4517        1196 :     const int nSrcGeomFieldCount = poSrcFDefn->GetGeomFieldCount();
    4518        1196 :     if (m_bSelFieldsSet && !bAppend)
    4519             :     {
    4520          44 :         for (int iField = 0; m_papszSelFields && m_papszSelFields[iField];
    4521             :              iField++)
    4522             :         {
    4523          27 :             int iSrcField = poSrcFDefn->GetFieldIndex(m_papszSelFields[iField]);
    4524          27 :             if (iSrcField >= 0)
    4525             :             {
    4526             :                 /* do nothing */
    4527             :             }
    4528             :             else
    4529             :             {
    4530           3 :                 iSrcField =
    4531           3 :                     poSrcFDefn->GetGeomFieldIndex(m_papszSelFields[iField]);
    4532           3 :                 if (iSrcField >= 0)
    4533             :                 {
    4534           3 :                     anRequestedGeomFields.push_back(iSrcField);
    4535             :                 }
    4536             :                 else
    4537             :                 {
    4538           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    4539             :                              "Field '%s' not found in source layer.",
    4540           0 :                              m_papszSelFields[iField]);
    4541           0 :                     if (!psOptions->bSkipFailures)
    4542           0 :                         return nullptr;
    4543             :                 }
    4544             :             }
    4545             :         }
    4546             : 
    4547          18 :         if (anRequestedGeomFields.size() > 1 &&
    4548           1 :             !m_poDstDS->TestCapability(ODsCCreateGeomFieldAfterCreateLayer))
    4549             :         {
    4550           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    4551             :                      "Several geometry fields requested, but output "
    4552             :                      "datasource does not support multiple geometry "
    4553             :                      "fields.");
    4554           0 :             if (!psOptions->bSkipFailures)
    4555           0 :                 return nullptr;
    4556             :             else
    4557           0 :                 anRequestedGeomFields.resize(0);
    4558             :         }
    4559             :     }
    4560             : 
    4561        1196 :     const OGRSpatialReference *poOutputSRS = m_poOutputSRS;
    4562        1196 :     if (poOutputSRS == nullptr && !m_bNullifyOutputSRS)
    4563             :     {
    4564        1048 :         if (nSrcGeomFieldCount == 1 || anRequestedGeomFields.empty())
    4565        1046 :             poOutputSRS = poSrcLayer->GetSpatialRef();
    4566           2 :         else if (anRequestedGeomFields.size() == 1)
    4567             :         {
    4568           1 :             int iSrcGeomField = anRequestedGeomFields[0];
    4569             :             poOutputSRS =
    4570           1 :                 poSrcFDefn->GetGeomFieldDefn(iSrcGeomField)->GetSpatialRef();
    4571             :         }
    4572             :     }
    4573             : 
    4574        1196 :     int iSrcZField = -1;
    4575        1196 :     if (m_pszZField != nullptr)
    4576             :     {
    4577           4 :         iSrcZField = poSrcFDefn->GetFieldIndex(m_pszZField);
    4578           4 :         if (iSrcZField < 0)
    4579             :         {
    4580           1 :             CPLError(CE_Warning, CPLE_AppDefined,
    4581             :                      "zfield '%s' does not exist in layer %s", m_pszZField,
    4582           1 :                      poSrcLayer->GetName());
    4583             :         }
    4584             :     }
    4585             : 
    4586             :     /* -------------------------------------------------------------------- */
    4587             :     /*      Find the layer.                                                 */
    4588             :     /* -------------------------------------------------------------------- */
    4589             : 
    4590             :     bool bErrorOccurred;
    4591             :     bool bOverwriteActuallyDone;
    4592             :     bool bAddOverwriteLCO;
    4593        2392 :     OGRLayer *poDstLayer = GetLayerAndOverwriteIfNecessary(
    4594        1196 :         m_poDstDS, pszNewLayerName, m_bOverwrite, &bErrorOccurred,
    4595             :         &bOverwriteActuallyDone, &bAddOverwriteLCO);
    4596        1196 :     const bool bJustCreatedLayer = (poDstLayer == nullptr);
    4597        1196 :     if (bErrorOccurred)
    4598           0 :         return nullptr;
    4599             : 
    4600             :     /* -------------------------------------------------------------------- */
    4601             :     /*      If the layer does not exist, then create it.                    */
    4602             :     /* -------------------------------------------------------------------- */
    4603        1196 :     if (poDstLayer == nullptr)
    4604             :     {
    4605        1147 :         if (!m_poDstDS->TestCapability(ODsCCreateLayer))
    4606             :         {
    4607           0 :             CPLError(
    4608             :                 CE_Failure, CPLE_AppDefined,
    4609             :                 "Layer '%s' does not already exist in the output dataset, and "
    4610             :                 "cannot be created by the output driver.",
    4611             :                 pszNewLayerName);
    4612           4 :             return nullptr;
    4613             :         }
    4614             : 
    4615        1147 :         bool bForceGType = (eGType != GEOMTYPE_UNCHANGED);
    4616        1147 :         if (!bForceGType)
    4617             :         {
    4618        1127 :             if (anRequestedGeomFields.empty())
    4619             :             {
    4620        1125 :                 eGType = poSrcFDefn->GetGeomType();
    4621             :             }
    4622           2 :             else if (anRequestedGeomFields.size() == 1)
    4623             :             {
    4624           1 :                 int iSrcGeomField = anRequestedGeomFields[0];
    4625           1 :                 eGType = poSrcFDefn->GetGeomFieldDefn(iSrcGeomField)->GetType();
    4626             :             }
    4627             :             else
    4628             :             {
    4629           1 :                 eGType = wkbNone;
    4630             :             }
    4631             : 
    4632             :             bool bHasZ =
    4633        1127 :                 CPL_TO_BOOL(wkbHasZ(static_cast<OGRwkbGeometryType>(eGType)));
    4634        1127 :             eGType = ConvertType(m_eGeomTypeConversion,
    4635             :                                  static_cast<OGRwkbGeometryType>(eGType));
    4636             : 
    4637        1127 :             if (m_bExplodeCollections)
    4638             :             {
    4639          12 :                 const OGRwkbGeometryType eFGType = wkbFlatten(eGType);
    4640          12 :                 if (eFGType == wkbMultiPoint)
    4641             :                 {
    4642           1 :                     eGType = wkbPoint;
    4643             :                 }
    4644          11 :                 else if (eFGType == wkbMultiLineString)
    4645             :                 {
    4646           0 :                     eGType = wkbLineString;
    4647             :                 }
    4648          11 :                 else if (eFGType == wkbMultiPolygon)
    4649             :                 {
    4650           0 :                     eGType = wkbPolygon;
    4651             :                 }
    4652          11 :                 else if (eFGType == wkbGeometryCollection ||
    4653          11 :                          eFGType == wkbMultiCurve || eFGType == wkbMultiSurface)
    4654             :                 {
    4655           0 :                     eGType = wkbUnknown;
    4656             :                 }
    4657             :             }
    4658             : 
    4659        1127 :             if (bHasZ || (iSrcZField >= 0 && eGType != wkbNone))
    4660         114 :                 eGType = wkbSetZ(static_cast<OGRwkbGeometryType>(eGType));
    4661             :         }
    4662             : 
    4663        1147 :         eGType = ForceCoordDimension(eGType, m_nCoordDim);
    4664             : 
    4665        1147 :         CPLErrorReset();
    4666             : 
    4667        1147 :         CPLStringList aosLCOTemp(CSLDuplicate(m_papszLCO));
    4668             :         const char *pszDestCreationOptions =
    4669        1147 :             m_poDstDS->GetDriver()->GetMetadataItem(
    4670        1147 :                 GDAL_DS_LAYER_CREATIONOPTIONLIST);
    4671             : 
    4672        1147 :         int eGCreateLayerType = eGType;
    4673        1160 :         if (anRequestedGeomFields.empty() && nSrcGeomFieldCount > 1 &&
    4674          13 :             m_poDstDS->TestCapability(ODsCCreateGeomFieldAfterCreateLayer))
    4675             :         {
    4676          12 :             eGCreateLayerType = wkbNone;
    4677             :         }
    4678             :         // If the source layer has a single geometry column that is not nullable
    4679             :         // and that ODsCCreateGeomFieldAfterCreateLayer is available, use it
    4680             :         // so as to be able to set the not null constraint (if the driver
    4681             :         // supports it) and that the output driver has no GEOMETRY_NULLABLE
    4682             :         // layer creation option. Same if the source geometry column has a non
    4683             :         // empty name that is not overridden, and that the output driver has no
    4684             :         // GEOMETRY_NAME layer creation option, but no LAUNDER option (if
    4685             :         // laundering is available, then we might want to launder the geometry
    4686             :         // column name as well)
    4687         936 :         else if (eGType != wkbNone && anRequestedGeomFields.empty() &&
    4688         932 :                  nSrcGeomFieldCount == 1 &&
    4689         932 :                  m_poDstDS->TestCapability(
    4690        3295 :                      ODsCCreateGeomFieldAfterCreateLayer) &&
    4691         292 :                  ((!poSrcFDefn->GetGeomFieldDefn(0)->IsNullable() &&
    4692           2 :                    CSLFetchNameValue(m_papszLCO, "GEOMETRY_NULLABLE") ==
    4693           2 :                        nullptr &&
    4694           2 :                    (pszDestCreationOptions == nullptr ||
    4695           2 :                     strstr(pszDestCreationOptions, "GEOMETRY_NULLABLE") !=
    4696           0 :                         nullptr) &&
    4697           0 :                    !m_bForceNullable) ||
    4698         292 :                   (poSrcLayer->GetGeometryColumn() != nullptr &&
    4699         292 :                    CSLFetchNameValue(m_papszLCO, "GEOMETRY_NAME") == nullptr &&
    4700         292 :                    !EQUAL(poSrcLayer->GetGeometryColumn(), "") &&
    4701          49 :                    (pszDestCreationOptions == nullptr ||
    4702          49 :                     strstr(pszDestCreationOptions, "GEOMETRY_NAME") ==
    4703           8 :                         nullptr ||
    4704           8 :                     strstr(pszDestCreationOptions, "LAUNDER") != nullptr) &&
    4705          49 :                    poSrcFDefn->GetFieldIndex(poSrcLayer->GetGeometryColumn()) <
    4706             :                        0)))
    4707             :         {
    4708          49 :             anRequestedGeomFields.push_back(0);
    4709          49 :             eGCreateLayerType = wkbNone;
    4710             :         }
    4711        1087 :         else if (anRequestedGeomFields.size() == 1 &&
    4712           1 :                  m_poDstDS->TestCapability(ODsCCreateGeomFieldAfterCreateLayer))
    4713             :         {
    4714           0 :             eGCreateLayerType = wkbNone;
    4715             :         }
    4716             : 
    4717        1147 :         OGRGeomCoordinatePrecision oCoordPrec;
    4718        1147 :         std::string osGeomFieldName;
    4719        1147 :         bool bGeomFieldNullable = true;
    4720             : 
    4721             :         {
    4722        1147 :             int iSrcGeomField = -1;
    4723        1360 :             if (anRequestedGeomFields.empty() &&
    4724         213 :                 (nSrcGeomFieldCount == 1 ||
    4725         213 :                  (!m_poDstDS->TestCapability(
    4726         282 :                       ODsCCreateGeomFieldAfterCreateLayer) &&
    4727             :                   nSrcGeomFieldCount > 1)))
    4728             :             {
    4729         884 :                 iSrcGeomField = 0;
    4730             :             }
    4731         263 :             else if (anRequestedGeomFields.size() == 1)
    4732             :             {
    4733          50 :                 iSrcGeomField = anRequestedGeomFields[0];
    4734             :             }
    4735             : 
    4736        1147 :             if (iSrcGeomField >= 0)
    4737             :             {
    4738             :                 const auto poSrcGeomFieldDefn =
    4739         934 :                     poSrcFDefn->GetGeomFieldDefn(iSrcGeomField);
    4740         934 :                 if (!psOptions->bUnsetCoordPrecision)
    4741             :                 {
    4742         933 :                     oCoordPrec = poSrcGeomFieldDefn->GetCoordinatePrecision()
    4743        1866 :                                      .ConvertToOtherSRS(
    4744         933 :                                          poSrcGeomFieldDefn->GetSpatialRef(),
    4745         933 :                                          poOutputSRS);
    4746             :                 }
    4747             : 
    4748             :                 bGeomFieldNullable =
    4749         934 :                     CPL_TO_BOOL(poSrcGeomFieldDefn->IsNullable());
    4750             : 
    4751         934 :                 const char *pszGFldName = poSrcGeomFieldDefn->GetNameRef();
    4752        1172 :                 if (pszGFldName != nullptr && !EQUAL(pszGFldName, "") &&
    4753         238 :                     poSrcFDefn->GetFieldIndex(pszGFldName) < 0)
    4754             :                 {
    4755         237 :                     osGeomFieldName = pszGFldName;
    4756             : 
    4757             :                     // Use source geometry field name as much as possible
    4758         237 :                     if (eGType != wkbNone && pszDestCreationOptions &&
    4759         237 :                         strstr(pszDestCreationOptions, "GEOMETRY_NAME") !=
    4760         474 :                             nullptr &&
    4761         172 :                         CSLFetchNameValue(m_papszLCO, "GEOMETRY_NAME") ==
    4762             :                             nullptr)
    4763             :                     {
    4764         172 :                         aosLCOTemp.SetNameValue("GEOMETRY_NAME", pszGFldName);
    4765             :                     }
    4766             :                 }
    4767             :             }
    4768             :         }
    4769             : 
    4770             :         // If the source feature first geometry column is not nullable
    4771             :         // and that GEOMETRY_NULLABLE creation option is available, use it
    4772             :         // so as to be able to set the not null constraint (if the driver
    4773             :         // supports it)
    4774         948 :         if (eGType != wkbNone && anRequestedGeomFields.empty() &&
    4775         896 :             nSrcGeomFieldCount >= 1 &&
    4776         896 :             !poSrcFDefn->GetGeomFieldDefn(0)->IsNullable() &&
    4777           0 :             pszDestCreationOptions != nullptr &&
    4778           0 :             strstr(pszDestCreationOptions, "GEOMETRY_NULLABLE") != nullptr &&
    4779        2095 :             CSLFetchNameValue(m_papszLCO, "GEOMETRY_NULLABLE") == nullptr &&
    4780           0 :             !m_bForceNullable)
    4781             :         {
    4782           0 :             bGeomFieldNullable = false;
    4783           0 :             aosLCOTemp.SetNameValue("GEOMETRY_NULLABLE", "NO");
    4784           0 :             CPLDebug("GDALVectorTranslate", "Using GEOMETRY_NULLABLE=NO");
    4785             :         }
    4786             : 
    4787        1147 :         if (psOptions->dfXYRes != OGRGeomCoordinatePrecision::UNKNOWN)
    4788             :         {
    4789           7 :             if (m_poDstDS->GetDriver()->GetMetadataItem(
    4790           8 :                     GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION) == nullptr &&
    4791           1 :                 !OGRGeometryFactory::haveGEOS())
    4792             :             {
    4793           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    4794             :                          "-xyRes specified, but driver does not expose the "
    4795             :                          "DCAP_HONOR_GEOM_COORDINATE_PRECISION capability, "
    4796             :                          "and this build has no GEOS support");
    4797             :             }
    4798             : 
    4799           7 :             oCoordPrec.dfXYResolution = psOptions->dfXYRes;
    4800           7 :             if (!psOptions->osXYResUnit.empty())
    4801             :             {
    4802           5 :                 if (!poOutputSRS)
    4803             :                 {
    4804           1 :                     CPLError(CE_Failure, CPLE_AppDefined,
    4805             :                              "Unit suffix for -xyRes cannot be used with an "
    4806             :                              "unknown destination SRS");
    4807           1 :                     return nullptr;
    4808             :                 }
    4809             : 
    4810           4 :                 if (psOptions->osXYResUnit == "mm")
    4811             :                 {
    4812           1 :                     oCoordPrec.dfXYResolution *= 1e-3;
    4813             :                 }
    4814           3 :                 else if (psOptions->osXYResUnit == "deg")
    4815             :                 {
    4816             :                     double dfFactorDegToMeter =
    4817           2 :                         poOutputSRS->GetSemiMajor(nullptr) * M_PI / 180;
    4818           2 :                     oCoordPrec.dfXYResolution *= dfFactorDegToMeter;
    4819             :                 }
    4820             :                 else
    4821             :                 {
    4822             :                     // Checked at argument parsing time
    4823           1 :                     CPLAssert(psOptions->osXYResUnit == "m");
    4824             :                 }
    4825             : 
    4826           4 :                 OGRGeomCoordinatePrecision tmp;
    4827           4 :                 tmp.SetFromMeter(poOutputSRS, oCoordPrec.dfXYResolution, 0, 0);
    4828           4 :                 oCoordPrec.dfXYResolution = tmp.dfXYResolution;
    4829             :             }
    4830             :         }
    4831             : 
    4832        1146 :         if (psOptions->dfZRes != OGRGeomCoordinatePrecision::UNKNOWN)
    4833             :         {
    4834           4 :             if (m_poDstDS->GetDriver()->GetMetadataItem(
    4835           4 :                     GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION) == nullptr)
    4836             :             {
    4837           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    4838             :                          "-zRes specified, but driver does not expose the "
    4839             :                          "DCAP_HONOR_GEOM_COORDINATE_PRECISION capability");
    4840             :             }
    4841             : 
    4842           4 :             oCoordPrec.dfZResolution = psOptions->dfZRes;
    4843           4 :             if (!psOptions->osZResUnit.empty())
    4844             :             {
    4845           3 :                 if (!poOutputSRS)
    4846             :                 {
    4847           1 :                     CPLError(CE_Failure, CPLE_AppDefined,
    4848             :                              "Unit suffix for -zRes cannot be used with an "
    4849             :                              "unknown destination SRS");
    4850           1 :                     return nullptr;
    4851             :                 }
    4852             : 
    4853           2 :                 if (psOptions->osZResUnit == "mm")
    4854             :                 {
    4855           1 :                     oCoordPrec.dfZResolution *= 1e-3;
    4856             :                 }
    4857             :                 else
    4858             :                 {
    4859             :                     // Checked at argument parsing time
    4860           1 :                     CPLAssert(psOptions->osZResUnit == "m");
    4861             :                 }
    4862             : 
    4863           2 :                 OGRGeomCoordinatePrecision tmp;
    4864           2 :                 tmp.SetFromMeter(poOutputSRS, 0, oCoordPrec.dfZResolution, 0);
    4865           2 :                 oCoordPrec.dfZResolution = tmp.dfZResolution;
    4866             :             }
    4867             :         }
    4868             : 
    4869        1145 :         if (psOptions->dfMRes != OGRGeomCoordinatePrecision::UNKNOWN)
    4870             :         {
    4871           3 :             if (m_poDstDS->GetDriver()->GetMetadataItem(
    4872           3 :                     GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION) == nullptr)
    4873             :             {
    4874           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    4875             :                          "-mRes specified, but driver does not expose the "
    4876             :                          "DCAP_HONOR_GEOM_COORDINATE_PRECISION capability");
    4877             :             }
    4878             : 
    4879           3 :             oCoordPrec.dfMResolution = psOptions->dfMRes;
    4880             :         }
    4881             : 
    4882             :         // For JSONFG
    4883        1145 :         CSLConstList papszMeasures = poSrcLayer->GetMetadata("MEASURES");
    4884        1145 :         if (papszMeasures && pszDestCreationOptions)
    4885             :         {
    4886           6 :             for (const char *pszItem : {"UNIT", "DESCRIPTION"})
    4887             :             {
    4888             :                 const char *pszValue =
    4889           4 :                     CSLFetchNameValue(papszMeasures, pszItem);
    4890           4 :                 if (pszValue)
    4891             :                 {
    4892             :                     const std::string osOptionName =
    4893          12 :                         std::string("MEASURE_").append(pszItem);
    4894           8 :                     if (strstr(pszDestCreationOptions, osOptionName.c_str()) &&
    4895           4 :                         CSLFetchNameValue(m_papszLCO, osOptionName.c_str()) ==
    4896             :                             nullptr)
    4897             :                     {
    4898           4 :                         aosLCOTemp.SetNameValue(osOptionName.c_str(), pszValue);
    4899             :                     }
    4900             :                 }
    4901             :             }
    4902             :         }
    4903             : 
    4904        1145 :         auto poSrcDriver = m_poSrcDS->GetDriver();
    4905             : 
    4906             :         // Force FID column as 64 bit if the source feature has a 64 bit FID,
    4907             :         // the target driver supports 64 bit FID and the user didn't set it
    4908             :         // manually.
    4909        1145 :         if (poSrcLayer->GetMetadataItem(OLMD_FID64) != nullptr &&
    4910           1 :             EQUAL(poSrcLayer->GetMetadataItem(OLMD_FID64), "YES") &&
    4911           1 :             pszDestCreationOptions &&
    4912        1146 :             strstr(pszDestCreationOptions, "FID64") != nullptr &&
    4913           0 :             CSLFetchNameValue(m_papszLCO, "FID64") == nullptr)
    4914             :         {
    4915           0 :             aosLCOTemp.SetNameValue("FID64", "YES");
    4916           0 :             CPLDebug("GDALVectorTranslate", "Using FID64=YES");
    4917             :         }
    4918             : 
    4919             :         // If output driver supports FID layer creation option, set it with
    4920             :         // the FID column name of the source layer
    4921        1143 :         if (!m_bUnsetFid && !bAppend && poSrcLayer->GetFIDColumn() != nullptr &&
    4922        1141 :             !EQUAL(poSrcLayer->GetFIDColumn(), "") &&
    4923          71 :             pszDestCreationOptions != nullptr &&
    4924          71 :             (strstr(pszDestCreationOptions, "='FID'") != nullptr ||
    4925        2290 :              strstr(pszDestCreationOptions, "=\"FID\"") != nullptr) &&
    4926          64 :             CSLFetchNameValue(m_papszLCO, "FID") == nullptr)
    4927             :         {
    4928          64 :             aosLCOTemp.SetNameValue("FID", poSrcLayer->GetFIDColumn());
    4929          64 :             if (!psOptions->bExplodeCollections)
    4930             :             {
    4931          63 :                 CPLDebug("GDALVectorTranslate",
    4932             :                          "Using FID=%s and -preserve_fid",
    4933          63 :                          poSrcLayer->GetFIDColumn());
    4934          63 :                 bPreserveFID = true;
    4935             :             }
    4936             :             else
    4937             :             {
    4938           1 :                 CPLDebug("GDALVectorTranslate",
    4939             :                          "Using FID=%s and disable -preserve_fid because not "
    4940             :                          "compatible with -explodecollection",
    4941           1 :                          poSrcLayer->GetFIDColumn());
    4942           1 :                 bPreserveFID = false;
    4943             :             }
    4944             :         }
    4945             :         // Detect scenario of converting from GPX to a format like GPKG
    4946             :         // Cf https://github.com/OSGeo/gdal/issues/9225
    4947        1076 :         else if (!bPreserveFID && !m_bUnsetFid && !bAppend && poSrcDriver &&
    4948         892 :                  EQUAL(poSrcDriver->GetDescription(), "GPX") &&
    4949           5 :                  pszDestCreationOptions &&
    4950           5 :                  (strstr(pszDestCreationOptions, "='FID'") != nullptr ||
    4951        2157 :                   strstr(pszDestCreationOptions, "=\"FID\"") != nullptr) &&
    4952           5 :                  CSLFetchNameValue(m_papszLCO, "FID") == nullptr)
    4953             :         {
    4954           5 :             CPLDebug("GDALVectorTranslate",
    4955             :                      "Forcing -preserve_fid because source is GPX and layers "
    4956             :                      "have FID cross references");
    4957           5 :             bPreserveFID = true;
    4958             :         }
    4959             :         // Detect scenario of converting GML2 with fid attribute to GPKG
    4960        1171 :         else if (EQUAL(m_poDstDS->GetDriver()->GetDescription(), "GPKG") &&
    4961          95 :                  CSLFetchNameValue(m_papszLCO, "FID") == nullptr)
    4962             :         {
    4963          93 :             int nFieldIdx = poSrcLayer->GetLayerDefn()->GetFieldIndex("fid");
    4964          94 :             if (nFieldIdx >= 0 && poSrcLayer->GetLayerDefn()
    4965           1 :                                           ->GetFieldDefn(nFieldIdx)
    4966           1 :                                           ->GetType() == OFTString)
    4967             :             {
    4968           1 :                 CPLDebug("GDALVectorTranslate",
    4969             :                          "Source layer has a non-string 'fid' column. Using "
    4970             :                          "FID=gpkg_fid for GeoPackage");
    4971           1 :                 aosLCOTemp.SetNameValue("FID", "gpkg_fid");
    4972             :             }
    4973             :         }
    4974             : 
    4975             :         // For a MapInfo -> MapInfo translation, preserve the layer bounds.
    4976        1145 :         if (m_poSrcDS->GetDriver() == m_poDstDS->GetDriver() &&
    4977         437 :             EQUAL(m_poDstDS->GetDriver()->GetDescription(), "MapInfo File") &&
    4978        1585 :             (m_poOutputSRS == nullptr || !m_bTransform) &&
    4979           3 :             CSLFetchNameValue(m_papszLCO, "BOUNDS") == nullptr)
    4980             :         {
    4981           3 :             if (const char *pszBounds = poSrcLayer->GetMetadataItem("BOUNDS"))
    4982             :             {
    4983           3 :                 CPLDebug("GDALVectorTranslate", "Setting -lco BOUNDS=%s",
    4984             :                          pszBounds);
    4985           3 :                 aosLCOTemp.SetNameValue("BOUNDS", pszBounds);
    4986             :             }
    4987             :         }
    4988             : 
    4989             :         // If bAddOverwriteLCO is ON (set up when overwriting a CARTO layer),
    4990             :         // set OVERWRITE to YES so the new layer overwrites the old one
    4991        1145 :         if (bAddOverwriteLCO)
    4992             :         {
    4993           0 :             aosLCOTemp.SetNameValue("OVERWRITE", "ON");
    4994           0 :             CPLDebug("GDALVectorTranslate", "Using OVERWRITE=ON");
    4995             :         }
    4996             : 
    4997        3434 :         if (m_bNativeData &&
    4998        1144 :             poSrcLayer->GetMetadataItem("NATIVE_DATA", "NATIVE_DATA") !=
    4999          25 :                 nullptr &&
    5000          25 :             poSrcLayer->GetMetadataItem("NATIVE_MEDIA_TYPE", "NATIVE_DATA") !=
    5001          25 :                 nullptr &&
    5002          25 :             pszDestCreationOptions != nullptr &&
    5003        2314 :             strstr(pszDestCreationOptions, "NATIVE_DATA") != nullptr &&
    5004          25 :             strstr(pszDestCreationOptions, "NATIVE_MEDIA_TYPE") != nullptr)
    5005             :         {
    5006             :             aosLCOTemp.SetNameValue(
    5007             :                 "NATIVE_DATA",
    5008          25 :                 poSrcLayer->GetMetadataItem("NATIVE_DATA", "NATIVE_DATA"));
    5009             :             aosLCOTemp.SetNameValue("NATIVE_MEDIA_TYPE",
    5010             :                                     poSrcLayer->GetMetadataItem(
    5011          25 :                                         "NATIVE_MEDIA_TYPE", "NATIVE_DATA"));
    5012          25 :             CPLDebug("GDALVectorTranslate", "Transferring layer NATIVE_DATA");
    5013             :         }
    5014             : 
    5015             :         // For FileGeodatabase, automatically set
    5016             :         // CREATE_SHAPE_AREA_AND_LENGTH_FIELDS=YES creation option if the source
    5017             :         // layer has a Shape_Area/Shape_Length field
    5018        2267 :         if (pszDestCreationOptions &&
    5019        1122 :             strstr(pszDestCreationOptions,
    5020        2267 :                    "CREATE_SHAPE_AREA_AND_LENGTH_FIELDS") != nullptr &&
    5021          28 :             CSLFetchNameValue(m_papszLCO,
    5022             :                               "CREATE_SHAPE_AREA_AND_LENGTH_FIELDS") == nullptr)
    5023             :         {
    5024          28 :             const auto poSrcLayerDefn = poSrcLayer->GetLayerDefn();
    5025             :             const int nIdxShapeArea =
    5026          28 :                 poSrcLayerDefn->GetFieldIndex("Shape_Area");
    5027             :             const int nIdxShapeLength =
    5028          28 :                 poSrcLayerDefn->GetFieldIndex("Shape_Length");
    5029          30 :             if ((nIdxShapeArea >= 0 &&
    5030           2 :                  poSrcLayerDefn->GetFieldDefn(nIdxShapeArea)->GetDefault() !=
    5031           2 :                      nullptr &&
    5032           2 :                  EQUAL(
    5033             :                      poSrcLayerDefn->GetFieldDefn(nIdxShapeArea)->GetDefault(),
    5034           2 :                      "FILEGEODATABASE_SHAPE_AREA") &&
    5035           2 :                  (m_papszSelFields == nullptr ||
    5036          31 :                   CSLFindString(m_papszSelFields, "Shape_Area") >= 0)) ||
    5037           1 :                 (nIdxShapeLength >= 0 &&
    5038           1 :                  poSrcLayerDefn->GetFieldDefn(nIdxShapeLength)->GetDefault() !=
    5039           1 :                      nullptr &&
    5040           1 :                  EQUAL(poSrcLayerDefn->GetFieldDefn(nIdxShapeLength)
    5041             :                            ->GetDefault(),
    5042           1 :                        "FILEGEODATABASE_SHAPE_LENGTH") &&
    5043           1 :                  (m_papszSelFields == nullptr ||
    5044           0 :                   CSLFindString(m_papszSelFields, "Shape_Length") >= 0)))
    5045             :             {
    5046             :                 aosLCOTemp.SetNameValue("CREATE_SHAPE_AREA_AND_LENGTH_FIELDS",
    5047           3 :                                         "YES");
    5048           3 :                 CPLDebug("GDALVectorTranslate",
    5049             :                          "Setting CREATE_SHAPE_AREA_AND_LENGTH_FIELDS=YES");
    5050             :             }
    5051             :         }
    5052             : 
    5053             :         // Use case of https://github.com/OSGeo/gdal/issues/11057#issuecomment-2495479779
    5054             :         // Conversion from GPKG to OCI.
    5055             :         // OCI distinguishes between TIMESTAMP and TIMESTAMP WITH TIME ZONE
    5056             :         // GeoPackage is supposed to have DateTime in UTC, so we set
    5057             :         // TIMESTAMP_WITH_TIME_ZONE=YES
    5058         964 :         if (poSrcDriver && pszDestCreationOptions &&
    5059         941 :             strstr(pszDestCreationOptions, "TIMESTAMP_WITH_TIME_ZONE") &&
    5060           0 :             CSLFetchNameValue(m_papszLCO, "TIMESTAMP_WITH_TIME_ZONE") ==
    5061        2109 :                 nullptr &&
    5062           0 :             EQUAL(poSrcDriver->GetDescription(), "GPKG"))
    5063             :         {
    5064           0 :             aosLCOTemp.SetNameValue("TIMESTAMP_WITH_TIME_ZONE", "YES");
    5065           0 :             CPLDebug("GDALVectorTranslate",
    5066             :                      "Setting TIMESTAMP_WITH_TIME_ZONE=YES");
    5067             :         }
    5068             : 
    5069             :         OGRGeomFieldDefn oGeomFieldDefn(
    5070             :             osGeomFieldName.c_str(),
    5071        1145 :             static_cast<OGRwkbGeometryType>(eGCreateLayerType));
    5072        1145 :         oGeomFieldDefn.SetSpatialRef(poOutputSRS);
    5073        1145 :         oGeomFieldDefn.SetCoordinatePrecision(oCoordPrec);
    5074        1145 :         oGeomFieldDefn.SetNullable(bGeomFieldNullable);
    5075        1145 :         poDstLayer = m_poDstDS->CreateLayer(
    5076             :             pszNewLayerName,
    5077             :             eGCreateLayerType == wkbNone ? nullptr : &oGeomFieldDefn,
    5078        1145 :             aosLCOTemp.List());
    5079             : 
    5080        1145 :         if (poDstLayer == nullptr)
    5081             :         {
    5082           2 :             return nullptr;
    5083             :         }
    5084             : 
    5085             :         // Cf https://github.com/OSGeo/gdal/issues/6859
    5086             :         // warn if the user requests -t_srs but the driver uses a different SRS.
    5087        1179 :         if (m_poOutputSRS != nullptr && m_bTransform && !psOptions->bQuiet &&
    5088             :             // MapInfo is somewhat lossy regarding SRS, so do not warn
    5089          36 :             !EQUAL(m_poDstDS->GetDriver()->GetDescription(), "MapInfo File"))
    5090             :         {
    5091          35 :             auto poCreatedSRS = poDstLayer->GetSpatialRef();
    5092          35 :             if (poCreatedSRS != nullptr)
    5093             :             {
    5094          21 :                 const char *const apszOptions[] = {
    5095             :                     "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES",
    5096             :                     "CRITERION=EQUIVALENT", nullptr};
    5097          21 :                 if (!poCreatedSRS->IsSame(m_poOutputSRS, apszOptions))
    5098             :                 {
    5099           1 :                     const char *pszTargetSRSName = m_poOutputSRS->GetName();
    5100           1 :                     const char *pszCreatedSRSName = poCreatedSRS->GetName();
    5101           1 :                     CPLError(CE_Warning, CPLE_AppDefined,
    5102             :                              "Target SRS %s not taken into account as target "
    5103             :                              "driver likely implements on-the-fly reprojection "
    5104             :                              "to %s",
    5105             :                              pszTargetSRSName ? pszTargetSRSName : "",
    5106             :                              pszCreatedSRSName ? pszCreatedSRSName : "");
    5107             :                 }
    5108             :             }
    5109             :         }
    5110             : 
    5111        1143 :         if (m_bCopyMD)
    5112             :         {
    5113        2278 :             const CPLStringList aosDomains(poSrcLayer->GetMetadataDomainList());
    5114        1705 :             for (const char *pszMD : aosDomains)
    5115             :             {
    5116         566 :                 if (!EQUAL(pszMD, "IMAGE_STRUCTURE") &&
    5117         566 :                     !EQUAL(pszMD, "SUBDATASETS"))
    5118             :                 {
    5119         566 :                     if (CSLConstList papszMD = poSrcLayer->GetMetadata(pszMD))
    5120             :                     {
    5121             :                         // MapInfo: Avoid overwriting the "BOUNDS" metadata on the output layer
    5122             :                         // with the value from the source layer. If the value should be
    5123             :                         // propagated, it will have been done via a layer creation option already.
    5124         749 :                         if (pszMD[0] == '\0' &&
    5125         248 :                             EQUAL(m_poDstDS->GetDriverName(), "MapInfo File"))
    5126             :                         {
    5127             :                             const char *pszBounds =
    5128           8 :                                 aosLCOTemp.FetchNameValue("BOUNDS");
    5129          16 :                             CPLStringList aosTmpMD(CSLDuplicate(papszMD), true);
    5130           8 :                             aosTmpMD.SetNameValue("BOUNDS", pszBounds);
    5131           8 :                             poDstLayer->SetMetadata(aosTmpMD.List(), pszMD);
    5132             :                         }
    5133             :                         else
    5134             :                         {
    5135         493 :                             poDstLayer->SetMetadata(papszMD, pszMD);
    5136             :                         }
    5137             :                     }
    5138             :                 }
    5139             :             }
    5140             :         }
    5141             : 
    5142        1156 :         if (anRequestedGeomFields.empty() && nSrcGeomFieldCount > 1 &&
    5143          13 :             m_poDstDS->TestCapability(ODsCCreateGeomFieldAfterCreateLayer))
    5144             :         {
    5145         143 :             for (int i = 0; i < nSrcGeomFieldCount; i++)
    5146             :             {
    5147         131 :                 anRequestedGeomFields.push_back(i);
    5148             :             }
    5149             :         }
    5150             : 
    5151        2323 :         if (anRequestedGeomFields.size() > 1 ||
    5152        1130 :             (anRequestedGeomFields.size() == 1 &&
    5153          50 :              m_poDstDS->TestCapability(ODsCCreateGeomFieldAfterCreateLayer)))
    5154             :         {
    5155         244 :             for (int i = 0; i < static_cast<int>(anRequestedGeomFields.size());
    5156             :                  i++)
    5157             :             {
    5158         182 :                 const int iSrcGeomField = anRequestedGeomFields[i];
    5159             :                 OGRGeomFieldDefn oGFldDefn(
    5160         364 :                     poSrcFDefn->GetGeomFieldDefn(iSrcGeomField));
    5161         182 :                 if (m_poOutputSRS != nullptr)
    5162             :                 {
    5163             :                     auto poOutputSRSClone =
    5164             :                         OGRSpatialReferenceRefCountedPtr::makeClone(
    5165          26 :                             m_poOutputSRS);
    5166          13 :                     oGFldDefn.SetSpatialRef(poOutputSRSClone.get());
    5167             :                 }
    5168         182 :                 if (bForceGType)
    5169             :                 {
    5170           1 :                     oGFldDefn.SetType(static_cast<OGRwkbGeometryType>(eGType));
    5171             :                 }
    5172             :                 else
    5173             :                 {
    5174         181 :                     eGType = oGFldDefn.GetType();
    5175         181 :                     eGType =
    5176         181 :                         ConvertType(m_eGeomTypeConversion,
    5177             :                                     static_cast<OGRwkbGeometryType>(eGType));
    5178         181 :                     eGType = ForceCoordDimension(eGType, m_nCoordDim);
    5179         181 :                     oGFldDefn.SetType(static_cast<OGRwkbGeometryType>(eGType));
    5180             :                 }
    5181         182 :                 if (m_bForceNullable)
    5182           2 :                     oGFldDefn.SetNullable(TRUE);
    5183         182 :                 poDstLayer->CreateGeomField(&oGFldDefn);
    5184             :             }
    5185             :         }
    5186             : 
    5187        1143 :         bAppend = false;
    5188             :     }
    5189             : 
    5190             :     /* -------------------------------------------------------------------- */
    5191             :     /*      Otherwise we will append to it, if append was requested.        */
    5192             :     /* -------------------------------------------------------------------- */
    5193          49 :     else if (!bAppend && !m_bNewDataSource)
    5194             :     {
    5195           0 :         if (psOptions->bInvokedFromGdalAlgorithm)
    5196             :         {
    5197           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    5198             :                      "Layer %s already exists, and --append not specified. "
    5199             :                      "Consider using --append, or --overwrite-layer.",
    5200             :                      pszNewLayerName);
    5201             :         }
    5202             :         else
    5203             :         {
    5204           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    5205             :                      "Layer %s already exists, and -append not specified.\n"
    5206             :                      "        Consider using -append, or -overwrite.",
    5207             :                      pszNewLayerName);
    5208             :         }
    5209           0 :         return nullptr;
    5210             :     }
    5211             :     else
    5212             :     {
    5213          49 :         if (CSLCount(m_papszLCO) > 0)
    5214             :         {
    5215           0 :             CPLError(
    5216             :                 CE_Warning, CPLE_AppDefined,
    5217             :                 "Layer creation options ignored since an existing layer is\n"
    5218             :                 "         being appended to.");
    5219             :         }
    5220             :     }
    5221             : 
    5222             :     /* -------------------------------------------------------------------- */
    5223             :     /*      Process Layer style table                                       */
    5224             :     /* -------------------------------------------------------------------- */
    5225             : 
    5226        1192 :     poDstLayer->SetStyleTable(poSrcLayer->GetStyleTable());
    5227             :     /* -------------------------------------------------------------------- */
    5228             :     /*      Add fields.  Default to copy all field.                         */
    5229             :     /*      If only a subset of all fields requested, then output only      */
    5230             :     /*      the selected fields, and in the order that they were            */
    5231             :     /*      selected.                                                       */
    5232             :     /* -------------------------------------------------------------------- */
    5233        1192 :     const int nSrcFieldCount = poSrcFDefn->GetFieldCount();
    5234        1192 :     int iSrcFIDField = -1;
    5235             : 
    5236             :     // Initialize the index-to-index map to -1's
    5237        2384 :     std::vector<int> anMap(nSrcFieldCount, -1);
    5238             : 
    5239        2384 :     std::map<int, TargetLayerInfo::ResolvedInfo> oMapResolved;
    5240             : 
    5241             :     /* Determine if NUMERIC field width narrowing is allowed */
    5242        1192 :     auto poSrcDriver = m_poSrcDS->GetDriver();
    5243             :     const char *pszSrcWidthIncludesDecimalSeparator{
    5244        2202 :         poSrcDriver ? poSrcDriver->GetMetadataItem(
    5245        1010 :                           "DMD_NUMERIC_FIELD_WIDTH_INCLUDES_DECIMAL_SEPARATOR")
    5246        1192 :                     : nullptr};
    5247        1192 :     const bool bSrcWidthIncludesDecimalSeparator{
    5248        1500 :         pszSrcWidthIncludesDecimalSeparator &&
    5249         308 :         EQUAL(pszSrcWidthIncludesDecimalSeparator, "YES")};
    5250             :     const char *pszDstWidthIncludesDecimalSeparator{
    5251        1192 :         m_poDstDS->GetDriver()->GetMetadataItem(
    5252        1192 :             "DMD_NUMERIC_FIELD_WIDTH_INCLUDES_DECIMAL_SEPARATOR")};
    5253        1192 :     const bool bDstWidthIncludesDecimalSeparator{
    5254        1416 :         pszDstWidthIncludesDecimalSeparator &&
    5255         224 :         EQUAL(pszDstWidthIncludesDecimalSeparator, "YES")};
    5256             :     const char *pszSrcWidthIncludesMinusSign{
    5257        2202 :         poSrcDriver ? poSrcDriver->GetMetadataItem(
    5258        1010 :                           "DMD_NUMERIC_FIELD_WIDTH_INCLUDES_SIGN")
    5259        1192 :                     : nullptr};
    5260        1192 :     const bool bSrcWidthIncludesMinusSign{
    5261        1500 :         pszSrcWidthIncludesMinusSign &&
    5262         308 :         EQUAL(pszSrcWidthIncludesMinusSign, "YES")};
    5263             :     const char *pszDstWidthIncludesMinusSign{
    5264        1192 :         m_poDstDS->GetDriver()->GetMetadataItem(
    5265        1192 :             "DMD_NUMERIC_FIELD_WIDTH_INCLUDES_SIGN")};
    5266        1192 :     const bool bDstWidthIncludesMinusSign{
    5267        1416 :         pszDstWidthIncludesMinusSign &&
    5268         224 :         EQUAL(pszDstWidthIncludesMinusSign, "YES")};
    5269             : 
    5270             :     // Calculate width delta
    5271        1192 :     int iChangeWidthBy{0};
    5272             : 
    5273        1192 :     if (bSrcWidthIncludesDecimalSeparator && !bDstWidthIncludesDecimalSeparator)
    5274             :     {
    5275         184 :         iChangeWidthBy--;
    5276             :     }
    5277        1008 :     else if (!bSrcWidthIncludesDecimalSeparator &&
    5278             :              bDstWidthIncludesDecimalSeparator)
    5279             :     {
    5280         100 :         iChangeWidthBy++;
    5281             :     }
    5282             : 
    5283             :     // We cannot assume there is no minus sign, we can only inflate here
    5284        1192 :     if (!bSrcWidthIncludesMinusSign && bDstWidthIncludesMinusSign)
    5285             :     {
    5286         100 :         iChangeWidthBy++;
    5287             :     }
    5288             : 
    5289        1192 :     bool bError = false;
    5290        2384 :     OGRArrowArrayStream streamSrc;
    5291             : 
    5292             :     const bool bUseWriteArrowBatch =
    5293        2384 :         !EQUAL(m_poDstDS->GetDriver()->GetDescription(), "OCI") &&
    5294        1192 :         CanUseWriteArrowBatch(poSrcLayer, poDstLayer, bJustCreatedLayer,
    5295        1192 :                               psOptions, bPreserveFID, bError, streamSrc);
    5296        1192 :     if (bError)
    5297           0 :         return nullptr;
    5298             : 
    5299             :     /* Caution : at the time of writing, the MapInfo driver */
    5300             :     /* returns NULL until a field has been added */
    5301        1192 :     OGRFeatureDefn *poDstFDefn = poDstLayer->GetLayerDefn();
    5302             : 
    5303        1192 :     if (bUseWriteArrowBatch)
    5304             :     {
    5305             :         // Fields created above
    5306             :     }
    5307        1025 :     else if (m_papszFieldMap && bAppend)
    5308             :     {
    5309           2 :         bool bIdentity = false;
    5310             : 
    5311           2 :         if (EQUAL(m_papszFieldMap[0], "identity"))
    5312           1 :             bIdentity = true;
    5313           1 :         else if (CSLCount(m_papszFieldMap) != nSrcFieldCount)
    5314             :         {
    5315           0 :             CPLError(
    5316             :                 CE_Failure, CPLE_AppDefined,
    5317             :                 "Field map should contain the value 'identity' or "
    5318             :                 "the same number of integer values as the source field count.");
    5319           0 :             return nullptr;
    5320             :         }
    5321             : 
    5322          32 :         for (int iField = 0; iField < nSrcFieldCount; iField++)
    5323             :         {
    5324          30 :             anMap[iField] = bIdentity ? iField : atoi(m_papszFieldMap[iField]);
    5325          30 :             if (anMap[iField] >= poDstFDefn->GetFieldCount())
    5326             :             {
    5327           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    5328           0 :                          "Invalid destination field index %d.", anMap[iField]);
    5329           0 :                 return nullptr;
    5330             :             }
    5331           2 :         }
    5332             :     }
    5333        1023 :     else if (m_bSelFieldsSet && !bAppend)
    5334             :     {
    5335          15 :         int nDstFieldCount = poDstFDefn ? poDstFDefn->GetFieldCount() : 0;
    5336          40 :         for (int iField = 0; m_papszSelFields && m_papszSelFields[iField];
    5337             :              iField++)
    5338             :         {
    5339             :             const int iSrcField =
    5340          25 :                 poSrcFDefn->GetFieldIndex(m_papszSelFields[iField]);
    5341          25 :             if (iSrcField >= 0)
    5342             :             {
    5343             :                 const OGRFieldDefn *poSrcFieldDefn =
    5344          22 :                     poSrcFDefn->GetFieldDefn(iSrcField);
    5345          22 :                 OGRFieldDefn oFieldDefn(poSrcFieldDefn);
    5346             : 
    5347          22 :                 DoFieldTypeConversion(
    5348             :                     m_poDstDS, oFieldDefn, m_papszFieldTypesToString,
    5349          22 :                     m_papszMapFieldType, m_bUnsetFieldWidth, psOptions->bQuiet,
    5350          22 :                     m_bForceNullable, m_bUnsetDefault);
    5351             : 
    5352          22 :                 if (iChangeWidthBy != 0 && oFieldDefn.GetType() == OFTReal &&
    5353           0 :                     oFieldDefn.GetWidth() != 0)
    5354             :                 {
    5355           0 :                     oFieldDefn.SetWidth(oFieldDefn.GetWidth() + iChangeWidthBy);
    5356             :                 }
    5357             : 
    5358             :                 /* The field may have been already created at layer creation */
    5359             :                 const int iDstField =
    5360             :                     poDstFDefn
    5361          22 :                         ? poDstFDefn->GetFieldIndex(oFieldDefn.GetNameRef())
    5362          22 :                         : -1;
    5363          22 :                 if (iDstField >= 0)
    5364             :                 {
    5365           0 :                     anMap[iSrcField] = iDstField;
    5366             :                 }
    5367          22 :                 else if (poDstLayer->CreateField(&oFieldDefn) == OGRERR_NONE)
    5368             :                 {
    5369             :                     /* now that we've created a field, GetLayerDefn() won't
    5370             :                      * return NULL */
    5371          22 :                     if (poDstFDefn == nullptr)
    5372           0 :                         poDstFDefn = poDstLayer->GetLayerDefn();
    5373             : 
    5374             :                     /* Sanity check : if it fails, the driver is buggy */
    5375          44 :                     if (poDstFDefn != nullptr &&
    5376          22 :                         poDstFDefn->GetFieldCount() != nDstFieldCount + 1)
    5377             :                     {
    5378           0 :                         CPLError(CE_Warning, CPLE_AppDefined,
    5379             :                                  "The output driver has claimed to have added "
    5380             :                                  "the %s field, but it did not!",
    5381             :                                  oFieldDefn.GetNameRef());
    5382             :                     }
    5383             :                     else
    5384             :                     {
    5385          22 :                         anMap[iSrcField] = nDstFieldCount;
    5386          22 :                         nDstFieldCount++;
    5387             :                     }
    5388             :                 }
    5389           0 :                 else if (!psOptions->bSkipFailures)
    5390           0 :                     return nullptr;
    5391             :             }
    5392             :         }
    5393             : 
    5394             :         /* --------------------------------------------------------------------
    5395             :          */
    5396             :         /* Use SetIgnoredFields() on source layer if available */
    5397             :         /* --------------------------------------------------------------------
    5398             :          */
    5399          15 :         if (m_bSelFieldsSet && poSrcLayer->TestCapability(OLCIgnoreFields))
    5400             :         {
    5401          10 :             SetIgnoredFields(poSrcLayer);
    5402          15 :         }
    5403             :     }
    5404        1008 :     else if (!bAppend || m_bAddMissingFields)
    5405             :     {
    5406         973 :         int nDstFieldCount = poDstFDefn ? poDstFDefn->GetFieldCount() : 0;
    5407             : 
    5408             :         const bool caseInsensitive =
    5409         973 :             !EQUAL(m_poDstDS->GetDriver()->GetDescription(), "GeoJSON");
    5410       14842 :         const auto formatName = [caseInsensitive](const char *name)
    5411             :         {
    5412       14842 :             if (caseInsensitive)
    5413             :             {
    5414       28956 :                 return CPLString(name).toupper();
    5415             :             }
    5416             :             else
    5417             :             {
    5418         364 :                 return CPLString(name);
    5419             :             }
    5420         973 :         };
    5421             : 
    5422             :         /* Save the map of existing fields, before creating new ones */
    5423             :         /* This helps when converting a source layer that has duplicated field
    5424             :          * names */
    5425             :         /* which is a bad idea */
    5426         973 :         std::map<CPLString, int> oMapPreExistingFields;
    5427         973 :         std::unordered_set<std::string> oSetDstFieldNames;
    5428        1131 :         for (int iField = 0; iField < nDstFieldCount; iField++)
    5429             :         {
    5430             :             const char *pszFieldName =
    5431         158 :                 poDstFDefn->GetFieldDefn(iField)->GetNameRef();
    5432         316 :             CPLString osUpperFieldName(formatName(pszFieldName));
    5433         158 :             oSetDstFieldNames.insert(osUpperFieldName);
    5434         158 :             if (oMapPreExistingFields.find(osUpperFieldName) ==
    5435         316 :                 oMapPreExistingFields.end())
    5436         158 :                 oMapPreExistingFields[osUpperFieldName] = iField;
    5437             :             /*else
    5438             :                 CPLError(CE_Warning, CPLE_AppDefined,
    5439             :                          "The target layer has already a duplicated field name
    5440             :                '%s' before " "adding the fields of the source layer",
    5441             :                pszFieldName); */
    5442             :         }
    5443             : 
    5444         973 :         const char *pszFIDColumn = poDstLayer->GetFIDColumn();
    5445             : 
    5446         973 :         std::vector<int> anSrcFieldIndices;
    5447         973 :         if (m_bSelFieldsSet)
    5448             :         {
    5449           2 :             for (int iField = 0; m_papszSelFields && m_papszSelFields[iField];
    5450             :                  iField++)
    5451             :             {
    5452             :                 const int iSrcField =
    5453           1 :                     poSrcFDefn->GetFieldIndex(m_papszSelFields[iField]);
    5454           1 :                 if (iSrcField >= 0)
    5455             :                 {
    5456           1 :                     anSrcFieldIndices.push_back(iSrcField);
    5457             :                 }
    5458             :             }
    5459             :         }
    5460             :         else
    5461             :         {
    5462        4708 :             for (int iField = 0; iField < nSrcFieldCount; iField++)
    5463             :             {
    5464        3736 :                 anSrcFieldIndices.push_back(iField);
    5465             :             }
    5466             :         }
    5467             : 
    5468         973 :         std::unordered_set<std::string> oSetSrcFieldNames;
    5469        4711 :         for (int i = 0; i < poSrcFDefn->GetFieldCount(); i++)
    5470             :         {
    5471             :             oSetSrcFieldNames.insert(
    5472        3738 :                 formatName(poSrcFDefn->GetFieldDefn(i)->GetNameRef()));
    5473             :         }
    5474             : 
    5475             :         // For each source field name, memorize the last number suffix to have
    5476             :         // unique field names in the target. Let's imagine we have a source
    5477             :         // layer with the field name foo repeated twice After dealing the first
    5478             :         // field, oMapFieldNameToLastSuffix["foo"] will be 1, so when starting a
    5479             :         // unique name for the second field, we'll be able to start at 2. This
    5480             :         // avoids quadratic complexity if a big number of source field names are
    5481             :         // identical. Like in
    5482             :         // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37768
    5483         973 :         std::map<std::string, int> oMapFieldNameToLastSuffix;
    5484             : 
    5485        4708 :         for (size_t i = 0; i < anSrcFieldIndices.size(); i++)
    5486             :         {
    5487        3737 :             const int iField = anSrcFieldIndices[i];
    5488             :             const OGRFieldDefn *poSrcFieldDefn =
    5489        3737 :                 poSrcFDefn->GetFieldDefn(iField);
    5490        3737 :             OGRFieldDefn oFieldDefn(poSrcFieldDefn);
    5491             : 
    5492             :             // Avoid creating a field with the same name as the FID column
    5493        7475 :             if (pszFIDColumn != nullptr &&
    5494        3738 :                 EQUAL(pszFIDColumn, oFieldDefn.GetNameRef()) &&
    5495           1 :                 (oFieldDefn.GetType() == OFTInteger ||
    5496           0 :                  oFieldDefn.GetType() == OFTInteger64))
    5497             :             {
    5498           1 :                 iSrcFIDField = iField;
    5499           1 :                 continue;
    5500             :             }
    5501             : 
    5502        3736 :             DoFieldTypeConversion(
    5503             :                 m_poDstDS, oFieldDefn, m_papszFieldTypesToString,
    5504        3736 :                 m_papszMapFieldType, m_bUnsetFieldWidth, psOptions->bQuiet,
    5505        3736 :                 m_bForceNullable, m_bUnsetDefault);
    5506             : 
    5507        3957 :             if (iChangeWidthBy != 0 && oFieldDefn.GetType() == OFTReal &&
    5508         221 :                 oFieldDefn.GetWidth() != 0)
    5509             :             {
    5510         168 :                 oFieldDefn.SetWidth(oFieldDefn.GetWidth() + iChangeWidthBy);
    5511             :             }
    5512             : 
    5513             :             /* The field may have been already created at layer creation */
    5514             :             {
    5515             :                 const auto oIter = oMapPreExistingFields.find(
    5516        3736 :                     formatName(oFieldDefn.GetNameRef()));
    5517        3736 :                 if (oIter != oMapPreExistingFields.end())
    5518             :                 {
    5519         120 :                     anMap[iField] = oIter->second;
    5520         120 :                     continue;
    5521             :                 }
    5522             :             }
    5523             : 
    5524        3616 :             bool bHasRenamed = false;
    5525             :             /* In case the field name already exists in the target layer, */
    5526             :             /* build a unique field name */
    5527        3616 :             if (oSetDstFieldNames.find(formatName(oFieldDefn.GetNameRef())) !=
    5528        7232 :                 oSetDstFieldNames.end())
    5529             :             {
    5530             :                 const CPLString osTmpNameRaddixUC(
    5531           4 :                     formatName(oFieldDefn.GetNameRef()));
    5532           2 :                 int nTry = 1;
    5533             :                 const auto oIter =
    5534           2 :                     oMapFieldNameToLastSuffix.find(osTmpNameRaddixUC);
    5535           2 :                 if (oIter != oMapFieldNameToLastSuffix.end())
    5536           1 :                     nTry = oIter->second;
    5537           2 :                 CPLString osTmpNameUC = osTmpNameRaddixUC;
    5538           2 :                 osTmpNameUC.reserve(osTmpNameUC.size() + 10);
    5539             :                 while (true)
    5540             :                 {
    5541           3 :                     ++nTry;
    5542             :                     char szTry[32];
    5543           3 :                     snprintf(szTry, sizeof(szTry), "%d", nTry);
    5544             :                     osTmpNameUC.replace(osTmpNameRaddixUC.size(),
    5545           3 :                                         std::string::npos, szTry);
    5546             : 
    5547             :                     /* Check that the proposed name doesn't exist either in the
    5548             :                      * already */
    5549             :                     /* created fields or in the source fields */
    5550           3 :                     if (oSetDstFieldNames.find(osTmpNameUC) ==
    5551           9 :                             oSetDstFieldNames.end() &&
    5552           3 :                         oSetSrcFieldNames.find(osTmpNameUC) ==
    5553           6 :                             oSetSrcFieldNames.end())
    5554             :                     {
    5555           2 :                         bHasRenamed = true;
    5556           2 :                         oFieldDefn.SetName(
    5557           4 :                             (CPLString(oFieldDefn.GetNameRef()) + szTry)
    5558             :                                 .c_str());
    5559           2 :                         oMapFieldNameToLastSuffix[osTmpNameRaddixUC] = nTry;
    5560           2 :                         break;
    5561             :                     }
    5562           1 :                 }
    5563             :             }
    5564             : 
    5565             :             // Create field domain in output dataset if not already existing.
    5566        3616 :             const std::string osDomainName(oFieldDefn.GetDomainName());
    5567        3616 :             if (!osDomainName.empty())
    5568             :             {
    5569          26 :                 if (m_poDstDS->TestCapability(ODsCAddFieldDomain) &&
    5570          13 :                     m_poDstDS->GetFieldDomain(osDomainName) == nullptr)
    5571             :                 {
    5572             :                     const auto poSrcDomain =
    5573          13 :                         m_poSrcDS->GetFieldDomain(osDomainName);
    5574          13 :                     if (poSrcDomain)
    5575             :                     {
    5576          22 :                         std::string failureReason;
    5577          11 :                         if (!m_poDstDS->AddFieldDomain(
    5578          22 :                                 std::unique_ptr<OGRFieldDomain>(
    5579          11 :                                     poSrcDomain->Clone()),
    5580          11 :                                 failureReason))
    5581             :                         {
    5582           0 :                             oFieldDefn.SetDomainName(std::string());
    5583           0 :                             CPLDebug("OGR2OGR", "Cannot create domain %s: %s",
    5584             :                                      osDomainName.c_str(),
    5585             :                                      failureReason.c_str());
    5586             :                         }
    5587             :                     }
    5588             :                     else
    5589             :                     {
    5590           2 :                         CPLDebug("OGR2OGR",
    5591             :                                  "Cannot find domain %s in source dataset",
    5592             :                                  osDomainName.c_str());
    5593             :                     }
    5594             :                 }
    5595          13 :                 if (m_poDstDS->GetFieldDomain(osDomainName) == nullptr)
    5596             :                 {
    5597           2 :                     oFieldDefn.SetDomainName(std::string());
    5598             :                 }
    5599             :             }
    5600             : 
    5601        3616 :             if (poDstLayer->CreateField(&oFieldDefn) == OGRERR_NONE)
    5602             :             {
    5603             :                 /* now that we've created a field, GetLayerDefn() won't return
    5604             :                  * NULL */
    5605        3592 :                 if (poDstFDefn == nullptr)
    5606           0 :                     poDstFDefn = poDstLayer->GetLayerDefn();
    5607             : 
    5608             :                 /* Sanity check : if it fails, the driver is buggy */
    5609        7184 :                 if (poDstFDefn != nullptr &&
    5610        3592 :                     poDstFDefn->GetFieldCount() != nDstFieldCount + 1)
    5611             :                 {
    5612           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    5613             :                              "The output driver has claimed to have added the "
    5614             :                              "%s field, but it did not!",
    5615             :                              oFieldDefn.GetNameRef());
    5616             :                 }
    5617             :                 else
    5618             :                 {
    5619        3592 :                     if (poDstFDefn != nullptr)
    5620             :                     {
    5621             :                         const char *pszNewFieldName =
    5622        3592 :                             poDstFDefn->GetFieldDefn(nDstFieldCount)
    5623        3592 :                                 ->GetNameRef();
    5624        3592 :                         if (bHasRenamed)
    5625             :                         {
    5626           2 :                             CPLError(CE_Warning, CPLE_AppDefined,
    5627             :                                      "Field '%s' already exists. Renaming it "
    5628             :                                      "as '%s'",
    5629             :                                      poSrcFieldDefn->GetNameRef(),
    5630             :                                      pszNewFieldName);
    5631             :                         }
    5632        3592 :                         oSetDstFieldNames.insert(formatName(pszNewFieldName));
    5633             :                     }
    5634             : 
    5635        3592 :                     anMap[iField] = nDstFieldCount;
    5636        3592 :                     nDstFieldCount++;
    5637             :                 }
    5638             :             }
    5639          24 :             else if (!psOptions->bSkipFailures)
    5640           2 :                 return nullptr;
    5641             : 
    5642        3614 :             if (m_bResolveDomains && !osDomainName.empty())
    5643             :             {
    5644             :                 const auto poSrcDomain =
    5645           3 :                     m_poSrcDS->GetFieldDomain(osDomainName);
    5646           3 :                 if (poSrcDomain && poSrcDomain->GetDomainType() == OFDT_CODED)
    5647             :                 {
    5648             :                     OGRFieldDefn oResolvedField(
    5649             :                         CPLSPrintf("%s_resolved", oFieldDefn.GetNameRef()),
    5650           1 :                         OFTString);
    5651           1 :                     if (poDstLayer->CreateField(&oResolvedField) == OGRERR_NONE)
    5652             :                     {
    5653             :                         TargetLayerInfo::ResolvedInfo resolvedInfo;
    5654           1 :                         resolvedInfo.nSrcField = iField;
    5655           1 :                         resolvedInfo.poDomain = poSrcDomain;
    5656           1 :                         oMapResolved[nDstFieldCount] = resolvedInfo;
    5657           1 :                         nDstFieldCount++;
    5658             :                     }
    5659           0 :                     else if (!psOptions->bSkipFailures)
    5660           0 :                         return nullptr;
    5661             :                 }
    5662             :             }
    5663         971 :         }
    5664             :     }
    5665             :     else
    5666             :     {
    5667             :         /* For an existing layer, build the map by fetching the index in the
    5668             :          * destination */
    5669             :         /* layer for each source field */
    5670          35 :         if (poDstFDefn == nullptr)
    5671             :         {
    5672           0 :             CPLError(CE_Failure, CPLE_AppDefined, "poDstFDefn == NULL.");
    5673           0 :             return nullptr;
    5674             :         }
    5675             : 
    5676         118 :         for (int iField = 0; iField < nSrcFieldCount; iField++)
    5677             :         {
    5678             :             const OGRFieldDefn *poSrcFieldDefn =
    5679          83 :                 poSrcFDefn->GetFieldDefn(iField);
    5680          83 :             const int iDstField = poDstLayer->FindFieldIndex(
    5681          83 :                 poSrcFieldDefn->GetNameRef(), m_bExactFieldNameMatch);
    5682          83 :             if (iDstField >= 0)
    5683          79 :                 anMap[iField] = iDstField;
    5684             :             else
    5685             :             {
    5686           4 :                 if (m_bExactFieldNameMatch)
    5687             :                 {
    5688           4 :                     const int iDstFieldCandidate = poDstLayer->FindFieldIndex(
    5689           4 :                         poSrcFieldDefn->GetNameRef(), false);
    5690           4 :                     if (iDstFieldCandidate >= 0)
    5691             :                     {
    5692           1 :                         CPLError(CE_Warning, CPLE_AppDefined,
    5693             :                                  "Source field '%s' could have been identified "
    5694             :                                  "with existing field '%s' of destination "
    5695             :                                  "layer '%s' if the -relaxedFieldNameMatch "
    5696             :                                  "option had been specified.",
    5697             :                                  poSrcFieldDefn->GetNameRef(),
    5698           1 :                                  poDstLayer->GetLayerDefn()
    5699           1 :                                      ->GetFieldDefn(iDstFieldCandidate)
    5700             :                                      ->GetNameRef(),
    5701           1 :                                  poDstLayer->GetName());
    5702             :                     }
    5703             :                 }
    5704             : 
    5705           4 :                 CPLDebug(
    5706             :                     "GDALVectorTranslate",
    5707             :                     "Skipping field '%s' not found in destination layer '%s'.",
    5708           4 :                     poSrcFieldDefn->GetNameRef(), poDstLayer->GetName());
    5709             :             }
    5710             :         }
    5711             :     }
    5712             : 
    5713          16 :     if (bOverwriteActuallyDone && !bAddOverwriteLCO &&
    5714          16 :         EQUAL(m_poDstDS->GetDriver()->GetDescription(), "PostgreSQL") &&
    5715        1212 :         !psOptions->nLayerTransaction && psOptions->nGroupTransactions > 0 &&
    5716           6 :         CPLTestBool(CPLGetConfigOption("PG_COMMIT_WHEN_OVERWRITING", "YES")))
    5717             :     {
    5718           6 :         CPLDebug("GDALVectorTranslate",
    5719             :                  "Forcing transaction commit as table overwriting occurred");
    5720             :         // Commit when overwriting as this consumes a lot of PG resources
    5721             :         // and could result in """out of shared memory.
    5722             :         // You might need to increase max_locks_per_transaction."""" errors
    5723          12 :         if (m_poDstDS->CommitTransaction() == OGRERR_FAILURE ||
    5724           6 :             m_poDstDS->StartTransaction(psOptions->bForceTransaction) ==
    5725             :                 OGRERR_FAILURE)
    5726             :         {
    5727           0 :             return nullptr;
    5728             :         }
    5729           6 :         nTotalEventsDone = 0;
    5730             :     }
    5731             : 
    5732        2380 :     auto psInfo = std::make_unique<TargetLayerInfo>();
    5733        1190 :     psInfo->m_bUseWriteArrowBatch = bUseWriteArrowBatch;
    5734        1190 :     psInfo->m_nFeaturesRead = 0;
    5735        1190 :     psInfo->m_bPerFeatureCT = false;
    5736        1190 :     psInfo->m_poSrcLayer = poSrcLayer;
    5737        1190 :     psInfo->m_poDstLayer = poDstLayer;
    5738        1190 :     psInfo->m_aoReprojectionInfo.resize(
    5739        1190 :         poDstLayer->GetLayerDefn()->GetGeomFieldCount());
    5740        1190 :     psInfo->m_anMap = std::move(anMap);
    5741        1190 :     psInfo->m_iSrcZField = iSrcZField;
    5742        1190 :     psInfo->m_iSrcFIDField = iSrcFIDField;
    5743        1190 :     if (anRequestedGeomFields.size() == 1)
    5744          50 :         psInfo->m_iRequestedSrcGeomField = anRequestedGeomFields[0];
    5745             :     else
    5746        1140 :         psInfo->m_iRequestedSrcGeomField = -1;
    5747        1190 :     psInfo->m_bPreserveFID = bPreserveFID;
    5748        1190 :     psInfo->m_pszCTPipeline = m_pszCTPipeline;
    5749        1190 :     psInfo->m_aosCTOptions = m_aosCTOptions;
    5750        1190 :     psInfo->m_oMapResolved = std::move(oMapResolved);
    5751        1191 :     for (const auto &kv : psInfo->m_oMapResolved)
    5752             :     {
    5753           1 :         const auto poDomain = kv.second.poDomain;
    5754             :         const auto poCodedDomain =
    5755           1 :             cpl::down_cast<const OGRCodedFieldDomain *>(poDomain);
    5756           1 :         const auto enumeration = poCodedDomain->GetEnumeration();
    5757           2 :         std::map<std::string, std::string> oMapCodeValue;
    5758           4 :         for (int i = 0; enumeration[i].pszCode != nullptr; ++i)
    5759             :         {
    5760           6 :             oMapCodeValue[enumeration[i].pszCode] =
    5761           6 :                 enumeration[i].pszValue ? enumeration[i].pszValue : "";
    5762             :         }
    5763           1 :         psInfo->m_oMapDomainToKV[poDomain] = std::move(oMapCodeValue);
    5764             :     }
    5765             : 
    5766             :     // Detect if we can directly pass the source feature to the CreateFeature()
    5767             :     // method of the target layer, without doing any copying of field content.
    5768        1190 :     psInfo->m_bCanAvoidSetFrom = false;
    5769        1190 :     if (!m_bExplodeCollections && iSrcZField == -1 && poDstFDefn != nullptr)
    5770             :     {
    5771        1175 :         psInfo->m_bCanAvoidSetFrom = true;
    5772        1175 :         const int nDstGeomFieldCount = poDstFDefn->GetGeomFieldCount();
    5773        1175 :         if (nSrcFieldCount != poDstFDefn->GetFieldCount() ||
    5774             :             nSrcGeomFieldCount != nDstGeomFieldCount)
    5775             :         {
    5776         169 :             psInfo->m_bCanAvoidSetFrom = false;
    5777             :         }
    5778             :         else
    5779             :         {
    5780        4379 :             for (int i = 0; i < nSrcFieldCount; ++i)
    5781             :             {
    5782        3415 :                 auto poSrcFieldDefn = poSrcFDefn->GetFieldDefn(i);
    5783        3415 :                 auto poDstFieldDefn = poDstFDefn->GetFieldDefn(i);
    5784        6820 :                 if (poSrcFieldDefn->GetType() != poDstFieldDefn->GetType() ||
    5785        3405 :                     psInfo->m_anMap[i] != i)
    5786             :                 {
    5787          42 :                     psInfo->m_bCanAvoidSetFrom = false;
    5788          42 :                     break;
    5789             :                 }
    5790             :             }
    5791        1006 :             if (!psInfo->m_bCanAvoidSetFrom && nSrcGeomFieldCount > 1)
    5792             :             {
    5793           4 :                 for (int i = 0; i < nSrcGeomFieldCount; ++i)
    5794             :                 {
    5795           3 :                     auto poSrcGeomFieldDefn = poSrcFDefn->GetGeomFieldDefn(i);
    5796           3 :                     auto poDstGeomFieldDefn = poDstFDefn->GetGeomFieldDefn(i);
    5797           3 :                     if (!EQUAL(poSrcGeomFieldDefn->GetNameRef(),
    5798             :                                poDstGeomFieldDefn->GetNameRef()))
    5799             :                     {
    5800           1 :                         psInfo->m_bCanAvoidSetFrom = false;
    5801           1 :                         break;
    5802             :                     }
    5803             :                 }
    5804             :             }
    5805             :         }
    5806             :     }
    5807             : 
    5808        2380 :     psInfo->m_pszSpatSRSDef = psOptions->osSpatSRSDef.empty()
    5809        1190 :                                   ? nullptr
    5810           4 :                                   : psOptions->osSpatSRSDef.c_str();
    5811        1190 :     psInfo->m_hSpatialFilter =
    5812        1190 :         OGRGeometry::ToHandle(psOptions->poSpatialFilter.get());
    5813        1190 :     psInfo->m_pszGeomField =
    5814        1190 :         psOptions->bGeomFieldSet ? psOptions->osGeomField.c_str() : nullptr;
    5815             : 
    5816        1190 :     if (psOptions->nTZOffsetInSec != TZ_OFFSET_INVALID && poDstFDefn)
    5817             :     {
    5818          15 :         for (int i = 0; i < poDstFDefn->GetFieldCount(); ++i)
    5819             :         {
    5820          10 :             if (poDstFDefn->GetFieldDefn(i)->GetType() == OFTDateTime)
    5821             :             {
    5822           5 :                 psInfo->m_anDateTimeFieldIdx.push_back(i);
    5823             :             }
    5824             :         }
    5825             :     }
    5826             : 
    5827        1190 :     psInfo->m_bSupportCurves =
    5828        1190 :         CPL_TO_BOOL(poDstLayer->TestCapability(OLCCurveGeometries));
    5829        1190 :     psInfo->m_bSupportZ =
    5830        1190 :         CPL_TO_BOOL(poDstLayer->TestCapability(OLCZGeometries));
    5831        1190 :     psInfo->m_bSupportM =
    5832        1190 :         CPL_TO_BOOL(poDstLayer->TestCapability(OLCMeasuredGeometries));
    5833             : 
    5834        1190 :     psInfo->m_sArrowArrayStream = std::move(streamSrc);
    5835             : 
    5836        1190 :     return psInfo;
    5837             : }
    5838             : 
    5839             : /************************************************************************/
    5840             : /*                              SetupCT()                               */
    5841             : /************************************************************************/
    5842             : 
    5843             : static bool
    5844         952 : SetupCT(TargetLayerInfo *psInfo, OGRLayer *poSrcLayer, bool bTransform,
    5845             :         bool bWrapDateline, const CPLString &osDateLineOffset,
    5846             :         const OGRSpatialReference *poUserSourceSRS, OGRFeature *poFeature,
    5847             :         const OGRSpatialReference *poOutputSRS,
    5848             :         OGRCoordinateTransformation *poGCPCoordTrans, bool bVerboseError)
    5849             : {
    5850         952 :     OGRLayer *poDstLayer = psInfo->m_poDstLayer;
    5851             :     const int nDstGeomFieldCount =
    5852         952 :         poDstLayer->GetLayerDefn()->GetGeomFieldCount();
    5853        1840 :     for (int iGeom = 0; iGeom < nDstGeomFieldCount; iGeom++)
    5854             :     {
    5855             :         /* --------------------------------------------------------------------
    5856             :          */
    5857             :         /*      Setup coordinate transformation if we need it. */
    5858             :         /* --------------------------------------------------------------------
    5859             :          */
    5860         889 :         const OGRSpatialReference *poSourceSRS = nullptr;
    5861         889 :         OGRCoordinateTransformation *poCT = nullptr;
    5862         889 :         char **papszTransformOptions = nullptr;
    5863             : 
    5864             :         int iSrcGeomField;
    5865             :         auto poDstGeomFieldDefn =
    5866         889 :             poDstLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
    5867         889 :         if (psInfo->m_iRequestedSrcGeomField >= 0)
    5868             :         {
    5869          39 :             iSrcGeomField = psInfo->m_iRequestedSrcGeomField;
    5870             :         }
    5871             :         else
    5872             :         {
    5873        1700 :             iSrcGeomField = poSrcLayer->GetLayerDefn()->GetGeomFieldIndex(
    5874         850 :                 poDstGeomFieldDefn->GetNameRef());
    5875         850 :             if (iSrcGeomField < 0)
    5876             :             {
    5877         364 :                 if (nDstGeomFieldCount == 1 &&
    5878         182 :                     poSrcLayer->GetLayerDefn()->GetGeomFieldCount() > 0)
    5879             :                 {
    5880         174 :                     iSrcGeomField = 0;
    5881             :                 }
    5882             :                 else
    5883             :                 {
    5884           8 :                     continue;
    5885             :                 }
    5886             :             }
    5887             :         }
    5888             : 
    5889         881 :         if (psInfo->m_nFeaturesRead == 0)
    5890             :         {
    5891         880 :             poSourceSRS = poUserSourceSRS;
    5892         880 :             if (poSourceSRS == nullptr)
    5893             :             {
    5894         872 :                 if (iSrcGeomField > 0)
    5895         121 :                     poSourceSRS = poSrcLayer->GetLayerDefn()
    5896         121 :                                       ->GetGeomFieldDefn(iSrcGeomField)
    5897         121 :                                       ->GetSpatialRef();
    5898             :                 else
    5899         751 :                     poSourceSRS = poSrcLayer->GetSpatialRef();
    5900             :             }
    5901             :         }
    5902         881 :         if (poSourceSRS == nullptr)
    5903             :         {
    5904         362 :             if (poFeature == nullptr)
    5905             :             {
    5906           1 :                 if (bVerboseError)
    5907             :                 {
    5908           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    5909             :                              "Non-null feature expected to set transformation");
    5910             :                 }
    5911           1 :                 return false;
    5912             :             }
    5913             :             OGRGeometry *poSrcGeometry =
    5914         361 :                 poFeature->GetGeomFieldRef(iSrcGeomField);
    5915         361 :             if (poSrcGeometry)
    5916         289 :                 poSourceSRS = poSrcGeometry->getSpatialReference();
    5917         361 :             psInfo->m_bPerFeatureCT = (bTransform || bWrapDateline);
    5918             :         }
    5919             : 
    5920         880 :         if (bTransform)
    5921             :         {
    5922          39 :             if (poSourceSRS == nullptr && psInfo->m_pszCTPipeline == nullptr)
    5923             :             {
    5924           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    5925             :                          "Can't transform coordinates, source layer has no\n"
    5926             :                          "coordinate system.  Use -s_srs to set one.");
    5927             : 
    5928           0 :                 return false;
    5929             :             }
    5930             : 
    5931          39 :             if (psInfo->m_pszCTPipeline == nullptr)
    5932             :             {
    5933          35 :                 CPLAssert(nullptr != poSourceSRS);
    5934          35 :                 CPLAssert(nullptr != poOutputSRS);
    5935             :             }
    5936             : 
    5937          39 :             if (psInfo->m_nFeaturesRead == 0 && !psInfo->m_bPerFeatureCT)
    5938             :             {
    5939             :                 const auto &supportedSRSList =
    5940          37 :                     poSrcLayer->GetSupportedSRSList(iGeom);
    5941          37 :                 if (!supportedSRSList.empty())
    5942             :                 {
    5943           1 :                     const char *const apszOptions[] = {
    5944             :                         "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES", nullptr};
    5945           1 :                     for (const auto &poSRS : supportedSRSList)
    5946             :                     {
    5947           1 :                         if (poSRS->IsSame(poOutputSRS, apszOptions))
    5948             :                         {
    5949           2 :                             OGRSpatialReference oSourceSRSBackup;
    5950           1 :                             if (poSourceSRS)
    5951           1 :                                 oSourceSRSBackup = *poSourceSRS;
    5952           1 :                             if (poSrcLayer->SetActiveSRS(iGeom, poSRS.get()) ==
    5953             :                                 OGRERR_NONE)
    5954             :                             {
    5955           1 :                                 CPLDebug("ogr2ogr",
    5956             :                                          "Switching layer active SRS to %s",
    5957             :                                          poSRS->GetName());
    5958             : 
    5959           1 :                                 if (psInfo->m_hSpatialFilter != nullptr &&
    5960           0 :                                     ((psInfo->m_iRequestedSrcGeomField < 0 &&
    5961           0 :                                       iGeom == 0) ||
    5962             :                                      (iGeom ==
    5963           0 :                                       psInfo->m_iRequestedSrcGeomField)))
    5964             :                                 {
    5965           0 :                                     OGRSpatialReference oSpatSRS;
    5966           0 :                                     oSpatSRS.SetAxisMappingStrategy(
    5967             :                                         OAMS_TRADITIONAL_GIS_ORDER);
    5968           0 :                                     if (psInfo->m_pszSpatSRSDef)
    5969           0 :                                         oSpatSRS.SetFromUserInput(
    5970             :                                             psInfo->m_pszSpatSRSDef);
    5971           0 :                                     ApplySpatialFilter(
    5972             :                                         poSrcLayer,
    5973             :                                         OGRGeometry::FromHandle(
    5974             :                                             psInfo->m_hSpatialFilter),
    5975           0 :                                         !oSpatSRS.IsEmpty() ? &oSpatSRS
    5976           0 :                                         : !oSourceSRSBackup.IsEmpty()
    5977             :                                             ? &oSourceSRSBackup
    5978             :                                             : nullptr,
    5979             :                                         psInfo->m_pszGeomField, poOutputSRS);
    5980             :                                 }
    5981             : 
    5982           1 :                                 bTransform = false;
    5983             :                             }
    5984           1 :                             break;
    5985             :                         }
    5986             :                     }
    5987             :                 }
    5988             :             }
    5989             : 
    5990          39 :             if (!bTransform)
    5991             :             {
    5992             :                 // do nothing
    5993             :             }
    5994          39 :             else if (psInfo->m_aoReprojectionInfo[iGeom].m_poCT != nullptr &&
    5995           1 :                      psInfo->m_aoReprojectionInfo[iGeom]
    5996           1 :                              .m_poCT->GetSourceCS() == poSourceSRS)
    5997             :             {
    5998           0 :                 poCT = psInfo->m_aoReprojectionInfo[iGeom].m_poCT.get();
    5999             :             }
    6000             :             else
    6001             :             {
    6002          38 :                 OGRCoordinateTransformationOptions options;
    6003          38 :                 if (psInfo->m_pszCTPipeline)
    6004             :                 {
    6005           4 :                     options.SetCoordinateOperation(psInfo->m_pszCTPipeline,
    6006             :                                                    false);
    6007             :                 }
    6008             : 
    6009             :                 bool bWarnAboutDifferentCoordinateOperations =
    6010          75 :                     poGCPCoordTrans == nullptr &&
    6011          37 :                     !(poSourceSRS && poSourceSRS->IsGeocentric());
    6012             : 
    6013           0 :                 for (const auto &[key, value] :
    6014          38 :                      cpl::IterateNameValue(psInfo->m_aosCTOptions))
    6015             :                 {
    6016           0 :                     if (EQUAL(key, "ALLOW_BALLPARK"))
    6017             :                     {
    6018           0 :                         options.SetBallparkAllowed(CPLTestBool(value));
    6019             :                     }
    6020           0 :                     else if (EQUAL(key, "ONLY_BEST"))
    6021             :                     {
    6022           0 :                         options.SetOnlyBest(CPLTestBool(value));
    6023             :                     }
    6024           0 :                     else if (EQUAL(key, "WARN_ABOUT_DIFFERENT_COORD_OP"))
    6025             :                     {
    6026           0 :                         if (!CPLTestBool(value))
    6027           0 :                             bWarnAboutDifferentCoordinateOperations = false;
    6028             :                     }
    6029             :                     else
    6030             :                     {
    6031           0 :                         CPLError(CE_Warning, CPLE_AppDefined,
    6032             :                                  "Unknown coordinate transform option: %s",
    6033             :                                  key);
    6034             :                     }
    6035             :                 }
    6036             : 
    6037             :                 auto poNewCT = std::unique_ptr<OGRCoordinateTransformation>(
    6038             :                     OGRCreateCoordinateTransformation(poSourceSRS, poOutputSRS,
    6039          38 :                                                       options));
    6040          38 :                 if (poNewCT == nullptr)
    6041             :                 {
    6042           0 :                     char *pszWKT = nullptr;
    6043             : 
    6044           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    6045             :                              "Failed to create coordinate transformation "
    6046             :                              "between the\n"
    6047             :                              "following coordinate systems.  This may be "
    6048             :                              "because they\n"
    6049             :                              "are not transformable.");
    6050             : 
    6051           0 :                     if (poSourceSRS)
    6052             :                     {
    6053           0 :                         poSourceSRS->exportToPrettyWkt(&pszWKT, FALSE);
    6054           0 :                         CPLError(CE_Failure, CPLE_AppDefined, "Source:\n%s",
    6055             :                                  pszWKT);
    6056           0 :                         CPLFree(pszWKT);
    6057             :                     }
    6058             : 
    6059           0 :                     if (poOutputSRS)
    6060             :                     {
    6061           0 :                         poOutputSRS->exportToPrettyWkt(&pszWKT, FALSE);
    6062           0 :                         CPLError(CE_Failure, CPLE_AppDefined, "Target:\n%s",
    6063             :                                  pszWKT);
    6064           0 :                         CPLFree(pszWKT);
    6065             :                     }
    6066             : 
    6067           0 :                     return false;
    6068             :                 }
    6069          38 :                 if (poGCPCoordTrans)
    6070             :                 {
    6071           2 :                     poNewCT = std::make_unique<CompositeCT>(poGCPCoordTrans,
    6072           2 :                                                             std::move(poNewCT));
    6073             :                 }
    6074             :                 else
    6075             :                 {
    6076          37 :                     psInfo->m_aoReprojectionInfo[iGeom]
    6077          37 :                         .m_bWarnAboutDifferentCoordinateOperations =
    6078             :                         bWarnAboutDifferentCoordinateOperations;
    6079             :                 }
    6080          38 :                 psInfo->m_aoReprojectionInfo[iGeom].m_poCT = std::move(poNewCT);
    6081          38 :                 poCT = psInfo->m_aoReprojectionInfo[iGeom].m_poCT.get();
    6082          38 :                 psInfo->m_aoReprojectionInfo[iGeom].m_bCanInvalidateValidity =
    6083          75 :                     !(poGCPCoordTrans == nullptr && poSourceSRS &&
    6084          37 :                       poSourceSRS->IsGeographic() && poOutputSRS &&
    6085           4 :                       poOutputSRS->IsGeographic());
    6086             :             }
    6087             :         }
    6088             :         else
    6089             :         {
    6090         841 :             const char *const apszOptions[] = {
    6091             :                 "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES",
    6092             :                 "CRITERION=EQUIVALENT", nullptr};
    6093             :             auto poDstGeomFieldDefnSpatialRef =
    6094         841 :                 poDstGeomFieldDefn->GetSpatialRef();
    6095         482 :             if (poSourceSRS && poDstGeomFieldDefnSpatialRef &&
    6096         396 :                 poSourceSRS->GetDataAxisToSRSAxisMapping() !=
    6097             :                     poDstGeomFieldDefnSpatialRef
    6098        1323 :                         ->GetDataAxisToSRSAxisMapping() &&
    6099           3 :                 poSourceSRS->IsSame(poDstGeomFieldDefnSpatialRef, apszOptions))
    6100             :             {
    6101           0 :                 psInfo->m_aoReprojectionInfo[iGeom].m_poCT =
    6102           0 :                     std::make_unique<CompositeCT>(
    6103           0 :                         std::make_unique<AxisMappingCoordinateTransformation>(
    6104             :                             poSourceSRS->GetDataAxisToSRSAxisMapping(),
    6105             :                             poDstGeomFieldDefnSpatialRef
    6106             :                                 ->GetDataAxisToSRSAxisMapping()),
    6107           0 :                         poGCPCoordTrans);
    6108           0 :                 poCT = psInfo->m_aoReprojectionInfo[iGeom].m_poCT.get();
    6109             :             }
    6110         841 :             else if (poGCPCoordTrans)
    6111             :             {
    6112           5 :                 psInfo->m_aoReprojectionInfo[iGeom].m_poCT =
    6113          10 :                     std::make_unique<CompositeCT>(poGCPCoordTrans, nullptr);
    6114           5 :                 poCT = psInfo->m_aoReprojectionInfo[iGeom].m_poCT.get();
    6115             :             }
    6116             :         }
    6117             : 
    6118         880 :         if (bWrapDateline)
    6119             :         {
    6120           2 :             if (bTransform && poCT != nullptr && poOutputSRS != nullptr &&
    6121           8 :                 poOutputSRS->IsGeographic() &&
    6122           1 :                 !poOutputSRS->IsDerivedGeographic())
    6123             :             {
    6124             :                 papszTransformOptions =
    6125           1 :                     CSLAddString(papszTransformOptions, "WRAPDATELINE=YES");
    6126           1 :                 if (!osDateLineOffset.empty())
    6127             :                 {
    6128           1 :                     CPLString soOffset("DATELINEOFFSET=");
    6129           1 :                     soOffset += osDateLineOffset;
    6130             :                     papszTransformOptions =
    6131           1 :                         CSLAddString(papszTransformOptions, soOffset);
    6132             :                 }
    6133             :             }
    6134           8 :             else if (poSourceSRS != nullptr && poSourceSRS->IsGeographic() &&
    6135           4 :                      !poSourceSRS->IsDerivedGeographic())
    6136             :             {
    6137             :                 papszTransformOptions =
    6138           4 :                     CSLAddString(papszTransformOptions, "WRAPDATELINE=YES");
    6139           4 :                 if (!osDateLineOffset.empty())
    6140             :                 {
    6141           4 :                     CPLString soOffset("DATELINEOFFSET=");
    6142           4 :                     soOffset += osDateLineOffset;
    6143             :                     papszTransformOptions =
    6144           4 :                         CSLAddString(papszTransformOptions, soOffset);
    6145             :                 }
    6146             :             }
    6147             :             else
    6148             :             {
    6149           0 :                 CPLErrorOnce(CE_Failure, CPLE_IllegalArg,
    6150             :                              "-wrapdateline option only works when "
    6151             :                              "reprojecting to a geographic SRS");
    6152             :             }
    6153             : 
    6154           5 :             psInfo->m_aoReprojectionInfo[iGeom].m_aosTransformOptions.Assign(
    6155           5 :                 papszTransformOptions);
    6156             :         }
    6157             :     }
    6158         951 :     return true;
    6159             : }
    6160             : 
    6161             : /************************************************************************/
    6162             : /*                  LayerTranslator::TranslateArrow()                   */
    6163             : /************************************************************************/
    6164             : 
    6165         167 : bool LayerTranslator::TranslateArrow(
    6166             :     TargetLayerInfo *psInfo, GIntBig nCountLayerFeatures,
    6167             :     GIntBig *pnReadFeatureCount, GDALProgressFunc pfnProgress,
    6168             :     void *pProgressArg, const GDALVectorTranslateOptions *psOptions)
    6169             : {
    6170             :     struct ArrowSchema schema;
    6171         334 :     CPLStringList aosOptionsWriteArrowBatch;
    6172         167 :     if (psInfo->m_bPreserveFID)
    6173             :     {
    6174             :         aosOptionsWriteArrowBatch.SetNameValue(
    6175          30 :             "FID", psInfo->m_poSrcLayer->GetFIDColumn());
    6176             :         aosOptionsWriteArrowBatch.SetNameValue("IF_FID_NOT_PRESERVED",
    6177          30 :                                                "WARNING");
    6178             :     }
    6179             : 
    6180         167 :     if (psInfo->m_sArrowArrayStream.get_schema(&schema) != 0)
    6181             :     {
    6182           0 :         CPLError(CE_Failure, CPLE_AppDefined, "stream.get_schema() failed");
    6183           0 :         return false;
    6184             :     }
    6185             : 
    6186         167 :     int iArrowGeomFieldIndex = -1;
    6187         167 :     if (m_bTransform)
    6188             :     {
    6189          12 :         iArrowGeomFieldIndex = GetArrowGeomFieldIndex(
    6190          12 :             &schema, psInfo->m_poSrcLayer->GetGeometryColumn());
    6191          12 :         if (!SetupCT(psInfo, psInfo->m_poSrcLayer, m_bTransform,
    6192          12 :                      m_bWrapDateline, m_osDateLineOffset, m_poUserSourceSRS,
    6193             :                      nullptr, m_poOutputSRS, m_poGCPCoordTrans, false))
    6194             :         {
    6195           0 :             return false;
    6196             :         }
    6197             :     }
    6198             : 
    6199         167 :     bool bRet = true;
    6200             : 
    6201         167 :     GIntBig nCount = 0;
    6202         167 :     bool bGoOn = true;
    6203         167 :     std::vector<GByte> abyModifiedWKB;
    6204         167 :     const int nNumReprojectionThreads = []()
    6205             :     {
    6206         167 :         const char *pszNumThreads = nullptr;
    6207             :         int nVal =
    6208         167 :             GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
    6209             :                               /* bDefaultToAllCPUs = */ false, &pszNumThreads);
    6210         167 :         if (!pszNumThreads)
    6211           0 :             nVal = std::max(1, CPLGetNumCPUs() / 2);
    6212         167 :         return nVal;
    6213         167 :     }();
    6214             : 
    6215             :     // Somewhat arbitrary threshold (config option only/mostly for autotest purposes)
    6216         167 :     const int MIN_FEATURES_FOR_THREADED_REPROJ = atoi(CPLGetConfigOption(
    6217             :         "OGR2OGR_MIN_FEATURES_FOR_THREADED_REPROJ", "10000"));
    6218             : 
    6219         325 :     while (bGoOn)
    6220             :     {
    6221             :         struct ArrowArray array;
    6222             :         // Acquire source batch
    6223         323 :         if (psInfo->m_sArrowArrayStream.get_next(&array) != 0)
    6224             :         {
    6225           0 :             CPLError(CE_Failure, CPLE_AppDefined, "stream.get_next() failed");
    6226           0 :             bRet = false;
    6227         165 :             break;
    6228             :         }
    6229             : 
    6230         323 :         if (array.release == nullptr)
    6231             :         {
    6232             :             // End of stream
    6233         165 :             break;
    6234             :         }
    6235             : 
    6236             :         // Limit number of features in batch if needed
    6237         158 :         if (psOptions->nLimit >= 0 &&
    6238           2 :             nCount + array.length >= psOptions->nLimit)
    6239             :         {
    6240           2 :             const auto nAdjustedLength = psOptions->nLimit - nCount;
    6241          14 :             for (int i = 0; i < array.n_children; ++i)
    6242             :             {
    6243          12 :                 if (array.children[i]->length == array.length)
    6244          12 :                     array.children[i]->length = nAdjustedLength;
    6245             :             }
    6246           2 :             array.length = nAdjustedLength;
    6247           2 :             nCount = psOptions->nLimit;
    6248           2 :             bGoOn = false;
    6249             :         }
    6250             :         else
    6251             :         {
    6252         156 :             nCount += array.length;
    6253             :         }
    6254             : 
    6255         158 :         const auto nArrayLength = array.length;
    6256             : 
    6257             :         // Coordinate reprojection
    6258         158 :         if (m_bTransform)
    6259             :         {
    6260             :             struct GeomArrayReleaser
    6261             :             {
    6262             :                 const void *origin_buffers_2 = nullptr;
    6263             :                 void (*origin_release)(struct ArrowArray *) = nullptr;
    6264             :                 void *origin_private_data = nullptr;
    6265             : 
    6266          11 :                 static void init(struct ArrowArray *psGeomArray)
    6267             :                 {
    6268          11 :                     GeomArrayReleaser *releaser = new GeomArrayReleaser();
    6269          11 :                     CPLAssert(psGeomArray->n_buffers >= 3);
    6270          11 :                     releaser->origin_buffers_2 = psGeomArray->buffers[2];
    6271          11 :                     releaser->origin_private_data = psGeomArray->private_data;
    6272          11 :                     releaser->origin_release = psGeomArray->release;
    6273          11 :                     psGeomArray->release = GeomArrayReleaser::release;
    6274          11 :                     psGeomArray->private_data = releaser;
    6275          11 :                 }
    6276             : 
    6277          11 :                 static void release(struct ArrowArray *psGeomArray)
    6278             :                 {
    6279          11 :                     GeomArrayReleaser *releaser =
    6280             :                         static_cast<GeomArrayReleaser *>(
    6281             :                             psGeomArray->private_data);
    6282          11 :                     psGeomArray->buffers[2] = releaser->origin_buffers_2;
    6283          11 :                     psGeomArray->private_data = releaser->origin_private_data;
    6284          11 :                     psGeomArray->release = releaser->origin_release;
    6285          11 :                     if (psGeomArray->release)
    6286          11 :                         psGeomArray->release(psGeomArray);
    6287          11 :                     delete releaser;
    6288          11 :                 }
    6289             :             };
    6290             : 
    6291          11 :             auto *psGeomArray = array.children[iArrowGeomFieldIndex];
    6292          11 :             GeomArrayReleaser::init(psGeomArray);
    6293             : 
    6294          11 :             GByte *pabyWKB = static_cast<GByte *>(
    6295          11 :                 const_cast<void *>(psGeomArray->buffers[2]));
    6296          11 :             const uint32_t *panOffsets =
    6297          11 :                 static_cast<const uint32_t *>(psGeomArray->buffers[1]);
    6298          11 :             auto poCT = psInfo->m_aoReprojectionInfo[0].m_poCT.get();
    6299             : 
    6300             :             try
    6301             :             {
    6302          11 :                 abyModifiedWKB.resize(panOffsets[nArrayLength]);
    6303             :             }
    6304           0 :             catch (const std::exception &)
    6305             :             {
    6306           0 :                 CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
    6307           0 :                 bRet = false;
    6308           0 :                 if (array.release)
    6309           0 :                     array.release(&array);
    6310           0 :                 break;
    6311             :             }
    6312          11 :             memcpy(abyModifiedWKB.data(), pabyWKB, panOffsets[nArrayLength]);
    6313          11 :             psGeomArray->buffers[2] = abyModifiedWKB.data();
    6314             : 
    6315             :             // Collect left-most, right-most, top-most, bottom-most coordinates.
    6316          11 :             if (psInfo->m_aoReprojectionInfo[0]
    6317          11 :                     .m_bWarnAboutDifferentCoordinateOperations)
    6318             :             {
    6319             :                 struct OGRWKBPointVisitor final : public OGRWKBPointUpdater
    6320             :                 {
    6321             :                     TargetLayerInfo::ReprojectionInfo &m_info;
    6322             : 
    6323          11 :                     explicit OGRWKBPointVisitor(
    6324             :                         TargetLayerInfo::ReprojectionInfo &info)
    6325          11 :                         : m_info(info)
    6326             :                     {
    6327          11 :                     }
    6328             : 
    6329       10262 :                     bool update(bool bNeedSwap, void *x, void *y, void *z,
    6330             :                                 void * /* m */) override
    6331             :                     {
    6332             :                         double dfX, dfY, dfZ;
    6333       10262 :                         memcpy(&dfX, x, sizeof(double));
    6334       10262 :                         memcpy(&dfY, y, sizeof(double));
    6335       10262 :                         if (bNeedSwap)
    6336             :                         {
    6337           0 :                             CPL_SWAP64PTR(&dfX);
    6338           0 :                             CPL_SWAP64PTR(&dfY);
    6339             :                         }
    6340       10262 :                         if (z)
    6341             :                         {
    6342           0 :                             memcpy(&dfZ, z, sizeof(double));
    6343           0 :                             if (bNeedSwap)
    6344             :                             {
    6345           0 :                                 CPL_SWAP64PTR(&dfZ);
    6346             :                             }
    6347             :                         }
    6348             :                         else
    6349       10262 :                             dfZ = 0;
    6350       10262 :                         m_info.UpdateExtremePoints(dfX, dfY, dfZ);
    6351       10262 :                         return true;
    6352             :                     }
    6353             :                 };
    6354             : 
    6355          22 :                 OGRWKBPointVisitor oVisitor(psInfo->m_aoReprojectionInfo[0]);
    6356          11 :                 const GByte *pabyValidity =
    6357          11 :                     static_cast<const GByte *>(psGeomArray->buffers[0]);
    6358             : 
    6359       10046 :                 for (size_t i = 0; i < static_cast<size_t>(nArrayLength); ++i)
    6360             :                 {
    6361       10035 :                     const size_t iShifted =
    6362       10035 :                         static_cast<size_t>(i + psGeomArray->offset);
    6363       10035 :                     if (!pabyValidity || (pabyValidity[iShifted >> 8] &
    6364          24 :                                           (1 << (iShifted % 8))) != 0)
    6365             :                     {
    6366       10027 :                         const auto nWKBSize =
    6367       10027 :                             panOffsets[iShifted + 1] - panOffsets[iShifted];
    6368       10027 :                         OGRWKBUpdatePoints(abyModifiedWKB.data() +
    6369       10027 :                                                panOffsets[iShifted],
    6370             :                                            nWKBSize, oVisitor);
    6371             :                     }
    6372             :                 }
    6373             :             }
    6374             : 
    6375          11 :             std::atomic<bool> atomicRet{true};
    6376             :             const auto oReprojectionLambda =
    6377          11 :                 [psGeomArray, nArrayLength, panOffsets, &atomicRet,
    6378       40138 :                  &abyModifiedWKB, &poCT](int iThread, int nThreads)
    6379             :             {
    6380             :                 OGRWKBTransformCache oCache;
    6381          11 :                 OGREnvelope3D sEnv3D;
    6382             :                 auto poThisCT =
    6383          11 :                     std::unique_ptr<OGRCoordinateTransformation>(poCT->Clone());
    6384          11 :                 if (!poThisCT)
    6385             :                 {
    6386           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    6387             :                              "Cannot clone OGRCoordinateTransformation");
    6388           0 :                     atomicRet = false;
    6389           0 :                     return;
    6390             :                 }
    6391             : 
    6392          11 :                 const GByte *pabyValidity =
    6393          11 :                     static_cast<const GByte *>(psGeomArray->buffers[0]);
    6394          11 :                 const size_t iStart =
    6395          11 :                     static_cast<size_t>(iThread * nArrayLength / nThreads);
    6396          11 :                 const size_t iMax = static_cast<size_t>(
    6397          11 :                     (iThread + 1) * nArrayLength / nThreads);
    6398       10046 :                 for (size_t i = iStart; i < iMax; ++i)
    6399             :                 {
    6400       10035 :                     const size_t iShifted =
    6401       10035 :                         static_cast<size_t>(i + psGeomArray->offset);
    6402       10035 :                     if (!pabyValidity || (pabyValidity[iShifted >> 8] &
    6403          24 :                                           (1 << (iShifted % 8))) != 0)
    6404             :                     {
    6405       10027 :                         const auto nWKBSize =
    6406       10027 :                             panOffsets[iShifted + 1] - panOffsets[iShifted];
    6407       20054 :                         if (!OGRWKBTransform(
    6408       10027 :                                 abyModifiedWKB.data() + panOffsets[iShifted],
    6409             :                                 nWKBSize, poThisCT.get(), oCache, sEnv3D))
    6410             :                         {
    6411           0 :                             CPLError(CE_Failure, CPLE_AppDefined,
    6412             :                                      "Reprojection failed");
    6413           0 :                             atomicRet = false;
    6414           0 :                             break;
    6415             :                         }
    6416             :                     }
    6417             :                 }
    6418          11 :             };
    6419             : 
    6420          11 :             if (nArrayLength >= MIN_FEATURES_FOR_THREADED_REPROJ &&
    6421           5 :                 nNumReprojectionThreads >= 2)
    6422             :             {
    6423           0 :                 std::vector<std::future<void>> oTasks;
    6424           0 :                 for (int iThread = 0; iThread < nNumReprojectionThreads;
    6425             :                      ++iThread)
    6426             :                 {
    6427           0 :                     oTasks.emplace_back(std::async(std::launch::async,
    6428             :                                                    oReprojectionLambda, iThread,
    6429           0 :                                                    nNumReprojectionThreads));
    6430             :                 }
    6431           0 :                 for (auto &oTask : oTasks)
    6432             :                 {
    6433           0 :                     oTask.get();
    6434           0 :                 }
    6435             :             }
    6436             :             else
    6437             :             {
    6438          11 :                 oReprojectionLambda(0, 1);
    6439             :             }
    6440             : 
    6441          11 :             bRet = atomicRet;
    6442          11 :             if (!bRet)
    6443             :             {
    6444           0 :                 if (array.release)
    6445           0 :                     array.release(&array);
    6446           0 :                 break;
    6447             :             }
    6448             :         }
    6449             : 
    6450             :         // Write batch to target layer
    6451         158 :         const bool bWriteOK = psInfo->m_poDstLayer->WriteArrowBatch(
    6452         158 :             &schema, &array, aosOptionsWriteArrowBatch.List());
    6453             : 
    6454         158 :         if (array.release)
    6455          38 :             array.release(&array);
    6456             : 
    6457         158 :         if (!bWriteOK)
    6458             :         {
    6459           0 :             CPLError(CE_Failure, CPLE_AppDefined, "WriteArrowBatch() failed");
    6460           0 :             bRet = false;
    6461           0 :             break;
    6462             :         }
    6463             : 
    6464             :         /* Report progress */
    6465         158 :         if (pfnProgress)
    6466             :         {
    6467           0 :             if (!pfnProgress(nCountLayerFeatures
    6468           0 :                                  ? nCount * 1.0 / nCountLayerFeatures
    6469             :                                  : 1.0,
    6470             :                              "", pProgressArg))
    6471             :             {
    6472           0 :                 bGoOn = false;
    6473           0 :                 bRet = false;
    6474             :             }
    6475             :         }
    6476             : 
    6477         158 :         if (pnReadFeatureCount)
    6478           0 :             *pnReadFeatureCount = nCount;
    6479             :     }
    6480             : 
    6481         167 :     schema.release(&schema);
    6482             : 
    6483         167 :     return bRet;
    6484             : }
    6485             : 
    6486             : /************************************************************************/
    6487             : /*                     LayerTranslator::Translate()                     */
    6488             : /************************************************************************/
    6489             : 
    6490        2019 : bool LayerTranslator::Translate(
    6491             :     std::unique_ptr<OGRFeature> poFeatureIn, TargetLayerInfo *psInfo,
    6492             :     GIntBig nCountLayerFeatures, GIntBig *pnReadFeatureCount,
    6493             :     GIntBig &nTotalEventsDone, GDALProgressFunc pfnProgress, void *pProgressArg,
    6494             :     const GDALVectorTranslateOptions *psOptions)
    6495             : {
    6496        2019 :     if (psInfo->m_bUseWriteArrowBatch)
    6497             :     {
    6498         167 :         return TranslateArrow(psInfo, nCountLayerFeatures, pnReadFeatureCount,
    6499         167 :                               pfnProgress, pProgressArg, psOptions);
    6500             :     }
    6501             : 
    6502        1852 :     const int eGType = m_eGType;
    6503        1852 :     const OGRSpatialReference *poOutputSRS = m_poOutputSRS;
    6504             : 
    6505        1852 :     OGRLayer *poSrcLayer = psInfo->m_poSrcLayer;
    6506        1852 :     OGRLayer *poDstLayer = psInfo->m_poDstLayer;
    6507        1852 :     const int *const panMap = psInfo->m_anMap.data();
    6508        1852 :     const int iSrcZField = psInfo->m_iSrcZField;
    6509        1852 :     const bool bPreserveFID = psInfo->m_bPreserveFID;
    6510        1852 :     const auto poSrcFDefn = poSrcLayer->GetLayerDefn();
    6511        1852 :     const auto poDstFDefn = poDstLayer->GetLayerDefn();
    6512        1852 :     const int nSrcGeomFieldCount = poSrcFDefn->GetGeomFieldCount();
    6513        1852 :     const int nDstGeomFieldCount = poDstFDefn->GetGeomFieldCount();
    6514        1852 :     const bool bExplodeCollections =
    6515        1852 :         m_bExplodeCollections && nDstGeomFieldCount <= 1;
    6516        1852 :     const int iRequestedSrcGeomField = psInfo->m_iRequestedSrcGeomField;
    6517             : 
    6518        1852 :     if (poOutputSRS == nullptr && !m_bNullifyOutputSRS)
    6519             :     {
    6520        1819 :         if (nSrcGeomFieldCount == 1)
    6521             :         {
    6522         777 :             poOutputSRS = poSrcLayer->GetSpatialRef();
    6523             :         }
    6524        1042 :         else if (iRequestedSrcGeomField > 0)
    6525             :         {
    6526           1 :             poOutputSRS = poSrcLayer->GetLayerDefn()
    6527           1 :                               ->GetGeomFieldDefn(iRequestedSrcGeomField)
    6528           1 :                               ->GetSpatialRef();
    6529             :         }
    6530             :     }
    6531             : 
    6532             :     /* -------------------------------------------------------------------- */
    6533             :     /*      Transfer features.                                              */
    6534             :     /* -------------------------------------------------------------------- */
    6535        1852 :     if (psOptions->nGroupTransactions)
    6536             :     {
    6537        1851 :         if (psOptions->nLayerTransaction)
    6538             :         {
    6539         728 :             if (poDstLayer->StartTransaction() == OGRERR_FAILURE)
    6540             :             {
    6541           0 :                 return false;
    6542             :             }
    6543             :         }
    6544             :     }
    6545             : 
    6546        1852 :     std::unique_ptr<OGRFeature> poFeature;
    6547        3704 :     auto poDstFeature = std::make_unique<OGRFeature>(poDstFDefn);
    6548        1852 :     int nFeaturesInTransaction = 0;
    6549        1852 :     GIntBig nCount = 0; /* written + failed */
    6550        1852 :     GIntBig nFeaturesWritten = 0;
    6551        1852 :     bool bRunSetPrecisionEvaluated = false;
    6552        1852 :     bool bRunSetPrecision = false;
    6553             : 
    6554        1852 :     bool bRet = true;
    6555        1852 :     CPLErrorReset();
    6556             : 
    6557        1852 :     bool bSetupCTOK = false;
    6558        1852 :     if (m_bTransform && psInfo->m_nFeaturesRead == 0 &&
    6559          26 :         !psInfo->m_bPerFeatureCT)
    6560             :     {
    6561          26 :         bSetupCTOK = SetupCT(psInfo, poSrcLayer, m_bTransform, m_bWrapDateline,
    6562          26 :                              m_osDateLineOffset, m_poUserSourceSRS, nullptr,
    6563             :                              poOutputSRS, m_poGCPCoordTrans, false);
    6564             :     }
    6565             : 
    6566        1852 :     const bool bSingleIteration = poFeatureIn != nullptr;
    6567             :     while (true)
    6568             :     {
    6569       15326 :         if (m_nLimit >= 0 && psInfo->m_nFeaturesRead >= m_nLimit)
    6570             :         {
    6571           9 :             break;
    6572             :         }
    6573             : 
    6574       15317 :         if (poFeatureIn != nullptr)
    6575         974 :             poFeature = std::move(poFeatureIn);
    6576       14343 :         else if (psOptions->nFIDToFetch != OGRNullFID)
    6577           5 :             poFeature.reset(poSrcLayer->GetFeature(psOptions->nFIDToFetch));
    6578             :         else
    6579       14338 :             poFeature.reset(poSrcLayer->GetNextFeature());
    6580             : 
    6581       15317 :         if (poFeature == nullptr)
    6582             :         {
    6583         857 :             if (CPLGetLastErrorType() == CE_Failure)
    6584             :             {
    6585           8 :                 bRet = false;
    6586             :             }
    6587         857 :             break;
    6588             :         }
    6589             : 
    6590       14460 :         if (!bSetupCTOK &&
    6591       14349 :             (psInfo->m_nFeaturesRead == 0 || psInfo->m_bPerFeatureCT))
    6592             :         {
    6593        1828 :             if (!SetupCT(psInfo, poSrcLayer, m_bTransform, m_bWrapDateline,
    6594         914 :                          m_osDateLineOffset, m_poUserSourceSRS, poFeature.get(),
    6595             :                          poOutputSRS, m_poGCPCoordTrans, true))
    6596             :             {
    6597           6 :                 return false;
    6598             :             }
    6599             :         }
    6600             : 
    6601       14460 :         psInfo->m_nFeaturesRead++;
    6602             : 
    6603       14460 :         int nIters = 1;
    6604           0 :         std::unique_ptr<OGRGeometryCollection> poCollToExplode;
    6605       14460 :         int iGeomCollToExplode = -1;
    6606       14460 :         OGRGeometry *poSrcGeometry = nullptr;
    6607       14460 :         if (bExplodeCollections)
    6608             :         {
    6609          13 :             if (iRequestedSrcGeomField >= 0)
    6610             :                 poSrcGeometry =
    6611           0 :                     poFeature->GetGeomFieldRef(iRequestedSrcGeomField);
    6612             :             else
    6613          13 :                 poSrcGeometry = poFeature->GetGeometryRef();
    6614          26 :             if (poSrcGeometry &&
    6615          13 :                 OGR_GT_IsSubClassOf(poSrcGeometry->getGeometryType(),
    6616             :                                     wkbGeometryCollection))
    6617             :             {
    6618             :                 const int nParts =
    6619          12 :                     poSrcGeometry->toGeometryCollection()->getNumGeometries();
    6620          21 :                 if (nParts > 0 ||
    6621           9 :                     wkbFlatten(poSrcGeometry->getGeometryType()) !=
    6622             :                         wkbGeometryCollection)
    6623             :                 {
    6624          11 :                     iGeomCollToExplode = iRequestedSrcGeomField >= 0
    6625             :                                              ? iRequestedSrcGeomField
    6626             :                                              : 0;
    6627          11 :                     poCollToExplode.reset(
    6628             :                         poFeature->StealGeometry(iGeomCollToExplode)
    6629             :                             ->toGeometryCollection());
    6630          11 :                     nIters = std::max(1, nParts);
    6631             :                 }
    6632             :             }
    6633             :         }
    6634             : 
    6635       14460 :         const GIntBig nSrcFID = poFeature->GetFID();
    6636       14460 :         GIntBig nDesiredFID = OGRNullFID;
    6637       14460 :         if (bPreserveFID)
    6638        1164 :             nDesiredFID = nSrcFID;
    6639       13297 :         else if (psInfo->m_iSrcFIDField >= 0 &&
    6640           1 :                  poFeature->IsFieldSetAndNotNull(psInfo->m_iSrcFIDField))
    6641             :             nDesiredFID =
    6642           1 :                 poFeature->GetFieldAsInteger64(psInfo->m_iSrcFIDField);
    6643             : 
    6644       28917 :         for (int iPart = 0; iPart < nIters; iPart++)
    6645             :         {
    6646       20566 :             if (psOptions->nLayerTransaction &&
    6647        6103 :                 ++nFeaturesInTransaction == psOptions->nGroupTransactions)
    6648             :             {
    6649          36 :                 if (poDstLayer->CommitTransaction() == OGRERR_FAILURE ||
    6650          18 :                     poDstLayer->StartTransaction() == OGRERR_FAILURE)
    6651             :                 {
    6652           0 :                     return false;
    6653             :                 }
    6654          18 :                 nFeaturesInTransaction = 0;
    6655             :             }
    6656       37250 :             else if (!psOptions->nLayerTransaction &&
    6657       22785 :                      psOptions->nGroupTransactions > 0 &&
    6658        8340 :                      ++nTotalEventsDone >= psOptions->nGroupTransactions)
    6659             :             {
    6660          80 :                 if (m_poODS->CommitTransaction() == OGRERR_FAILURE ||
    6661          40 :                     m_poODS->StartTransaction(psOptions->bForceTransaction) ==
    6662             :                         OGRERR_FAILURE)
    6663             :                 {
    6664           0 :                     return false;
    6665             :                 }
    6666          40 :                 nTotalEventsDone = 0;
    6667             :             }
    6668             : 
    6669       14463 :             CPLErrorReset();
    6670       14463 :             if (psInfo->m_bCanAvoidSetFrom)
    6671             :             {
    6672       14164 :                 poDstFeature = std::move(poFeature);
    6673             :                 // From now on, poFeature is null !
    6674       14164 :                 poDstFeature->SetFDefnUnsafe(poDstFDefn);
    6675       14164 :                 poDstFeature->SetFID(nDesiredFID);
    6676             :             }
    6677             :             else
    6678             :             {
    6679             :                 /* Optimization to avoid duplicating the source geometry in the
    6680             :                  */
    6681             :                 /* target feature : we steal it from the source feature for
    6682             :                  * now... */
    6683           0 :                 std::unique_ptr<OGRGeometry> poStolenGeometry;
    6684         299 :                 if (!bExplodeCollections && nSrcGeomFieldCount == 1 &&
    6685         104 :                     (nDstGeomFieldCount == 1 ||
    6686         104 :                      (nDstGeomFieldCount == 0 && m_poClipSrcOri)))
    6687             :                 {
    6688         134 :                     poStolenGeometry.reset(poFeature->StealGeometry());
    6689             :                 }
    6690         165 :                 else if (!bExplodeCollections && iRequestedSrcGeomField >= 0)
    6691             :                 {
    6692           0 :                     poStolenGeometry.reset(
    6693             :                         poFeature->StealGeometry(iRequestedSrcGeomField));
    6694             :                 }
    6695             : 
    6696         299 :                 if (nDstGeomFieldCount == 0 && poStolenGeometry &&
    6697           0 :                     m_poClipSrcOri)
    6698             :                 {
    6699           0 :                     if (poStolenGeometry->IsEmpty())
    6700           0 :                         goto end_loop;
    6701             : 
    6702             :                     const auto clipGeomDesc =
    6703           0 :                         GetSrcClipGeom(poStolenGeometry->getSpatialReference());
    6704             : 
    6705           0 :                     if (clipGeomDesc.poGeom && clipGeomDesc.poEnv)
    6706             :                     {
    6707           0 :                         OGREnvelope oEnv;
    6708           0 :                         poStolenGeometry->getEnvelope(&oEnv);
    6709           0 :                         if (!clipGeomDesc.poEnv->Contains(oEnv) &&
    6710           0 :                             !(clipGeomDesc.poEnv->Intersects(oEnv) &&
    6711           0 :                               clipGeomDesc.poGeom->Intersects(
    6712           0 :                                   poStolenGeometry.get())))
    6713             :                         {
    6714           0 :                             goto end_loop;
    6715             :                         }
    6716             :                     }
    6717             :                 }
    6718             : 
    6719         299 :                 poDstFeature->Reset();
    6720             : 
    6721         598 :                 if (poDstFeature->SetFrom(
    6722         299 :                         poFeature.get(), panMap, /* bForgiving = */ TRUE,
    6723         299 :                         /* bUseISO8601ForDateTimeAsString = */ true) !=
    6724             :                     OGRERR_NONE)
    6725             :                 {
    6726           0 :                     if (psOptions->nGroupTransactions)
    6727             :                     {
    6728           0 :                         if (psOptions->nLayerTransaction)
    6729             :                         {
    6730           0 :                             if (poDstLayer->CommitTransaction() != OGRERR_NONE)
    6731             :                             {
    6732           0 :                                 return false;
    6733             :                             }
    6734             :                         }
    6735             :                     }
    6736             : 
    6737           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    6738             :                              "Unable to translate feature " CPL_FRMT_GIB
    6739             :                              " from layer %s.",
    6740           0 :                              nSrcFID, poSrcLayer->GetName());
    6741             : 
    6742           0 :                     return false;
    6743             :                 }
    6744             : 
    6745             :                 /* ... and now we can attach the stolen geometry */
    6746         299 :                 if (poStolenGeometry)
    6747             :                 {
    6748         127 :                     poDstFeature->SetGeometryDirectly(
    6749             :                         poStolenGeometry.release());
    6750             :                 }
    6751             : 
    6752         299 :                 if (!psInfo->m_oMapResolved.empty())
    6753             :                 {
    6754           4 :                     for (const auto &kv : psInfo->m_oMapResolved)
    6755             :                     {
    6756           2 :                         const int nDstField = kv.first;
    6757           2 :                         const int nSrcField = kv.second.nSrcField;
    6758           2 :                         if (poFeature->IsFieldSetAndNotNull(nSrcField))
    6759             :                         {
    6760           2 :                             const auto poDomain = kv.second.poDomain;
    6761             :                             const auto &oMapKV =
    6762           2 :                                 psInfo->m_oMapDomainToKV[poDomain];
    6763             :                             const auto iter = oMapKV.find(
    6764           2 :                                 poFeature->GetFieldAsString(nSrcField));
    6765           2 :                             if (iter != oMapKV.end())
    6766             :                             {
    6767           2 :                                 poDstFeature->SetField(nDstField,
    6768           1 :                                                        iter->second.c_str());
    6769             :                             }
    6770             :                         }
    6771             :                     }
    6772             :                 }
    6773             : 
    6774         299 :                 if (nDesiredFID != OGRNullFID)
    6775           2 :                     poDstFeature->SetFID(nDesiredFID);
    6776             :             }
    6777             : 
    6778       14463 :             if (psOptions->bEmptyStrAsNull)
    6779             :             {
    6780           2 :                 for (int i = 0; i < poDstFeature->GetFieldCount(); i++)
    6781             :                 {
    6782           1 :                     if (!poDstFeature->IsFieldSetAndNotNull(i))
    6783           0 :                         continue;
    6784           1 :                     auto fieldDef = poDstFeature->GetFieldDefnRef(i);
    6785           1 :                     if (fieldDef->GetType() != OGRFieldType::OFTString)
    6786           0 :                         continue;
    6787           1 :                     auto str = poDstFeature->GetFieldAsString(i);
    6788           1 :                     if (strcmp(str, "") == 0)
    6789           1 :                         poDstFeature->SetFieldNull(i);
    6790             :                 }
    6791             :             }
    6792             : 
    6793       14463 :             if (!psInfo->m_anDateTimeFieldIdx.empty())
    6794             :             {
    6795          40 :                 for (int i : psInfo->m_anDateTimeFieldIdx)
    6796             :                 {
    6797          20 :                     if (!poDstFeature->IsFieldSetAndNotNull(i))
    6798          11 :                         continue;
    6799          15 :                     auto psField = poDstFeature->GetRawFieldRef(i);
    6800          15 :                     if (psField->Date.TZFlag == 0 || psField->Date.TZFlag == 1)
    6801           5 :                         continue;
    6802             : 
    6803          10 :                     const int nTZOffsetInSec =
    6804          10 :                         (psField->Date.TZFlag - 100) * 15 * 60;
    6805          10 :                     if (nTZOffsetInSec == psOptions->nTZOffsetInSec)
    6806           1 :                         continue;
    6807             : 
    6808             :                     struct tm brokendowntime;
    6809           9 :                     memset(&brokendowntime, 0, sizeof(brokendowntime));
    6810           9 :                     brokendowntime.tm_year = psField->Date.Year - 1900;
    6811           9 :                     brokendowntime.tm_mon = psField->Date.Month - 1;
    6812           9 :                     brokendowntime.tm_mday = psField->Date.Day;
    6813           9 :                     GIntBig nUnixTime = CPLYMDHMSToUnixTime(&brokendowntime);
    6814           9 :                     int nSec = psField->Date.Hour * 3600 +
    6815           9 :                                psField->Date.Minute * 60 +
    6816           9 :                                static_cast<int>(psField->Date.Second);
    6817           9 :                     nSec += psOptions->nTZOffsetInSec - nTZOffsetInSec;
    6818           9 :                     nUnixTime += nSec;
    6819           9 :                     CPLUnixTimeToYMDHMS(nUnixTime, &brokendowntime);
    6820             : 
    6821           9 :                     psField->Date.Year =
    6822           9 :                         static_cast<GInt16>(brokendowntime.tm_year + 1900);
    6823           9 :                     psField->Date.Month =
    6824           9 :                         static_cast<GByte>(brokendowntime.tm_mon + 1);
    6825           9 :                     psField->Date.Day =
    6826           9 :                         static_cast<GByte>(brokendowntime.tm_mday);
    6827           9 :                     psField->Date.Hour =
    6828           9 :                         static_cast<GByte>(brokendowntime.tm_hour);
    6829           9 :                     psField->Date.Minute =
    6830           9 :                         static_cast<GByte>(brokendowntime.tm_min);
    6831           9 :                     psField->Date.Second = static_cast<float>(
    6832           9 :                         brokendowntime.tm_sec + fmod(psField->Date.Second, 1));
    6833           9 :                     psField->Date.TZFlag = static_cast<GByte>(
    6834           9 :                         100 + psOptions->nTZOffsetInSec / (15 * 60));
    6835             :                 }
    6836             :             }
    6837             : 
    6838             :             /* Erase native data if asked explicitly */
    6839       14463 :             if (!m_bNativeData)
    6840             :             {
    6841           1 :                 poDstFeature->SetNativeData(nullptr);
    6842           1 :                 poDstFeature->SetNativeMediaType(nullptr);
    6843             :             }
    6844             : 
    6845       22887 :             for (int iGeom = 0; iGeom < nDstGeomFieldCount; iGeom++)
    6846             :             {
    6847           0 :                 std::unique_ptr<OGRGeometry> poDstGeometry;
    6848             : 
    6849        8471 :                 if (poCollToExplode && iGeom == iGeomCollToExplode)
    6850             :                 {
    6851          14 :                     if (poSrcGeometry && poCollToExplode->IsEmpty())
    6852             :                     {
    6853             :                         const OGRwkbGeometryType eSrcType =
    6854           8 :                             poSrcGeometry->getGeometryType();
    6855             :                         const OGRwkbGeometryType eSrcFlattenType =
    6856           8 :                             wkbFlatten(eSrcType);
    6857           8 :                         OGRwkbGeometryType eDstType = eSrcType;
    6858           8 :                         switch (eSrcFlattenType)
    6859             :                         {
    6860           4 :                             case wkbMultiPoint:
    6861           4 :                                 eDstType = wkbPoint;
    6862           4 :                                 break;
    6863           1 :                             case wkbMultiLineString:
    6864           1 :                                 eDstType = wkbLineString;
    6865           1 :                                 break;
    6866           1 :                             case wkbMultiPolygon:
    6867           1 :                                 eDstType = wkbPolygon;
    6868           1 :                                 break;
    6869           1 :                             case wkbMultiCurve:
    6870           1 :                                 eDstType = wkbCompoundCurve;
    6871           1 :                                 break;
    6872           1 :                             case wkbMultiSurface:
    6873           1 :                                 eDstType = wkbCurvePolygon;
    6874           1 :                                 break;
    6875           0 :                             default:
    6876           0 :                                 break;
    6877             :                         }
    6878             :                         eDstType =
    6879           8 :                             OGR_GT_SetModifier(eDstType, OGR_GT_HasZ(eSrcType),
    6880             :                                                OGR_GT_HasM(eSrcType));
    6881           8 :                         poDstGeometry.reset(
    6882             :                             OGRGeometryFactory::createGeometry(eDstType));
    6883             :                     }
    6884             :                     else
    6885             :                     {
    6886             :                         OGRGeometry *poPart =
    6887           6 :                             poCollToExplode->getGeometryRef(0);
    6888           6 :                         poCollToExplode->removeGeometry(0, FALSE);
    6889           6 :                         poDstGeometry.reset(poPart);
    6890             :                     }
    6891             :                 }
    6892             :                 else
    6893             :                 {
    6894        8457 :                     poDstGeometry.reset(poDstFeature->StealGeometry(iGeom));
    6895             :                 }
    6896        8471 :                 if (poDstGeometry == nullptr)
    6897         676 :                     continue;
    6898             : 
    6899             :                 // poFeature hasn't been moved if iSrcZField != -1
    6900             :                 // cppcheck-suppress accessMoved
    6901        7795 :                 if (iSrcZField != -1 && poFeature != nullptr)
    6902             :                 {
    6903          30 :                     SetZ(poDstGeometry.get(),
    6904             :                          poFeature->GetFieldAsDouble(iSrcZField));
    6905             :                     /* This will correct the coordinate dimension to 3 */
    6906          30 :                     poDstGeometry.reset(poDstGeometry->clone());
    6907             :                 }
    6908             : 
    6909        7795 :                 if (m_nCoordDim == 2 || m_nCoordDim == 3)
    6910             :                 {
    6911          24 :                     poDstGeometry->setCoordinateDimension(m_nCoordDim);
    6912             :                 }
    6913        7771 :                 else if (m_nCoordDim == 4)
    6914             :                 {
    6915           2 :                     poDstGeometry->set3D(TRUE);
    6916           2 :                     poDstGeometry->setMeasured(TRUE);
    6917             :                 }
    6918        7769 :                 else if (m_nCoordDim == COORD_DIM_XYM)
    6919             :                 {
    6920           2 :                     poDstGeometry->set3D(FALSE);
    6921           2 :                     poDstGeometry->setMeasured(TRUE);
    6922             :                 }
    6923        7767 :                 else if (m_nCoordDim == COORD_DIM_LAYER_DIM)
    6924             :                 {
    6925             :                     const OGRwkbGeometryType eDstLayerGeomType =
    6926           2 :                         poDstLayer->GetLayerDefn()
    6927           2 :                             ->GetGeomFieldDefn(iGeom)
    6928           2 :                             ->GetType();
    6929           2 :                     poDstGeometry->set3D(wkbHasZ(eDstLayerGeomType));
    6930           2 :                     poDstGeometry->setMeasured(wkbHasM(eDstLayerGeomType));
    6931             :                 }
    6932             : 
    6933        7795 :                 if (m_eGeomOp == GEOMOP_SEGMENTIZE)
    6934             :                 {
    6935          20 :                     if (m_dfGeomOpParam > 0)
    6936          20 :                         poDstGeometry->segmentize(m_dfGeomOpParam);
    6937             :                 }
    6938        7775 :                 else if (m_eGeomOp == GEOMOP_SIMPLIFY_PRESERVE_TOPOLOGY)
    6939             :                 {
    6940           1 :                     if (m_dfGeomOpParam > 0)
    6941             :                     {
    6942             :                         auto poNewGeom = std::unique_ptr<OGRGeometry>(
    6943             :                             poDstGeometry->SimplifyPreserveTopology(
    6944           2 :                                 m_dfGeomOpParam));
    6945           1 :                         if (poNewGeom)
    6946             :                         {
    6947           1 :                             poDstGeometry = std::move(poNewGeom);
    6948             :                         }
    6949             :                     }
    6950             :                 }
    6951             : 
    6952        7795 :                 if (m_poClipSrcOri)
    6953             :                 {
    6954          50 :                     if (poDstGeometry->IsEmpty())
    6955          26 :                         goto end_loop;
    6956             : 
    6957             :                     const auto clipGeomDesc =
    6958          50 :                         GetSrcClipGeom(poDstGeometry->getSpatialReference());
    6959             : 
    6960          50 :                     if (!(clipGeomDesc.poGeom && clipGeomDesc.poEnv))
    6961           0 :                         goto end_loop;
    6962             : 
    6963          50 :                     OGREnvelope oDstEnv;
    6964          50 :                     poDstGeometry->getEnvelope(&oDstEnv);
    6965             : 
    6966          50 :                     if (!(clipGeomDesc.bGeomIsRectangle &&
    6967           0 :                           clipGeomDesc.poEnv->Contains(oDstEnv)))
    6968             :                     {
    6969           0 :                         std::unique_ptr<OGRGeometry> poClipped;
    6970          50 :                         if (clipGeomDesc.poEnv->Intersects(oDstEnv))
    6971             :                         {
    6972          30 :                             poClipped.reset(clipGeomDesc.poGeom->Intersection(
    6973          30 :                                 poDstGeometry.get()));
    6974             :                         }
    6975          50 :                         if (poClipped == nullptr || poClipped->IsEmpty())
    6976             :                         {
    6977          25 :                             goto end_loop;
    6978             :                         }
    6979             : 
    6980          25 :                         const int nDim = poDstGeometry->getDimension();
    6981          26 :                         if (poClipped->getDimension() < nDim &&
    6982           1 :                             wkbFlatten(poDstFDefn->GetGeomFieldDefn(iGeom)
    6983             :                                            ->GetType()) != wkbUnknown)
    6984             :                         {
    6985           3 :                             CPLDebug(
    6986             :                                 "OGR2OGR",
    6987             :                                 "Discarding feature " CPL_FRMT_GIB
    6988             :                                 " of layer %s, "
    6989             :                                 "as its intersection with -clipsrc is a %s "
    6990             :                                 "whereas the input is a %s",
    6991           1 :                                 nSrcFID, poSrcLayer->GetName(),
    6992           1 :                                 OGRToOGCGeomType(poClipped->getGeometryType()),
    6993             :                                 OGRToOGCGeomType(
    6994           1 :                                     poDstGeometry->getGeometryType()));
    6995           1 :                             goto end_loop;
    6996             :                         }
    6997             : 
    6998          72 :                         poDstGeometry = OGRGeometryFactory::makeCompatibleWith(
    6999          24 :                             std::move(poClipped),
    7000          48 :                             poDstFDefn->GetGeomFieldDefn(iGeom)->GetType());
    7001             :                     }
    7002             :                 }
    7003             : 
    7004             :                 OGRCoordinateTransformation *const poCT =
    7005        7769 :                     psInfo->m_aoReprojectionInfo[iGeom].m_poCT.get();
    7006             :                 char **const papszTransformOptions =
    7007        7769 :                     psInfo->m_aoReprojectionInfo[iGeom]
    7008        7769 :                         .m_aosTransformOptions.List();
    7009             :                 const bool bReprojCanInvalidateValidity =
    7010        7769 :                     psInfo->m_aoReprojectionInfo[iGeom]
    7011        7769 :                         .m_bCanInvalidateValidity;
    7012             : 
    7013        7769 :                 if (poCT != nullptr || papszTransformOptions != nullptr)
    7014             :                 {
    7015             :                     // If we need to change the geometry type to linear, and
    7016             :                     // we have a geometry with curves, then convert it to
    7017             :                     // linear first, to avoid invalidities due to the fact
    7018             :                     // that validity of arc portions isn't always kept while
    7019             :                     // reprojecting and then discretizing.
    7020         124 :                     if (bReprojCanInvalidateValidity &&
    7021         122 :                         (!psInfo->m_bSupportCurves ||
    7022          51 :                          m_eGeomTypeConversion == GTC_CONVERT_TO_LINEAR ||
    7023          49 :                          m_eGeomTypeConversion ==
    7024             :                              GTC_PROMOTE_TO_MULTI_AND_CONVERT_TO_LINEAR))
    7025             :                     {
    7026          73 :                         if (poDstGeometry->hasCurveGeometry(TRUE))
    7027             :                         {
    7028           4 :                             OGRwkbGeometryType eTargetType = OGR_GT_GetLinear(
    7029           4 :                                 poDstGeometry->getGeometryType());
    7030           8 :                             poDstGeometry = OGRGeometryFactory::forceTo(
    7031           8 :                                 std::move(poDstGeometry), eTargetType);
    7032          73 :                         }
    7033             :                     }
    7034          49 :                     else if (bReprojCanInvalidateValidity &&
    7035           2 :                              eGType != GEOMTYPE_UNCHANGED &&
    7036           2 :                              !OGR_GT_IsNonLinear(
    7037         100 :                                  static_cast<OGRwkbGeometryType>(eGType)) &&
    7038           2 :                              poDstGeometry->hasCurveGeometry(TRUE))
    7039             :                     {
    7040           4 :                         poDstGeometry = OGRGeometryFactory::forceTo(
    7041           2 :                             std::move(poDstGeometry),
    7042           2 :                             static_cast<OGRwkbGeometryType>(eGType));
    7043             :                     }
    7044             : 
    7045             :                     // Collect left-most, right-most, top-most, bottom-most coordinates.
    7046         124 :                     if (psInfo->m_aoReprojectionInfo[iGeom]
    7047         124 :                             .m_bWarnAboutDifferentCoordinateOperations)
    7048             :                     {
    7049             :                         struct Visitor : public OGRDefaultConstGeometryVisitor
    7050             :                         {
    7051             :                             TargetLayerInfo::ReprojectionInfo &m_info;
    7052             : 
    7053         110 :                             explicit Visitor(
    7054             :                                 TargetLayerInfo::ReprojectionInfo &info)
    7055         110 :                                 : m_info(info)
    7056             :                             {
    7057         110 :                             }
    7058             : 
    7059             :                             using OGRDefaultConstGeometryVisitor::visit;
    7060             : 
    7061        2814 :                             void visit(const OGRPoint *point) override
    7062             :                             {
    7063        2814 :                                 m_info.UpdateExtremePoints(point->getX(),
    7064             :                                                            point->getY(),
    7065             :                                                            point->getZ());
    7066        2814 :                             }
    7067             :                         };
    7068             : 
    7069         220 :                         Visitor oVisit(psInfo->m_aoReprojectionInfo[iGeom]);
    7070         110 :                         poDstGeometry->accept(&oVisit);
    7071             :                     }
    7072             : 
    7073         125 :                     for (int iIter = 0; iIter < 2; ++iIter)
    7074             :                     {
    7075             :                         auto poReprojectedGeom = std::unique_ptr<OGRGeometry>(
    7076             :                             OGRGeometryFactory::transformWithOptions(
    7077         125 :                                 poDstGeometry.get(), poCT,
    7078             :                                 papszTransformOptions,
    7079         125 :                                 m_transformWithOptionsCache));
    7080         125 :                         if (poReprojectedGeom == nullptr)
    7081             :                         {
    7082           0 :                             if (psOptions->nGroupTransactions)
    7083             :                             {
    7084           0 :                                 if (psOptions->nLayerTransaction)
    7085             :                                 {
    7086           0 :                                     if (poDstLayer->CommitTransaction() !=
    7087           0 :                                             OGRERR_NONE &&
    7088           0 :                                         !psOptions->bSkipFailures)
    7089             :                                     {
    7090           0 :                                         return false;
    7091             :                                     }
    7092             :                                 }
    7093             :                             }
    7094             : 
    7095           0 :                             CPLError(CE_Failure, CPLE_AppDefined,
    7096             :                                      "Failed to reproject feature " CPL_FRMT_GIB
    7097             :                                      " (geometry probably out of source or "
    7098             :                                      "destination SRS).",
    7099             :                                      nSrcFID);
    7100           0 :                             if (!psOptions->bSkipFailures)
    7101             :                             {
    7102           0 :                                 return false;
    7103             :                             }
    7104             :                         }
    7105             : 
    7106             :                         // Check if a curve geometry is no longer valid after
    7107             :                         // reprojection
    7108         125 :                         const auto eType = poDstGeometry->getGeometryType();
    7109         125 :                         const auto eFlatType = wkbFlatten(eType);
    7110             : 
    7111         125 :                         std::string osReason;
    7112         124 :                         if (iIter == 0 && bReprojCanInvalidateValidity &&
    7113         122 :                             OGRGeometryFactory::haveGEOS() &&
    7114         120 :                             (eFlatType == wkbCurvePolygon ||
    7115         120 :                              eFlatType == wkbCompoundCurve ||
    7116         120 :                              eFlatType == wkbMultiCurve ||
    7117           2 :                              eFlatType == wkbMultiSurface) &&
    7118         251 :                             poDstGeometry->hasCurveGeometry(TRUE) &&
    7119           2 :                             poDstGeometry->IsValid(&osReason))
    7120             :                         {
    7121           2 :                             OGRwkbGeometryType eTargetType = OGR_GT_GetLinear(
    7122           2 :                                 poDstGeometry->getGeometryType());
    7123             :                             auto poDstGeometryTmp = OGRGeometryFactory::forceTo(
    7124           2 :                                 std::unique_ptr<OGRGeometry>(
    7125           2 :                                     poReprojectedGeom->clone()),
    7126           2 :                                 eTargetType);
    7127           2 :                             if (!poDstGeometryTmp->IsValid(&osReason))
    7128             :                             {
    7129           1 :                                 CPLDebug("OGR2OGR",
    7130             :                                          "Curve geometry no longer valid (%s) "
    7131             :                                          "after reprojection: transforming it "
    7132             :                                          "into linear one before reprojecting",
    7133             :                                          osReason.c_str());
    7134           2 :                                 poDstGeometry = OGRGeometryFactory::forceTo(
    7135           2 :                                     std::move(poDstGeometry), eTargetType);
    7136           2 :                                 poDstGeometry = OGRGeometryFactory::forceTo(
    7137           2 :                                     std::move(poDstGeometry), eType);
    7138             :                             }
    7139             :                             else
    7140             :                             {
    7141           1 :                                 poDstGeometry = std::move(poReprojectedGeom);
    7142           1 :                                 break;
    7143             :                             }
    7144             :                         }
    7145             :                         else
    7146             :                         {
    7147         123 :                             poDstGeometry = std::move(poReprojectedGeom);
    7148         123 :                             break;
    7149             :                         }
    7150         124 :                     }
    7151             :                 }
    7152        7645 :                 else if (poOutputSRS != nullptr)
    7153             :                 {
    7154        6495 :                     poDstGeometry->assignSpatialReference(poOutputSRS);
    7155             :                 }
    7156             : 
    7157        7769 :                 if (poDstGeometry != nullptr)
    7158             :                 {
    7159        7769 :                     if (m_poClipDstOri)
    7160             :                     {
    7161          40 :                         if (poDstGeometry->IsEmpty())
    7162          20 :                             goto end_loop;
    7163             : 
    7164             :                         const auto clipGeomDesc = GetDstClipGeom(
    7165          40 :                             poDstGeometry->getSpatialReference());
    7166          40 :                         if (!clipGeomDesc.poGeom || !clipGeomDesc.poEnv)
    7167             :                         {
    7168           0 :                             goto end_loop;
    7169             :                         }
    7170             : 
    7171          40 :                         OGREnvelope oDstEnv;
    7172          40 :                         poDstGeometry->getEnvelope(&oDstEnv);
    7173             : 
    7174          74 :                         if (!(clipGeomDesc.bGeomIsRectangle &&
    7175          34 :                               clipGeomDesc.poEnv->Contains(oDstEnv)))
    7176             :                         {
    7177           0 :                             std::unique_ptr<OGRGeometry> poClipped;
    7178          35 :                             if (clipGeomDesc.poEnv->Intersects(oDstEnv))
    7179             :                             {
    7180          20 :                                 poClipped.reset(
    7181          20 :                                     clipGeomDesc.poGeom->Intersection(
    7182          20 :                                         poDstGeometry.get()));
    7183             :                             }
    7184             : 
    7185          35 :                             if (poClipped == nullptr || poClipped->IsEmpty())
    7186             :                             {
    7187          19 :                                 goto end_loop;
    7188             :                             }
    7189             : 
    7190          16 :                             const int nDim = poDstGeometry->getDimension();
    7191          17 :                             if (poClipped->getDimension() < nDim &&
    7192           1 :                                 wkbFlatten(poDstFDefn->GetGeomFieldDefn(iGeom)
    7193             :                                                ->GetType()) != wkbUnknown)
    7194             :                             {
    7195           3 :                                 CPLDebug(
    7196             :                                     "OGR2OGR",
    7197             :                                     "Discarding feature " CPL_FRMT_GIB
    7198             :                                     " of layer %s, "
    7199             :                                     "as its intersection with -clipdst is a %s "
    7200             :                                     "whereas the input is a %s",
    7201           1 :                                     nSrcFID, poSrcLayer->GetName(),
    7202             :                                     OGRToOGCGeomType(
    7203           1 :                                         poClipped->getGeometryType()),
    7204             :                                     OGRToOGCGeomType(
    7205           1 :                                         poDstGeometry->getGeometryType()));
    7206           1 :                                 goto end_loop;
    7207             :                             }
    7208             : 
    7209             :                             poDstGeometry =
    7210          45 :                                 OGRGeometryFactory::makeCompatibleWith(
    7211          15 :                                     std::move(poClipped),
    7212          15 :                                     poDstFDefn->GetGeomFieldDefn(iGeom)
    7213          15 :                                         ->GetType());
    7214             :                         }
    7215             :                     }
    7216             : 
    7217       15498 :                     if (psOptions->dfXYRes !=
    7218           1 :                             OGRGeomCoordinatePrecision::UNKNOWN &&
    7219        7750 :                         OGRGeometryFactory::haveGEOS() &&
    7220           1 :                         !poDstGeometry->hasCurveGeometry())
    7221             :                     {
    7222             :                         // OGR_APPLY_GEOM_SET_PRECISION default value for
    7223             :                         // OGRLayer::CreateFeature() purposes, but here in the
    7224             :                         // ogr2ogr -xyRes context, we force calling SetPrecision(),
    7225             :                         // unless the user explicitly asks not to do it by
    7226             :                         // setting the config option to NO.
    7227           1 :                         if (!bRunSetPrecisionEvaluated)
    7228             :                         {
    7229           1 :                             bRunSetPrecisionEvaluated = true;
    7230           1 :                             bRunSetPrecision = CPLTestBool(CPLGetConfigOption(
    7231             :                                 "OGR_APPLY_GEOM_SET_PRECISION", "YES"));
    7232             :                         }
    7233           1 :                         if (bRunSetPrecision)
    7234             :                         {
    7235             :                             auto poNewGeom = std::unique_ptr<OGRGeometry>(
    7236           1 :                                 poDstGeometry->SetPrecision(psOptions->dfXYRes,
    7237           1 :                                                             /* nFlags = */ 0));
    7238           1 :                             if (!poNewGeom)
    7239           0 :                                 goto end_loop;
    7240           1 :                             poDstGeometry = std::move(poNewGeom);
    7241             :                         }
    7242             :                     }
    7243             : 
    7244        7749 :                     if (m_bMakeValid)
    7245             :                     {
    7246             :                         const bool bIsGeomCollection =
    7247           7 :                             wkbFlatten(poDstGeometry->getGeometryType()) ==
    7248           7 :                             wkbGeometryCollection;
    7249             :                         auto poNewGeom = std::unique_ptr<OGRGeometry>(
    7250           7 :                             poDstGeometry->MakeValid());
    7251           7 :                         if (!poNewGeom)
    7252           0 :                             goto end_loop;
    7253           7 :                         poDstGeometry = std::move(poNewGeom);
    7254           7 :                         if (!bIsGeomCollection)
    7255             :                         {
    7256           6 :                             poDstGeometry.reset(
    7257             :                                 OGRGeometryFactory::
    7258             :                                     removeLowerDimensionSubGeoms(
    7259           6 :                                         poDstGeometry.get()));
    7260             :                         }
    7261             :                     }
    7262             : 
    7263        7749 :                     if (m_bSkipInvalidGeom && !poDstGeometry->IsValid())
    7264           1 :                         goto end_loop;
    7265             : 
    7266        7748 :                     if (m_eGeomTypeConversion != GTC_DEFAULT)
    7267             :                     {
    7268             :                         OGRwkbGeometryType eTargetType =
    7269          11 :                             poDstGeometry->getGeometryType();
    7270             :                         eTargetType =
    7271          11 :                             ConvertType(m_eGeomTypeConversion, eTargetType);
    7272          22 :                         poDstGeometry = OGRGeometryFactory::forceTo(
    7273          22 :                             std::move(poDstGeometry), eTargetType);
    7274             :                     }
    7275        7737 :                     else if (eGType != GEOMTYPE_UNCHANGED)
    7276             :                     {
    7277         118 :                         poDstGeometry = OGRGeometryFactory::forceTo(
    7278          59 :                             std::move(poDstGeometry),
    7279          59 :                             static_cast<OGRwkbGeometryType>(eGType));
    7280             :                     }
    7281             :                 }
    7282             : 
    7283        7748 :                 if (poDstGeometry && !psOptions->bQuiet)
    7284             :                 {
    7285       23216 :                     if (!psInfo->m_bHasWarnedAboutCurves &&
    7286       10622 :                         !psInfo->m_bSupportCurves &&
    7287        2883 :                         OGR_GT_IsNonLinear(poDstGeometry->getGeometryType()))
    7288             :                     {
    7289           3 :                         CPLError(CE_Warning, CPLE_AppDefined,
    7290             :                                  "Attempt to write curve geometries to layer "
    7291             :                                  "%s that does not support them. They will be "
    7292             :                                  "linearized",
    7293           3 :                                  poDstLayer->GetDescription());
    7294           3 :                         psInfo->m_bHasWarnedAboutCurves = true;
    7295             :                     }
    7296        8692 :                     if (!psInfo->m_bHasWarnedAboutZ && !psInfo->m_bSupportZ &&
    7297         953 :                         OGR_GT_HasZ(poDstGeometry->getGeometryType()))
    7298             :                     {
    7299           3 :                         CPLError(CE_Warning, CPLE_AppDefined,
    7300             :                                  "Attempt to write Z geometries to layer %s "
    7301             :                                  "that does not support them. Z component will "
    7302             :                                  "be discarded",
    7303           3 :                                  poDstLayer->GetDescription());
    7304           3 :                         psInfo->m_bHasWarnedAboutZ = true;
    7305             :                     }
    7306        8776 :                     if (!psInfo->m_bHasWarnedAboutM && !psInfo->m_bSupportM &&
    7307        1037 :                         OGR_GT_HasM(poDstGeometry->getGeometryType()))
    7308             :                     {
    7309           1 :                         CPLError(CE_Warning, CPLE_AppDefined,
    7310             :                                  "Attempt to write M geometries to layer %s "
    7311             :                                  "that does not support them. M component will "
    7312             :                                  "be discarded",
    7313           1 :                                  poDstLayer->GetDescription());
    7314           1 :                         psInfo->m_bHasWarnedAboutM = true;
    7315             :                     }
    7316             :                 }
    7317             : 
    7318        7748 :                 poDstFeature->SetGeomField(iGeom, std::move(poDstGeometry));
    7319             :             }
    7320             : 
    7321       14416 :             CPLErrorReset();
    7322       28832 :             if ((psOptions->bUpsert
    7323       14416 :                      ? poDstLayer->UpsertFeature(poDstFeature.get())
    7324       14416 :                      : poDstLayer->CreateFeature(poDstFeature.get())) ==
    7325             :                 OGRERR_NONE)
    7326             :             {
    7327       14407 :                 nFeaturesWritten++;
    7328       15564 :                 if (nDesiredFID != OGRNullFID &&
    7329        1157 :                     poDstFeature->GetFID() != nDesiredFID)
    7330             :                 {
    7331           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
    7332             :                              "Feature id " CPL_FRMT_GIB " not preserved",
    7333             :                              nDesiredFID);
    7334             :                 }
    7335             :             }
    7336           9 :             else if (!psOptions->bSkipFailures)
    7337             :             {
    7338           6 :                 if (psOptions->nGroupTransactions)
    7339             :                 {
    7340           6 :                     if (psOptions->nLayerTransaction)
    7341           2 :                         poDstLayer->RollbackTransaction();
    7342             :                 }
    7343             : 
    7344           6 :                 CPLError(CE_Failure, CPLE_AppDefined,
    7345             :                          "Unable to write feature " CPL_FRMT_GIB
    7346             :                          " from layer %s.",
    7347           6 :                          nSrcFID, poSrcLayer->GetName());
    7348             : 
    7349           6 :                 return false;
    7350             :             }
    7351             :             else
    7352             :             {
    7353           3 :                 CPLDebug("GDALVectorTranslate",
    7354             :                          "Unable to write feature " CPL_FRMT_GIB
    7355             :                          " into layer %s.",
    7356           3 :                          nSrcFID, poSrcLayer->GetName());
    7357           3 :                 if (psOptions->nGroupTransactions)
    7358             :                 {
    7359           3 :                     if (psOptions->nLayerTransaction)
    7360             :                     {
    7361           2 :                         poDstLayer->RollbackTransaction();
    7362           2 :                         CPL_IGNORE_RET_VAL(poDstLayer->StartTransaction());
    7363             :                     }
    7364             :                     else
    7365             :                     {
    7366           1 :                         m_poODS->RollbackTransaction();
    7367           1 :                         m_poODS->StartTransaction(psOptions->bForceTransaction);
    7368             :                     }
    7369             :                 }
    7370             :             }
    7371             : 
    7372       14457 :         end_loop:;  // nothing
    7373             :         }
    7374             : 
    7375             :         /* Report progress */
    7376       14454 :         nCount++;
    7377       14454 :         bool bGoOn = true;
    7378       14454 :         if (pfnProgress)
    7379             :         {
    7380        5295 :             bGoOn = pfnProgress(nCountLayerFeatures
    7381        2646 :                                     ? nCount * 1.0 / nCountLayerFeatures
    7382             :                                     : 1.0,
    7383             :                                 "", pProgressArg) != FALSE;
    7384             :         }
    7385       14454 :         if (!bGoOn)
    7386             :         {
    7387           1 :             bRet = false;
    7388           1 :             break;
    7389             :         }
    7390             : 
    7391       14453 :         if (pnReadFeatureCount)
    7392           0 :             *pnReadFeatureCount = nCount;
    7393             : 
    7394       14453 :         if (psOptions->nFIDToFetch != OGRNullFID)
    7395           5 :             break;
    7396       14448 :         if (bSingleIteration)
    7397         974 :             break;
    7398       13474 :     }
    7399             : 
    7400        1846 :     if (psOptions->nGroupTransactions)
    7401             :     {
    7402        1845 :         if (psOptions->nLayerTransaction)
    7403             :         {
    7404         726 :             if (poDstLayer->CommitTransaction() != OGRERR_NONE)
    7405           0 :                 bRet = false;
    7406             :         }
    7407             :     }
    7408             : 
    7409        1846 :     if (!bSingleIteration)
    7410             :     {
    7411         872 :         CPLDebug("GDALVectorTranslate",
    7412             :                  CPL_FRMT_GIB " features written in layer '%s'",
    7413         872 :                  nFeaturesWritten, poDstLayer->GetName());
    7414             :     }
    7415             : 
    7416        1846 :     return bRet;
    7417             : }
    7418             : 
    7419             : /************************************************************************/
    7420             : /*                  LayerTranslator::GetDstClipGeom()                   */
    7421             : /************************************************************************/
    7422             : 
    7423             : /** Returns the destination clip geometry and its envelope
    7424             :  *
    7425             :  * @param poGeomSRS The SRS into which the destination clip geometry should be
    7426             :  *                  expressed.
    7427             :  * @return the destination clip geometry and its envelope, or (nullptr, nullptr)
    7428             :  */
    7429             : LayerTranslator::ClipGeomDesc
    7430          40 : LayerTranslator::GetDstClipGeom(const OGRSpatialReference *poGeomSRS)
    7431             : {
    7432          40 :     if (m_poClipDstReprojectedToDstSRS_SRS != poGeomSRS)
    7433             :     {
    7434          36 :         auto poClipDstSRS = m_poClipDstOri->getSpatialReference();
    7435          36 :         if (poClipDstSRS && poGeomSRS && !poClipDstSRS->IsSame(poGeomSRS))
    7436             :         {
    7437             :             // Transform clip geom to geometry SRS
    7438           1 :             m_poClipDstReprojectedToDstSRS.reset(m_poClipDstOri->clone());
    7439           1 :             if (m_poClipDstReprojectedToDstSRS->transformTo(poGeomSRS) !=
    7440             :                 OGRERR_NONE)
    7441             :             {
    7442           0 :                 return ClipGeomDesc();
    7443             :             }
    7444           1 :             m_poClipDstReprojectedToDstSRS_SRS = poGeomSRS;
    7445             :         }
    7446          35 :         else if (!poClipDstSRS && poGeomSRS)
    7447             :         {
    7448          35 :             if (!m_bWarnedClipDstSRS)
    7449             :             {
    7450           2 :                 m_bWarnedClipDstSRS = true;
    7451           2 :                 CPLError(CE_Warning, CPLE_AppDefined,
    7452             :                          "Clip destination geometry has no "
    7453             :                          "attached SRS, but the feature's "
    7454             :                          "geometry has one. Assuming clip "
    7455             :                          "destination geometry SRS is the "
    7456             :                          "same as the feature's geometry");
    7457             :             }
    7458             :         }
    7459          36 :         m_oClipDstEnv = OGREnvelope();
    7460             :     }
    7461             : 
    7462             :     const auto poGeom = m_poClipDstReprojectedToDstSRS
    7463          40 :                             ? m_poClipDstReprojectedToDstSRS.get()
    7464          40 :                             : m_poClipDstOri;
    7465          40 :     if (poGeom && !m_oClipDstEnv.IsInit())
    7466             :     {
    7467          40 :         poGeom->getEnvelope(&m_oClipDstEnv);
    7468          40 :         m_bClipDstIsRectangle = poGeom->IsRectangle();
    7469             :     }
    7470          40 :     ClipGeomDesc ret;
    7471          40 :     ret.poGeom = poGeom;
    7472          40 :     ret.poEnv = poGeom ? &m_oClipDstEnv : nullptr;
    7473          40 :     ret.bGeomIsRectangle = m_bClipDstIsRectangle;
    7474          40 :     return ret;
    7475             : }
    7476             : 
    7477             : /************************************************************************/
    7478             : /*                  LayerTranslator::GetSrcClipGeom()                   */
    7479             : /************************************************************************/
    7480             : 
    7481             : /** Returns the source clip geometry and its envelope
    7482             :  *
    7483             :  * @param poGeomSRS The SRS into which the source clip geometry should be
    7484             :  *                  expressed.
    7485             :  * @return the source clip geometry and its envelope, or (nullptr, nullptr)
    7486             :  */
    7487             : LayerTranslator::ClipGeomDesc
    7488          50 : LayerTranslator::GetSrcClipGeom(const OGRSpatialReference *poGeomSRS)
    7489             : {
    7490          50 :     if (m_poClipSrcReprojectedToSrcSRS_SRS != poGeomSRS)
    7491             :     {
    7492          42 :         auto poClipSrcSRS = m_poClipSrcOri->getSpatialReference();
    7493          42 :         if (poClipSrcSRS && poGeomSRS && !poClipSrcSRS->IsSame(poGeomSRS))
    7494             :         {
    7495             :             // Transform clip geom to geometry SRS
    7496           1 :             m_poClipSrcReprojectedToSrcSRS.reset(m_poClipSrcOri->clone());
    7497           1 :             if (m_poClipSrcReprojectedToSrcSRS->transformTo(poGeomSRS) !=
    7498             :                 OGRERR_NONE)
    7499             :             {
    7500           0 :                 return ClipGeomDesc();
    7501             :             }
    7502           1 :             m_poClipSrcReprojectedToSrcSRS_SRS = poGeomSRS;
    7503             :         }
    7504          41 :         else if (!poClipSrcSRS && poGeomSRS)
    7505             :         {
    7506          41 :             if (!m_bWarnedClipSrcSRS)
    7507             :             {
    7508           3 :                 m_bWarnedClipSrcSRS = true;
    7509           3 :                 CPLError(CE_Warning, CPLE_AppDefined,
    7510             :                          "Clip source geometry has no attached SRS, "
    7511             :                          "but the feature's geometry has one. "
    7512             :                          "Assuming clip source geometry SRS is the "
    7513             :                          "same as the feature's geometry");
    7514             :             }
    7515             :         }
    7516          42 :         m_oClipSrcEnv = OGREnvelope();
    7517             :     }
    7518             : 
    7519             :     const auto poGeom = m_poClipSrcReprojectedToSrcSRS
    7520          50 :                             ? m_poClipSrcReprojectedToSrcSRS.get()
    7521          50 :                             : m_poClipSrcOri;
    7522          50 :     if (poGeom && !m_oClipSrcEnv.IsInit())
    7523             :     {
    7524          50 :         poGeom->getEnvelope(&m_oClipSrcEnv);
    7525          50 :         m_bClipSrcIsRectangle = poGeom->IsRectangle();
    7526             :     }
    7527          50 :     ClipGeomDesc ret;
    7528          50 :     ret.poGeom = poGeom;
    7529          50 :     ret.poEnv = poGeom ? &m_oClipSrcEnv : nullptr;
    7530          50 :     ret.bGeomIsRectangle = m_bClipDstIsRectangle;
    7531          50 :     return ret;
    7532             : }
    7533             : 
    7534             : /************************************************************************/
    7535             : /*           TargetLayerInfo::CheckSameCoordinateOperation()            */
    7536             : /************************************************************************/
    7537             : 
    7538        1186 : void TargetLayerInfo::CheckSameCoordinateOperation() const
    7539             : {
    7540        2275 :     for (auto &info : m_aoReprojectionInfo)
    7541             :     {
    7542        1089 :         if (info.m_bWarnAboutDifferentCoordinateOperations &&
    7543          36 :             info.m_dfLeftX <= info.m_dfRightX)
    7544             :         {
    7545             :             // Start recording if different coordinate operations are
    7546             :             // going to be used
    7547          35 :             OGRProjCTDifferentOperationsStart(info.m_poCT.get());
    7548             : 
    7549             :             {
    7550          70 :                 CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
    7551             :                 {
    7552          35 :                     double dfX = info.m_dfLeftX;
    7553          35 :                     double dfY = info.m_dfLeftY;
    7554          35 :                     double dfZ = info.m_dfLeftZ;
    7555          35 :                     info.m_poCT->Transform(1, &dfX, &dfY, &dfZ);
    7556             :                 }
    7557             : 
    7558             :                 {
    7559          35 :                     double dfX = info.m_dfRightX;
    7560          35 :                     double dfY = info.m_dfRightY;
    7561          35 :                     double dfZ = info.m_dfRightZ;
    7562          35 :                     info.m_poCT->Transform(1, &dfX, &dfY, &dfZ);
    7563             :                 }
    7564             : 
    7565             :                 {
    7566          35 :                     double dfX = info.m_dfTopX;
    7567          35 :                     double dfY = info.m_dfTopY;
    7568          35 :                     double dfZ = info.m_dfTopZ;
    7569          35 :                     info.m_poCT->Transform(1, &dfX, &dfY, &dfZ);
    7570             :                 }
    7571             : 
    7572             :                 {
    7573          35 :                     double dfX = info.m_dfBottomX;
    7574          35 :                     double dfY = info.m_dfBottomY;
    7575          35 :                     double dfZ = info.m_dfBottomZ;
    7576          35 :                     info.m_poCT->Transform(1, &dfX, &dfY, &dfZ);
    7577             :                 }
    7578             :             }
    7579             : 
    7580          35 :             if (OGRProjCTDifferentOperationsUsed(info.m_poCT.get()))
    7581             :             {
    7582           0 :                 CPLError(
    7583             :                     CE_Warning, CPLE_AppDefined,
    7584             :                     "Several coordinate operations have been used to transform "
    7585             :                     "layer %s. Artifacts may appear. You may consider "
    7586             :                     "using the -ct_opt ALLOW_BALLPARK=NO and/or "
    7587             :                     "-ct_opt ONLY_BEST=YES warping options, or specify "
    7588             :                     "a particular coordinate operation with -ct. "
    7589             :                     "This warning can be silenced with "
    7590             :                     "-ct_opt WARN_ABOUT_DIFFERENT_COORD_OP=NO.",
    7591           0 :                     m_poSrcLayer->GetName());
    7592             :             }
    7593             : 
    7594             :             // Stop recording
    7595          35 :             OGRProjCTDifferentOperationsStop(info.m_poCT.get());
    7596             :         }
    7597             :     }
    7598        1186 : }
    7599             : 
    7600             : /************************************************************************/
    7601             : /*                GDALVectorTranslateOptionsGetParser()                 */
    7602             : /************************************************************************/
    7603             : 
    7604        1073 : static std::unique_ptr<GDALArgumentParser> GDALVectorTranslateOptionsGetParser(
    7605             :     GDALVectorTranslateOptions *psOptions,
    7606             :     GDALVectorTranslateOptionsForBinary *psOptionsForBinary, int nCountClipSrc,
    7607             :     int nCountClipDst)
    7608             : {
    7609             :     auto argParser = std::make_unique<GDALArgumentParser>(
    7610        1073 :         "ogr2ogr", /* bForBinary=*/psOptionsForBinary != nullptr);
    7611             : 
    7612        1073 :     argParser->add_description(
    7613        1073 :         _("Converts simple features data between file formats."));
    7614             : 
    7615        1073 :     argParser->add_epilog(
    7616        1073 :         _("For more details, consult https://gdal.org/programs/ogr2ogr.html"));
    7617             : 
    7618        1073 :     argParser->add_output_format_argument(psOptions->osFormat);
    7619             : 
    7620        1073 :     argParser->add_dataset_creation_options_argument(psOptions->aosDSCO);
    7621             : 
    7622        1073 :     argParser->add_layer_creation_options_argument(psOptions->aosLCO);
    7623             : 
    7624        1073 :     argParser->add_usage_newline();
    7625             : 
    7626             :     {
    7627        1073 :         auto &group = argParser->add_mutually_exclusive_group();
    7628        1073 :         group.add_argument("-append")
    7629        1073 :             .flag()
    7630          39 :             .action([psOptions](const std::string &)
    7631        1073 :                     { psOptions->eAccessMode = ACCESS_APPEND; })
    7632        1073 :             .help(_("Append to existing layer instead of creating new."));
    7633             : 
    7634        1073 :         group.add_argument("-upsert")
    7635        1073 :             .flag()
    7636             :             .action(
    7637           4 :                 [psOptions](const std::string &)
    7638             :                 {
    7639           4 :                     psOptions->eAccessMode = ACCESS_APPEND;
    7640           4 :                     psOptions->bUpsert = true;
    7641        1073 :                 })
    7642             :             .help(_("Variant of -append where the UpsertFeature() operation is "
    7643        1073 :                     "used to insert or update features."));
    7644             : 
    7645        1073 :         group.add_argument("-overwrite")
    7646        1073 :             .flag()
    7647          18 :             .action([psOptions](const std::string &)
    7648        1073 :                     { psOptions->eAccessMode = ACCESS_OVERWRITE; })
    7649        1073 :             .help(_("Delete the output layer and recreate it empty."));
    7650             :     }
    7651             : 
    7652        1073 :     argParser->add_argument("-update")
    7653        1073 :         .flag()
    7654             :         .action(
    7655          22 :             [psOptions](const std::string &)
    7656             :             {
    7657             :                 /* Don't reset -append or -overwrite */
    7658           8 :                 if (psOptions->eAccessMode != ACCESS_APPEND &&
    7659           7 :                     psOptions->eAccessMode != ACCESS_OVERWRITE)
    7660           7 :                     psOptions->eAccessMode = ACCESS_UPDATE;
    7661        1073 :             })
    7662             :         .help(_("Open existing output datasource in update mode rather than "
    7663        1073 :                 "trying to create a new one."));
    7664             : 
    7665        1073 :     argParser->add_argument("-sql")
    7666        2146 :         .metavar("<statement>|@<filename>")
    7667             :         .action(
    7668          34 :             [psOptions](const std::string &s)
    7669             :             {
    7670          17 :                 GByte *pabyRet = nullptr;
    7671          20 :                 if (!s.empty() && s.front() == '@' &&
    7672           3 :                     VSIIngestFile(nullptr, s.c_str() + 1, &pabyRet, nullptr,
    7673             :                                   10 * 1024 * 1024))
    7674             :                 {
    7675           3 :                     GDALRemoveBOM(pabyRet);
    7676           3 :                     char *pszSQLStatement = reinterpret_cast<char *>(pabyRet);
    7677             :                     psOptions->osSQLStatement =
    7678           3 :                         CPLRemoveSQLComments(pszSQLStatement);
    7679           3 :                     VSIFree(pszSQLStatement);
    7680             :                 }
    7681             :                 else
    7682             :                 {
    7683          14 :                     psOptions->osSQLStatement = s;
    7684             :                 }
    7685        1090 :             })
    7686        1073 :         .help(_("SQL statement to execute."));
    7687             : 
    7688        1073 :     argParser->add_argument("-dialect")
    7689        2146 :         .metavar("<dialect>")
    7690        1073 :         .store_into(psOptions->osDialect)
    7691        1073 :         .help(_("SQL dialect."));
    7692             : 
    7693        1073 :     argParser->add_argument("-spat")
    7694        2146 :         .metavar("<xmin> <ymin> <xmax> <ymax>")
    7695        1073 :         .nargs(4)
    7696        1073 :         .scan<'g', double>()
    7697             :         .help(_("Spatial query extents, in the SRS of the source layer(s) (or "
    7698        1073 :                 "the one specified with -spat_srs."));
    7699             : 
    7700        1073 :     argParser->add_argument("-where")
    7701        2146 :         .metavar("<restricted_where>|@<filename>")
    7702             :         .action(
    7703          16 :             [psOptions](const std::string &s)
    7704             :             {
    7705           8 :                 GByte *pabyRet = nullptr;
    7706           9 :                 if (!s.empty() && s.front() == '@' &&
    7707           1 :                     VSIIngestFile(nullptr, s.c_str() + 1, &pabyRet, nullptr,
    7708             :                                   10 * 1024 * 1024))
    7709             :                 {
    7710           1 :                     GDALRemoveBOM(pabyRet);
    7711           1 :                     char *pszWHERE = reinterpret_cast<char *>(pabyRet);
    7712           1 :                     psOptions->osWHERE = pszWHERE;
    7713           1 :                     VSIFree(pszWHERE);
    7714             :                 }
    7715             :                 else
    7716             :                 {
    7717           7 :                     psOptions->osWHERE = s;
    7718             :                 }
    7719        1081 :             })
    7720        1073 :         .help(_("Attribute query (like SQL WHERE)."));
    7721             : 
    7722        1073 :     argParser->add_argument("-select")
    7723        2146 :         .metavar("<field_list>")
    7724             :         .action(
    7725          38 :             [psOptions](const std::string &s)
    7726             :             {
    7727          19 :                 psOptions->bSelFieldsSet = true;
    7728             :                 psOptions->aosSelFields =
    7729          19 :                     CSLTokenizeStringComplex(s.c_str(), ",", TRUE, FALSE);
    7730        1073 :             })
    7731             :         .help(_("Comma-delimited list of fields from input layer to copy to "
    7732        1073 :                 "the new layer."));
    7733             : 
    7734        1073 :     argParser->add_argument("-nln")
    7735        2146 :         .metavar("<name>")
    7736        1073 :         .store_into(psOptions->osNewLayerName)
    7737        1073 :         .help(_("Assign an alternate name to the new layer."));
    7738             : 
    7739        1073 :     argParser->add_argument("-nlt")
    7740        2146 :         .metavar("<type>")
    7741        1073 :         .append()
    7742             :         .action(
    7743         243 :             [psOptions](const std::string &osGeomNameIn)
    7744             :             {
    7745          53 :                 bool bIs3D = false;
    7746         106 :                 std::string osGeomName(osGeomNameIn);
    7747         106 :                 if (osGeomName.size() > 3 &&
    7748          53 :                     STARTS_WITH_CI(osGeomName.c_str() + osGeomName.size() - 3,
    7749             :                                    "25D"))
    7750             :                 {
    7751           1 :                     bIs3D = true;
    7752           1 :                     osGeomName.resize(osGeomName.size() - 3);
    7753             :                 }
    7754         104 :                 else if (osGeomName.size() > 1 &&
    7755          52 :                          STARTS_WITH_CI(
    7756             :                              osGeomName.c_str() + osGeomName.size() - 1, "Z"))
    7757             :                 {
    7758           0 :                     bIs3D = true;
    7759           0 :                     osGeomName.pop_back();
    7760             :                 }
    7761          53 :                 if (EQUAL(osGeomName.c_str(), "NONE"))
    7762             :                 {
    7763           1 :                     if (psOptions->eGType != GEOMTYPE_UNCHANGED)
    7764             :                     {
    7765             :                         throw std::invalid_argument(
    7766           0 :                             "Unsupported combination of -nlt arguments.");
    7767             :                     }
    7768           1 :                     psOptions->eGType = wkbNone;
    7769             :                 }
    7770          52 :                 else if (EQUAL(osGeomName.c_str(), "GEOMETRY"))
    7771             :                 {
    7772           4 :                     if (psOptions->eGType != GEOMTYPE_UNCHANGED)
    7773             :                     {
    7774             :                         throw std::invalid_argument(
    7775           0 :                             "Unsupported combination of -nlt arguments.");
    7776             :                     }
    7777           4 :                     psOptions->eGType = wkbUnknown;
    7778             :                 }
    7779          48 :                 else if (EQUAL(osGeomName.c_str(), "PROMOTE_TO_MULTI"))
    7780             :                 {
    7781           8 :                     if (psOptions->eGeomTypeConversion == GTC_CONVERT_TO_LINEAR)
    7782           2 :                         psOptions->eGeomTypeConversion =
    7783             :                             GTC_PROMOTE_TO_MULTI_AND_CONVERT_TO_LINEAR;
    7784           6 :                     else if (psOptions->eGeomTypeConversion == GTC_DEFAULT)
    7785           5 :                         psOptions->eGeomTypeConversion = GTC_PROMOTE_TO_MULTI;
    7786             :                     else
    7787             :                     {
    7788             :                         throw std::invalid_argument(
    7789           1 :                             "Unsupported combination of -nlt arguments.");
    7790             :                     }
    7791             :                 }
    7792          40 :                 else if (EQUAL(osGeomName.c_str(), "CONVERT_TO_LINEAR"))
    7793             :                 {
    7794          12 :                     if (psOptions->eGeomTypeConversion == GTC_PROMOTE_TO_MULTI)
    7795           2 :                         psOptions->eGeomTypeConversion =
    7796             :                             GTC_PROMOTE_TO_MULTI_AND_CONVERT_TO_LINEAR;
    7797          10 :                     else if (psOptions->eGeomTypeConversion == GTC_DEFAULT)
    7798           9 :                         psOptions->eGeomTypeConversion = GTC_CONVERT_TO_LINEAR;
    7799             :                     else
    7800             :                     {
    7801             :                         throw std::invalid_argument(
    7802           1 :                             "Unsupported combination of -nlt arguments.");
    7803             :                     }
    7804             :                 }
    7805          28 :                 else if (EQUAL(osGeomName.c_str(), "CONVERT_TO_CURVE"))
    7806             :                 {
    7807           7 :                     if (psOptions->eGeomTypeConversion == GTC_DEFAULT)
    7808           5 :                         psOptions->eGeomTypeConversion = GTC_CONVERT_TO_CURVE;
    7809             :                     else
    7810             :                     {
    7811             :                         throw std::invalid_argument(
    7812           2 :                             "Unsupported combination of -nlt arguments.");
    7813             :                     }
    7814             :                 }
    7815             :                 else
    7816             :                 {
    7817          21 :                     if (psOptions->eGType != GEOMTYPE_UNCHANGED)
    7818             :                     {
    7819             :                         throw std::invalid_argument(
    7820           3 :                             "Unsupported combination of -nlt arguments.");
    7821             :                     }
    7822          18 :                     psOptions->eGType = OGRFromOGCGeomType(osGeomName.c_str());
    7823          18 :                     if (psOptions->eGType == wkbUnknown)
    7824             :                     {
    7825             :                         throw std::invalid_argument(
    7826             :                             CPLSPrintf("-nlt %s: type not recognised.",
    7827           0 :                                        osGeomName.c_str()));
    7828             :                     }
    7829             :                 }
    7830          46 :                 if (psOptions->eGType != GEOMTYPE_UNCHANGED &&
    7831          27 :                     psOptions->eGType != wkbNone && bIs3D)
    7832           1 :                     psOptions->eGType = wkbSetZ(
    7833             :                         static_cast<OGRwkbGeometryType>(psOptions->eGType));
    7834        1119 :             })
    7835        1073 :         .help(_("Define the geometry type for the created layer."));
    7836             : 
    7837        1073 :     argParser->add_argument("-s_srs")
    7838        2146 :         .metavar("<srs_def>")
    7839        1073 :         .store_into(psOptions->osSourceSRSDef)
    7840        1073 :         .help(_("Set/override source SRS."));
    7841             : 
    7842             :     {
    7843        1073 :         auto &group = argParser->add_mutually_exclusive_group();
    7844        1073 :         group.add_argument("-a_srs")
    7845        2146 :             .metavar("<srs_def>")
    7846             :             .action(
    7847         353 :                 [psOptions](const std::string &osOutputSRSDef)
    7848             :                 {
    7849         115 :                     psOptions->osOutputSRSDef = osOutputSRSDef;
    7850         230 :                     if (EQUAL(psOptions->osOutputSRSDef.c_str(), "NULL") ||
    7851         115 :                         EQUAL(psOptions->osOutputSRSDef.c_str(), "NONE"))
    7852             :                     {
    7853           4 :                         psOptions->osOutputSRSDef.clear();
    7854           4 :                         psOptions->bNullifyOutputSRS = true;
    7855             :                     }
    7856        1073 :                 })
    7857        1073 :             .help(_("Assign an output SRS, but without reprojecting."));
    7858             : 
    7859        1073 :         group.add_argument("-t_srs")
    7860        2146 :             .metavar("<srs_def>")
    7861             :             .action(
    7862          72 :                 [psOptions](const std::string &osOutputSRSDef)
    7863             :                 {
    7864          36 :                     psOptions->osOutputSRSDef = osOutputSRSDef;
    7865          36 :                     psOptions->bTransform = true;
    7866        1073 :                 })
    7867             :             .help(_("Reproject/transform to this SRS on output, and assign it "
    7868        1073 :                     "as output SRS."));
    7869             :     }
    7870             : 
    7871             :     ///////////////////////////////////////////////////////////////////////
    7872        1073 :     argParser->add_group("Field related options");
    7873             : 
    7874        1073 :     argParser->add_argument("-addfields")
    7875        1073 :         .flag()
    7876             :         .action(
    7877           4 :             [psOptions](const std::string &)
    7878             :             {
    7879           4 :                 psOptions->bAddMissingFields = true;
    7880           4 :                 psOptions->eAccessMode = ACCESS_APPEND;
    7881        1073 :             })
    7882        1073 :         .help(_("Same as append, but add also any new fields."));
    7883             : 
    7884        1073 :     argParser->add_argument("-relaxedFieldNameMatch")
    7885        1073 :         .flag()
    7886           1 :         .action([psOptions](const std::string &)
    7887        1073 :                 { psOptions->bExactFieldNameMatch = false; })
    7888             :         .help(_("Do field name matching between source and existing target "
    7889        1073 :                 "layer in a more relaxed way."));
    7890             : 
    7891        1073 :     argParser->add_argument("-fieldTypeToString")
    7892        2146 :         .metavar("All|<type1>[,<type2>]...")
    7893             :         .action(
    7894           0 :             [psOptions](const std::string &s)
    7895             :             {
    7896             :                 psOptions->aosFieldTypesToString =
    7897           0 :                     CSLTokenizeStringComplex(s.c_str(), " ,", FALSE, FALSE);
    7898           0 :                 CSLConstList iter = psOptions->aosFieldTypesToString.List();
    7899           0 :                 while (*iter)
    7900             :                 {
    7901           0 :                     if (IsFieldType(*iter))
    7902             :                     {
    7903             :                         /* Do nothing */
    7904             :                     }
    7905           0 :                     else if (EQUAL(*iter, "All"))
    7906             :                     {
    7907           0 :                         psOptions->aosFieldTypesToString.Clear();
    7908           0 :                         psOptions->aosFieldTypesToString.AddString("All");
    7909           0 :                         break;
    7910             :                     }
    7911             :                     else
    7912             :                     {
    7913             :                         throw std::invalid_argument(CPLSPrintf(
    7914             :                             "Unhandled type for fieldTypeToString option : %s",
    7915           0 :                             *iter));
    7916             :                     }
    7917           0 :                     iter++;
    7918             :                 }
    7919        1073 :             })
    7920             :         .help(_("Converts any field of the specified type to a field of type "
    7921        1073 :                 "string in the destination layer."));
    7922             : 
    7923        1073 :     argParser->add_argument("-mapFieldType")
    7924        2146 :         .metavar("<srctype>|All=<dsttype>[,<srctype2>=<dsttype2>]...")
    7925             :         .action(
    7926          12 :             [psOptions](const std::string &s)
    7927             :             {
    7928             :                 psOptions->aosMapFieldType =
    7929           4 :                     CSLTokenizeStringComplex(s.c_str(), " ,", FALSE, FALSE);
    7930           4 :                 CSLConstList iter = psOptions->aosMapFieldType.List();
    7931           8 :                 while (*iter)
    7932             :                 {
    7933           4 :                     char *pszKey = nullptr;
    7934           4 :                     const char *pszValue = CPLParseNameValue(*iter, &pszKey);
    7935           4 :                     if (pszKey && pszValue)
    7936             :                     {
    7937           8 :                         if (!((IsFieldType(pszKey) || EQUAL(pszKey, "All")) &&
    7938           4 :                               IsFieldType(pszValue)))
    7939             :                         {
    7940           0 :                             CPLFree(pszKey);
    7941             :                             throw std::invalid_argument(CPLSPrintf(
    7942           0 :                                 "Invalid value for -mapFieldType : %s", *iter));
    7943             :                         }
    7944             :                     }
    7945           4 :                     CPLFree(pszKey);
    7946           4 :                     iter++;
    7947             :                 }
    7948        1077 :             })
    7949        1073 :         .help(_("Converts any field of the specified type to another type."));
    7950             : 
    7951        1073 :     argParser->add_argument("-fieldmap")
    7952        2146 :         .metavar("<field_1>[,<field_2>]...")
    7953             :         .action(
    7954           4 :             [psOptions](const std::string &s)
    7955             :             {
    7956             :                 psOptions->aosFieldMap =
    7957           2 :                     CSLTokenizeStringComplex(s.c_str(), ",", FALSE, FALSE);
    7958        1073 :             })
    7959             :         .help(_("Specifies the list of field indexes to be copied from the "
    7960        1073 :                 "source to the destination."));
    7961             : 
    7962        1073 :     argParser->add_argument("-splitlistfields")
    7963        1073 :         .store_into(psOptions->bSplitListFields)
    7964             :         .help(_("Split fields of type list type into as many fields of scalar "
    7965        1073 :                 "type as necessary."));
    7966             : 
    7967        1073 :     argParser->add_argument("-maxsubfields")
    7968        2146 :         .metavar("<n>")
    7969        1073 :         .scan<'i', int>()
    7970             :         .action(
    7971           0 :             [psOptions](const std::string &s)
    7972             :             {
    7973           0 :                 const int nVal = atoi(s.c_str());
    7974           0 :                 if (nVal > 0)
    7975             :                 {
    7976           0 :                     psOptions->nMaxSplitListSubFields = nVal;
    7977             :                 }
    7978        1073 :             })
    7979             :         .help(_("To be combined with -splitlistfields to limit the number of "
    7980        1073 :                 "subfields created for each split field."));
    7981             : 
    7982        1073 :     argParser->add_argument("-emptyStrAsNull")
    7983        1073 :         .store_into(psOptions->bEmptyStrAsNull)
    7984        1073 :         .help(_("Treat empty string values as null."));
    7985             : 
    7986        1073 :     argParser->add_argument("-forceNullable")
    7987        1073 :         .store_into(psOptions->bForceNullable)
    7988             :         .help(_("Do not propagate not-nullable constraints to target layer if "
    7989        1073 :                 "they exist in source layer."));
    7990             : 
    7991        1073 :     argParser->add_argument("-unsetFieldWidth")
    7992        1073 :         .store_into(psOptions->bUnsetFieldWidth)
    7993        1073 :         .help(_("Set field width and precision to 0."));
    7994             : 
    7995        1073 :     argParser->add_argument("-unsetDefault")
    7996        1073 :         .store_into(psOptions->bUnsetDefault)
    7997             :         .help(_("Do not propagate default field values to target layer if they "
    7998        1073 :                 "exist in source layer."));
    7999             : 
    8000        1073 :     argParser->add_argument("-resolveDomains")
    8001        1073 :         .store_into(psOptions->bResolveDomains)
    8002             :         .help(_("Cause any selected field that is linked to a coded field "
    8003        1073 :                 "domain will be accompanied by an additional field."));
    8004             : 
    8005        1073 :     argParser->add_argument("-dateTimeTo")
    8006        2146 :         .metavar("UTC|UTC(+|-)<HH>|UTC(+|-)<HH>:<MM>")
    8007             :         .action(
    8008          33 :             [psOptions](const std::string &s)
    8009             :             {
    8010          13 :                 const char *pszFormat = s.c_str();
    8011          13 :                 if (EQUAL(pszFormat, "UTC"))
    8012             :                 {
    8013           1 :                     psOptions->nTZOffsetInSec = 0;
    8014             :                 }
    8015          12 :                 else if (STARTS_WITH_CI(pszFormat, "UTC") &&
    8016          11 :                          (strlen(pszFormat) == strlen("UTC+HH") ||
    8017           9 :                           strlen(pszFormat) == strlen("UTC+HH:MM")) &&
    8018           7 :                          (pszFormat[3] == '+' || pszFormat[3] == '-'))
    8019             :                 {
    8020           6 :                     const int nHour = atoi(pszFormat + strlen("UTC+"));
    8021           6 :                     if (nHour < 0 || nHour > 14)
    8022             :                     {
    8023           1 :                         throw std::invalid_argument("Invalid UTC hour offset.");
    8024             :                     }
    8025           5 :                     else if (strlen(pszFormat) == strlen("UTC+HH"))
    8026             :                     {
    8027           0 :                         psOptions->nTZOffsetInSec = nHour * 3600;
    8028           0 :                         if (pszFormat[3] == '-')
    8029           0 :                             psOptions->nTZOffsetInSec =
    8030           0 :                                 -psOptions->nTZOffsetInSec;
    8031             :                     }
    8032             :                     else  // if( strlen(pszFormat) == strlen("UTC+HH:MM") )
    8033             :                     {
    8034           5 :                         const int nMin = atoi(pszFormat + strlen("UTC+HH:"));
    8035           5 :                         if (nMin == 0 || nMin == 15 || nMin == 30 || nMin == 45)
    8036             :                         {
    8037           4 :                             psOptions->nTZOffsetInSec =
    8038           4 :                                 nHour * 3600 + nMin * 60;
    8039           4 :                             if (pszFormat[3] == '-')
    8040           3 :                                 psOptions->nTZOffsetInSec =
    8041           3 :                                     -psOptions->nTZOffsetInSec;
    8042             :                         }
    8043             :                     }
    8044             :                 }
    8045          12 :                 if (psOptions->nTZOffsetInSec == TZ_OFFSET_INVALID)
    8046             :                 {
    8047             :                     throw std::invalid_argument(
    8048             :                         "Value of -dateTimeTo should be UTC, UTC(+|-)HH or "
    8049           7 :                         "UTC(+|-)HH:MM with HH in [0,14] and MM=00,15,30,45");
    8050             :                 }
    8051        1078 :             })
    8052             :         .help(_("Converts date time values from the timezone specified in the "
    8053        1073 :                 "source value to the target timezone."));
    8054             : 
    8055        1073 :     argParser->add_argument("-noNativeData")
    8056        1073 :         .flag()
    8057           1 :         .action([psOptions](const std::string &)
    8058        1073 :                 { psOptions->bNativeData = false; })
    8059        1073 :         .help(_("Disable copying of native data."));
    8060             : 
    8061             :     ///////////////////////////////////////////////////////////////////////
    8062        1073 :     argParser->add_group("Advanced geometry and SRS related options");
    8063             : 
    8064        1073 :     argParser->add_argument("-dim")
    8065        2146 :         .metavar("layer_dim|2|XY|3|XYZ|XYM|XYZM")
    8066             :         .action(
    8067          24 :             [psOptions](const std::string &osDim)
    8068             :             {
    8069          12 :                 if (EQUAL(osDim.c_str(), "layer_dim"))
    8070           2 :                     psOptions->nCoordDim = COORD_DIM_LAYER_DIM;
    8071          18 :                 else if (EQUAL(osDim.c_str(), "XY") ||
    8072           8 :                          EQUAL(osDim.c_str(), "2"))
    8073           3 :                     psOptions->nCoordDim = 2;
    8074          12 :                 else if (EQUAL(osDim.c_str(), "XYZ") ||
    8075           5 :                          EQUAL(osDim.c_str(), "3"))
    8076           3 :                     psOptions->nCoordDim = 3;
    8077           4 :                 else if (EQUAL(osDim.c_str(), "XYM"))
    8078           2 :                     psOptions->nCoordDim = COORD_DIM_XYM;
    8079           2 :                 else if (EQUAL(osDim.c_str(), "XYZM"))
    8080           2 :                     psOptions->nCoordDim = 4;
    8081             :                 else
    8082             :                 {
    8083             :                     throw std::invalid_argument(CPLSPrintf(
    8084           0 :                         "-dim %s: value not handled.", osDim.c_str()));
    8085             :                 }
    8086        1085 :             })
    8087        1073 :         .help(_("Force the coordinate dimension."));
    8088             : 
    8089        1073 :     argParser->add_argument("-s_coord_epoch")
    8090        2146 :         .metavar("<epoch>")
    8091        1073 :         .store_into(psOptions->dfSourceCoordinateEpoch)
    8092        1073 :         .help(_("Assign a coordinate epoch, linked with the source SRS."));
    8093             : 
    8094        1073 :     argParser->add_argument("-a_coord_epoch")
    8095        2146 :         .metavar("<epoch>")
    8096        1073 :         .store_into(psOptions->dfOutputCoordinateEpoch)
    8097             :         .help(_("Assign a coordinate epoch, linked with the output SRS when "
    8098        1073 :                 "-a_srs is used."));
    8099             : 
    8100        1073 :     argParser->add_argument("-t_coord_epoch")
    8101        2146 :         .metavar("<epoch>")
    8102        1073 :         .store_into(psOptions->dfOutputCoordinateEpoch)
    8103             :         .help(_("Assign a coordinate epoch, linked with the output SRS when "
    8104        1073 :                 "-t_srs is used."));
    8105             : 
    8106        1073 :     argParser->add_argument("-ct")
    8107        2146 :         .metavar("<pipeline_def>")
    8108             :         .action(
    8109           8 :             [psOptions](const std::string &s)
    8110             :             {
    8111           4 :                 psOptions->osCTPipeline = s;
    8112           4 :                 psOptions->bTransform = true;
    8113        1073 :             })
    8114             :         .help(_("Override the default transformation from the source to the "
    8115        1073 :                 "target CRS."));
    8116             : 
    8117        1073 :     argParser->add_argument("-ct_opt")
    8118        2146 :         .metavar("<NAME>=<VALUE>")
    8119        1073 :         .append()
    8120           0 :         .action([psOptions](const std::string &s)
    8121        1073 :                 { psOptions->aosCTOptions.AddString(s.c_str()); })
    8122        1073 :         .help(_("Coordinate transform option(s)."));
    8123             : 
    8124        1073 :     argParser->add_argument("-spat_srs")
    8125        2146 :         .metavar("<srs_def>")
    8126        1073 :         .store_into(psOptions->osSpatSRSDef)
    8127        1073 :         .help(_("Override spatial filter SRS."));
    8128             : 
    8129        1073 :     argParser->add_argument("-geomfield")
    8130        2146 :         .metavar("<name>")
    8131             :         .action(
    8132           2 :             [psOptions](const std::string &s)
    8133             :             {
    8134           1 :                 psOptions->osGeomField = s;
    8135           1 :                 psOptions->bGeomFieldSet = true;
    8136        1073 :             })
    8137             :         .help(_("Name of the geometry field on which the spatial filter "
    8138        1073 :                 "operates on."));
    8139             : 
    8140        1073 :     argParser->add_argument("-segmentize")
    8141        2146 :         .metavar("<max_dist>")
    8142        1073 :         .store_into(psOptions->dfGeomOpParam)
    8143           2 :         .action([psOptions](const std::string &)
    8144        1073 :                 { psOptions->eGeomOp = GEOMOP_SEGMENTIZE; })
    8145        1073 :         .help(_("Maximum distance between 2 nodes."));
    8146             : 
    8147        1073 :     argParser->add_argument("-simplify")
    8148        2146 :         .metavar("<tolerance>")
    8149        1073 :         .store_into(psOptions->dfGeomOpParam)
    8150           1 :         .action([psOptions](const std::string &)
    8151        1073 :                 { psOptions->eGeomOp = GEOMOP_SIMPLIFY_PRESERVE_TOPOLOGY; })
    8152        1073 :         .help(_("Distance tolerance for simplification."));
    8153             : 
    8154        1073 :     argParser->add_argument("-makevalid")
    8155        1073 :         .flag()
    8156             :         .action(
    8157          10 :             [psOptions](const std::string &)
    8158             :             {
    8159           5 :                 if (!OGRGeometryFactory::haveGEOS())
    8160             :                 {
    8161             :                     throw std::invalid_argument(
    8162           0 :                         "-makevalid only supported for builds against GEOS");
    8163             :                 }
    8164           5 :                 psOptions->bMakeValid = true;
    8165        1078 :             })
    8166             :         .help(_("Fix geometries to be valid regarding the rules of the Simple "
    8167        1073 :                 "Features specification."));
    8168             : 
    8169        1073 :     argParser->add_argument("-skipinvalid")
    8170        1073 :         .flag()
    8171             :         .action(
    8172           2 :             [psOptions](const std::string &)
    8173             :             {
    8174           1 :                 if (!OGRGeometryFactory::haveGEOS())
    8175             :                 {
    8176             :                     throw std::invalid_argument(
    8177           0 :                         "-skipinvalid only supported for builds against GEOS");
    8178             :                 }
    8179           1 :                 psOptions->bSkipInvalidGeom = true;
    8180        1074 :             })
    8181             :         .help(_("Whether to skip features with invalid geometries regarding the"
    8182        1073 :                 "rules of the Simple Features specification."));
    8183             : 
    8184        1073 :     argParser->add_argument("-wrapdateline")
    8185        1073 :         .store_into(psOptions->bWrapDateline)
    8186        1073 :         .help(_("Split geometries crossing the dateline meridian."));
    8187             : 
    8188        1073 :     argParser->add_argument("-datelineoffset")
    8189        2146 :         .metavar("<val_in_degree>")
    8190        1073 :         .default_value(psOptions->dfDateLineOffset)
    8191        1073 :         .store_into(psOptions->dfDateLineOffset)
    8192        1073 :         .help(_("Offset from dateline in degrees."));
    8193             : 
    8194             :     auto &clipsrcArg =
    8195        1073 :         argParser->add_argument("-clipsrc")
    8196             :             .metavar(
    8197        2146 :                 "[<xmin> <ymin> <xmax> <ymax>]|<WKT>|<datasource>|spat_extent")
    8198        1073 :             .help(_("Clip geometries (in source SRS)."));
    8199        1073 :     if (nCountClipSrc > 1)
    8200           1 :         clipsrcArg.nargs(nCountClipSrc);
    8201             : 
    8202        1073 :     argParser->add_argument("-clipsrcsql")
    8203        2146 :         .metavar("<sql_statement>")
    8204        1073 :         .store_into(psOptions->osClipSrcSQL)
    8205             :         .help(_("Select desired geometries from the source clip datasource "
    8206        1073 :                 "using an SQL query."));
    8207             : 
    8208        1073 :     argParser->add_argument("-clipsrclayer")
    8209        2146 :         .metavar("<layername>")
    8210        1073 :         .store_into(psOptions->osClipSrcLayer)
    8211        1073 :         .help(_("Select the named layer from the source clip datasource."));
    8212             : 
    8213        1073 :     argParser->add_argument("-clipsrcwhere")
    8214        2146 :         .metavar("<expression>")
    8215        1073 :         .store_into(psOptions->osClipSrcWhere)
    8216             :         .help(_("Restrict desired geometries from the source clip layer based "
    8217        1073 :                 "on an attribute query."));
    8218             : 
    8219             :     auto &clipdstArg =
    8220        1073 :         argParser->add_argument("-clipdst")
    8221        2146 :             .metavar("[<xmin> <ymin> <xmax> <ymax>]|<WKT>|<datasource>")
    8222        1073 :             .help(_("Clip geometries (in target SRS)."));
    8223        1073 :     if (nCountClipDst > 1)
    8224           2 :         clipdstArg.nargs(nCountClipDst);
    8225             : 
    8226        1073 :     argParser->add_argument("-clipdstsql")
    8227        2146 :         .metavar("<sql_statement>")
    8228        1073 :         .store_into(psOptions->osClipDstSQL)
    8229             :         .help(_("Select desired geometries from the destination clip "
    8230        1073 :                 "datasource using an SQL query."));
    8231             : 
    8232        1073 :     argParser->add_argument("-clipdstlayer")
    8233        2146 :         .metavar("<layername>")
    8234        1073 :         .store_into(psOptions->osClipDstLayer)
    8235             :         .help(
    8236        1073 :             _("Select the named layer from the destination clip datasource."));
    8237             : 
    8238        1073 :     argParser->add_argument("-clipdstwhere")
    8239        2146 :         .metavar("<expression>")
    8240        1073 :         .store_into(psOptions->osClipDstWhere)
    8241             :         .help(_("Restrict desired geometries from the destination clip layer "
    8242        1073 :                 "based on an attribute query."));
    8243             : 
    8244        1073 :     argParser->add_argument("-explodecollections")
    8245        1073 :         .store_into(psOptions->bExplodeCollections)
    8246             :         .help(_("Produce one feature for each geometry in any kind of geometry "
    8247        1073 :                 "collection in the source file."));
    8248             : 
    8249        1073 :     argParser->add_argument("-zfield")
    8250        2146 :         .metavar("<name>")
    8251        1073 :         .store_into(psOptions->osZField)
    8252             :         .help(_("Uses the specified field to fill the Z coordinate of "
    8253        1073 :                 "geometries."));
    8254             : 
    8255        1073 :     argParser->add_argument("-gcp")
    8256             :         .metavar(
    8257        2146 :             "<ungeoref_x> <ungeoref_y> <georef_x> <georef_y> [<elevation>]")
    8258        1073 :         .nargs(4, 5)
    8259        1073 :         .append()
    8260        1073 :         .scan<'g', double>()
    8261        1073 :         .help(_("Add the indicated ground control point."));
    8262             : 
    8263        1073 :     argParser->add_argument("-tps")
    8264        1073 :         .flag()
    8265           1 :         .action([psOptions](const std::string &)
    8266        1073 :                 { psOptions->nTransformOrder = -1; })
    8267             :         .help(_("Force use of thin plate spline transformer based on available "
    8268        1073 :                 "GCPs."));
    8269             : 
    8270        1073 :     argParser->add_argument("-order")
    8271        2146 :         .metavar("1|2|3")
    8272        1073 :         .store_into(psOptions->nTransformOrder)
    8273        1073 :         .help(_("Order of polynomial used for warping."));
    8274             : 
    8275        1073 :     argParser->add_argument("-xyRes")
    8276        2146 :         .metavar("<val>[ m|mm|deg]")
    8277             :         .action(
    8278          25 :             [psOptions](const std::string &s)
    8279             :             {
    8280           9 :                 const char *pszVal = s.c_str();
    8281             : 
    8282           9 :                 char *endptr = nullptr;
    8283           9 :                 psOptions->dfXYRes = CPLStrtodM(pszVal, &endptr);
    8284           9 :                 if (!endptr)
    8285             :                 {
    8286             :                     throw std::invalid_argument(
    8287             :                         "Invalid value for -xyRes. Must be of the form "
    8288           0 :                         "{numeric_value}[ ]?[m|mm|deg]?");
    8289             :                 }
    8290           9 :                 if (*endptr == ' ')
    8291           6 :                     ++endptr;
    8292           9 :                 if (*endptr != 0 && strcmp(endptr, "m") != 0 &&
    8293           5 :                     strcmp(endptr, "mm") != 0 && strcmp(endptr, "deg") != 0)
    8294             :                 {
    8295             :                     throw std::invalid_argument(
    8296             :                         "Invalid value for -xyRes. Must be of the form "
    8297           2 :                         "{numeric_value}[ ]?[m|mm|deg]?");
    8298             :                 }
    8299           7 :                 psOptions->osXYResUnit = endptr;
    8300        1080 :             })
    8301        1073 :         .help(_("Set/override the geometry X/Y coordinate resolution."));
    8302             : 
    8303        1073 :     argParser->add_argument("-zRes")
    8304        2146 :         .metavar("<val>[ m|mm]")
    8305             :         .action(
    8306          16 :             [psOptions](const std::string &s)
    8307             :             {
    8308           6 :                 const char *pszVal = s.c_str();
    8309             : 
    8310           6 :                 char *endptr = nullptr;
    8311           6 :                 psOptions->dfZRes = CPLStrtodM(pszVal, &endptr);
    8312           6 :                 if (!endptr)
    8313             :                 {
    8314             :                     throw std::invalid_argument(
    8315             :                         "Invalid value for -zRes. Must be of the form "
    8316           0 :                         "{numeric_value}[ ]?[m|mm]?");
    8317             :                 }
    8318           6 :                 if (*endptr == ' ')
    8319           4 :                     ++endptr;
    8320           6 :                 if (*endptr != 0 && strcmp(endptr, "m") != 0 &&
    8321           3 :                     strcmp(endptr, "mm") != 0 && strcmp(endptr, "deg") != 0)
    8322             :                 {
    8323             :                     throw std::invalid_argument(
    8324             :                         "Invalid value for -zRes. Must be of the form "
    8325           2 :                         "{numeric_value}[ ]?[m|mm]?");
    8326             :                 }
    8327           4 :                 psOptions->osZResUnit = endptr;
    8328        1077 :             })
    8329        1073 :         .help(_("Set/override the geometry Z coordinate resolution."));
    8330             : 
    8331        1073 :     argParser->add_argument("-mRes")
    8332        2146 :         .metavar("<val>")
    8333        1073 :         .store_into(psOptions->dfMRes)
    8334        1073 :         .help(_("Set/override the geometry M coordinate resolution."));
    8335             : 
    8336        1073 :     argParser->add_argument("-unsetCoordPrecision")
    8337        1073 :         .store_into(psOptions->bUnsetCoordPrecision)
    8338             :         .help(_("Prevent the geometry coordinate resolution from being set on "
    8339        1073 :                 "target layer(s)."));
    8340             : 
    8341             :     ///////////////////////////////////////////////////////////////////////
    8342        1073 :     argParser->add_group("Other options");
    8343             : 
    8344        1073 :     argParser->add_quiet_argument(&psOptions->bQuiet);
    8345             : 
    8346        1073 :     argParser->add_argument("-progress")
    8347        1073 :         .store_into(psOptions->bDisplayProgress)
    8348             :         .help(_("Display progress on terminal. Only works if input layers have "
    8349        1073 :                 "the 'fast feature count' capability."));
    8350             : 
    8351             :     argParser->add_input_format_argument(
    8352             :         psOptionsForBinary ? &psOptionsForBinary->aosAllowInputDrivers
    8353        1073 :                            : nullptr);
    8354             : 
    8355             :     argParser->add_open_options_argument(
    8356        1073 :         psOptionsForBinary ? &(psOptionsForBinary->aosOpenOptions) : nullptr);
    8357             : 
    8358        1073 :     argParser->add_argument("-doo")
    8359        2146 :         .metavar("<NAME>=<VALUE>")
    8360        1073 :         .append()
    8361           0 :         .action([psOptions](const std::string &s)
    8362        1073 :                 { psOptions->aosDestOpenOptions.AddString(s.c_str()); })
    8363        1073 :         .help(_("Open option(s) for output dataset."));
    8364             : 
    8365        1073 :     argParser->add_usage_newline();
    8366             : 
    8367        1073 :     argParser->add_argument("-fid")
    8368        2146 :         .metavar("<FID>")
    8369        1073 :         .store_into(psOptions->nFIDToFetch)
    8370             :         .help(_("If provided, only the feature with the specified feature id "
    8371        1073 :                 "will be processed."));
    8372             : 
    8373        1073 :     argParser->add_argument("-preserve_fid")
    8374        1073 :         .store_into(psOptions->bPreserveFID)
    8375             :         .help(_("Use the FID of the source features instead of letting the "
    8376        1073 :                 "output driver automatically assign a new one."));
    8377             : 
    8378        1073 :     argParser->add_argument("-unsetFid")
    8379        1073 :         .store_into(psOptions->bUnsetFid)
    8380             :         .help(_("Prevent the name of the source FID column and source feature "
    8381        1073 :                 "IDs from being reused."));
    8382             : 
    8383             :     {
    8384        1073 :         auto &group = argParser->add_mutually_exclusive_group();
    8385        1073 :         group.add_argument("-skip", "-skipfailures")
    8386        1073 :             .flag()
    8387             :             .action(
    8388          12 :                 [psOptions](const std::string &)
    8389             :                 {
    8390          12 :                     psOptions->bSkipFailures = true;
    8391          12 :                     psOptions->nGroupTransactions = 1; /* #2409 */
    8392        1073 :                 })
    8393        1073 :             .help(_("Continue after a failure, skipping the failed feature."));
    8394             : 
    8395        1073 :         auto &arg = group.add_argument("-gt")
    8396        2146 :                         .metavar("<n>|unlimited")
    8397             :                         .action(
    8398           8 :                             [psOptions](const std::string &s)
    8399             :                             {
    8400             :                                 /* If skipfailures is already set we should not
    8401             :                modify nGroupTransactions = 1  #2409 */
    8402           4 :                                 if (!psOptions->bSkipFailures)
    8403             :                                 {
    8404           4 :                                     if (EQUAL(s.c_str(), "unlimited"))
    8405           1 :                                         psOptions->nGroupTransactions = -1;
    8406             :                                     else
    8407           3 :                                         psOptions->nGroupTransactions =
    8408           3 :                                             atoi(s.c_str());
    8409             :                                 }
    8410        1073 :                             })
    8411        1073 :                         .help(_("Group <n> features per transaction "));
    8412             : 
    8413        1073 :         argParser->add_hidden_alias_for(arg, "tg");
    8414             :     }
    8415             : 
    8416        1073 :     argParser->add_argument("-limit")
    8417        2146 :         .metavar("<nb_features>")
    8418        1073 :         .store_into(psOptions->nLimit)
    8419        1073 :         .help(_("Limit the number of features per layer."));
    8420             : 
    8421        1073 :     argParser->add_argument("-ds_transaction")
    8422        1073 :         .flag()
    8423             :         .action(
    8424           1 :             [psOptions](const std::string &)
    8425             :             {
    8426           1 :                 psOptions->nLayerTransaction = FALSE;
    8427           1 :                 psOptions->bForceTransaction = true;
    8428        1073 :             })
    8429        1073 :         .help(_("Force the use of a dataset level transaction."));
    8430             : 
    8431             :     /* Undocumented. Just a provision. Default behavior should be OK */
    8432        1073 :     argParser->add_argument("-lyr_transaction")
    8433        1073 :         .flag()
    8434        1073 :         .hidden()
    8435           0 :         .action([psOptions](const std::string &)
    8436        1073 :                 { psOptions->nLayerTransaction = TRUE; })
    8437        1073 :         .help(_("Force the use of a layer level transaction."));
    8438             : 
    8439             :     argParser->add_metadata_item_options_argument(
    8440        1073 :         psOptions->aosMetadataOptions);
    8441             : 
    8442        1073 :     argParser->add_argument("-nomd")
    8443        1073 :         .flag()
    8444           4 :         .action([psOptions](const std::string &)
    8445        1073 :                 { psOptions->bCopyMD = false; })
    8446             :         .help(_("Disable copying of metadata from source dataset and layers "
    8447        1073 :                 "into target dataset and layers."));
    8448             : 
    8449             :     // Undocumented option used by gdal vector convert
    8450        1073 :     argParser->add_argument("--no-overwrite")
    8451        1073 :         .store_into(psOptions->bNoOverwrite)
    8452        1073 :         .hidden();
    8453             : 
    8454             :     // Undocumented option used by gdal vector * algorithms
    8455        1073 :     argParser->add_argument("--invoked-from-gdal-algorithm")
    8456        1073 :         .store_into(psOptions->bInvokedFromGdalAlgorithm)
    8457        1073 :         .hidden();
    8458             : 
    8459        1073 :     if (psOptionsForBinary)
    8460             :     {
    8461         133 :         argParser->add_argument("dst_dataset_name")
    8462         266 :             .metavar("<dst_dataset_name>")
    8463         133 :             .store_into(psOptionsForBinary->osDestDataSource)
    8464         133 :             .help(_("Output dataset."));
    8465             : 
    8466         133 :         argParser->add_argument("src_dataset_name")
    8467         266 :             .metavar("<src_dataset_name>")
    8468         133 :             .store_into(psOptionsForBinary->osDataSource)
    8469         133 :             .help(_("Input dataset."));
    8470             :     }
    8471             : 
    8472        1073 :     argParser->add_argument("layer")
    8473        1073 :         .remaining()
    8474        2146 :         .metavar("<layer_name>")
    8475        1073 :         .help(_("Layer name"));
    8476        1073 :     return argParser;
    8477             : }
    8478             : 
    8479             : /************************************************************************/
    8480             : /*                 GDALVectorTranslateGetParserUsage()                  */
    8481             : /************************************************************************/
    8482             : 
    8483           1 : std::string GDALVectorTranslateGetParserUsage()
    8484             : {
    8485             :     try
    8486             :     {
    8487           2 :         GDALVectorTranslateOptions sOptions;
    8488           2 :         GDALVectorTranslateOptionsForBinary sOptionsForBinary;
    8489             :         auto argParser = GDALVectorTranslateOptionsGetParser(
    8490           2 :             &sOptions, &sOptionsForBinary, 1, 1);
    8491           1 :         return argParser->usage();
    8492             :     }
    8493           0 :     catch (const std::exception &err)
    8494             :     {
    8495           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Unexpected exception: %s",
    8496           0 :                  err.what());
    8497           0 :         return std::string();
    8498             :     }
    8499             : }
    8500             : 
    8501             : /************************************************************************/
    8502             : /*                  CHECK_HAS_ENOUGH_ADDITIONAL_ARGS()                  */
    8503             : /************************************************************************/
    8504             : 
    8505             : #ifndef CheckHasEnoughAdditionalArgs_defined
    8506             : #define CheckHasEnoughAdditionalArgs_defined
    8507             : 
    8508          57 : static bool CheckHasEnoughAdditionalArgs(CSLConstList papszArgv, int i,
    8509             :                                          int nExtraArg, int nArgc)
    8510             : {
    8511          57 :     if (i + nExtraArg >= nArgc)
    8512             :     {
    8513           2 :         CPLError(CE_Failure, CPLE_IllegalArg,
    8514           2 :                  "%s option requires %d argument%s", papszArgv[i], nExtraArg,
    8515             :                  nExtraArg == 1 ? "" : "s");
    8516           2 :         return false;
    8517             :     }
    8518          55 :     return true;
    8519             : }
    8520             : #endif
    8521             : 
    8522             : #define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg)                            \
    8523             :     if (!CheckHasEnoughAdditionalArgs(papszArgv, i, nExtraArg, nArgc))         \
    8524             :     {                                                                          \
    8525             :         return nullptr;                                                        \
    8526             :     }
    8527             : 
    8528             : /************************************************************************/
    8529             : /*                   GDALVectorTranslateOptionsNew()                    */
    8530             : /************************************************************************/
    8531             : 
    8532             : /**
    8533             :  * allocates a GDALVectorTranslateOptions struct.
    8534             :  *
    8535             :  * @param papszArgv NULL terminated list of options (potentially including
    8536             :  * filename and open options too), or NULL. The accepted options are the ones of
    8537             :  * the <a href="/programs/ogr2ogr.html">ogr2ogr</a> utility.
    8538             :  * @param psOptionsForBinary (output) may be NULL (and should generally be
    8539             :  * NULL), otherwise (gdal_translate_bin.cpp use case) must be allocated with
    8540             :  *                           GDALVectorTranslateOptionsForBinaryNew() prior to
    8541             :  * this function. Will be filled with potentially present filename, open
    8542             :  * options,...
    8543             :  * @return pointer to the allocated GDALVectorTranslateOptions struct. Must be
    8544             :  * freed with GDALVectorTranslateOptionsFree().
    8545             :  *
    8546             :  * @since GDAL 2.1
    8547             :  */
    8548        1076 : GDALVectorTranslateOptions *GDALVectorTranslateOptionsNew(
    8549             :     char **papszArgv, GDALVectorTranslateOptionsForBinary *psOptionsForBinary)
    8550             : {
    8551        2152 :     auto psOptions = std::make_unique<GDALVectorTranslateOptions>();
    8552             : 
    8553             :     /* -------------------------------------------------------------------- */
    8554             :     /*      Pre-processing for custom syntax that ArgumentParser does not   */
    8555             :     /*      support.                                                        */
    8556             :     /* -------------------------------------------------------------------- */
    8557             : 
    8558        2152 :     CPLStringList aosArgv;
    8559        1076 :     const int nArgc = CSLCount(papszArgv);
    8560        1076 :     int nCountClipSrc = 0;
    8561        1076 :     int nCountClipDst = 0;
    8562        4941 :     for (int i = 0;
    8563        4941 :          i < nArgc && papszArgv != nullptr && papszArgv[i] != nullptr; i++)
    8564             :     {
    8565        3869 :         if (EQUAL(papszArgv[i], "-gcp"))
    8566             :         {
    8567             :             // repeated argument of varying size: not handled by argparse.
    8568             : 
    8569          18 :             CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(4);
    8570          18 :             char *endptr = nullptr;
    8571             :             /* -gcp pixel line easting northing [elev] */
    8572             : 
    8573          18 :             psOptions->asGCPs.resize(psOptions->asGCPs.size() + 1);
    8574          18 :             auto &sGCP = psOptions->asGCPs.back();
    8575             : 
    8576          18 :             sGCP.Pixel() = CPLAtof(papszArgv[++i]);
    8577          18 :             sGCP.Line() = CPLAtof(papszArgv[++i]);
    8578          18 :             sGCP.X() = CPLAtof(papszArgv[++i]);
    8579          18 :             sGCP.Y() = CPLAtof(papszArgv[++i]);
    8580          33 :             if (papszArgv[i + 1] != nullptr &&
    8581          15 :                 (CPLStrtod(papszArgv[i + 1], &endptr) != 0.0 ||
    8582          15 :                  papszArgv[i + 1][0] == '0'))
    8583             :             {
    8584             :                 /* Check that last argument is really a number and not a
    8585             :                  * filename */
    8586             :                 /* looking like a number (see ticket #863) */
    8587           0 :                 if (endptr && *endptr == 0)
    8588           0 :                     sGCP.Z() = CPLAtof(papszArgv[++i]);
    8589             :             }
    8590             : 
    8591             :             /* should set id and info? */
    8592             :         }
    8593             : 
    8594        3851 :         else if (EQUAL(papszArgv[i], "-clipsrc"))
    8595             :         {
    8596          23 :             if (nCountClipSrc)
    8597             :             {
    8598           1 :                 CPLError(CE_Failure, CPLE_AppDefined, "Duplicate argument %s",
    8599           1 :                          papszArgv[i]);
    8600           1 :                 return nullptr;
    8601             :             }
    8602             :             // argparse doesn't handle well variable number of values
    8603             :             // just before the positional arguments, so we have to detect
    8604             :             // it manually and set the correct number.
    8605          22 :             nCountClipSrc = 1;
    8606          22 :             CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
    8607          24 :             if (CPLGetValueType(papszArgv[i + 1]) != CPL_VALUE_STRING &&
    8608           3 :                 i + 4 < nArgc)
    8609             :             {
    8610           2 :                 nCountClipSrc = 4;
    8611             :             }
    8612             : 
    8613          69 :             for (int j = 0; j < 1 + nCountClipSrc; ++j)
    8614             :             {
    8615          48 :                 aosArgv.AddString(papszArgv[i]);
    8616          48 :                 ++i;
    8617             :             }
    8618          21 :             --i;
    8619             :         }
    8620             : 
    8621        3828 :         else if (EQUAL(papszArgv[i], "-clipdst"))
    8622             :         {
    8623          18 :             if (nCountClipDst)
    8624             :             {
    8625           1 :                 CPLError(CE_Failure, CPLE_AppDefined, "Duplicate argument %s",
    8626           1 :                          papszArgv[i]);
    8627           1 :                 return nullptr;
    8628             :             }
    8629             :             // argparse doesn't handle well variable number of values
    8630             :             // just before the positional arguments, so we have to detect
    8631             :             // it manually and set the correct number.
    8632          17 :             nCountClipDst = 1;
    8633          17 :             CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
    8634          20 :             if (CPLGetValueType(papszArgv[i + 1]) != CPL_VALUE_STRING &&
    8635           4 :                 i + 4 < nArgc)
    8636             :             {
    8637           3 :                 nCountClipDst = 4;
    8638             :             }
    8639             : 
    8640          57 :             for (int j = 0; j < 1 + nCountClipDst; ++j)
    8641             :             {
    8642          41 :                 aosArgv.AddString(papszArgv[i]);
    8643          41 :                 ++i;
    8644             :             }
    8645          16 :             --i;
    8646             :         }
    8647             : 
    8648             :         else
    8649             :         {
    8650        3810 :             aosArgv.AddString(papszArgv[i]);
    8651             :         }
    8652             :     }
    8653             : 
    8654             :     try
    8655             :     {
    8656             :         auto argParser = GDALVectorTranslateOptionsGetParser(
    8657        2144 :             psOptions.get(), psOptionsForBinary, nCountClipSrc, nCountClipDst);
    8658             : 
    8659             :         // Collect non-positional arguments for VectorTranslateFrom() case
    8660        1071 :         psOptions->aosArguments =
    8661        2143 :             argParser->get_non_positional_arguments(aosArgv);
    8662             : 
    8663        1071 :         argParser->parse_args_without_binary_name(aosArgv.List());
    8664             : 
    8665        1048 :         if (psOptionsForBinary)
    8666         131 :             psOptionsForBinary->bQuiet = psOptions->bQuiet;
    8667             : 
    8668        1056 :         if (auto oSpat = argParser->present<std::vector<double>>("-spat"))
    8669             :         {
    8670           8 :             const double dfMinX = (*oSpat)[0];
    8671           8 :             const double dfMinY = (*oSpat)[1];
    8672           8 :             const double dfMaxX = (*oSpat)[2];
    8673           8 :             const double dfMaxY = (*oSpat)[3];
    8674             : 
    8675             :             auto poSpatialFilter =
    8676          16 :                 std::make_shared<OGRPolygon>(dfMinX, dfMinY, dfMaxX, dfMaxY);
    8677           8 :             psOptions->poSpatialFilter = poSpatialFilter;
    8678             :         }
    8679             : 
    8680        1048 :         if (auto oClipSrc =
    8681        1048 :                 argParser->present<std::vector<std::string>>("-clipsrc"))
    8682             :         {
    8683          20 :             const std::string &osVal = (*oClipSrc)[0];
    8684             : 
    8685          20 :             psOptions->poClipSrc.reset();
    8686          20 :             psOptions->osClipSrcDS.clear();
    8687             : 
    8688             :             VSIStatBufL sStat;
    8689          20 :             psOptions->bClipSrc = true;
    8690          20 :             if (oClipSrc->size() == 4)
    8691             :             {
    8692           1 :                 const double dfMinX = CPLAtofM((*oClipSrc)[0].c_str());
    8693           1 :                 const double dfMinY = CPLAtofM((*oClipSrc)[1].c_str());
    8694           1 :                 const double dfMaxX = CPLAtofM((*oClipSrc)[2].c_str());
    8695           1 :                 const double dfMaxY = CPLAtofM((*oClipSrc)[3].c_str());
    8696             : 
    8697           2 :                 OGRLinearRing oRing;
    8698             : 
    8699           1 :                 oRing.addPoint(dfMinX, dfMinY);
    8700           1 :                 oRing.addPoint(dfMinX, dfMaxY);
    8701           1 :                 oRing.addPoint(dfMaxX, dfMaxY);
    8702           1 :                 oRing.addPoint(dfMaxX, dfMinY);
    8703           1 :                 oRing.addPoint(dfMinX, dfMinY);
    8704             : 
    8705           2 :                 auto poPoly = std::make_shared<OGRPolygon>();
    8706           1 :                 psOptions->poClipSrc = poPoly;
    8707           1 :                 poPoly->addRing(&oRing);
    8708             :             }
    8709          19 :             else if ((STARTS_WITH_CI(osVal.c_str(), "POLYGON") ||
    8710          27 :                       STARTS_WITH_CI(osVal.c_str(), "MULTIPOLYGON")) &&
    8711           8 :                      VSIStatL(osVal.c_str(), &sStat) != 0)
    8712             :             {
    8713           8 :                 psOptions->poClipSrc =
    8714          16 :                     OGRGeometryFactory::createFromWkt(osVal.c_str()).first;
    8715           8 :                 if (psOptions->poClipSrc == nullptr)
    8716             :                 {
    8717           0 :                     CPLError(
    8718             :                         CE_Failure, CPLE_IllegalArg,
    8719             :                         "Invalid -clipsrc geometry. Must be a valid POLYGON or "
    8720             :                         "MULTIPOLYGON WKT");
    8721           0 :                     return nullptr;
    8722             :                 }
    8723             :             }
    8724          11 :             else if (EQUAL(osVal.c_str(), "spat_extent"))
    8725             :             {
    8726             :                 // Nothing to do
    8727             :             }
    8728             :             else
    8729             :             {
    8730          10 :                 psOptions->osClipSrcDS = osVal;
    8731             :             }
    8732             :         }
    8733             : 
    8734        1048 :         if (auto oClipDst =
    8735        1048 :                 argParser->present<std::vector<std::string>>("-clipdst"))
    8736             :         {
    8737          15 :             const std::string &osVal = (*oClipDst)[0];
    8738             : 
    8739          15 :             psOptions->poClipDst.reset();
    8740          15 :             psOptions->osClipDstDS.clear();
    8741             : 
    8742             :             VSIStatBufL sStat;
    8743          15 :             if (oClipDst->size() == 4)
    8744             :             {
    8745           2 :                 const double dfMinX = CPLAtofM((*oClipDst)[0].c_str());
    8746           2 :                 const double dfMinY = CPLAtofM((*oClipDst)[1].c_str());
    8747           2 :                 const double dfMaxX = CPLAtofM((*oClipDst)[2].c_str());
    8748           2 :                 const double dfMaxY = CPLAtofM((*oClipDst)[3].c_str());
    8749             : 
    8750             :                 auto poPoly = std::make_shared<OGRPolygon>(dfMinX, dfMinY,
    8751           4 :                                                            dfMaxX, dfMaxY);
    8752           2 :                 psOptions->poClipDst = poPoly;
    8753             :             }
    8754          13 :             else if ((STARTS_WITH_CI(osVal.c_str(), "POLYGON") ||
    8755          16 :                       STARTS_WITH_CI(osVal.c_str(), "MULTIPOLYGON")) &&
    8756           3 :                      VSIStatL(osVal.c_str(), &sStat) != 0)
    8757             :             {
    8758           3 :                 psOptions->poClipDst =
    8759           6 :                     OGRGeometryFactory::createFromWkt(osVal.c_str()).first;
    8760           3 :                 if (psOptions->poClipDst == nullptr)
    8761             :                 {
    8762           0 :                     CPLError(
    8763             :                         CE_Failure, CPLE_IllegalArg,
    8764             :                         "Invalid -clipdst geometry. Must be a valid POLYGON or "
    8765             :                         "MULTIPOLYGON WKT");
    8766           0 :                     return nullptr;
    8767             :                 }
    8768             :             }
    8769             :             else
    8770             :             {
    8771          10 :                 psOptions->osClipDstDS = osVal;
    8772             :             }
    8773             :         }
    8774             : 
    8775        2096 :         auto layers = argParser->present<std::vector<std::string>>("layer");
    8776        1048 :         if (layers)
    8777             :         {
    8778          58 :             for (const auto &layer : *layers)
    8779             :             {
    8780          40 :                 psOptions->aosLayers.AddString(layer.c_str());
    8781             :             }
    8782             :         }
    8783        1048 :         if (psOptionsForBinary)
    8784             :         {
    8785         131 :             psOptionsForBinary->eAccessMode = psOptions->eAccessMode;
    8786         131 :             psOptionsForBinary->osFormat = psOptions->osFormat;
    8787             : 
    8788         131 :             if (!(CPLTestBool(
    8789             :                     psOptionsForBinary->aosOpenOptions.FetchNameValueDef(
    8790             :                         "NATIVE_DATA",
    8791             :                         psOptionsForBinary->aosOpenOptions.FetchNameValueDef(
    8792             :                             "@NATIVE_DATA", "TRUE")))))
    8793             :             {
    8794           0 :                 psOptions->bNativeData = false;
    8795             :             }
    8796             : 
    8797         131 :             if (psOptions->bNativeData &&
    8798         130 :                 psOptionsForBinary->aosOpenOptions.FetchNameValue(
    8799         261 :                     "NATIVE_DATA") == nullptr &&
    8800         130 :                 psOptionsForBinary->aosOpenOptions.FetchNameValue(
    8801             :                     "@NATIVE_DATA") == nullptr)
    8802             :             {
    8803             :                 psOptionsForBinary->aosOpenOptions.AddString(
    8804         130 :                     "@NATIVE_DATA=YES");
    8805             :             }
    8806             :         }
    8807             : 
    8808        1048 :         return psOptions.release();
    8809             :     }
    8810          24 :     catch (const std::exception &err)
    8811             :     {
    8812          24 :         CPLError(CE_Failure, CPLE_AppDefined, "%s", err.what());
    8813          24 :         if (psOptionsForBinary)
    8814           1 :             psOptionsForBinary->bShowUsageIfError = true;
    8815          24 :         return nullptr;
    8816             :     }
    8817             : }
    8818             : 
    8819             : /************************************************************************/
    8820             : /*                   GDALVectorTranslateOptionsFree()                   */
    8821             : /************************************************************************/
    8822             : 
    8823             : /**
    8824             :  * Frees the GDALVectorTranslateOptions struct.
    8825             :  *
    8826             :  * @param psOptions the options struct for GDALVectorTranslate().
    8827             :  * @since GDAL 2.1
    8828             :  */
    8829             : 
    8830        1046 : void GDALVectorTranslateOptionsFree(GDALVectorTranslateOptions *psOptions)
    8831             : {
    8832        1046 :     delete psOptions;
    8833        1046 : }
    8834             : 
    8835             : /************************************************************************/
    8836             : /*               GDALVectorTranslateOptionsSetProgress()                */
    8837             : /************************************************************************/
    8838             : 
    8839             : /**
    8840             :  * Set a progress function.
    8841             :  *
    8842             :  * @param psOptions the options struct for GDALVectorTranslate().
    8843             :  * @param pfnProgress the progress callback.
    8844             :  * @param pProgressData the user data for the progress callback.
    8845             :  *
    8846             :  * @since GDAL 2.1
    8847             :  */
    8848             : 
    8849         373 : void GDALVectorTranslateOptionsSetProgress(
    8850             :     GDALVectorTranslateOptions *psOptions, GDALProgressFunc pfnProgress,
    8851             :     void *pProgressData)
    8852             : {
    8853         373 :     psOptions->pfnProgress = pfnProgress ? pfnProgress : GDALDummyProgress;
    8854         373 :     psOptions->pProgressData = pProgressData;
    8855         373 :     if (pfnProgress == GDALTermProgress)
    8856         129 :         psOptions->bQuiet = false;
    8857         373 : }
    8858             : 
    8859             : #undef CHECK_HAS_ENOUGH_ADDITIONAL_ARGS

Generated by: LCOV version 1.14