forked from JoshDullen/libgeotiff.net
/
geo_get.cs
executable file
·418 lines (371 loc) · 14.1 KB
/
geo_get.cs
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
//*********************************************************************
//
// geo_get.cs -- Public routines for GEOTIFF GeoKey access.
//
// Written By: Niles D. Ritter
// The Authors
//
// Copyright (c) 1995 Niles D. Ritter
// Copyright (c) 2008 by the Authors
//
// Permission granted to use this software, so long as this copyright
// notice accompanies any products derived therefrom.
//
// Revision History;
//
// 20 June, 1995 Niles D. Ritter New
// 03 July, 1995 Greg Martin Fix strings and index
// 06 July, 1995 Niles D. Ritter Unfix indexing.
// 30 Sept, 2008 The Authors Port to C#
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Text;
namespace Free.Ports.LibGeoTiff
{
public static partial class libgeotiff
{
// return the Header info of this geotiff file
public static void GTIFDirectoryInfo(GTIF gtif, int[] version, out int keycount)
{
if(version!=null&&version.Length>=3)
{
version[0]=gtif.gt_version;
version[1]=gtif.gt_rev_major;
version[2]=gtif.gt_rev_minor;
}
keycount=gtif.gt_keys.Count;
}
public static int GTIFKeyInfo(GTIF gtif, geokey_t key, out tagtype_t type)
{
type=tagtype_t.TYPE_UNKNOWN;
if(!gtif.gt_keys.ContainsKey(key)) return 0;
GeoKey keyptr=gtif.gt_keys[key];
type=keyptr.gk_type;
return keyptr.gk_count;
}
// This function reads the value of a single GeoKey from a GeoTIFF file.
//
// @param gtif The geotiff information handle from GTIFNew().
//
// @param thekey The geokey_t name (such as ProjectedCSTypeGeoKey).
// This must come from the list of legal geokey_t values
// (an enumeration) listed below.
//
// @param val The <b>val</b> argument is a pointer to the
// variable into which the value should be read. The type of the variable
// varies depending on the geokey_t given. While there is no ready mapping
// of geokey_t values onto types, in general code values are of type <i>short</i>,
// citations are strings, and everything else is of type <i>double</i>. Note
// that pointer's to <i>int</i> should never be passed to GTIFKeyGet() for
// integer values as they will be shorts, and the int's may not be properly
// initialized (and will be grossly wrong on MSB systems).
//
// @param index Indicates how far into the list of values
// for this geokey to offset. Should normally be zero.
//
// @param count Indicates how many values
// to read. At this time all keys except for strings have only one value,
// so <b>index</b> should be zero, and <b>count</b> should be one.
//
// @return The GTIFKeyGet() function returns the number of values read. Normally
// this would be one if successful or zero if the key doesn't exist for this
// file.
//
// From geokeys.inc we see the following geokey_t values are possible:<p>
//
// -- 6.2.1 GeoTIFF Configuration Keys --
//
// ValuePair(GTModelTypeGeoKey, 1024) -- Section 6.3.1.1 Codes
// ValuePair(GTRasterTypeGeoKey, 1025) -- Section 6.3.1.2 Codes
// ValuePair(GTCitationGeoKey, 1026) -- documentation
//
// -- 6.2.2 Geographic CS Parameter Keys --
//
// ValuePair(GeographicTypeGeoKey, 2048) -- Section 6.3.2.1 Codes
// ValuePair(GeogCitationGeoKey, 2049) -- documentation
// ValuePair(GeogGeodeticDatumGeoKey, 2050) -- Section 6.3.2.2 Codes
// ValuePair(GeogPrimeMeridianGeoKey, 2051) -- Section 6.3.2.4 codes
// ValuePair(GeogLinearUnitsGeoKey, 2052) -- Section 6.3.1.3 Codes
// ValuePair(GeogLinearUnitSizeGeoKey, 2053) -- meters
// ValuePair(GeogAngularUnitsGeoKey, 2054) -- Section 6.3.1.4 Codes
// ValuePair(GeogAngularUnitSizeGeoKey, 2055) -- radians
// ValuePair(GeogEllipsoidGeoKey, 2056) -- Section 6.3.2.3 Codes
// ValuePair(GeogSemiMajorAxisGeoKey, 2057) -- GeogLinearUnits
// ValuePair(GeogSemiMinorAxisGeoKey, 2058) -- GeogLinearUnits
// ValuePair(GeogInvFlatteningGeoKey, 2059) -- ratio
// ValuePair(GeogAzimuthUnitsGeoKey, 2060) -- Section 6.3.1.4 Codes
// ValuePair(GeogPrimeMeridianLongGeoKey, 2061) -- GeoAngularUnit
// ValuePair(GeogTOWGS84GeoKey, 2062) -- 2011 - proposed addition
//
// -- 6.2.3 Projected CS Parameter Keys --
// -- Several keys have been renamed,--
// -- and the deprecated names aliased for backward compatibility --
//
// ValuePair(ProjectedCSTypeGeoKey, 3072) -- Section 6.3.3.1 codes
// ValuePair(PCSCitationGeoKey, 3073) -- documentation
// ValuePair(ProjectionGeoKey, 3074) -- Section 6.3.3.2 codes
// ValuePair(ProjCoordTransGeoKey, 3075) -- Section 6.3.3.3 codes
// ValuePair(ProjLinearUnitsGeoKey, 3076) -- Section 6.3.1.3 codes
// ValuePair(ProjLinearUnitSizeGeoKey, 3077) -- meters
// ValuePair(ProjStdParallel1GeoKey, 3078) -- GeogAngularUnit
// ValuePair(ProjStdParallelGeoKey,ProjStdParallel1GeoKey)
// ValuePair(ProjStdParallel2GeoKey, 3079) -- GeogAngularUnit
// ValuePair(ProjNatOriginLongGeoKey, 3080) -- GeogAngularUnit
// ValuePair(ProjOriginLongGeoKey, ProjNatOriginLongGeoKey)
// ValuePair(ProjNatOriginLatGeoKey, 3081) -- GeogAngularUnit
// ValuePair(ProjOriginLatGeoKey, ProjNatOriginLatGeoKey)
// ValuePair(ProjFalseEastingGeoKey, 3082) -- ProjLinearUnits
// ValuePair(ProjFalseNorthingGeoKey, 3083) -- ProjLinearUnits
// ValuePair(ProjFalseOriginLongGeoKey, 3084) -- GeogAngularUnit
// ValuePair(ProjFalseOriginLatGeoKey, 3085) -- GeogAngularUnit
// ValuePair(ProjFalseOriginEastingGeoKey, 3086) -- ProjLinearUnits
// ValuePair(ProjFalseOriginNorthingGeoKey, 3087) -- ProjLinearUnits
// ValuePair(ProjCenterLongGeoKey, 3088) -- GeogAngularUnit
// ValuePair(ProjCenterLatGeoKey, 3089) -- GeogAngularUnit
// ValuePair(ProjCenterEastingGeoKey, 3090) -- ProjLinearUnits
// ValuePair(ProjCenterNorthingGeoKey, 3091) -- ProjLinearUnits
// ValuePair(ProjScaleAtNatOriginGeoKey, 3092) -- ratio
// ValuePair(ProjScaleAtOriginGeoKey, ProjScaleAtNatOriginGeoKey)
// ValuePair(ProjScaleAtCenterGeoKey, 3093) -- ratio
// ValuePair(ProjAzimuthAngleGeoKey, 3094) -- GeogAzimuthUnit
// ValuePair(ProjStraightVertPoleLongGeoKey, 3095) -- GeogAngularUnit
//
// 6.2.4 Vertical CS Keys
//
// ValuePair(VerticalCSTypeGeoKey, 4096) -- Section 6.3.4.1 codes
// ValuePair(VerticalCitationGeoKey, 4097) -- documentation
// ValuePair(VerticalDatumGeoKey, 4098) -- Section 6.3.4.2 codes
// ValuePair(VerticalUnitsGeoKey, 4099) -- Section 6.3.1 (.x) codes
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out ushort val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out ushort val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out ellipsoid_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(ellipsoid_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out ellipsoid_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out geodeticdatum_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(geodeticdatum_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out geodeticdatum_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out geographic_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(geographic_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out geographic_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out modeltype_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(modeltype_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out modeltype_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out geounits_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(geounits_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out geounits_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out pcstype_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(pcstype_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out pcstype_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out projection_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(projection_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out projection_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out primemeridian_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(primemeridian_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out primemeridian_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out coordtrans_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(coordtrans_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out coordtrans_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out rastertype_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(rastertype_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out rastertype_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out vertcstype_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(vertcstype_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out vertcstype_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out vdatum_t val, int index, int count)
{
// ignore count
val=0;
ushort[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=(vdatum_t)vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out vdatum_t val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out ushort[] val, int index, int count)
{
val=null;
if(!gtif.gt_keys.ContainsKey(thekey)) return 0;
GeoKey key=gtif.gt_keys[thekey];
if(key.gk_type!=tagtype_t.TYPE_SHORT) return 0;
ushort[] val_=(ushort[])key.gk_data;
if(count==0||(count+index)>val_.Length) count=val_.Length-index;
if(count<=0) return 0;
val=new ushort[count];
Array.Copy(val_, index, val, 0, count);
return count;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out double val, int index, int count)
{
// ignore count
val=0;
double[] vals;
if(GTIFKeyGet(gtif, thekey, out vals, index, 1)==0) return 0;
val=vals[0];
return 1;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out double val, int index)
{
return GTIFKeyGet(gtif, thekey, out val, index, 0);
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out double[] val, int index, int count)
{
val=null;
if(!gtif.gt_keys.ContainsKey(thekey)) return 0;
GeoKey key=gtif.gt_keys[thekey];
if(key.gk_type!=tagtype_t.TYPE_DOUBLE) return 0;
double[] val_=(double[])key.gk_data;
if(count==0||(count+index)>val_.Length) count=val_.Length-index;
if(count<=0) return 0;
val=new double[count];
Array.Copy(val_, index, val, 0, count);
return count;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out string val)
{
val=null;
if(!gtif.gt_keys.ContainsKey(thekey)) return 0;
GeoKey key=gtif.gt_keys[thekey];
if(key.gk_type!=tagtype_t.TYPE_ASCII) return 0;
val=(string)key.gk_data;
return val.Length;
}
public static int GTIFKeyGet(GTIF gtif, geokey_t thekey, out string val, int index, int count)
{
val=null;
if(count<0) return 0;
if(!gtif.gt_keys.ContainsKey(thekey)) return 0;
GeoKey key=gtif.gt_keys[thekey];
if(key.gk_type!=tagtype_t.TYPE_ASCII) return 0;
string val_=(string)key.gk_data;
if(count==0||(count+index)>val_.Length) val=val_.Substring(index);
else val=val_.Substring(index, count);
return val.Length;
}
}
}