-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathgdaltransformer.cpp
4970 lines (4413 loc) · 193 KB
/
gdaltransformer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: Mapinfo Image Warper
* Purpose: Implementation of one or more GDALTrasformerFunc types, including
* the GenImgProj (general image reprojector) transformer.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2002, i3 - information integration and imaging
* Fort Collin, CO
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
* Copyright (c) 2021, CLS
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_alg.h"
#include "gdal_alg_priv.h"
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <limits>
#include <utility>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_list.h"
#include "cpl_minixml.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "gdal.h"
#include "gdal_priv.h"
#include "ogr_core.h"
#include "ogr_spatialref.h"
#include "ogr_srs_api.h"
CPL_C_START
void *GDALDeserializeGCPTransformer(CPLXMLNode *psTree);
void *GDALDeserializeTPSTransformer(CPLXMLNode *psTree);
void *GDALDeserializeGeoLocTransformer(CPLXMLNode *psTree);
void *GDALDeserializeRPCTransformer(CPLXMLNode *psTree);
CPL_C_END
static CPLXMLNode *GDALSerializeReprojectionTransformer(void *pTransformArg);
static void *GDALDeserializeReprojectionTransformer(CPLXMLNode *psTree);
static CPLXMLNode *GDALSerializeGenImgProjTransformer(void *pTransformArg);
static void *GDALDeserializeGenImgProjTransformer(CPLXMLNode *psTree);
static void *GDALCreateApproxTransformer2(GDALTransformerFunc pfnRawTransformer,
void *pRawTransformerArg,
double dfMaxErrorForward,
double dfMaxErrorReverse);
/************************************************************************/
/* GDALIsTransformer() */
/************************************************************************/
bool GDALIsTransformer(void *hTransformerArg, const char *pszClassName)
{
if (!hTransformerArg)
return false;
// All transformers should have a GDALTransformerInfo member as their first members
GDALTransformerInfo *psInfo =
static_cast<GDALTransformerInfo *>(hTransformerArg);
return memcmp(psInfo->abySignature, GDAL_GTI2_SIGNATURE,
strlen(GDAL_GTI2_SIGNATURE)) == 0 &&
strcmp(psInfo->pszClassName, pszClassName) == 0;
}
/************************************************************************/
/* GDALTransformFunc */
/* */
/* Documentation for GDALTransformFunc typedef. */
/************************************************************************/
/*!
\typedef typedef int (*GDALTransformerFunc)( void *pTransformerArg, int
bDstToSrc, int nPointCount, double *x, double *y, double *z, int *panSuccess );
Generic signature for spatial point transformers.
This function signature is used for a variety of functions that accept
passed in functions used to transform point locations between two coordinate
spaces.
The GDALCreateGenImgProjTransformer(), GDALCreateReprojectionTransformerEx(),
GDALCreateGCPTransformer() and GDALCreateApproxTransformer() functions can
be used to prepare argument data for some built-in transformers. As well,
applications can implement their own transformers to the following signature.
\code
typedef int
(*GDALTransformerFunc)( void *pTransformerArg,
int bDstToSrc, int nPointCount,
double *x, double *y, double *z, int *panSuccess );
\endcode
@param pTransformerArg application supplied callback data used by the
transformer.
@param bDstToSrc if TRUE the transformation will be from the destination
coordinate space to the source coordinate system, otherwise the transformation
will be from the source coordinate system to the destination coordinate system.
@param nPointCount number of points in the x, y and z arrays.
@param x input X coordinates. Results returned in same array.
@param y input Y coordinates. Results returned in same array.
@param z input Z coordinates. Results returned in same array.
@param panSuccess array of ints in which success (TRUE) or failure (FALSE)
flags are returned for the translation of each point.
@return TRUE if the overall transformation succeeds (though some individual
points may have failed) or FALSE if the overall transformation fails.
*/
/************************************************************************/
/* GDALSuggestedWarpOutput() */
/************************************************************************/
/**
* Suggest output file size.
*
* This function is used to suggest the size, and georeferenced extents
* appropriate given the indicated transformation and input file. It walks
* the edges of the input file (approximately 20 sample points along each
* edge) transforming into output coordinates in order to get an extents box.
*
* Then a resolution is computed with the intent that the length of the
* distance from the top left corner of the output imagery to the bottom right
* corner would represent the same number of pixels as in the source image.
* Note that if the image is somewhat rotated the diagonal taken isn't of the
* whole output bounding rectangle, but instead of the locations where the
* top/left and bottom/right corners transform. The output pixel size is
* always square. This is intended to approximately preserve the resolution
* of the input data in the output file.
*
* The values returned in padfGeoTransformOut, pnPixels and pnLines are
* the suggested number of pixels and lines for the output file, and the
* geotransform relating those pixels to the output georeferenced coordinates.
*
* The trickiest part of using the function is ensuring that the
* transformer created is from source file pixel/line coordinates to
* output file georeferenced coordinates. This can be accomplished with
* GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.
*
* @param hSrcDS the input image (it is assumed the whole input image is
* being transformed).
* @param pfnTransformer the transformer function.
* @param pTransformArg the callback data for the transformer function.
* @param padfGeoTransformOut the array of six doubles in which the suggested
* geotransform is returned.
* @param pnPixels int in which the suggest pixel width of output is returned.
* @param pnLines int in which the suggest pixel height of output is returned.
*
* @return CE_None if successful or CE_Failure otherwise.
*/
CPLErr CPL_STDCALL GDALSuggestedWarpOutput(GDALDatasetH hSrcDS,
GDALTransformerFunc pfnTransformer,
void *pTransformArg,
double *padfGeoTransformOut,
int *pnPixels, int *pnLines)
{
VALIDATE_POINTER1(hSrcDS, "GDALSuggestedWarpOutput", CE_Failure);
double adfExtent[4] = {};
return GDALSuggestedWarpOutput2(hSrcDS, pfnTransformer, pTransformArg,
padfGeoTransformOut, pnPixels, pnLines,
adfExtent, 0);
}
static bool GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
GDALTransformerFunc pfnTransformer, void *pTransformArg, double *padfExtent,
int /* nPixels*/, int nLines, double dfPixelSizeX, double dfPixelSizeY)
{
double adfX[21] = {};
double adfY[21] = {};
const double dfMaxXOut = padfExtent[2];
const double dfMaxYOut = padfExtent[3];
// Take 20 steps.
int nSamplePoints = 0;
for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
{
// Ensure we end exactly at the end.
if (dfRatio > 0.99)
dfRatio = 1.0;
// Along right.
adfX[nSamplePoints] = dfMaxXOut;
adfY[nSamplePoints] = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
nSamplePoints++;
}
double adfZ[21] = {};
int abSuccess[21] = {};
pfnTransformer(pTransformArg, TRUE, nSamplePoints, adfX, adfY, adfZ,
abSuccess);
int abSuccess2[21] = {};
pfnTransformer(pTransformArg, FALSE, nSamplePoints, adfX, adfY, adfZ,
abSuccess2);
nSamplePoints = 0;
int nBadCount = 0;
for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
{
const double expected_x = dfMaxXOut;
const double expected_y = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
if (!abSuccess[nSamplePoints] || !abSuccess2[nSamplePoints] ||
fabs(adfX[nSamplePoints] - expected_x) > dfPixelSizeX ||
fabs(adfY[nSamplePoints] - expected_y) > dfPixelSizeY)
{
nBadCount++;
}
nSamplePoints++;
}
return nBadCount == nSamplePoints;
}
static bool GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
GDALTransformerFunc pfnTransformer, void *pTransformArg, double *padfExtent,
int nPixels, int /* nLines */, double dfPixelSizeX, double dfPixelSizeY)
{
double adfX[21] = {};
double adfY[21] = {};
const double dfMinXOut = padfExtent[0];
const double dfMinYOut = padfExtent[1];
// Take 20 steps.
int nSamplePoints = 0;
for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
{
// Ensure we end exactly at the end.
if (dfRatio > 0.99)
dfRatio = 1.0;
// Along right.
adfX[nSamplePoints] = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
adfY[nSamplePoints] = dfMinYOut;
nSamplePoints++;
}
double adfZ[21] = {};
int abSuccess[21] = {};
pfnTransformer(pTransformArg, TRUE, nSamplePoints, adfX, adfY, adfZ,
abSuccess);
int abSuccess2[21] = {};
pfnTransformer(pTransformArg, FALSE, nSamplePoints, adfX, adfY, adfZ,
abSuccess2);
nSamplePoints = 0;
int nBadCount = 0;
for (double dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05)
{
const double expected_x = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
const double expected_y = dfMinYOut;
if (!abSuccess[nSamplePoints] || !abSuccess2[nSamplePoints] ||
fabs(adfX[nSamplePoints] - expected_x) > dfPixelSizeX ||
fabs(adfY[nSamplePoints] - expected_y) > dfPixelSizeY)
{
nBadCount++;
}
nSamplePoints++;
}
return nBadCount == nSamplePoints;
}
/************************************************************************/
/* GDALSuggestedWarpOutput2() */
/************************************************************************/
/**
* Suggest output file size.
*
* This function is used to suggest the size, and georeferenced extents
* appropriate given the indicated transformation and input file. It walks
* the edges of the input file (approximately 20 sample points along each
* edge) transforming into output coordinates in order to get an extents box.
*
* Then a resolution is computed with the intent that the length of the
* distance from the top left corner of the output imagery to the bottom right
* corner would represent the same number of pixels as in the source image.
* Note that if the image is somewhat rotated the diagonal taken isn't of the
* whole output bounding rectangle, but instead of the locations where the
* top/left and bottom/right corners transform. The output pixel size is
* always square. This is intended to approximately preserve the resolution
* of the input data in the output file.
*
* The values returned in padfGeoTransformOut, pnPixels and pnLines are
* the suggested number of pixels and lines for the output file, and the
* geotransform relating those pixels to the output georeferenced coordinates.
*
* The trickiest part of using the function is ensuring that the
* transformer created is from source file pixel/line coordinates to
* output file georeferenced coordinates. This can be accomplished with
* GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.
*
* @param hSrcDS the input image (it is assumed the whole input image is
* being transformed).
* @param pfnTransformer the transformer function.
* @param pTransformArg the callback data for the transformer function.
* @param padfGeoTransformOut the array of six doubles in which the suggested
* geotransform is returned.
* @param pnPixels int in which the suggest pixel width of output is returned.
* @param pnLines int in which the suggest pixel height of output is returned.
* @param padfExtent Four entry array to return extents as (xmin, ymin, xmax,
* ymax).
* @param nOptions Options flags. Zero or GDAL_SWO_ROUND_UP_SIZE to ask *pnPixels
* and *pnLines to be rounded up instead of being rounded to the closes integer, or
* GDAL_SWO_FORCE_SQUARE_PIXEL to indicate that the generated pixel size is a square.
*
* @return CE_None if successful or CE_Failure otherwise.
*/
CPLErr CPL_STDCALL GDALSuggestedWarpOutput2(GDALDatasetH hSrcDS,
GDALTransformerFunc pfnTransformer,
void *pTransformArg,
double *padfGeoTransformOut,
int *pnPixels, int *pnLines,
double *padfExtent, int nOptions)
{
VALIDATE_POINTER1(hSrcDS, "GDALSuggestedWarpOutput2", CE_Failure);
const bool bIsGDALGenImgProjTransform{
pTransformArg &&
GDALIsTransformer(pTransformArg, GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME)};
/* -------------------------------------------------------------------- */
/* Setup sample points all around the edge of the input raster. */
/* -------------------------------------------------------------------- */
if (bIsGDALGenImgProjTransform)
{
// In case CHECK_WITH_INVERT_PROJ has been modified.
GDALRefreshGenImgProjTransformer(pTransformArg);
}
else if (GDALIsTransformer(pTransformArg,
GDAL_APPROX_TRANSFORMER_CLASS_NAME))
{
// In case CHECK_WITH_INVERT_PROJ has been modified.
GDALRefreshApproxTransformer(pTransformArg);
}
const int nInXSize = GDALGetRasterXSize(hSrcDS);
const int nInYSize = GDALGetRasterYSize(hSrcDS);
/* ------------------------------------------------------------- */
/* Special case for warping on the same (or null) CRS. */
/* ------------------------------------------------------------- */
if ((!nOptions || (nOptions & GDAL_SWO_FORCE_SQUARE_PIXEL) == 0) &&
pTransformArg && bIsGDALGenImgProjTransform)
{
const GDALGenImgProjTransformInfo *psInfo =
static_cast<const GDALGenImgProjTransformInfo *>(pTransformArg);
if (!psInfo->pSrcTransformer &&
!psInfo->bHasCustomTransformationPipeline &&
!psInfo->pDstTransformer && psInfo->adfSrcGeoTransform[2] == 0 &&
psInfo->adfSrcGeoTransform[4] == 0 &&
psInfo->adfDstGeoTransform[0] == 0 &&
psInfo->adfDstGeoTransform[1] == 1 &&
psInfo->adfDstGeoTransform[2] == 0 &&
psInfo->adfDstGeoTransform[3] == 0 &&
psInfo->adfDstGeoTransform[4] == 0 &&
psInfo->adfDstGeoTransform[5] == 1)
{
const OGRSpatialReference *poSourceCRS = nullptr;
const OGRSpatialReference *poTargetCRS = nullptr;
if (psInfo->pReprojectArg)
{
const GDALReprojectionTransformInfo *psRTI =
static_cast<const GDALReprojectionTransformInfo *>(
psInfo->pReprojectArg);
poSourceCRS = psRTI->poForwardTransform->GetSourceCS();
poTargetCRS = psRTI->poForwardTransform->GetTargetCS();
}
if ((!poSourceCRS && !poTargetCRS) ||
(poSourceCRS && poTargetCRS &&
poSourceCRS->IsSame(poTargetCRS)))
{
const bool bNorthUp{psInfo->adfSrcGeoTransform[5] < 0.0};
memcpy(padfGeoTransformOut, psInfo->adfSrcGeoTransform,
sizeof(double) * 6);
if (!bNorthUp)
{
padfGeoTransformOut[3] = padfGeoTransformOut[3] +
nInYSize * padfGeoTransformOut[5];
padfGeoTransformOut[5] = -padfGeoTransformOut[5];
}
*pnPixels = nInXSize;
*pnLines = nInYSize;
// Calculate extent from hSrcDS
if (padfExtent)
{
padfExtent[0] = psInfo->adfSrcGeoTransform[0];
padfExtent[1] = psInfo->adfSrcGeoTransform[3] +
nInYSize * psInfo->adfSrcGeoTransform[5];
padfExtent[2] = psInfo->adfSrcGeoTransform[0] +
nInXSize * psInfo->adfSrcGeoTransform[1];
padfExtent[3] = psInfo->adfSrcGeoTransform[3];
if (!bNorthUp)
{
std::swap(padfExtent[1], padfExtent[3]);
}
}
return CE_None;
}
}
}
const int N_PIXELSTEP = 50;
int nSteps = static_cast<int>(
static_cast<double>(std::min(nInYSize, nInXSize)) / N_PIXELSTEP + 0.5);
if (nSteps < 20)
nSteps = 20;
else if (nSteps > 100)
nSteps = 100;
// TODO(rouault): How is this goto retry supposed to work? Added in r20537.
// Does redoing the same malloc multiple times work? If it is needed, can
// it be converted to a tigher while loop around the MALLOC3s and free? Is
// the point to try with the full requested steps. Then, if there is not
// enough memory, back off and try with just 20 steps?
retry:
int nStepsPlusOne = nSteps + 1;
int nSampleMax = nStepsPlusOne * nStepsPlusOne;
double dfStep = 1.0 / nSteps;
double *padfY = nullptr;
double *padfZ = nullptr;
double *padfYRevert = nullptr;
double *padfZRevert = nullptr;
int *pabSuccess = static_cast<int *>(
VSI_MALLOC3_VERBOSE(sizeof(int), nStepsPlusOne, nStepsPlusOne));
double *padfX = static_cast<double *>(
VSI_MALLOC3_VERBOSE(sizeof(double) * 3, nStepsPlusOne, nStepsPlusOne));
double *padfXRevert = static_cast<double *>(
VSI_MALLOC3_VERBOSE(sizeof(double) * 3, nStepsPlusOne, nStepsPlusOne));
if (pabSuccess == nullptr || padfX == nullptr || padfXRevert == nullptr)
{
CPLFree(padfX);
CPLFree(padfXRevert);
CPLFree(pabSuccess);
if (nSteps > 20)
{
nSteps = 20;
goto retry;
}
return CE_Failure;
}
padfY = padfX + nSampleMax;
padfZ = padfX + nSampleMax * 2;
padfYRevert = padfXRevert + nSampleMax;
padfZRevert = padfXRevert + nSampleMax * 2;
// Take N_STEPS steps.
for (int iStep = 0; iStep <= nSteps; iStep++)
{
double dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
int iStep2 = iStep;
// Along top.
padfX[iStep2] = dfRatio * nInXSize;
padfY[iStep2] = 0.0;
padfZ[iStep2] = 0.0;
// Along bottom.
iStep2 += nStepsPlusOne;
padfX[iStep2] = dfRatio * nInXSize;
padfY[iStep2] = nInYSize;
padfZ[iStep2] = 0.0;
// Along left.
iStep2 += nStepsPlusOne;
padfX[iStep2] = 0.0;
padfY[iStep2] = dfRatio * nInYSize;
padfZ[iStep2] = 0.0;
// Along right.
iStep2 += nStepsPlusOne;
padfX[iStep2] = nInXSize;
padfY[iStep2] = dfRatio * nInYSize;
padfZ[iStep2] = 0.0;
}
int nSamplePoints = 4 * nStepsPlusOne;
memset(pabSuccess, 1, sizeof(int) * nSampleMax);
/* -------------------------------------------------------------------- */
/* Transform them to the output coordinate system. */
/* -------------------------------------------------------------------- */
{
CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
pfnTransformer(pTransformArg, FALSE, nSamplePoints, padfX, padfY, padfZ,
pabSuccess);
}
constexpr int SIGN_FINAL_UNINIT = -2;
constexpr int SIGN_FINAL_INVALID = 0;
int iSignDiscontinuity = SIGN_FINAL_UNINIT;
int nFailedCount = 0;
const int iSignArray[2] = {-1, 1};
for (int i = 0; i < nSamplePoints; i++)
{
if (pabSuccess[i])
{
// Fix for https://trac.osgeo.org/gdal/ticket/7243
// where echo "-2050000.000 2050000.000" |
// gdaltransform -s_srs EPSG:3411 -t_srs EPSG:4326
// gives "-180 63.691332898492"
// but we would rather like 180
if (iSignDiscontinuity == 1 || iSignDiscontinuity == -1)
{
if (!((iSignDiscontinuity * padfX[i] > 0 &&
iSignDiscontinuity * padfX[i] <= 180.0) ||
(fabs(padfX[i] - iSignDiscontinuity * -180.0) < 1e-8)))
{
iSignDiscontinuity = SIGN_FINAL_INVALID;
}
}
else if (iSignDiscontinuity == SIGN_FINAL_UNINIT)
{
for (const auto &iSign : iSignArray)
{
if ((iSign * padfX[i] > 0 && iSign * padfX[i] <= 180.0) ||
(fabs(padfX[i] - iSign * -180.0) < 1e-8))
{
iSignDiscontinuity = iSign;
break;
}
}
if (iSignDiscontinuity == SIGN_FINAL_UNINIT)
{
iSignDiscontinuity = SIGN_FINAL_INVALID;
}
}
}
else
{
nFailedCount++;
}
}
if (iSignDiscontinuity == 1 || iSignDiscontinuity == -1)
{
for (int i = 0; i < nSamplePoints; i++)
{
if (pabSuccess[i])
{
if (fabs(padfX[i] - iSignDiscontinuity * -180.0) < 1e-8)
{
double axTemp[2] = {iSignDiscontinuity * -180.0,
iSignDiscontinuity * 180.0};
double ayTemp[2] = {padfY[i], padfY[i]};
double azTemp[2] = {padfZ[i], padfZ[i]};
int abSuccess[2] = {FALSE, FALSE};
CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
if (pfnTransformer(pTransformArg, TRUE, 2, axTemp, ayTemp,
azTemp, abSuccess) &&
fabs(axTemp[0] - axTemp[1]) < 1e-8 &&
fabs(ayTemp[0] - ayTemp[1]) < 1e-8)
{
padfX[i] = iSignDiscontinuity * 180.0;
}
}
}
}
}
/* -------------------------------------------------------------------- */
/* Check if the computed target coordinates are revertable. */
/* If not, try the detailed grid sampling. */
/* -------------------------------------------------------------------- */
if (nFailedCount)
{
CPLDebug("WARP", "At least one point failed after direct transform");
}
else
{
memcpy(padfXRevert, padfX, nSamplePoints * sizeof(double));
memcpy(padfYRevert, padfY, nSamplePoints * sizeof(double));
memcpy(padfZRevert, padfZ, nSamplePoints * sizeof(double));
{
CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
pfnTransformer(pTransformArg, TRUE, nSamplePoints, padfXRevert,
padfYRevert, padfZRevert, pabSuccess);
}
for (int i = 0; nFailedCount == 0 && i < nSamplePoints; i++)
{
if (!pabSuccess[i])
{
nFailedCount++;
break;
}
double dfRatio = (i % nStepsPlusOne) * dfStep;
if (dfRatio > 0.99)
dfRatio = 1.0;
double dfExpectedX = 0.0;
double dfExpectedY = 0.0;
if (i < nStepsPlusOne)
{
dfExpectedX = dfRatio * nInXSize;
}
else if (i < 2 * nStepsPlusOne)
{
dfExpectedX = dfRatio * nInXSize;
dfExpectedY = nInYSize;
}
else if (i < 3 * nStepsPlusOne)
{
dfExpectedY = dfRatio * nInYSize;
}
else
{
dfExpectedX = nInXSize;
dfExpectedY = dfRatio * nInYSize;
}
if (fabs(padfXRevert[i] - dfExpectedX) >
nInXSize / static_cast<double>(nSteps) ||
fabs(padfYRevert[i] - dfExpectedY) >
nInYSize / static_cast<double>(nSteps))
nFailedCount++;
}
if (nFailedCount != 0)
CPLDebug("WARP",
"At least one point failed after revert transform");
}
/* -------------------------------------------------------------------- */
/* If any of the edge points failed to transform, we need to */
/* build a fairly detailed internal grid of points instead to */
/* help identify the area that is transformable. */
/* -------------------------------------------------------------------- */
if (nFailedCount)
{
nSamplePoints = 0;
// Take N_STEPS steps.
for (int iStep = 0; iStep <= nSteps; iStep++)
{
double dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
for (int iStep2 = 0; iStep2 <= nSteps; iStep2++)
{
const double dfRatio2 =
iStep2 == nSteps ? 1.0 : iStep2 * dfStep;
// From top to bottom, from left to right.
padfX[nSamplePoints] = dfRatio2 * nInXSize;
padfY[nSamplePoints] = dfRatio * nInYSize;
padfZ[nSamplePoints] = 0.0;
nSamplePoints++;
}
}
CPLAssert(nSamplePoints == nSampleMax);
{
CPLTurnFailureIntoWarningBackuper oErrorsToWarnings{};
pfnTransformer(pTransformArg, FALSE, nSamplePoints, padfX, padfY,
padfZ, pabSuccess);
}
}
/* -------------------------------------------------------------------- */
/* Collect the bounds, ignoring any failed points. */
/* -------------------------------------------------------------------- */
double dfMinXOut = 0.0;
double dfMinYOut = 0.0;
double dfMaxXOut = 0.0;
double dfMaxYOut = 0.0;
bool bGotInitialPoint = false;
nFailedCount = 0;
for (int i = 0; i < nSamplePoints; i++)
{
int x_i = 0;
int y_i = 0;
if (nSamplePoints == nSampleMax)
{
x_i = i % nStepsPlusOne;
y_i = i / nStepsPlusOne;
}
else
{
if (i < 2 * nStepsPlusOne)
{
x_i = i % nStepsPlusOne;
y_i = (i < nStepsPlusOne) ? 0 : nSteps;
}
}
if (x_i > 0 && (pabSuccess[i - 1] || pabSuccess[i]))
{
double x_out_before = padfX[i - 1];
double x_out_after = padfX[i];
int nIter = 0;
double x_in_before =
static_cast<double>(x_i - 1) * nInXSize / nSteps;
double x_in_after = static_cast<double>(x_i) * nInXSize / nSteps;
int invalid_before = !(pabSuccess[i - 1]);
int invalid_after = !(pabSuccess[i]);
// Detect discontinuity in target coordinates when the target x
// coordinates change sign. This may be a false positive when the
// target tx is around 0 Dichotomic search to reduce the interval
// to near the discontinuity and get a better out extent.
while ((invalid_before || invalid_after ||
x_out_before * x_out_after < 0.0) &&
nIter < 16)
{
double x = (x_in_before + x_in_after) / 2.0;
double y = static_cast<double>(y_i) * nInYSize / nSteps;
double z = 0.0;
int bSuccess = TRUE;
if (pfnTransformer(pTransformArg, FALSE, 1, &x, &y, &z,
&bSuccess) &&
bSuccess)
{
if (bGotInitialPoint)
{
dfMinXOut = std::min(dfMinXOut, x);
dfMinYOut = std::min(dfMinYOut, y);
dfMaxXOut = std::max(dfMaxXOut, x);
dfMaxYOut = std::max(dfMaxYOut, y);
}
else
{
bGotInitialPoint = true;
dfMinXOut = x;
dfMaxXOut = x;
dfMinYOut = y;
dfMaxYOut = y;
}
if (invalid_before || x_out_before * x < 0)
{
invalid_after = FALSE;
x_in_after = (x_in_before + x_in_after) / 2.0;
x_out_after = x;
}
else
{
invalid_before = FALSE;
x_out_before = x;
x_in_before = (x_in_before + x_in_after) / 2.0;
}
}
else
{
if (invalid_before)
{
x_in_before = (x_in_before + x_in_after) / 2.0;
}
else if (invalid_after)
{
x_in_after = (x_in_before + x_in_after) / 2.0;
}
else
{
break;
}
}
nIter++;
}
}
if (!pabSuccess[i])
{
nFailedCount++;
continue;
}
if (bGotInitialPoint)
{
dfMinXOut = std::min(dfMinXOut, padfX[i]);
dfMinYOut = std::min(dfMinYOut, padfY[i]);
dfMaxXOut = std::max(dfMaxXOut, padfX[i]);
dfMaxYOut = std::max(dfMaxYOut, padfY[i]);
}
else
{
bGotInitialPoint = true;
dfMinXOut = padfX[i];
dfMaxXOut = padfX[i];
dfMinYOut = padfY[i];
dfMaxYOut = padfY[i];
}
}
if (nFailedCount > nSamplePoints - 10)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Too many points (%d out of %d) failed to transform, "
"unable to compute output bounds.",
nFailedCount, nSamplePoints);
CPLFree(padfX);
CPLFree(padfXRevert);
CPLFree(pabSuccess);
return CE_Failure;
}
if (nFailedCount)
CPLDebug("GDAL",
"GDALSuggestedWarpOutput(): %d out of %d points failed to "
"transform.",
nFailedCount, nSamplePoints);
bool bIsGeographicCoordsDeg = false;
if (bIsGDALGenImgProjTransform)
{
const GDALGenImgProjTransformInfo *pGIPTI =
static_cast<const GDALGenImgProjTransformInfo *>(pTransformArg);
if (pGIPTI->pSrcTransformer == GDALGeoLocTransform &&
pGIPTI->pDstTransformer == nullptr &&
pGIPTI->adfDstGeoTransform[0] == 0 &&
pGIPTI->adfDstGeoTransform[1] == 1 &&
pGIPTI->adfDstGeoTransform[2] == 0 &&
pGIPTI->adfDstGeoTransform[3] == 0 &&
pGIPTI->adfDstGeoTransform[4] == 0 &&
pGIPTI->adfDstGeoTransform[5] == 1)
{
/* --------------------------------------------------------------------
*/
/* Special case for geolocation array, to quickly find the
* bounds. */
/* --------------------------------------------------------------------
*/
const GDALGeoLocTransformInfo *pGLTI =
static_cast<const GDALGeoLocTransformInfo *>(
pGIPTI->pSrcTransformArg);
if (pGIPTI->pReproject == nullptr)
{
const char *pszGLSRS =
CSLFetchNameValue(pGLTI->papszGeolocationInfo, "SRS");
if (pszGLSRS == nullptr)
{
bIsGeographicCoordsDeg = true;
}
else
{
OGRSpatialReference oSRS;
if (oSRS.SetFromUserInput(pszGLSRS) == OGRERR_NONE &&
oSRS.IsGeographic() &&
std::fabs(oSRS.GetAngularUnits() -
CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9)
{
bIsGeographicCoordsDeg = true;
}
}
}
for (const auto &xy :
{std::pair<double, double>(pGLTI->dfMinX, pGLTI->dfYAtMinX),
std::pair<double, double>(pGLTI->dfXAtMinY, pGLTI->dfMinY),
std::pair<double, double>(pGLTI->dfMaxX, pGLTI->dfYAtMaxX),
std::pair<double, double>(pGLTI->dfXAtMaxY, pGLTI->dfMaxY)})
{
double x = xy.first;
double y = xy.second;
if (pGLTI->bSwapXY)
{
std::swap(x, y);
}
double xOut = std::numeric_limits<double>::quiet_NaN();
double yOut = std::numeric_limits<double>::quiet_NaN();
if (pGIPTI->pReproject == nullptr ||
pGIPTI->pReproject(pGIPTI->pReprojectArg, false, 1, &x, &y,
nullptr, nullptr))
{
xOut = x;
yOut = y;
}
dfMinXOut = std::min(dfMinXOut, xOut);
dfMinYOut = std::min(dfMinYOut, yOut);
dfMaxXOut = std::max(dfMaxXOut, xOut);
dfMaxYOut = std::max(dfMaxYOut, yOut);
}
}
else if (pGIPTI->pSrcTransformer == nullptr &&
pGIPTI->pDstTransformer == nullptr &&
pGIPTI->pReproject == GDALReprojectionTransform &&
pGIPTI->adfDstGeoTransform[0] == 0 &&
pGIPTI->adfDstGeoTransform[1] == 1 &&
pGIPTI->adfDstGeoTransform[2] == 0 &&
pGIPTI->adfDstGeoTransform[3] == 0 &&
pGIPTI->adfDstGeoTransform[4] == 0 &&
pGIPTI->adfDstGeoTransform[5] == 1)
{
/* ------------------------------------------------------------- */
/* Special case for warping using source geotransform and */
/* reprojection to deal with the poles. */
/* ------------------------------------------------------------- */
const GDALReprojectionTransformInfo *psRTI =
static_cast<const GDALReprojectionTransformInfo *>(
pGIPTI->pReprojectArg);
const OGRSpatialReference *poSourceCRS =
psRTI->poForwardTransform->GetSourceCS();
const OGRSpatialReference *poTargetCRS =
psRTI->poForwardTransform->GetTargetCS();
if (poTargetCRS != nullptr &&
psRTI->poReverseTransform != nullptr &&
poTargetCRS->IsGeographic() &&
fabs(poTargetCRS->GetAngularUnits() -
CPLAtof(SRS_UA_DEGREE_CONV)) < 1e-9 &&
(!poSourceCRS || !poSourceCRS->IsGeographic()))
{
bIsGeographicCoordsDeg = true;
std::unique_ptr<CPLConfigOptionSetter> poSetter;
if (pGIPTI->bCheckWithInvertPROJ)
{
// CHECK_WITH_INVERT_PROJ=YES prevent reliable
// transformation of poles.
poSetter = std::make_unique<CPLConfigOptionSetter>(
"CHECK_WITH_INVERT_PROJ", "NO", false);
GDALRefreshGenImgProjTransformer(pTransformArg);
// GDALRefreshGenImgProjTransformer() has invalidated psRTI
psRTI = static_cast<const GDALReprojectionTransformInfo *>(
pGIPTI->pReprojectArg);
}
for (const auto &sign : iSignArray)
{
double X = 0.0;
const double Yinit = 90.0 * sign;
double Y = Yinit;
if (psRTI->poReverseTransform->Transform(1, &X, &Y))
{
const auto invGT = pGIPTI->adfSrcInvGeoTransform;
const double x = invGT[0] + X * invGT[1] + Y * invGT[2];
const double y = invGT[3] + X * invGT[4] + Y * invGT[5];
constexpr double EPSILON = 1e-5;
if (x >= -EPSILON && x <= nInXSize + EPSILON &&
y >= -EPSILON && y <= nInYSize + EPSILON)
{
if (psRTI->poForwardTransform->Transform(1, &X,
&Y) &&
fabs(Y - Yinit) <= 1e-6)
{
bool bMinXMaxXSet = false;
if (poSourceCRS)
{
const char *pszProjection =
poSourceCRS->GetAttrValue("PROJECTION");
if (pszProjection &&
EQUAL(pszProjection,
SRS_PT_ORTHOGRAPHIC))
{
const double dfLon0 =
poSourceCRS->GetNormProjParm(
SRS_PP_CENTRAL_MERIDIAN, 0.0);
dfMinXOut = dfLon0 - 90;
dfMaxXOut = dfLon0 + 90;
bMinXMaxXSet = true;
}
}
if (!bMinXMaxXSet)