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

Generated by: LCOV version 1.14