-
Notifications
You must be signed in to change notification settings - Fork 2
/
geo_normalize.c
2593 lines (2189 loc) · 97 KB
/
geo_normalize.c
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
/******************************************************************************
* $Id: geo_normalize.c 2037 2011-05-24 01:45:24Z warmerdam $
*
* Project: libgeotiff
* Purpose: Code to normalize PCS and other composite codes in a GeoTIFF file.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
*
* 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_serv.h"
#include "geo_tiffp.h"
#include "geovalues.h"
#include "geo_normalize.h"
#ifndef KvUserDefined
# define KvUserDefined 32767
#endif
#ifndef PI
# define PI 3.14159265358979323846
#endif
/* EPSG Codes for projection parameters. Unfortunately, these bear no
relationship to the GeoTIFF codes even though the names are so similar. */
#define EPSGNatOriginLat 8801
#define EPSGNatOriginLong 8802
#define EPSGNatOriginScaleFactor 8805
#define EPSGFalseEasting 8806
#define EPSGFalseNorthing 8807
#define EPSGProjCenterLat 8811
#define EPSGProjCenterLong 8812
#define EPSGAzimuth 8813
#define EPSGAngleRectifiedToSkewedGrid 8814
#define EPSGInitialLineScaleFactor 8815
#define EPSGProjCenterEasting 8816
#define EPSGProjCenterNorthing 8817
#define EPSGPseudoStdParallelLat 8818
#define EPSGPseudoStdParallelScaleFactor 8819
#define EPSGFalseOriginLat 8821
#define EPSGFalseOriginLong 8822
#define EPSGStdParallel1Lat 8823
#define EPSGStdParallel2Lat 8824
#define EPSGFalseOriginEasting 8826
#define EPSGFalseOriginNorthing 8827
#define EPSGSphericalOriginLat 8828
#define EPSGSphericalOriginLong 8829
#define EPSGInitialLongitude 8830
#define EPSGZoneWidth 8831
#define EPSGLatOfStdParallel 8832
#define EPSGOriginLong 8833
#define EPSGTopocentricOriginLat 8834
#define EPSGTopocentricOriginLong 8835
#define EPSGTopocentricOriginHeight 8836
/************************************************************************/
/* GTIFGetPCSInfo() */
/************************************************************************/
int GTIFGetPCSInfo( int nPCSCode, char **ppszEPSGName,
short *pnProjOp, short *pnUOMLengthCode,
short *pnGeogCS )
{
char **papszRecord;
char szSearchKey[24];
const char *pszFilename;
int nDatum;
int nZone;
int Proj = GTIFPCSToMapSys( nPCSCode, &nDatum, &nZone );
if ((Proj == MapSys_UTM_North || Proj == MapSys_UTM_South) &&
nDatum != KvUserDefined)
{
const char* pszDatumName = NULL;
switch (nDatum)
{
case GCS_NAD27: pszDatumName = "NAD27"; break;
case GCS_NAD83: pszDatumName = "NAD83"; break;
case GCS_WGS_72: pszDatumName = "WGS 72"; break;
case GCS_WGS_72BE: pszDatumName = "WGS 72BE"; break;
case GCS_WGS_84: pszDatumName = "WGS 84"; break;
default: break;
}
if (pszDatumName)
{
if (ppszEPSGName)
{
char szEPSGName[64];
sprintf(szEPSGName, "%s / UTM zone %d%c",
pszDatumName, nZone, (Proj == MapSys_UTM_North) ? 'N' : 'S');
*ppszEPSGName = CPLStrdup(szEPSGName);
}
if (pnProjOp)
*pnProjOp = (short) (((Proj == MapSys_UTM_North) ? Proj_UTM_zone_1N - 1 : Proj_UTM_zone_1S - 1) + nZone);
if (pnUOMLengthCode)
*pnUOMLengthCode = 9001; /* Linear_Meter */
if (pnGeogCS)
*pnGeogCS = (short) nDatum;
return TRUE;
}
}
/* -------------------------------------------------------------------- */
/* Search the pcs.override table for this PCS. */
/* -------------------------------------------------------------------- */
pszFilename = CSVFilename( "pcs.override.csv" );
sprintf( szSearchKey, "%d", nPCSCode );
papszRecord = CSVScanFileByName( pszFilename, "COORD_REF_SYS_CODE",
szSearchKey, CC_Integer );
/* -------------------------------------------------------------------- */
/* If not found, search the EPSG PCS database. */
/* -------------------------------------------------------------------- */
if( papszRecord == NULL )
{
pszFilename = CSVFilename( "pcs.csv" );
sprintf( szSearchKey, "%d", nPCSCode );
papszRecord = CSVScanFileByName( pszFilename, "COORD_REF_SYS_CODE",
szSearchKey, CC_Integer );
if( papszRecord == NULL )
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Get the name, if requested. */
/* -------------------------------------------------------------------- */
if( ppszEPSGName != NULL )
{
*ppszEPSGName =
CPLStrdup( CSLGetField( papszRecord,
CSVGetFileFieldId(pszFilename,
"COORD_REF_SYS_NAME") ));
}
/* -------------------------------------------------------------------- */
/* Get the UOM Length code, if requested. */
/* -------------------------------------------------------------------- */
if( pnUOMLengthCode != NULL )
{
const char *pszValue;
pszValue =
CSLGetField( papszRecord,
CSVGetFileFieldId(pszFilename,"UOM_CODE"));
if( atoi(pszValue) > 0 )
*pnUOMLengthCode = (short) atoi(pszValue);
else
*pnUOMLengthCode = KvUserDefined;
}
/* -------------------------------------------------------------------- */
/* Get the UOM Length code, if requested. */
/* -------------------------------------------------------------------- */
if( pnProjOp != NULL )
{
const char *pszValue;
pszValue =
CSLGetField( papszRecord,
CSVGetFileFieldId(pszFilename,"COORD_OP_CODE"));
if( atoi(pszValue) > 0 )
*pnProjOp = (short) atoi(pszValue);
else
*pnUOMLengthCode = KvUserDefined;
}
/* -------------------------------------------------------------------- */
/* Get the GeogCS (Datum with PM) code, if requested. */
/* -------------------------------------------------------------------- */
if( pnGeogCS != NULL )
{
const char *pszValue;
pszValue =
CSLGetField( papszRecord,
CSVGetFileFieldId(pszFilename,"SOURCE_GEOGCRS_CODE"));
if( atoi(pszValue) > 0 )
*pnGeogCS = (short) atoi(pszValue);
else
*pnGeogCS = KvUserDefined;
}
return TRUE;
}
/************************************************************************/
/* GTIFAngleToDD() */
/* */
/* Convert a numeric angle to decimal degress. */
/************************************************************************/
double GTIFAngleToDD( double dfAngle, int nUOMAngle )
{
if( nUOMAngle == 9110 ) /* DDD.MMSSsss */
{
char szAngleString[32];
sprintf( szAngleString, "%12.7f", dfAngle );
dfAngle = GTIFAngleStringToDD( szAngleString, nUOMAngle );
}
else if ( nUOMAngle != KvUserDefined )
{
double dfInDegrees = 1.0;
GTIFGetUOMAngleInfo( nUOMAngle, NULL, &dfInDegrees );
dfAngle = dfAngle * dfInDegrees;
}
return( dfAngle );
}
/************************************************************************/
/* GTIFAngleStringToDD() */
/* */
/* Convert an angle in the specified units to decimal degrees. */
/************************************************************************/
double GTIFAngleStringToDD( const char * pszAngle, int nUOMAngle )
{
double dfAngle;
if( nUOMAngle == 9110 ) /* DDD.MMSSsss */
{
char *pszDecimal;
dfAngle = ABS(atoi(pszAngle));
pszDecimal = strchr(pszAngle,'.');
if( pszDecimal != NULL && strlen(pszDecimal) > 1 )
{
char szMinutes[3];
char szSeconds[64];
szMinutes[0] = pszDecimal[1];
if( pszDecimal[2] >= '0' && pszDecimal[2] <= '9' )
szMinutes[1] = pszDecimal[2];
else
szMinutes[1] = '0';
szMinutes[2] = '\0';
dfAngle += atoi(szMinutes) / 60.0;
if( strlen(pszDecimal) > 3 )
{
szSeconds[0] = pszDecimal[3];
if( pszDecimal[4] >= '0' && pszDecimal[4] <= '9' )
{
szSeconds[1] = pszDecimal[4];
szSeconds[2] = '.';
strncpy( szSeconds+3, pszDecimal + 5, sizeof(szSeconds) - 3 );
szSeconds[sizeof(szSeconds) - 1] = 0;
}
else
{
szSeconds[1] = '0';
szSeconds[2] = '\0';
}
dfAngle += GTIFAtof(szSeconds) / 3600.0;
}
}
if( pszAngle[0] == '-' )
dfAngle *= -1;
}
else if( nUOMAngle == 9105 || nUOMAngle == 9106 ) /* grad */
{
dfAngle = 180 * (GTIFAtof(pszAngle ) / 200);
}
else if( nUOMAngle == 9101 ) /* radians */
{
dfAngle = 180 * (GTIFAtof(pszAngle ) / PI);
}
else if( nUOMAngle == 9103 ) /* arc-minute */
{
dfAngle = GTIFAtof(pszAngle) / 60;
}
else if( nUOMAngle == 9104 ) /* arc-second */
{
dfAngle = GTIFAtof(pszAngle) / 3600;
}
else /* decimal degrees ... some cases missing but seeminly never used */
{
CPLAssert( nUOMAngle == 9102 || nUOMAngle == KvUserDefined
|| nUOMAngle == 0 );
dfAngle = GTIFAtof(pszAngle );
}
return( dfAngle );
}
/************************************************************************/
/* GTIFGetGCSInfo() */
/* */
/* Fetch the datum, and prime meridian related to a particular */
/* GCS. */
/************************************************************************/
int GTIFGetGCSInfo( int nGCSCode, char ** ppszName,
short * pnDatum, short * pnPM, short *pnUOMAngle )
{
char szSearchKey[24];
int nDatum=0, nPM, nUOMAngle;
const char *pszFilename;
/* -------------------------------------------------------------------- */
/* Handle some "well known" GCS codes directly */
/* -------------------------------------------------------------------- */
const char * pszName = NULL;
nPM = PM_Greenwich;
nUOMAngle = Angular_DMS_Hemisphere;
if( nGCSCode == GCS_NAD27 )
{
nDatum = Datum_North_American_Datum_1927;
pszName = "NAD27";
}
else if( nGCSCode == GCS_NAD83 )
{
nDatum = Datum_North_American_Datum_1983;
pszName = "NAD83";
}
else if( nGCSCode == GCS_WGS_84 )
{
nDatum = Datum_WGS84;
pszName = "WGS 84";
}
else if( nGCSCode == GCS_WGS_72 )
{
nDatum = Datum_WGS72;
pszName = "WGS 72";
}
else if ( nGCSCode == KvUserDefined )
{
return FALSE;
}
if (pszName != NULL)
{
if( ppszName != NULL )
*ppszName = CPLStrdup( pszName );
if( pnDatum != NULL )
*pnDatum = (short) nDatum;
if( pnPM != NULL )
*pnPM = (short) nPM;
if( pnUOMAngle != NULL )
*pnUOMAngle = (short) nUOMAngle;
return TRUE;
}
/* -------------------------------------------------------------------- */
/* Search the database for the corresponding datum code. */
/* -------------------------------------------------------------------- */
pszFilename = CSVFilename("gcs.override.csv");
sprintf( szSearchKey, "%d", nGCSCode );
nDatum = atoi(CSVGetField( pszFilename,
"COORD_REF_SYS_CODE", szSearchKey,
CC_Integer, "DATUM_CODE" ) );
if( nDatum < 1 )
{
pszFilename = CSVFilename("gcs.csv");
sprintf( szSearchKey, "%d", nGCSCode );
nDatum = atoi(CSVGetField( pszFilename,
"COORD_REF_SYS_CODE", szSearchKey,
CC_Integer, "DATUM_CODE" ) );
}
if( nDatum < 1 )
{
return FALSE;
}
if( pnDatum != NULL )
*pnDatum = (short) nDatum;
/* -------------------------------------------------------------------- */
/* Get the PM. */
/* -------------------------------------------------------------------- */
if( pnPM != NULL )
{
nPM = atoi(CSVGetField( pszFilename,
"COORD_REF_SYS_CODE", szSearchKey, CC_Integer,
"PRIME_MERIDIAN_CODE" ) );
if( nPM < 1 )
return FALSE;
*pnPM = (short) nPM;
}
/* -------------------------------------------------------------------- */
/* Get the angular units. */
/* -------------------------------------------------------------------- */
nUOMAngle = atoi(CSVGetField( pszFilename,
"COORD_REF_SYS_CODE",szSearchKey, CC_Integer,
"UOM_CODE" ) );
if( nUOMAngle < 1 )
return FALSE;
if( pnUOMAngle != NULL )
*pnUOMAngle = (short) nUOMAngle;
/* -------------------------------------------------------------------- */
/* Get the name, if requested. */
/* -------------------------------------------------------------------- */
if( ppszName != NULL )
*ppszName =
CPLStrdup(CSVGetField( pszFilename,
"COORD_REF_SYS_CODE",szSearchKey,CC_Integer,
"COORD_REF_SYS_NAME" ));
return( TRUE );
}
/************************************************************************/
/* GTIFGetEllipsoidInfo() */
/* */
/* Fetch info about an ellipsoid. Axes are always returned in */
/* meters. SemiMajor computed based on inverse flattening */
/* where that is provided. */
/************************************************************************/
int GTIFGetEllipsoidInfo( int nEllipseCode, char ** ppszName,
double * pdfSemiMajor, double * pdfSemiMinor )
{
char szSearchKey[24];
double dfSemiMajor=0.0, dfToMeters = 1.0;
int nUOMLength;
const char* pszFilename;
/* -------------------------------------------------------------------- */
/* Try some well known ellipsoids. */
/* -------------------------------------------------------------------- */
double dfInvFlattening=0.0, dfSemiMinor=0.0;
const char *pszName = NULL;
if( nEllipseCode == Ellipse_Clarke_1866 )
{
pszName = "Clarke 1866";
dfSemiMajor = 6378206.4;
dfSemiMinor = 6356583.8;
dfInvFlattening = 0.0;
}
else if( nEllipseCode == Ellipse_GRS_1980 )
{
pszName = "GRS 1980";
dfSemiMajor = 6378137.0;
dfSemiMinor = 0.0;
dfInvFlattening = 298.257222101;
}
else if( nEllipseCode == Ellipse_WGS_84 )
{
pszName = "WGS 84";
dfSemiMajor = 6378137.0;
dfSemiMinor = 0.0;
dfInvFlattening = 298.257223563;
}
else if( nEllipseCode == 7043 )
{
pszName = "WGS 72";
dfSemiMajor = 6378135.0;
dfSemiMinor = 0.0;
dfInvFlattening = 298.26;
}
if (pszName != NULL)
{
if( dfSemiMinor == 0.0 )
dfSemiMinor = dfSemiMajor * (1 - 1.0/dfInvFlattening);
if( pdfSemiMinor != NULL )
*pdfSemiMinor = dfSemiMinor;
if( pdfSemiMajor != NULL )
*pdfSemiMajor = dfSemiMajor;
if( ppszName != NULL )
*ppszName = CPLStrdup( pszName );
return TRUE;
}
/* -------------------------------------------------------------------- */
/* Get the semi major axis. */
/* -------------------------------------------------------------------- */
sprintf( szSearchKey, "%d", nEllipseCode );
pszFilename = CSVFilename("ellipsoid.csv" );
dfSemiMajor =
GTIFAtof(CSVGetField( pszFilename,
"ELLIPSOID_CODE", szSearchKey, CC_Integer,
"SEMI_MAJOR_AXIS" ) );
if( dfSemiMajor == 0.0 )
{
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Get the translation factor into meters. */
/* -------------------------------------------------------------------- */
nUOMLength = atoi(CSVGetField( pszFilename,
"ELLIPSOID_CODE", szSearchKey, CC_Integer,
"UOM_CODE" ));
GTIFGetUOMLengthInfo( nUOMLength, NULL, &dfToMeters );
dfSemiMajor *= dfToMeters;
if( pdfSemiMajor != NULL )
*pdfSemiMajor = dfSemiMajor;
/* -------------------------------------------------------------------- */
/* Get the semi-minor if requested. If the Semi-minor axis */
/* isn't available, compute it based on the inverse flattening. */
/* -------------------------------------------------------------------- */
if( pdfSemiMinor != NULL )
{
*pdfSemiMinor =
GTIFAtof(CSVGetField( pszFilename,
"ELLIPSOID_CODE", szSearchKey, CC_Integer,
"SEMI_MINOR_AXIS" )) * dfToMeters;
if( *pdfSemiMinor == 0.0 )
{
double dfInvFlattening;
dfInvFlattening =
GTIFAtof(CSVGetField( pszFilename,
"ELLIPSOID_CODE", szSearchKey, CC_Integer,
"INV_FLATTENING" ));
*pdfSemiMinor = dfSemiMajor * (1 - 1.0/dfInvFlattening);
}
}
/* -------------------------------------------------------------------- */
/* Get the name, if requested. */
/* -------------------------------------------------------------------- */
if( ppszName != NULL )
*ppszName =
CPLStrdup(CSVGetField( pszFilename,
"ELLIPSOID_CODE", szSearchKey, CC_Integer,
"ELLIPSOID_NAME" ));
return( TRUE );
}
/************************************************************************/
/* GTIFGetPMInfo() */
/* */
/* Get the offset between a given prime meridian and Greenwich */
/* in degrees. */
/************************************************************************/
int GTIFGetPMInfo( int nPMCode, char ** ppszName, double *pdfOffset )
{
char szSearchKey[24];
int nUOMAngle;
const char *pszFilename;
/* -------------------------------------------------------------------- */
/* Use a special short cut for Greenwich, since it is so common. */
/* -------------------------------------------------------------------- */
if( nPMCode == PM_Greenwich )
{
if( pdfOffset != NULL )
*pdfOffset = 0.0;
if( ppszName != NULL )
*ppszName = CPLStrdup( "Greenwich" );
return TRUE;
}
/* -------------------------------------------------------------------- */
/* Search the database for the corresponding datum code. */
/* -------------------------------------------------------------------- */
pszFilename = CSVFilename("prime_meridian.csv");
sprintf( szSearchKey, "%d", nPMCode );
nUOMAngle =
atoi(CSVGetField( pszFilename,
"PRIME_MERIDIAN_CODE", szSearchKey, CC_Integer,
"UOM_CODE" ) );
if( nUOMAngle < 1 )
return FALSE;
/* -------------------------------------------------------------------- */
/* Get the PM offset. */
/* -------------------------------------------------------------------- */
if( pdfOffset != NULL )
{
*pdfOffset =
GTIFAngleStringToDD(
CSVGetField( pszFilename,
"PRIME_MERIDIAN_CODE", szSearchKey, CC_Integer,
"GREENWICH_LONGITUDE" ),
nUOMAngle );
}
/* -------------------------------------------------------------------- */
/* Get the name, if requested. */
/* -------------------------------------------------------------------- */
if( ppszName != NULL )
*ppszName =
CPLStrdup(
CSVGetField( pszFilename,
"PRIME_MERIDIAN_CODE", szSearchKey, CC_Integer,
"PRIME_MERIDIAN_NAME" ));
return( TRUE );
}
/************************************************************************/
/* GTIFGetDatumInfo() */
/* */
/* Fetch the ellipsoid, and name for a datum. */
/************************************************************************/
int GTIFGetDatumInfo( int nDatumCode, char ** ppszName, short * pnEllipsoid )
{
char szSearchKey[24];
int nEllipsoid = 0;
const char *pszFilename;
FILE *fp;
const char *pszName = NULL;
/* -------------------------------------------------------------------- */
/* Handle a few built-in datums. */
/* -------------------------------------------------------------------- */
if( nDatumCode == Datum_North_American_Datum_1927 )
{
nEllipsoid = Ellipse_Clarke_1866;
pszName = "North American Datum 1927";
}
else if( nDatumCode == Datum_North_American_Datum_1983 )
{
nEllipsoid = Ellipse_GRS_1980;
pszName = "North American Datum 1983";
}
else if( nDatumCode == Datum_WGS84 )
{
nEllipsoid = Ellipse_WGS_84;
pszName = "World Geodetic System 1984";
}
else if( nDatumCode == Datum_WGS72 )
{
nEllipsoid = 7043; /* WGS72 */
pszName = "World Geodetic System 1972";
}
if (pszName != NULL)
{
if( pnEllipsoid != NULL )
*pnEllipsoid = (short) nEllipsoid;
if( ppszName != NULL )
*ppszName = CPLStrdup( pszName );
return TRUE;
}
/* -------------------------------------------------------------------- */
/* If we can't find datum.csv then gdal_datum.csv is an */
/* acceptable fallback. Mostly this is for GDAL. */
/* -------------------------------------------------------------------- */
pszFilename = CSVFilename( "datum.csv" );
if( (fp = VSIFOpen(pszFilename,"r")) == NULL )
{
if( (fp = VSIFOpen(CSVFilename("gdal_datum.csv"), "r")) != NULL )
{
pszFilename = CSVFilename( "gdal_datum.csv" );
VSIFClose( fp );
}
}
else
VSIFClose( fp );
/* -------------------------------------------------------------------- */
/* Search the database for the corresponding datum code. */
/* -------------------------------------------------------------------- */
sprintf( szSearchKey, "%d", nDatumCode );
nEllipsoid = atoi(CSVGetField( pszFilename,
"DATUM_CODE", szSearchKey, CC_Integer,
"ELLIPSOID_CODE" ) );
if( pnEllipsoid != NULL )
*pnEllipsoid = (short) nEllipsoid;
if( nEllipsoid < 1 )
{
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Get the name, if requested. */
/* -------------------------------------------------------------------- */
if( ppszName != NULL )
*ppszName =
CPLStrdup(CSVGetField( pszFilename,
"DATUM_CODE", szSearchKey, CC_Integer,
"DATUM_NAME" ));
return( TRUE );
}
/************************************************************************/
/* GTIFGetUOMLengthInfo() */
/* */
/* Note: This function should eventually also know how to */
/* lookup length aliases in the UOM_LE_ALIAS table. */
/************************************************************************/
int GTIFGetUOMLengthInfo( int nUOMLengthCode,
char **ppszUOMName,
double * pdfInMeters )
{
char **papszUnitsRecord;
char szSearchKey[24];
int iNameField;
const char *pszFilename;
/* -------------------------------------------------------------------- */
/* We short cut meter to save work and avoid failure for missing */
/* in the most common cases. */
/* -------------------------------------------------------------------- */
if( nUOMLengthCode == 9001 )
{
if( ppszUOMName != NULL )
*ppszUOMName = CPLStrdup( "metre" );
if( pdfInMeters != NULL )
*pdfInMeters = 1.0;
return TRUE;
}
if( nUOMLengthCode == 9002 )
{
if( ppszUOMName != NULL )
*ppszUOMName = CPLStrdup( "foot" );
if( pdfInMeters != NULL )
*pdfInMeters = 0.3048;
return TRUE;
}
if( nUOMLengthCode == 9003 )
{
if( ppszUOMName != NULL )
*ppszUOMName = CPLStrdup( "US survey foot" );
if( pdfInMeters != NULL )
*pdfInMeters = 12.0 / 39.37;
return TRUE;
}
/* -------------------------------------------------------------------- */
/* Search the units database for this unit. If we don't find */
/* it return failure. */
/* -------------------------------------------------------------------- */
pszFilename = CSVFilename( "unit_of_measure.csv" );
sprintf( szSearchKey, "%d", nUOMLengthCode );
papszUnitsRecord =
CSVScanFileByName( pszFilename,
"UOM_CODE", szSearchKey, CC_Integer );
if( papszUnitsRecord == NULL )
return FALSE;
/* -------------------------------------------------------------------- */
/* Get the name, if requested. */
/* -------------------------------------------------------------------- */
if( ppszUOMName != NULL )
{
iNameField = CSVGetFileFieldId( pszFilename,
"UNIT_OF_MEAS_NAME" );
*ppszUOMName = CPLStrdup( CSLGetField(papszUnitsRecord, iNameField) );
}
/* -------------------------------------------------------------------- */
/* Get the A and B factor fields, and create the multiplicative */
/* factor. */
/* -------------------------------------------------------------------- */
if( pdfInMeters != NULL )
{
int iBFactorField, iCFactorField;
iBFactorField = CSVGetFileFieldId( pszFilename, "FACTOR_B" );
iCFactorField = CSVGetFileFieldId( pszFilename, "FACTOR_C" );
if( GTIFAtof(CSLGetField(papszUnitsRecord, iCFactorField)) > 0.0 )
*pdfInMeters = GTIFAtof(CSLGetField(papszUnitsRecord, iBFactorField))
/ GTIFAtof(CSLGetField(papszUnitsRecord, iCFactorField));
else
*pdfInMeters = 0.0;
}
return( TRUE );
}
/************************************************************************/
/* GTIFGetUOMAngleInfo() */
/************************************************************************/
int GTIFGetUOMAngleInfo( int nUOMAngleCode,
char **ppszUOMName,
double * pdfInDegrees )
{
const char *pszUOMName = NULL;
double dfInDegrees = 1.0;
const char *pszFilename;
char szSearchKey[24];
switch( nUOMAngleCode )
{
case 9101:
pszUOMName = "radian";
dfInDegrees = 180.0 / PI;
break;
case 9102:
case 9107:
case 9108:
case 9110:
case 9122:
pszUOMName = "degree";
dfInDegrees = 1.0;
break;
case 9103:
pszUOMName = "arc-minute";
dfInDegrees = 1 / 60.0;
break;
case 9104:
pszUOMName = "arc-second";
dfInDegrees = 1 / 3600.0;
break;
case 9105:
pszUOMName = "grad";
dfInDegrees = 180.0 / 200.0;
break;
case 9106:
pszUOMName = "gon";
dfInDegrees = 180.0 / 200.0;
break;
case 9109:
pszUOMName = "microradian";
dfInDegrees = 180.0 / (PI * 1000000.0);
break;
default:
break;
}
if (pszUOMName)
{
if( ppszUOMName != NULL )
{
if( pszUOMName != NULL )
*ppszUOMName = CPLStrdup( pszUOMName );
else
*ppszUOMName = NULL;
}
if( pdfInDegrees != NULL )
*pdfInDegrees = dfInDegrees;
return TRUE;
}
pszFilename = CSVFilename( "unit_of_measure.csv" );
sprintf( szSearchKey, "%d", nUOMAngleCode );
pszUOMName = CSVGetField( pszFilename,
"UOM_CODE", szSearchKey, CC_Integer,
"UNIT_OF_MEAS_NAME" );
/* -------------------------------------------------------------------- */
/* If the file is found, read from there. Note that FactorC is */
/* an empty field for any of the DMS style formats, and in this */
/* case we really want to return the default InDegrees value */
/* (1.0) from above. */
/* -------------------------------------------------------------------- */
if( pszUOMName != NULL )
{
double dfFactorB, dfFactorC, dfInRadians;
dfFactorB =
GTIFAtof(CSVGetField( pszFilename,
"UOM_CODE", szSearchKey, CC_Integer,
"FACTOR_B" ));
dfFactorC =
GTIFAtof(CSVGetField( pszFilename,
"UOM_CODE", szSearchKey, CC_Integer,
"FACTOR_C" ));
if( dfFactorC != 0.0 )
{
dfInRadians = (dfFactorB / dfFactorC);
dfInDegrees = dfInRadians * 180.0 / PI;
}
}
else
{
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Return to caller. */
/* -------------------------------------------------------------------- */
if( ppszUOMName != NULL )
{
if( pszUOMName != NULL )
*ppszUOMName = CPLStrdup( pszUOMName );
else
*ppszUOMName = NULL;
}
if( pdfInDegrees != NULL )
*pdfInDegrees = dfInDegrees;
return( TRUE );
}
/************************************************************************/
/* EPSGProjMethodToCTProjMethod() */
/* */
/* Convert between the EPSG enumeration for projection methods, */
/* and the GeoTIFF CT codes. */
/************************************************************************/
static int EPSGProjMethodToCTProjMethod( int nEPSG )
{
/* see trf_method.csv for list of EPSG codes */
switch( nEPSG )
{
case 9801:
return( CT_LambertConfConic_1SP );
case 9802:
return( CT_LambertConfConic_2SP );
case 9803:
return( CT_LambertConfConic_2SP ); /* Belgian variant not supported */
case 9804:
return( CT_Mercator ); /* 1SP and 2SP not differentiated */
case 9805:
return( CT_Mercator ); /* 1SP and 2SP not differentiated */
/* Mercator 1SP (Spherical) For EPSG:3785 */
case 9841:
return( CT_Mercator ); /* 1SP and 2SP not differentiated */
/* Google Mercator For EPSG:3857 */
case 1024:
return( CT_Mercator ); /* 1SP and 2SP not differentiated */