-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ogrct.cpp
3459 lines (3125 loc) · 123 KB
/
ogrct.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: OpenGIS Simple Features Reference Implementation
* Purpose: The OGRSCoordinateTransformation class.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2000, Frank Warmerdam
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "ogr_spatialref.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <limits>
#include <list>
#include <mutex>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_mem_cache.h"
#include "cpl_string.h"
#include "ogr_core.h"
#include "ogr_srs_api.h"
#include "ogr_proj_p.h"
#include "proj.h"
#include "proj_experimental.h"
CPL_CVSID("$Id$")
#ifdef DEBUG_PERF
static double g_dfTotalTimeCRStoCRS = 0;
static double g_dfTotalTimeReprojection = 0;
/************************************************************************/
/* CPLGettimeofday() */
/************************************************************************/
#if defined(_WIN32) && !defined(__CYGWIN__)
# include <sys/timeb.h>
namespace {
struct CPLTimeVal
{
time_t tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
}
static void CPLGettimeofday(struct CPLTimeVal* tp, void* /* timezonep*/ )
{
struct _timeb theTime;
_ftime(&theTime);
tp->tv_sec = static_cast<time_t>(theTime.time);
tp->tv_usec = theTime.millitm * 1000;
}
#else
# include <sys/time.h> /* for gettimeofday() */
# define CPLTimeVal timeval
# define CPLGettimeofday(t,u) gettimeofday(t,u)
#endif
#endif // DEBUG_PERF
// Cache of OGRProjCT objects
static std::mutex g_oCTCacheMutex;
class OGRProjCT;
// We wrap a OGRProjCT in a shared_ptr<unique_ptr>, because we need a copyable
// type to be inserted in the cache (shared_ptr), and we need to be able to
// alter the content of the value to release() the unique_ptr value when we
// find a value in it.
typedef std::string CTCacheKey;
typedef std::shared_ptr<std::unique_ptr<OGRProjCT>> CTCacheValue;
static lru11::Cache<CTCacheKey, CTCacheValue>* g_poCTCache = nullptr;
/************************************************************************/
/* OGRCoordinateTransformationOptions::Private */
/************************************************************************/
struct OGRCoordinateTransformationOptions::Private
{
bool bHasAreaOfInterest = false;
double dfWestLongitudeDeg = 0.0;
double dfSouthLatitudeDeg = 0.0;
double dfEastLongitudeDeg = 0.0;
double dfNorthLatitudeDeg = 0.0;
CPLString osCoordOperation{};
bool bReverseCO = false;
bool bAllowBallpark = true;
double dfAccuracy = -1; // no constraint
bool bHasSourceCenterLong = false;
double dfSourceCenterLong = 0.0;
bool bHasTargetCenterLong = false;
double dfTargetCenterLong = 0.0;
bool bCheckWithInvertProj = false;
Private();
Private(const Private&) = default;
Private(Private&&) = default;
Private& operator=(const Private&) = default;
Private& operator=(Private&&) = default;
std::string GetKey() const;
void RefreshCheckWithInvertProj();
};
/************************************************************************/
/* Private() */
/************************************************************************/
OGRCoordinateTransformationOptions::Private::Private()
{
RefreshCheckWithInvertProj();
}
/************************************************************************/
/* GetKey() */
/************************************************************************/
std::string OGRCoordinateTransformationOptions::Private::GetKey() const
{
std::string ret;
ret += std::to_string(static_cast<int>(bHasAreaOfInterest));
ret += std::to_string(dfWestLongitudeDeg);
ret += std::to_string(dfSouthLatitudeDeg);
ret += std::to_string(dfEastLongitudeDeg);
ret += std::to_string(dfNorthLatitudeDeg);
ret += osCoordOperation;
ret += std::to_string(static_cast<int>(bReverseCO));
ret += std::to_string(static_cast<int>(bAllowBallpark));
ret += std::to_string(dfAccuracy);
ret += std::to_string(static_cast<int>(bHasSourceCenterLong));
ret += std::to_string(dfSourceCenterLong);
ret += std::to_string(static_cast<int>(bHasTargetCenterLong));
ret += std::to_string(dfTargetCenterLong);
ret += std::to_string(static_cast<int>(bCheckWithInvertProj));
return ret;
}
/************************************************************************/
/* RefreshCheckWithInvertProj() */
/************************************************************************/
void OGRCoordinateTransformationOptions::Private::RefreshCheckWithInvertProj()
{
bCheckWithInvertProj =
CPLTestBool(CPLGetConfigOption( "CHECK_WITH_INVERT_PROJ", "NO" ));
}
/************************************************************************/
/* GetWktOrProjString() */
/************************************************************************/
static char* GetWktOrProjString(const OGRSpatialReference* poSRS)
{
CPLErrorStateBackuper oErrorStateBackuper;
CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
const char* const apszOptionsWKT2_2018[] = { "FORMAT=WKT2_2018", nullptr };
// If there's a PROJ4 EXTENSION node in WKT1, then use
// it. For example when dealing with "+proj=longlat +lon_wrap=180"
char* pszText = nullptr;
if( poSRS->GetExtension(nullptr, "PROJ4", nullptr) )
{
poSRS->exportToProj4(&pszText);
if (strstr(pszText, " +type=crs") == nullptr )
{
auto tmpText = std::string(pszText) + " +type=crs";
CPLFree(pszText);
pszText = CPLStrdup(tmpText.c_str());
}
}
else
poSRS->exportToWkt(&pszText, apszOptionsWKT2_2018);
return pszText;
}
/************************************************************************/
/* OGRCoordinateTransformationOptions() */
/************************************************************************/
/** \brief Constructs a new OGRCoordinateTransformationOptions.
*
* @since GDAL 3.0
*/
OGRCoordinateTransformationOptions::OGRCoordinateTransformationOptions():
d(new Private())
{
}
/************************************************************************/
/* OGRCoordinateTransformationOptions() */
/************************************************************************/
/** \brief Copy constructor
*
* @since GDAL 3.1
*/
OGRCoordinateTransformationOptions::OGRCoordinateTransformationOptions(
const OGRCoordinateTransformationOptions& other):
d(new Private(*(other.d)))
{}
/************************************************************************/
/* operator =() */
/************************************************************************/
/** \brief Assignment operator
*
* @since GDAL 3.1
*/
OGRCoordinateTransformationOptions&
OGRCoordinateTransformationOptions::operator= (const OGRCoordinateTransformationOptions& other)
{
if( this != &other )
{
*d = *(other.d);
}
return *this;
}
/************************************************************************/
/* OGRCoordinateTransformationOptions() */
/************************************************************************/
/** \brief Destroys a OGRCoordinateTransformationOptions.
*
* @since GDAL 3.0
*/
OGRCoordinateTransformationOptions::~OGRCoordinateTransformationOptions()
{
}
/************************************************************************/
/* OCTNewCoordinateTransformationOptions() */
/************************************************************************/
/** \brief Create coordinate transformation options.
*
* To be freed with OCTDestroyCoordinateTransformationOptions()
*
* @since GDAL 3.0
*/
OGRCoordinateTransformationOptionsH OCTNewCoordinateTransformationOptions(void)
{
return new OGRCoordinateTransformationOptions();
}
/************************************************************************/
/* OCTDestroyCoordinateTransformationOptions() */
/************************************************************************/
/** \brief Destroy coordinate transformation options.
*
* @since GDAL 3.0
*/
void OCTDestroyCoordinateTransformationOptions(
OGRCoordinateTransformationOptionsH hOptions)
{
delete hOptions;
}
/************************************************************************/
/* SetAreaOfInterest() */
/************************************************************************/
/** \brief Sets an area of interest.
*
* The west longitude is generally lower than the east longitude, except for
* areas of interest that go across the anti-meridian.
*
* @param dfWestLongitudeDeg West longitude (in degree). Must be in [-180,180]
* @param dfSouthLatitudeDeg South latitude (in degree). Must be in [-90,90]
* @param dfEastLongitudeDeg East longitude (in degree). Must be in [-180,180]
* @param dfNorthLatitudeDeg North latitude (in degree). Must be in [-90,90]
* @return true in case of success.
*
* @since GDAL 3.0
*/
bool OGRCoordinateTransformationOptions::SetAreaOfInterest(
double dfWestLongitudeDeg, double dfSouthLatitudeDeg,
double dfEastLongitudeDeg, double dfNorthLatitudeDeg)
{
if( std::fabs(dfWestLongitudeDeg) > 180 )
{
CPLError(CE_Failure, CPLE_AppDefined, "Invalid dfWestLongitudeDeg");
return false;
}
if( std::fabs(dfSouthLatitudeDeg) > 90 )
{
CPLError(CE_Failure, CPLE_AppDefined, "Invalid dfSouthLatitudeDeg");
return false;
}
if( std::fabs(dfEastLongitudeDeg) > 180 )
{
CPLError(CE_Failure, CPLE_AppDefined, "Invalid dfEastLongitudeDeg");
return false;
}
if( std::fabs(dfNorthLatitudeDeg) > 90 )
{
CPLError(CE_Failure, CPLE_AppDefined, "Invalid dfNorthLatitudeDeg");
return false;
}
if( dfSouthLatitudeDeg > dfNorthLatitudeDeg )
{
CPLError(CE_Failure, CPLE_AppDefined,
"dfSouthLatitudeDeg should be lower than dfNorthLatitudeDeg");
return false;
}
d->bHasAreaOfInterest = true;
d->dfWestLongitudeDeg = dfWestLongitudeDeg;
d->dfSouthLatitudeDeg = dfSouthLatitudeDeg;
d->dfEastLongitudeDeg = dfEastLongitudeDeg;
d->dfNorthLatitudeDeg = dfNorthLatitudeDeg;
return true;
}
/************************************************************************/
/* OCTCoordinateTransformationOptionsSetAreaOfInterest() */
/************************************************************************/
/** \brief Sets an area of interest.
*
* See OGRCoordinateTransformationOptions::SetAreaOfInterest()
*
* @since GDAL 3.0
*/
int OCTCoordinateTransformationOptionsSetAreaOfInterest(
OGRCoordinateTransformationOptionsH hOptions,
double dfWestLongitudeDeg,
double dfSouthLatitudeDeg,
double dfEastLongitudeDeg,
double dfNorthLatitudeDeg)
{
return hOptions->SetAreaOfInterest(
dfWestLongitudeDeg, dfSouthLatitudeDeg,
dfEastLongitudeDeg, dfNorthLatitudeDeg);
}
/************************************************************************/
/* SetCoordinateOperation() */
/************************************************************************/
/** \brief Sets a coordinate operation.
*
* This is a user override to be used instead of the normally computed pipeline.
*
* The pipeline must take into account the axis order of the source and target
* SRS.
*
* The pipeline may be provided as a PROJ string (single step operation or
* multiple step string starting with +proj=pipeline), a WKT2 string describing
* a CoordinateOperation, or a "urn:ogc:def:coordinateOperation:EPSG::XXXX" URN
*
* @param pszCO PROJ or WKT string describing a coordinate operation
* @param bReverseCO Whether the PROJ or WKT string should be evaluated in the reverse path
* @return true in case of success.
*
* @since GDAL 3.0
*/
bool OGRCoordinateTransformationOptions::SetCoordinateOperation(const char* pszCO, bool bReverseCO)
{
d->osCoordOperation = pszCO ? pszCO : "";
d->bReverseCO = bReverseCO;
return true;
}
/************************************************************************/
/* SetSourceCenterLong() */
/************************************************************************/
/*! @cond Doxygen_Suppress */
void OGRCoordinateTransformationOptions::SetSourceCenterLong(double dfCenterLong)
{
d->dfSourceCenterLong = dfCenterLong;
d->bHasSourceCenterLong = true;
}
/*! @endcond */
/************************************************************************/
/* SetTargetCenterLong() */
/************************************************************************/
/*! @cond Doxygen_Suppress */
void OGRCoordinateTransformationOptions::SetTargetCenterLong(double dfCenterLong)
{
d->dfTargetCenterLong = dfCenterLong;
d->bHasTargetCenterLong = true;
}
/*! @endcond */
/************************************************************************/
/* OCTCoordinateTransformationOptionsSetOperation() */
/************************************************************************/
/** \brief Sets a coordinate operation.
*
* See OGRCoordinateTransformationOptions::SetCoordinateTransformation()
*
* @since GDAL 3.0
*/
int OCTCoordinateTransformationOptionsSetOperation(
OGRCoordinateTransformationOptionsH hOptions,
const char* pszCO, int bReverseCO)
{
return hOptions->SetCoordinateOperation(pszCO, CPL_TO_BOOL(bReverseCO));
}
/************************************************************************/
/* SetDesiredAccuracy() */
/************************************************************************/
/** \brief Sets the desired accuracy for coordinate operations.
*
* Only coordinate operations that offer an accuracy of at least the one
* specified will be considered.
*
* An accuracy of 0 is valid and means a coordinate operation made only of one or
* several conversions (map projections, unit conversion, etc.)
* Operations involving ballpark transformations have a unknown accuracy, and
* will be filtered out by any dfAccuracy >= 0 value.
*
* If this option is specified with PROJ < 8, the OGR_CT_OP_SELECTION configuration
* option will default to BEST_ACCURACY.
*
* @param dfAccuracy accuracy in meters (or a negative value to disable this filter)
*
* @since GDAL 3.3
*/
bool OGRCoordinateTransformationOptions::SetDesiredAccuracy(double dfAccuracy)
{
d->dfAccuracy = dfAccuracy;
return true;
}
/************************************************************************/
/* OCTCoordinateTransformationOptionsSetDesiredAccuracy() */
/************************************************************************/
/** \brief Sets the desired accuracy for coordinate operations.
*
* See OGRCoordinateTransformationOptions::SetDesiredAccuracy()
*
* @since GDAL 3.3
*/
int OCTCoordinateTransformationOptionsSetDesiredAccuracy(
OGRCoordinateTransformationOptionsH hOptions, double dfAccuracy)
{
return hOptions->SetDesiredAccuracy(dfAccuracy);
}
/************************************************************************/
/* SetBallparkAllowed() */
/************************************************************************/
/** \brief Sets whether ballpark transformations are allowed.
*
* By default, PROJ may generate "ballpark transformations" (see
* https://proj.org/glossary.html) when precise datum transformations are missing.
* For high accuracy use cases, such transformations might not be allowed.
*
* If this option is specified with PROJ < 8, the OGR_CT_OP_SELECTION configuration
* option will default to BEST_ACCURACY.
*
* @param bAllowBallpark false to disable the user of ballpark transformations
*
* @since GDAL 3.3
*/
bool OGRCoordinateTransformationOptions::SetBallparkAllowed(bool bAllowBallpark)
{
d->bAllowBallpark = bAllowBallpark;
return true;
}
/************************************************************************/
/* OCTCoordinateTransformationOptionsSetBallparkAllowed() */
/************************************************************************/
/** \brief Sets whether ballpark transformations are allowed.
*
* See OGRCoordinateTransformationOptions::SetDesiredAccuracy()
*
* @since GDAL 3.3 and PROJ 8
*/
int OCTCoordinateTransformationOptionsSetBallparkAllowed(
OGRCoordinateTransformationOptionsH hOptions, int bAllowBallpark)
{
return hOptions->SetBallparkAllowed(CPL_TO_BOOL(bAllowBallpark));
}
/************************************************************************/
/* OGRProjCT */
/************************************************************************/
//! @cond Doxygen_Suppress
class OGRProjCT : public OGRCoordinateTransformation
{
class PjPtr
{
PJ* m_pj = nullptr;
void reset()
{
if( m_pj )
{
proj_assign_context(m_pj, OSRGetProjTLSContext());
proj_destroy(m_pj);
}
}
public:
PjPtr() : m_pj(nullptr){}
explicit PjPtr(PJ* pjIn) : m_pj(pjIn){}
~PjPtr()
{
reset();
}
PjPtr(const PjPtr& other) :
m_pj((other.m_pj != nullptr) ?
(proj_clone(OSRGetProjTLSContext(), other.m_pj)) :
(nullptr))
{}
PjPtr(PjPtr&& other) :
m_pj(other.m_pj)
{
other.m_pj = nullptr;
}
PjPtr& operator=(const PjPtr& other)
{
if(this != &other)
{
reset();
m_pj = (other.m_pj != nullptr) ?
(proj_clone(OSRGetProjTLSContext(), other.m_pj)) :
(nullptr);
}
return *this;
}
PjPtr& operator=(PJ* pjIn)
{
if(m_pj != pjIn)
{
reset();
m_pj = pjIn;
}
return *this;
}
operator PJ* () { return m_pj; }
operator const PJ* () const{ return m_pj; }
};
OGRSpatialReference *poSRSSource = nullptr;
bool bSourceLatLong = false;
bool bSourceWrap = false;
double dfSourceWrapLong = 0.0;
bool bSourceIsDynamicCRS = false;
double dfSourceCoordinateEpoch = 0.0;
OGRSpatialReference *poSRSTarget = nullptr;
bool bTargetLatLong = false;
bool bTargetWrap = false;
double dfTargetWrapLong = 0.0;
bool bTargetIsDynamicCRS = false;
double dfTargetCoordinateEpoch = 0.0;
bool bWebMercatorToWGS84LongLat = false;
int nErrorCount = 0;
double dfThreshold = 0.0;
PjPtr m_pj{};
bool m_bReversePj = false;
bool m_bEmitErrors = true;
bool bNoTransform = false;
enum class Strategy
{
PROJ,
BEST_ACCURACY,
FIRST_MATCHING
};
#if PROJ_VERSION_MAJOR > 6 || PROJ_VERSION_MINOR >= 3
Strategy m_eStrategy = Strategy::PROJ;
#else
Strategy m_eStrategy = Strategy::BEST_ACCURACY;
#endif
bool ListCoordinateOperations(const char* pszSrcSRS,
const char* pszTargetSRS,
const OGRCoordinateTransformationOptions& options );
struct Transformation
{
double minx = 0.0;
double miny = 0.0;
double maxx = 0.0;
double maxy = 0.0;
PjPtr pj{};
CPLString osName{};
CPLString osProjString{};
double accuracy = 0.0;
Transformation(double minxIn, double minyIn, double maxxIn, double maxyIn,
PJ* pjIn,
const CPLString& osNameIn,
const CPLString& osProjStringIn,
double accuracyIn):
minx(minxIn), miny(minyIn), maxx(maxxIn), maxy(maxyIn),
pj(pjIn), osName(osNameIn), osProjString(osProjStringIn),
accuracy(accuracyIn) {}
};
std::vector<Transformation> m_oTransformations{};
int m_iCurTransformation = -1;
OGRCoordinateTransformationOptions m_options{};
void ComputeThreshold();
OGRProjCT(const OGRProjCT& other);
OGRProjCT& operator= (const OGRProjCT& ) = delete;
static CTCacheKey MakeCacheKey(const OGRSpatialReference* poSRS1,
const OGRSpatialReference* poSRS2,
const OGRCoordinateTransformationOptions& options);
bool ContainsNorthPole(
const double xmin,
const double ymin,
const double xmax,
const double ymax,
bool lon_lat_order
);
bool ContainsSouthPole(
const double xmin,
const double ymin,
const double xmax,
const double ymax,
bool lon_lat_order
);
public:
OGRProjCT();
~OGRProjCT() override;
int Initialize( const OGRSpatialReference *poSource,
const OGRSpatialReference *poTarget,
const OGRCoordinateTransformationOptions& options );
OGRSpatialReference *GetSourceCS() override;
OGRSpatialReference *GetTargetCS() override;
int Transform( int nCount,
double *x, double *y, double *z, double *t,
int *pabSuccess ) override;
int TransformWithErrorCodes( int nCount,
double *x, double *y, double *z, double *t,
int *panErrorCodes ) override;
int TransformBounds( const double xmin,
const double ymin,
const double xmax,
const double ymax,
double* out_xmin,
double* out_ymin,
double* out_xmax,
double* out_ymax,
const int densify_pts ) override;
bool GetEmitErrors() const override { return m_bEmitErrors; }
void SetEmitErrors( bool bEmitErrors ) override
{ m_bEmitErrors = bEmitErrors; }
OGRCoordinateTransformation* Clone() const override;
OGRCoordinateTransformation* GetInverse() const override;
static void InsertIntoCache( OGRProjCT* poCT );
static OGRProjCT* FindFromCache( const OGRSpatialReference *poSource,
const OGRSpatialReference *poTarget,
const OGRCoordinateTransformationOptions& options );
};
//! @endcond
/************************************************************************/
/* OCTDestroyCoordinateTransformation() */
/************************************************************************/
/**
* \brief OGRCoordinateTransformation destructor.
*
* This function is the same as OGRCoordinateTransformation::DestroyCT()
*
* @param hCT the object to delete
*/
void CPL_STDCALL
OCTDestroyCoordinateTransformation( OGRCoordinateTransformationH hCT )
{
OGRCoordinateTransformation::DestroyCT(
OGRCoordinateTransformation::FromHandle(hCT));
}
/************************************************************************/
/* DestroyCT() */
/************************************************************************/
/**
* \brief OGRCoordinateTransformation destructor.
*
* This function is the same as
* OGRCoordinateTransformation::~OGRCoordinateTransformation()
* and OCTDestroyCoordinateTransformation()
*
* This static method will destroy a OGRCoordinateTransformation. It is
* equivalent to calling delete on the object, but it ensures that the
* deallocation is properly executed within the OGR libraries heap on
* platforms where this can matter (win32).
*
* @param poCT the object to delete
*
* @since GDAL 1.7.0
*/
void OGRCoordinateTransformation::DestroyCT( OGRCoordinateTransformation* poCT )
{
auto poProjCT = dynamic_cast<OGRProjCT*>(poCT);
if( poProjCT )
{
OGRProjCT::InsertIntoCache(poProjCT);
}
else
{
delete poCT;
}
}
/************************************************************************/
/* OGRCreateCoordinateTransformation() */
/************************************************************************/
/**
* Create transformation object.
*
* This is the same as the C function OCTNewCoordinateTransformation().
*
* Input spatial reference system objects are assigned
* by copy (calling clone() method) and no ownership transfer occurs.
*
* The delete operator, or OCTDestroyCoordinateTransformation() should
* be used to destroy transformation objects.
*
* This will honour the axis order advertized by the source and target SRS,
* as well as their "data axis to SRS axis mapping".
* To have a behavior similar to GDAL < 3.0, the OGR_CT_FORCE_TRADITIONAL_GIS_ORDER
* configuration option can be set to YES.
*
* @param poSource source spatial reference system.
* @param poTarget target spatial reference system.
* @return NULL on failure or a ready to use transformation object.
*/
OGRCoordinateTransformation*
OGRCreateCoordinateTransformation( const OGRSpatialReference *poSource,
const OGRSpatialReference *poTarget )
{
return OGRCreateCoordinateTransformation(
poSource, poTarget, OGRCoordinateTransformationOptions());
}
/**
* Create transformation object.
*
* This is the same as the C function OCTNewCoordinateTransformationEx().
*
* Input spatial reference system objects are assigned
* by copy (calling clone() method) and no ownership transfer occurs.
*
* The delete operator, or OCTDestroyCoordinateTransformation() should
* be used to destroy transformation objects.
*
* This will honour the axis order advertized by the source and target SRS,
* as well as their "data axis to SRS axis mapping".
* To have a behavior similar to GDAL < 3.0, the OGR_CT_FORCE_TRADITIONAL_GIS_ORDER
* configuration option can be set to YES.
*
* The source SRS and target SRS should generally not be NULL. This is only
* allowed if a custom coordinate operation is set through the hOptions argument.
*
* Starting with GDAL 3.0.3, the OGR_CT_OP_SELECTION configuration option can be
* set to PROJ (default if PROJ >= 6.3), BEST_ACCURACY or FIRST_MATCHING to decide
* of the strategy to select the operation to use among candidates, whose area of
* use is compatible with the points to transform. It is only taken into account
* if no user defined coordinate transformation pipeline has been specified.
* <ul>
* <li>PROJ means the default behavior used by PROJ proj_create_crs_to_crs().
* In particular the operation to use among several initial candidates is
* evaluated for each point to transform.</li>
* <li>BEST_ACCURACY means the operation whose accuracy is best. It should be
* close to PROJ behavior, except that the operation to select is decided
* for the average point of the coordinates passed in a single Transform() call.
* Note: if the OGRCoordinateTransformationOptions::SetDesiredAccuracy() or
* OGRCoordinateTransformationOptions::SetBallparkAllowed() methods are called
* with PROJ < 8, this strategy will be selected instead of PROJ.
* </li>
* <li>FIRST_MATCHING is the operation ordered first in the list of candidates:
* it will not necessarily have the best accuracy, but generally a larger area of
* use. It is evaluated for the average point of the coordinates passed in a
* single Transform() call. This was the default behavior for GDAL 3.0.0 to
* 3.0.2</li>
* </ul>
*
* By default, if the source or target SRS definition refers to an official
* CRS through a code, GDAL will use the official definition if the official
* definition and the source/target SRS definition are equivalent. Note that TOWGS84[]
* clauses are ignored when checking equivalence. Starting with GDAL 3.4.1, if
* you set the OGR_CT_PREFER_OFFICIAL_SRS_DEF configuration option to NO,
* the source or target SRS definition will be always used.
*
* If options contains a user defined coordinate transformation pipeline, it
* will be unconditionally used.
* If options has an area of interest defined, it will be used to research the
* best fitting coordinate transformation (which will be used for all coordinate
* transformations, even if they don't fall into the declared area of interest)
* If no options are set, then a list of candidate coordinate operations will be
* researched, and at each call to Transform(), the best of those candidate
* regarding the centroid of the coordinate set will be dynamically selected.
*
* @param poSource source spatial reference system.
* @param poTarget target spatial reference system.
* @param options Coordinate transformation options.
* @return NULL on failure or a ready to use transformation object.
* @since GDAL 3.0
*/
OGRCoordinateTransformation*
OGRCreateCoordinateTransformation( const OGRSpatialReference *poSource,
const OGRSpatialReference *poTarget,
const OGRCoordinateTransformationOptions& options )
{
// Try to find if we have a match in the case
auto poCTFromCache = OGRProjCT::FindFromCache(poSource, poTarget, options);
if( poCTFromCache )
return poCTFromCache;
OGRProjCT *poCT = new OGRProjCT();
if( !poCT->Initialize( poSource, poTarget, options ) )
{
delete poCT;
return nullptr;
}
return poCT;
}
/************************************************************************/
/* OCTNewCoordinateTransformation() */
/************************************************************************/
/**
* Create transformation object.
*
* This is the same as the C++ function OGRCreateCoordinateTransformation(const OGRSpatialReference *, const OGRSpatialReference *)
*
* Input spatial reference system objects are assigned
* by copy (calling clone() method) and no ownership transfer occurs.
*
* OCTDestroyCoordinateTransformation() should
* be used to destroy transformation objects.
*
* This will honour the axis order advertized by the source and target SRS,
* as well as their "data axis to SRS axis mapping".
* To have a behavior similar to GDAL < 3.0, the OGR_CT_FORCE_TRADITIONAL_GIS_ORDER
* configuration option can be set to YES.
*
* @param hSourceSRS source spatial reference system.
* @param hTargetSRS target spatial reference system.
* @return NULL on failure or a ready to use transformation object.
*/
OGRCoordinateTransformationH CPL_STDCALL
OCTNewCoordinateTransformation(
OGRSpatialReferenceH hSourceSRS, OGRSpatialReferenceH hTargetSRS )
{
return reinterpret_cast<OGRCoordinateTransformationH>(
OGRCreateCoordinateTransformation(
reinterpret_cast<OGRSpatialReference *>(hSourceSRS),
reinterpret_cast<OGRSpatialReference *>(hTargetSRS)));
}
/************************************************************************/
/* OCTNewCoordinateTransformationEx() */
/************************************************************************/
/**
* Create transformation object.
*
* This is the same as the C++ function OGRCreateCoordinateTransformation(const OGRSpatialReference *, const OGRSpatialReference *, const OGRCoordinateTransformationOptions& )
*
* Input spatial reference system objects are assigned
* by copy (calling clone() method) and no ownership transfer occurs.
*
* OCTDestroyCoordinateTransformation() should
* be used to destroy transformation objects.
*
* The source SRS and target SRS should generally not be NULL. This is only
* allowed if a custom coordinate operation is set through the hOptions argument.
*
* This will honour the axis order advertized by the source and target SRS,
* as well as their "data axis to SRS axis mapping".
* To have a behavior similar to GDAL < 3.0, the OGR_CT_FORCE_TRADITIONAL_GIS_ORDER
* configuration option can be set to YES.
*
* If options contains a user defined coordinate transformation pipeline, it
* will be unconditionally used.
* If options has an area of interest defined, it will be used to research the
* best fitting coordinate transformation (which will be used for all coordinate
* transformations, even if they don't fall into the declared area of interest)
* If no options are set, then a list of candidate coordinate operations will be
* researched, and at each call to Transform(), the best of those candidate
* regarding the centroid of the coordinate set will be dynamically selected.
*
* @param hSourceSRS source spatial reference system.
* @param hTargetSRS target spatial reference system.
* @param hOptions Coordinate transformation options.
* @return NULL on failure or a ready to use transformation object.
* @since GDAL 3.0
*/
OGRCoordinateTransformationH
OCTNewCoordinateTransformationEx(
OGRSpatialReferenceH hSourceSRS, OGRSpatialReferenceH hTargetSRS,
OGRCoordinateTransformationOptionsH hOptions)
{
return reinterpret_cast<OGRCoordinateTransformationH>(
OGRCreateCoordinateTransformation(
reinterpret_cast<OGRSpatialReference *>(hSourceSRS),
reinterpret_cast<OGRSpatialReference *>(hTargetSRS),
hOptions ? *hOptions : OGRCoordinateTransformationOptions()));
}
/************************************************************************/
/* OCTClone() */
/************************************************************************/
/**
* Clone transformation object.
*
* This is the same as the C++ function OGRCreateCoordinateTransformation::Clone
*
* @return handle to transformation's clone or NULL on error,
* must be freed with OCTDestroyCoordinateTransformation
*
* @since GDAL 3.4
*/
OGRCoordinateTransformationH
OCTClone(OGRCoordinateTransformationH hTransform)
{
VALIDATE_POINTER1( hTransform, "OCTClone", nullptr );
return OGRCoordinateTransformation::ToHandle(
OGRCoordinateTransformation::FromHandle(hTransform)->Clone());
}
/************************************************************************/
/* OCTGetSourceCS() */