/
gt_wkt_srs.cpp
3666 lines (3160 loc) · 146 KB
/
gt_wkt_srs.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: GeoTIFF Driver
* Purpose: Implements translation between GeoTIFF normalized projection
* definitions and OpenGIS WKT SRS format.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2014, 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 "gt_wkt_srs.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <mutex>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "gt_citation.h"
#include "gt_wkt_srs_for_gdal.h"
#include "gt_wkt_srs_priv.h"
#include "gtiff.h"
#include "gdal.h"
#include "geokeys.h"
#include "geovalues.h"
#include "ogr_core.h"
#include "ogr_spatialref.h"
#include "ogr_srs_api.h"
#include "ogr_proj_p.h"
#include "tiff.h"
#include "tiffio.h"
#include "tifvsi.h"
#include "xtiffio.h"
#include "proj.h"
static const geokey_t ProjLinearUnitsInterpCorrectGeoKey =
static_cast<geokey_t>(3059);
#ifndef CT_HotineObliqueMercatorAzimuthCenter
# define CT_HotineObliqueMercatorAzimuthCenter 9815
#endif
#if !defined(GTIFAtof)
# define GTIFAtof CPLAtof
#endif
// To remind myself not to use CPLString in this file!
#define CPLString Please_do_not_use_CPLString_in_this_file
static const char * const papszDatumEquiv[] =
{
"Militar_Geographische_Institut", "Militar_Geographische_Institute",
"World_Geodetic_System_1984", "WGS_1984",
"WGS_72_Transit_Broadcast_Ephemeris", "WGS_1972_Transit_Broadcast_Ephemeris",
"World_Geodetic_System_1972", "WGS_1972",
"European_Terrestrial_Reference_System_89", "European_Reference_System_1989",
"D_North_American_1927", "North_American_Datum_1927", // #6863
nullptr
};
// Older libgeotiff's won't list this.
#ifndef CT_CylindricalEqualArea
# define CT_CylindricalEqualArea 28
#endif
#if LIBGEOTIFF_VERSION < 1700
constexpr geokey_t CoordinateEpochGeoKey = static_cast<geokey_t>(5120);
#endif
// Exists since 8.0.1
#ifndef PROJ_AT_LEAST_VERSION
#define PROJ_COMPUTE_VERSION(maj,min,patch) ((maj)*10000+(min)*100+(patch))
#define PROJ_VERSION_NUMBER \
PROJ_COMPUTE_VERSION(PROJ_VERSION_MAJOR, PROJ_VERSION_MINOR, PROJ_VERSION_PATCH)
#define PROJ_AT_LEAST_VERSION(maj,min,patch) \
(PROJ_VERSION_NUMBER >= PROJ_COMPUTE_VERSION(maj,min,patch))
#endif
/************************************************************************/
/* LibgeotiffOneTimeInit() */
/************************************************************************/
static std::mutex oDeleteMutex;
void LibgeotiffOneTimeInit()
{
std::lock_guard<std::mutex> oLock(oDeleteMutex);
static bool bOneTimeInitDone = false;
if( bOneTimeInitDone )
return;
bOneTimeInitDone = true;
// This isn't thread-safe, so better do it now
XTIFFInitialize();
}
/************************************************************************/
/* GTIFToCPLRecyleString() */
/* */
/* This changes a string from the libgeotiff heap to the GDAL */
/* heap. */
/************************************************************************/
static void GTIFToCPLRecycleString( char **ppszTarget )
{
if( *ppszTarget == nullptr )
return;
char *pszTempString = CPLStrdup(*ppszTarget);
GTIFFreeMemory( *ppszTarget );
*ppszTarget = pszTempString;
}
/************************************************************************/
/* WKTMassageDatum() */
/* */
/* Massage an EPSG datum name into WMT format. Also transform */
/* specific exception cases into WKT versions. */
/************************************************************************/
static void WKTMassageDatum( char ** ppszDatum )
{
char *pszDatum = *ppszDatum;
if( !pszDatum || pszDatum[0] == '\0' )
return;
/* -------------------------------------------------------------------- */
/* Translate non-alphanumeric values to underscores. */
/* -------------------------------------------------------------------- */
for( int i = 0; pszDatum[i] != '\0'; i++ )
{
if( pszDatum[i] != '+'
&& !(pszDatum[i] >= 'A' && pszDatum[i] <= 'Z')
&& !(pszDatum[i] >= 'a' && pszDatum[i] <= 'z')
&& !(pszDatum[i] >= '0' && pszDatum[i] <= '9') )
{
pszDatum[i] = '_';
}
}
/* -------------------------------------------------------------------- */
/* Remove repeated and trailing underscores. */
/* -------------------------------------------------------------------- */
int j = 0; // Used after for.
for( int i = 1; pszDatum[i] != '\0'; i++ )
{
if( pszDatum[j] == '_' && pszDatum[i] == '_' )
continue;
pszDatum[++j] = pszDatum[i];
}
if( pszDatum[j] == '_' )
pszDatum[j] = '\0';
else
pszDatum[j+1] = '\0';
/* -------------------------------------------------------------------- */
/* Search for datum equivalences. Specific massaged names get */
/* mapped to OpenGIS specified names. */
/* -------------------------------------------------------------------- */
for( int i = 0; papszDatumEquiv[i] != nullptr; i += 2 )
{
if( EQUAL(*ppszDatum,papszDatumEquiv[i]) )
{
CPLFree( *ppszDatum );
*ppszDatum = CPLStrdup( papszDatumEquiv[i+1] );
return;
}
}
}
/************************************************************************/
/* GTIFCleanupImageineNames() */
/* */
/* Erdas Imagine sometimes emits big copyright messages, and */
/* other stuff into citations. These can be pretty messy when */
/* turned into WKT, so we try to trim and clean the strings */
/* somewhat. */
/************************************************************************/
/* For example:
GTCitationGeoKey (Ascii,215): "IMAGINE GeoTIFF Support\nCopyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved\n@(#)$RCSfile$ $Revision: 34309 $ $Date: 2016-05-29 11:29:40 -0700 (Sun, 29 May 2016) $\nProjection Name = UTM\nUnits = meters\nGeoTIFF Units = meters"
GeogCitationGeoKey (Ascii,267): "IMAGINE GeoTIFF Support\nCopyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved\n@(#)$RCSfile$ $Revision: 34309 $ $Date: 2016-05-29 11:29:40 -0700 (Sun, 29 May 2016) $\nUnable to match Ellipsoid (Datum) to a GeographicTypeGeoKey value\nEllipsoid = Clarke 1866\nDatum = NAD27 (CONUS)"
PCSCitationGeoKey (Ascii,214): "IMAGINE GeoTIFF Support\nCopyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved\n@(#)$RCSfile$ $Revision: 34309 $ $Date: 2016-05-29 11:29:40 -0700 (Sun, 29 May 2016) $\nUTM Zone 10N\nEllipsoid = Clarke 1866\nDatum = NAD27 (CONUS)"
*/
static void GTIFCleanupImagineNames( char *pszCitation )
{
if( strstr(pszCitation,"IMAGINE GeoTIFF") == nullptr )
return;
/* -------------------------------------------------------------------- */
/* First, we skip past all the copyright, and RCS stuff. We */
/* assume that this will have a "$" at the end of it all. */
/* -------------------------------------------------------------------- */
char *pszSkip = pszCitation + strlen(pszCitation) - 1;
for( ;
pszSkip != pszCitation && *pszSkip != '$';
pszSkip-- ) {}
if( *pszSkip == '$' )
pszSkip++;
if( *pszSkip == '\n' )
pszSkip++;
memmove( pszCitation, pszSkip, strlen(pszSkip)+1 );
/* -------------------------------------------------------------------- */
/* Convert any newlines into spaces, they really gum up the */
/* WKT. */
/* -------------------------------------------------------------------- */
for( int i = 0; pszCitation[i] != '\0'; i++ )
{
if( pszCitation[i] == '\n' )
pszCitation[i] = ' ';
}
}
#if LIBGEOTIFF_VERSION < 1600
/************************************************************************/
/* GDALGTIFKeyGet() */
/************************************************************************/
static int GDALGTIFKeyGet( GTIF *hGTIF, geokey_t key,
void* pData,
int nIndex,
int nCount,
tagtype_t expected_tagtype )
{
tagtype_t tagtype = TYPE_UNKNOWN;
if( !GTIFKeyInfo(hGTIF, key, nullptr, &tagtype) )
return 0;
if( tagtype != expected_tagtype )
{
CPLError( CE_Warning, CPLE_AppDefined,
"Expected key %s to be of type %s. Got %s",
GTIFKeyName(key), GTIFTypeName(expected_tagtype),
GTIFTypeName(tagtype) );
return 0;
}
return GTIFKeyGet( hGTIF, key, pData, nIndex, nCount );
}
/************************************************************************/
/* GDALGTIFKeyGetASCII() */
/************************************************************************/
int GDALGTIFKeyGetASCII( GTIF *hGTIF, geokey_t key,
char* szStr,
int szStrMaxLen )
{
return GDALGTIFKeyGet( hGTIF, key, szStr, 0, szStrMaxLen, TYPE_ASCII );
}
/************************************************************************/
/* GDALGTIFKeyGetSHORT() */
/************************************************************************/
int GDALGTIFKeyGetSHORT( GTIF *hGTIF, geokey_t key,
unsigned short* pnVal,
int nIndex,
int nCount )
{
return GDALGTIFKeyGet( hGTIF, key, pnVal, nIndex, nCount, TYPE_SHORT );
}
/************************************************************************/
/* GDALGTIFKeyGetDOUBLE() */
/************************************************************************/
int GDALGTIFKeyGetDOUBLE( GTIF *hGTIF, geokey_t key,
double* pdfVal,
int nIndex,
int nCount )
{
return GDALGTIFKeyGet( hGTIF, key, pdfVal, nIndex, nCount, TYPE_DOUBLE );
}
#endif
/************************************************************************/
/* FillCompoundCRSWithManualVertCS() */
/************************************************************************/
static void FillCompoundCRSWithManualVertCS( GTIF *hGTIF,
OGRSpatialReference& oSRS,
const char* pszVertCSName,
int verticalDatum,
int verticalUnits )
{
/* -------------------------------------------------------------------- */
/* Setup VERT_CS with citation if present. */
/* -------------------------------------------------------------------- */
oSRS.SetNode( "COMPD_CS|VERT_CS", pszVertCSName );
/* -------------------------------------------------------------------- */
/* Setup the vertical datum. */
/* -------------------------------------------------------------------- */
std::string osVDatumName = "unknown";
const char *pszVDatumType = "2005"; // CS_VD_GeoidModelDerived
if( verticalDatum > 0 && verticalDatum != KvUserDefined )
{
char szCode[12];
snprintf(szCode, sizeof(szCode), "%d", verticalDatum);
auto ctx = static_cast<PJ_CONTEXT*>(
GTIFGetPROJContext(hGTIF, true, nullptr));
auto datum = proj_create_from_database(
ctx, "EPSG", szCode, PJ_CATEGORY_DATUM, 0, nullptr);
if( datum )
{
const char* pszName = proj_get_name(datum);
if( pszName )
{
osVDatumName = pszName;
}
proj_destroy(datum);
}
}
oSRS.SetNode( "COMPD_CS|VERT_CS|VERT_DATUM", osVDatumName.c_str() );
oSRS.GetAttrNode( "COMPD_CS|VERT_CS|VERT_DATUM" )
->AddChild( new OGR_SRSNode( pszVDatumType ) );
if( verticalDatum > 0 && verticalDatum != KvUserDefined )
oSRS.SetAuthority( "COMPD_CS|VERT_CS|VERT_DATUM", "EPSG",
verticalDatum );
/* -------------------------------------------------------------------- */
/* Set the vertical units. */
/* -------------------------------------------------------------------- */
if( verticalUnits > 0 && verticalUnits != KvUserDefined
&& verticalUnits != 9001 )
{
char szCode[12];
snprintf(szCode, sizeof(szCode), "%d", verticalUnits);
auto ctx = static_cast<PJ_CONTEXT*>(
GTIFGetPROJContext(hGTIF, true, nullptr));
const char* pszName = nullptr;
double dfInMeters = 0.0;
if( proj_uom_get_info_from_database(
ctx, "EPSG", szCode, &pszName, &dfInMeters, nullptr) )
{
if( pszName )
oSRS.SetNode( "COMPD_CS|VERT_CS|UNIT", pszName );
char szInMeters[128] = {};
CPLsnprintf( szInMeters, sizeof(szInMeters),
"%.16g", dfInMeters );
oSRS.GetAttrNode( "COMPD_CS|VERT_CS|UNIT" )
->AddChild( new OGR_SRSNode( szInMeters ) );
}
oSRS.SetAuthority( "COMPD_CS|VERT_CS|UNIT", "EPSG", verticalUnits);
}
else
{
oSRS.SetNode( "COMPD_CS|VERT_CS|UNIT", "metre" );
oSRS.GetAttrNode( "COMPD_CS|VERT_CS|UNIT" )
->AddChild( new OGR_SRSNode( "1.0" ) );
oSRS.SetAuthority( "COMPD_CS|VERT_CS|UNIT", "EPSG", 9001 );
}
/* -------------------------------------------------------------------- */
/* Set the axis and VERT_CS authority. */
/* -------------------------------------------------------------------- */
oSRS.SetNode( "COMPD_CS|VERT_CS|AXIS", "Up" );
oSRS.GetAttrNode( "COMPD_CS|VERT_CS|AXIS" )
->AddChild( new OGR_SRSNode( "UP" ) );
}
/************************************************************************/
/* GTIFGetEPSGOfficialName() */
/************************************************************************/
static char* GTIFGetEPSGOfficialName(GTIF *hGTIF, PJ_TYPE searchType,
const char* pszName)
{
char* pszRet = nullptr;
/* Search in database the corresponding EPSG 'official' name */
auto ctx = static_cast<PJ_CONTEXT*>(
GTIFGetPROJContext(hGTIF, true, nullptr));
auto list = proj_create_from_name(ctx, "EPSG", pszName,
&searchType, 1,
false, /* approximate match */
1,
nullptr);
if( list )
{
const auto listSize = proj_list_get_count(list);
if( listSize == 1 )
{
auto obj = proj_list_get(ctx, list, 0);
if( obj )
{
const char* pszOfficialName = proj_get_name(obj);
if( pszOfficialName )
{
pszRet = CPLStrdup(pszOfficialName);
}
}
proj_destroy(obj);
}
proj_list_destroy(list);
}
return pszRet;
}
/************************************************************************/
/* GTIFGetOGISDefnAsOSR() */
/************************************************************************/
OGRSpatialReferenceH GTIFGetOGISDefnAsOSR( GTIF *hGTIF, GTIFDefn * psDefn )
{
OGRSpatialReference oSRS;
LibgeotiffOneTimeInit();
#if LIBGEOTIFF_VERSION >= 1600
void* projContext = GTIFGetPROJContext(hGTIF, FALSE, nullptr);
#endif
/* -------------------------------------------------------------------- */
/* Handle non-standard coordinate systems where GTModelTypeGeoKey */
/* is not defined, but ProjectedCSTypeGeoKey is defined (ticket #3019) */
/* -------------------------------------------------------------------- */
if( psDefn->Model == KvUserDefined && psDefn->PCS != KvUserDefined)
{
psDefn->Model = ModelTypeProjected;
}
/* ==================================================================== */
/* Read keys related to vertical component. */
/* ==================================================================== */
unsigned short verticalCSType = 0;
unsigned short verticalDatum = 0;
unsigned short verticalUnits = 0;
GDALGTIFKeyGetSHORT( hGTIF, VerticalCSTypeGeoKey, &verticalCSType, 0, 1 );
GDALGTIFKeyGetSHORT( hGTIF, VerticalDatumGeoKey, &verticalDatum, 0, 1 );
GDALGTIFKeyGetSHORT( hGTIF, VerticalUnitsGeoKey, &verticalUnits, 0, 1 );
if( verticalCSType != 0 || verticalDatum != 0 || verticalUnits != 0 )
{
int versions[3];
GTIFDirectoryInfo(hGTIF, versions, nullptr);
// GeoTIFF 1.0
if( versions[0] == 1 && versions[1]== 1 && versions[2] == 0 )
{
/* -------------------------------------------------------------------- */
/* The original geotiff specification appears to have */
/* misconstrued the EPSG codes 5101 to 5106 to be vertical */
/* coordinate system codes, when in fact they are vertical */
/* datum codes. So if these are found in the */
/* VerticalCSTypeGeoKey move them to the VerticalDatumGeoKey */
/* and insert the "normal" corresponding VerticalCSTypeGeoKey */
/* value. */
/* -------------------------------------------------------------------- */
if( (verticalCSType >= 5101 && verticalCSType <= 5112)
&& verticalDatum == 0 )
{
verticalDatum = verticalCSType;
verticalCSType = verticalDatum + 600;
}
/* -------------------------------------------------------------------- */
/* This addresses another case where the EGM96 Vertical Datum code */
/* is misused as a Vertical CS code (#4922). */
/* -------------------------------------------------------------------- */
if( verticalCSType == 5171 )
{
verticalDatum = 5171;
verticalCSType = 5773;
}
}
/* -------------------------------------------------------------------- */
/* Somewhat similarly, codes 5001 to 5033 were treated as */
/* vertical coordinate systems based on ellipsoidal heights. */
/* We use the corresponding geodetic datum as the vertical */
/* datum and clear the vertical coordinate system code since */
/* there isn't one in EPSG. */
/* -------------------------------------------------------------------- */
if( (verticalCSType >= 5001 && verticalCSType <= 5033)
&& verticalDatum == 0 )
{
verticalDatum = verticalCSType + 1000;
verticalCSType = 0;
}
}
/* -------------------------------------------------------------------- */
/* Handle non-standard coordinate systems as LOCAL_CS. */
/* -------------------------------------------------------------------- */
if( psDefn->Model != ModelTypeProjected
&& psDefn->Model != ModelTypeGeographic
&& psDefn->Model != ModelTypeGeocentric )
{
char szPeStr[2400] = { '\0' };
/** check if there is a pe string citation key **/
if( GDALGTIFKeyGetASCII( hGTIF, PCSCitationGeoKey, szPeStr,
sizeof(szPeStr) ) &&
strstr(szPeStr, "ESRI PE String = " ) )
{
const char* pszWKT = szPeStr + strlen("ESRI PE String = ");
oSRS.importFromWkt(pszWKT);
if( strstr( pszWKT,
"PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\"" ) )
{
oSRS.SetExtension(
"PROJCS", "PROJ4",
"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 "
"+x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null "
"+wktext +no_defs" ); // TODO(schwehr): Why 2 spaces?
}
return OGRSpatialReference::ToHandle(oSRS.Clone());
}
else
{
char *pszUnitsName = nullptr;
char szPCSName[300] = { '\0' };
int nKeyCount = 0;
int anVersion[3] = { 0 };
GTIFDirectoryInfo( hGTIF, anVersion, &nKeyCount );
if( nKeyCount > 0 ) // Use LOCAL_CS if we have any geokeys at all.
{
// Handle citation.
strcpy( szPCSName, "unnamed" );
if( !GDALGTIFKeyGetASCII( hGTIF, GTCitationGeoKey, szPCSName,
sizeof(szPCSName) ) )
GDALGTIFKeyGetASCII( hGTIF, GeogCitationGeoKey, szPCSName,
sizeof(szPCSName) );
GTIFCleanupImagineNames( szPCSName );
oSRS.SetLocalCS( szPCSName );
// Handle units
if( psDefn->UOMLength != KvUserDefined )
{
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetUOMLengthInfoEx( projContext,
#else
GTIFGetUOMLengthInfo(
#endif
psDefn->UOMLength, &pszUnitsName, nullptr );
}
if( pszUnitsName != nullptr )
{
char szUOMLength[12];
snprintf(szUOMLength, sizeof(szUOMLength),
"%d", psDefn->UOMLength );
oSRS.SetTargetLinearUnits(
nullptr, pszUnitsName, psDefn->UOMLengthInMeters,
"EPSG", szUOMLength);
}
else
oSRS.SetLinearUnits( "unknown", psDefn->UOMLengthInMeters );
if( verticalUnits != 0 )
{
char szVertCSCitation[2048] = {0};
if( GDALGTIFKeyGetASCII( hGTIF, VerticalCitationGeoKey, szVertCSCitation,
sizeof(szVertCSCitation) ) )
{
if( STARTS_WITH_CI(szVertCSCitation, "VCS Name = ") )
{
memmove(szVertCSCitation, szVertCSCitation + strlen("VCS Name = "),
strlen(szVertCSCitation + strlen("VCS Name = ")) + 1);
char* pszPipeChar = strchr(szVertCSCitation, '|');
if( pszPipeChar )
*pszPipeChar = '\0';
}
}
else
{
strcpy( szVertCSCitation, "unknown" );
}
const char* pszHorizontalName = oSRS.GetName();
const std::string osHorizontalName( pszHorizontalName ? pszHorizontalName : "unnamed" );
/* -------------------------------------------------------------------- */
/* Promote to being a compound coordinate system. */
/* -------------------------------------------------------------------- */
OGR_SRSNode *poOldRoot = oSRS.GetRoot()->Clone();
oSRS.Clear();
/* -------------------------------------------------------------------- */
/* Set COMPD_CS name. */
/* -------------------------------------------------------------------- */
char szCTString[512];
szCTString[0] = '\0';
if( GDALGTIFKeyGetASCII( hGTIF, GTCitationGeoKey, szCTString,
sizeof(szCTString) ) &&
strstr( szCTString, " = " ) == nullptr )
{
oSRS.SetNode( "COMPD_CS", szCTString );
}
else
{
oSRS.SetNode( "COMPD_CS", (osHorizontalName + " + " + szVertCSCitation).c_str() );
}
oSRS.GetRoot()->AddChild( poOldRoot );
FillCompoundCRSWithManualVertCS(hGTIF, oSRS,
szVertCSCitation,
verticalDatum, verticalUnits);
}
GTIFFreeMemory( pszUnitsName );
}
return OGRSpatialReference::ToHandle(oSRS.Clone());
}
}
/* -------------------------------------------------------------------- */
/* Handle Geocentric coordinate systems. */
/* -------------------------------------------------------------------- */
if( psDefn->Model == ModelTypeGeocentric )
{
char szName[300] = { '\0' };
strcpy( szName, "unnamed" );
if( !GDALGTIFKeyGetASCII( hGTIF, GTCitationGeoKey, szName,
sizeof(szName) ) )
GDALGTIFKeyGetASCII( hGTIF, GeogCitationGeoKey, szName,
sizeof(szName) );
oSRS.SetGeocCS( szName );
char *pszUnitsName = nullptr;
if( psDefn->UOMLength != KvUserDefined )
{
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetUOMLengthInfoEx( projContext,
#else
GTIFGetUOMLengthInfo(
#endif
psDefn->UOMLength, &pszUnitsName, nullptr );
}
if( pszUnitsName != nullptr )
{
char szUOMLength[12];
snprintf(szUOMLength, sizeof(szUOMLength),
"%d", psDefn->UOMLength );
oSRS.SetTargetLinearUnits( nullptr,
pszUnitsName, psDefn->UOMLengthInMeters, "EPSG", szUOMLength);
}
else
oSRS.SetLinearUnits( "unknown", psDefn->UOMLengthInMeters );
GTIFFreeMemory( pszUnitsName );
}
/* -------------------------------------------------------------------- */
/* #3901: In libgeotiff 1.3.0 and earlier we incorrectly */
/* interpreted linear projection parameter geokeys (false */
/* easting/northing) as being in meters instead of the */
/* coordinate system of the file. The following code attempts */
/* to provide mechanisms for fixing the issue if we are linked */
/* with an older version of libgeotiff. */
/* -------------------------------------------------------------------- */
const char *pszLinearUnits =
CPLGetConfigOption( "GTIFF_LINEAR_UNITS", "DEFAULT" );
/* -------------------------------------------------------------------- */
/* #3901: If folks have broken GeoTIFF files generated with */
/* older versions of GDAL+libgeotiff, then they may need a */
/* hack to allow them to be read properly. This is that */
/* hack. We basically try to undue the conversion applied by */
/* libgeotiff to meters (or above) to simulate the old */
/* behavior. */
/* -------------------------------------------------------------------- */
unsigned short bLinearUnitsMarkedCorrect = FALSE;
GDALGTIFKeyGetSHORT(hGTIF, ProjLinearUnitsInterpCorrectGeoKey,
&bLinearUnitsMarkedCorrect, 0, 1);
if( EQUAL(pszLinearUnits,"BROKEN")
&& psDefn->Projection == KvUserDefined
&& !bLinearUnitsMarkedCorrect )
{
for( int iParam = 0; iParam < psDefn->nParms; iParam++ )
{
switch( psDefn->ProjParmId[iParam] )
{
case ProjFalseEastingGeoKey:
case ProjFalseNorthingGeoKey:
case ProjFalseOriginEastingGeoKey:
case ProjFalseOriginNorthingGeoKey:
case ProjCenterEastingGeoKey:
case ProjCenterNorthingGeoKey:
if( psDefn->UOMLengthInMeters != 0
&& psDefn->UOMLengthInMeters != 1.0 )
{
psDefn->ProjParm[iParam] /= psDefn->UOMLengthInMeters;
CPLDebug(
"GTIFF",
"Converting geokey to accommodate old broken file "
"due to GTIFF_LINEAR_UNITS=BROKEN setting." );
}
break;
default:
break;
}
}
}
/* -------------------------------------------------------------------- */
/* If this is a projected SRS we set the PROJCS keyword first */
/* to ensure that the GEOGCS will be a child. */
/* -------------------------------------------------------------------- */
OGRBoolean linearUnitIsSet = FALSE;
if( psDefn->Model == ModelTypeProjected )
{
char szCTString[512] = { '\0' };
if( psDefn->PCS != KvUserDefined )
{
char *pszPCSName = nullptr;
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetPCSInfoEx( projContext,
#else
GTIFGetPCSInfo(
#endif
psDefn->PCS, &pszPCSName, nullptr, nullptr, nullptr );
oSRS.SetProjCS( pszPCSName ? pszPCSName : "unnamed" );
if ( pszPCSName )
GTIFFreeMemory( pszPCSName );
oSRS.SetLinearUnits("unknown", 1.0);
}
else
{
bool bTryGTCitationGeoKey = true;
if( GDALGTIFKeyGetASCII( hGTIF, PCSCitationGeoKey,
szCTString,
sizeof(szCTString)) )
{
bTryGTCitationGeoKey = false;
if (!SetCitationToSRS( hGTIF, szCTString, sizeof(szCTString),
PCSCitationGeoKey, &oSRS,
&linearUnitIsSet) )
{
if( !STARTS_WITH_CI(szCTString, "LUnits = ") )
{
oSRS.SetProjCS( szCTString );
oSRS.SetLinearUnits("unknown", 1.0);
}
else
{
bTryGTCitationGeoKey = true;
}
}
}
if( bTryGTCitationGeoKey )
{
if( GDALGTIFKeyGetASCII( hGTIF, GTCitationGeoKey, szCTString,
sizeof(szCTString) ) &&
!SetCitationToSRS( hGTIF, szCTString, sizeof(szCTString),
GTCitationGeoKey, &oSRS,
&linearUnitIsSet ) )
{
oSRS.SetNode( "PROJCS", szCTString );
oSRS.SetLinearUnits("unknown", 1.0);
}
else
{
oSRS.SetNode( "PROJCS", "unnamed" );
oSRS.SetLinearUnits("unknown", 1.0);
}
}
}
/* Handle ESRI/Erdas style state plane and UTM in citation key */
if( CheckCitationKeyForStatePlaneUTM( hGTIF, psDefn, &oSRS,
&linearUnitIsSet ) )
{
return OGRSpatialReference::ToHandle(oSRS.Clone());
}
/* Handle ESRI PE string in citation */
szCTString[0] = '\0';
if( GDALGTIFKeyGetASCII( hGTIF, GTCitationGeoKey, szCTString,
sizeof(szCTString) ) )
SetCitationToSRS( hGTIF, szCTString, sizeof(szCTString),
GTCitationGeoKey, &oSRS, &linearUnitIsSet );
}
/* ==================================================================== */
/* Setup the GeogCS */
/* ==================================================================== */
char *pszGeogName = nullptr;
char *pszDatumName = nullptr;
char *pszPMName = nullptr;
char *pszSpheroidName = nullptr;
char *pszAngularUnits = nullptr;
char szGCSName[512] = { '\0' };
if( !
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetGCSInfoEx( projContext,
#else
GTIFGetGCSInfo(
#endif
psDefn->GCS, &pszGeogName, nullptr, nullptr, nullptr )
&& GDALGTIFKeyGetASCII( hGTIF, GeogCitationGeoKey, szGCSName,
sizeof(szGCSName)) )
{
GetGeogCSFromCitation(szGCSName, sizeof(szGCSName),
GeogCitationGeoKey,
&pszGeogName, &pszDatumName,
&pszPMName, &pszSpheroidName,
&pszAngularUnits);
}
else
{
GTIFToCPLRecycleString( &pszGeogName );
}
if( pszGeogName && STARTS_WITH(pszGeogName, "GCS_") )
{
// Morph from potential ESRI name
char* pszOfficialName = GTIFGetEPSGOfficialName(
hGTIF, PJ_TYPE_GEOGRAPHIC_2D_CRS, pszGeogName);
if( pszOfficialName )
{
CPLFree(pszGeogName);
pszGeogName = pszOfficialName;
}
}
if( pszDatumName && strchr(pszDatumName, '_') )
{
// Morph from potential ESRI name
char* pszOfficialName = GTIFGetEPSGOfficialName(
hGTIF, PJ_TYPE_GEODETIC_REFERENCE_FRAME, pszDatumName);
if( pszOfficialName )
{
CPLFree(pszDatumName);
pszDatumName = pszOfficialName;
}
}
if( pszSpheroidName && strchr(pszSpheroidName, '_') )
{
// Morph from potential ESRI name
char* pszOfficialName = GTIFGetEPSGOfficialName(
hGTIF, PJ_TYPE_ELLIPSOID, pszSpheroidName);
if( pszOfficialName )
{
CPLFree(pszSpheroidName);
pszSpheroidName = pszOfficialName;
}
}
if( !pszDatumName )
{
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetDatumInfoEx( projContext,
#else
GTIFGetDatumInfo(
#endif
psDefn->Datum, &pszDatumName, nullptr );
GTIFToCPLRecycleString( &pszDatumName );
}
double dfSemiMajor = 0.0;
double dfInvFlattening = 0.0;
if( !pszSpheroidName )
{
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetEllipsoidInfoEx( projContext,
#else
GTIFGetEllipsoidInfo(
#endif
psDefn->Ellipsoid, &pszSpheroidName, nullptr, nullptr );
GTIFToCPLRecycleString( &pszSpheroidName );
}
else
{
CPL_IGNORE_RET_VAL(
GDALGTIFKeyGetDOUBLE( hGTIF, GeogSemiMajorAxisGeoKey,
&(psDefn->SemiMajor), 0, 1 ));
CPL_IGNORE_RET_VAL(
GDALGTIFKeyGetDOUBLE( hGTIF, GeogInvFlatteningGeoKey,
&dfInvFlattening, 0, 1 ));
if( std::isinf(dfInvFlattening) )
{
// Deal with the non-nominal case of
// https://github.com/OSGeo/PROJ/issues/2317
dfInvFlattening = 0;
}
}
if( !pszPMName )
{
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetPMInfoEx( projContext,
#else
GTIFGetPMInfo(
#endif
psDefn->PM, &pszPMName, nullptr );
GTIFToCPLRecycleString( &pszPMName );
}
else
{
CPL_IGNORE_RET_VAL(
GDALGTIFKeyGetDOUBLE( hGTIF, GeogPrimeMeridianLongGeoKey,
&(psDefn->PMLongToGreenwich), 0, 1 ));
}
if( !pszAngularUnits )
{
#if LIBGEOTIFF_VERSION >= 1600
GTIFGetUOMAngleInfoEx( projContext,
#else
GTIFGetUOMAngleInfo(
#endif
psDefn->UOMAngle, &pszAngularUnits, &psDefn->UOMAngleInDegrees );
if( pszAngularUnits == nullptr )
pszAngularUnits = CPLStrdup("unknown");
else
GTIFToCPLRecycleString( &pszAngularUnits );
}
else
{
double dfRadians = 0.0;
if( GDALGTIFKeyGetDOUBLE(hGTIF, GeogAngularUnitSizeGeoKey, &dfRadians,
0, 1) )
{
psDefn->UOMAngleInDegrees = dfRadians / CPLAtof(SRS_UA_DEGREE_CONV);
}
}
// Avoid later division by zero.
if( psDefn->UOMAngleInDegrees == 0 )
{
CPLError(CE_Warning, CPLE_AppDefined,
"Invalid value for GeogAngularUnitSizeGeoKey.");
psDefn->UOMAngleInDegrees = 1;
}
dfSemiMajor = psDefn->SemiMajor;
if( dfSemiMajor == 0.0 )
{
CPLFree(pszSpheroidName);
pszSpheroidName = CPLStrdup("unretrievable - using WGS84");