1 /**********************************************************************
2  *
3  *  geo_get.c  -- Public routines for GEOTIFF GeoKey access.
4  *
5  *    Written By: Niles D. Ritter.
6  *
7  *  copyright (c) 1995   Niles D. Ritter
8  *
9  *  Permission granted to use this software, so long as this copyright
10  *  notice accompanies any products derived therefrom.
11  *
12  *  Revision History;
13  *
14  *    20 June, 1995      Niles D. Ritter         New
15  *    3 July,  1995      Greg Martin             Fix strings and index
16  *    6 July,  1995      Niles D. Ritter         Unfix indexing.
17  *
18  **********************************************************************/
19 
20 #include "geotiff.h"   /* public interface        */
21 #include "geo_tiffp.h" /* external TIFF interface */
22 #include "geo_keyp.h"  /* private interface       */
23 #include "geokeys.h"
24 
25 /* return the Header info of this geotiff file */
26 
GTIFDirectoryInfo(GTIF * gtif,int version[3],int * keycount)27 void GTIFDirectoryInfo(GTIF *gtif, int version[3], int *keycount)
28 {
29         if (version)
30         {
31                 version[0]  = gtif->gt_version;
32                 version[1]  = gtif->gt_rev_major;
33                 version[2]  = gtif->gt_rev_minor;
34         }
35         if (keycount) *keycount = gtif->gt_num_keys;
36 }
37 
38 
GTIFKeyInfo(GTIF * gtif,geokey_t key,int * size,tagtype_t * type)39 int GTIFKeyInfo(GTIF *gtif, geokey_t key, int *size, tagtype_t* type)
40 {
41         int index = gtif->gt_keyindex[ key ];
42         GeoKey *keyptr;
43 
44         if (!index) return 0;
45 
46         keyptr = gtif->gt_keys + index;
47         if (size) *size = (int) keyptr->gk_size;
48         if (type) *type = keyptr->gk_type;
49 
50         return keyptr->gk_count;
51 }
52 
53 /**
54 
55 This function reads the value of a single GeoKey from a GeoTIFF file.
56 
57 @param gtif The geotiff information handle from GTIFNew().
58 
59 @param thekey The geokey_t name (such as ProjectedCSTypeGeoKey).
60 This must come from the list of legal geokey_t values
61 (an enumeration) listed below.
62 
63 @param val The <b>val</b> argument is a pointer to the
64 variable into which the value should be read.  The type of the variable
65 varies depending on the geokey_t given.  While there is no ready mapping
66 of geokey_t values onto types, in general code values are of type <i>short</i>,
67 citations are strings, and everything else is of type <i>double</i>.  Note
68 that pointer's to <i>int</i> should never be passed to GTIFKeyGet() for
69 integer values as they will be shorts, and the int's may not be properly
70 initialized (and will be grossly wrong on MSB systems).
71 
72 @param index Indicates how far into the list of values
73 for this geokey to offset. Should normally be zero.
74 
75 @param count Indicates how many values
76 to read.  At this time all keys except for strings have only one value,
77 so <b>index</b> should be zero, and <b>count</b> should be one.
78 
79 @return The GTIFKeyGet() function returns the number of values read.  Normally
80 this would be one if successful or zero if the key doesn't exist for this
81 file.
82 
83 From geokeys.inc we see the following geokey_t values are possible:<p>
84 
85 <pre>
86 -- 6.2.1 GeoTIFF Configuration Keys --
87 
88 ValuePair(  GTModelTypeGeoKey,        1024) -- Section 6.3.1.1 Codes       --
89 ValuePair(  GTRasterTypeGeoKey,        1025) -- Section 6.3.1.2 Codes       --
90 ValuePair(  GTCitationGeoKey,        1026) -- documentation --
91 
92 -- 6.2.2 Geographic CS Parameter Keys --
93 
94 ValuePair(  GeographicTypeGeoKey,        2048) -- Section 6.3.2.1 Codes     --
95 ValuePair(  GeogCitationGeoKey,        2049) -- documentation             --
96 ValuePair(  GeogGeodeticDatumGeoKey,        2050) -- Section 6.3.2.2 Codes     --
97 ValuePair(  GeogPrimeMeridianGeoKey,        2051) -- Section 6.3.2.4 codes     --
98 ValuePair(  GeogLinearUnitsGeoKey,        2052) -- Section 6.3.1.3 Codes     --
99 ValuePair(  GeogLinearUnitSizeGeoKey,        2053) -- meters                    --
100 ValuePair(  GeogAngularUnitsGeoKey,        2054) -- Section 6.3.1.4 Codes     --
101 ValuePair(  GeogAngularUnitSizeGeoKey,        2055) -- radians                   --
102 ValuePair(  GeogEllipsoidGeoKey,        2056) -- Section 6.3.2.3 Codes     --
103 ValuePair(  GeogSemiMajorAxisGeoKey,        2057) -- GeogLinearUnits           --
104 ValuePair(  GeogSemiMinorAxisGeoKey,        2058) -- GeogLinearUnits           --
105 ValuePair(  GeogInvFlatteningGeoKey,        2059) -- ratio                     --
106 ValuePair(  GeogAzimuthUnitsGeoKey,        2060) -- Section 6.3.1.4 Codes     --
107 ValuePair(  GeogPrimeMeridianLongGeoKey,        2061) -- GeoAngularUnit            --
108 
109 -- 6.2.3 Projected CS Parameter Keys --
110 --    Several keys have been renamed,--
111 --    and the deprecated names aliased for backward compatibility --
112 
113 ValuePair(  ProjectedCSTypeGeoKey,        3072)     -- Section 6.3.3.1 codes   --
114 ValuePair(  PCSCitationGeoKey,        3073)     -- documentation           --
115 ValuePair(  ProjectionGeoKey,        3074)     -- Section 6.3.3.2 codes   --
116 ValuePair(  ProjCoordTransGeoKey,        3075)     -- Section 6.3.3.3 codes   --
117 ValuePair(  ProjLinearUnitsGeoKey,        3076)     -- Section 6.3.1.3 codes   --
118 ValuePair(  ProjLinearUnitSizeGeoKey,        3077)     -- meters                  --
119 ValuePair(  ProjStdParallel1GeoKey,        3078)     -- GeogAngularUnit --
120 ValuePair(  ProjStdParallelGeoKey,ProjStdParallel1GeoKey) -- ** alias **   --
121 ValuePair(  ProjStdParallel2GeoKey,        3079)     -- GeogAngularUnit --
122 ValuePair(  ProjNatOriginLongGeoKey,        3080)     -- GeogAngularUnit --
123 ValuePair(  ProjOriginLongGeoKey,ProjNatOriginLongGeoKey) -- ** alias **     --
124 ValuePair(  ProjNatOriginLatGeoKey,        3081)     -- GeogAngularUnit --
125 ValuePair(  ProjOriginLatGeoKey,ProjNatOriginLatGeoKey)   -- ** alias **     --
126 ValuePair(  ProjFalseEastingGeoKey,        3082)     -- ProjLinearUnits --
127 ValuePair(  ProjFalseNorthingGeoKey,        3083)     -- ProjLinearUnits --
128 ValuePair(  ProjFalseOriginLongGeoKey,        3084)     -- GeogAngularUnit --
129 ValuePair(  ProjFalseOriginLatGeoKey,        3085)     -- GeogAngularUnit --
130 ValuePair(  ProjFalseOriginEastingGeoKey,        3086)     -- ProjLinearUnits --
131 ValuePair(  ProjFalseOriginNorthingGeoKey,        3087)     -- ProjLinearUnits --
132 ValuePair(  ProjCenterLongGeoKey,        3088)     -- GeogAngularUnit --
133 ValuePair(  ProjCenterLatGeoKey,        3089)     -- GeogAngularUnit --
134 ValuePair(  ProjCenterEastingGeoKey,        3090)     -- ProjLinearUnits --
135 ValuePair(  ProjCenterNorthingGeoKey,        3091)     -- ProjLinearUnits --
136 ValuePair(  ProjScaleAtNatOriginGeoKey,        3092)     -- ratio   --
137 ValuePair(  ProjScaleAtOriginGeoKey,ProjScaleAtNatOriginGeoKey)  -- ** alias **   --
138 ValuePair(  ProjScaleAtCenterGeoKey,        3093)     -- ratio   --
139 ValuePair(  ProjAzimuthAngleGeoKey,        3094)     -- GeogAzimuthUnit --
140 ValuePair(  ProjStraightVertPoleLongGeoKey,        3095)     -- GeogAngularUnit --
141 
142  6.2.4 Vertical CS Keys
143 
144 ValuePair(  VerticalCSTypeGeoKey,        4096)  -- Section 6.3.4.1 codes   --
145 ValuePair(  VerticalCitationGeoKey,        4097)  -- documentation --
146 ValuePair(  VerticalDatumGeoKey,        4098)  -- Section 6.3.4.2 codes   --
147 ValuePair(  VerticalUnitsGeoKey,        4099)  -- Section 6.3.1 (.x) codes   --
148 </pre>
149 */
150 
GTIFKeyGet(GTIF * gtif,geokey_t thekey,void * val,int index,int count)151 int GTIFKeyGet(GTIF *gtif, geokey_t thekey, void *val, int index, int count)
152 {
153         int kindex = gtif->gt_keyindex[ thekey ];
154         GeoKey *key;
155         gsize_t size;
156         char *data;
157         tagtype_t type;
158 
159         if (!kindex) return 0;
160 
161         key = gtif->gt_keys+kindex;
162         if (!count) count = key->gk_count - index;
163         if (count <=0) return 0;
164         if (count > key->gk_count) count = key->gk_count;
165         size = key->gk_size;
166         type = key->gk_type;
167 
168         if (count==1 && type==TYPE_SHORT) data = (char *)&key->gk_data;
169         else data = key->gk_data;
170 
171         _GTIFmemcpy( val, data + index*size, count*size );
172 
173         if (type==TYPE_ASCII)
174            ((char *)val)[count-1] = '\0'; /* replace last char with NULL */
175 
176         return count;
177 }
178