LCOV - code coverage report
Current view: top level - alg - gdaltransformer.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 1685 1907 88.4 %
Date: 2025-12-05 02:43:06 Functions: 62 66 93.9 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Mapinfo Image Warper
       4             :  * Purpose:  Implementation of one or more GDALTrasformerFunc types, including
       5             :  *           the GenImgProj (general image reprojector) transformer.
       6             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2002, i3 - information integration and imaging
      10             :  *                          Fort Collin, CO
      11             :  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
      12             :  * Copyright (c) 2021, CLS
      13             :  *
      14             :  * SPDX-License-Identifier: MIT
      15             :  ****************************************************************************/
      16             : 
      17             : #include "cpl_port.h"
      18             : #include "gdal_alg.h"
      19             : #include "gdal_alg_priv.h"
      20             : 
      21             : #include <climits>
      22             : #include <cmath>
      23             : #include <cstddef>
      24             : #include <cstdlib>
      25             : #include <cstring>
      26             : 
      27             : #include <algorithm>
      28             : #include <limits>
      29             : #include <utility>
      30             : 
      31             : #include "cpl_conv.h"
      32             : #include "cpl_error.h"
      33             : #include "cpl_list.h"
      34             : #include "cpl_minixml.h"
      35             : #include "cpl_multiproc.h"
      36             : #include "cpl_string.h"
      37             : #include "cpl_vsi.h"
      38             : #include "gdal.h"
      39             : #include "gdal_priv.h"
      40             : #include "ogr_core.h"
      41             : #include "ogr_spatialref.h"
      42             : #include "ogr_srs_api.h"
      43             : 
      44             : CPL_C_START
      45             : void *GDALDeserializeGCPTransformer(CPLXMLNode *psTree);
      46             : void *GDALDeserializeTPSTransformer(CPLXMLNode *psTree);
      47             : void *GDALDeserializeGeoLocTransformer(CPLXMLNode *psTree);
      48             : void *GDALDeserializeRPCTransformer(CPLXMLNode *psTree);
      49             : void *GDALDeserializeHomographyTransformer(CPLXMLNode *psTree);
      50             : CPL_C_END
      51             : 
      52             : static CPLXMLNode *GDALSerializeReprojectionTransformer(void *pTransformArg);
      53             : static void *GDALDeserializeReprojectionTransformer(CPLXMLNode *psTree);
      54             : 
      55             : static CPLXMLNode *GDALSerializeGenImgProjTransformer(void *pTransformArg);
      56             : static void *GDALDeserializeGenImgProjTransformer(CPLXMLNode *psTree);
      57             : 
      58             : static void *GDALCreateApproxTransformer2(GDALTransformerFunc pfnRawTransformer,
      59             :                                           void *pRawTransformerArg,
      60             :                                           double dfMaxErrorForward,
      61             :                                           double dfMaxErrorReverse);
      62             : 
      63             : /************************************************************************/
      64             : /*                            GDALIsTransformer()                       */
      65             : /************************************************************************/
      66             : 
      67       14941 : bool GDALIsTransformer(void *hTransformerArg, const char *pszClassName)
      68             : {
      69       14941 :     if (!hTransformerArg)
      70         612 :         return false;
      71             :     // All transformers should have a GDALTransformerInfo member as their first members
      72       14329 :     GDALTransformerInfo *psInfo =
      73             :         static_cast<GDALTransformerInfo *>(hTransformerArg);
      74       14329 :     return memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
      75       27764 :                   strlen(GDAL_GTI2_SIGNATURE)) == 0 &&
      76       27764 :            strcmp(psInfo->pszClassName, pszClassName) == 0;
      77             : }
      78             : 
      79             : /************************************************************************/
      80             : /*                          GDALTransformFunc                           */
      81             : /*                                                                      */
      82             : /*      Documentation for GDALTransformFunc typedef.                    */
      83             : /************************************************************************/
      84             : 
      85             : /*!
      86             : 
      87             : \typedef typedef int (*GDALTransformerFunc)( void *pTransformerArg, int
      88             : bDstToSrc, int nPointCount, double *x, double *y, double *z, int *panSuccess );
      89             : 
      90             : Generic signature for spatial point transformers.
      91             : 
      92             : This function signature is used for a variety of functions that accept
      93             : passed in functions used to transform point locations between two coordinate
      94             : spaces.
      95             : 
      96             : The GDALCreateGenImgProjTransformer(), GDALCreateReprojectionTransformerEx(),
      97             : GDALCreateGCPTransformer() and GDALCreateApproxTransformer() functions can
      98             : be used to prepare argument data for some built-in transformers.  As well,
      99             : applications can implement their own transformers to the following signature.
     100             : 
     101             : \code
     102             : typedef int
     103             : (*GDALTransformerFunc)( void *pTransformerArg,
     104             :                         int bDstToSrc, int nPointCount,
     105             :                         double *x, double *y, double *z, int *panSuccess );
     106             : \endcode
     107             : 
     108             : @param pTransformerArg application supplied callback data used by the
     109             : transformer.
     110             : 
     111             : @param bDstToSrc if TRUE the transformation will be from the destination
     112             : coordinate space to the source coordinate system, otherwise the transformation
     113             : will be from the source coordinate system to the destination coordinate system.
     114             : 
     115             : @param nPointCount number of points in the x, y and z arrays.
     116             : 
     117             : @param[in,out] x input X coordinates.  Results returned in same array.
     118             : 
     119             : @param[in,out] y input Y coordinates.  Results returned in same array.
     120             : 
     121             : @param[in,out] z input Z coordinates.  Results returned in same array.
     122             : 
     123             : @param[out] panSuccess array of ints in which success (TRUE) or failure (FALSE)
     124             : flags are returned for the translation of each point. Must not be NULL.
     125             : 
     126             : @return TRUE if all points have been successfully transformed (changed in 3.11,
     127             : previously was TRUE if some points have been successfully transformed)
     128             : 
     129             : */
     130             : 
     131             : /************************************************************************/
     132             : /*                      GDALSuggestedWarpOutput()                       */
     133             : /************************************************************************/
     134             : 
     135             : /**
     136             :  * Suggest output file size.
     137             :  *
     138             :  * This function is used to suggest the size, and georeferenced extents
     139             :  * appropriate given the indicated transformation and input file.  It walks
     140             :  * the edges of the input file (approximately 20 sample points along each
     141             :  * edge) transforming into output coordinates in order to get an extents box.
     142             :  *
     143             :  * Then a resolution is computed with the intent that the length of the
     144             :  * distance from the top left corner of the output imagery to the bottom right
     145             :  * corner would represent the same number of pixels as in the source image.
     146             :  * Note that if the image is somewhat rotated the diagonal taken isn't of the
     147             :  * whole output bounding rectangle, but instead of the locations where the
     148             :  * top/left and bottom/right corners transform.  The output pixel size is
     149             :  * always square.  This is intended to approximately preserve the resolution
     150             :  * of the input data in the output file.
     151             :  *
     152             :  * The values returned in padfGeoTransformOut, pnPixels and pnLines are
     153             :  * the suggested number of pixels and lines for the output file, and the
     154             :  * geotransform relating those pixels to the output georeferenced coordinates.
     155             :  *
     156             :  * The trickiest part of using the function is ensuring that the
     157             :  * transformer created is from source file pixel/line coordinates to
     158             :  * output file georeferenced coordinates.  This can be accomplished with
     159             :  * GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.
     160             :  *
     161             :  * @param hSrcDS the input image (it is assumed the whole input image is
     162             :  * being transformed).
     163             :  * @param pfnTransformer the transformer function.
     164             :  * @param pTransformArg the callback data for the transformer function.
     165             :  * @param padfGeoTransformOut the array of six doubles in which the suggested
     166             :  * geotransform is returned.
     167             :  * @param pnPixels int in which the suggest pixel width of output is returned.
     168             :  * @param pnLines int in which the suggest pixel height of output is returned.
     169             :  *
     170             :  * @return CE_None if successful or CE_Failure otherwise.
     171             :  */
     172             : 
     173          41 : CPLErr CPL_STDCALL GDALSuggestedWarpOutput(GDALDatasetH hSrcDS,
     174             :                                            GDALTransformerFunc pfnTransformer,
     175             :                                            void *pTransformArg,
     176             :                                            double *padfGeoTransformOut,
     177             :                                            int *pnPixels, int *pnLines)
     178             : 
     179             : {
     180          41 :     VALIDATE_POINTER1(hSrcDS, "GDALSuggestedWarpOutput", CE_Failure);
     181             : 
     182          41 :     double adfExtent[4] = {};
     183             : 
     184          41 :     return GDALSuggestedWarpOutput2(hSrcDS, pfnTransformer, pTransformArg,
     185             :                                     padfGeoTransformOut, pnPixels, pnLines,
     186          41 :                                     adfExtent, 0);
     187             : }
     188             : 
     189         636 : static bool GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
     190             :     GDALTransformerFunc pfnTransformer, void *pTransformArg, double *padfExtent,
     191             :     int /* nPixels*/, int nLines, double dfPixelSizeX, double dfPixelSizeY)
     192             : {
     193         636 :     double adfX[21] = {};
     194         636 :     double adfY[21] = {};
     195             : 
     196         636 :     const double dfMaxXOut = padfExtent[2];
     197         636 :     const double dfMaxYOut = padfExtent[3];
     198             : 
     199             :     // Take 20 steps.
     200         636 :     int nSamplePoints = 0;
     201       13992 :     for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
     202             :     {
     203             :         // Ensure we end exactly at the end.
     204       13356 :         if (dfRatio > 0.99)
     205         636 :             dfRatio = 1.0;
     206             : 
     207             :         // Along right.
     208       13356 :         adfX[nSamplePoints] = dfMaxXOut;
     209       13356 :         adfY[nSamplePoints] = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
     210       13356 :         nSamplePoints++;
     211             :     }
     212         636 :     double adfZ[21] = {};
     213             : 
     214         636 :     int abSuccess[21] = {};
     215             : 
     216         636 :     pfnTransformer(pTransformArg, TRUE, nSamplePoints, adfX, adfY, adfZ,
     217             :                    abSuccess);
     218             : 
     219         636 :     int abSuccess2[21] = {};
     220             : 
     221         636 :     pfnTransformer(pTransformArg, FALSE, nSamplePoints, adfX, adfY, adfZ,
     222             :                    abSuccess2);
     223             : 
     224         636 :     nSamplePoints = 0;
     225         636 :     int nBadCount = 0;
     226       13992 :     for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
     227             :     {
     228       13356 :         const double expected_x = dfMaxXOut;
     229       13356 :         const double expected_y = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
     230       13356 :         if (!abSuccess[nSamplePoints] || !abSuccess2[nSamplePoints] ||
     231       10512 :             fabs(adfX[nSamplePoints] - expected_x) > dfPixelSizeX ||
     232        9138 :             fabs(adfY[nSamplePoints] - expected_y) > dfPixelSizeY)
     233             :         {
     234        4218 :             nBadCount++;
     235             :         }
     236       13356 :         nSamplePoints++;
     237             :     }
     238             : 
     239         636 :     return nBadCount == nSamplePoints;
     240             : }
     241             : 
     242         539 : static bool GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
     243             :     GDALTransformerFunc pfnTransformer, void *pTransformArg, double *padfExtent,
     244             :     int nPixels, int /* nLines */, double dfPixelSizeX, double dfPixelSizeY)
     245             : {
     246         539 :     double adfX[21] = {};
     247         539 :     double adfY[21] = {};
     248             : 
     249         539 :     const double dfMinXOut = padfExtent[0];
     250         539 :     const double dfMinYOut = padfExtent[1];
     251             : 
     252             :     // Take 20 steps.
     253         539 :     int nSamplePoints = 0;
     254       11858 :     for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
     255             :     {
     256             :         // Ensure we end exactly at the end.
     257       11319 :         if (dfRatio > 0.99)
     258         539 :             dfRatio = 1.0;
     259             : 
     260             :         // Along right.
     261       11319 :         adfX[nSamplePoints] = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
     262       11319 :         adfY[nSamplePoints] = dfMinYOut;
     263       11319 :         nSamplePoints++;
     264             :     }
     265         539 :     double adfZ[21] = {};
     266             : 
     267         539 :     int abSuccess[21] = {};
     268             : 
     269         539 :     pfnTransformer(pTransformArg, TRUE, nSamplePoints, adfX, adfY, adfZ,
     270             :                    abSuccess);
     271             : 
     272         539 :     int abSuccess2[21] = {};
     273             : 
     274         539 :     pfnTransformer(pTransformArg, FALSE, nSamplePoints, adfX, adfY, adfZ,
     275             :                    abSuccess2);
     276             : 
     277         539 :     nSamplePoints = 0;
     278         539 :     int nBadCount = 0;
     279       11858 :     for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
     280             :     {
     281       11319 :         const double expected_x = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
     282       11319 :         const double expected_y = dfMinYOut;
     283       11319 :         if (!abSuccess[nSamplePoints] || !abSuccess2[nSamplePoints] ||
     284        9361 :             fabs(adfX[nSamplePoints] - expected_x) > dfPixelSizeX ||
     285        9072 :             fabs(adfY[nSamplePoints] - expected_y) > dfPixelSizeY)
     286             :         {
     287        2247 :             nBadCount++;
     288             :         }
     289       11319 :         nSamplePoints++;
     290             :     }
     291             : 
     292         539 :     return nBadCount == nSamplePoints;
     293             : }
     294             : 
     295             : /************************************************************************/
     296             : /*                      GDALSuggestedWarpOutput2()                      */
     297             : /************************************************************************/
     298             : 
     299             : /**
     300             :  * Suggest output file size.
     301             :  *
     302             :  * This function is used to suggest the size, and georeferenced extents
     303             :  * appropriate given the indicated transformation and input file.  It walks
     304             :  * the edges of the input file (approximately 20 sample points along each
     305             :  * edge) transforming into output coordinates in order to get an extents box.
     306             :  *
     307             :  * Then a resolution is computed with the intent that the length of the
     308             :  * distance from the top left corner of the output imagery to the bottom right
     309             :  * corner would represent the same number of pixels as in the source image.
     310             :  * Note that if the image is somewhat rotated the diagonal taken isn't of the
     311             :  * whole output bounding rectangle, but instead of the locations where the
     312             :  * top/left and bottom/right corners transform.  The output pixel size is
     313             :  * always square.  This is intended to approximately preserve the resolution
     314             :  * of the input data in the output file.
     315             :  *
     316             :  * The values returned in padfGeoTransformOut, pnPixels and pnLines are
     317             :  * the suggested number of pixels and lines for the output file, and the
     318             :  * geotransform relating those pixels to the output georeferenced coordinates.
     319             :  *
     320             :  * The trickiest part of using the function is ensuring that the
     321             :  * transformer created is from source file pixel/line coordinates to
     322             :  * output file georeferenced coordinates.  This can be accomplished with
     323             :  * GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.
     324             :  *
     325             :  * @param hSrcDS the input image (it is assumed the whole input image is
     326             :  * being transformed).
     327             :  * @param pfnTransformer the transformer function.
     328             :  * @param pTransformArg the callback data for the transformer function.
     329             :  * @param padfGeoTransformOut the array of six doubles in which the suggested
     330             :  * geotransform is returned.
     331             :  * @param pnPixels int in which the suggest pixel width of output is returned.
     332             :  * @param pnLines int in which the suggest pixel height of output is returned.
     333             :  * @param padfExtent Four entry array to return extents as (xmin, ymin, xmax,
     334             :  * ymax).
     335             :  * @param nOptions Options flags. Zero or GDAL_SWO_ROUND_UP_SIZE  to ask *pnPixels
     336             :  * and *pnLines to be rounded up instead of being rounded to the closes integer, or
     337             :  * GDAL_SWO_FORCE_SQUARE_PIXEL to indicate that the generated pixel size is a square.
     338             :  *
     339             :  * @return CE_None if successful or CE_Failure otherwise.
     340             :  */
     341             : 
     342        1145 : CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
     343             :                                             GDALTransformerFunc pfnTransformer,
     344             :                                             void *pTransformArg,
     345             :                                             double *padfGeoTransformOut,
     346             :                                             int *pnPixels, int *pnLines,
     347             :                                             double *padfExtent, int nOptions)
     348             : {
     349        1145 :     VALIDATE_POINTER1(hSrcDS, "GDALSuggestedWarpOutput2", CE_Failure);
     350             : 
     351             :     const bool bIsGDALGenImgProjTransform{
     352        2290 :         pTransformArg &&
     353        1145 :         GDALIsTransformer(pTransformArg, GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME)};
     354             : 
     355             :     /* -------------------------------------------------------------------- */
     356             :     /*      Setup sample points all around the edge of the input raster.    */
     357             :     /* -------------------------------------------------------------------- */
     358        1145 :     if (bIsGDALGenImgProjTransform)
     359             :     {
     360             :         // In case CHECK_WITH_INVERT_PROJ has been modified.
     361        1145 :         GDALRefreshGenImgProjTransformer(pTransformArg);
     362             :     }
     363           0 :     else if (GDALIsTransformer(pTransformArg,
     364             :                                GDAL_APPROX_TRANSFORMER_CLASS_NAME))
     365             :     {
     366             :         // In case CHECK_WITH_INVERT_PROJ has been modified.
     367           0 :         GDALRefreshApproxTransformer(pTransformArg);
     368             :     }
     369             : 
     370        1145 :     const int nInXSize = GDALGetRasterXSize(hSrcDS);
     371        1145 :     const int nInYSize = GDALGetRasterYSize(hSrcDS);
     372             : 
     373             :     /* ------------------------------------------------------------- */
     374             :     /* Special case for warping on the same (or null) CRS.           */
     375             :     /* ------------------------------------------------------------- */
     376        1145 :     if ((!nOptions || (nOptions & GDAL_SWO_FORCE_SQUARE_PIXEL) == 0) &&
     377        1144 :         pTransformArg && bIsGDALGenImgProjTransform)
     378             :     {
     379        1144 :         const GDALGenImgProjTransformInfo *psInfo =
     380             :             static_cast<const GDALGenImgProjTransformInfo *>(pTransformArg);
     381             : 
     382        1144 :         if (!psInfo->sSrcParams.pTransformer &&
     383        1071 :             !psInfo->bHasCustomTransformationPipeline &&
     384        1067 :             !psInfo->sDstParams.pTransformer &&
     385        1067 :             psInfo->sSrcParams.adfGeoTransform[2] == 0 &&
     386        1067 :             psInfo->sSrcParams.adfGeoTransform[4] == 0 &&
     387        1067 :             psInfo->sDstParams.adfGeoTransform[0] == 0 &&
     388        1055 :             psInfo->sDstParams.adfGeoTransform[1] == 1 &&
     389        1055 :             psInfo->sDstParams.adfGeoTransform[2] == 0 &&
     390        1055 :             psInfo->sDstParams.adfGeoTransform[3] == 0 &&
     391        1055 :             psInfo->sDstParams.adfGeoTransform[4] == 0 &&
     392        1055 :             psInfo->sDstParams.adfGeoTransform[5] == 1)
     393             :         {
     394        1055 :             const OGRSpatialReference *poSourceCRS = nullptr;
     395        1055 :             const OGRSpatialReference *poTargetCRS = nullptr;
     396             : 
     397        1055 :             if (psInfo->pReprojectArg)
     398             :             {
     399         744 :                 const GDALReprojectionTransformInfo *psRTI =
     400             :                     static_cast<const GDALReprojectionTransformInfo *>(
     401             :                         psInfo->pReprojectArg);
     402         744 :                 poSourceCRS = psRTI->poForwardTransform->GetSourceCS();
     403         744 :                 poTargetCRS = psRTI->poForwardTransform->GetTargetCS();
     404             :             }
     405             : 
     406        1799 :             if ((!poSourceCRS && !poTargetCRS) ||
     407         744 :                 (poSourceCRS && poTargetCRS &&
     408         744 :                  poSourceCRS->IsSame(poTargetCRS)))
     409             :             {
     410             : 
     411         671 :                 const bool bNorthUp{psInfo->sSrcParams.adfGeoTransform[5] <
     412             :                                     0.0};
     413             : 
     414         671 :                 memcpy(padfGeoTransformOut, psInfo->sSrcParams.adfGeoTransform,
     415             :                        sizeof(double) * 6);
     416             : 
     417         671 :                 if (!bNorthUp)
     418             :                 {
     419          69 :                     padfGeoTransformOut[3] = padfGeoTransformOut[3] +
     420          69 :                                              nInYSize * padfGeoTransformOut[5];
     421          69 :                     padfGeoTransformOut[5] = -padfGeoTransformOut[5];
     422             :                 }
     423             : 
     424         671 :                 *pnPixels = nInXSize;
     425         671 :                 *pnLines = nInYSize;
     426             : 
     427             :                 // Calculate extent from hSrcDS
     428         671 :                 if (padfExtent)
     429             :                 {
     430         671 :                     padfExtent[0] = psInfo->sSrcParams.adfGeoTransform[0];
     431         671 :                     padfExtent[1] =
     432         671 :                         psInfo->sSrcParams.adfGeoTransform[3] +
     433         671 :                         nInYSize * psInfo->sSrcParams.adfGeoTransform[5];
     434         671 :                     padfExtent[2] =
     435         671 :                         psInfo->sSrcParams.adfGeoTransform[0] +
     436         671 :                         nInXSize * psInfo->sSrcParams.adfGeoTransform[1];
     437         671 :                     padfExtent[3] = psInfo->sSrcParams.adfGeoTransform[3];
     438         671 :                     if (!bNorthUp)
     439             :                     {
     440          69 :                         std::swap(padfExtent[1], padfExtent[3]);
     441             :                     }
     442             :                 }
     443         671 :                 return CE_None;
     444             :             }
     445             :         }
     446             :     }
     447             : 
     448         474 :     const int N_PIXELSTEP = 50;
     449             :     int nSteps = static_cast<int>(
     450         474 :         static_cast<double>(std::min(nInYSize, nInXSize)) / N_PIXELSTEP + 0.5);
     451         474 :     if (nSteps < 20)
     452         447 :         nSteps = 20;
     453          27 :     else if (nSteps > 100)
     454          12 :         nSteps = 100;
     455             : 
     456             :     // TODO(rouault): How is this goto retry supposed to work?  Added in r20537.
     457             :     // Does redoing the same malloc multiple times work?  If it is needed, can
     458             :     // it be converted to a tigher while loop around the MALLOC3s and free?  Is
     459             :     // the point to try with the full requested steps.  Then, if there is not
     460             :     // enough memory, back off and try with just 20 steps?
     461         474 : retry:
     462         474 :     int nStepsPlusOne = nSteps + 1;
     463         474 :     int nSampleMax = nStepsPlusOne * nStepsPlusOne;
     464             : 
     465         474 :     double dfStep = 1.0 / nSteps;
     466         474 :     double *padfY = nullptr;
     467         474 :     double *padfZ = nullptr;
     468         474 :     double *padfYRevert = nullptr;
     469         474 :     double *padfZRevert = nullptr;
     470             : 
     471             :     int *pabSuccess = static_cast<int *>(
     472         474 :         VSI_MALLOC3_VERBOSE(sizeof(int), nStepsPlusOne, nStepsPlusOne));
     473             :     double *padfX = static_cast<double *>(
     474         474 :         VSI_MALLOC3_VERBOSE(sizeof(double) * 3, nStepsPlusOne, nStepsPlusOne));
     475             :     double *padfXRevert = static_cast<double *>(
     476         474 :         VSI_MALLOC3_VERBOSE(sizeof(double) * 3, nStepsPlusOne, nStepsPlusOne));
     477         474 :     if (pabSuccess == nullptr || padfX == nullptr || padfXRevert == nullptr)
     478             :     {
     479           0 :         CPLFree(padfX);
     480           0 :         CPLFree(padfXRevert);
     481           0 :         CPLFree(pabSuccess);
     482           0 :         if (nSteps > 20)
     483             :         {
     484           0 :             nSteps = 20;
     485           0 :             goto retry;
     486             :         }
     487           0 :         return CE_Failure;
     488             :     }
     489             : 
     490         474 :     padfY = padfX + nSampleMax;
     491         474 :     padfZ = padfX + nSampleMax * 2;
     492         474 :     padfYRevert = padfXRevert + nSampleMax;
     493         474 :     padfZRevert = padfXRevert + nSampleMax * 2;
     494             : 
     495             :     // Take N_STEPS steps.
     496       11491 :     for (int iStep = 0; iStep <= nSteps; iStep++)
     497             :     {
     498       11017 :         double dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
     499       11017 :         int iStep2 = iStep;
     500             : 
     501             :         // Along top.
     502       11017 :         padfX[iStep2] = dfRatio * nInXSize;
     503       11017 :         padfY[iStep2] = 0.0;
     504       11017 :         padfZ[iStep2] = 0.0;
     505             : 
     506             :         // Along bottom.
     507       11017 :         iStep2 += nStepsPlusOne;
     508       11017 :         padfX[iStep2] = dfRatio * nInXSize;
     509       11017 :         padfY[iStep2] = nInYSize;
     510       11017 :         padfZ[iStep2] = 0.0;
     511             : 
     512             :         // Along left.
     513       11017 :         iStep2 += nStepsPlusOne;
     514       11017 :         padfX[iStep2] = 0.0;
     515       11017 :         padfY[iStep2] = dfRatio * nInYSize;
     516       11017 :         padfZ[iStep2] = 0.0;
     517             : 
     518             :         // Along right.
     519       11017 :         iStep2 += nStepsPlusOne;
     520       11017 :         padfX[iStep2] = nInXSize;
     521       11017 :         padfY[iStep2] = dfRatio * nInYSize;
     522       11017 :         padfZ[iStep2] = 0.0;
     523             :     }
     524             : 
     525         474 :     int nSamplePoints = 4 * nStepsPlusOne;
     526             : 
     527         474 :     memset(pabSuccess, 1, sizeof(int) * nSampleMax);
     528             : 
     529             :     /* -------------------------------------------------------------------- */
     530             :     /*      Transform them to the output coordinate system.                 */
     531             :     /* -------------------------------------------------------------------- */
     532             :     {
     533         948 :         CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
     534         474 :         pfnTransformer(pTransformArg, FALSE, nSamplePoints, padfX, padfY, padfZ,
     535             :                        pabSuccess);
     536             :     }
     537         474 :     constexpr int SIGN_FINAL_UNINIT = -2;
     538         474 :     constexpr int SIGN_FINAL_INVALID = 0;
     539         474 :     int iSignDiscontinuity = SIGN_FINAL_UNINIT;
     540         474 :     int nFailedCount = 0;
     541         474 :     const int iSignArray[2] = {-1, 1};
     542       44542 :     for (int i = 0; i < nSamplePoints; i++)
     543             :     {
     544       44068 :         if (pabSuccess[i])
     545             :         {
     546             :             // Fix for https://trac.osgeo.org/gdal/ticket/7243
     547             :             // where echo "-2050000.000 2050000.000" |
     548             :             //              gdaltransform -s_srs EPSG:3411 -t_srs EPSG:4326
     549             :             // gives "-180 63.691332898492"
     550             :             // but we would rather like 180
     551       38857 :             if (iSignDiscontinuity == 1 || iSignDiscontinuity == -1)
     552             :             {
     553       10797 :                 if (!((iSignDiscontinuity * padfX[i] > 0 &&
     554       10729 :                        iSignDiscontinuity * padfX[i] <= 180.0) ||
     555          69 :                       (fabs(padfX[i] - iSignDiscontinuity * -180.0) < 1e-8)))
     556             :                 {
     557          46 :                     iSignDiscontinuity = SIGN_FINAL_INVALID;
     558             :                 }
     559             :             }
     560       28060 :             else if (iSignDiscontinuity == SIGN_FINAL_UNINIT)
     561             :             {
     562        1140 :                 for (const auto &iSign : iSignArray)
     563             :                 {
     564         830 :                     if ((iSign * padfX[i] > 0 && iSign * padfX[i] <= 180.0) ||
     565         668 :                         (fabs(padfX[i] - iSign * -180.0) < 1e-8))
     566             :                     {
     567         162 :                         iSignDiscontinuity = iSign;
     568         162 :                         break;
     569             :                     }
     570             :                 }
     571         472 :                 if (iSignDiscontinuity == SIGN_FINAL_UNINIT)
     572             :                 {
     573         310 :                     iSignDiscontinuity = SIGN_FINAL_INVALID;
     574             :                 }
     575             :             }
     576             :         }
     577             :         else
     578             :         {
     579        5211 :             nFailedCount++;
     580             :         }
     581             :     }
     582             : 
     583         474 :     if (iSignDiscontinuity == 1 || iSignDiscontinuity == -1)
     584             :     {
     585       11188 :         for (int i = 0; i < nSamplePoints; i++)
     586             :         {
     587       11072 :             if (pabSuccess[i])
     588             :             {
     589       10455 :                 if (fabs(padfX[i] - iSignDiscontinuity * -180.0) < 1e-8)
     590             :                 {
     591           2 :                     double axTemp[2] = {iSignDiscontinuity * -180.0,
     592           2 :                                         iSignDiscontinuity * 180.0};
     593           2 :                     double ayTemp[2] = {padfY[i], padfY[i]};
     594           2 :                     double azTemp[2] = {padfZ[i], padfZ[i]};
     595           2 :                     int abSuccess[2] = {FALSE, FALSE};
     596           4 :                     CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
     597           2 :                     if (pfnTransformer(pTransformArg, TRUE, 2, axTemp, ayTemp,
     598           2 :                                        azTemp, abSuccess) &&
     599           4 :                         fabs(axTemp[0] - axTemp[1]) < 1e-8 &&
     600           2 :                         fabs(ayTemp[0] - ayTemp[1]) < 1e-8)
     601             :                     {
     602           2 :                         padfX[i] = iSignDiscontinuity * 180.0;
     603             :                     }
     604             :                 }
     605             :             }
     606             :         }
     607             :     }
     608             : 
     609             :     /* -------------------------------------------------------------------- */
     610             :     /*      Check if the computed target coordinates are revertable.        */
     611             :     /*      If not, try the detailed grid sampling.                         */
     612             :     /* -------------------------------------------------------------------- */
     613         474 :     if (nFailedCount)
     614             :     {
     615         134 :         CPLDebug("WARP", "At least one point failed after direct transform");
     616             :     }
     617             :     else
     618             :     {
     619         340 :         memcpy(padfXRevert, padfX, nSamplePoints * sizeof(double));
     620         340 :         memcpy(padfYRevert, padfY, nSamplePoints * sizeof(double));
     621         340 :         memcpy(padfZRevert, padfZ, nSamplePoints * sizeof(double));
     622             :         {
     623         680 :             CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
     624         340 :             pfnTransformer(pTransformArg, TRUE, nSamplePoints, padfXRevert,
     625             :                            padfYRevert, padfZRevert, pabSuccess);
     626             :         }
     627             : 
     628       31034 :         for (int i = 0; nFailedCount == 0 && i < nSamplePoints; i++)
     629             :         {
     630       30710 :             if (!pabSuccess[i])
     631             :             {
     632          16 :                 nFailedCount++;
     633          16 :                 break;
     634             :             }
     635             : 
     636       30694 :             double dfRatio = (i % nStepsPlusOne) * dfStep;
     637       30694 :             if (dfRatio > 0.99)
     638        1282 :                 dfRatio = 1.0;
     639             : 
     640       30694 :             double dfExpectedX = 0.0;
     641       30694 :             double dfExpectedY = 0.0;
     642       30694 :             if (i < nStepsPlusOne)
     643             :             {
     644        7807 :                 dfExpectedX = dfRatio * nInXSize;
     645             :             }
     646       22887 :             else if (i < 2 * nStepsPlusOne)
     647             :             {
     648        7757 :                 dfExpectedX = dfRatio * nInXSize;
     649        7757 :                 dfExpectedY = nInYSize;
     650             :             }
     651       15130 :             else if (i < 3 * nStepsPlusOne)
     652             :             {
     653        7627 :                 dfExpectedY = dfRatio * nInYSize;
     654             :             }
     655             :             else
     656             :             {
     657        7503 :                 dfExpectedX = nInXSize;
     658        7503 :                 dfExpectedY = dfRatio * nInYSize;
     659             :             }
     660             : 
     661       30694 :             if (fabs(padfXRevert[i] - dfExpectedX) >
     662       30694 :                     nInXSize / static_cast<double>(nSteps) ||
     663       30682 :                 fabs(padfYRevert[i] - dfExpectedY) >
     664       30682 :                     nInYSize / static_cast<double>(nSteps))
     665          12 :                 nFailedCount++;
     666             :         }
     667         340 :         if (nFailedCount != 0)
     668          28 :             CPLDebug("WARP",
     669             :                      "At least one point failed after revert transform");
     670             :     }
     671             : 
     672             :     /* -------------------------------------------------------------------- */
     673             :     /*      If any of the edge points failed to transform, we need to       */
     674             :     /*      build a fairly detailed internal grid of points instead to      */
     675             :     /*      help identify the area that is transformable.                   */
     676             :     /* -------------------------------------------------------------------- */
     677         474 :     if (nFailedCount)
     678             :     {
     679         162 :         nSamplePoints = 0;
     680             : 
     681             :         // Take N_STEPS steps.
     682        3718 :         for (int iStep = 0; iStep <= nSteps; iStep++)
     683             :         {
     684        3556 :             double dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
     685             : 
     686       91758 :             for (int iStep2 = 0; iStep2 <= nSteps; iStep2++)
     687             :             {
     688       88202 :                 const double dfRatio2 =
     689       88202 :                     iStep2 == nSteps ? 1.0 : iStep2 * dfStep;
     690             : 
     691             :                 // From top to bottom, from left to right.
     692       88202 :                 padfX[nSamplePoints] = dfRatio2 * nInXSize;
     693       88202 :                 padfY[nSamplePoints] = dfRatio * nInYSize;
     694       88202 :                 padfZ[nSamplePoints] = 0.0;
     695       88202 :                 nSamplePoints++;
     696             :             }
     697             :         }
     698             : 
     699         162 :         CPLAssert(nSamplePoints == nSampleMax);
     700             : 
     701             :         {
     702         324 :             CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
     703         162 :             pfnTransformer(pTransformArg, FALSE, nSamplePoints, padfX, padfY,
     704             :                            padfZ, pabSuccess);
     705             :         }
     706             :     }
     707             : 
     708             :     /* -------------------------------------------------------------------- */
     709             :     /*      Collect the bounds, ignoring any failed points.                 */
     710             :     /* -------------------------------------------------------------------- */
     711         474 :     double dfMinXOut = 0.0;
     712         474 :     double dfMinYOut = 0.0;
     713         474 :     double dfMaxXOut = 0.0;
     714         474 :     double dfMaxYOut = 0.0;
     715         474 :     bool bGotInitialPoint = false;
     716             : 
     717         474 :     nFailedCount = 0;
     718      118520 :     for (int i = 0; i < nSamplePoints; i++)
     719             :     {
     720      118046 :         int x_i = 0;
     721      118046 :         int y_i = 0;
     722             : 
     723      118046 :         if (nSamplePoints == nSampleMax)
     724             :         {
     725       88202 :             x_i = i % nStepsPlusOne;
     726       88202 :             y_i = i / nStepsPlusOne;
     727             :         }
     728             :         else
     729             :         {
     730       29844 :             if (i < 2 * nStepsPlusOne)
     731             :             {
     732       14922 :                 x_i = i % nStepsPlusOne;
     733       14922 :                 y_i = (i < nStepsPlusOne) ? 0 : nSteps;
     734             :             }
     735             :         }
     736             : 
     737      118046 :         if (x_i > 0 && (pabSuccess[i - 1] || pabSuccess[i]))
     738             :         {
     739       85265 :             double x_out_before = padfX[i - 1];
     740       85265 :             double x_out_after = padfX[i];
     741       85265 :             int nIter = 0;
     742       85265 :             double x_in_before =
     743       85265 :                 static_cast<double>(x_i - 1) * nInXSize / nSteps;
     744       85265 :             double x_in_after = static_cast<double>(x_i) * nInXSize / nSteps;
     745       85265 :             int invalid_before = !(pabSuccess[i - 1]);
     746       85265 :             int invalid_after = !(pabSuccess[i]);
     747             : 
     748             :             // Detect discontinuity in target coordinates when the target x
     749             :             // coordinates change sign. This may be a false positive when the
     750             :             // target tx is around 0 Dichotomic search to reduce the interval
     751             :             // to near the discontinuity and get a better out extent.
     752      103870 :             while ((invalid_before || invalid_after ||
     753      215407 :                     x_out_before * x_out_after < 0.0) &&
     754             :                    nIter < 16)
     755             :             {
     756       26272 :                 double x = (x_in_before + x_in_after) / 2.0;
     757       26272 :                 double y = static_cast<double>(y_i) * nInYSize / nSteps;
     758       26272 :                 double z = 0.0;
     759       26272 :                 int bSuccess = TRUE;
     760       26272 :                 if (pfnTransformer(pTransformArg, FALSE, 1, &x, &y, &z,
     761       45734 :                                    &bSuccess) &&
     762       19462 :                     bSuccess)
     763             :                 {
     764       19462 :                     if (bGotInitialPoint)
     765             :                     {
     766       19442 :                         dfMinXOut = std::min(dfMinXOut, x);
     767       19442 :                         dfMinYOut = std::min(dfMinYOut, y);
     768       19442 :                         dfMaxXOut = std::max(dfMaxXOut, x);
     769       19442 :                         dfMaxYOut = std::max(dfMaxYOut, y);
     770             :                     }
     771             :                     else
     772             :                     {
     773          20 :                         bGotInitialPoint = true;
     774          20 :                         dfMinXOut = x;
     775          20 :                         dfMaxXOut = x;
     776          20 :                         dfMinYOut = y;
     777          20 :                         dfMaxYOut = y;
     778             :                     }
     779             : 
     780       19462 :                     if (invalid_before || x_out_before * x < 0)
     781             :                     {
     782       10301 :                         invalid_after = FALSE;
     783       10301 :                         x_in_after = (x_in_before + x_in_after) / 2.0;
     784       10301 :                         x_out_after = x;
     785             :                     }
     786             :                     else
     787             :                     {
     788        9161 :                         invalid_before = FALSE;
     789        9161 :                         x_out_before = x;
     790        9161 :                         x_in_before = (x_in_before + x_in_after) / 2.0;
     791             :                     }
     792             :                 }
     793             :                 else
     794             :                 {
     795        6810 :                     if (invalid_before)
     796             :                     {
     797        3402 :                         x_in_before = (x_in_before + x_in_after) / 2.0;
     798             :                     }
     799        3408 :                     else if (invalid_after)
     800             :                     {
     801        3408 :                         x_in_after = (x_in_before + x_in_after) / 2.0;
     802             :                     }
     803             :                     else
     804             :                     {
     805           0 :                         break;
     806             :                     }
     807             :                 }
     808       26272 :                 nIter++;
     809             :             }
     810             :         }
     811             : 
     812      118046 :         if (!pabSuccess[i])
     813             :         {
     814       14768 :             nFailedCount++;
     815       14768 :             continue;
     816             :         }
     817             : 
     818      103278 :         if (bGotInitialPoint)
     819             :         {
     820      102826 :             dfMinXOut = std::min(dfMinXOut, padfX[i]);
     821      102826 :             dfMinYOut = std::min(dfMinYOut, padfY[i]);
     822      102826 :             dfMaxXOut = std::max(dfMaxXOut, padfX[i]);
     823      102826 :             dfMaxYOut = std::max(dfMaxYOut, padfY[i]);
     824             :         }
     825             :         else
     826             :         {
     827         452 :             bGotInitialPoint = true;
     828         452 :             dfMinXOut = padfX[i];
     829         452 :             dfMaxXOut = padfX[i];
     830         452 :             dfMinYOut = padfY[i];
     831         452 :             dfMaxYOut = padfY[i];
     832             :         }
     833             :     }
     834             : 
     835         474 :     if (nFailedCount > nSamplePoints - 10)
     836             :     {
     837           5 :         CPLError(CE_Failure, CPLE_AppDefined,
     838             :                  "Too many points (%d out of %d) failed to transform, "
     839             :                  "unable to compute output bounds.",
     840             :                  nFailedCount, nSamplePoints);
     841             : 
     842           5 :         CPLFree(padfX);
     843           5 :         CPLFree(padfXRevert);
     844           5 :         CPLFree(pabSuccess);
     845             : 
     846           5 :         return CE_Failure;
     847             :     }
     848             : 
     849         469 :     if (nFailedCount)
     850         130 :         CPLDebug("GDAL",
     851             :                  "GDALSuggestedWarpOutput(): %d out of %d points failed to "
     852             :                  "transform.",
     853             :                  nFailedCount, nSamplePoints);
     854             : 
     855         469 :     bool bIsGeographicCoordsDeg = false;
     856         469 :     if (bIsGDALGenImgProjTransform)
     857             :     {
     858         469 :         const GDALGenImgProjTransformInfo *pGIPTI =
     859             :             static_cast<const GDALGenImgProjTransformInfo *>(pTransformArg);
     860         469 :         if (pGIPTI->sSrcParams.pTransformer == GDALGeoLocTransform &&
     861          28 :             pGIPTI->sDstParams.pTransformer == nullptr &&
     862          28 :             pGIPTI->sDstParams.adfGeoTransform[0] == 0 &&
     863          26 :             pGIPTI->sDstParams.adfGeoTransform[1] == 1 &&
     864          26 :             pGIPTI->sDstParams.adfGeoTransform[2] == 0 &&
     865          26 :             pGIPTI->sDstParams.adfGeoTransform[3] == 0 &&
     866          26 :             pGIPTI->sDstParams.adfGeoTransform[4] == 0 &&
     867          26 :             pGIPTI->sDstParams.adfGeoTransform[5] == 1)
     868             :         {
     869             :             /* --------------------------------------------------------------------
     870             :              */
     871             :             /*      Special case for geolocation array, to quickly find the
     872             :              * bounds. */
     873             :             /* --------------------------------------------------------------------
     874             :              */
     875          26 :             const GDALGeoLocTransformInfo *pGLTI =
     876             :                 static_cast<const GDALGeoLocTransformInfo *>(
     877             :                     pGIPTI->sSrcParams.pTransformArg);
     878             : 
     879          26 :             if (pGIPTI->pReproject == nullptr)
     880             :             {
     881             :                 const char *pszGLSRS =
     882          26 :                     CSLFetchNameValue(pGLTI->papszGeolocationInfo, "SRS");
     883          26 :                 if (pszGLSRS == nullptr)
     884             :                 {
     885           4 :                     bIsGeographicCoordsDeg = true;
     886             :                 }
     887             :                 else
     888             :                 {
     889          44 :                     OGRSpatialReference oSRS;
     890          22 :                     if (oSRS.SetFromUserInput(pszGLSRS) == OGRERR_NONE &&
     891          42 :                         oSRS.IsGeographic() &&
     892          20 :                         std::fabs(oSRS.GetAngularUnits() -
     893          20 :                                   CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9)
     894             :                     {
     895          20 :                         bIsGeographicCoordsDeg = true;
     896             :                     }
     897             :                 }
     898             :             }
     899             : 
     900         208 :             for (const auto &xy :
     901          26 :                  {std::pair<double, double>(pGLTI->dfMinX, pGLTI->dfYAtMinX),
     902          26 :                   std::pair<double, double>(pGLTI->dfXAtMinY, pGLTI->dfMinY),
     903          26 :                   std::pair<double, double>(pGLTI->dfMaxX, pGLTI->dfYAtMaxX),
     904         130 :                   std::pair<double, double>(pGLTI->dfXAtMaxY, pGLTI->dfMaxY)})
     905             :             {
     906         104 :                 double x = xy.first;
     907         104 :                 double y = xy.second;
     908         104 :                 if (pGLTI->bSwapXY)
     909             :                 {
     910           4 :                     std::swap(x, y);
     911             :                 }
     912         104 :                 double xOut = std::numeric_limits<double>::quiet_NaN();
     913         104 :                 double yOut = std::numeric_limits<double>::quiet_NaN();
     914         104 :                 if (pGIPTI->pReproject == nullptr ||
     915           0 :                     pGIPTI->pReproject(pGIPTI->pReprojectArg, false, 1, &x, &y,
     916             :                                        nullptr, nullptr))
     917             :                 {
     918         104 :                     xOut = x;
     919         104 :                     yOut = y;
     920             :                 }
     921         104 :                 dfMinXOut = std::min(dfMinXOut, xOut);
     922         104 :                 dfMinYOut = std::min(dfMinYOut, yOut);
     923         104 :                 dfMaxXOut = std::max(dfMaxXOut, xOut);
     924         104 :                 dfMaxYOut = std::max(dfMaxYOut, yOut);
     925          26 :             }
     926             :         }
     927         443 :         else if (pGIPTI->sSrcParams.pTransformer == nullptr &&
     928         400 :                  pGIPTI->sDstParams.pTransformer == nullptr &&
     929         400 :                  pGIPTI->pReproject == GDALReprojectionTransform &&
     930         389 :                  pGIPTI->sDstParams.adfGeoTransform[0] == 0 &&
     931         387 :                  pGIPTI->sDstParams.adfGeoTransform[1] == 1 &&
     932         387 :                  pGIPTI->sDstParams.adfGeoTransform[2] == 0 &&
     933         387 :                  pGIPTI->sDstParams.adfGeoTransform[3] == 0 &&
     934         387 :                  pGIPTI->sDstParams.adfGeoTransform[4] == 0 &&
     935         387 :                  pGIPTI->sDstParams.adfGeoTransform[5] == 1)
     936             :         {
     937             :             /* ------------------------------------------------------------- */
     938             :             /* Special case for warping using source geotransform and        */
     939             :             /* reprojection to deal with the poles.                          */
     940             :             /* ------------------------------------------------------------- */
     941         387 :             const GDALReprojectionTransformInfo *psRTI =
     942             :                 static_cast<const GDALReprojectionTransformInfo *>(
     943             :                     pGIPTI->pReprojectArg);
     944             :             const OGRSpatialReference *poSourceCRS =
     945         387 :                 psRTI->poForwardTransform->GetSourceCS();
     946             :             const OGRSpatialReference *poTargetCRS =
     947         387 :                 psRTI->poForwardTransform->GetTargetCS();
     948         773 :             if (poTargetCRS != nullptr &&
     949         386 :                 psRTI->poReverseTransform != nullptr &&
     950         386 :                 poTargetCRS->IsGeographic() &&
     951         118 :                 fabs(poTargetCRS->GetAngularUnits() -
     952         891 :                      CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9 &&
     953         118 :                 (!poSourceCRS || !poSourceCRS->IsGeographic()))
     954             :             {
     955          91 :                 bIsGeographicCoordsDeg = true;
     956             : 
     957          91 :                 std::unique_ptr<CPLConfigOptionSetter> poSetter;
     958          91 :                 if (pGIPTI->bCheckWithInvertPROJ)
     959             :                 {
     960             :                     // CHECK_WITH_INVERT_PROJ=YES prevent reliable
     961             :                     // transformation of poles.
     962           4 :                     poSetter = std::make_unique<CPLConfigOptionSetter>(
     963           4 :                         "CHECK_WITH_INVERT_PROJ", "NO", false);
     964           4 :                     GDALRefreshGenImgProjTransformer(pTransformArg);
     965             :                     // GDALRefreshGenImgProjTransformer() has invalidated psRTI
     966           4 :                     psRTI = static_cast<const GDALReprojectionTransformInfo *>(
     967             :                         pGIPTI->pReprojectArg);
     968             :                 }
     969             : 
     970         273 :                 for (const auto &sign : iSignArray)
     971             :                 {
     972         182 :                     double X = 0.0;
     973         182 :                     const double Yinit = 90.0 * sign;
     974         182 :                     double Y = Yinit;
     975         182 :                     if (psRTI->poReverseTransform->Transform(1, &X, &Y))
     976             :                     {
     977         124 :                         const auto invGT =
     978             :                             pGIPTI->sSrcParams.adfInvGeoTransform;
     979         124 :                         const double x = invGT[0] + X * invGT[1] + Y * invGT[2];
     980         124 :                         const double y = invGT[3] + X * invGT[4] + Y * invGT[5];
     981         124 :                         constexpr double EPSILON = 1e-5;
     982         124 :                         if (x >= -EPSILON && x <= nInXSize + EPSILON &&
     983          27 :                             y >= -EPSILON && y <= nInYSize + EPSILON)
     984             :                         {
     985           6 :                             if (psRTI->poForwardTransform->Transform(1, &X,
     986          12 :                                                                      &Y) &&
     987           6 :                                 fabs(Y - Yinit) <= 1e-6)
     988             :                             {
     989           6 :                                 bool bMinXMaxXSet = false;
     990           6 :                                 if (poSourceCRS)
     991             :                                 {
     992             :                                     const char *pszProjection =
     993           6 :                                         poSourceCRS->GetAttrValue("PROJECTION");
     994           6 :                                     if (pszProjection &&
     995           6 :                                         EQUAL(pszProjection,
     996             :                                               SRS_PT_ORTHOGRAPHIC))
     997             :                                     {
     998             :                                         const double dfLon0 =
     999           4 :                                             poSourceCRS->GetNormProjParm(
    1000             :                                                 SRS_PP_CENTRAL_MERIDIAN, 0.0);
    1001           4 :                                         dfMinXOut = dfLon0 - 90;
    1002           4 :                                         dfMaxXOut = dfLon0 + 90;
    1003           4 :                                         bMinXMaxXSet = true;
    1004             :                                     }
    1005             :                                 }
    1006           6 :                                 if (!bMinXMaxXSet)
    1007             :                                 {
    1008           2 :                                     dfMinXOut = -180;
    1009           2 :                                     dfMaxXOut = 180;
    1010             :                                 }
    1011           6 :                                 if (sign < 0)
    1012           2 :                                     dfMinYOut = Yinit;
    1013             :                                 else
    1014           4 :                                     dfMaxYOut = Yinit;
    1015             :                             }
    1016             :                         }
    1017             :                     }
    1018             :                 }
    1019             : 
    1020          91 :                 if (poSetter)
    1021             :                 {
    1022           4 :                     poSetter.reset();
    1023           4 :                     GDALRefreshGenImgProjTransformer(pTransformArg);
    1024           4 :                     pGIPTI = static_cast<const GDALGenImgProjTransformInfo *>(
    1025             :                         pTransformArg);
    1026           4 :                     psRTI = static_cast<const GDALReprojectionTransformInfo *>(
    1027             :                         pGIPTI->pReprojectArg);
    1028           4 :                     poSourceCRS = psRTI->poForwardTransform->GetSourceCS();
    1029           4 :                     poTargetCRS = psRTI->poForwardTransform->GetTargetCS();
    1030             :                 }
    1031             :             }
    1032             : 
    1033             :             // Use TransformBounds() to handle more particular cases
    1034         387 :             if (poSourceCRS != nullptr && poTargetCRS != nullptr &&
    1035         386 :                 pGIPTI->sSrcParams.adfGeoTransform[1] != 0 &&
    1036         386 :                 pGIPTI->sSrcParams.adfGeoTransform[2] == 0 &&
    1037         386 :                 pGIPTI->sSrcParams.adfGeoTransform[4] == 0 &&
    1038         386 :                 pGIPTI->sSrcParams.adfGeoTransform[5] != 0)
    1039             :             {
    1040         386 :                 const double dfULX = pGIPTI->sSrcParams.adfGeoTransform[0];
    1041         386 :                 const double dfULY = pGIPTI->sSrcParams.adfGeoTransform[3];
    1042         386 :                 const double dfLRX =
    1043         386 :                     dfULX + pGIPTI->sSrcParams.adfGeoTransform[1] * nInXSize;
    1044         386 :                 const double dfLRY =
    1045         386 :                     dfULY + pGIPTI->sSrcParams.adfGeoTransform[5] * nInYSize;
    1046         386 :                 const double dfMinSrcX = std::min(dfULX, dfLRX);
    1047         386 :                 const double dfMinSrcY = std::min(dfULY, dfLRY);
    1048         386 :                 const double dfMaxSrcX = std::max(dfULX, dfLRX);
    1049         386 :                 const double dfMaxSrcY = std::max(dfULY, dfLRY);
    1050         386 :                 double dfTmpMinXOut = std::numeric_limits<double>::max();
    1051         386 :                 double dfTmpMinYOut = std::numeric_limits<double>::max();
    1052         386 :                 double dfTmpMaxXOut = std::numeric_limits<double>::min();
    1053         386 :                 double dfTmpMaxYOut = std::numeric_limits<double>::min();
    1054         772 :                 if (psRTI->poForwardTransform->TransformBounds(
    1055             :                         dfMinSrcX, dfMinSrcY, dfMaxSrcX, dfMaxSrcY,
    1056             :                         &dfTmpMinXOut, &dfTmpMinYOut, &dfTmpMaxXOut,
    1057             :                         &dfTmpMaxYOut,
    1058         386 :                         2))  // minimum number of points as we already have a
    1059             :                              // logic above to sample
    1060             :                 {
    1061         379 :                     dfMinXOut = std::min(dfMinXOut, dfTmpMinXOut);
    1062         379 :                     dfMinYOut = std::min(dfMinYOut, dfTmpMinYOut);
    1063         379 :                     dfMaxXOut = std::max(dfMaxXOut, dfTmpMaxXOut);
    1064         379 :                     dfMaxYOut = std::max(dfMaxYOut, dfTmpMaxYOut);
    1065             :                 }
    1066             :             }
    1067             :         }
    1068             :     }
    1069             : 
    1070             :     /* -------------------------------------------------------------------- */
    1071             :     /*      Compute the distance in "georeferenced" units from the top      */
    1072             :     /*      corner of the transformed input image to the bottom left        */
    1073             :     /*      corner of the transformed input.  Use this distance to          */
    1074             :     /*      compute an approximate pixel size in the output                 */
    1075             :     /*      georeferenced coordinates.                                      */
    1076             :     /* -------------------------------------------------------------------- */
    1077         469 :     double dfDiagonalDist = 0.0;
    1078         469 :     double dfDeltaX = 0.0;
    1079         469 :     double dfDeltaY = 0.0;
    1080             : 
    1081         469 :     if (pabSuccess[0] && pabSuccess[nSamplePoints - 1])
    1082             :     {
    1083         344 :         dfDeltaX = padfX[nSamplePoints - 1] - padfX[0];
    1084         344 :         dfDeltaY = padfY[nSamplePoints - 1] - padfY[0];
    1085             :         // In some cases this can result in 0 values. See #5980
    1086             :         // Fallback to safer method in that case.
    1087             :     }
    1088         469 :     if (dfDeltaX == 0.0 || dfDeltaY == 0.0)
    1089             :     {
    1090         131 :         dfDeltaX = dfMaxXOut - dfMinXOut;
    1091         131 :         dfDeltaY = dfMaxYOut - dfMinYOut;
    1092             :     }
    1093             : 
    1094         469 :     dfDiagonalDist = sqrt(dfDeltaX * dfDeltaX + dfDeltaY * dfDeltaY);
    1095             : 
    1096             :     /* -------------------------------------------------------------------- */
    1097             :     /*      Compute a pixel size from this.                                 */
    1098             :     /* -------------------------------------------------------------------- */
    1099             :     double dfPixelSize =
    1100         469 :         dfDiagonalDist / sqrt(static_cast<double>(nInXSize) * nInXSize +
    1101         469 :                               static_cast<double>(nInYSize) * nInYSize);
    1102             : 
    1103         469 :     double dfPixels = (dfMaxXOut - dfMinXOut) / dfPixelSize;
    1104         469 :     double dfLines = (dfMaxYOut - dfMinYOut) / dfPixelSize;
    1105             : 
    1106         469 :     const int knIntMaxMinusOne = std::numeric_limits<int>::max() - 1;
    1107         469 :     if (dfPixels > knIntMaxMinusOne && dfLines <= dfPixels)
    1108             :     {
    1109           0 :         dfPixels = knIntMaxMinusOne;
    1110           0 :         dfPixelSize = (dfMaxXOut - dfMinXOut) / dfPixels;
    1111           0 :         dfLines = (dfMaxYOut - dfMinYOut) / dfPixelSize;
    1112             :     }
    1113         469 :     else if (dfLines > knIntMaxMinusOne)
    1114             :     {
    1115           2 :         dfLines = knIntMaxMinusOne;
    1116           2 :         dfPixelSize = (dfMaxYOut - dfMinYOut) / dfLines;
    1117           2 :         dfPixels = (dfMaxXOut - dfMinXOut) / dfPixelSize;
    1118             :     }
    1119             : 
    1120         469 :     if (dfPixels > knIntMaxMinusOne || dfLines > knIntMaxMinusOne)
    1121             :     {
    1122           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    1123             :                  "Computed dimensions are too big : %.0f x %.0f",
    1124             :                  dfPixels + 0.5, dfLines + 0.5);
    1125             : 
    1126           0 :         CPLFree(padfX);
    1127           0 :         CPLFree(padfXRevert);
    1128           0 :         CPLFree(pabSuccess);
    1129             : 
    1130           0 :         return CE_Failure;
    1131             :     }
    1132             : 
    1133         469 :     if ((nOptions & GDAL_SWO_ROUND_UP_SIZE) != 0)
    1134             :     {
    1135           8 :         constexpr double EPS = 1e-5;
    1136           8 :         *pnPixels = static_cast<int>(std::ceil(dfPixels - EPS));
    1137           8 :         *pnLines = static_cast<int>(std::ceil(dfLines - EPS));
    1138             :     }
    1139             :     else
    1140             :     {
    1141         461 :         *pnPixels = static_cast<int>(dfPixels + 0.5);
    1142         461 :         *pnLines = static_cast<int>(dfLines + 0.5);
    1143             :     }
    1144             : 
    1145         469 :     double dfPixelSizeX = dfPixelSize;
    1146         469 :     double dfPixelSizeY = dfPixelSize;
    1147             : 
    1148         469 :     const double adfRatioArray[] = {0.000, 0.001, 0.010, 0.100, 1.000};
    1149             : 
    1150             :     /* -------------------------------------------------------------------- */
    1151             :     /*      Check that the right border is not completely out of source     */
    1152             :     /*      image. If so, adjust the x pixel size a bit in the hope it will */
    1153             :     /*      fit.                                                            */
    1154             :     /* -------------------------------------------------------------------- */
    1155         644 :     for (const auto &dfRatio : adfRatioArray)
    1156             :     {
    1157         636 :         const double dfTryPixelSizeX =
    1158         636 :             dfPixelSizeX - dfPixelSizeX * dfRatio / *pnPixels;
    1159         636 :         double adfExtent[4] = {dfMinXOut, dfMaxYOut - (*pnLines) * dfPixelSizeY,
    1160         636 :                                dfMinXOut + (*pnPixels) * dfTryPixelSizeX,
    1161         636 :                                dfMaxYOut};
    1162         636 :         if (!GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
    1163             :                 pfnTransformer, pTransformArg, adfExtent, *pnPixels, *pnLines,
    1164             :                 dfTryPixelSizeX, dfPixelSizeY))
    1165             :         {
    1166         461 :             dfPixelSizeX = dfTryPixelSizeX;
    1167         461 :             break;
    1168             :         }
    1169             :     }
    1170             : 
    1171             :     /* -------------------------------------------------------------------- */
    1172             :     /*      Check that the bottom border is not completely out of source    */
    1173             :     /*      image. If so, adjust the y pixel size a bit in the hope it will */
    1174             :     /*      fit.                                                            */
    1175             :     /* -------------------------------------------------------------------- */
    1176         549 :     for (const auto &dfRatio : adfRatioArray)
    1177             :     {
    1178         539 :         const double dfTryPixelSizeY =
    1179         539 :             dfPixelSizeY - dfPixelSizeY * dfRatio / *pnLines;
    1180             :         double adfExtent[4] = {
    1181         539 :             dfMinXOut, dfMaxYOut - (*pnLines) * dfTryPixelSizeY,
    1182         539 :             dfMinXOut + (*pnPixels) * dfPixelSizeX, dfMaxYOut};
    1183         539 :         if (!GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
    1184             :                 pfnTransformer, pTransformArg, adfExtent, *pnPixels, *pnLines,
    1185             :                 dfPixelSizeX, dfTryPixelSizeY))
    1186             :         {
    1187         459 :             dfPixelSizeY = dfTryPixelSizeY;
    1188         459 :             break;
    1189             :         }
    1190             :     }
    1191             : 
    1192             :     /* -------------------------------------------------------------------- */
    1193             :     /*      Recompute some bounds so that all return values are consistent  */
    1194             :     /* -------------------------------------------------------------------- */
    1195         469 :     double dfMaxXOutNew = dfMinXOut + (*pnPixels) * dfPixelSizeX;
    1196         469 :     if (bIsGeographicCoordsDeg &&
    1197         115 :         ((dfMaxXOut <= 180 && dfMaxXOutNew > 180) || dfMaxXOut == 180))
    1198             :     {
    1199           3 :         dfMaxXOut = 180;
    1200           3 :         dfPixelSizeX = (dfMaxXOut - dfMinXOut) / *pnPixels;
    1201             :     }
    1202             :     else
    1203             :     {
    1204         466 :         dfMaxXOut = dfMaxXOutNew;
    1205             :     }
    1206             : 
    1207         469 :     double dfMinYOutNew = dfMaxYOut - (*pnLines) * dfPixelSizeY;
    1208         469 :     if (bIsGeographicCoordsDeg && dfMinYOut >= -90 && dfMinYOutNew < -90)
    1209             :     {
    1210           0 :         dfMinYOut = -90;
    1211           0 :         dfPixelSizeY = (dfMaxYOut - dfMinYOut) / *pnLines;
    1212             :     }
    1213             :     else
    1214             :     {
    1215         469 :         dfMinYOut = dfMinYOutNew;
    1216             :     }
    1217             : 
    1218             :     /* -------------------------------------------------------------------- */
    1219             :     /*      Return raw extents.                                             */
    1220             :     /* -------------------------------------------------------------------- */
    1221         469 :     padfExtent[0] = dfMinXOut;
    1222         469 :     padfExtent[1] = dfMinYOut;
    1223         469 :     padfExtent[2] = dfMaxXOut;
    1224         469 :     padfExtent[3] = dfMaxYOut;
    1225             : 
    1226             :     /* -------------------------------------------------------------------- */
    1227             :     /*      Set the output geotransform.                                    */
    1228             :     /* -------------------------------------------------------------------- */
    1229         469 :     padfGeoTransformOut[0] = dfMinXOut;
    1230         469 :     padfGeoTransformOut[1] = dfPixelSizeX;
    1231         469 :     padfGeoTransformOut[2] = 0.0;
    1232         469 :     padfGeoTransformOut[3] = dfMaxYOut;
    1233         469 :     padfGeoTransformOut[4] = 0.0;
    1234         469 :     padfGeoTransformOut[5] = -dfPixelSizeY;
    1235             : 
    1236         469 :     CPLFree(padfX);
    1237         469 :     CPLFree(padfXRevert);
    1238         469 :     CPLFree(pabSuccess);
    1239             : 
    1240         469 :     return CE_None;
    1241             : }
    1242             : 
    1243             : /************************************************************************/
    1244             : /*                    GetCurrentCheckWithInvertPROJ()                   */
    1245             : /************************************************************************/
    1246             : 
    1247        3483 : static bool GetCurrentCheckWithInvertPROJ()
    1248             : {
    1249        3483 :     return CPLTestBool(CPLGetConfigOption("CHECK_WITH_INVERT_PROJ", "NO"));
    1250             : }
    1251             : 
    1252             : /************************************************************************/
    1253             : /*               GDALCreateGenImgProjTransformerInternal()              */
    1254             : /************************************************************************/
    1255             : 
    1256             : static void *GDALCreateSimilarGenImgProjTransformer(void *hTransformArg,
    1257             :                                                     double dfRatioX,
    1258             :                                                     double dfRatioY);
    1259             : 
    1260        2224 : static GDALGenImgProjTransformInfo *GDALCreateGenImgProjTransformerInternal()
    1261             : {
    1262             :     /* -------------------------------------------------------------------- */
    1263             :     /*      Initialize the transform info.                                  */
    1264             :     /* -------------------------------------------------------------------- */
    1265             :     GDALGenImgProjTransformInfo *psInfo =
    1266             :         static_cast<GDALGenImgProjTransformInfo *>(
    1267        2224 :             CPLCalloc(sizeof(GDALGenImgProjTransformInfo), 1));
    1268             : 
    1269        2224 :     memcpy(psInfo->sTI.abySignature, GDAL_GTI2_SIGNATURE,
    1270             :            strlen(GDAL_GTI2_SIGNATURE));
    1271        2224 :     psInfo->sTI.pszClassName = GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME;
    1272        2224 :     psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
    1273        2224 :     psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
    1274        2224 :     psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
    1275        2224 :     psInfo->sTI.pfnCreateSimilar = GDALCreateSimilarGenImgProjTransformer;
    1276             : 
    1277        2224 :     psInfo->bCheckWithInvertPROJ = GetCurrentCheckWithInvertPROJ();
    1278        2224 :     psInfo->bHasCustomTransformationPipeline = false;
    1279             : 
    1280        2224 :     return psInfo;
    1281             : }
    1282             : 
    1283             : /************************************************************************/
    1284             : /*                GDALCreateSimilarGenImgProjTransformer()              */
    1285             : /************************************************************************/
    1286             : 
    1287          56 : static void *GDALCreateSimilarGenImgProjTransformer(void *hTransformArg,
    1288             :                                                     double dfRatioX,
    1289             :                                                     double dfRatioY)
    1290             : {
    1291          56 :     VALIDATE_POINTER1(hTransformArg, "GDALCreateSimilarGenImgProjTransformer",
    1292             :                       nullptr);
    1293             : 
    1294          56 :     GDALGenImgProjTransformInfo *psInfo =
    1295             :         static_cast<GDALGenImgProjTransformInfo *>(hTransformArg);
    1296             : 
    1297             :     GDALGenImgProjTransformInfo *psClonedInfo =
    1298          56 :         GDALCreateGenImgProjTransformerInternal();
    1299             : 
    1300          56 :     memcpy(psClonedInfo, psInfo, sizeof(GDALGenImgProjTransformInfo));
    1301             : 
    1302          56 :     psClonedInfo->bCheckWithInvertPROJ = GetCurrentCheckWithInvertPROJ();
    1303             : 
    1304          56 :     if (psClonedInfo->sSrcParams.pTransformArg)
    1305           7 :         psClonedInfo->sSrcParams.pTransformArg = GDALCreateSimilarTransformer(
    1306             :             psInfo->sSrcParams.pTransformArg, dfRatioX, dfRatioY);
    1307          49 :     else if (dfRatioX != 1.0 || dfRatioY != 1.0)
    1308             :     {
    1309          10 :         if (psClonedInfo->sSrcParams.adfGeoTransform[2] == 0.0 &&
    1310          10 :             psClonedInfo->sSrcParams.adfGeoTransform[4] == 0.0)
    1311             :         {
    1312          10 :             psClonedInfo->sSrcParams.adfGeoTransform[1] *= dfRatioX;
    1313          10 :             psClonedInfo->sSrcParams.adfGeoTransform[5] *= dfRatioY;
    1314             :         }
    1315             :         else
    1316             :         {
    1317             :             // If the x and y ratios are not equal, then we cannot really
    1318             :             // compute a geotransform.
    1319           0 :             psClonedInfo->sSrcParams.adfGeoTransform[1] *= dfRatioX;
    1320           0 :             psClonedInfo->sSrcParams.adfGeoTransform[2] *= dfRatioX;
    1321           0 :             psClonedInfo->sSrcParams.adfGeoTransform[4] *= dfRatioX;
    1322           0 :             psClonedInfo->sSrcParams.adfGeoTransform[5] *= dfRatioX;
    1323             :         }
    1324          10 :         if (!GDALInvGeoTransform(psClonedInfo->sSrcParams.adfGeoTransform,
    1325          10 :                                  psClonedInfo->sSrcParams.adfInvGeoTransform))
    1326             :         {
    1327           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
    1328           0 :             GDALDestroyGenImgProjTransformer(psClonedInfo);
    1329           0 :             return nullptr;
    1330             :         }
    1331             :     }
    1332             : 
    1333          56 :     if (psClonedInfo->pReprojectArg)
    1334          35 :         psClonedInfo->pReprojectArg =
    1335          35 :             GDALCloneTransformer(psInfo->pReprojectArg);
    1336             : 
    1337          56 :     if (psClonedInfo->sDstParams.pTransformArg)
    1338           0 :         psClonedInfo->sDstParams.pTransformArg =
    1339           0 :             GDALCloneTransformer(psInfo->sDstParams.pTransformArg);
    1340             : 
    1341          56 :     return psClonedInfo;
    1342             : }
    1343             : 
    1344             : /************************************************************************/
    1345             : /*                  GDALCreateGenImgProjTransformer()                   */
    1346             : /************************************************************************/
    1347             : 
    1348             : /**
    1349             :  * Create image to image transformer.
    1350             :  *
    1351             :  * This function creates a transformation object that maps from pixel/line
    1352             :  * coordinates on one image to pixel/line coordinates on another image.  The
    1353             :  * images may potentially be georeferenced in different coordinate systems,
    1354             :  * and may used GCPs to map between their pixel/line coordinates and
    1355             :  * georeferenced coordinates (as opposed to the default assumption that their
    1356             :  * geotransform should be used).
    1357             :  *
    1358             :  * This transformer potentially performs three concatenated transformations.
    1359             :  *
    1360             :  * The first stage is from source image pixel/line coordinates to source
    1361             :  * image georeferenced coordinates, and may be done using the geotransform,
    1362             :  * or if not defined using a polynomial model derived from GCPs.  If GCPs
    1363             :  * are used this stage is accomplished using GDALGCPTransform().
    1364             :  *
    1365             :  * The second stage is to change projections from the source coordinate system
    1366             :  * to the destination coordinate system, assuming they differ.  This is
    1367             :  * accomplished internally using GDALReprojectionTransform().
    1368             :  *
    1369             :  * The third stage is converting from destination image georeferenced
    1370             :  * coordinates to destination image coordinates.  This is done using the
    1371             :  * destination image geotransform, or if not available, using a polynomial
    1372             :  * model derived from GCPs. If GCPs are used this stage is accomplished using
    1373             :  * GDALGCPTransform().  This stage is skipped if hDstDS is NULL when the
    1374             :  * transformation is created.
    1375             :  *
    1376             :  * @param hSrcDS source dataset, or NULL.
    1377             :  * @param pszSrcWKT the coordinate system for the source dataset.  If NULL,
    1378             :  * it will be read from the dataset itself.
    1379             :  * @param hDstDS destination dataset (or NULL).
    1380             :  * @param pszDstWKT the coordinate system for the destination dataset.  If
    1381             :  * NULL, and hDstDS not NULL, it will be read from the destination dataset.
    1382             :  * @param bGCPUseOK TRUE if GCPs should be used if the geotransform is not
    1383             :  * available on the source dataset (not destination).
    1384             :  * @param dfGCPErrorThreshold ignored/deprecated.
    1385             :  * @param nOrder the maximum order to use for GCP derived polynomials if
    1386             :  * possible.  Use 0 to autoselect, or -1 for thin plate splines.
    1387             :  *
    1388             :  * @return handle suitable for use GDALGenImgProjTransform(), and to be
    1389             :  * deallocated with GDALDestroyGenImgProjTransformer().
    1390             :  */
    1391             : 
    1392          62 : void *GDALCreateGenImgProjTransformer(GDALDatasetH hSrcDS,
    1393             :                                       const char *pszSrcWKT,
    1394             :                                       GDALDatasetH hDstDS,
    1395             :                                       const char *pszDstWKT, int bGCPUseOK,
    1396             :                                       CPL_UNUSED double dfGCPErrorThreshold,
    1397             :                                       int nOrder)
    1398             : {
    1399          62 :     char **papszOptions = nullptr;
    1400             : 
    1401          62 :     if (pszSrcWKT != nullptr)
    1402           3 :         papszOptions = CSLSetNameValue(papszOptions, "SRC_SRS", pszSrcWKT);
    1403          62 :     if (pszDstWKT != nullptr)
    1404           3 :         papszOptions = CSLSetNameValue(papszOptions, "DST_SRS", pszDstWKT);
    1405          62 :     if (!bGCPUseOK)
    1406           0 :         papszOptions = CSLSetNameValue(papszOptions, "GCPS_OK", "FALSE");
    1407          62 :     if (nOrder != 0)
    1408           0 :         papszOptions = CSLSetNameValue(papszOptions, "MAX_GCP_ORDER",
    1409           0 :                                        CPLString().Printf("%d", nOrder));
    1410             : 
    1411          62 :     void *pRet = GDALCreateGenImgProjTransformer2(hSrcDS, hDstDS, papszOptions);
    1412          62 :     CSLDestroy(papszOptions);
    1413             : 
    1414          62 :     return pRet;
    1415             : }
    1416             : 
    1417             : /************************************************************************/
    1418             : /*                          InsertCenterLong()                          */
    1419             : /*                                                                      */
    1420             : /*      Insert a CENTER_LONG Extension entry on a GEOGCS to indicate    */
    1421             : /*      the center longitude of the dataset for wrapping purposes.      */
    1422             : /************************************************************************/
    1423             : 
    1424         974 : static void InsertCenterLong(GDALDatasetH hDS, const OGRSpatialReference *poSRS,
    1425             :                              const OGRSpatialReference *poDstSRS,
    1426             :                              const char *pszTargetExtent,
    1427             :                              CPLStringList &aosOptions)
    1428             : 
    1429             : {
    1430        1706 :     if (!poSRS->IsGeographic() || std::fabs(poSRS->GetAngularUnits() -
    1431         732 :                                             CPLAtof(SRS_UA_DEGREE_CONV)) > 1e-9)
    1432             :     {
    1433         245 :         return;
    1434             :     }
    1435             : 
    1436         731 :     if (poSRS->GetExtension(nullptr, "CENTER_LONG"))
    1437           0 :         return;
    1438             : 
    1439             :     /* -------------------------------------------------------------------- */
    1440             :     /*      For now we only do this if we have a geotransform since         */
    1441             :     /*      other forms require a bunch of extra work.                      */
    1442             :     /* -------------------------------------------------------------------- */
    1443         731 :     double adfGeoTransform[6] = {};
    1444             : 
    1445         731 :     if (GDALGetGeoTransform(hDS, adfGeoTransform) != CE_None)
    1446           0 :         return;
    1447             : 
    1448             :     /* -------------------------------------------------------------------- */
    1449             :     /*      Compute min/max longitude based on testing the four corners.    */
    1450             :     /* -------------------------------------------------------------------- */
    1451         731 :     const int nXSize = GDALGetRasterXSize(hDS);
    1452         731 :     const int nYSize = GDALGetRasterYSize(hDS);
    1453             : 
    1454             :     const double dfMinLong =
    1455        1462 :         std::min(std::min(adfGeoTransform[0] + 0 * adfGeoTransform[1] +
    1456         731 :                               0 * adfGeoTransform[2],
    1457        1462 :                           adfGeoTransform[0] + nXSize * adfGeoTransform[1] +
    1458         731 :                               0 * adfGeoTransform[2]),
    1459        1462 :                  std::min(adfGeoTransform[0] + 0 * adfGeoTransform[1] +
    1460         731 :                               nYSize * adfGeoTransform[2],
    1461        1462 :                           adfGeoTransform[0] + nXSize * adfGeoTransform[1] +
    1462         731 :                               nYSize * adfGeoTransform[2]));
    1463             :     const double dfMaxLong =
    1464        1462 :         std::max(std::max(adfGeoTransform[0] + 0 * adfGeoTransform[1] +
    1465         731 :                               0 * adfGeoTransform[2],
    1466        1462 :                           adfGeoTransform[0] + nXSize * adfGeoTransform[1] +
    1467         731 :                               0 * adfGeoTransform[2]),
    1468        1462 :                  std::max(adfGeoTransform[0] + 0 * adfGeoTransform[1] +
    1469         731 :                               nYSize * adfGeoTransform[2],
    1470        1462 :                           adfGeoTransform[0] + nXSize * adfGeoTransform[1] +
    1471         731 :                               nYSize * adfGeoTransform[2]));
    1472             : 
    1473             :     // If the raster covers more than 360 degree, give up,
    1474             :     // except is the target SRS is geographic and crossing the antimeridian
    1475         731 :     if (dfMaxLong - dfMinLong > 360.0)
    1476             :     {
    1477           3 :         const CPLStringList aosTE(CSLTokenizeString2(pszTargetExtent, ",", 0));
    1478           6 :         if (aosTE.size() == 4 && poDstSRS->IsGeographic() &&
    1479           1 :             std::fabs(poDstSRS->GetAngularUnits() -
    1480           7 :                       CPLAtof(SRS_UA_DEGREE_CONV)) <= 1e-9 &&
    1481           1 :             ((CPLAtof(aosTE[0]) >= -179 && CPLAtof(aosTE[0]) < 180 &&
    1482           1 :               CPLAtof(aosTE[2]) > 180) ||
    1483           0 :              (CPLAtof(aosTE[0]) < -180 && CPLAtof(aosTE[2]) > -180 &&
    1484           0 :               CPLAtof(aosTE[2]) <= 179)))
    1485             :         {
    1486             :             // insert CENTER_LONG
    1487             :         }
    1488             :         else
    1489             :         {
    1490           2 :             return;
    1491             :         }
    1492             :     }
    1493             : 
    1494             :     /* -------------------------------------------------------------------- */
    1495             :     /*      Insert center long.                                             */
    1496             :     /* -------------------------------------------------------------------- */
    1497         729 :     const double dfCenterLong = (dfMaxLong + dfMinLong) / 2.0;
    1498         729 :     aosOptions.SetNameValue("CENTER_LONG", CPLSPrintf("%g", dfCenterLong));
    1499             : }
    1500             : 
    1501             : /************************************************************************/
    1502             : /*                      GDALComputeAreaOfInterest()                     */
    1503             : /************************************************************************/
    1504             : 
    1505        1337 : bool GDALComputeAreaOfInterest(const OGRSpatialReference *poSRS,
    1506             :                                double adfGT[6], int nXSize, int nYSize,
    1507             :                                double &dfWestLongitudeDeg,
    1508             :                                double &dfSouthLatitudeDeg,
    1509             :                                double &dfEastLongitudeDeg,
    1510             :                                double &dfNorthLatitudeDeg)
    1511             : {
    1512        1337 :     bool ret = false;
    1513             : 
    1514        1337 :     if (!poSRS)
    1515           0 :         return false;
    1516             : 
    1517        1337 :     OGRSpatialReference oSrcSRSHoriz(*poSRS);
    1518        1337 :     if (oSrcSRSHoriz.IsCompound())
    1519             :     {
    1520          18 :         oSrcSRSHoriz.StripVertical();
    1521             :     }
    1522             : 
    1523        1337 :     OGRSpatialReference *poGeog = oSrcSRSHoriz.CloneGeogCS();
    1524        1337 :     if (poGeog)
    1525             :     {
    1526        1337 :         poGeog->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    1527        1337 :         poGeog->SetAngularUnits(SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV));
    1528             : 
    1529        1337 :         auto poCT = OGRCreateCoordinateTransformation(&oSrcSRSHoriz, poGeog);
    1530        1337 :         if (poCT)
    1531             :         {
    1532        1337 :             poCT->SetEmitErrors(false);
    1533             : 
    1534             :             double x[4], y[4];
    1535        1337 :             x[0] = adfGT[0];
    1536        1337 :             y[0] = adfGT[3];
    1537        1337 :             x[1] = adfGT[0] + nXSize * adfGT[1];
    1538        1337 :             y[1] = adfGT[3];
    1539        1337 :             x[2] = adfGT[0];
    1540        1337 :             y[2] = adfGT[3] + nYSize * adfGT[5];
    1541        1337 :             x[3] = x[1];
    1542        1337 :             y[3] = y[2];
    1543        1337 :             int validity[4] = {false, false, false, false};
    1544        1337 :             poCT->Transform(4, x, y, nullptr, validity);
    1545        1337 :             dfWestLongitudeDeg = std::numeric_limits<double>::max();
    1546        1337 :             dfSouthLatitudeDeg = std::numeric_limits<double>::max();
    1547        1337 :             dfEastLongitudeDeg = -std::numeric_limits<double>::max();
    1548        1337 :             dfNorthLatitudeDeg = -std::numeric_limits<double>::max();
    1549        6685 :             for (int i = 0; i < 4; i++)
    1550             :             {
    1551        5348 :                 if (validity[i])
    1552             :                 {
    1553        5332 :                     ret = true;
    1554        5332 :                     dfWestLongitudeDeg = std::min(dfWestLongitudeDeg, x[i]);
    1555        5332 :                     dfSouthLatitudeDeg = std::min(dfSouthLatitudeDeg, y[i]);
    1556        5332 :                     dfEastLongitudeDeg = std::max(dfEastLongitudeDeg, x[i]);
    1557        5332 :                     dfNorthLatitudeDeg = std::max(dfNorthLatitudeDeg, y[i]);
    1558             :                 }
    1559             :             }
    1560        1337 :             if (validity[0] && validity[1] && x[0] > x[1])
    1561             :             {
    1562           3 :                 dfWestLongitudeDeg = x[0];
    1563           3 :                 dfEastLongitudeDeg = x[1];
    1564             :             }
    1565        1337 :             if (ret && std::fabs(dfWestLongitudeDeg) <= 180 &&
    1566        1330 :                 std::fabs(dfEastLongitudeDeg) <= 180 &&
    1567        1326 :                 std::fabs(dfSouthLatitudeDeg) <= 90 &&
    1568        1322 :                 std::fabs(dfNorthLatitudeDeg) <= 90)
    1569             :             {
    1570        1322 :                 CPLDebug("GDAL", "Computing area of interest: %g, %g, %g, %g",
    1571             :                          dfWestLongitudeDeg, dfSouthLatitudeDeg,
    1572             :                          dfEastLongitudeDeg, dfNorthLatitudeDeg);
    1573             :             }
    1574             :             else
    1575             :             {
    1576          15 :                 CPLDebug("GDAL", "Could not compute area of interest");
    1577          15 :                 dfWestLongitudeDeg = 0;
    1578          15 :                 dfSouthLatitudeDeg = 0;
    1579          15 :                 dfEastLongitudeDeg = 0;
    1580          15 :                 dfNorthLatitudeDeg = 0;
    1581             :             }
    1582        1337 :             OGRCoordinateTransformation::DestroyCT(poCT);
    1583             :         }
    1584             : 
    1585        1337 :         delete poGeog;
    1586             :     }
    1587             : 
    1588        1337 :     return ret;
    1589             : }
    1590             : 
    1591           5 : bool GDALComputeAreaOfInterest(const OGRSpatialReference *poSRS, double dfX1,
    1592             :                                double dfY1, double dfX2, double dfY2,
    1593             :                                double &dfWestLongitudeDeg,
    1594             :                                double &dfSouthLatitudeDeg,
    1595             :                                double &dfEastLongitudeDeg,
    1596             :                                double &dfNorthLatitudeDeg)
    1597             : {
    1598           5 :     bool ret = false;
    1599             : 
    1600           5 :     if (!poSRS)
    1601           0 :         return false;
    1602             : 
    1603           5 :     OGRSpatialReference oSrcSRSHoriz(*poSRS);
    1604           5 :     if (oSrcSRSHoriz.IsCompound())
    1605             :     {
    1606           0 :         oSrcSRSHoriz.StripVertical();
    1607             :     }
    1608             : 
    1609           5 :     OGRSpatialReference *poGeog = oSrcSRSHoriz.CloneGeogCS();
    1610           5 :     if (poGeog)
    1611             :     {
    1612           5 :         poGeog->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    1613             : 
    1614           5 :         auto poCT = OGRCreateCoordinateTransformation(&oSrcSRSHoriz, poGeog);
    1615           5 :         if (poCT)
    1616             :         {
    1617             :             double x[4], y[4];
    1618           5 :             x[0] = dfX1;
    1619           5 :             y[0] = dfY1;
    1620           5 :             x[1] = dfX2;
    1621           5 :             y[1] = dfY1;
    1622           5 :             x[2] = dfX1;
    1623           5 :             y[2] = dfY2;
    1624           5 :             x[3] = dfX2;
    1625           5 :             y[3] = dfY2;
    1626           5 :             int validity[4] = {false, false, false, false};
    1627           5 :             poCT->Transform(4, x, y, nullptr, validity);
    1628           5 :             dfWestLongitudeDeg = std::numeric_limits<double>::max();
    1629           5 :             dfSouthLatitudeDeg = std::numeric_limits<double>::max();
    1630           5 :             dfEastLongitudeDeg = -std::numeric_limits<double>::max();
    1631           5 :             dfNorthLatitudeDeg = -std::numeric_limits<double>::max();
    1632          25 :             for (int i = 0; i < 4; i++)
    1633             :             {
    1634          20 :                 if (validity[i])
    1635             :                 {
    1636          20 :                     ret = true;
    1637          20 :                     dfWestLongitudeDeg = std::min(dfWestLongitudeDeg, x[i]);
    1638          20 :                     dfSouthLatitudeDeg = std::min(dfSouthLatitudeDeg, y[i]);
    1639          20 :                     dfEastLongitudeDeg = std::max(dfEastLongitudeDeg, x[i]);
    1640          20 :                     dfNorthLatitudeDeg = std::max(dfNorthLatitudeDeg, y[i]);
    1641             :                 }
    1642             :             }
    1643           5 :             if (validity[0] && validity[1] && (dfX1 - dfX2) * (x[0] - x[1]) < 0)
    1644             :             {
    1645           0 :                 dfWestLongitudeDeg = x[0];
    1646           0 :                 dfEastLongitudeDeg = x[1];
    1647             :             }
    1648           5 :             if (ret)
    1649             :             {
    1650           5 :                 CPLDebug("GDAL", "Computing area of interest: %g, %g, %g, %g",
    1651             :                          dfWestLongitudeDeg, dfSouthLatitudeDeg,
    1652             :                          dfEastLongitudeDeg, dfNorthLatitudeDeg);
    1653             :             }
    1654             :             else
    1655             :             {
    1656           0 :                 CPLDebug("GDAL", "Could not compute area of interest");
    1657           0 :                 dfWestLongitudeDeg = 0;
    1658           0 :                 dfSouthLatitudeDeg = 0;
    1659           0 :                 dfEastLongitudeDeg = 0;
    1660           0 :                 dfNorthLatitudeDeg = 0;
    1661             :             }
    1662           5 :             delete poCT;
    1663             :         }
    1664             : 
    1665           5 :         delete poGeog;
    1666             :     }
    1667             : 
    1668           5 :     return ret;
    1669             : }
    1670             : 
    1671             : /************************************************************************/
    1672             : /*                    GDALGCPAntimeridianUnwrap()                       */
    1673             : /************************************************************************/
    1674             : 
    1675             : /* Deal with discontinuties of dfGCPX longitudes around the anti-meridian.
    1676             :  * Cf https://github.com/OSGeo/gdal/issues/8371
    1677             :  */
    1678          41 : static void GDALGCPAntimeridianUnwrap(int nGCPCount, GDAL_GCP *pasGCPList,
    1679             :                                       const OGRSpatialReference &oSRS,
    1680             :                                       CSLConstList papszOptions)
    1681             : {
    1682             :     const char *pszGCPAntimeridianUnwrap =
    1683          41 :         CSLFetchNameValueDef(papszOptions, "GCP_ANTIMERIDIAN_UNWRAP", "AUTO");
    1684         122 :     const bool bForced = EQUAL(pszGCPAntimeridianUnwrap, "YES") ||
    1685          40 :                          EQUAL(pszGCPAntimeridianUnwrap, "ON") ||
    1686         121 :                          EQUAL(pszGCPAntimeridianUnwrap, "TRUE") ||
    1687          40 :                          EQUAL(pszGCPAntimeridianUnwrap, "1");
    1688          47 :     if (bForced || (!oSRS.IsEmpty() && oSRS.IsGeographic() &&
    1689           6 :                     fabs(oSRS.GetAngularUnits(nullptr) -
    1690           6 :                          CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-8 &&
    1691           6 :                     EQUAL(pszGCPAntimeridianUnwrap, "AUTO")))
    1692             :     {
    1693           6 :         if (!bForced)
    1694             :         {
    1695             :             // Proceed to unwrapping only if the longitudes are within
    1696             :             // [-180, -170] or [170, 180]
    1697         425 :             for (int i = 0; i < nGCPCount; ++i)
    1698             :             {
    1699         423 :                 const double dfLongAbs = fabs(pasGCPList[i].dfGCPX);
    1700         423 :                 if (dfLongAbs > 180 || dfLongAbs < 170)
    1701             :                 {
    1702           3 :                     return;
    1703             :                 }
    1704             :             }
    1705             :         }
    1706             : 
    1707           3 :         bool bDone = false;
    1708         633 :         for (int i = 0; i < nGCPCount; ++i)
    1709             :         {
    1710         630 :             if (pasGCPList[i].dfGCPX < 0)
    1711             :             {
    1712          48 :                 if (!bDone)
    1713             :                 {
    1714           3 :                     bDone = true;
    1715           3 :                     CPLDebug("WARP", "GCP longitude unwrapping");
    1716             :                 }
    1717          48 :                 pasGCPList[i].dfGCPX += 360;
    1718             :             }
    1719             :         }
    1720             :     }
    1721             : }
    1722             : 
    1723             : /************************************************************************/
    1724             : /*              GDALGetGenImgProjTranformerOptionList()                 */
    1725             : /************************************************************************/
    1726             : 
    1727             : /** Return a XML string describing options accepted by
    1728             :  * GDALCreateGenImgProjTransformer2().
    1729             :  *
    1730             :  * @since 3.11
    1731             :  */
    1732        1954 : const char *GDALGetGenImgProjTranformerOptionList(void)
    1733             : {
    1734             :     return "<OptionList>"
    1735             :            "<Option name='SRC_SRS' type='string' description='WKT SRS, or any "
    1736             :            "string recognized by OGRSpatialReference::SetFromUserInput(), to "
    1737             :            "be used as an override for CRS of input dataset'/>"
    1738             :            "<Option name='DST_SRS' type='string' description='WKT SRS, or any "
    1739             :            "string recognized by OGRSpatialReference::SetFromUserInput(), to "
    1740             :            "be used as an override for CRS of output dataset'/>"
    1741             :            "<Option name='PROMOTE_TO_3D' type='boolean' description='"
    1742             :            "Whether to promote SRC_SRS / DST_SRS to 3D.' "
    1743             :            "default='NO'/>"
    1744             :            "<Option name='COORDINATE_OPERATION' type='string' description='"
    1745             :            "Coordinate operation, as a PROJ or WKT string, used as an override "
    1746             :            "over the normally computed pipeline. The pipeline must take into "
    1747             :            "account the axis order of the source and target SRS.'/>"
    1748             :            "<Option name='ALLOW_BALLPARK' type='boolean' description='"
    1749             :            "Whether ballpark coordinate operations are allowed.' "
    1750             :            "default='YES'/>"
    1751             :            "<Option name='ONLY_BEST' type='string-select' "
    1752             :            "description='"
    1753             :            "By default (at least in the PROJ 9.x series), PROJ may use "
    1754             :            "coordinate operations that are not the \"best\" if resources "
    1755             :            "(typically grids) needed to use them are missing. It will then "
    1756             :            "fallback to other coordinate operations that have a lesser "
    1757             :            "accuracy, for example using Helmert transformations, or in the "
    1758             :            "absence of such operations, to ones with potential very rough "
    1759             :            " accuracy, using \"ballpark\" transformations (see "
    1760             :            "https://proj.org/glossary.html). "
    1761             :            "When calling this method with YES, PROJ will only consider the "
    1762             :            "\"best\" operation, and error out (at Transform() time) if they "
    1763             :            "cannot be used. This method may be used together with "
    1764             :            "ALLOW_BALLPARK=NO to only allow best operations that have a known "
    1765             :            "accuracy. Note that this method has no effect on PROJ versions "
    1766             :            "before 9.2. The default value for this option can be also set with "
    1767             :            "the PROJ_ONLY_BEST_DEFAULT environment variable, or with the "
    1768             :            "\"only_best_default\" setting of proj.ini. Setting "
    1769             :            "ONLY_BEST=YES/NO overrides such default value' default='AUTO'>"
    1770             :            "  <Value>AUTO</Value>"
    1771             :            "  <Value>YES</Value>"
    1772             :            "  <Value>NO</Value>"
    1773             :            "</Option>"
    1774             :            "<Option name='COORDINATE_EPOCH' type='float' description='"
    1775             :            "Coordinate epoch, expressed as a decimal year. Useful for "
    1776             :            "time-dependent coordinate operations.'/>"
    1777             :            "<Option name='SRC_COORDINATE_EPOCH' type='float' description='"
    1778             :            "Coordinate epoch of source CRS, expressed as a decimal year. "
    1779             :            "Useful for time-dependent coordinate operations.'/>"
    1780             :            "<Option name='DST_COORDINATE_EPOCH' type='float' description='"
    1781             :            "Coordinate epoch of target CRS, expressed as a decimal year. "
    1782             :            "Useful for time-dependent coordinate operations.'/>"
    1783             :            "<Option name='GCPS_OK' type='boolean' description='"
    1784             :            "Allow use of GCPs.' default='YES'/>"
    1785             :            "<Option name='REFINE_MINIMUM_GCPS' type='int' description='"
    1786             :            "The minimum amount of GCPs that should be available after the "
    1787             :            "refinement'/>"
    1788             :            "<Option name='REFINE_TOLERANCE' type='float' description='"
    1789             :            "The tolerance that specifies when a GCP will be eliminated.'/>"
    1790             :            "<Option name='MAX_GCP_ORDER' type='int' description='"
    1791             :            "The maximum order to use for GCP derived polynomials if possible. "
    1792             :            "The default is to autoselect based on the number of GCPs. A value "
    1793             :            "of -1 triggers use of Thin Plate Spline instead of polynomials.'/>"
    1794             :            "<Option name='GCP_ANTIMERIDIAN_UNWRAP' type='string-select' "
    1795             :            "description='"
    1796             :            "Whether to \"unwrap\" longitudes of ground control points that "
    1797             :            "span the antimeridian. For datasets with GCPs in "
    1798             :            "longitude/latitude coordinate space spanning the antimeridian, "
    1799             :            "longitudes will have a discontinuity on +/- 180 deg, and will "
    1800             :            "result in a subset of the GCPs with longitude in the [-180,-170] "
    1801             :            "range and another subset in [170, 180]. By default (AUTO), that "
    1802             :            "situation will be detected and longitudes in [-180,-170] will be "
    1803             :            "shifted to [180, 190] to get a continuous set. This option can be "
    1804             :            "set to YES to force that behavior (useful if no SRS information is "
    1805             :            "available), or to NO to disable it.' default='AUTO'>"
    1806             :            "  <Value>AUTO</Value>"
    1807             :            "  <Value>YES</Value>"
    1808             :            "  <Value>NO</Value>"
    1809             :            "</Option>"
    1810             :            "<Option name='SRC_METHOD' alias='METHOD' type='string-select' "
    1811             :            "description='"
    1812             :            "Force only one geolocation method to be considered on the source "
    1813             :            "dataset. Will be used for pixel/line to georef transformation on "
    1814             :            "the source dataset. NO_GEOTRANSFORM can be used to specify the "
    1815             :            "identity geotransform (ungeoreferenced image)'>"
    1816             :            "  <Value>GEOTRANSFORM</Value>"
    1817             :            "  <Value>GCP_POLYNOMIAL</Value>"
    1818             :            "  <Value>GCP_TPS</Value>"
    1819             :            "  <Value>GCP_HOMOGRAPHY</Value>"
    1820             :            "  <Value>GEOLOC_ARRAY</Value>"
    1821             :            "  <Value>RPC</Value>"
    1822             :            "  <Value>NO_GEOTRANSFORM</Value>"
    1823             :            "</Option>"
    1824             :            "<Option name='DST_METHOD' type='string-select' description='"
    1825             :            "Force only one geolocation method to be considered on the target "
    1826             :            "dataset. Will be used for pixel/line to georef transformation on "
    1827             :            "the targe dataset. NO_GEOTRANSFORM can be used to specify the "
    1828             :            "identity geotransform (ungeoreferenced image)'>"
    1829             :            "  <Value>GEOTRANSFORM</Value>"
    1830             :            "  <Value>GCP_POLYNOMIAL</Value>"
    1831             :            "  <Value>GCP_TPS</Value>"
    1832             :            "  <Value>GCP_HOMOGRAPHY</Value>"
    1833             :            "  <Value>GEOLOC_ARRAY</Value>"
    1834             :            "  <Value>RPC</Value>"
    1835             :            "  <Value>NO_GEOTRANSFORM</Value>"
    1836             :            "</Option>"
    1837             :            "<Option name='RPC_HEIGHT' type='float' description='"
    1838             :            "A fixed height to be used with RPC calculations. If RPC_HEIGHT and "
    1839             :            "RPC_DEM are not specified but that the RPC metadata domain contains"
    1840             :            " a HEIGHT_DEFAULT item (for example, the DIMAP driver may fill it),"
    1841             :            "this value will be used as the RPC_HEIGHT. Otherwise, if none of "
    1842             :            "RPC_HEIGHT and RPC_DEM are specified as transformer options and "
    1843             :            "if HEIGHT_DEFAULT is no available, a height of 0 will be used.'/>"
    1844             :            "<Option name='RPC_DEM' type='string' description='"
    1845             :            "Name of a GDAL dataset (a DEM file typically) used to extract "
    1846             :            "elevation offsets from. In this situation the Z passed into the "
    1847             :            "transformation function is assumed to be height above ground. "
    1848             :            "This option should be used in replacement of RPC_HEIGHT to provide "
    1849             :            "a way of defining a non uniform ground for the target scene.'/>"
    1850             :            "<Option name='RPC_HEIGHT_SCALE' type='float' description='"
    1851             :            "Factor used to multiply heights above ground. Useful when "
    1852             :            "elevation offsets of the DEM are not expressed in meters.'/>"
    1853             :            "<Option name='RPC_DEMINTERPOLATION' type='string-select' "
    1854             :            "description='DEM interpolation method' default='BILINEAR'>"
    1855             :            "  <Value>NEAR</Value>"
    1856             :            "  <Value>BILINEAR</Value>"
    1857             :            "  <Value>CUBIC</Value>"
    1858             :            "</Option>"
    1859             :            "<Option name='RPC_DEM_MISSING_VALUE' type='float' description='"
    1860             :            "Value of DEM height that must be used in case the DEM has nodata "
    1861             :            "value at the sampling point, or if its extent does not cover the "
    1862             :            "requested coordinate. When not specified, missing values will "
    1863             :            "cause a failed transform.'/>"
    1864             :            "<Option name='RPC_DEM_SRS' type='string' description='"
    1865             :            "WKT SRS, or any string recognized by "
    1866             :            "OGRSpatialReference::SetFromUserInput(), to be used as an "
    1867             :            "override for DEM SRS. Useful if DEM SRS does not have an explicit "
    1868             :            "vertical component.'/>"
    1869             :            "<Option name='RPC_DEM_APPLY_VDATUM_SHIFT' type='boolean' "
    1870             :            "description='"
    1871             :            "Whether the vertical component of a compound SRS for the DEM "
    1872             :            "should be used (when it is present). This is useful so as to "
    1873             :            "be able to transform the raw values from the DEM expressed with "
    1874             :            "respect to a geoid to the heights with respect to the WGS84 "
    1875             :            "ellipsoid. When this is enabled, the GTIFF_REPORT_COMPD_CS "
    1876             :            "configuration option will be also set temporarily so as to get "
    1877             :            "the vertical information from GeoTIFF files.' default='YES'/>"
    1878             :            "<Option name='RPC_PIXEL_ERROR_THRESHOLD' type='float' description='"
    1879             :            "Overrides the dfPixErrThreshold parameter, i.e. the error "
    1880             :            "(measured in pixels) allowed in the iterative solution of "
    1881             :            "pixel/line to lat/long computations (the other way is always "
    1882             :            "exact given the equations).'/>"
    1883             :            "<Option name='RPC_MAX_ITERATIONS' type='int' description='"
    1884             :            "Maximum number of iterations allowed in the iterative solution of "
    1885             :            "pixel/line to lat/long computations. Default value is 10 in the "
    1886             :            "absence of a DEM, or 20 if there is a DEM.'/>"
    1887             :            "<Option name='RPC_FOOTPRINT' type='string' description='"
    1888             :            "WKT or GeoJSON polygon (in long / lat coordinate space) with a "
    1889             :            "validity footprint for the RPC. Any coordinate transformation that "
    1890             :            "goes from or arrive outside this footprint will be considered "
    1891             :            "invalid. This* is useful in situations where the RPC values become "
    1892             :            "highly unstable outside of the area on which they have been "
    1893             :            "computed for, potentially leading to undesirable \"echoes\" / "
    1894             :            "false positives. This requires GDAL to be built against GEOS..'/>"
    1895             :            "<Option name='RPC_MAX_ITERATIONS' type='int' description='"
    1896             :            "Maximum number of iterations allowed in the iterative solution of "
    1897             :            "pixel/line to lat/long computations. Default value is 10 in the "
    1898             :            "absence of a DEM, or 20 if there is a DEM.'/>"
    1899             :            "<Option name='INSERT_CENTER_LONG' type='boolean' description='"
    1900             :            "May be set to FALSE to disable setting up a CENTER_LONG value on "
    1901             :            "the coordinate system to rewrap things around the center of the "
    1902             :            "image.' default='YES'/>"
    1903             :            "<Option name='SRC_APPROX_ERROR_IN_SRS_UNIT' type='float' "
    1904             :            "description='"
    1905             :            "Use an approximate transformer for the source transformer. Must be "
    1906             :            "defined together with SRC_APPROX_ERROR_IN_PIXEL to be taken into "
    1907             :            "account.'/>"
    1908             :            "<Option name='SRC_APPROX_ERROR_IN_PIXEL' type='float' "
    1909             :            "description='"
    1910             :            "Use an approximate transformer for the source transformer. Must be "
    1911             :            "defined together with SRC_APPROX_ERROR_IN_SRS_UNIT to be taken "
    1912             :            "into "
    1913             :            "account.'/>"
    1914             :            "<Option name='DST_APPROX_ERROR_IN_SRS_UNIT' type='float' "
    1915             :            "description='"
    1916             :            "Use an approximate transformer for the target transformer. Must be "
    1917             :            "defined together with DST_APPROX_ERROR_IN_PIXEL to be taken into "
    1918             :            "account.'/>"
    1919             :            "<Option name='DST_APPROX_ERROR_IN_PIXEL' type='float' "
    1920             :            "description='"
    1921             :            "Use an approximate transformer for the target transformer. Must be "
    1922             :            "defined together with DST_APPROX_ERROR_IN_SRS_UNIT to be taken "
    1923             :            "into "
    1924             :            "account.'/>"
    1925             :            "<Option name='REPROJECTION_APPROX_ERROR_IN_SRC_SRS_UNIT' "
    1926             :            "type='float' "
    1927             :            "description='"
    1928             :            "Use an approximate transformer for the coordinate reprojection. "
    1929             :            "Must be used together with "
    1930             :            "REPROJECTION_APPROX_ERROR_IN_DST_SRS_UNIT to be taken into "
    1931             :            "account.'/>"
    1932             :            "<Option name='REPROJECTION_APPROX_ERROR_IN_DST_SRS_UNIT' "
    1933             :            "type='float' "
    1934             :            "description='"
    1935             :            "Use an approximate transformer for the coordinate reprojection. "
    1936             :            "Must be used together with "
    1937             :            "REPROJECTION_APPROX_ERROR_IN_SRC_SRS_UNIT to be taken into "
    1938             :            "account.'/>"
    1939             :            "<Option name='AREA_OF_INTEREST' type='string' "
    1940             :            "description='"
    1941             :            "Area of interest, as "
    1942             :            "west_lon_deg,south_lat_deg,east_lon_deg,north_lat_deg, used to "
    1943             :            "compute the best coordinate operation between the source and "
    1944             :            "target SRS. If not specified, the bounding box of the source "
    1945             :            "raster will be used.'/>"
    1946             :            "<Option name='TARGET_EXTENT' type='string' "
    1947             :            "description='Target extent as minx,miny,maxx,maxy expressed in "
    1948             :            "target SRS.'/>"
    1949             :            "<Option name='GEOLOC_BACKMAP_OVERSAMPLE_FACTOR' type='float' "
    1950             :            "min='0.1' max='2' description='"
    1951             :            "Oversample factor used to derive the size of the \"backmap\" used "
    1952             :            "for geolocation array transformers.' default='1.3'/>"
    1953             :            "<Option name='GEOLOC_USE_TEMP_DATASETS' type='boolean' "
    1954             :            "description='"
    1955             :            "Whether temporary GeoTIFF datasets should be used to store the "
    1956             :            "backmap. The default is NO, that is to use in-memory arrays, "
    1957             :            "unless the number of pixels of the geolocation array is greater "
    1958             :            "than 16 megapixels.' default='NO'/>"
    1959             :            "<Option name='GEOLOC_ARRAY' alias='SRC_GEOLOC_ARRAY' type='string' "
    1960             :            "description='"
    1961             :            "Name of a GDAL dataset containing a geolocation array and "
    1962             :            "associated metadata. This is an alternative to having geolocation "
    1963             :            "information described in the GEOLOCATION metadata domain of the "
    1964             :            "source dataset. The dataset specified may have a GEOLOCATION "
    1965             :            "metadata domain containing appropriate metadata, however default "
    1966             :            "values are assigned for all omitted items. X_BAND defaults to 1 "
    1967             :            "and Y_BAND to 2, however the dataset must contain exactly 2 bands. "
    1968             :            "PIXEL_OFFSET and LINE_OFFSET default to 0. PIXEL_STEP and "
    1969             :            "LINE_STEP default to the ratio of the width/height of the source "
    1970             :            "dataset divided by the with/height of the geolocation array. "
    1971             :            "SRS defaults to the spatial reference system of the geolocation "
    1972             :            "array dataset, if set, otherwise WGS84 is used. "
    1973             :            "GEOREFERENCING_CONVENTION is selected from the main metadata "
    1974             :            "domain if it is omitted from the GEOLOCATION domain, and if not "
    1975             :            "available TOP_LEFT_CORNER is assigned as a default. "
    1976             :            "If GEOLOC_ARRAY is set SRC_METHOD defaults to GEOLOC_ARRAY.'/>"
    1977             :            "<Option name='DST_GEOLOC_ARRAY' type='string' "
    1978             :            "description='"
    1979             :            "Name of a GDAL dataset that contains at least 2 bands with the X "
    1980             :            "and Y geolocation bands. This is an alternative to having "
    1981             :            "geolocation information described in the GEOLOCATION metadata "
    1982             :            "domain of the destination dataset. See SRC_GEOLOC_ARRAY "
    1983             :            "description for details, assumptions, and defaults. If this "
    1984             :            "option is set, DST_METHOD=GEOLOC_ARRAY will be assumed if not "
    1985             :            "set.'/>"
    1986             :            "<Option name='GEOLOC_NORMALIZE_LONGITUDE_MINUS_180_PLUS_180' "
    1987             :            "type='boolean' "
    1988             :            "description='"
    1989             :            "Force geolocation longitudes into -180,180 when longitude/latitude "
    1990             :            "is the coordinate system of the geolocation arrays' default='NO'>"
    1991             :            "  <Value>YES</Value>"
    1992             :            "  <Value>NO</Value>"
    1993             :            "</Option>"
    1994             :            "<Option name='NUM_THREADS' type='string' "
    1995             :            "description='Number of threads to use'/>"
    1996        1954 :            "</OptionList>";
    1997             : }
    1998             : 
    1999             : /************************************************************************/
    2000             : /*                  GDALCreateGenImgProjTransformer2()                  */
    2001             : /************************************************************************/
    2002             : 
    2003             : /* clang-format off */
    2004             : /**
    2005             :  * Create image to image transformer.
    2006             :  *
    2007             :  * This function creates a transformation object that maps from pixel/line
    2008             :  * coordinates on one image to pixel/line coordinates on another image.  The
    2009             :  * images may potentially be georeferenced in different coordinate systems,
    2010             :  * and may used GCPs to map between their pixel/line coordinates and
    2011             :  * georeferenced coordinates (as opposed to the default assumption that their
    2012             :  * geotransform should be used).
    2013             :  *
    2014             :  * This transformer potentially performs three concatenated transformations.
    2015             :  *
    2016             :  * The first stage is from source image pixel/line coordinates to source
    2017             :  * image georeferenced coordinates, and may be done using the geotransform,
    2018             :  * or if not defined using a polynomial model derived from GCPs.  If GCPs
    2019             :  * are used this stage is accomplished using GDALGCPTransform().
    2020             :  *
    2021             :  * The second stage is to change projections from the source coordinate system
    2022             :  * to the destination coordinate system, assuming they differ.  This is
    2023             :  * accomplished internally using GDALReprojectionTransform().
    2024             :  *
    2025             :  * The third stage is converting from destination image georeferenced
    2026             :  * coordinates to destination image coordinates.  This is done using the
    2027             :  * destination image geotransform, or if not available, using a polynomial
    2028             :  * model derived from GCPs. If GCPs are used this stage is accomplished using
    2029             :  * GDALGCPTransform().  This stage is skipped if hDstDS is NULL when the
    2030             :  * transformation is created.
    2031             :  *
    2032             :  * Supported Options (specified with the -to switch of gdalwarp for example):
    2033             :  * <ul>
    2034             :  * <li> SRC_SRS: WKT SRS, or any string recognized by
    2035             :  * OGRSpatialReference::SetFromUserInput(), to be used as an override for
    2036             :  * hSrcDS.</li>
    2037             :  * <li> DST_SRS: WKT SRS, or any string recognized by
    2038             :  * OGRSpatialReference::SetFromUserInput(),  to be used as an override for
    2039             :  * hDstDS.
    2040             :  * </li>
    2041             :  * <li>PROMOTE_TO_3D=YES/NO: whether to promote SRC_SRS / DST_SRS to 3D.
    2042             :  * Default is NO</li>
    2043             :  * <li> COORDINATE_OPERATION: (GDAL &gt;= 3.0) Coordinate operation, as
    2044             :  * a PROJ or WKT string, used as an override over the normally computed
    2045             :  * pipeline. The pipeline must take into account the axis order of the source
    2046             :  * and target SRS.
    2047             :  * </li>
    2048             :  * <li> ALLOW_BALLPARK=YES/NO: (GDAL &gt;= 3.11) Whether ballpark coordinate
    2049             :  * operations are allowed. Defaults to YES.</li>
    2050             :  * <li> ONLY_BEST=YES/NO/AUTO: (GDAL &gt;= 3.11) By default (at least in the
    2051             :  * PROJ 9.x series), PROJ may use coordinate
    2052             :  * operations that are not the "best" if resources (typically grids) needed
    2053             :  * to use them are missing. It will then fallback to other coordinate operations
    2054             :  * that have a lesser accuracy, for example using Helmert transformations,
    2055             :  * or in the absence of such operations, to ones with potential very rough
    2056             :  * accuracy, using "ballpark" transformations
    2057             :  * (see https://proj.org/glossary.html).
    2058             :  * When calling this method with YES, PROJ will only consider the
    2059             :  * "best" operation, and error out (at Transform() time) if they cannot be
    2060             :  * used.
    2061             :  * This method may be used together with ALLOW_BALLPARK=NO to
    2062             :  * only allow best operations that have a known accuracy.
    2063             :  * Note that this method has no effect on PROJ versions before 9.2.
    2064             :  * The default value for this option can be also set with the
    2065             :  * PROJ_ONLY_BEST_DEFAULT environment variable, or with the "only_best_default"
    2066             :  * setting of proj.ini. Calling SetOnlyBest() overrides such default value.</li>
    2067             :  * <li> COORDINATE_EPOCH: (GDAL &gt;= 3.0) Coordinate epoch,
    2068             :  * expressed as a decimal year. Useful for time-dependent coordinate operations.
    2069             :  * </li>
    2070             :  * <li> SRC_COORDINATE_EPOCH: (GDAL &gt;= 3.4) Coordinate epoch of source CRS,
    2071             :  * expressed as a decimal year. Useful for time-dependent coordinate operations.
    2072             :  * </li>
    2073             :  * <li> DST_COORDINATE_EPOCH: (GDAL &gt;= 3.4) Coordinate epoch of target CRS,
    2074             :  * expressed as a decimal year. Useful for time-dependent coordinate operations.
    2075             :  * </li>
    2076             :  * <li> GCPS_OK: If false, GCPs will not be used, default is TRUE.
    2077             :  * </li>
    2078             :  * <li> REFINE_MINIMUM_GCPS: The minimum amount of GCPs that should be available
    2079             :  * after the refinement.
    2080             :  * </li>
    2081             :  * <li> REFINE_TOLERANCE: The tolerance that specifies when a GCP will be
    2082             :  * eliminated.
    2083             :  * </li>
    2084             :  * <li> MAX_GCP_ORDER: the maximum order to use for GCP derived polynomials if
    2085             :  * possible.  The default is to autoselect based on the number of GCPs.
    2086             :  * A value of -1 triggers use of Thin Plate Spline instead of polynomials.
    2087             :  * </li>
    2088             :  * <li>GCP_ANTIMERIDIAN_UNWRAP=AUTO/YES/NO. (GDAL &gt;= 3.8) Whether to
    2089             :  * "unwrap" longitudes of ground control points that span the antimeridian.
    2090             :  * For datasets with GCPs in longitude/latitude coordinate space spanning the
    2091             :  * antimeridian, longitudes will have a discontinuity on +/- 180 deg, and
    2092             :  * will result in a subset of the GCPs with longitude in the [-180,-170] range
    2093             :  * and another subset in [170, 180]. By default (AUTO), that situation will be
    2094             :  * detected and longitudes in [-180,-170] will be shifted to [180, 190] to get
    2095             :  * a continuous set. This option can be set to YES to force that behavior
    2096             :  * (useful if no SRS information is available), or to NO to disable it.
    2097             :  * </li>
    2098             :  * <li> SRC_METHOD: may have a value which is one of GEOTRANSFORM, GCP_HOMOGRAPHY,
    2099             :  * GCP_POLYNOMIAL, GCP_TPS, GEOLOC_ARRAY, RPC to force only one geolocation
    2100             :  * method to be considered on the source dataset. Will be used for pixel/line
    2101             :  * to georef transformation on the source dataset. NO_GEOTRANSFORM can be
    2102             :  * used to specify the identity geotransform (ungeoreferenced image)
    2103             :  * </li>
    2104             :  * <li> DST_METHOD: may have a value which is one of GEOTRANSFORM,
    2105             :  * GCP_POLYNOMIAL, GCP_HOMOGRAPHY, GCP_TPS, GEOLOC_ARRAY (added in 3.5), RPC to
    2106             :  * force only one
    2107             :  * geolocation method to be considered on the target dataset.  Will be used for
    2108             :  * pixel/line to georef transformation on the destination dataset.
    2109             :  * NO_GEOTRANSFORM can be used to specify the identity geotransform
    2110             :  * (ungeoreferenced image)
    2111             :  * </li>
    2112             :  * <li> RPC_HEIGHT: A fixed height to be used with RPC
    2113             :  * calculations. If RPC_HEIGHT and RPC_DEM are not specified but that the RPC
    2114             :  * metadata domain contains a HEIGHT_DEFAULT item (for example, the DIMAP driver
    2115             :  * may fill it), this value will be used as the RPC_HEIGHT. Otherwise, if none
    2116             :  * of RPC_HEIGHT and RPC_DEM are specified as transformer
    2117             :  * options and if HEIGHT_DEFAULT is no available, a height of 0 will be used.
    2118             :  * </li>
    2119             :  * <li> RPC_DEM: The name of a DEM file to be used with RPC
    2120             :  * calculations. See GDALCreateRPCTransformerV2() for more details.
    2121             :  * </li>
    2122             :  * <li> Other RPC related options. See GDALCreateRPCTransformerV2()
    2123             :  * </li>
    2124             :  * <li>
    2125             :  * INSERT_CENTER_LONG: May be set to FALSE to disable setting up a CENTER_LONG
    2126             :  * value on the coordinate system to rewrap things around the center of the
    2127             :  * image.
    2128             :  * </li>
    2129             :  * <li> SRC_APPROX_ERROR_IN_SRS_UNIT=err_threshold_in_SRS_units. (GDAL
    2130             :  * &gt;= 2.2) Use an approximate transformer for the source transformer. Must be
    2131             :  * defined together with SRC_APPROX_ERROR_IN_PIXEL to be taken into account.
    2132             :  * </li>
    2133             :  * <li> SRC_APPROX_ERROR_IN_PIXEL=err_threshold_in_pixel. (GDAL &gt;= 2.2) Use
    2134             :  * an approximate transformer for the source transformer.. Must be defined
    2135             :  * together with SRC_APPROX_ERROR_IN_SRS_UNIT to be taken into account.
    2136             :  * </li>
    2137             :  * <li>
    2138             :  * DST_APPROX_ERROR_IN_SRS_UNIT=err_threshold_in_SRS_units. (GDAL &gt;= 2.2) Use
    2139             :  * an approximate transformer for the destination transformer. Must be defined
    2140             :  * together with DST_APPROX_ERROR_IN_PIXEL to be taken into account.
    2141             :  * </li>
    2142             :  * <li>
    2143             :  * DST_APPROX_ERROR_IN_PIXEL=err_threshold_in_pixel. (GDAL &gt;= 2.2) Use an
    2144             :  * approximate transformer for the destination transformer. Must be defined
    2145             :  * together with DST_APPROX_ERROR_IN_SRS_UNIT to be taken into account.
    2146             :  * </li>
    2147             :  * <li>
    2148             :  * REPROJECTION_APPROX_ERROR_IN_SRC_SRS_UNIT=err_threshold_in_src_SRS_units.
    2149             :  * (GDAL &gt;= 2.2) Use an approximate transformer for the coordinate
    2150             :  * reprojection. Must be used together with
    2151             :  * REPROJECTION_APPROX_ERROR_IN_DST_SRS_UNIT to be taken into account.
    2152             :  * </li>
    2153             :  * <li>
    2154             :  * REPROJECTION_APPROX_ERROR_IN_DST_SRS_UNIT=err_threshold_in_dst_SRS_units.
    2155             :  * (GDAL &gt;= 2.2) Use an approximate transformer for the coordinate
    2156             :  * reprojection. Must be used together with
    2157             :  * REPROJECTION_APPROX_ERROR_IN_SRC_SRS_UNIT to be taken into account.
    2158             :  * </li>
    2159             :  * <li>
    2160             :  * AREA_OF_INTEREST=west_lon_deg,south_lat_deg,east_lon_deg,north_lat_deg. (GDAL
    2161             :  * &gt;= 3.0) Area of interest, used to compute the best coordinate operation
    2162             :  * between the source and target SRS. If not specified, the bounding box of the
    2163             :  * source raster will be used.
    2164             :  * </li>
    2165             :  * <li> GEOLOC_BACKMAP_OVERSAMPLE_FACTOR=[0.1,2]. (GDAL &gt;= 3.5) Oversample
    2166             :  * factor used to derive the size of the "backmap" used for geolocation array
    2167             :  * transformers. Default value is 1.3.
    2168             :  * </li>
    2169             :  * <li> GEOLOC_USE_TEMP_DATASETS=YES/NO.
    2170             :  * (GDAL &gt;= 3.5) Whether temporary GeoTIFF datasets should be used to store
    2171             :  * the backmap. The default is NO, that is to use in-memory arrays, unless the
    2172             :  * number of pixels of the geolocation array is greater than 16 megapixels.
    2173             :  * </li>
    2174             :  * <li>
    2175             :  * GEOLOC_ARRAY/SRC_GEOLOC_ARRAY=filename. (GDAL &gt;= 3.5.2) Name of a GDAL
    2176             :  * dataset containing a geolocation array and associated metadata. This is an
    2177             :  * alternative to having geolocation information described in the GEOLOCATION
    2178             :  * metadata domain of the source dataset. The dataset specified may have a
    2179             :  * GEOLOCATION metadata domain containing appropriate metadata, however default
    2180             :  * values are assigned for all omitted items. X_BAND defaults to 1 and Y_BAND to
    2181             :  * 2, however the dataset must contain exactly 2 bands. PIXEL_OFFSET and
    2182             :  * LINE_OFFSET default to 0. PIXEL_STEP and LINE_STEP default to the ratio of
    2183             :  * the width/height of the source dataset divided by the with/height of the
    2184             :  * geolocation array. SRS defaults to the geolocation array dataset's spatial
    2185             :  * reference system if set, otherwise WGS84 is used.
    2186             :  * GEOREFERENCING_CONVENTION is selected from the main metadata domain if it
    2187             :  * is omitted from the GEOLOCATION domain, and if not available
    2188             :  * TOP_LEFT_CORNER is assigned as a default.
    2189             :  * If GEOLOC_ARRAY is set SRC_METHOD
    2190             :  * defaults to GEOLOC_ARRAY.
    2191             :  * </li>
    2192             :  * <li>DST_GEOLOC_ARRAY=filename. (GDAL &gt;= 3.5.2) Name of a
    2193             :  * GDAL dataset that contains at least 2 bands with the X and Y geolocation
    2194             :  * bands. This is an alternative to having geolocation information described in
    2195             :  * the GEOLOCATION metadata domain of the destination dataset. See
    2196             :  * SRC_GEOLOC_ARRAY description for details, assumptions, and defaults. If this
    2197             :  * option is set, DST_METHOD=GEOLOC_ARRAY will be assumed if not set.
    2198             :  * </li>
    2199             :  * <li>GEOLOC_NORMALIZE_LONGITUDE_MINUS_180_PLUS_180=YES/NO. (GDAL &gt;= 3.12.0)
    2200             :  * Whether to force geolocation longitudes into -180,180 when longitude/latitude is
    2201             :  * the coordinate system of the geolocation arrays. The default is to enable this mode
    2202             :  * when the values in the geolocation array are in the -180,180, otherwise NO.
    2203             :  * </li>
    2204             :  * </ul>
    2205             :  *
    2206             :  * The use case for the *_APPROX_ERROR_* options is when defining an approximate
    2207             :  * transformer on top of the GenImgProjTransformer globally is not practical.
    2208             :  * Such a use case is when the source dataset has RPC with a RPC DEM. In such
    2209             :  * case we don't want to use the approximate transformer on the RPC
    2210             :  * transformation, as the RPC DEM generally involves non-linearities that the
    2211             :  * approximate transformer will not detect. In such case, we must a
    2212             :  * non-approximated GenImgProjTransformer, but it might be worthwhile to use
    2213             :  * approximate sub- transformers, for example on coordinate reprojection. For
    2214             :  * example if warping from a source dataset with RPC to a destination dataset
    2215             :  * with a UTM projection, since the inverse UTM transformation is rather costly.
    2216             :  * In which case, one can use the REPROJECTION_APPROX_ERROR_IN_SRC_SRS_UNIT and
    2217             :  * REPROJECTION_APPROX_ERROR_IN_DST_SRS_UNIT options.
    2218             :  *
    2219             :  * The list of supported options can also be programmatically obtained with
    2220             :  * GDALGetGenImgProjTranformerOptionList().
    2221             :  *
    2222             :  * @param hSrcDS source dataset, or NULL.
    2223             :  * @param hDstDS destination dataset (or NULL).
    2224             :  * @param papszOptions NULL-terminated list of string options (or NULL).
    2225             :  *
    2226             :  * @return handle suitable for use GDALGenImgProjTransform(), and to be
    2227             :  * deallocated with GDALDestroyGenImgProjTransformer() or NULL on failure.
    2228             :  */
    2229             : /* clang-format on */
    2230             : 
    2231        1953 : void *GDALCreateGenImgProjTransformer2(GDALDatasetH hSrcDS, GDALDatasetH hDstDS,
    2232             :                                        CSLConstList papszOptions)
    2233             : 
    2234             : {
    2235        1953 :     GDALValidateOptions(GDALGetGenImgProjTranformerOptionList(), papszOptions,
    2236             :                         "option", "transformer options");
    2237             : 
    2238        1953 :     double dfWestLongitudeDeg = 0.0;
    2239        1953 :     double dfSouthLatitudeDeg = 0.0;
    2240        1953 :     double dfEastLongitudeDeg = 0.0;
    2241        1953 :     double dfNorthLatitudeDeg = 0.0;
    2242        1953 :     bool bHasAreaOfInterest = false;
    2243        1953 :     if (const char *pszAreaOfInterest =
    2244        1953 :             CSLFetchNameValue(papszOptions, "AREA_OF_INTEREST"))
    2245             :     {
    2246             :         const CPLStringList aosTokens(
    2247           0 :             CSLTokenizeString2(pszAreaOfInterest, ", ", 0));
    2248           0 :         if (aosTokens.size() == 4)
    2249             :         {
    2250           0 :             dfWestLongitudeDeg = CPLAtof(aosTokens[0]);
    2251           0 :             dfSouthLatitudeDeg = CPLAtof(aosTokens[1]);
    2252           0 :             dfEastLongitudeDeg = CPLAtof(aosTokens[2]);
    2253           0 :             dfNorthLatitudeDeg = CPLAtof(aosTokens[3]);
    2254           0 :             bHasAreaOfInterest = true;
    2255             :         }
    2256             :     }
    2257             : 
    2258        1953 :     const char *pszCO = CSLFetchNameValue(papszOptions, "COORDINATE_OPERATION");
    2259             : 
    2260             :     const auto SetAxisMapping =
    2261        4039 :         [papszOptions](OGRSpatialReference &oSRS, const char *pszPrefix)
    2262             :     {
    2263        1347 :         const char *pszMapping = CSLFetchNameValue(
    2264        2694 :             papszOptions, std::string(pszPrefix)
    2265        1347 :                               .append("_DATA_AXIS_TO_SRS_AXIS_MAPPING")
    2266             :                               .c_str());
    2267        1347 :         if (pszMapping)
    2268             :         {
    2269           4 :             CPLStringList aosTokens(CSLTokenizeString2(pszMapping, ",", 0));
    2270           4 :             std::vector<int> anMapping;
    2271           6 :             for (int i = 0; i < aosTokens.size(); ++i)
    2272           4 :                 anMapping.push_back(atoi(aosTokens[i]));
    2273           2 :             oSRS.SetDataAxisToSRSAxisMapping(anMapping);
    2274             :         }
    2275             :         else
    2276             :         {
    2277        1345 :             const char *pszStrategy = CSLFetchNameValueDef(
    2278             :                 papszOptions,
    2279        2690 :                 std::string(pszPrefix).append("_AXIS_MAPPING_STRATEGY").c_str(),
    2280             :                 "TRADITIONAL_GIS_ORDER");
    2281        1345 :             if (EQUAL(pszStrategy, "TRADITIONAL_GIS_ORDER"))
    2282        1344 :                 oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2283           1 :             else if (EQUAL(pszStrategy, "AUTHORITY_COMPLIANT"))
    2284           1 :                 oSRS.SetAxisMappingStrategy(OAMS_AUTHORITY_COMPLIANT);
    2285             :             else
    2286             :             {
    2287           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
    2288             :                          "Unrecognized value '%s' for %s", pszStrategy,
    2289           0 :                          std::string(pszPrefix)
    2290           0 :                              .append("_AXIS_MAPPING_STRATEGY")
    2291             :                              .c_str());
    2292           0 :                 return false;
    2293             :             }
    2294             :         }
    2295        1347 :         return true;
    2296        1953 :     };
    2297             : 
    2298             :     /* -------------------------------------------------------------------- */
    2299             :     /*      Initialize the transform info.                                  */
    2300             :     /* -------------------------------------------------------------------- */
    2301             :     GDALGenImgProjTransformInfo *psInfo =
    2302        1953 :         GDALCreateGenImgProjTransformerInternal();
    2303             : 
    2304             :     const auto DealWithForwardOrInverse =
    2305        3892 :         [bHasAreaOfInterest, &dfWestLongitudeDeg, &dfSouthLatitudeDeg,
    2306             :          &dfEastLongitudeDeg, &dfNorthLatitudeDeg, pszCO, papszOptions,
    2307             :          &SetAxisMapping](GDALGenImgProjTransformPart &part, GDALDatasetH hDS,
    2308             :                           const char *pszPrefix, OGRSpatialReference &oSRS,
    2309       37400 :                           bool &bCanUseGeoTransform)
    2310             :     {
    2311             :         const int nOrder =
    2312        3892 :             atoi(CSLFetchNameValueDef(papszOptions, "MAX_GCP_ORDER", "0"));
    2313             : 
    2314             :         const bool bGCPUseOK =
    2315        3892 :             CPLTestBool(CSLFetchNameValueDef(papszOptions, "GCPS_OK", "YES"));
    2316        3892 :         const int nMinimumGcps = atoi(
    2317             :             CSLFetchNameValueDef(papszOptions, "REFINE_MINIMUM_GCPS", "-1"));
    2318             : 
    2319             :         const char *pszRefineTolerance =
    2320        3892 :             CSLFetchNameValue(papszOptions, "REFINE_TOLERANCE");
    2321        3892 :         const bool bRefine = pszRefineTolerance != nullptr;
    2322             :         const double dfTolerance =
    2323        3892 :             pszRefineTolerance ? CPLAtof(pszRefineTolerance) : 0.0;
    2324             : 
    2325             :         const std::string osSRSOptionName =
    2326       11676 :             std::string(pszPrefix).append("_SRS");
    2327             :         const char *pszSRS =
    2328        3892 :             CSLFetchNameValue(papszOptions, osSRSOptionName.c_str());
    2329        3892 :         if (pszSRS)
    2330             :         {
    2331        2690 :             if (pszSRS[0] != '\0' &&
    2332        1343 :                 oSRS.SetFromUserInput(pszSRS) != OGRERR_NONE)
    2333             :             {
    2334           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    2335             :                          "Failed to import coordinate system `%s'.", pszSRS);
    2336           0 :                 return false;
    2337             :             }
    2338        1347 :             if (!SetAxisMapping(oSRS, osSRSOptionName.c_str()))
    2339           0 :                 return false;
    2340             :         }
    2341             : 
    2342        3892 :         CSLConstList papszMD = nullptr;
    2343             :         GDALRPCInfoV2 sRPCInfo;
    2344             : 
    2345        3892 :         bCanUseGeoTransform = false;
    2346             : 
    2347        3892 :         const char *pszMethod = CSLFetchNameValue(
    2348        7784 :             papszOptions, std::string(pszPrefix).append("_METHOD").c_str());
    2349        3892 :         if (!pszMethod && EQUAL(pszPrefix, "SRC"))
    2350        1916 :             pszMethod = CSLFetchNameValue(papszOptions, "METHOD");
    2351             : 
    2352        3892 :         const char *pszGeolocArray = CSLFetchNameValue(
    2353             :             papszOptions,
    2354        7784 :             std::string(pszPrefix).append("_GEOLOC_ARRAY").c_str());
    2355        3892 :         if (!pszGeolocArray && EQUAL(pszPrefix, "SRC"))
    2356        1952 :             pszGeolocArray = CSLFetchNameValue(papszOptions, "GEOLOC_ARRAY");
    2357        3892 :         if (!pszMethod && pszGeolocArray != nullptr)
    2358           9 :             pszMethod = "GEOLOC_ARRAY";
    2359             : 
    2360             :         /* -------------------------------------------------------------------- */
    2361             :         /*      Get forward and inverse geotransform for the source image.      */
    2362             :         /* -------------------------------------------------------------------- */
    2363        3892 :         if (hDS == nullptr ||
    2364          91 :             (pszMethod != nullptr && EQUAL(pszMethod, "NO_GEOTRANSFORM")))
    2365             :         {
    2366        1566 :             part.adfGeoTransform[0] = 0.0;
    2367        1566 :             part.adfGeoTransform[1] = 1.0;
    2368        1566 :             part.adfGeoTransform[2] = 0.0;
    2369        1566 :             part.adfGeoTransform[3] = 0.0;
    2370        1566 :             part.adfGeoTransform[4] = 0.0;
    2371        1566 :             part.adfGeoTransform[5] = 1.0;
    2372        1566 :             memcpy(part.adfInvGeoTransform, part.adfGeoTransform,
    2373             :                    sizeof(double) * 6);
    2374             :         }
    2375        4585 :         else if ((pszMethod == nullptr || EQUAL(pszMethod, "GEOTRANSFORM")) &&
    2376        2259 :                  GDALGetGeoTransform(hDS, part.adfGeoTransform) == CE_None)
    2377             :         {
    2378        2179 :             if (!GDALInvGeoTransform(part.adfGeoTransform,
    2379        2179 :                                      part.adfInvGeoTransform))
    2380             :             {
    2381           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    2382             :                          "Cannot invert geotransform");
    2383           0 :                 return false;
    2384             :             }
    2385        2179 :             if (pszSRS == nullptr)
    2386             :             {
    2387        1939 :                 auto hSRS = GDALGetSpatialRef(hDS);
    2388        1939 :                 if (hSRS)
    2389        1458 :                     oSRS = *(OGRSpatialReference::FromHandle(hSRS));
    2390             :             }
    2391        2179 :             if (EQUAL(pszPrefix, "SRC"))
    2392             :             {
    2393        1562 :                 if (!bHasAreaOfInterest && pszCO == nullptr && !oSRS.IsEmpty())
    2394             :                 {
    2395        1321 :                     GDALComputeAreaOfInterest(
    2396        1321 :                         &oSRS, part.adfGeoTransform, GDALGetRasterXSize(hDS),
    2397             :                         GDALGetRasterYSize(hDS), dfWestLongitudeDeg,
    2398             :                         dfSouthLatitudeDeg, dfEastLongitudeDeg,
    2399             :                         dfNorthLatitudeDeg);
    2400             :                 }
    2401        1562 :                 bCanUseGeoTransform = true;
    2402             :             }
    2403             :         }
    2404         147 :         else if (bGCPUseOK &&
    2405          80 :                  ((pszMethod == nullptr && GDALGetGCPCount(hDS) >= 4 &&
    2406         147 :                    GDALGetGCPCount(hDS) < 6) ||
    2407          67 :                   (pszMethod != nullptr &&
    2408         295 :                    EQUAL(pszMethod, "GCP_HOMOGRAPHY"))) &&
    2409          22 :                  GDALGetGCPCount(hDS) > 0)
    2410             :         {
    2411          22 :             if (pszSRS == nullptr)
    2412             :             {
    2413          21 :                 auto hSRS = GDALGetGCPSpatialRef(hDS);
    2414          21 :                 if (hSRS)
    2415          21 :                     oSRS = *(OGRSpatialReference::FromHandle(hSRS));
    2416             :             }
    2417             : 
    2418          22 :             const auto nGCPCount = GDALGetGCPCount(hDS);
    2419          22 :             auto pasGCPList = GDALDuplicateGCPs(nGCPCount, GDALGetGCPs(hDS));
    2420          22 :             GDALGCPAntimeridianUnwrap(nGCPCount, pasGCPList, oSRS,
    2421             :                                       papszOptions);
    2422             : 
    2423          22 :             part.pTransformArg =
    2424          22 :                 GDALCreateHomographyTransformerFromGCPs(nGCPCount, pasGCPList);
    2425             : 
    2426          22 :             GDALDeinitGCPs(nGCPCount, pasGCPList);
    2427          22 :             CPLFree(pasGCPList);
    2428             : 
    2429          22 :             if (part.pTransformArg == nullptr)
    2430             :             {
    2431           0 :                 return false;
    2432             :             }
    2433          22 :             part.pTransformer = GDALHomographyTransform;
    2434             :         }
    2435         125 :         else if (bGCPUseOK &&
    2436          66 :                  (pszMethod == nullptr || EQUAL(pszMethod, "GCP_POLYNOMIAL")) &&
    2437         250 :                  GDALGetGCPCount(hDS) > 0 && nOrder >= 0)
    2438             :         {
    2439           8 :             if (pszSRS == nullptr)
    2440             :             {
    2441           8 :                 auto hSRS = GDALGetGCPSpatialRef(hDS);
    2442           8 :                 if (hSRS)
    2443           8 :                     oSRS = *(OGRSpatialReference::FromHandle(hSRS));
    2444             :             }
    2445             : 
    2446           8 :             const auto nGCPCount = GDALGetGCPCount(hDS);
    2447           8 :             auto pasGCPList = GDALDuplicateGCPs(nGCPCount, GDALGetGCPs(hDS));
    2448           8 :             GDALGCPAntimeridianUnwrap(nGCPCount, pasGCPList, oSRS,
    2449             :                                       papszOptions);
    2450             : 
    2451           8 :             if (bRefine)
    2452             :             {
    2453           0 :                 part.pTransformArg = GDALCreateGCPRefineTransformer(
    2454             :                     nGCPCount, pasGCPList, nOrder, FALSE, dfTolerance,
    2455             :                     nMinimumGcps);
    2456             :             }
    2457             :             else
    2458             :             {
    2459           8 :                 part.pTransformArg = GDALCreateGCPTransformer(
    2460             :                     nGCPCount, pasGCPList, nOrder, FALSE);
    2461             :             }
    2462             : 
    2463           8 :             GDALDeinitGCPs(nGCPCount, pasGCPList);
    2464           8 :             CPLFree(pasGCPList);
    2465             : 
    2466           8 :             if (part.pTransformArg == nullptr)
    2467             :             {
    2468           0 :                 return false;
    2469             :             }
    2470           8 :             part.pTransformer = GDALGCPTransform;
    2471             :         }
    2472             : 
    2473         128 :         else if (bGCPUseOK && GDALGetGCPCount(hDS) > 0 && nOrder <= 0 &&
    2474          11 :                  (pszMethod == nullptr || EQUAL(pszMethod, "GCP_TPS")))
    2475             :         {
    2476          11 :             if (pszSRS == nullptr)
    2477             :             {
    2478          11 :                 auto hSRS = GDALGetGCPSpatialRef(hDS);
    2479          11 :                 if (hSRS)
    2480          10 :                     oSRS = *(OGRSpatialReference::FromHandle(hSRS));
    2481             :             }
    2482             : 
    2483          11 :             const auto nGCPCount = GDALGetGCPCount(hDS);
    2484          11 :             auto pasGCPList = GDALDuplicateGCPs(nGCPCount, GDALGetGCPs(hDS));
    2485          11 :             GDALGCPAntimeridianUnwrap(nGCPCount, pasGCPList, oSRS,
    2486             :                                       papszOptions);
    2487             : 
    2488          11 :             part.pTransformArg = GDALCreateTPSTransformerInt(
    2489             :                 nGCPCount, pasGCPList, FALSE, papszOptions);
    2490             : 
    2491          11 :             GDALDeinitGCPs(nGCPCount, pasGCPList);
    2492          11 :             CPLFree(pasGCPList);
    2493             : 
    2494          11 :             if (part.pTransformArg == nullptr)
    2495             :             {
    2496           2 :                 return false;
    2497             :             }
    2498           9 :             part.pTransformer = GDALTPSTransform;
    2499             :         }
    2500             : 
    2501          52 :         else if ((pszMethod == nullptr || EQUAL(pszMethod, "RPC")) &&
    2502         204 :                  (papszMD = GDALGetMetadata(hDS, "RPC")) != nullptr &&
    2503          46 :                  GDALExtractRPCInfoV2(papszMD, &sRPCInfo))
    2504             :         {
    2505          46 :             CPLStringList aosOptions(papszOptions);
    2506          87 :             if (!CSLFetchNameValue(papszOptions, "RPC_HEIGHT") &&
    2507          41 :                 !CSLFetchNameValue(papszOptions, "RPC_DEM"))
    2508             :             {
    2509           8 :                 if (const char *pszHEIGHT_DEFAULT =
    2510           8 :                         CSLFetchNameValue(papszMD, "HEIGHT_DEFAULT"))
    2511             :                 {
    2512           1 :                     CPLDebug("GDAL",
    2513             :                              "For %s, using RPC_HEIGHT = HEIGHT_DEFAULT = %s",
    2514             :                              pszPrefix, pszHEIGHT_DEFAULT);
    2515           1 :                     aosOptions.SetNameValue("RPC_HEIGHT", pszHEIGHT_DEFAULT);
    2516             :                 }
    2517             :             }
    2518          46 :             part.pTransformArg = GDALCreateRPCTransformerV2(&sRPCInfo, FALSE, 0,
    2519          46 :                                                             aosOptions.List());
    2520          46 :             if (part.pTransformArg == nullptr)
    2521             :             {
    2522           1 :                 return false;
    2523             :             }
    2524          45 :             part.pTransformer = GDALRPCTransform;
    2525          45 :             if (pszSRS == nullptr)
    2526             :             {
    2527          45 :                 oSRS.SetFromUserInput(SRS_WKT_WGS84_LAT_LONG);
    2528          45 :                 oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2529             :             }
    2530             :         }
    2531             : 
    2532         119 :         else if ((pszMethod == nullptr || EQUAL(pszMethod, "GEOLOC_ARRAY")) &&
    2533          59 :                  ((papszMD = GDALGetMetadata(hDS, "GEOLOCATION")) != nullptr ||
    2534             :                   pszGeolocArray != nullptr))
    2535             :         {
    2536          52 :             CPLStringList aosGeolocMD;  // keep in this scope
    2537          52 :             if (pszGeolocArray != nullptr)
    2538             :             {
    2539           9 :                 if (papszMD != nullptr)
    2540             :                 {
    2541           0 :                     CPLError(
    2542             :                         CE_Warning, CPLE_AppDefined,
    2543             :                         "Both GEOLOCATION metadata domain on the source "
    2544             :                         "dataset "
    2545             :                         "and [%s_]GEOLOC_ARRAY transformer option are set. "
    2546             :                         "Only using the later.",
    2547             :                         pszPrefix);
    2548             :                 }
    2549           9 :                 aosGeolocMD = GDALCreateGeolocationMetadata(
    2550             :                     hDS, pszGeolocArray,
    2551           9 :                     /* bIsSource= */ EQUAL(pszPrefix, "SRC"));
    2552           9 :                 if (aosGeolocMD.empty())
    2553             :                 {
    2554           3 :                     return false;
    2555             :                 }
    2556           6 :                 papszMD = aosGeolocMD.List();
    2557             :             }
    2558             : 
    2559          49 :             part.pTransformArg = GDALCreateGeoLocTransformerEx(
    2560             :                 hDS, papszMD, FALSE, nullptr, papszOptions);
    2561          49 :             if (part.pTransformArg == nullptr)
    2562             :             {
    2563           2 :                 return false;
    2564             :             }
    2565          47 :             part.pTransformer = GDALGeoLocTransform;
    2566          47 :             if (pszSRS == nullptr)
    2567             :             {
    2568          47 :                 pszSRS = CSLFetchNameValue(papszMD, "SRS");
    2569          47 :                 if (pszSRS)
    2570             :                 {
    2571          45 :                     oSRS.SetFromUserInput(pszSRS);
    2572          45 :                     oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2573             :                 }
    2574             :             }
    2575             :         }
    2576             : 
    2577           8 :         else if (pszMethod != nullptr && EQUAL(pszPrefix, "SRC"))
    2578             :         {
    2579           1 :             CPLError(CE_Failure, CPLE_AppDefined,
    2580             :                      "Unable to compute a %s based transformation between "
    2581             :                      "pixel/line and georeferenced coordinates for %s.",
    2582             :                      pszMethod, GDALGetDescription(hDS));
    2583             : 
    2584           1 :             return false;
    2585             :         }
    2586             : 
    2587             :         else
    2588             :         {
    2589           7 :             CPLError(CE_Failure, CPLE_AppDefined,
    2590             :                      "Unable to compute a transformation between pixel/line "
    2591             :                      "and georeferenced coordinates for %s. "
    2592             :                      "There is no affine transformation and no GCPs. "
    2593             :                      "Specify transformation option %s_METHOD=NO_GEOTRANSFORM "
    2594             :                      "to bypass this check.",
    2595             :                      GDALGetDescription(hDS), pszPrefix);
    2596             : 
    2597           7 :             return false;
    2598             :         }
    2599             : 
    2600             :         /* ---------------------------------------------------------------- */
    2601             :         /*      Handle optional source approximation transformer.           */
    2602             :         /* ---------------------------------------------------------------- */
    2603        3876 :         if (part.pTransformer)
    2604             :         {
    2605         131 :             const char *pszApproxErrorFwd = CSLFetchNameValue(
    2606         262 :                 papszOptions, std::string(pszPrefix)
    2607         131 :                                   .append("_APPROX_ERROR_IN_SRS_UNIT")
    2608             :                                   .c_str());
    2609         131 :             const char *pszApproxErrorReverse = CSLFetchNameValue(
    2610         262 :                 papszOptions, std::string(pszPrefix)
    2611         131 :                                   .append("_APPROX_ERROR_IN_PIXEL")
    2612             :                                   .c_str());
    2613         131 :             if (pszApproxErrorFwd && pszApproxErrorReverse)
    2614             :             {
    2615           1 :                 void *pArg = GDALCreateApproxTransformer2(
    2616             :                     part.pTransformer, part.pTransformArg,
    2617             :                     CPLAtof(pszApproxErrorFwd), CPLAtof(pszApproxErrorReverse));
    2618           1 :                 if (pArg == nullptr)
    2619             :                 {
    2620           0 :                     return false;
    2621             :                 }
    2622           1 :                 part.pTransformArg = pArg;
    2623           1 :                 part.pTransformer = GDALApproxTransform;
    2624           1 :                 GDALApproxTransformerOwnsSubtransformer(part.pTransformArg,
    2625             :                                                         TRUE);
    2626             :             }
    2627             :         }
    2628             : 
    2629        3876 :         return true;
    2630        1953 :     };
    2631             : 
    2632             :     /* -------------------------------------------------------------------- */
    2633             :     /*      Get forward and inverse geotransform for the source image.      */
    2634             :     /* -------------------------------------------------------------------- */
    2635        1953 :     bool bCanUseSrcGeoTransform = false;
    2636        3906 :     OGRSpatialReference oSrcSRS;
    2637        1953 :     if (!DealWithForwardOrInverse(psInfo->sSrcParams, hSrcDS, "SRC", oSrcSRS,
    2638             :                                   bCanUseSrcGeoTransform))
    2639             :     {
    2640          14 :         GDALDestroyGenImgProjTransformer(psInfo);
    2641          14 :         return nullptr;
    2642             :     }
    2643             : 
    2644             :     /* -------------------------------------------------------------------- */
    2645             :     /*      Get forward and inverse geotransform for destination image.     */
    2646             :     /*      If we have no destination use a unit transform.                 */
    2647             :     /* -------------------------------------------------------------------- */
    2648        1939 :     bool bIgnored = false;
    2649        3878 :     OGRSpatialReference oDstSRS;
    2650        1939 :     if (!DealWithForwardOrInverse(psInfo->sDstParams, hDstDS, "DST", oDstSRS,
    2651             :                                   bIgnored))
    2652             :     {
    2653           2 :         GDALDestroyGenImgProjTransformer(psInfo);
    2654           2 :         return nullptr;
    2655             :     }
    2656             : 
    2657             :     /* -------------------------------------------------------------------- */
    2658             :     /*      Setup reprojection.                                             */
    2659             :     /* -------------------------------------------------------------------- */
    2660             : 
    2661        1937 :     if (CPLFetchBool(papszOptions, "@STRIP_VERT_CS", false))
    2662             :     {
    2663           1 :         if (oSrcSRS.IsCompound())
    2664             :         {
    2665           1 :             oSrcSRS.StripVertical();
    2666             :         }
    2667           1 :         if (oDstSRS.IsCompound())
    2668             :         {
    2669           0 :             oDstSRS.StripVertical();
    2670             :         }
    2671             :     }
    2672             : 
    2673             :     const bool bMayInsertCenterLong =
    2674        3260 :         (bCanUseSrcGeoTransform && !oSrcSRS.IsEmpty() && hSrcDS &&
    2675        1323 :          CPLFetchBool(papszOptions, "INSERT_CENTER_LONG", true));
    2676             :     const char *pszSrcCoordEpoch =
    2677        1937 :         CSLFetchNameValue(papszOptions, "SRC_COORDINATE_EPOCH");
    2678             :     const char *pszDstCoordEpoch =
    2679        1937 :         CSLFetchNameValue(papszOptions, "DST_COORDINATE_EPOCH");
    2680        3425 :     if ((!oSrcSRS.IsEmpty() && !oDstSRS.IsEmpty() &&
    2681        1374 :          (pszSrcCoordEpoch || pszDstCoordEpoch || !oSrcSRS.IsSame(&oDstSRS) ||
    2682        3425 :           (oSrcSRS.IsGeographic() && bMayInsertCenterLong))) ||
    2683             :         pszCO)
    2684             :     {
    2685         991 :         CPLStringList aosOptions;
    2686             : 
    2687         991 :         if (bMayInsertCenterLong)
    2688             :         {
    2689         974 :             InsertCenterLong(hSrcDS, &oSrcSRS, &oDstSRS,
    2690             :                              CSLFetchNameValue(papszOptions, "TARGET_EXTENT"),
    2691             :                              aosOptions);
    2692             :         }
    2693             : 
    2694         991 :         if (CPLFetchBool(papszOptions, "PROMOTE_TO_3D", false))
    2695             :         {
    2696          19 :             oSrcSRS.PromoteTo3D(nullptr);
    2697          19 :             oDstSRS.PromoteTo3D(nullptr);
    2698             :         }
    2699             : 
    2700         991 :         if (!(dfWestLongitudeDeg == 0.0 && dfSouthLatitudeDeg == 0.0 &&
    2701          32 :               dfEastLongitudeDeg == 0.0 && dfNorthLatitudeDeg == 0.0))
    2702             :         {
    2703             :             aosOptions.SetNameValue(
    2704             :                 "AREA_OF_INTEREST",
    2705             :                 CPLSPrintf("%.16g,%.16g,%.16g,%.16g", dfWestLongitudeDeg,
    2706             :                            dfSouthLatitudeDeg, dfEastLongitudeDeg,
    2707         959 :                            dfNorthLatitudeDeg));
    2708             :         }
    2709         991 :         if (pszCO)
    2710             :         {
    2711           7 :             aosOptions.SetNameValue("COORDINATE_OPERATION", pszCO);
    2712             :         }
    2713             : 
    2714             :         const char *pszCoordEpoch =
    2715         991 :             CSLFetchNameValue(papszOptions, "COORDINATE_EPOCH");
    2716         991 :         if (pszCoordEpoch)
    2717             :         {
    2718           1 :             aosOptions.SetNameValue("COORDINATE_EPOCH", pszCoordEpoch);
    2719             :         }
    2720             : 
    2721         991 :         if (pszSrcCoordEpoch)
    2722             :         {
    2723           0 :             aosOptions.SetNameValue("SRC_COORDINATE_EPOCH", pszSrcCoordEpoch);
    2724           0 :             oSrcSRS.SetCoordinateEpoch(CPLAtof(pszSrcCoordEpoch));
    2725             :         }
    2726             : 
    2727         991 :         if (pszDstCoordEpoch)
    2728             :         {
    2729           0 :             aosOptions.SetNameValue("DST_COORDINATE_EPOCH", pszDstCoordEpoch);
    2730           0 :             oDstSRS.SetCoordinateEpoch(CPLAtof(pszDstCoordEpoch));
    2731             :         }
    2732             : 
    2733         991 :         if (const char *pszAllowBallpark =
    2734         991 :                 CSLFetchNameValue(papszOptions, "ALLOW_BALLPARK"))
    2735             :         {
    2736           2 :             aosOptions.SetNameValue("ALLOW_BALLPARK", pszAllowBallpark);
    2737             :         }
    2738             : 
    2739         991 :         if (const char *pszOnlyBest =
    2740         991 :                 CSLFetchNameValue(papszOptions, "ONLY_BEST"))
    2741             :         {
    2742           1 :             aosOptions.SetNameValue("ONLY_BEST", pszOnlyBest);
    2743             :         }
    2744             : 
    2745         995 :         psInfo->pReprojectArg = GDALCreateReprojectionTransformerEx(
    2746         991 :             !oSrcSRS.IsEmpty() ? OGRSpatialReference::ToHandle(&oSrcSRS)
    2747             :                                : nullptr,
    2748         991 :             !oDstSRS.IsEmpty() ? OGRSpatialReference::ToHandle(&oDstSRS)
    2749             :                                : nullptr,
    2750         991 :             aosOptions.List());
    2751             : 
    2752         991 :         if (pszCO)
    2753             :         {
    2754           7 :             psInfo->bHasCustomTransformationPipeline = true;
    2755             :         }
    2756             : 
    2757         991 :         if (psInfo->pReprojectArg == nullptr)
    2758             :         {
    2759           5 :             GDALDestroyGenImgProjTransformer(psInfo);
    2760           5 :             return nullptr;
    2761             :         }
    2762         986 :         psInfo->pReproject = GDALReprojectionTransform;
    2763             : 
    2764             :         /* --------------------------------------------------------------------
    2765             :          */
    2766             :         /*      Handle optional reprojection approximation transformer. */
    2767             :         /* --------------------------------------------------------------------
    2768             :          */
    2769         986 :         const char *psApproxErrorFwd = CSLFetchNameValue(
    2770             :             papszOptions, "REPROJECTION_APPROX_ERROR_IN_DST_SRS_UNIT");
    2771         986 :         const char *psApproxErrorReverse = CSLFetchNameValue(
    2772             :             papszOptions, "REPROJECTION_APPROX_ERROR_IN_SRC_SRS_UNIT");
    2773         986 :         if (psApproxErrorFwd && psApproxErrorReverse)
    2774             :         {
    2775           1 :             void *pArg = GDALCreateApproxTransformer2(
    2776             :                 psInfo->pReproject, psInfo->pReprojectArg,
    2777             :                 CPLAtof(psApproxErrorFwd), CPLAtof(psApproxErrorReverse));
    2778           1 :             if (pArg == nullptr)
    2779             :             {
    2780           0 :                 GDALDestroyGenImgProjTransformer(psInfo);
    2781           0 :                 return nullptr;
    2782             :             }
    2783           1 :             psInfo->pReprojectArg = pArg;
    2784           1 :             psInfo->pReproject = GDALApproxTransform;
    2785           1 :             GDALApproxTransformerOwnsSubtransformer(psInfo->pReprojectArg,
    2786             :                                                     TRUE);
    2787             :         }
    2788             :     }
    2789             : 
    2790        1932 :     return psInfo;
    2791             : }
    2792             : 
    2793             : /************************************************************************/
    2794             : /*                  GDALRefreshGenImgProjTransformer()                  */
    2795             : /************************************************************************/
    2796             : 
    2797        1197 : void GDALRefreshGenImgProjTransformer(void *hTransformArg)
    2798             : {
    2799        1197 :     GDALGenImgProjTransformInfo *psInfo =
    2800             :         static_cast<GDALGenImgProjTransformInfo *>(hTransformArg);
    2801             : 
    2802        2006 :     if (psInfo->pReprojectArg &&
    2803         809 :         psInfo->bCheckWithInvertPROJ != GetCurrentCheckWithInvertPROJ())
    2804             :     {
    2805          69 :         psInfo->bCheckWithInvertPROJ = !psInfo->bCheckWithInvertPROJ;
    2806             : 
    2807             :         CPLXMLNode *psXML =
    2808          69 :             GDALSerializeTransformer(psInfo->pReproject, psInfo->pReprojectArg);
    2809          69 :         GDALDestroyTransformer(psInfo->pReprojectArg);
    2810          69 :         GDALDeserializeTransformer(psXML, &psInfo->pReproject,
    2811             :                                    &psInfo->pReprojectArg);
    2812          69 :         CPLDestroyXMLNode(psXML);
    2813             :     }
    2814        1197 : }
    2815             : 
    2816             : /************************************************************************/
    2817             : /*                  GDALCreateGenImgProjTransformer3()                  */
    2818             : /************************************************************************/
    2819             : 
    2820             : /**
    2821             :  * Create image to image transformer.
    2822             :  *
    2823             :  * This function creates a transformation object that maps from pixel/line
    2824             :  * coordinates on one image to pixel/line coordinates on another image.  The
    2825             :  * images may potentially be georeferenced in different coordinate systems,
    2826             :  * and may used GCPs to map between their pixel/line coordinates and
    2827             :  * georeferenced coordinates (as opposed to the default assumption that their
    2828             :  * geotransform should be used).
    2829             :  *
    2830             :  * This transformer potentially performs three concatenated transformations.
    2831             :  *
    2832             :  * The first stage is from source image pixel/line coordinates to source
    2833             :  * image georeferenced coordinates, and may be done using the geotransform,
    2834             :  * or if not defined using a polynomial model derived from GCPs.  If GCPs
    2835             :  * are used this stage is accomplished using GDALGCPTransform().
    2836             :  *
    2837             :  * The second stage is to change projections from the source coordinate system
    2838             :  * to the destination coordinate system, assuming they differ.  This is
    2839             :  * accomplished internally using GDALReprojectionTransform().
    2840             :  *
    2841             :  * The third stage is converting from destination image georeferenced
    2842             :  * coordinates to destination image coordinates.  This is done using the
    2843             :  * destination image geotransform, or if not available, using a polynomial
    2844             :  * model derived from GCPs. If GCPs are used this stage is accomplished using
    2845             :  * GDALGCPTransform().  This stage is skipped if hDstDS is NULL when the
    2846             :  * transformation is created.
    2847             :  *
    2848             :  * @param pszSrcWKT source WKT (or NULL).
    2849             :  * @param padfSrcGeoTransform source geotransform (or NULL).
    2850             :  * @param pszDstWKT destination WKT (or NULL).
    2851             :  * @param padfDstGeoTransform destination geotransform (or NULL).
    2852             :  *
    2853             :  * @return handle suitable for use GDALGenImgProjTransform(), and to be
    2854             :  * deallocated with GDALDestroyGenImgProjTransformer() or NULL on failure.
    2855             :  */
    2856             : 
    2857           0 : void *GDALCreateGenImgProjTransformer3(const char *pszSrcWKT,
    2858             :                                        const double *padfSrcGeoTransform,
    2859             :                                        const char *pszDstWKT,
    2860             :                                        const double *padfDstGeoTransform)
    2861             : 
    2862             : {
    2863           0 :     OGRSpatialReference oSrcSRS;
    2864           0 :     if (pszSrcWKT)
    2865             :     {
    2866           0 :         oSrcSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2867           0 :         if (pszSrcWKT[0] != '\0' &&
    2868           0 :             oSrcSRS.importFromWkt(pszSrcWKT) != OGRERR_NONE)
    2869             :         {
    2870           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2871             :                      "Failed to import coordinate system `%s'.", pszSrcWKT);
    2872           0 :             return nullptr;
    2873             :         }
    2874             :     }
    2875             : 
    2876           0 :     OGRSpatialReference oDstSRS;
    2877           0 :     if (pszDstWKT)
    2878             :     {
    2879           0 :         oDstSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    2880           0 :         if (pszDstWKT[0] != '\0' &&
    2881           0 :             oDstSRS.importFromWkt(pszDstWKT) != OGRERR_NONE)
    2882             :         {
    2883           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    2884             :                      "Failed to import coordinate system `%s'.", pszDstWKT);
    2885           0 :             return nullptr;
    2886             :         }
    2887             :     }
    2888           0 :     return GDALCreateGenImgProjTransformer4(
    2889             :         OGRSpatialReference::ToHandle(&oSrcSRS), padfSrcGeoTransform,
    2890           0 :         OGRSpatialReference::ToHandle(&oDstSRS), padfDstGeoTransform, nullptr);
    2891             : }
    2892             : 
    2893             : /************************************************************************/
    2894             : /*                  GDALCreateGenImgProjTransformer4()                  */
    2895             : /************************************************************************/
    2896             : 
    2897             : /**
    2898             :  * Create image to image transformer.
    2899             :  *
    2900             :  * Similar to GDALCreateGenImgProjTransformer3(), except that it takes
    2901             :  * OGRSpatialReferenceH objects and options.
    2902             :  * The options are the ones supported by GDALCreateReprojectionTransformerEx()
    2903             :  *
    2904             :  * @since GDAL 3.0
    2905             :  */
    2906          16 : void *GDALCreateGenImgProjTransformer4(OGRSpatialReferenceH hSrcSRS,
    2907             :                                        const double *padfSrcGeoTransform,
    2908             :                                        OGRSpatialReferenceH hDstSRS,
    2909             :                                        const double *padfDstGeoTransform,
    2910             :                                        const char *const *papszOptions)
    2911             : {
    2912             :     /* -------------------------------------------------------------------- */
    2913             :     /*      Initialize the transform info.                                  */
    2914             :     /* -------------------------------------------------------------------- */
    2915             :     GDALGenImgProjTransformInfo *psInfo =
    2916          16 :         GDALCreateGenImgProjTransformerInternal();
    2917             : 
    2918             :     /* -------------------------------------------------------------------- */
    2919             :     /*      Get forward and inverse geotransform for the source image.      */
    2920             :     /* -------------------------------------------------------------------- */
    2921             : 
    2922             :     const auto SetParams =
    2923          30 :         [](GDALGenImgProjTransformPart &part, const double *padfGT)
    2924             :     {
    2925          30 :         if (padfGT)
    2926             :         {
    2927          30 :             memcpy(part.adfGeoTransform, padfGT, sizeof(part.adfGeoTransform));
    2928          30 :             if (!GDALInvGeoTransform(part.adfGeoTransform,
    2929          30 :                                      part.adfInvGeoTransform))
    2930             :             {
    2931           2 :                 CPLError(CE_Failure, CPLE_AppDefined,
    2932             :                          "Cannot invert geotransform");
    2933           2 :                 return false;
    2934             :             }
    2935             :         }
    2936             :         else
    2937             :         {
    2938           0 :             part.adfGeoTransform[0] = 0.0;
    2939           0 :             part.adfGeoTransform[1] = 1.0;
    2940           0 :             part.adfGeoTransform[2] = 0.0;
    2941           0 :             part.adfGeoTransform[3] = 0.0;
    2942           0 :             part.adfGeoTransform[4] = 0.0;
    2943           0 :             part.adfGeoTransform[5] = 1.0;
    2944           0 :             memcpy(part.adfInvGeoTransform, part.adfGeoTransform,
    2945             :                    sizeof(double) * 6);
    2946             :         }
    2947          28 :         return true;
    2948             :     };
    2949             : 
    2950          16 :     if (!SetParams(psInfo->sSrcParams, padfSrcGeoTransform))
    2951             :     {
    2952           1 :         GDALDestroyGenImgProjTransformer(psInfo);
    2953           1 :         return nullptr;
    2954             :     }
    2955             : 
    2956             :     /* -------------------------------------------------------------------- */
    2957             :     /*      Setup reprojection.                                             */
    2958             :     /* -------------------------------------------------------------------- */
    2959          15 :     OGRSpatialReference *poSrcSRS = OGRSpatialReference::FromHandle(hSrcSRS);
    2960          15 :     OGRSpatialReference *poDstSRS = OGRSpatialReference::FromHandle(hDstSRS);
    2961          30 :     if (!poSrcSRS->IsEmpty() && !poDstSRS->IsEmpty() &&
    2962          15 :         !poSrcSRS->IsSame(poDstSRS))
    2963             :     {
    2964           4 :         psInfo->pReprojectArg =
    2965           4 :             GDALCreateReprojectionTransformerEx(hSrcSRS, hDstSRS, papszOptions);
    2966           4 :         if (psInfo->pReprojectArg == nullptr)
    2967             :         {
    2968           1 :             GDALDestroyGenImgProjTransformer(psInfo);
    2969           1 :             return nullptr;
    2970             :         }
    2971           3 :         psInfo->pReproject = GDALReprojectionTransform;
    2972             :     }
    2973             : 
    2974             :     /* -------------------------------------------------------------------- */
    2975             :     /*      Get forward and inverse geotransform for destination image.     */
    2976             :     /*      If we have no destination matrix use a unit transform.          */
    2977             :     /* -------------------------------------------------------------------- */
    2978          14 :     if (!SetParams(psInfo->sDstParams, padfDstGeoTransform))
    2979             :     {
    2980           1 :         GDALDestroyGenImgProjTransformer(psInfo);
    2981           1 :         return nullptr;
    2982             :     }
    2983             : 
    2984          13 :     return psInfo;
    2985             : }
    2986             : 
    2987             : /************************************************************************/
    2988             : /*            GDALSetGenImgProjTransformerDstGeoTransform()             */
    2989             : /************************************************************************/
    2990             : 
    2991             : /**
    2992             :  * Set GenImgProj output geotransform.
    2993             :  *
    2994             :  * Normally the "destination geotransform", or transformation between
    2995             :  * georeferenced output coordinates and pixel/line coordinates on the
    2996             :  * destination file is extracted from the destination file by
    2997             :  * GDALCreateGenImgProjTransformer() and stored in the GenImgProj private
    2998             :  * info.  However, sometimes it is inconvenient to have an output file
    2999             :  * handle with appropriate geotransform information when creating the
    3000             :  * transformation.  For these cases, this function can be used to apply
    3001             :  * the destination geotransform.
    3002             :  *
    3003             :  * @param hTransformArg the handle to update.
    3004             :  * @param padfGeoTransform the destination geotransform to apply (six doubles).
    3005             :  */
    3006             : 
    3007         920 : void GDALSetGenImgProjTransformerDstGeoTransform(void *hTransformArg,
    3008             :                                                  const double *padfGeoTransform)
    3009             : 
    3010             : {
    3011         920 :     VALIDATE_POINTER0(hTransformArg,
    3012             :                       "GDALSetGenImgProjTransformerDstGeoTransform");
    3013             : 
    3014         920 :     GDALGenImgProjTransformInfo *psInfo =
    3015             :         static_cast<GDALGenImgProjTransformInfo *>(hTransformArg);
    3016             : 
    3017         920 :     memcpy(psInfo->sDstParams.adfGeoTransform, padfGeoTransform,
    3018             :            sizeof(double) * 6);
    3019         920 :     if (!GDALInvGeoTransform(psInfo->sDstParams.adfGeoTransform,
    3020         920 :                              psInfo->sDstParams.adfInvGeoTransform))
    3021             :     {
    3022           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
    3023             :     }
    3024             : }
    3025             : 
    3026             : /************************************************************************/
    3027             : /*                  GDALDestroyGenImgProjTransformer()                  */
    3028             : /************************************************************************/
    3029             : 
    3030             : /**
    3031             :  * GenImgProjTransformer deallocator.
    3032             :  *
    3033             :  * This function is used to deallocate the handle created with
    3034             :  * GDALCreateGenImgProjTransformer().
    3035             :  *
    3036             :  * @param hTransformArg the handle to deallocate.
    3037             :  */
    3038             : 
    3039        2224 : void GDALDestroyGenImgProjTransformer(void *hTransformArg)
    3040             : 
    3041             : {
    3042        2224 :     if (hTransformArg == nullptr)
    3043           0 :         return;
    3044             : 
    3045        2224 :     GDALGenImgProjTransformInfo *psInfo =
    3046             :         static_cast<GDALGenImgProjTransformInfo *>(hTransformArg);
    3047             : 
    3048        2224 :     if (psInfo->sSrcParams.pTransformArg != nullptr)
    3049         147 :         GDALDestroyTransformer(psInfo->sSrcParams.pTransformArg);
    3050             : 
    3051        2224 :     if (psInfo->sDstParams.pTransformArg != nullptr)
    3052           7 :         GDALDestroyTransformer(psInfo->sDstParams.pTransformArg);
    3053             : 
    3054        2224 :     if (psInfo->pReprojectArg != nullptr)
    3055        1122 :         GDALDestroyTransformer(psInfo->pReprojectArg);
    3056             : 
    3057        2224 :     CPLFree(psInfo);
    3058             : }
    3059             : 
    3060             : /************************************************************************/
    3061             : /*                      GDALGenImgProjTransform()                       */
    3062             : /************************************************************************/
    3063             : 
    3064             : /**
    3065             :  * Perform general image reprojection transformation.
    3066             :  *
    3067             :  * Actually performs the transformation setup in
    3068             :  * GDALCreateGenImgProjTransformer().  This function matches the signature
    3069             :  * required by the GDALTransformerFunc(), and more details on the arguments
    3070             :  * can be found in that topic.
    3071             :  */
    3072             : 
    3073             : #ifdef DEBUG_APPROX_TRANSFORMER
    3074             : int countGDALGenImgProjTransform = 0;
    3075             : #endif
    3076             : 
    3077     2280350 : int GDALGenImgProjTransform(void *pTransformArgIn, int bDstToSrc,
    3078             :                             int nPointCount, double *padfX, double *padfY,
    3079             :                             double *padfZ, int *panSuccess)
    3080             : {
    3081             :     // Sanity check (see issue GH #13498)
    3082     2280350 :     if (nullptr == pTransformArgIn)
    3083           0 :         return FALSE;
    3084             : 
    3085     2280350 :     GDALGenImgProjTransformInfo *psInfo =
    3086             :         static_cast<GDALGenImgProjTransformInfo *>(pTransformArgIn);
    3087             : 
    3088             : #ifdef DEBUG_APPROX_TRANSFORMER
    3089             :     CPLAssert(nPointCount > 0);
    3090             :     countGDALGenImgProjTransform += nPointCount;
    3091             : #endif
    3092             : 
    3093    20465700 :     for (int i = 0; i < nPointCount; i++)
    3094             :     {
    3095    18185400 :         panSuccess[i] = (padfX[i] != HUGE_VAL && padfY[i] != HUGE_VAL);
    3096             :     }
    3097             : 
    3098     2280350 :     int ret = TRUE;
    3099             : 
    3100             :     /* -------------------------------------------------------------------- */
    3101             :     /*      Convert from src (dst) pixel/line to src (dst)                  */
    3102             :     /*      georeferenced coordinates.                                      */
    3103             :     /* -------------------------------------------------------------------- */
    3104             :     {
    3105     2280350 :         const auto params = bDstToSrc ? psInfo->sDstParams : psInfo->sSrcParams;
    3106     2280350 :         const double *padfGeoTransform = params.adfGeoTransform;
    3107     2280350 :         void *pTransformArg = params.pTransformArg;
    3108     2280350 :         GDALTransformerFunc pTransformer = params.pTransformer;
    3109             : 
    3110     2280350 :         if (pTransformArg != nullptr)
    3111             :         {
    3112       41619 :             if (!pTransformer(pTransformArg, FALSE, nPointCount, padfX, padfY,
    3113             :                               padfZ, panSuccess))
    3114        1773 :                 ret = FALSE;
    3115             :         }
    3116             :         else
    3117             :         {
    3118    20344300 :             for (int i = 0; i < nPointCount; i++)
    3119             :             {
    3120    18105600 :                 if (!panSuccess[i])
    3121        2071 :                     continue;
    3122             : 
    3123    18103500 :                 const double dfNewX = padfGeoTransform[0] +
    3124    18103500 :                                       padfX[i] * padfGeoTransform[1] +
    3125    18103500 :                                       padfY[i] * padfGeoTransform[2];
    3126    18103500 :                 const double dfNewY = padfGeoTransform[3] +
    3127    18103500 :                                       padfX[i] * padfGeoTransform[4] +
    3128    18103500 :                                       padfY[i] * padfGeoTransform[5];
    3129             : 
    3130    18103500 :                 padfX[i] = dfNewX;
    3131    18103500 :                 padfY[i] = dfNewY;
    3132             :             }
    3133             :         }
    3134             :     }
    3135             : 
    3136             :     /* -------------------------------------------------------------------- */
    3137             :     /*      Reproject if needed.                                            */
    3138             :     /* -------------------------------------------------------------------- */
    3139     2280350 :     if (psInfo->pReprojectArg)
    3140             :     {
    3141     1904570 :         if (!psInfo->pReproject(psInfo->pReprojectArg, bDstToSrc, nPointCount,
    3142             :                                 padfX, padfY, padfZ, panSuccess))
    3143       20335 :             ret = FALSE;
    3144             :     }
    3145             : 
    3146             :     /* -------------------------------------------------------------------- */
    3147             :     /*      Convert dst (src) georef coordinates back to pixel/line.        */
    3148             :     /* -------------------------------------------------------------------- */
    3149             :     {
    3150     2280350 :         const auto params = bDstToSrc ? psInfo->sSrcParams : psInfo->sDstParams;
    3151     2280350 :         const double *padfInvGeoTransform = params.adfInvGeoTransform;
    3152     2280350 :         void *pTransformArg = params.pTransformArg;
    3153     2280350 :         GDALTransformerFunc pTransformer = params.pTransformer;
    3154             : 
    3155     2280350 :         if (pTransformArg != nullptr)
    3156             :         {
    3157       51490 :             if (!pTransformer(pTransformArg, TRUE, nPointCount, padfX, padfY,
    3158             :                               padfZ, panSuccess))
    3159        1648 :                 ret = FALSE;
    3160             :         }
    3161             :         else
    3162             :         {
    3163    19560200 :             for (int i = 0; i < nPointCount; i++)
    3164             :             {
    3165    17331300 :                 if (!panSuccess[i])
    3166     3559410 :                     continue;
    3167             : 
    3168    13771900 :                 const double dfNewX = padfInvGeoTransform[0] +
    3169    13771900 :                                       padfX[i] * padfInvGeoTransform[1] +
    3170    13771900 :                                       padfY[i] * padfInvGeoTransform[2];
    3171    13771900 :                 const double dfNewY = padfInvGeoTransform[3] +
    3172    13771900 :                                       padfX[i] * padfInvGeoTransform[4] +
    3173    13771900 :                                       padfY[i] * padfInvGeoTransform[5];
    3174             : 
    3175    13771900 :                 padfX[i] = dfNewX;
    3176    13771900 :                 padfY[i] = dfNewY;
    3177             :             }
    3178             :         }
    3179             :     }
    3180             : 
    3181     2280350 :     return ret;
    3182             : }
    3183             : 
    3184             : /************************************************************************/
    3185             : /*              GDALTransformLonLatToDestGenImgProjTransformer()        */
    3186             : /************************************************************************/
    3187             : 
    3188        3002 : int GDALTransformLonLatToDestGenImgProjTransformer(void *hTransformArg,
    3189             :                                                    double *pdfX, double *pdfY)
    3190             : {
    3191        3002 :     GDALGenImgProjTransformInfo *psInfo =
    3192             :         static_cast<GDALGenImgProjTransformInfo *>(hTransformArg);
    3193             : 
    3194        3002 :     if (psInfo->pReprojectArg == nullptr ||
    3195        1724 :         psInfo->pReproject != GDALReprojectionTransform)
    3196        1282 :         return false;
    3197             : 
    3198        1720 :     GDALReprojectionTransformInfo *psReprojInfo =
    3199             :         static_cast<GDALReprojectionTransformInfo *>(psInfo->pReprojectArg);
    3200        3440 :     if (psReprojInfo->poForwardTransform == nullptr ||
    3201        1720 :         psReprojInfo->poForwardTransform->GetSourceCS() == nullptr)
    3202           2 :         return false;
    3203             : 
    3204        1718 :     double z = 0;
    3205        1718 :     int success = true;
    3206        1718 :     auto poSourceCRS = psReprojInfo->poForwardTransform->GetSourceCS();
    3207        3044 :     if (poSourceCRS->IsGeographic() &&
    3208        1326 :         std::fabs(poSourceCRS->GetAngularUnits() -
    3209        1326 :                   CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9)
    3210             :     {
    3211             :         // Optimization to avoid creating a OGRCoordinateTransformation
    3212        1324 :         OGRAxisOrientation eSourceFirstAxisOrient = OAO_Other;
    3213        1324 :         poSourceCRS->GetAxis(nullptr, 0, &eSourceFirstAxisOrient);
    3214        1324 :         const auto &mapping = poSourceCRS->GetDataAxisToSRSAxisMapping();
    3215        2648 :         if ((mapping[0] == 2 && eSourceFirstAxisOrient == OAO_East) ||
    3216        1324 :             (mapping[0] == 1 && eSourceFirstAxisOrient != OAO_East))
    3217             :         {
    3218           6 :             std::swap(*pdfX, *pdfY);
    3219             :         }
    3220             :     }
    3221             :     else
    3222             :     {
    3223             :         auto poLongLat =
    3224         394 :             std::unique_ptr<OGRSpatialReference>(poSourceCRS->CloneGeogCS());
    3225         394 :         if (poLongLat == nullptr)
    3226           0 :             return false;
    3227         394 :         poLongLat->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3228             : 
    3229             :         const bool bCurrentCheckWithInvertProj =
    3230         394 :             GetCurrentCheckWithInvertPROJ();
    3231         394 :         if (!bCurrentCheckWithInvertProj)
    3232         394 :             CPLSetThreadLocalConfigOption("CHECK_WITH_INVERT_PROJ", "YES");
    3233             :         auto poCT = std::unique_ptr<OGRCoordinateTransformation>(
    3234         394 :             OGRCreateCoordinateTransformation(poLongLat.get(), poSourceCRS));
    3235         394 :         if (!bCurrentCheckWithInvertProj)
    3236         394 :             CPLSetThreadLocalConfigOption("CHECK_WITH_INVERT_PROJ", nullptr);
    3237         394 :         if (poCT == nullptr)
    3238           0 :             return false;
    3239             : 
    3240         394 :         poCT->SetEmitErrors(false);
    3241         394 :         if (!poCT->Transform(1, pdfX, pdfY))
    3242           4 :             return false;
    3243             : 
    3244         390 :         if (!psInfo->pReproject(psInfo->pReprojectArg, false, 1, pdfX, pdfY, &z,
    3245         636 :                                 &success) ||
    3246         246 :             !success)
    3247             :         {
    3248         144 :             return false;
    3249             :         }
    3250             :     }
    3251             : 
    3252        1570 :     double *padfGeoTransform = psInfo->sDstParams.adfInvGeoTransform;
    3253        1570 :     void *pTransformArg = psInfo->sDstParams.pTransformArg;
    3254        1570 :     GDALTransformerFunc pTransformer = psInfo->sDstParams.pTransformer;
    3255        1570 :     if (pTransformArg != nullptr)
    3256             :     {
    3257           4 :         if (!pTransformer(pTransformArg, TRUE, 1, pdfX, pdfY, &z, &success) ||
    3258           0 :             !success)
    3259             :         {
    3260           4 :             return false;
    3261             :         }
    3262             :     }
    3263             :     else
    3264             :     {
    3265        1566 :         const double dfNewX = padfGeoTransform[0] +
    3266        1566 :                               pdfX[0] * padfGeoTransform[1] +
    3267        1566 :                               pdfY[0] * padfGeoTransform[2];
    3268        1566 :         const double dfNewY = padfGeoTransform[3] +
    3269        1566 :                               pdfX[0] * padfGeoTransform[4] +
    3270        1566 :                               pdfY[0] * padfGeoTransform[5];
    3271             : 
    3272        1566 :         pdfX[0] = dfNewX;
    3273        1566 :         pdfY[0] = dfNewY;
    3274             :     }
    3275             : 
    3276        1566 :     return true;
    3277             : }
    3278             : 
    3279             : /************************************************************************/
    3280             : /*                 GDALSerializeGenImgProjTransformer()                 */
    3281             : /************************************************************************/
    3282             : 
    3283          78 : static CPLXMLNode *GDALSerializeGenImgProjTransformer(void *pTransformArg)
    3284             : 
    3285             : {
    3286          78 :     GDALGenImgProjTransformInfo *psInfo =
    3287             :         static_cast<GDALGenImgProjTransformInfo *>(pTransformArg);
    3288             : 
    3289             :     CPLXMLNode *psTree =
    3290          78 :         CPLCreateXMLNode(nullptr, CXT_Element, "GenImgProjTransformer");
    3291             : 
    3292             :     const auto SerializePart =
    3293         455 :         [psTree](const char *pszPrefix, const GDALGenImgProjTransformPart &part)
    3294             :     {
    3295         156 :         char szWork[200] = {};
    3296             : 
    3297             :         /* ------------------------------------------------------------- */
    3298             :         /*      Handle transformation.                                   */
    3299             :         /* ------------------------------------------------------------- */
    3300         156 :         if (part.pTransformArg != nullptr)
    3301             :         {
    3302             :             CPLXMLNode *psTransformer =
    3303          13 :                 GDALSerializeTransformer(part.pTransformer, part.pTransformArg);
    3304          13 :             if (psTransformer != nullptr)
    3305             :             {
    3306          13 :                 CPLXMLNode *psTransformerContainer = CPLCreateXMLNode(
    3307             :                     psTree, CXT_Element,
    3308             :                     CPLSPrintf("%s%s", pszPrefix, psTransformer->pszValue));
    3309             : 
    3310          13 :                 CPLAddXMLChild(psTransformerContainer, psTransformer);
    3311             :             }
    3312             :         }
    3313             : 
    3314             :         /* ------------------------------------------------------------- */
    3315             :         /*      Handle geotransforms.                                    */
    3316             :         /* ------------------------------------------------------------- */
    3317             :         else
    3318             :         {
    3319         143 :             CPLsnprintf(szWork, sizeof(szWork),
    3320             :                         "%.17g,%.17g,%.17g,%.17g,%.17g,%.17g",
    3321         143 :                         part.adfGeoTransform[0], part.adfGeoTransform[1],
    3322         143 :                         part.adfGeoTransform[2], part.adfGeoTransform[3],
    3323         143 :                         part.adfGeoTransform[4], part.adfGeoTransform[5]);
    3324         143 :             CPLCreateXMLElementAndValue(
    3325             :                 psTree, CPLSPrintf("%sGeoTransform", pszPrefix), szWork);
    3326             : 
    3327         143 :             CPLsnprintf(szWork, sizeof(szWork),
    3328             :                         "%.17g,%.17g,%.17g,%.17g,%.17g,%.17g",
    3329         143 :                         part.adfInvGeoTransform[0], part.adfInvGeoTransform[1],
    3330         143 :                         part.adfInvGeoTransform[2], part.adfInvGeoTransform[3],
    3331         143 :                         part.adfInvGeoTransform[4], part.adfInvGeoTransform[5]);
    3332         143 :             CPLCreateXMLElementAndValue(
    3333             :                 psTree, CPLSPrintf("%sInvGeoTransform", pszPrefix), szWork);
    3334             :         }
    3335         156 :     };
    3336             : 
    3337          78 :     SerializePart("Src", psInfo->sSrcParams);
    3338          78 :     SerializePart("Dst", psInfo->sDstParams);
    3339             : 
    3340             :     /* -------------------------------------------------------------------- */
    3341             :     /*      Do we have a reprojection transformer?                          */
    3342             :     /* -------------------------------------------------------------------- */
    3343          78 :     if (psInfo->pReprojectArg != nullptr)
    3344             :     {
    3345             : 
    3346             :         CPLXMLNode *psTransformerContainer =
    3347          51 :             CPLCreateXMLNode(psTree, CXT_Element, "ReprojectTransformer");
    3348             : 
    3349             :         CPLXMLNode *psTransformer =
    3350          51 :             GDALSerializeTransformer(psInfo->pReproject, psInfo->pReprojectArg);
    3351          51 :         if (psTransformer != nullptr)
    3352          51 :             CPLAddXMLChild(psTransformerContainer, psTransformer);
    3353             :     }
    3354             : 
    3355          78 :     return psTree;
    3356             : }
    3357             : 
    3358             : /************************************************************************/
    3359             : /*                    GDALDeserializeGeoTransform()                     */
    3360             : /************************************************************************/
    3361             : 
    3362         739 : static void GDALDeserializeGeoTransform(const char *pszGT,
    3363             :                                         double adfGeoTransform[6])
    3364             : {
    3365         739 :     CPLsscanf(pszGT, "%lf,%lf,%lf,%lf,%lf,%lf", adfGeoTransform + 0,
    3366             :               adfGeoTransform + 1, adfGeoTransform + 2, adfGeoTransform + 3,
    3367             :               adfGeoTransform + 4, adfGeoTransform + 5);
    3368         739 : }
    3369             : 
    3370             : /************************************************************************/
    3371             : /*                GDALDeserializeGenImgProjTransformer()                */
    3372             : /************************************************************************/
    3373             : 
    3374         199 : void *GDALDeserializeGenImgProjTransformer(CPLXMLNode *psTree)
    3375             : 
    3376             : {
    3377             :     /* -------------------------------------------------------------------- */
    3378             :     /*      Initialize the transform info.                                  */
    3379             :     /* -------------------------------------------------------------------- */
    3380             :     GDALGenImgProjTransformInfo *psInfo =
    3381         199 :         GDALCreateGenImgProjTransformerInternal();
    3382             : 
    3383             :     const auto DeserializePart =
    3384        1194 :         [psTree](const char *pszPrefix, GDALGenImgProjTransformPart &part)
    3385             :     {
    3386             :         /* ----------------------------------------------------------------- */
    3387             :         /*      Geotransform                                                 */
    3388             :         /* ----------------------------------------------------------------- */
    3389         398 :         if (const auto psGTNode =
    3390         398 :                 CPLGetXMLNode(psTree, CPLSPrintf("%sGeoTransform", pszPrefix)))
    3391             :         {
    3392         382 :             GDALDeserializeGeoTransform(CPLGetXMLValue(psGTNode, "", ""),
    3393         382 :                                         part.adfGeoTransform);
    3394             : 
    3395         382 :             if (const auto psInvGTNode = CPLGetXMLNode(
    3396             :                     psTree, CPLSPrintf("%sInvGeoTransform", pszPrefix)))
    3397             :             {
    3398         357 :                 GDALDeserializeGeoTransform(CPLGetXMLValue(psInvGTNode, "", ""),
    3399         357 :                                             part.adfInvGeoTransform);
    3400             :             }
    3401             :             else
    3402             :             {
    3403          25 :                 if (!GDALInvGeoTransform(part.adfGeoTransform,
    3404          25 :                                          part.adfInvGeoTransform))
    3405             :                 {
    3406           0 :                     CPLError(CE_Failure, CPLE_AppDefined,
    3407             :                              "Cannot invert geotransform");
    3408             :                 }
    3409             :             }
    3410             :         }
    3411             : 
    3412             :         /* ---------------------------------------------------------------- */
    3413             :         /*      Transform                                                   */
    3414             :         /* ---------------------------------------------------------------- */
    3415             :         else
    3416             :         {
    3417          16 :             for (CPLXMLNode *psIter = psTree->psChild; psIter != nullptr;
    3418           0 :                  psIter = psIter->psNext)
    3419             :             {
    3420          16 :                 if (psIter->eType == CXT_Element &&
    3421          16 :                     STARTS_WITH_CI(psIter->pszValue, pszPrefix))
    3422             :                 {
    3423          16 :                     GDALDeserializeTransformer(psIter->psChild,
    3424             :                                                &part.pTransformer,
    3425             :                                                &part.pTransformArg);
    3426          16 :                     break;
    3427             :                 }
    3428             :             }
    3429             :         }
    3430         597 :     };
    3431             : 
    3432         199 :     DeserializePart("Src", psInfo->sSrcParams);
    3433         199 :     DeserializePart("Dst", psInfo->sDstParams);
    3434             : 
    3435             :     /* -------------------------------------------------------------------- */
    3436             :     /*      Reproject transformer                                           */
    3437             :     /* -------------------------------------------------------------------- */
    3438         199 :     CPLXMLNode *psSubtree = CPLGetXMLNode(psTree, "ReprojectTransformer");
    3439         199 :     if (psSubtree != nullptr && psSubtree->psChild != nullptr)
    3440             :     {
    3441          98 :         GDALDeserializeTransformer(psSubtree->psChild, &psInfo->pReproject,
    3442             :                                    &psInfo->pReprojectArg);
    3443             :     }
    3444             : 
    3445         199 :     return psInfo;
    3446             : }
    3447             : 
    3448             : /************************************************************************/
    3449             : /*                 GDALCreateReprojectionTransformer()                  */
    3450             : /************************************************************************/
    3451             : 
    3452             : /**
    3453             :  * Create reprojection transformer.
    3454             :  *
    3455             :  * Creates a callback data structure suitable for use with
    3456             :  * GDALReprojectionTransformation() to represent a transformation from
    3457             :  * one geographic or projected coordinate system to another.  On input
    3458             :  * the coordinate systems are described in OpenGIS WKT format.
    3459             :  *
    3460             :  * Internally the OGRCoordinateTransformation object is used to implement
    3461             :  * the reprojection.
    3462             :  *
    3463             :  * @param pszSrcWKT the coordinate system for the source coordinate system.
    3464             :  * @param pszDstWKT the coordinate system for the destination coordinate
    3465             :  * system.
    3466             :  *
    3467             :  * @return Handle for use with GDALReprojectionTransform(), or NULL if the
    3468             :  * system fails to initialize the reprojection.
    3469             :  **/
    3470             : 
    3471           0 : void *GDALCreateReprojectionTransformer(const char *pszSrcWKT,
    3472             :                                         const char *pszDstWKT)
    3473             : 
    3474             : {
    3475             :     /* -------------------------------------------------------------------- */
    3476             :     /*      Ingest the SRS definitions.                                     */
    3477             :     /* -------------------------------------------------------------------- */
    3478           0 :     OGRSpatialReference oSrcSRS;
    3479           0 :     oSrcSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3480           0 :     if (oSrcSRS.importFromWkt(pszSrcWKT) != OGRERR_NONE)
    3481             :     {
    3482           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    3483             :                  "Failed to import coordinate system `%s'.", pszSrcWKT);
    3484           0 :         return nullptr;
    3485             :     }
    3486             : 
    3487           0 :     OGRSpatialReference oDstSRS;
    3488           0 :     oDstSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3489           0 :     if (oDstSRS.importFromWkt(pszDstWKT) != OGRERR_NONE)
    3490             :     {
    3491           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    3492             :                  "Failed to import coordinate system `%s'.", pszSrcWKT);
    3493           0 :         return nullptr;
    3494             :     }
    3495             : 
    3496           0 :     return GDALCreateReprojectionTransformerEx(
    3497             :         OGRSpatialReference::ToHandle(&oSrcSRS),
    3498           0 :         OGRSpatialReference::ToHandle(&oDstSRS), nullptr);
    3499             : }
    3500             : 
    3501             : /************************************************************************/
    3502             : /*                 GDALCreateReprojectionTransformerEx()                */
    3503             : /************************************************************************/
    3504             : 
    3505             : /**
    3506             :  * Create reprojection transformer.
    3507             :  *
    3508             :  * Creates a callback data structure suitable for use with
    3509             :  * GDALReprojectionTransformation() to represent a transformation from
    3510             :  * one geographic or projected coordinate system to another.
    3511             :  *
    3512             :  * Internally the OGRCoordinateTransformation object is used to implement
    3513             :  * the reprojection.
    3514             :  *
    3515             :  * @param hSrcSRS the coordinate system for the source coordinate system.
    3516             :  * @param hDstSRS the coordinate system for the destination coordinate
    3517             :  * system.
    3518             :  * @param papszOptions NULL-terminated list of options, or NULL. Currently
    3519             :  * supported options are:
    3520             :  * <ul>
    3521             :  * <li>AREA_OF_INTEREST=west_long,south_lat,east_long,north_lat: Values in
    3522             :  * degrees. longitudes in [-180,180], latitudes in [-90,90].</li>
    3523             :  * <li>COORDINATE_OPERATION=string: PROJ or WKT string representing a
    3524             :  * coordinate operation, overriding the default computed transformation.</li>
    3525             :  * <li>COORDINATE_EPOCH=decimal_year: Coordinate epoch, expressed as a
    3526             :  * decimal year. Useful for time-dependent coordinate operations.</li>
    3527             :  * <li> SRC_COORDINATE_EPOCH: (GDAL &gt;= 3.4) Coordinate epoch of source CRS,
    3528             :  * expressed as a decimal year. Useful for time-dependent coordinate
    3529             :  *operations.</li>
    3530             :  * <li> DST_COORDINATE_EPOCH: (GDAL &gt;= 3.4) Coordinate epoch
    3531             :  *of target CRS, expressed as a decimal year. Useful for time-dependent
    3532             :  *coordinate operations.</li>
    3533             :  * <li> ALLOW_BALLPARK=YES/NO: (GDAL &gt;= 3.11) Whether ballpark coordinate
    3534             :  * operations are allowed. Defaults to YES.</li>
    3535             :  * <li> ONLY_BEST=YES/NO/AUTO: (GDAL &gt;= 3.11) By default (at least in the
    3536             :  * PROJ 9.x series), PROJ may use coordinate
    3537             :  * operations that are not the "best" if resources (typically grids) needed
    3538             :  * to use them are missing. It will then fallback to other coordinate operations
    3539             :  * that have a lesser accuracy, for example using Helmert transformations,
    3540             :  * or in the absence of such operations, to ones with potential very rough
    3541             :  * accuracy, using "ballpark" transformations
    3542             :  * (see https://proj.org/glossary.html).
    3543             :  * When calling this method with YES, PROJ will only consider the
    3544             :  * "best" operation, and error out (at Transform() time) if they cannot be
    3545             :  * used.
    3546             :  * This method may be used together with ALLOW_BALLPARK=NO to
    3547             :  * only allow best operations that have a known accuracy.
    3548             :  * Note that this method has no effect on PROJ versions before 9.2.
    3549             :  * The default value for this option can be also set with the
    3550             :  * PROJ_ONLY_BEST_DEFAULT environment variable, or with the "only_best_default"
    3551             :  * setting of proj.ini. Calling SetOnlyBest() overrides such default value.</li>
    3552             :  * </ul>
    3553             :  *
    3554             :  * @return Handle for use with GDALReprojectionTransform(), or NULL if the
    3555             :  * system fails to initialize the reprojection.
    3556             :  *
    3557             :  * @since GDAL 3.0
    3558             :  **/
    3559             : 
    3560        1197 : void *GDALCreateReprojectionTransformerEx(OGRSpatialReferenceH hSrcSRS,
    3561             :                                           OGRSpatialReferenceH hDstSRS,
    3562             :                                           const char *const *papszOptions)
    3563             : {
    3564        1197 :     OGRSpatialReference *poSrcSRS = OGRSpatialReference::FromHandle(hSrcSRS);
    3565        1197 :     OGRSpatialReference *poDstSRS = OGRSpatialReference::FromHandle(hDstSRS);
    3566             : 
    3567             :     /* -------------------------------------------------------------------- */
    3568             :     /*      Build the forward coordinate transformation.                    */
    3569             :     /* -------------------------------------------------------------------- */
    3570        1197 :     double dfWestLongitudeDeg = 0.0;
    3571        1197 :     double dfSouthLatitudeDeg = 0.0;
    3572        1197 :     double dfEastLongitudeDeg = 0.0;
    3573        1197 :     double dfNorthLatitudeDeg = 0.0;
    3574        1197 :     const char *pszBBOX = CSLFetchNameValue(papszOptions, "AREA_OF_INTEREST");
    3575        1197 :     if (pszBBOX)
    3576             :     {
    3577        1128 :         char **papszTokens = CSLTokenizeString2(pszBBOX, ",", 0);
    3578        1128 :         if (CSLCount(papszTokens) == 4)
    3579             :         {
    3580        1128 :             dfWestLongitudeDeg = CPLAtof(papszTokens[0]);
    3581        1128 :             dfSouthLatitudeDeg = CPLAtof(papszTokens[1]);
    3582        1128 :             dfEastLongitudeDeg = CPLAtof(papszTokens[2]);
    3583        1128 :             dfNorthLatitudeDeg = CPLAtof(papszTokens[3]);
    3584             :         }
    3585        1128 :         CSLDestroy(papszTokens);
    3586             :     }
    3587        1197 :     const char *pszCO = CSLFetchNameValue(papszOptions, "COORDINATE_OPERATION");
    3588             : 
    3589        2394 :     OGRCoordinateTransformationOptions optionsFwd;
    3590        1197 :     if (!(dfWestLongitudeDeg == 0.0 && dfSouthLatitudeDeg == 0.0 &&
    3591             :           dfEastLongitudeDeg == 0.0 && dfNorthLatitudeDeg == 0.0))
    3592             :     {
    3593        1128 :         optionsFwd.SetAreaOfInterest(dfWestLongitudeDeg, dfSouthLatitudeDeg,
    3594             :                                      dfEastLongitudeDeg, dfNorthLatitudeDeg);
    3595             :     }
    3596        1197 :     if (pszCO)
    3597             :     {
    3598           7 :         optionsFwd.SetCoordinateOperation(pszCO, false);
    3599             :     }
    3600             : 
    3601        1197 :     const char *pszCENTER_LONG = CSLFetchNameValue(papszOptions, "CENTER_LONG");
    3602        1197 :     if (pszCENTER_LONG)
    3603             :     {
    3604         824 :         optionsFwd.SetSourceCenterLong(CPLAtof(pszCENTER_LONG));
    3605             :     }
    3606             : 
    3607        1197 :     optionsFwd.SetBallparkAllowed(CPLTestBool(
    3608             :         CSLFetchNameValueDef(papszOptions, "ALLOW_BALLPARK", "YES")));
    3609             : 
    3610             :     const char *pszOnlyBest =
    3611        1197 :         CSLFetchNameValueDef(papszOptions, "ONLY_BEST", "AUTO");
    3612        1197 :     if (!EQUAL(pszOnlyBest, "AUTO"))
    3613             :     {
    3614           1 :         optionsFwd.SetOnlyBest(CPLTestBool(pszOnlyBest));
    3615             :     }
    3616             : 
    3617             :     OGRCoordinateTransformation *poForwardTransform =
    3618        1197 :         OGRCreateCoordinateTransformation(poSrcSRS, poDstSRS, optionsFwd);
    3619             : 
    3620        1197 :     if (poForwardTransform == nullptr)
    3621             :         // OGRCreateCoordinateTransformation() will report errors on its own.
    3622           6 :         return nullptr;
    3623             : 
    3624        1191 :     poForwardTransform->SetEmitErrors(false);
    3625             : 
    3626             :     /* -------------------------------------------------------------------- */
    3627             :     /*      Create a structure to hold the transform info, and also         */
    3628             :     /*      build reverse transform.  We assume that if the forward         */
    3629             :     /*      transform can be created, then so can the reverse one.          */
    3630             :     /* -------------------------------------------------------------------- */
    3631        1191 :     GDALReprojectionTransformInfo *psInfo = new GDALReprojectionTransformInfo();
    3632             : 
    3633        1191 :     psInfo->papszOptions = CSLDuplicate(papszOptions);
    3634        1191 :     psInfo->poForwardTransform = poForwardTransform;
    3635        1191 :     psInfo->dfTime = CPLAtof(CSLFetchNameValueDef(
    3636             :         papszOptions, "COORDINATE_EPOCH",
    3637             :         CSLFetchNameValueDef(
    3638             :             papszOptions, "DST_COORDINATE_EPOCH",
    3639             :             CSLFetchNameValueDef(papszOptions, "SRC_COORDINATE_EPOCH", "0"))));
    3640        1191 :     psInfo->poReverseTransform = poForwardTransform->GetInverse();
    3641             : 
    3642        1191 :     if (psInfo->poReverseTransform)
    3643        1191 :         psInfo->poReverseTransform->SetEmitErrors(false);
    3644             : 
    3645        1191 :     memcpy(psInfo->sTI.abySignature, GDAL_GTI2_SIGNATURE,
    3646             :            strlen(GDAL_GTI2_SIGNATURE));
    3647        1191 :     psInfo->sTI.pszClassName = GDAL_REPROJECTION_TRANSFORMER_CLASS_NAME;
    3648        1191 :     psInfo->sTI.pfnTransform = GDALReprojectionTransform;
    3649        1191 :     psInfo->sTI.pfnCleanup = GDALDestroyReprojectionTransformer;
    3650        1191 :     psInfo->sTI.pfnSerialize = GDALSerializeReprojectionTransformer;
    3651             : 
    3652        1191 :     return psInfo;
    3653             : }
    3654             : 
    3655             : /************************************************************************/
    3656             : /*                 GDALDestroyReprojectionTransformer()                 */
    3657             : /************************************************************************/
    3658             : 
    3659             : /**
    3660             :  * Destroy reprojection transformation.
    3661             :  *
    3662             :  * @param pTransformArg the transformation handle returned by
    3663             :  * GDALCreateReprojectionTransformer().
    3664             :  */
    3665             : 
    3666        1191 : void GDALDestroyReprojectionTransformer(void *pTransformArg)
    3667             : 
    3668             : {
    3669        1191 :     if (pTransformArg == nullptr)
    3670           0 :         return;
    3671             : 
    3672        1191 :     GDALReprojectionTransformInfo *psInfo =
    3673             :         static_cast<GDALReprojectionTransformInfo *>(pTransformArg);
    3674             : 
    3675        1191 :     if (psInfo->poForwardTransform)
    3676        1191 :         OGRCoordinateTransformation::DestroyCT(psInfo->poForwardTransform);
    3677             : 
    3678        1191 :     if (psInfo->poReverseTransform)
    3679        1191 :         OGRCoordinateTransformation::DestroyCT(psInfo->poReverseTransform);
    3680             : 
    3681        1191 :     CSLDestroy(psInfo->papszOptions);
    3682             : 
    3683        1191 :     delete psInfo;
    3684             : }
    3685             : 
    3686             : /************************************************************************/
    3687             : /*                     GDALReprojectionTransform()                      */
    3688             : /************************************************************************/
    3689             : 
    3690             : /**
    3691             :  * Perform reprojection transformation.
    3692             :  *
    3693             :  * Actually performs the reprojection transformation described in
    3694             :  * GDALCreateReprojectionTransformer().  This function matches the
    3695             :  * GDALTransformerFunc() signature.  Details of the arguments are described
    3696             :  * there.
    3697             :  */
    3698             : 
    3699     1904960 : int GDALReprojectionTransform(void *pTransformArg, int bDstToSrc,
    3700             :                               int nPointCount, double *padfX, double *padfY,
    3701             :                               double *padfZ, int *panSuccess)
    3702             : 
    3703             : {
    3704     1904960 :     GDALReprojectionTransformInfo *psInfo =
    3705             :         static_cast<GDALReprojectionTransformInfo *>(pTransformArg);
    3706             :     int bSuccess;
    3707             : 
    3708     1904960 :     std::vector<double> adfTime;
    3709     1904960 :     double *padfT = nullptr;
    3710     1904960 :     if (psInfo->dfTime != 0.0 && nPointCount > 0)
    3711             :     {
    3712           1 :         adfTime.resize(nPointCount, psInfo->dfTime);
    3713           1 :         padfT = &adfTime[0];
    3714             :     }
    3715             : 
    3716     1904960 :     if (bDstToSrc)
    3717             :     {
    3718     1645310 :         if (psInfo->poReverseTransform == nullptr)
    3719             :         {
    3720           0 :             CPLError(
    3721             :                 CE_Failure, CPLE_AppDefined,
    3722             :                 "Inverse coordinate transformation cannot be instantiated");
    3723           0 :             if (panSuccess)
    3724             :             {
    3725           0 :                 for (int i = 0; i < nPointCount; i++)
    3726           0 :                     panSuccess[i] = FALSE;
    3727             :             }
    3728           0 :             bSuccess = false;
    3729             :         }
    3730             :         else
    3731             :         {
    3732     1645320 :             bSuccess = psInfo->poReverseTransform->Transform(
    3733     1645310 :                 nPointCount, padfX, padfY, padfZ, padfT, panSuccess);
    3734             :         }
    3735             :     }
    3736             :     else
    3737      259648 :         bSuccess = psInfo->poForwardTransform->Transform(
    3738      259649 :             nPointCount, padfX, padfY, padfZ, padfT, panSuccess);
    3739             : 
    3740     3809920 :     return bSuccess;
    3741             : }
    3742             : 
    3743             : /************************************************************************/
    3744             : /*                GDALSerializeReprojectionTransformer()                */
    3745             : /************************************************************************/
    3746             : 
    3747         155 : static CPLXMLNode *GDALSerializeReprojectionTransformer(void *pTransformArg)
    3748             : 
    3749             : {
    3750             :     CPLXMLNode *psTree;
    3751         155 :     GDALReprojectionTransformInfo *psInfo =
    3752             :         static_cast<GDALReprojectionTransformInfo *>(pTransformArg);
    3753             : 
    3754         155 :     psTree = CPLCreateXMLNode(nullptr, CXT_Element, "ReprojectionTransformer");
    3755             : 
    3756             :     /* -------------------------------------------------------------------- */
    3757             :     /*      Handle SourceCS.                                                */
    3758             :     /* -------------------------------------------------------------------- */
    3759         310 :     const auto ExportToWkt = [](const OGRSpatialReference *poSRS)
    3760             :     {
    3761             :         // Try first in WKT1 for backward compat
    3762             :         {
    3763         310 :             char *pszWKT = nullptr;
    3764         310 :             const char *const apszOptions[] = {"FORMAT=WKT1", nullptr};
    3765         310 :             CPLErrorHandlerPusher oHandler(CPLQuietErrorHandler);
    3766         309 :             CPLErrorStateBackuper oBackuper;
    3767         310 :             if (poSRS->exportToWkt(&pszWKT, apszOptions) == OGRERR_NONE)
    3768             :             {
    3769         618 :                 std::string osRet(pszWKT);
    3770         309 :                 CPLFree(pszWKT);
    3771         309 :                 return osRet;
    3772             :             }
    3773           1 :             CPLFree(pszWKT);
    3774             :         }
    3775             : 
    3776           1 :         char *pszWKT = nullptr;
    3777           1 :         const char *const apszOptions[] = {"FORMAT=WKT2_2019", nullptr};
    3778           1 :         if (poSRS->exportToWkt(&pszWKT, apszOptions) == OGRERR_NONE)
    3779             :         {
    3780           2 :             std::string osRet(pszWKT);
    3781           1 :             CPLFree(pszWKT);
    3782           1 :             return osRet;
    3783             :         }
    3784           0 :         CPLFree(pszWKT);
    3785           0 :         return std::string();
    3786             :     };
    3787             : 
    3788         155 :     auto poSRS = psInfo->poForwardTransform->GetSourceCS();
    3789         155 :     if (poSRS)
    3790             :     {
    3791         310 :         const auto osWKT = ExportToWkt(poSRS);
    3792         155 :         CPLCreateXMLElementAndValue(psTree, "SourceSRS", osWKT.c_str());
    3793             :     }
    3794             : 
    3795             :     /* -------------------------------------------------------------------- */
    3796             :     /*      Handle DestinationCS.                                           */
    3797             :     /* -------------------------------------------------------------------- */
    3798         155 :     poSRS = psInfo->poForwardTransform->GetTargetCS();
    3799         155 :     if (poSRS)
    3800             :     {
    3801         310 :         const auto osWKT = ExportToWkt(poSRS);
    3802         155 :         CPLCreateXMLElementAndValue(psTree, "TargetSRS", osWKT.c_str());
    3803             :     }
    3804             : 
    3805             :     /* -------------------------------------------------------------------- */
    3806             :     /*      Serialize options.                                              */
    3807             :     /* -------------------------------------------------------------------- */
    3808         155 :     if (psInfo->papszOptions)
    3809             :     {
    3810             :         CPLXMLNode *psOptions =
    3811         142 :             CPLCreateXMLNode(psTree, CXT_Element, "Options");
    3812         358 :         for (auto iter = psInfo->papszOptions; *iter != nullptr; ++iter)
    3813             :         {
    3814         216 :             char *pszKey = nullptr;
    3815         216 :             const char *pszValue = CPLParseNameValue(*iter, &pszKey);
    3816         216 :             if (pszKey && pszValue)
    3817             :             {
    3818             :                 auto elt =
    3819         216 :                     CPLCreateXMLElementAndValue(psOptions, "Option", pszValue);
    3820         216 :                 CPLAddXMLAttributeAndValue(elt, "key", pszKey);
    3821             :             }
    3822         216 :             CPLFree(pszKey);
    3823             :         }
    3824             :     }
    3825             : 
    3826         155 :     return psTree;
    3827             : }
    3828             : 
    3829             : /************************************************************************/
    3830             : /*               GDALDeserializeReprojectionTransformer()               */
    3831             : /************************************************************************/
    3832             : 
    3833         202 : static void *GDALDeserializeReprojectionTransformer(CPLXMLNode *psTree)
    3834             : 
    3835             : {
    3836         202 :     const char *pszSourceSRS = CPLGetXMLValue(psTree, "SourceSRS", nullptr);
    3837         202 :     const char *pszTargetSRS = CPLGetXMLValue(psTree, "TargetSRS", nullptr);
    3838         202 :     char *pszSourceWKT = nullptr, *pszTargetWKT = nullptr;
    3839         202 :     void *pResult = nullptr;
    3840             : 
    3841         404 :     OGRSpatialReference oSrcSRS;
    3842         404 :     OGRSpatialReference oDstSRS;
    3843             : 
    3844         202 :     oSrcSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3845         202 :     oDstSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    3846         202 :     if (pszSourceSRS != nullptr)
    3847             :     {
    3848         202 :         oSrcSRS.SetFromUserInput(pszSourceSRS);
    3849             :     }
    3850             : 
    3851         202 :     if (pszTargetSRS != nullptr)
    3852             :     {
    3853         202 :         oDstSRS.SetFromUserInput(pszTargetSRS);
    3854             :     }
    3855             : 
    3856         202 :     CPLStringList aosList;
    3857         202 :     const CPLXMLNode *psOptions = CPLGetXMLNode(psTree, "Options");
    3858         202 :     if (psOptions)
    3859             :     {
    3860         426 :         for (auto iter = psOptions->psChild; iter; iter = iter->psNext)
    3861             :         {
    3862         260 :             if (iter->eType == CXT_Element &&
    3863         260 :                 strcmp(iter->pszValue, "Option") == 0)
    3864             :             {
    3865         260 :                 const char *pszKey = CPLGetXMLValue(iter, "key", nullptr);
    3866         260 :                 const char *pszValue = CPLGetXMLValue(iter, nullptr, nullptr);
    3867         260 :                 if (pszKey && pszValue)
    3868             :                 {
    3869         260 :                     aosList.SetNameValue(pszKey, pszValue);
    3870             :                 }
    3871             :             }
    3872             :         }
    3873             :     }
    3874             : 
    3875         202 :     pResult = GDALCreateReprojectionTransformerEx(
    3876         202 :         !oSrcSRS.IsEmpty() ? OGRSpatialReference::ToHandle(&oSrcSRS) : nullptr,
    3877         202 :         !oDstSRS.IsEmpty() ? OGRSpatialReference::ToHandle(&oDstSRS) : nullptr,
    3878         202 :         aosList.List());
    3879             : 
    3880         202 :     CPLFree(pszSourceWKT);
    3881         202 :     CPLFree(pszTargetWKT);
    3882             : 
    3883         404 :     return pResult;
    3884             : }
    3885             : 
    3886             : /************************************************************************/
    3887             : /* ==================================================================== */
    3888             : /*      Approximate transformer.                                        */
    3889             : /* ==================================================================== */
    3890             : /************************************************************************/
    3891             : 
    3892             : /************************************************************************/
    3893             : /*                  GDALCreateSimilarApproxTransformer()                */
    3894             : /************************************************************************/
    3895             : 
    3896          41 : static void *GDALCreateSimilarApproxTransformer(void *hTransformArg,
    3897             :                                                 double dfSrcRatioX,
    3898             :                                                 double dfSrcRatioY)
    3899             : {
    3900          41 :     VALIDATE_POINTER1(hTransformArg, "GDALCreateSimilarApproxTransformer",
    3901             :                       nullptr);
    3902             : 
    3903          41 :     GDALApproxTransformInfo *psInfo =
    3904             :         static_cast<GDALApproxTransformInfo *>(hTransformArg);
    3905             : 
    3906          41 :     void *pBaseCBData = GDALCreateSimilarTransformer(psInfo->pBaseCBData,
    3907             :                                                      dfSrcRatioX, dfSrcRatioY);
    3908          41 :     if (pBaseCBData == nullptr)
    3909             :     {
    3910           0 :         return nullptr;
    3911             :     }
    3912             : 
    3913             :     GDALApproxTransformInfo *psClonedInfo =
    3914          41 :         static_cast<GDALApproxTransformInfo *>(GDALCreateApproxTransformer2(
    3915             :             psInfo->pfnBaseTransformer, pBaseCBData, psInfo->dfMaxErrorForward,
    3916             :             psInfo->dfMaxErrorReverse));
    3917          41 :     psClonedInfo->bOwnSubtransformer = TRUE;
    3918             : 
    3919          41 :     return psClonedInfo;
    3920             : }
    3921             : 
    3922             : /************************************************************************/
    3923             : /*                   GDALSerializeApproxTransformer()                   */
    3924             : /************************************************************************/
    3925             : 
    3926          58 : static CPLXMLNode *GDALSerializeApproxTransformer(void *pTransformArg)
    3927             : 
    3928             : {
    3929             :     CPLXMLNode *psTree;
    3930          58 :     GDALApproxTransformInfo *psInfo =
    3931             :         static_cast<GDALApproxTransformInfo *>(pTransformArg);
    3932             : 
    3933          58 :     psTree = CPLCreateXMLNode(nullptr, CXT_Element, "ApproxTransformer");
    3934             : 
    3935             :     /* -------------------------------------------------------------------- */
    3936             :     /*      Attach max error.                                               */
    3937             :     /* -------------------------------------------------------------------- */
    3938          58 :     if (psInfo->dfMaxErrorForward == psInfo->dfMaxErrorReverse)
    3939             :     {
    3940          56 :         CPLCreateXMLElementAndValue(
    3941             :             psTree, "MaxError",
    3942         112 :             CPLString().Printf("%g", psInfo->dfMaxErrorForward));
    3943             :     }
    3944             :     else
    3945             :     {
    3946           2 :         CPLCreateXMLElementAndValue(
    3947             :             psTree, "MaxErrorForward",
    3948           4 :             CPLString().Printf("%g", psInfo->dfMaxErrorForward));
    3949           2 :         CPLCreateXMLElementAndValue(
    3950             :             psTree, "MaxErrorReverse",
    3951           4 :             CPLString().Printf("%g", psInfo->dfMaxErrorReverse));
    3952             :     }
    3953             : 
    3954             :     /* -------------------------------------------------------------------- */
    3955             :     /*      Capture underlying transformer.                                 */
    3956             :     /* -------------------------------------------------------------------- */
    3957             :     CPLXMLNode *psTransformerContainer =
    3958          58 :         CPLCreateXMLNode(psTree, CXT_Element, "BaseTransformer");
    3959             : 
    3960          58 :     CPLXMLNode *psTransformer = GDALSerializeTransformer(
    3961             :         psInfo->pfnBaseTransformer, psInfo->pBaseCBData);
    3962          58 :     if (psTransformer != nullptr)
    3963          58 :         CPLAddXMLChild(psTransformerContainer, psTransformer);
    3964             : 
    3965          58 :     return psTree;
    3966             : }
    3967             : 
    3968             : /************************************************************************/
    3969             : /*                    GDALCreateApproxTransformer()                     */
    3970             : /************************************************************************/
    3971             : 
    3972             : /**
    3973             :  * Create an approximating transformer.
    3974             :  *
    3975             :  * This function creates a context for an approximated transformer.  Basically
    3976             :  * a high precision transformer is supplied as input and internally linear
    3977             :  * approximations are computed to generate results to within a defined
    3978             :  * precision.
    3979             :  *
    3980             :  * The approximation is actually done at the point where GDALApproxTransform()
    3981             :  * calls are made, and depend on the assumption that they are roughly linear.
    3982             :  * The first and last point passed in must be the extreme values and the
    3983             :  * intermediate values should describe a curve between the end points.  The
    3984             :  * approximator transforms and centers using the approximate transformer, and
    3985             :  * then compares the true middle transformed value to a linear approximation
    3986             :  * based on the end points.  If the error is within the supplied threshold then
    3987             :  * the end points are used to linearly approximate all the values otherwise the
    3988             :  * input points are split into two smaller sets, and the function is recursively
    3989             :  * called until a sufficiently small set of points is found that the linear
    3990             :  * approximation is OK, or that all the points are exactly computed.
    3991             :  *
    3992             :  * This function is very suitable for approximating transformation results
    3993             :  * from output pixel/line space to input coordinates for warpers that operate
    3994             :  * on one input scanline at a time.  Care should be taken using it in other
    3995             :  * circumstances as little internal validation is done in order to keep things
    3996             :  * fast.
    3997             :  *
    3998             :  * @param pfnBaseTransformer the high precision transformer which should be
    3999             :  * approximated.
    4000             :  * @param pBaseTransformArg the callback argument for the high precision
    4001             :  * transformer.
    4002             :  * @param dfMaxError the maximum cartesian error in the "output" space that
    4003             :  * is to be accepted in the linear approximation, evaluated as a Manhattan
    4004             :  * distance.
    4005             :  *
    4006             :  * @return callback pointer suitable for use with GDALApproxTransform().  It
    4007             :  * should be deallocated with GDALDestroyApproxTransformer().
    4008             :  */
    4009             : 
    4010        1148 : void *GDALCreateApproxTransformer(GDALTransformerFunc pfnBaseTransformer,
    4011             :                                   void *pBaseTransformArg, double dfMaxError)
    4012             : 
    4013             : {
    4014        1148 :     return GDALCreateApproxTransformer2(pfnBaseTransformer, pBaseTransformArg,
    4015        1148 :                                         dfMaxError, dfMaxError);
    4016             : }
    4017             : 
    4018             : static void *
    4019        1342 : GDALCreateApproxTransformer2(GDALTransformerFunc pfnBaseTransformer,
    4020             :                              void *pBaseTransformArg, double dfMaxErrorForward,
    4021             :                              double dfMaxErrorReverse)
    4022             : 
    4023             : {
    4024        1342 :     GDALApproxTransformInfo *psATInfo = new GDALApproxTransformInfo;
    4025        1342 :     psATInfo->pfnBaseTransformer = pfnBaseTransformer;
    4026        1342 :     psATInfo->pBaseCBData = pBaseTransformArg;
    4027        1342 :     psATInfo->dfMaxErrorForward = dfMaxErrorForward;
    4028        1342 :     psATInfo->dfMaxErrorReverse = dfMaxErrorReverse;
    4029        1342 :     psATInfo->bOwnSubtransformer = FALSE;
    4030             : 
    4031        1342 :     memcpy(psATInfo->sTI.abySignature, GDAL_GTI2_SIGNATURE,
    4032             :            strlen(GDAL_GTI2_SIGNATURE));
    4033        1342 :     psATInfo->sTI.pszClassName = GDAL_APPROX_TRANSFORMER_CLASS_NAME;
    4034        1342 :     psATInfo->sTI.pfnTransform = GDALApproxTransform;
    4035        1342 :     psATInfo->sTI.pfnCleanup = GDALDestroyApproxTransformer;
    4036        1342 :     psATInfo->sTI.pfnSerialize = GDALSerializeApproxTransformer;
    4037        1342 :     psATInfo->sTI.pfnCreateSimilar = GDALCreateSimilarApproxTransformer;
    4038             : 
    4039        1342 :     return psATInfo;
    4040             : }
    4041             : 
    4042             : /************************************************************************/
    4043             : /*              GDALApproxTransformerOwnsSubtransformer()               */
    4044             : /************************************************************************/
    4045             : 
    4046             : /** Set bOwnSubtransformer flag */
    4047        1298 : void GDALApproxTransformerOwnsSubtransformer(void *pCBData, int bOwnFlag)
    4048             : 
    4049             : {
    4050        1298 :     GDALApproxTransformInfo *psATInfo =
    4051             :         static_cast<GDALApproxTransformInfo *>(pCBData);
    4052             : 
    4053        1298 :     psATInfo->bOwnSubtransformer = bOwnFlag;
    4054        1298 : }
    4055             : 
    4056             : /************************************************************************/
    4057             : /*                    GDALDestroyApproxTransformer()                    */
    4058             : /************************************************************************/
    4059             : 
    4060             : /**
    4061             :  * Cleanup approximate transformer.
    4062             :  *
    4063             :  * Deallocates the resources allocated by GDALCreateApproxTransformer().
    4064             :  *
    4065             :  * @param pCBData callback data originally returned by
    4066             :  * GDALCreateApproxTransformer().
    4067             :  */
    4068             : 
    4069        1342 : void GDALDestroyApproxTransformer(void *pCBData)
    4070             : 
    4071             : {
    4072        1342 :     if (pCBData == nullptr)
    4073           0 :         return;
    4074             : 
    4075        1342 :     GDALApproxTransformInfo *psATInfo =
    4076             :         static_cast<GDALApproxTransformInfo *>(pCBData);
    4077             : 
    4078        1342 :     if (psATInfo->bOwnSubtransformer)
    4079        1339 :         GDALDestroyTransformer(psATInfo->pBaseCBData);
    4080             : 
    4081        1342 :     delete psATInfo;
    4082             : }
    4083             : 
    4084             : /************************************************************************/
    4085             : /*                  GDALRefreshApproxTransformer()                      */
    4086             : /************************************************************************/
    4087             : 
    4088          44 : void GDALRefreshApproxTransformer(void *hTransformArg)
    4089             : {
    4090          44 :     GDALApproxTransformInfo *psInfo =
    4091             :         static_cast<GDALApproxTransformInfo *>(hTransformArg);
    4092             : 
    4093          44 :     if (GDALIsTransformer(psInfo->pBaseCBData,
    4094             :                           GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME))
    4095             :     {
    4096          44 :         GDALRefreshGenImgProjTransformer(psInfo->pBaseCBData);
    4097             :     }
    4098          44 : }
    4099             : 
    4100             : /************************************************************************/
    4101             : /*                      GDALApproxTransformInternal()                   */
    4102             : /************************************************************************/
    4103             : 
    4104     1304960 : static int GDALApproxTransformInternal(void *pCBData, int bDstToSrc,
    4105             :                                        int nPoints, double *x, double *y,
    4106             :                                        double *z, int *panSuccess,
    4107             :                                        // SME = Start, Middle, End.
    4108             :                                        const double xSMETransformed[3],
    4109             :                                        const double ySMETransformed[3],
    4110             :                                        const double zSMETransformed[3])
    4111             : {
    4112     1304960 :     GDALApproxTransformInfo *psATInfo =
    4113             :         static_cast<GDALApproxTransformInfo *>(pCBData);
    4114     1304960 :     const int nMiddle = (nPoints - 1) / 2;
    4115             : 
    4116             : #ifdef notdef_sanify_check
    4117             :     {
    4118             :         double x2[3] = {x[0], x[nMiddle], x[nPoints - 1]};
    4119             :         double y2[3] = {y[0], y[nMiddle], y[nPoints - 1]};
    4120             :         double z2[3] = {z[0], z[nMiddle], z[nPoints - 1]};
    4121             :         int anSuccess2[3] = {};
    4122             : 
    4123             :         const int bSuccess = psATInfo->pfnBaseTransformer(
    4124             :             psATInfo->pBaseCBData, bDstToSrc, 3, x2, y2, z2, anSuccess2);
    4125             :         CPLAssert(bSuccess);
    4126             :         CPLAssert(anSuccess2[0]);
    4127             :         CPLAssert(anSuccess2[1]);
    4128             :         CPLAssert(anSuccess2[2]);
    4129             :         CPLAssert(x2[0] == xSMETransformed[0]);
    4130             :         CPLAssert(y2[0] == ySMETransformed[0]);
    4131             :         CPLAssert(z2[0] == zSMETransformed[0]);
    4132             :         CPLAssert(x2[1] == xSMETransformed[1]);
    4133             :         CPLAssert(y2[1] == ySMETransformed[1]);
    4134             :         CPLAssert(z2[1] == zSMETransformed[1]);
    4135             :         CPLAssert(x2[2] == xSMETransformed[2]);
    4136             :         CPLAssert(y2[2] == ySMETransformed[2]);
    4137             :         CPLAssert(z2[2] == zSMETransformed[2]);
    4138             :     }
    4139             : #endif
    4140             : 
    4141             : #ifdef DEBUG_APPROX_TRANSFORMER
    4142             :     fprintf(stderr, "start (%.3f,%.3f) -> (%.3f,%.3f)\n", /*ok*/
    4143             :             x[0], y[0], xSMETransformed[0], ySMETransformed[0]);
    4144             :     fprintf(stderr, "middle (%.3f,%.3f) -> (%.3f,%.3f)\n", /*ok*/
    4145             :             x[nMiddle], y[nMiddle], xSMETransformed[1], ySMETransformed[1]);
    4146             :     fprintf(stderr, "end (%.3f,%.3f) -> (%.3f,%.3f)\n", /*ok*/
    4147             :             x[nPoints - 1], y[nPoints - 1], xSMETransformed[2],
    4148             :             ySMETransformed[2]);
    4149             : #endif
    4150             : 
    4151             :     /* -------------------------------------------------------------------- */
    4152             :     /*      Is the error at the middle acceptable relative to an            */
    4153             :     /*      interpolation of the middle position?                           */
    4154             :     /* -------------------------------------------------------------------- */
    4155     1304960 :     const double dfDeltaX =
    4156     1304960 :         (xSMETransformed[2] - xSMETransformed[0]) / (x[nPoints - 1] - x[0]);
    4157     1304960 :     const double dfDeltaY =
    4158     1304960 :         (ySMETransformed[2] - ySMETransformed[0]) / (x[nPoints - 1] - x[0]);
    4159     1304960 :     const double dfDeltaZ =
    4160     1304960 :         (zSMETransformed[2] - zSMETransformed[0]) / (x[nPoints - 1] - x[0]);
    4161             : 
    4162     1304960 :     const double dfError =
    4163     1304960 :         fabs((xSMETransformed[0] + dfDeltaX * (x[nMiddle] - x[0])) -
    4164     1304960 :              xSMETransformed[1]) +
    4165     1304960 :         fabs((ySMETransformed[0] + dfDeltaY * (x[nMiddle] - x[0])) -
    4166     1304960 :              ySMETransformed[1]);
    4167             : 
    4168     1304960 :     const double dfMaxError =
    4169     1304960 :         (bDstToSrc) ? psATInfo->dfMaxErrorReverse : psATInfo->dfMaxErrorForward;
    4170     1304960 :     if (dfError > dfMaxError)
    4171             :     {
    4172             : #if DEBUG_VERBOSE
    4173             :         CPLDebug("GDAL",
    4174             :                  "ApproxTransformer - "
    4175             :                  "error %g over threshold %g, subdivide %d points.",
    4176             :                  dfError, dfMaxError, nPoints);
    4177             : #endif
    4178             : 
    4179      596079 :         double xMiddle[3] = {x[(nMiddle - 1) / 2], x[nMiddle - 1],
    4180      596079 :                              x[nMiddle + (nPoints - nMiddle - 1) / 2]};
    4181      596079 :         double yMiddle[3] = {y[(nMiddle - 1) / 2], y[nMiddle - 1],
    4182      596079 :                              y[nMiddle + (nPoints - nMiddle - 1) / 2]};
    4183      596079 :         double zMiddle[3] = {z[(nMiddle - 1) / 2], z[nMiddle - 1],
    4184      596079 :                              z[nMiddle + (nPoints - nMiddle - 1) / 2]};
    4185             : 
    4186      596079 :         const bool bUseBaseTransformForHalf1 =
    4187      467262 :             nMiddle <= 5 || y[0] != y[nMiddle - 1] ||
    4188     1530600 :             y[0] != y[(nMiddle - 1) / 2] || x[0] == x[nMiddle - 1] ||
    4189      467262 :             x[0] == x[(nMiddle - 1) / 2];
    4190      596079 :         const bool bUseBaseTransformForHalf2 =
    4191      481397 :             nPoints - nMiddle <= 5 || y[nMiddle] != y[nPoints - 1] ||
    4192      481397 :             y[nMiddle] != y[nMiddle + (nPoints - nMiddle - 1) / 2] ||
    4193     1558870 :             x[nMiddle] == x[nPoints - 1] ||
    4194      481397 :             x[nMiddle] == x[nMiddle + (nPoints - nMiddle - 1) / 2];
    4195             : 
    4196      596079 :         int anSuccess2[3] = {};
    4197      596079 :         int bSuccess = FALSE;
    4198      596079 :         if (!bUseBaseTransformForHalf1 && !bUseBaseTransformForHalf2)
    4199      467262 :             bSuccess = psATInfo->pfnBaseTransformer(
    4200             :                 psATInfo->pBaseCBData, bDstToSrc, 3, xMiddle, yMiddle, zMiddle,
    4201             :                 anSuccess2);
    4202      128817 :         else if (!bUseBaseTransformForHalf1)
    4203             :         {
    4204           0 :             bSuccess = psATInfo->pfnBaseTransformer(
    4205             :                 psATInfo->pBaseCBData, bDstToSrc, 2, xMiddle, yMiddle, zMiddle,
    4206             :                 anSuccess2);
    4207           0 :             anSuccess2[2] = TRUE;
    4208             :         }
    4209      128817 :         else if (!bUseBaseTransformForHalf2)
    4210             :         {
    4211       14135 :             bSuccess = psATInfo->pfnBaseTransformer(
    4212             :                 psATInfo->pBaseCBData, bDstToSrc, 1, xMiddle + 2, yMiddle + 2,
    4213             :                 zMiddle + 2, anSuccess2 + 2);
    4214       14135 :             anSuccess2[0] = TRUE;
    4215       14135 :             anSuccess2[1] = TRUE;
    4216             :         }
    4217             : 
    4218      596079 :         if (!bSuccess || !anSuccess2[0] || !anSuccess2[1] || !anSuccess2[2])
    4219             :         {
    4220      114698 :             bSuccess = psATInfo->pfnBaseTransformer(
    4221             :                 psATInfo->pBaseCBData, bDstToSrc, nMiddle - 1, x + 1, y + 1,
    4222             :                 z + 1, panSuccess + 1);
    4223      229396 :             bSuccess &= psATInfo->pfnBaseTransformer(
    4224      114698 :                 psATInfo->pBaseCBData, bDstToSrc, nPoints - nMiddle - 2,
    4225      114698 :                 x + nMiddle + 1, y + nMiddle + 1, z + nMiddle + 1,
    4226      114698 :                 panSuccess + nMiddle + 1);
    4227             : 
    4228      114698 :             x[0] = xSMETransformed[0];
    4229      114698 :             y[0] = ySMETransformed[0];
    4230      114698 :             z[0] = zSMETransformed[0];
    4231      114698 :             panSuccess[0] = TRUE;
    4232      114698 :             x[nMiddle] = xSMETransformed[1];
    4233      114698 :             y[nMiddle] = ySMETransformed[1];
    4234      114698 :             z[nMiddle] = zSMETransformed[1];
    4235      114698 :             panSuccess[nMiddle] = TRUE;
    4236      114698 :             x[nPoints - 1] = xSMETransformed[2];
    4237      114698 :             y[nPoints - 1] = ySMETransformed[2];
    4238      114698 :             z[nPoints - 1] = zSMETransformed[2];
    4239      114698 :             panSuccess[nPoints - 1] = TRUE;
    4240      114698 :             return bSuccess;
    4241             :         }
    4242             : 
    4243      481381 :         double x2[3] = {};
    4244      481381 :         double y2[3] = {};
    4245      481381 :         double z2[3] = {};
    4246      481381 :         if (!bUseBaseTransformForHalf1)
    4247             :         {
    4248      467246 :             x2[0] = xSMETransformed[0];
    4249      467246 :             y2[0] = ySMETransformed[0];
    4250      467246 :             z2[0] = zSMETransformed[0];
    4251      467246 :             x2[1] = xMiddle[0];
    4252      467246 :             y2[1] = yMiddle[0];
    4253      467246 :             z2[1] = zMiddle[0];
    4254      467246 :             x2[2] = xMiddle[1];
    4255      467246 :             y2[2] = yMiddle[1];
    4256      467246 :             z2[2] = zMiddle[1];
    4257             : 
    4258      467246 :             bSuccess = GDALApproxTransformInternal(
    4259             :                 psATInfo, bDstToSrc, nMiddle, x, y, z, panSuccess, x2, y2, z2);
    4260             :         }
    4261             :         else
    4262             :         {
    4263       14135 :             bSuccess = psATInfo->pfnBaseTransformer(
    4264             :                 psATInfo->pBaseCBData, bDstToSrc, nMiddle - 1, x + 1, y + 1,
    4265             :                 z + 1, panSuccess + 1);
    4266       14135 :             x[0] = xSMETransformed[0];
    4267       14135 :             y[0] = ySMETransformed[0];
    4268       14135 :             z[0] = zSMETransformed[0];
    4269       14135 :             panSuccess[0] = TRUE;
    4270             :         }
    4271             : 
    4272      481381 :         if (!bSuccess)
    4273          24 :             return FALSE;
    4274             : 
    4275      481357 :         if (!bUseBaseTransformForHalf2)
    4276             :         {
    4277      481357 :             x2[0] = xSMETransformed[1];
    4278      481357 :             y2[0] = ySMETransformed[1];
    4279      481357 :             z2[0] = zSMETransformed[1];
    4280      481357 :             x2[1] = xMiddle[2];
    4281      481357 :             y2[1] = yMiddle[2];
    4282      481357 :             z2[1] = zMiddle[2];
    4283      481357 :             x2[2] = xSMETransformed[2];
    4284      481357 :             y2[2] = ySMETransformed[2];
    4285      481357 :             z2[2] = zSMETransformed[2];
    4286             : 
    4287      481357 :             bSuccess = GDALApproxTransformInternal(
    4288      481357 :                 psATInfo, bDstToSrc, nPoints - nMiddle, x + nMiddle,
    4289      481357 :                 y + nMiddle, z + nMiddle, panSuccess + nMiddle, x2, y2, z2);
    4290             :         }
    4291             :         else
    4292             :         {
    4293           0 :             bSuccess = psATInfo->pfnBaseTransformer(
    4294           0 :                 psATInfo->pBaseCBData, bDstToSrc, nPoints - nMiddle - 2,
    4295           0 :                 x + nMiddle + 1, y + nMiddle + 1, z + nMiddle + 1,
    4296           0 :                 panSuccess + nMiddle + 1);
    4297             : 
    4298           0 :             x[nMiddle] = xSMETransformed[1];
    4299           0 :             y[nMiddle] = ySMETransformed[1];
    4300           0 :             z[nMiddle] = zSMETransformed[1];
    4301           0 :             panSuccess[nMiddle] = TRUE;
    4302           0 :             x[nPoints - 1] = xSMETransformed[2];
    4303           0 :             y[nPoints - 1] = ySMETransformed[2];
    4304           0 :             z[nPoints - 1] = zSMETransformed[2];
    4305           0 :             panSuccess[nPoints - 1] = TRUE;
    4306             :         }
    4307             : 
    4308      481357 :         if (!bSuccess)
    4309           2 :             return FALSE;
    4310             : 
    4311      481355 :         return TRUE;
    4312             :     }
    4313             : 
    4314             :     /* -------------------------------------------------------------------- */
    4315             :     /*      Error is OK since this is just used to compute output bounds    */
    4316             :     /*      of newly created file for gdalwarper.  So just use affine       */
    4317             :     /*      approximation of the reverse transform.  Eventually we          */
    4318             :     /*      should implement iterative searching to find a result within    */
    4319             :     /*      our error threshold.                                            */
    4320             :     /*      NOTE: the above comment is not true: gdalwarp uses approximator */
    4321             :     /*      also to compute the source pixel of each target pixel.          */
    4322             :     /* -------------------------------------------------------------------- */
    4323   144066000 :     for (int i = nPoints - 1; i >= 0; i--)
    4324             :     {
    4325             : #ifdef check_error
    4326             :         double xtemp = x[i];
    4327             :         double ytemp = y[i];
    4328             :         double ztemp = z[i];
    4329             :         double x_ori = xtemp;
    4330             :         double y_ori = ytemp;
    4331             :         int btemp = FALSE;
    4332             :         psATInfo->pfnBaseTransformer(psATInfo->pBaseCBData, bDstToSrc, 1,
    4333             :                                      &xtemp, &ytemp, &ztemp, &btemp);
    4334             : #endif
    4335   143357000 :         const double dfDist = (x[i] - x[0]);
    4336   143357000 :         x[i] = xSMETransformed[0] + dfDeltaX * dfDist;
    4337   143357000 :         y[i] = ySMETransformed[0] + dfDeltaY * dfDist;
    4338   143357000 :         z[i] = zSMETransformed[0] + dfDeltaZ * dfDist;
    4339             : #ifdef check_error
    4340             :         const double dfError2 = fabs(x[i] - xtemp) + fabs(y[i] - ytemp);
    4341             :         if (dfError2 > 4 /*10 * dfMaxError*/)
    4342             :         {
    4343             :             /*ok*/ printf("Error = %f on (%f, %f)\n", dfError2, x_ori, y_ori);
    4344             :         }
    4345             : #endif
    4346   143357000 :         panSuccess[i] = TRUE;
    4347             :     }
    4348             : 
    4349      708883 :     return TRUE;
    4350             : }
    4351             : 
    4352             : /************************************************************************/
    4353             : /*                        GDALApproxTransform()                         */
    4354             : /************************************************************************/
    4355             : 
    4356             : /**
    4357             :  * Perform approximate transformation.
    4358             :  *
    4359             :  * Actually performs the approximate transformation described in
    4360             :  * GDALCreateApproxTransformer().  This function matches the
    4361             :  * GDALTransformerFunc() signature.  Details of the arguments are described
    4362             :  * there.
    4363             :  */
    4364             : 
    4365      779334 : int GDALApproxTransform(void *pCBData, int bDstToSrc, int nPoints, double *x,
    4366             :                         double *y, double *z, int *panSuccess)
    4367             : 
    4368             : {
    4369      779334 :     GDALApproxTransformInfo *psATInfo =
    4370             :         static_cast<GDALApproxTransformInfo *>(pCBData);
    4371      779334 :     double x2[3] = {};
    4372      779334 :     double y2[3] = {};
    4373      779334 :     double z2[3] = {};
    4374      779334 :     int anSuccess2[3] = {};
    4375             :     int bSuccess;
    4376             : 
    4377      779334 :     const int nMiddle = (nPoints - 1) / 2;
    4378             : 
    4379             :     /* -------------------------------------------------------------------- */
    4380             :     /*      Bail if our preconditions are not met, or if error is not       */
    4381             :     /*      acceptable.                                                     */
    4382             :     /* -------------------------------------------------------------------- */
    4383      779334 :     int bRet = FALSE;
    4384      779334 :     if (y[0] != y[nPoints - 1] || y[0] != y[nMiddle] ||
    4385      769627 :         x[0] == x[nPoints - 1] || x[0] == x[nMiddle] ||
    4386      362159 :         (psATInfo->dfMaxErrorForward == 0.0 &&
    4387      362159 :          psATInfo->dfMaxErrorReverse == 0.0) ||
    4388             :         nPoints <= 5)
    4389             :     {
    4390      417555 :         bRet = psATInfo->pfnBaseTransformer(psATInfo->pBaseCBData, bDstToSrc,
    4391             :                                             nPoints, x, y, z, panSuccess);
    4392      417570 :         goto end;
    4393             :     }
    4394             : 
    4395             :     /* -------------------------------------------------------------------- */
    4396             :     /*      Transform first, last and middle point.                         */
    4397             :     /* -------------------------------------------------------------------- */
    4398      361779 :     x2[0] = x[0];
    4399      361779 :     y2[0] = y[0];
    4400      361779 :     z2[0] = z[0];
    4401      361779 :     x2[1] = x[nMiddle];
    4402      361779 :     y2[1] = y[nMiddle];
    4403      361779 :     z2[1] = z[nMiddle];
    4404      361779 :     x2[2] = x[nPoints - 1];
    4405      361779 :     y2[2] = y[nPoints - 1];
    4406      361779 :     z2[2] = z[nPoints - 1];
    4407             : 
    4408      361779 :     bSuccess = psATInfo->pfnBaseTransformer(psATInfo->pBaseCBData, bDstToSrc, 3,
    4409             :                                             x2, y2, z2, anSuccess2);
    4410      361783 :     if (!bSuccess || !anSuccess2[0] || !anSuccess2[1] || !anSuccess2[2])
    4411             :     {
    4412        5426 :         bRet = psATInfo->pfnBaseTransformer(psATInfo->pBaseCBData, bDstToSrc,
    4413             :                                             nPoints, x, y, z, panSuccess);
    4414        5416 :         goto end;
    4415             :     }
    4416             : 
    4417      356357 :     bRet = GDALApproxTransformInternal(pCBData, bDstToSrc, nPoints, x, y, z,
    4418             :                                        panSuccess, x2, y2, z2);
    4419             : 
    4420      779354 : end:
    4421             : #ifdef DEBUG_APPROX_TRANSFORMER
    4422             :     for (int i = 0; i < nPoints; i++)
    4423             :         fprintf(stderr, "[%d] (%.10f,%.10f) %d\n", /*ok*/
    4424             :                 i, x[i], y[i], panSuccess[i]);
    4425             : #endif
    4426             : 
    4427      779354 :     return bRet;
    4428             : }
    4429             : 
    4430             : /************************************************************************/
    4431             : /*                  GDALDeserializeApproxTransformer()                  */
    4432             : /************************************************************************/
    4433             : 
    4434         151 : static void *GDALDeserializeApproxTransformer(CPLXMLNode *psTree)
    4435             : 
    4436             : {
    4437         151 :     double dfMaxErrorForward = 0.25;
    4438         151 :     double dfMaxErrorReverse = 0.25;
    4439         151 :     const char *pszMaxError = CPLGetXMLValue(psTree, "MaxError", nullptr);
    4440         151 :     if (pszMaxError != nullptr)
    4441             :     {
    4442         149 :         dfMaxErrorForward = CPLAtof(pszMaxError);
    4443         149 :         dfMaxErrorReverse = dfMaxErrorForward;
    4444             :     }
    4445             :     const char *pszMaxErrorForward =
    4446         151 :         CPLGetXMLValue(psTree, "MaxErrorForward", nullptr);
    4447         151 :     if (pszMaxErrorForward != nullptr)
    4448             :     {
    4449           2 :         dfMaxErrorForward = CPLAtof(pszMaxErrorForward);
    4450             :     }
    4451             :     const char *pszMaxErrorReverse =
    4452         151 :         CPLGetXMLValue(psTree, "MaxErrorReverse", nullptr);
    4453         151 :     if (pszMaxErrorReverse != nullptr)
    4454             :     {
    4455           2 :         dfMaxErrorReverse = CPLAtof(pszMaxErrorReverse);
    4456             :     }
    4457             : 
    4458         151 :     GDALTransformerFunc pfnBaseTransform = nullptr;
    4459         151 :     void *pBaseCBData = nullptr;
    4460             : 
    4461         151 :     CPLXMLNode *psContainer = CPLGetXMLNode(psTree, "BaseTransformer");
    4462             : 
    4463         151 :     if (psContainer != nullptr && psContainer->psChild != nullptr)
    4464             :     {
    4465         151 :         GDALDeserializeTransformer(psContainer->psChild, &pfnBaseTransform,
    4466             :                                    &pBaseCBData);
    4467             :     }
    4468             : 
    4469         151 :     if (pfnBaseTransform == nullptr)
    4470             :     {
    4471           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4472             :                  "Cannot get base transform for approx transformer.");
    4473           0 :         return nullptr;
    4474             :     }
    4475             : 
    4476         151 :     void *pApproxCBData = GDALCreateApproxTransformer2(
    4477             :         pfnBaseTransform, pBaseCBData, dfMaxErrorForward, dfMaxErrorReverse);
    4478         151 :     GDALApproxTransformerOwnsSubtransformer(pApproxCBData, TRUE);
    4479             : 
    4480         151 :     return pApproxCBData;
    4481             : }
    4482             : 
    4483             : /************************************************************************/
    4484             : /*                 GDALTransformLonLatToDestApproxTransformer()         */
    4485             : /************************************************************************/
    4486             : 
    4487        2648 : int GDALTransformLonLatToDestApproxTransformer(void *hTransformArg,
    4488             :                                                double *pdfX, double *pdfY)
    4489             : {
    4490        2648 :     GDALApproxTransformInfo *psInfo =
    4491             :         static_cast<GDALApproxTransformInfo *>(hTransformArg);
    4492             : 
    4493        2648 :     if (GDALIsTransformer(psInfo->pBaseCBData,
    4494             :                           GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME))
    4495             :     {
    4496        2648 :         return GDALTransformLonLatToDestGenImgProjTransformer(
    4497        2648 :             psInfo->pBaseCBData, pdfX, pdfY);
    4498             :     }
    4499           0 :     return false;
    4500             : }
    4501             : 
    4502             : /************************************************************************/
    4503             : /*                       GDALApplyGeoTransform()                        */
    4504             : /************************************************************************/
    4505             : 
    4506             : /**
    4507             :  * Apply GeoTransform to x/y coordinate.
    4508             :  *
    4509             :  * Applies the following computation, converting a (pixel, line) coordinate
    4510             :  * into a georeferenced (geo_x, geo_y) location.
    4511             :  * \code{.c}
    4512             :  *  *pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
    4513             :  *                                 + dfLine  * padfGeoTransform[2];
    4514             :  *  *pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
    4515             :  *                                 + dfLine  * padfGeoTransform[5];
    4516             :  * \endcode
    4517             :  *
    4518             :  * @param padfGeoTransform Six coefficient GeoTransform to apply.
    4519             :  * @param dfPixel Input pixel position.
    4520             :  * @param dfLine Input line position.
    4521             :  * @param pdfGeoX output location where geo_x (easting/longitude)
    4522             :  * location is placed.
    4523             :  * @param pdfGeoY output location where geo_y (northing/latitude)
    4524             :  * location is placed.
    4525             :  */
    4526             : 
    4527      405329 : void CPL_STDCALL GDALApplyGeoTransform(const double *padfGeoTransform,
    4528             :                                        double dfPixel, double dfLine,
    4529             :                                        double *pdfGeoX, double *pdfGeoY)
    4530             : {
    4531      405329 :     *pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1] +
    4532      405329 :                dfLine * padfGeoTransform[2];
    4533      405329 :     *pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4] +
    4534      405329 :                dfLine * padfGeoTransform[5];
    4535      405329 : }
    4536             : 
    4537             : /************************************************************************/
    4538             : /*                        GDALInvGeoTransform()                         */
    4539             : /************************************************************************/
    4540             : 
    4541             : /**
    4542             :  * Invert Geotransform.
    4543             :  *
    4544             :  * This function will invert a standard 3x2 set of GeoTransform coefficients.
    4545             :  * This converts the equation from being pixel to geo to being geo to pixel.
    4546             :  *
    4547             :  * @param gt_in Input geotransform (six doubles - unaltered).
    4548             :  * @param gt_out Output geotransform (six doubles - updated).
    4549             :  *
    4550             :  * @return TRUE on success or FALSE if the equation is uninvertable.
    4551             :  */
    4552             : 
    4553        3777 : int CPL_STDCALL GDALInvGeoTransform(const double *gt_in, double *gt_out)
    4554             : 
    4555             : {
    4556             :     // Special case - no rotation - to avoid computing determinate
    4557             :     // and potential precision issues.
    4558        3777 :     if (gt_in[2] == 0.0 && gt_in[4] == 0.0 && gt_in[1] != 0.0 &&
    4559        3719 :         gt_in[5] != 0.0)
    4560             :     {
    4561             :         /*X = gt_in[0] + x * gt_in[1]
    4562             :           Y = gt_in[3] + y * gt_in[5]
    4563             :           -->
    4564             :           x = -gt_in[0] / gt_in[1] + (1 / gt_in[1]) * X
    4565             :           y = -gt_in[3] / gt_in[5] + (1 / gt_in[5]) * Y
    4566             :         */
    4567        3719 :         gt_out[0] = -gt_in[0] / gt_in[1];
    4568        3719 :         gt_out[1] = 1.0 / gt_in[1];
    4569        3719 :         gt_out[2] = 0.0;
    4570        3719 :         gt_out[3] = -gt_in[3] / gt_in[5];
    4571        3719 :         gt_out[4] = 0.0;
    4572        3719 :         gt_out[5] = 1.0 / gt_in[5];
    4573        3719 :         return 1;
    4574             :     }
    4575             : 
    4576             :     // Assume a 3rd row that is [1 0 0].
    4577             : 
    4578             :     // Compute determinate.
    4579             : 
    4580          58 :     const double det = gt_in[1] * gt_in[5] - gt_in[2] * gt_in[4];
    4581         116 :     const double magnitude = std::max(std::max(fabs(gt_in[1]), fabs(gt_in[2])),
    4582          58 :                                       std::max(fabs(gt_in[4]), fabs(gt_in[5])));
    4583             : 
    4584          58 :     if (fabs(det) <= 1e-10 * magnitude * magnitude)
    4585           9 :         return 0;
    4586             : 
    4587          49 :     const double inv_det = 1.0 / det;
    4588             : 
    4589             :     // Compute adjoint, and divide by determinate.
    4590             : 
    4591          49 :     gt_out[1] = gt_in[5] * inv_det;
    4592          49 :     gt_out[4] = -gt_in[4] * inv_det;
    4593             : 
    4594          49 :     gt_out[2] = -gt_in[2] * inv_det;
    4595          49 :     gt_out[5] = gt_in[1] * inv_det;
    4596             : 
    4597          49 :     gt_out[0] = (gt_in[2] * gt_in[3] - gt_in[0] * gt_in[5]) * inv_det;
    4598          49 :     gt_out[3] = (-gt_in[1] * gt_in[3] + gt_in[0] * gt_in[4]) * inv_det;
    4599             : 
    4600          49 :     return 1;
    4601             : }
    4602             : 
    4603             : /************************************************************************/
    4604             : /*                      GDALSerializeTransformer()                      */
    4605             : /************************************************************************/
    4606             : 
    4607         269 : CPLXMLNode *GDALSerializeTransformer(GDALTransformerFunc /* pfnFunc */,
    4608             :                                      void *pTransformArg)
    4609             : {
    4610         269 :     VALIDATE_POINTER1(pTransformArg, "GDALSerializeTransformer", nullptr);
    4611             : 
    4612         269 :     GDALTransformerInfo *psInfo =
    4613             :         static_cast<GDALTransformerInfo *>(pTransformArg);
    4614             : 
    4615         269 :     if (psInfo == nullptr || memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4616             :                                     strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4617             :     {
    4618           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4619             :                  "Attempt to serialize non-GTI2 transformer.");
    4620           0 :         return nullptr;
    4621             :     }
    4622         269 :     else if (psInfo->pfnSerialize == nullptr)
    4623             :     {
    4624           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4625             :                  "No serialization function available for this transformer.");
    4626           0 :         return nullptr;
    4627             :     }
    4628             : 
    4629         269 :     return psInfo->pfnSerialize(pTransformArg);
    4630             : }
    4631             : 
    4632             : /************************************************************************/
    4633             : /*                  GDALRegisterTransformDeserializer()                 */
    4634             : /************************************************************************/
    4635             : 
    4636             : static CPLList *psListDeserializer = nullptr;
    4637             : static CPLMutex *hDeserializerMutex = nullptr;
    4638             : 
    4639             : typedef struct
    4640             : {
    4641             :     char *pszTransformName;
    4642             :     GDALTransformerFunc pfnTransformerFunc;
    4643             :     GDALTransformDeserializeFunc pfnDeserializeFunc;
    4644             : } TransformDeserializerInfo;
    4645             : 
    4646           0 : void *GDALRegisterTransformDeserializer(
    4647             :     const char *pszTransformName, GDALTransformerFunc pfnTransformerFunc,
    4648             :     GDALTransformDeserializeFunc pfnDeserializeFunc)
    4649             : {
    4650             :     TransformDeserializerInfo *psInfo =
    4651             :         static_cast<TransformDeserializerInfo *>(
    4652           0 :             CPLMalloc(sizeof(TransformDeserializerInfo)));
    4653           0 :     psInfo->pszTransformName = CPLStrdup(pszTransformName);
    4654           0 :     psInfo->pfnTransformerFunc = pfnTransformerFunc;
    4655           0 :     psInfo->pfnDeserializeFunc = pfnDeserializeFunc;
    4656             : 
    4657           0 :     CPLMutexHolderD(&hDeserializerMutex);
    4658           0 :     psListDeserializer = CPLListInsert(psListDeserializer, psInfo, 0);
    4659             : 
    4660           0 :     return psInfo;
    4661             : }
    4662             : 
    4663             : /************************************************************************/
    4664             : /*                GDALUnregisterTransformDeserializer()                 */
    4665             : /************************************************************************/
    4666             : 
    4667           0 : void GDALUnregisterTransformDeserializer(void *pData)
    4668             : {
    4669           0 :     CPLMutexHolderD(&hDeserializerMutex);
    4670           0 :     CPLList *psList = psListDeserializer;
    4671           0 :     CPLList *psLast = nullptr;
    4672           0 :     while (psList)
    4673             :     {
    4674           0 :         if (psList->pData == pData)
    4675             :         {
    4676           0 :             TransformDeserializerInfo *psInfo =
    4677             :                 static_cast<TransformDeserializerInfo *>(pData);
    4678           0 :             CPLFree(psInfo->pszTransformName);
    4679           0 :             CPLFree(pData);
    4680           0 :             if (psLast)
    4681           0 :                 psLast->psNext = psList->psNext;
    4682             :             else
    4683           0 :                 psListDeserializer = nullptr;
    4684           0 :             CPLFree(psList);
    4685           0 :             break;
    4686             :         }
    4687           0 :         psLast = psList;
    4688           0 :         psList = psList->psNext;
    4689             :     }
    4690           0 : }
    4691             : 
    4692             : /************************************************************************/
    4693             : /*                GDALUnregisterTransformDeserializer()                 */
    4694             : /************************************************************************/
    4695             : 
    4696        1123 : void GDALCleanupTransformDeserializerMutex()
    4697             : {
    4698        1123 :     if (hDeserializerMutex != nullptr)
    4699             :     {
    4700           0 :         CPLDestroyMutex(hDeserializerMutex);
    4701           0 :         hDeserializerMutex = nullptr;
    4702             :     }
    4703        1123 : }
    4704             : 
    4705             : /************************************************************************/
    4706             : /*                     GDALDeserializeTransformer()                     */
    4707             : /************************************************************************/
    4708             : 
    4709         568 : CPLErr GDALDeserializeTransformer(CPLXMLNode *psTree,
    4710             :                                   GDALTransformerFunc *ppfnFunc,
    4711             :                                   void **ppTransformArg)
    4712             : 
    4713             : {
    4714         568 :     *ppfnFunc = nullptr;
    4715         568 :     *ppTransformArg = nullptr;
    4716             : 
    4717         568 :     CPLErrorReset();
    4718             : 
    4719         568 :     if (psTree == nullptr || psTree->eType != CXT_Element)
    4720           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4721             :                  "Malformed element in GDALDeserializeTransformer");
    4722         568 :     else if (EQUAL(psTree->pszValue, "GenImgProjTransformer"))
    4723             :     {
    4724         199 :         *ppfnFunc = GDALGenImgProjTransform;
    4725         199 :         *ppTransformArg = GDALDeserializeGenImgProjTransformer(psTree);
    4726             :     }
    4727         369 :     else if (EQUAL(psTree->pszValue, "ReprojectionTransformer"))
    4728             :     {
    4729         202 :         *ppfnFunc = GDALReprojectionTransform;
    4730         202 :         *ppTransformArg = GDALDeserializeReprojectionTransformer(psTree);
    4731             :     }
    4732         167 :     else if (EQUAL(psTree->pszValue, "GCPTransformer"))
    4733             :     {
    4734           5 :         *ppfnFunc = GDALGCPTransform;
    4735           5 :         *ppTransformArg = GDALDeserializeGCPTransformer(psTree);
    4736             :     }
    4737         162 :     else if (EQUAL(psTree->pszValue, "TPSTransformer"))
    4738             :     {
    4739           3 :         *ppfnFunc = GDALTPSTransform;
    4740           3 :         *ppTransformArg = GDALDeserializeTPSTransformer(psTree);
    4741             :     }
    4742         159 :     else if (EQUAL(psTree->pszValue, "GeoLocTransformer"))
    4743             :     {
    4744           1 :         *ppfnFunc = GDALGeoLocTransform;
    4745           1 :         *ppTransformArg = GDALDeserializeGeoLocTransformer(psTree);
    4746             :     }
    4747         158 :     else if (EQUAL(psTree->pszValue, "RPCTransformer"))
    4748             :     {
    4749           0 :         *ppfnFunc = GDALRPCTransform;
    4750           0 :         *ppTransformArg = GDALDeserializeRPCTransformer(psTree);
    4751             :     }
    4752         158 :     else if (EQUAL(psTree->pszValue, "ApproxTransformer"))
    4753             :     {
    4754         151 :         *ppfnFunc = GDALApproxTransform;
    4755         151 :         *ppTransformArg = GDALDeserializeApproxTransformer(psTree);
    4756             :     }
    4757           7 :     else if (EQUAL(psTree->pszValue, "HomographyTransformer"))
    4758             :     {
    4759           7 :         *ppfnFunc = GDALHomographyTransform;
    4760           7 :         *ppTransformArg = GDALDeserializeHomographyTransformer(psTree);
    4761             :     }
    4762             :     else
    4763             :     {
    4764           0 :         GDALTransformDeserializeFunc pfnDeserializeFunc = nullptr;
    4765             :         {
    4766           0 :             CPLMutexHolderD(&hDeserializerMutex);
    4767           0 :             CPLList *psList = psListDeserializer;
    4768           0 :             while (psList)
    4769             :             {
    4770           0 :                 TransformDeserializerInfo *psInfo =
    4771             :                     static_cast<TransformDeserializerInfo *>(psList->pData);
    4772           0 :                 if (strcmp(psInfo->pszTransformName, psTree->pszValue) == 0)
    4773             :                 {
    4774           0 :                     *ppfnFunc = psInfo->pfnTransformerFunc;
    4775           0 :                     pfnDeserializeFunc = psInfo->pfnDeserializeFunc;
    4776           0 :                     break;
    4777             :                 }
    4778           0 :                 psList = psList->psNext;
    4779             :             }
    4780             :         }
    4781             : 
    4782           0 :         if (pfnDeserializeFunc != nullptr)
    4783             :         {
    4784           0 :             *ppTransformArg = pfnDeserializeFunc(psTree);
    4785             :         }
    4786             :         else
    4787             :         {
    4788           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    4789             :                      "Unrecognized element '%s' GDALDeserializeTransformer",
    4790             :                      psTree->pszValue);
    4791             :         }
    4792             :     }
    4793             : 
    4794         568 :     return CPLGetLastErrorType();
    4795             : }
    4796             : 
    4797             : /************************************************************************/
    4798             : /*                       GDALDestroyTransformer()                       */
    4799             : /************************************************************************/
    4800             : 
    4801        4726 : void GDALDestroyTransformer(void *pTransformArg)
    4802             : 
    4803             : {
    4804        4726 :     if (pTransformArg == nullptr)
    4805           0 :         return;
    4806             : 
    4807        4726 :     GDALTransformerInfo *psInfo =
    4808             :         static_cast<GDALTransformerInfo *>(pTransformArg);
    4809             : 
    4810        4726 :     if (memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4811             :                strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4812             :     {
    4813           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4814             :                  "Attempt to destroy non-GTI2 transformer.");
    4815           0 :         return;
    4816             :     }
    4817             : 
    4818        4726 :     psInfo->pfnCleanup(pTransformArg);
    4819             : }
    4820             : 
    4821             : /************************************************************************/
    4822             : /*                         GDALUseTransformer()                         */
    4823             : /************************************************************************/
    4824             : 
    4825        8683 : int GDALUseTransformer(void *pTransformArg, int bDstToSrc, int nPointCount,
    4826             :                        double *x, double *y, double *z, int *panSuccess)
    4827             : {
    4828        8683 :     GDALTransformerInfo *psInfo =
    4829             :         static_cast<GDALTransformerInfo *>(pTransformArg);
    4830             : 
    4831        8683 :     if (psInfo == nullptr || memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4832             :                                     strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4833             :     {
    4834           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4835             :                  "Attempt to use non-GTI2 transformer.");
    4836           0 :         return FALSE;
    4837             :     }
    4838             : 
    4839        8683 :     return psInfo->pfnTransform(pTransformArg, bDstToSrc, nPointCount, x, y, z,
    4840        8683 :                                 panSuccess);
    4841             : }
    4842             : 
    4843             : /************************************************************************/
    4844             : /*                        GDALCloneTransformer()                        */
    4845             : /************************************************************************/
    4846             : 
    4847          71 : void *GDALCloneTransformer(void *pTransformArg)
    4848             : {
    4849          71 :     GDALTransformerInfo *psInfo =
    4850             :         static_cast<GDALTransformerInfo *>(pTransformArg);
    4851             : 
    4852          71 :     if (psInfo == nullptr || memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4853             :                                     strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4854             :     {
    4855           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4856             :                  "Attempt to clone non-GTI2 transformer.");
    4857           0 :         return nullptr;
    4858             :     }
    4859             : 
    4860          71 :     if (psInfo->pfnCreateSimilar != nullptr)
    4861             :     {
    4862          36 :         return psInfo->pfnCreateSimilar(psInfo, 1.0, 1.0);
    4863             :     }
    4864             : 
    4865          35 :     if (psInfo->pfnSerialize == nullptr)
    4866             :     {
    4867           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4868             :                  "No serialization function available for this transformer.");
    4869           0 :         return nullptr;
    4870             :     }
    4871             : 
    4872          35 :     CPLXMLNode *pSerialized = psInfo->pfnSerialize(pTransformArg);
    4873          35 :     if (pSerialized == nullptr)
    4874           0 :         return nullptr;
    4875          35 :     GDALTransformerFunc pfnTransformer = nullptr;
    4876          35 :     void *pClonedTransformArg = nullptr;
    4877          35 :     if (GDALDeserializeTransformer(pSerialized, &pfnTransformer,
    4878          35 :                                    &pClonedTransformArg) != CE_None)
    4879             :     {
    4880           0 :         CPLDestroyXMLNode(pSerialized);
    4881           0 :         CPLFree(pClonedTransformArg);
    4882           0 :         return nullptr;
    4883             :     }
    4884             : 
    4885          35 :     CPLDestroyXMLNode(pSerialized);
    4886          35 :     return pClonedTransformArg;
    4887             : }
    4888             : 
    4889             : /************************************************************************/
    4890             : /*                   GDALCreateSimilarTransformer()                     */
    4891             : /************************************************************************/
    4892             : 
    4893          68 : void *GDALCreateSimilarTransformer(void *pTransformArg, double dfRatioX,
    4894             :                                    double dfRatioY)
    4895             : {
    4896          68 :     GDALTransformerInfo *psInfo =
    4897             :         static_cast<GDALTransformerInfo *>(pTransformArg);
    4898             : 
    4899          68 :     if (psInfo == nullptr || memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4900             :                                     strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4901             :     {
    4902           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4903             :                  "Attempt to call CreateSimilar on a non-GTI2 transformer.");
    4904           0 :         return nullptr;
    4905             :     }
    4906             : 
    4907          68 :     if (psInfo->pfnCreateSimilar == nullptr)
    4908             :     {
    4909           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4910             :                  "No CreateSimilar function available for this transformer.");
    4911           0 :         return nullptr;
    4912             :     }
    4913             : 
    4914          68 :     return psInfo->pfnCreateSimilar(psInfo, dfRatioX, dfRatioY);
    4915             : }
    4916             : 
    4917             : /************************************************************************/
    4918             : /*                      GetGenImgProjTransformInfo()                    */
    4919             : /************************************************************************/
    4920             : 
    4921          44 : static GDALTransformerInfo *GetGenImgProjTransformInfo(const char *pszFunc,
    4922             :                                                        void *pTransformArg)
    4923             : {
    4924          44 :     GDALTransformerInfo *psInfo =
    4925             :         static_cast<GDALTransformerInfo *>(pTransformArg);
    4926             : 
    4927          44 :     if (psInfo == nullptr || memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4928             :                                     strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4929             :     {
    4930           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    4931             :                  "Attempt to call %s on "
    4932             :                  "a non-GTI2 transformer.",
    4933             :                  pszFunc);
    4934           0 :         return nullptr;
    4935             :     }
    4936             : 
    4937          44 :     if (EQUAL(psInfo->pszClassName, GDAL_APPROX_TRANSFORMER_CLASS_NAME))
    4938             :     {
    4939          14 :         GDALApproxTransformInfo *psATInfo =
    4940             :             static_cast<GDALApproxTransformInfo *>(pTransformArg);
    4941          14 :         psInfo = static_cast<GDALTransformerInfo *>(psATInfo->pBaseCBData);
    4942             : 
    4943          14 :         if (psInfo == nullptr ||
    4944          14 :             memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
    4945             :                    strlen(GDAL_GTI2_SIGNATURE)) != 0)
    4946             :         {
    4947           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    4948             :                      "Attempt to call %s on "
    4949             :                      "a non-GTI2 transformer.",
    4950             :                      pszFunc);
    4951           0 :             return nullptr;
    4952             :         }
    4953             :     }
    4954             : 
    4955          44 :     if (EQUAL(psInfo->pszClassName, GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME))
    4956             :     {
    4957          44 :         return psInfo;
    4958             :     }
    4959             : 
    4960           0 :     return nullptr;
    4961             : }
    4962             : 
    4963             : /************************************************************************/
    4964             : /*                 GDALSetTransformerDstGeoTransform()                  */
    4965             : /************************************************************************/
    4966             : 
    4967             : /**
    4968             :  * Set ApproxTransformer or GenImgProj output geotransform.
    4969             :  *
    4970             :  * This is a layer above GDALSetGenImgProjTransformerDstGeoTransform() that
    4971             :  * checks that the passed hTransformArg is compatible.
    4972             :  *
    4973             :  * Normally the "destination geotransform", or transformation between
    4974             :  * georeferenced output coordinates and pixel/line coordinates on the
    4975             :  * destination file is extracted from the destination file by
    4976             :  * GDALCreateGenImgProjTransformer() and stored in the GenImgProj private
    4977             :  * info.  However, sometimes it is inconvenient to have an output file
    4978             :  * handle with appropriate geotransform information when creating the
    4979             :  * transformation.  For these cases, this function can be used to apply
    4980             :  * the destination geotransform.
    4981             :  *
    4982             :  * @param pTransformArg the handle to update.
    4983             :  * @param padfGeoTransform the destination geotransform to apply (six doubles).
    4984             :  */
    4985             : 
    4986          22 : void GDALSetTransformerDstGeoTransform(void *pTransformArg,
    4987             :                                        const double *padfGeoTransform)
    4988             : {
    4989          22 :     VALIDATE_POINTER0(pTransformArg, "GDALSetTransformerDstGeoTransform");
    4990             : 
    4991          22 :     GDALTransformerInfo *psInfo = GetGenImgProjTransformInfo(
    4992             :         "GDALSetTransformerDstGeoTransform", pTransformArg);
    4993          22 :     if (psInfo)
    4994             :     {
    4995          22 :         GDALSetGenImgProjTransformerDstGeoTransform(psInfo, padfGeoTransform);
    4996             :     }
    4997             : }
    4998             : 
    4999             : /************************************************************************/
    5000             : /*                 GDALGetTransformerDstGeoTransform()                  */
    5001             : /************************************************************************/
    5002             : 
    5003             : /**
    5004             :  * Get ApproxTransformer or GenImgProj output geotransform.
    5005             :  *
    5006             :  * @param pTransformArg transformer handle.
    5007             :  * @param padfGeoTransform (output) the destination geotransform to return (six
    5008             :  * doubles).
    5009             :  */
    5010             : 
    5011          22 : void GDALGetTransformerDstGeoTransform(void *pTransformArg,
    5012             :                                        double *padfGeoTransform)
    5013             : {
    5014          22 :     VALIDATE_POINTER0(pTransformArg, "GDALGetTransformerDstGeoTransform");
    5015             : 
    5016          22 :     GDALTransformerInfo *psInfo = GetGenImgProjTransformInfo(
    5017             :         "GDALGetTransformerDstGeoTransform", pTransformArg);
    5018          22 :     if (psInfo)
    5019             :     {
    5020          22 :         GDALGenImgProjTransformInfo *psGenImgProjInfo =
    5021             :             reinterpret_cast<GDALGenImgProjTransformInfo *>(psInfo);
    5022             : 
    5023          22 :         memcpy(padfGeoTransform, psGenImgProjInfo->sDstParams.adfGeoTransform,
    5024             :                sizeof(double) * 6);
    5025             :     }
    5026             : }
    5027             : 
    5028             : /************************************************************************/
    5029             : /*            GDALTransformIsTranslationOnPixelBoundaries()             */
    5030             : /************************************************************************/
    5031             : 
    5032        1654 : bool GDALTransformIsTranslationOnPixelBoundaries(GDALTransformerFunc,
    5033             :                                                  void *pTransformerArg)
    5034             : {
    5035        1654 :     if (GDALIsTransformer(pTransformerArg, GDAL_APPROX_TRANSFORMER_CLASS_NAME))
    5036             :     {
    5037        1324 :         const auto *pApproxInfo =
    5038             :             static_cast<const GDALApproxTransformInfo *>(pTransformerArg);
    5039        1324 :         pTransformerArg = pApproxInfo->pBaseCBData;
    5040             :     }
    5041        1654 :     if (GDALIsTransformer(pTransformerArg, GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME))
    5042             :     {
    5043        1501 :         const auto *pGenImgpProjInfo =
    5044             :             static_cast<GDALGenImgProjTransformInfo *>(pTransformerArg);
    5045         486 :         const auto IsCloseToInteger = [](double dfVal)
    5046         486 :         { return std::fabs(dfVal - std::round(dfVal)) <= 1e-6; };
    5047        2922 :         return pGenImgpProjInfo->sSrcParams.pTransformArg == nullptr &&
    5048        1421 :                pGenImgpProjInfo->sDstParams.pTransformArg == nullptr &&
    5049        1419 :                pGenImgpProjInfo->pReproject == nullptr &&
    5050         571 :                pGenImgpProjInfo->sSrcParams.adfGeoTransform[1] ==
    5051         571 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[1] &&
    5052         298 :                pGenImgpProjInfo->sSrcParams.adfGeoTransform[5] ==
    5053         298 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[5] &&
    5054         261 :                pGenImgpProjInfo->sSrcParams.adfGeoTransform[2] ==
    5055         261 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[2] &&
    5056         261 :                pGenImgpProjInfo->sSrcParams.adfGeoTransform[4] ==
    5057         522 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[4] &&
    5058             :                // Check that the georeferenced origin of the destination
    5059             :                // geotransform is close to be an integer value when transformed
    5060             :                // to source image coordinates
    5061         261 :                IsCloseToInteger(
    5062         261 :                    pGenImgpProjInfo->sSrcParams.adfInvGeoTransform[0] +
    5063         261 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[0] *
    5064         261 :                        pGenImgpProjInfo->sSrcParams.adfInvGeoTransform[1] +
    5065         261 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[3] *
    5066        3183 :                        pGenImgpProjInfo->sSrcParams.adfInvGeoTransform[2]) &&
    5067         225 :                IsCloseToInteger(
    5068         225 :                    pGenImgpProjInfo->sSrcParams.adfInvGeoTransform[3] +
    5069         225 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[0] *
    5070         225 :                        pGenImgpProjInfo->sSrcParams.adfInvGeoTransform[4] +
    5071         225 :                    pGenImgpProjInfo->sDstParams.adfGeoTransform[3] *
    5072        1726 :                        pGenImgpProjInfo->sSrcParams.adfInvGeoTransform[5]);
    5073             :     }
    5074         153 :     return false;
    5075             : }
    5076             : 
    5077             : /************************************************************************/
    5078             : /*                   GDALTransformIsAffineNoRotation()                  */
    5079             : /************************************************************************/
    5080             : 
    5081          18 : bool GDALTransformIsAffineNoRotation(GDALTransformerFunc, void *pTransformerArg)
    5082             : {
    5083          18 :     if (GDALIsTransformer(pTransformerArg, GDAL_APPROX_TRANSFORMER_CLASS_NAME))
    5084             :     {
    5085          18 :         const auto *pApproxInfo =
    5086             :             static_cast<const GDALApproxTransformInfo *>(pTransformerArg);
    5087          18 :         pTransformerArg = pApproxInfo->pBaseCBData;
    5088             :     }
    5089          18 :     if (GDALIsTransformer(pTransformerArg, GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME))
    5090             :     {
    5091          18 :         const auto *pGenImgpProjInfo =
    5092             :             static_cast<GDALGenImgProjTransformInfo *>(pTransformerArg);
    5093          36 :         return pGenImgpProjInfo->sSrcParams.pTransformArg == nullptr &&
    5094          18 :                pGenImgpProjInfo->sDstParams.pTransformArg == nullptr &&
    5095          18 :                pGenImgpProjInfo->pReproject == nullptr &&
    5096           8 :                pGenImgpProjInfo->sSrcParams.adfGeoTransform[2] == 0 &&
    5097           8 :                pGenImgpProjInfo->sSrcParams.adfGeoTransform[4] == 0 &&
    5098          44 :                pGenImgpProjInfo->sDstParams.adfGeoTransform[2] == 0 &&
    5099          26 :                pGenImgpProjInfo->sDstParams.adfGeoTransform[4] == 0;
    5100             :     }
    5101           0 :     return false;
    5102             : }
    5103             : 
    5104             : /************************************************************************/
    5105             : /*                        GDALTransformHasFastClone()                   */
    5106             : /************************************************************************/
    5107             : 
    5108             : /** Returns whether GDALCloneTransformer() on this transformer is
    5109             :  * "fast"
    5110             :  * Counter-examples are GCPs or TPSs transformers.
    5111             :  */
    5112           2 : bool GDALTransformHasFastClone(void *pTransformerArg)
    5113             : {
    5114           2 :     if (GDALIsTransformer(pTransformerArg, GDAL_APPROX_TRANSFORMER_CLASS_NAME))
    5115             :     {
    5116           1 :         const auto *pApproxInfo =
    5117             :             static_cast<const GDALApproxTransformInfo *>(pTransformerArg);
    5118           1 :         pTransformerArg = pApproxInfo->pBaseCBData;
    5119             :         // Fallback to next lines
    5120             :     }
    5121             : 
    5122           2 :     if (GDALIsTransformer(pTransformerArg, GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME))
    5123             :     {
    5124           2 :         const auto *pGenImgpProjInfo =
    5125             :             static_cast<GDALGenImgProjTransformInfo *>(pTransformerArg);
    5126           2 :         return (pGenImgpProjInfo->sSrcParams.pTransformArg == nullptr ||
    5127           0 :                 GDALTransformHasFastClone(
    5128           4 :                     pGenImgpProjInfo->sSrcParams.pTransformArg)) &&
    5129           2 :                (pGenImgpProjInfo->sDstParams.pTransformArg == nullptr ||
    5130           0 :                 GDALTransformHasFastClone(
    5131           2 :                     pGenImgpProjInfo->sDstParams.pTransformArg));
    5132             :     }
    5133           0 :     else if (GDALIsTransformer(pTransformerArg,
    5134             :                                GDAL_RPC_TRANSFORMER_CLASS_NAME))
    5135             :     {
    5136           0 :         return true;
    5137             :     }
    5138             :     else
    5139             :     {
    5140           0 :         return false;
    5141             :     }
    5142             : }

Generated by: LCOV version 1.14