-
Notifications
You must be signed in to change notification settings - Fork 58
/
geotiffinfo_lcog_matlab2020a.m
2830 lines (2390 loc) · 94.3 KB
/
geotiffinfo_lcog_matlab2020a.m
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
function info = geotiffinfo_lcog_matlab2020a(fileOrURL)
%GEOTIFFINFO Information about GeoTIFF file
%
% INFO = GEOTIFFINFO(FILENAME) returns a structure whose fields contain
% image properties and cartographic information about a GeoTIFF file.
%
% FILENAME is a string scalar or character vector that specifies the name
% of the GeoTIFF file. Include the folder name in FILENAME or place the
% file in the current folder or in a folder on the MATLAB path. If the
% named file includes the extension '.TIF' or '.TIFF' (either upper or
% lower case), you can omit the extension from FILENAME.
%
% If the named file contains multiple GeoTIFF images, INFO is a structure
% array with one element for each image in the file. For example,
% INFO(3) would contain information about the third image in the file. If
% multiple images exist in the file, it is assumed that each image has
% the same cartographic information and the same image width and height.
%
% INFO = GEOTIFFINFO(URL) reads the GeoTIFF image from a URL. The URL
% must include the protocol type (e.g., "http://").
%
% The INFO structure contains these fields:
%
% Filename Character vector containing the name of the file or URL.
%
% FileModDate Character vector containing the modification date of the
% file.
%
% FileSize Integer indicating the size of the file in bytes.
%
% Format Character vector containing the file format, which
% should always be 'tiff'.
%
% FormatVersion Character vector or number specifying the file format
% version.
%
% Height Integer indicating the height of the image in pixels.
%
% Width Integer indicating the width of the image in pixels.
%
% BitDepth Integer indicating the number of bits per pixel.
%
% ColorType Character vector indicating the type of image:
% 'truecolor' for a truecolor (RGB) image,
% 'grayscale' for a grayscale image,
% 'indexed' for an indexed image.
%
% ModelType Character vector indicating the type of coordinate
% system used to georeference the image:
% 'ModelTypeProjected', 'ModelTypeGeographic',
% 'ModelTypeGeocentric' or ''.
%
% PCS Character vector indicating the projected coordinate
% system.
%
% Projection Character vector describing the EPSG identifier for the
% underlying projection method.
%
% MapSys Character vector indicating the map system, if
% applicable:
% 'STATE_PLANE_27', 'STATE_PLANE_83',
% 'UTM_NORTH', 'UTM_SOUTH', or ''.
%
% Zone Double indicating the UTM or State Plane Zone number,
% empty ([]) if not applicable or unknown.
%
% CTProjection Character vector containing the GeoTIFF identifier for
% the underlying projection method.
%
% ProjParm N-by-1 double containing projection parameter values.
% The identify of each element is specified by the
% corresponding element of ProjParmId. Lengths are in
% meters, angles in decimal degrees.
%
% ProjParmId N-by-1 cell array listing the projection parameter
% identifier for each corresponding numerical element
% ProjParm. The possible values are:
% 'ProjNatOriginLatGeoKey'
% 'ProjNatOriginLongGeoKey'
% 'ProjFalseEastingGeoKey'
% 'ProjFalseNorthingGeoKey'
% 'ProjFalseOriginLatGeoKey'
% 'ProjFalseOriginLongGeoKey'
% 'ProjCenterLatGeoKey'
% 'ProjCenterLongGeoKey'
% 'ProjAzimuthAngleGeoKey'
% 'ProjRectifiedGridAngleGeoKey'
% 'ProjScaleAtNatOriginGeoKey'
% 'ProjStdParallel1GeoKey'
% 'ProjStdParallel2GeoKey'
%
% GCS Character vector indicating the geographic coordinate
% system.
%
% Datum Character vector indicating the projection datum type,
% such as
% 'North American Datum 1927' or
% 'North American Datum 1983'.
%
% Ellipsoid Character vector indicating the ellipsoid name as
% defined by the ellipsoid.csv EPSG file.
%
% SemiMajor Double indicating the length of the semimajor axis
% of the ellipsoid, in meters.
%
% SemiMinor Double indicating the length of the semiminor axis
% of the ellipsoid, in meters.
%
% PM Character vector indicating the prime meridian location,
% for example, 'Greenwich' or 'Paris'.
%
% PmLongToGreenwich Double indicating the decimal degrees of longitude
% between this prime meridian and Greenwich. Prime
% meridians to the west of Greenwich are negative.
%
% UOMLength Character vector indicating the units of length used in
% the projected coordinate system.
%
% UOMLengthInMeters Double defining the UOMLength unit in meters.
%
% UOMAngle Character vector indicating the angular units used for
% geographic coordinates.
%
% UOMAngleInDegrees Double defining the UOMAngle unit in degrees.
%
% TiePoints Structure containing the image tiepoints.
% The structure contains these fields:
%
% ImagePoints Structure containing row and column
% coordinates of each image tiepoint.
% The structure contains these fields:
% Row Double array of size 1-by-N.
% Col Double array of size 1-by-N.
%
% WorldPoints Structure containing the x and y
% world coordinates of the tiepoints.
% The structure contains these fields:
% X Double array of size 1-by-N.
% Y Double array of size 1-by-N.
%
% PixelScale 3-by-1 array that specifies the X,Y,Z pixel scale
% values.
%
% SpatialRef Map raster reference object if ModelType is 'ModelTypeProjected',
% or a geographic raster reference object if ModelType is
% 'ModelTypeGeographic'. If ModelType is empty, a warning
% is issued and SpatialRef is a map raster reference object.
% SpatialRef is empty if ModelType is 'ModelTypeGeocentric',
% or if the spatial referencing is ambiguously defined by
% the GeoTIFF file, or if ModelType is 'ModelTypeGeographic'
% and the geometric transformation type is 'affine'.
%
% RefMatrix 3-by-2 double referencing matrix that must be
% unambiguously defined by the GeoTIFF file. Otherwise it
% is empty.
%
% BoundingBox 2-by-2 double array that specifies the minimum (row 1)
% and maximum (row 2) values for each dimension of the
% image data in the GeoTIFF file.
%
% CornerCoords Structure with six fields that contains coordinates of
% the outer corners of the GeoTIFF image. Each field is a
% 1-by-4 double array, or empty ([]) if unknown. The
% arrays contain the coordinates of the outer corners of
% the corner pixels, starting from the (1,1) corner and
% proceeding clockwise:
%
% X Easting coordinates in the projected coordinate
% system. X equals Lon (below) if the model type is
% 'ModelTypeGeographic'.
%
% Y Northing coordinates in the projected coordinate
% system. Y equals Lat if the model type is
% 'ModelTypeGeographic'.
%
% Row Row coordinates of the corner.
%
% Col Column coordinates of the corner.
%
% Lat Latitudes of the corner.
%
% Lon Longitudes of the corner.
%
% GeoTIFFCodes Structure containing raw numeric values for those
% GeoTIFF fields that are encoded numerically in the file.
% These raw values, converted to a character vector
% elsewhere in the INFO structure, are provided here for
% reference.
%
% The GeoTIFFCodes are:
% Model
% PCS
% GCS
% UOMLength
% UOMAngle
% Datum
% PM
% Ellipsoid
% ProjCode
% Projection
% CTProjection
% ProjParmId
% MapSys
%
% Each is scalar except for ProjParmId which is a column
% vector.
%
% GeoTIFFTags Structure containing field names that match the GeoTIFF
% tags found in the file. At least one GeoTIFF tag must be
% present in the file or an error is issued.
% The following fields may be included:
%
% ModelPixelScaleTag 1-by-3 double
% ModelTiepointTag 1-by-6 double
% ModelTransformationTag 1-by-16 double
% GeoKeyDirectoryTag scalar structure
% GeoAsciiParamsTag character vector
% GeoDoubleParamsTag 1-by-N double
% RPCCoefficientTag scalar map.geotiff.RPCCoefficientTag
%
% The GeoKeyDirectoryTag contains field names that match
% the names of the "GeoKeys". For more information about
% the "GeoKeys" refer to the GeoTIFF specification at
% http://geotiff.maptools.org/spec/geotiff6.html#6.2
%
% The RPCCoefficientTag contains properties with names
% corresponding to the tag elements listed in the
% "RPCs in GeoTIFF" technical note at:
% http://geotiff.maptools.org/rpc_prop.html
%
% ImageDescription Character vector describing the image. If no
% description is included in the file, the field is
% omitted.
%
% Example
% -------
% info = geotiffinfo('boston.tif')
%
% See also GEOTIFFREAD, GEOTIFFWRITE, map.geotiff.RPCCoefficientTag, PROJFWD, PROJINV
% Copyright 1996-2018 The MathWorks, Inc.
%
% Modified for reading Landsat COG data, which may have multiple tiff_infos(see L.279&280 in this code)
% This would require the function constructGeoKeyDirectoryMap at the end.
% by Shi May 8, 2020
% Validate the input. If it's a filename, return the full pathname. If it's
% a URL, download the URL to a temporary file and set isURL to true.
if nargin > 0
fileOrURL = convertStringsToChars(fileOrURL);
end
[filename, isURL] = internal.map.checkfilename( ...
fileOrURL, {'tif', 'tiff'}, mfilename, 1, true);
if (isURL)
% Delete temporary file from Internet download regardless if any
% errors are issued.
clean = onCleanup(@() deleteDownload(filename));
end
% Create the information structure from contents in the file.
info = readinfo(filename);
if (isURL)
% Replace the temporary file name with the URL.
info.Filename = fileOrURL;
end
%------------------------- info functions ---------------------------------
function info = readinfo(filename)
% Read the GeoTIFF info from the file and return the information structure.
% Obtain the TIFF information structure from the file.
tiff_info = tiffinfo(filename);
% modified for reading Landsat COG data, which may have multiple tiff_infos
tiff_info = tiff_info(1); % force to use the first layer
tiff_info(1).GeoTIFFTags = tiff_info(1).GeoTIFFTags(1); % force to use the first GeoTag
% Validate that at least one GeoTIFF tag exists in the file.
map.internal.assert(~isempty(fieldnames(tiff_info(1).GeoTIFFTags)), ...
'map:geotiff:expectedGeoTIFFTags', filename, 'IMFINFO')
% Ensure that all images have the same Height and Width values.
map.internal.assert(isconsistent(tiff_info.Height) && isconsistent(tiff_info.Width), ...
'map:geotiff:inconsistentImageSizes');
% Obtain the GeoTIFF tags from the TIFF info structure.
tags = tiff_info(1).GeoTIFFTags;
% Obtain height and width variables from the first element.
height = tiff_info(1).Height;
width = tiff_info(1).Width;
% Obtain the GeoTIFF information structure from the TIFF tags.
gtiff_info = gtiffinfo(tags, height, width);
% Construct a new information structure and copy the input data to it.
info = copyinfo(tiff_info, gtiff_info);
%--------------------------------------------------------------------------
function tiff_info = tiffinfo(filename)
% Obtain subset of TIFF information from filename.
% Turn off TIFF library warnings for this function.
w = warning('off', 'imageio:tiffmexutils:libtiffWarning');
wobj = onCleanup(@()warning(w));
% Open and validate the file.
% Close the TIFF file when the function terminates.
t = Tiff(filename);
tobj = onCleanup(@()close(t));
% Obtain the tag names to retrieve from the file.
[tiffTags, optionalTags, geoTiffTags] = getTagNames();
% Obtain the TIFF tag values from the Tiff object and return a structure
% with field names that match the names of the requested tags.
tags = getTiffTags(t, tiffTags);
% Create the TIFF information structure.
tiff_info = tiffstruct(filename, tags);
% Add the TIFF tags to the structure.
tiff_info = addTiffTags(tiff_info, tags, optionalTags);
% Add the GeoTIFF tags to the structure.
tiff_info = addGeoTiffTags(tiff_info, tags, geoTiffTags);
%--------------------------------------------------------------------------
function gtiff_info = gtiffinfo(tags, height, width)
% Obtain GeoTIFF information from TIFF GeoTIFF tags.
% Create the GeoTIFF information structure.
gtiff_info = gtiffstruct();
% Obtain the model type from the GeoKeyDirectoryTag, if defined.
gtiff_info = modelinfo(gtiff_info, tags);
% Set UserDefined value.
gtiff_info.UserDefined = 32767;
switch gtiff_info.ModelType
case 'ModelTypeProjected'
% Obtain the PCS information from the tags.
gtiff_info = pcsinfo(gtiff_info, tags);
case 'ModelTypeGeographic'
% Obtain the GCS information from the tags.
gtiff_info = gcsinfo(gtiff_info, tags);
case 'ModelTypeGeocentric'
% Obtain any GCS and PCS information from the tags.
gtiff_info = gcsinfo(gtiff_info, tags);
gtiff_info = pcsinfo(gtiff_info, tags);
otherwise
end
% Obtain the spatial referencing information.
gtiff_info = spatialinfo(gtiff_info, tags, height, width);
%--------------------------------------------------------------------------
function gtiff_info = modelinfo(gtiff_info, tags)
% Construct the ModelType string and Model code values from the values in
% the GeoKeyDirectoryTag. If the GTModelTypeTags is contained in the
% GeoTIFFTags structure use that value for the Model code; otherwise,
% set it to []. Use the Model code value to determine the ModelType string.
tagname = 'GeoKeyDirectoryTag';
keyname = 'GTModelTypeGeoKey';
if isfield(tags, tagname) && isfield(tags.(tagname), keyname)
modelTypeValue = tags.(tagname).(keyname);
gtiff_info.GeoTIFFCodes.Model = modelTypeValue;
else
modelTypeValue = '';
gtiff_info.GeoTIFFCodes.Model = [];
end
switch modelTypeValue
case 1
gtiff_info.ModelType = 'ModelTypeProjected';
case 2
gtiff_info.ModelType = 'ModelTypeGeographic';
case 3
gtiff_info.ModelType = 'ModelTypeGeocentric';
otherwise
gtiff_info.ModelType = '';
end
%--------------------------------------------------------------------------
function gtiff_info = pcsinfo(gtiff_info, tags)
% Get the PCS information from the values in the GeoKeyDirectoryTag,
% if it is contained in the TAGS structure. Otherwise, do not modify the
% GTIFF_INFO structure.
tagname = 'GeoKeyDirectoryTag';
keyname = 'ProjectedCSTypeGeoKey';
if isfield(tags, tagname) && isfield(tags.(tagname), keyname)
% Obtain the geokeys from the GeoKeyDirectoryTag.
geokeys = tags.(tagname);
% Obtain the code number.
pcscode = geokeys.(keyname);
if pcscode > 0
% Search the pcs.override and pcs tables for this code.
result = pcsread(pcscode, gtiff_info.UserDefined);
% Assign the PCS code.
gtiff_info.GeoTIFFCodes.PCS = pcscode;
% Assign the PCS string.
gtiff_info.PCS = getStringValue(result, 'COORD_REF_SYS_NAME');
% Assign the projcode.
epsgname = 'COORD_OP_CODE';
keyname = 'ProjectionGeoKey';
if isfield(geokeys, keyname) && ~isempty(geokeys.(keyname))
default = geokeys.(keyname);
else
default = gtiff_info.UserDefined;
end
projcode = getDoubleValue(result, epsgname, default);
gtiff_info.GeoTIFFCodes.ProjCode = projcode;
% Obtain GCS code.
header_name = 'SOURCE_GEOGCRS_CODE';
code = getDoubleValue(result, header_name, gtiff_info.UserDefined);
% Search the gcs.override and gcs tables for this code.
% If the code is found in the geokeys, then override the projection
% GCS code.
gtiff_info = gcsinfo(gtiff_info, tags, code);
% Assign the projection values.
gtiff_info = projinfo(gtiff_info, projcode, geokeys);
% Assign the length unit values.
keyname = 'ProjLinearUnitsGeoKey';
code = getTagDoubleValue(geokeys, keyname, result, 'UOM_CODE');
gtiff_info = uomlengthinfo(gtiff_info, code);
end
end
%--------------------------------------------------------------------------
function gtiff_info = projinfo(gtiff_info, code, geokeys)
% Convert the code to projection values.
% Assign Projection values.
epsgname = 'COORD_OP_METHOD_CODE';
result = tableread('projop_wparm', code, epsgname, gtiff_info.UserDefined);
projmethod = getDoubleValue(result, epsgname, gtiff_info.UserDefined);
projname = getStringValue(result, 'COORD_OP_NAME');
gtiff_info.Projection = projname;
gtiff_info.GeoTIFFCodes.Projection = projmethod;
% Assign CTProjection values.
keyname = 'ProjCoordTransGeoKey';
if isfield(geokeys, keyname)
% Code is found in the geokey. Convert the standard CT code number to a
% CTProjection name.
projcode = geokeys.(keyname);
gtiff_info.CTProjection = codeToCTProjection(projcode);
else
% Code is found in EPSG table. Convert the code value to a CTProjection
% name. Using the inverse map, lookup the CTProjection code from the
% CTProjection name.
if code ~= gtiff_info.UserDefined
projcode = projmethod;
[projname, inverseMap] = codeToCTProjection(projcode);
projcode = inverseMap(projname);
gtiff_info.CTProjection = projname;
else
% code is user-defined.
projcode = code;
gtiff_info.CTProjection = '';
end
end
gtiff_info.GeoTIFFCodes.CTProjection = projcode;
% Assign UTM, State Plane, and Zone EPSG values.
UTM_North = -9001;
UTM_South = -9002;
State_Plane_27 = -9003;
State_Plane_83 = -9004;
UTM_zone_1N = 16001;
UTM_zone_60N = 16060;
UTM_zone_1S = 16101;
UTM_zone_60S = 16160;
if code >= UTM_zone_1N && code <= UTM_zone_60N
mapSysCode = UTM_North;
mapSys = 'UTM_NORTH';
zone = code - UTM_zone_1N + 1;
elseif code >= UTM_zone_1S && code <= UTM_zone_60S
mapSysCode = UTM_South;
mapSys = 'UTM_SOUTH';
zone = code - UTM_zone_1S + 1;
elseif code >= 10101 && code <= 15299
if rem(code, 100) >= 30
mapSysCode = State_Plane_83;
mapSys = 'STATE_PLANE_83';
zone = code - 10000 - 30;
else
mapSysCode = State_Plane_27;
mapSys = 'STATE_PLANE_27';
zone = code - 10000;
end
else
mapSysCode = gtiff_info.UserDefined;
zone = gtiff_info.UserDefined;
mapSys = '';
end
% Assign the MapSys and Zone values.
gtiff_info.GeoTIFFCodes.MapSys = mapSysCode;
gtiff_info.MapSys = mapSys;
gtiff_info.Zone = zone;
% Assign ProjParm info values.
gtiff_info = projparamsinfo(gtiff_info, result, geokeys);
%--------------------------------------------------------------------------
function gtiff_info = projparamsinfo(gtiff_info, result, geokeys)
% Assign the ProjParm info parameters.
[idToNameMap, nameToIdMap] = constructGeoKeyDirectoryMap;
projcode = gtiff_info.GeoTIFFCodes.CTProjection;
[name, inverseMap] = codeToCTProjection(projcode);
id = @(x)(nameToIdMap(lower(x)));
% Construct projParmCodes and EPSG codes values.
[projParmCodes, epsgCodes, EPSG] = constructProjParmCodes(name, id);
% Construct ProjParm values.
projParm = constructProjParm(result, EPSG, epsgCodes);
% Update proj params if the ProjCoordTansGeoKey has been set.
if isfield(geokeys, 'ProjCoordTransGeoKey')
[projParm, projParmCodes] = updateProjParm( ...
projParm, projParmCodes, geokeys, name, id);
end
% Translate codes to names.
projParmId = cell(size(projParmCodes));
idToNameMap(0) = 'Unknown-0';
for k=1:numel(projParmCodes)
projParmId{k} = idToNameMap(projParmCodes(k));
end
% Assign values to gtiff_info.
gtiff_info.GeoTIFFCodes.ProjParmId = projParmCodes;
gtiff_info.ProjParmId = projParmId;
gtiff_info.ProjParm = projParm;
% Adjust projection parameters if using CT_PolarStereographic_B.
if strcmp(name, 'CT_PolarStereographic_B')
gtiff_info = adjustPolarStereographicBParams(gtiff_info, inverseMap);
end
%--------------------------------------------------------------------------
function gtiff_info = gcsinfo(gtiff_info, tags, code)
% Get the GCS string from the value in the GeoKeyDirectoryTag,
% if it is contained in the GeoTIFFTags structure, otherwise, return empty.
if ~exist('code', 'var')
code = 0;
end
if isfield(tags,'GeoKeyDirectoryTag')
% Obtain the geokeys from the GeoKeyDirectoryTag.
geokeys = tags.GeoKeyDirectoryTag;
if isfield(geokeys, 'GeographicTypeGeoKey')
% Obtain the code number for the key.
code = geokeys.GeographicTypeGeoKey;
end
else
geokeys =struct();
end
if code > 0
% Search the gcs.override and gcs tables for this code.
result = gcsread(code, gtiff_info.UserDefined);
% Assign the GCS code.
gtiff_info.GeoTIFFCodes.GCS = code;
% Assign the GCS string.
gtiff_info.GCS = getStringValue(result, 'COORD_REF_SYS_NAME');
% Assign the angle unit values.
% Note: The GeoTIFF library uses the angle code from the datum if
% defined rather than the code from the key, if given. For
% compatibility, use the code from the datum information if provided.
% In the future, this behavior should change. Remove the four lines
% below and replace them with this one.
% code = getTagDoubleValue(geokeys, keyname, result, 'UOM_CODE');
keyname = 'GeogAngularUnitsGeoKey';
code = getDoubleValue(result, 'UOM_CODE', gtiff_info.UserDefined);
if code == gtiff_info.UserDefined
code = getTagDoubleValue(geokeys, keyname, result, 'UOM_CODE');
end
gtiff_info = uomangleinfo(gtiff_info, code);
% Assign the Datum code.
keyname = 'GeogGeodeticDatumGeoKey';
code = getTagDoubleValue(geokeys, keyname, result, 'DATUM_CODE');
gtiff_info.GeoTIFFCodes.Datum = code;
% Search the datum.csv table for this code.
epsgname = 'datum_name';
result = tableread('datum', code, epsgname, gtiff_info.UserDefined);
% Assign the Datum string.
gtiff_info.Datum = getStringValue(result, epsgname);
% Assign the PM code.
keyname = 'GeogPrimeMeridianGeoKey';
code = getTagDoubleValue(geokeys, keyname, result, 'prime_meridian_code');
gtiff_info.GeoTIFFCodes.PM = code;
% Assign the PM values.
gtiff_info = pminfo(gtiff_info, code);
% Assign the Ellipsoid code.
keyname = 'GeogEllipsoidGeoKey';
code = getTagDoubleValue(geokeys, keyname, result, 'ellipsoid_code');
gtiff_info.GeoTIFFCodes.Ellipsoid = code;
% Assign the ellipsoid values.
gtiff_info = ellipsoidinfo(gtiff_info, geokeys, code);
end
%--------------------------------------------------------------------------
function gtiff_info = pminfo(gtiff_info, code)
% Search the prime_meridian table for the code.
% Check to see if the prime meridian code is for Greenwich since it is the
% most common code used.
PM_Greenwich = 8901;
if code == PM_Greenwich
gtiff_info.PM = 'Greenwich';
gtiff_info.PMLongToGreenwich = 0;
else
epsgname = 'PRIME_MERIDIAN_NAME';
result = tableread('prime_meridian', code, epsgname, gtiff_info.UserDefined);
gtiff_info.PM = getStringValue(result, epsgname);
epsgname = 'GREENWICH_LONGITUDE';
gtiff_info.PMLongToGreenwich = getDoubleValue(result, epsgname, 0);
end
gtiff_info.GeoTIFFCodes.PM = code;
%--------------------------------------------------------------------------
function gtiff_info = uomlengthinfo(gtiff_info, code)
% Search the unit_of_measure table for the code.
% Save time with common values.
switch code
case 9001
gtiff_info.UOMLength = 'metre';
gtiff_info.UOMLengthInMeters = 1.0;
case 9002
gtiff_info.UOMLength = 'foot';
gtiff_info.UOMLengthInMeters = .3048;
case 9003
gtiff_info.UOMLength = 'US survey foot';
gtiff_info.UOMLengthInMeters = 12.0/39.37;
otherwise
[name, factor_b, factor_c] = uomread(code, gtiff_info.UserDefined);
gtiff_info.UOMLength = name;
if isempty(factor_b) || isempty(factor_c)
dfInMeters = 0.0;
else
dfInMeters = factor_b / factor_c;
end
gtiff_info.UOMLengthInMeters = dfInMeters;
end
% Assign the UOMLength code.
gtiff_info.GeoTIFFCodes.UOMLength = code;
%--------------------------------------------------------------------------
function gtiff_info = uomangleinfo(gtiff_info, code)
% Search the unit_of_measure table for the code.
% Save time with common values.
switch code
case 9101
gtiff_info.UOMAngle = 'radian';
gtiff_info.UOMAngleInDegrees = 180.0 / pi;
case {9102 9107 9108 9110 9122}
gtiff_info.UOMAngle = 'degree';
gtiff_info.UOMAngleInDegrees = 1.0;
case 9103
gtiff_info.UOMAngle = 'arc-minute';
gtiff_info.UOMAngleInDegrees = 1.0 / 60.0;
case 9104
gtiff_info.UOMAngle = 'arc-second';
gtiff_info.UOMAngleInDegrees = 1.0 / 3600.0;
case 9105
gtiff_info.UOMAngle = 'grad';
gtiff_info.UOMAngleInDegrees = 180.0 / 200.0;
case 9106
gtiff_info.UOMAngle = 'gon';
gtiff_info.UOMAngleInDegrees = 180.0 / 200.0;
case 9109
gtiff_info.UOMAngle = 'microradian';
gtiff_info.UOMAngleInDegrees = 180.0 / (pi * 1000000.0);
otherwise
[name, factor_b, factor_c] = uomread(code, gtiff_info.UserDefined);
if factor_c ~= 0.0
angleInRadians = factor_b / factor_c;
angleInDegrees = angleInRadians * 180.0 / pi;
else
angleInDegrees = [];
end
gtiff_info.UOMAngle = name;
gtiff_info.UOMAngleInDegrees = angleInDegrees;
end
% Assign the UOMAngle code.
gtiff_info.GeoTIFFCodes.UOMAngle = code;
%--------------------------------------------------------------------------
function gtiff_info = ellipsoidinfo(gtiff_info, geokeys, code)
% Get the ellipsoid information using the code value. If the code equals
% UserDefined, then set the values to empty. Override SemiMajor and
% SemiMinor values if found in the geokeys.
if code == gtiff_info.UserDefined || isempty(code) || code <= 0
gtiff_info.Ellipsoid = ''; % 'UserDefined'
gtiff_info.SemiMajor = [];
gtiff_info.SemiMinor = [];
elseif code > 0
% Obtain the ellipsoid values from the table.
e = epsgread('ellipsoid', code);
% Assign the Ellipsoid name, SemiMajor, and SemiMinor values.
gtiff_info.Ellipsoid = e.Ellipsoid;
gtiff_info.SemiMajor = e.SemiMajor;
gtiff_info.SemiMinor = e.SemiMinor;
end
% Check for overridden SemiMajor and SemiMinor parameters.
keyname = 'GeogSemiMajorAxisGeoKey';
if isfield(geokeys, keyname)
gtiff_info.SemiMajor = geokeys.(keyname);
end
keyname = 'GeogSemiMinorAxisGeoKey';
if isfield(geokeys, keyname)
gtiff_info.SemiMinor = geokeys.(keyname);
end
%--------------------------------------------------------------------------
function gtiff_info = spatialinfo(gtiff_info, tags, height, width)
% Obtain the spatial referencing information.
% Assign the PixelScale value.
if isfield(tags, 'ModelPixelScaleTag')
gtiff_info.PixelScale = tags.ModelPixelScaleTag';
else
gtiff_info.PixelScale = [];
end
% Construct the spatial referencing object.
gtiff_info.SpatialRef = constructSpatialRef( ...
tags, height, width, gtiff_info.ModelType);
% Construct the referencing matrix.
gtiff_info.RefMatrix = constructRefMatrix(gtiff_info.SpatialRef);
% Construct the TiePoints structure.
gtiff_info.TiePoints = constructTiePoints(tags);
% Construct the BoundingBox array.
gtiff_info.BoundingBox = constructBoundingBox(gtiff_info.SpatialRef);
% Construct the CornerCoords structure.
gtiff_info = constructCornerCoords(gtiff_info, gtiff_info.SpatialRef);
%------------------------- struct functions -------------------------------
function tiff_info = tiffstruct(filename, tags)
% Create the TIFF information structure. tags is a structure array with one
% element per TIFF directory. optionalTags is a cell array of strings.
% Create the information structure.
requiredFields = { ...
'Filename', ...
'FileModDate', ...
'FileSize', ...
'Format', ...
'FormatVersion', ...
'Height', ...
'Width', ...
'BitDepth', ...
'ColorType'};
tiff_info = cell2struct(cell(size(requiredFields)), requiredFields, 2);
numdirs = numel(tags);
tiff_info(numdirs) = tiff_info;
% Obtain information about the file.
d = dir(filename);
fileModDate = d.date;
fileSize = d.bytes;
fileFormat = 'tif';
% Copy data to the tiff_info structure.
[tiff_info.Filename] = deal(filename);
[tiff_info.FileModDate] = deal(fileModDate);
[tiff_info.FileSize] = deal(fileSize);
[tiff_info.Format] = deal(fileFormat);
[tiff_info.FormatVersion] = deal([]);
[tiff_info.Height] = deal(tags.ImageLength);
[tiff_info.Width] = deal(tags.ImageWidth);
%--------------------------------------------------------------------------
function gtiff_info = gtiffstruct()
% Create the GeoTIFF information structure.
codes = struct( ...
'Model', [], ...
'PCS', [], ...
'GCS', [], ...
'UOMLength', [], ...
'UOMAngle', [], ...
'Datum', [], ...
'PM', [], ...
'Ellipsoid', [], ...
'ProjCode', [], ...
'Projection', [], ...
'CTProjection', [], ...
'MapSys', [], ...
'ProjParmId', []);
gtiff_info = struct( ...
'ModelType', '', ...
'PCS', '', ...
'GCS', '', ...
'UOMLength', '', ...
'UOMLengthInMeters', [], ...
'UOMAngle', '', ...
'UOMAngleInDegrees', [], ...
'Datum', '', ...
'PM', '', ...
'PMLongToGreenwich', [], ...
'Ellipsoid', '', ...
'SemiMajor', [], ...
'SemiMinor', [], ...
'Projection', '', ...
'CTProjection', '', ...
'ProjParm', [], ...
'ProjParmId', '', ...
'PixelScale', [], ...
'MapSys', '', ...
'Zone', [], ...
'CornerCoords', [], ...
'GeoTIFFCodes', codes);
%--------------------------------------------------------------------------
function info = infostruct(tiff_info)
% Initialize info structure and set it's size to the size of tiff_info.
info = struct( ...
'Filename', '', ...
'FileModDate', '', ...
'FileSize', [], ...
'Format', '', ...
'FormatVersion', [], ...
'Height', [], ...
'Width', [], ...
'BitDepth', [], ...
'ColorType', '', ...
'ModelType', '', ...
'PCS', '', ...
'Projection', '', ...
'MapSys', '', ...
'Zone', [], ...
'CTProjection', '', ...
'ProjParm', [], ...
'ProjParmId', [], ...
'GCS', '', ...
'Datum', '', ...
'Ellipsoid', '', ...
'SemiMajor', [], ...
'SemiMinor', [], ...
'PM', '', ...
'PMLongToGreenwich', [], ...
'UOMLength', '', ...
'UOMLengthInMeters', [], ...
'UOMAngle', '', ...
'UOMAngleInDegrees', [], ...
'TiePoints', [], ...
'PixelScale', [], ...
'SpatialRef', [], ...
'RefMatrix', [], ...
'BoundingBox', [], ...
'CornerCoords', [], ...
'GeoTIFFCodes', [], ...
'GeoTIFFTags', []);
if isfield(tiff_info,'ImageDescription')
info.ImageDescription = '';
end
info(numel(tiff_info)) = info;
%--------------------------------------------------------------------------
function EPSG = epsgstruct()
% Construct EPSG structure.
EPSG = struct( ...
'NatOriginLat' , 8801, ...
'NatOriginLong' , 8802, ...
'NatOriginScaleFactor' , 8805, ...
'FalseEasting' , 8806, ...
'FalseNorthing' , 8807, ...
'ProjCenterLat' , 8811, ...
'ProjCenterLong' , 8812, ...
'Azimuth' , 8813, ...
'AngleRectifiedToSkewedGrid' , 8814, ...
'InitialLineScaleFactor', 8815, ...
'ProjCenterEasting' , 8816, ...
'ProjCenterNorthing' , 8817, ...
'PseudoStdParallelScaleFactor' , 8819, ...
'FalseOriginLat' , 8821, ...
'FalseOriginLong' , 8822, ...
'StdParallel1Lat' , 8823, ...
'StdParallel2Lat' , 8824, ...
'FalseOriginEasting' , 8826, ...
'FalseOriginNorthing' , 8827, ...
'StdParallelLat' , 8832, ...
'OriginLong' , 8833);
%------------------------- read functions ---------------------------------
function result = epsgread(table, code)
% Search the EPSG table for the code value. If found, return a structure,
% otherwise return empty.
% For performance reasons, cache the found results into the persistent
% variable epsgtables. Only store the found results since the entire
% database is over 80 MB when converted to a structure.
persistent epsgtables
if isempty(epsgtables) || ~isstruct(epsgtables)
epsgtables = struct();
end
% Remove any '.' from table name.
tablename = strrep(table, '.', '_');
% Cache the override EPSG tables since so few requests will actually find
% data from these tables and they are small.
override_names = {'pcs_override', 'gcs_override'};
% Assign a logical if reading the ellipsoid table.
readingEllipsoid = strcmp('ellipsoid', tablename);