Coverage Report

Created: 2025-08-28 06:57

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