-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeoTiffInfo.java
More file actions
388 lines (317 loc) · 15.6 KB
/
GeoTiffInfo.java
File metadata and controls
388 lines (317 loc) · 15.6 KB
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
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import mil.nga.tiff.Rasters;
import mil.nga.tiff.TIFFImage;
import mil.nga.tiff.TiffReader;
import mil.nga.tiff.TiffWriter;
import mil.nga.tiff.FileDirectory;
import mil.nga.tiff.FileDirectoryEntry;
import mil.nga.tiff.FieldType;
import mil.nga.tiff.util.TiffConstants;
import mil.nga.tiff.FieldTagType;
// for EU_DTM dataset from OT
// Horizontal: ETRS89-extended / LAEA Europe [EPSG: 3035]
// Vertical: EGM2008 [EPSG: 3855]
// We need to convert the horizontal to WGS84
// for SRTM dataset from OT
// Horizontal: WGS84 [EPSG: 4326]
// Vertical: WGS84 (EGM GEOID)
// for USGS10m dataset from OT
// Horizontal Coordinates: NAD83 [EPSG: 4269]
// Vertical Coordinates: NAVD88 [EPSG: 5703], EGM GEOID
// We need to convert the horizontal to WGS84
// java -cp ./lib/core-0.4.1-SNAPSHOT.jar GeoTiffInfo.java
// we use core lib to get access to mil.nga.tiff
public class GeoTiffInfo
{
static int GEOKEY_PIXELSCALE_TAG = 33550;
static int GEOKEY_DIRECTORY_TAG = 34735;
//static int VERTICAL_CRS_TAG = 4096;
//static int HORIZONTAL_CRS_TAG = 3072;
static int PROJECTED_CRS_TAG = 3072;
static int GEOGRAPHIC_CRS_TAG = 2048;
static int VERTICAL_CRS_TAG = 4096;
static int GEOKEY_ASCII_PARAMS_TAG = 34737;
static int samplesPerPixel = 0;
public static void main(String[] args)
{
if (args.length < 1) {
System.out.println("Usage: GeoTiffInfo <inputFilePath>");
return;
}
String inputFilePath = args[0];
try {
// Read the GeoTIFF file using TiffReader
TIFFImage tiffImage = TiffReader.readTiff(new File(inputFilePath));
// Get the rasters from the first image directory
FileDirectory originalDirectory = tiffImage.getFileDirectories().get(0);
Rasters rasters = originalDirectory.readRasters();
if (rasters == null) {
System.out.println("No raster data found in the GeoTIFF.");
return;
}
System.out.println("Read GeoTIFF with dimensions: "
+ rasters.getWidth() + "x" + rasters.getHeight());
// Extract pixel scale and model tie point from metadata
double[] pixelScale = extractModelPixelScale(originalDirectory);
double[] modelTiePoint = extractModelTiePoint(originalDirectory);
System.out.println("Horizontal Datum: "+getHorizontalDatum(originalDirectory));
//System.out.println("Horziontal Datum: "+extractHorizontalDatum(originalDirectory));
System.out.println("Vertical Datum: "+getVerticalDatum(originalDirectory));
System.out.println("Vertical Datum: "+extractVerticalDatum(originalDirectory));
searchAllMetadata(tiffImage);
String crs = extractCoordinateSystem(originalDirectory);
System.out.println("Coordinate system is "+crs);
// Define FieldType array (FLOAT to accommodate latitude, longitude, elevation)
samplesPerPixel = rasters.getSamplesPerPixel();
FieldType[] fieldTypes = new FieldType[samplesPerPixel];
for (int i = 0; i < samplesPerPixel; i++) {
fieldTypes[i] = FieldType.FLOAT; // Use FLOAT for coordinate and elevation data
}
System.out.println("Samples per pixel is "+samplesPerPixel);
// inspect each pixel
for (int y=0; y < rasters.getHeight(); y++) {
for (int x=0; x < rasters.getWidth(); x++) {
Number[] sampleValues = rasters.getPixel(x,y);
double[] doubleValues = new double[sampleValues.length];
for (int i=0; i< sampleValues.length; i++) {
doubleValues[i] = sampleValues[i].doubleValue();
}
//System.out.println(x+","+y+": "+doubleValues[0]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static double[] extractModelPixelScale(FileDirectory directory)
{
// use API to get pixel scale
List<Double> pixelScaleList = directory.getModelPixelScale();
if ((pixelScaleList == null) || (pixelScaleList.isEmpty())) {
System.out.println("extractModelPixelScale: empty/null pixel scale list");
return null;
}
double[] pixelScale = new double[3];
pixelScale[0] = pixelScaleList.get(0); // x
pixelScale[1] = pixelScaleList.get(1); // y
pixelScale[2] = pixelScaleList.get(2); // z
System.out.println("Pixel Scale is "+String.format("%.15f",pixelScale[0])+","+
String.format("%.15f",pixelScale[1])+","+
String.format("%.15f",pixelScale[2]));
return pixelScale;
}
public static double[] extractModelTiePoint(FileDirectory directory)
{
double[] modelTiePoint = {0.0, 0.0, 0.0};
List<Double>tiePointList = directory.getModelTiepoint();
if ((tiePointList == null) || (tiePointList.isEmpty())) {
System.out.println("extractModelTiePoint: empty/null tie point list");
return null;
}
double[] tiePoints = new double[6];
tiePoints[0] = tiePointList.get(0); // X raster space coordinates
tiePoints[1] = tiePointList.get(1); // Y
tiePoints[2] = tiePointList.get(2); // Z
tiePoints[3] = tiePointList.get(3); // x CRS x
tiePoints[4] = tiePointList.get(4); // y CRS y
tiePoints[5] = tiePointList.get(5); // z CRS z
System.out.println("extractModelTiePoint: "+tiePoints[0]+","+tiePoints[1]+","+tiePoints[2]+" -> "+
tiePoints[3]+","+tiePoints[4]+","+tiePoints[5]);
return tiePoints;
}
// EPSG:1 is orthometric height
// EPSG:4326 is WGS 84 horizontal
// EPSG:4979 (WGS 84 Ellipsoidal height)
// EPSG:3035 is ETRS89 / LAEA Europe
// horizontal coordinate system in LAEA Europe is ETRS89, often represented as EPSG:4258.
// WGS 84 (EPSG:4326)
// NAD83 (EPSG:4269)
// ETRS89 (EPSG:4258)
// Vertical Datum: NAVD88 (EPSG:5703).
public static String extractCoordinateSystem(FileDirectory directory)
{
for (FileDirectoryEntry entry : directory.getEntries()) {
if (entry.getFieldTag() == FieldTagType.getById(GeoTiffInfo.GEOKEY_ASCII_PARAMS_TAG)) {
Object value = entry.getValues();
if (value instanceof List) {
List<?> valuesList = (List<?>) value;
for (Object val : valuesList) {
System.out.println("Val class "+val.getClass());
if (val instanceof String) {
System.out.println("extractCoordinateSystem: string "+val);
}
}
}
}
}
return "a list of tags";
}
// public static int getHorizontalDatum(FileDirectory directory)
// {
// FileDirectoryEntry fde = directory.get(FieldTagType.GeoKeyDirectory);
// ArrayList<Integer> geoKeys = (ArrayList<Integer>)fde.getValues();
// int numberOfKeys = geoKeys.get(3);
// for (int i = 0; i < numberOfKeys; i++) {
// int index = 4 + i * 4;
// int keyId = geoKeys.get(index);
// int tiffTagLocation = geoKeys.get(index + 1);
// int count = geoKeys.get(index + 2);
// int valueOffset = geoKeys.get(index + 3);
// if (keyId == 2048) { // GeographicTypeGeoKey
// if (tiffTagLocation == 0) {
// // The valueOffset is the value of the key.
// int value = valueOffset;
// System.out.println("Horizontal datum:: "+value);
// return value;
// } else {
// // The valueOffset is an offset into the tag specified by tiffTagLocation.
// // Read the value from the specified tag.
// FileDirectoryEntry tagEntry = directory.get(FieldTagType.getById(tiffTagLocation));
// if (tagEntry != null) {
// Object tagValues = tagEntry.getValues();
// if (tagValues instanceof ArrayList) {
// ArrayList<Integer> tagArray = (ArrayList<Integer>) tagValues;
// int arrayIndex = valueOffset / 2; // Convert byte offset to array index
// if (arrayIndex < tagArray.size()) {
// int tagValue = tagArray.get(arrayIndex);
// System.out.println("Horizontal datum: "+tagValue);
// return tagValue;
// }
// }
// }
// }
// }
// }
// return 0;
// }
public static int getVerticalDatum(FileDirectory directory)
{
FileDirectoryEntry fde = directory.get(FieldTagType.GeoKeyDirectory);
ArrayList<Integer> geoKeys = (ArrayList<Integer>)fde.getValues();
int numberOfKeys = geoKeys.get(3);
for (int i = 0; i < numberOfKeys; i++) {
int index = 4 + i * 4;
int keyId = geoKeys.get(index);
int tiffTagLocation = geoKeys.get(index + 1);
int count = geoKeys.get(index + 2);
int valueOffset = geoKeys.get(index + 3);
if (keyId == 4096) { // VERTICAL_CRS_TAG
if (tiffTagLocation == 0) {
// The valueOffset is the value of the key.
int value = valueOffset;
System.out.println("getVerticalDatum:: "+value);
return value;
} else {
// The valueOffset is an offset into the tag specified by tiffTagLocation.
// Read the value from the specified tag.
FileDirectoryEntry tagEntry = directory.get(FieldTagType.getById(tiffTagLocation));
if (tagEntry != null) {
Object tagValues = tagEntry.getValues();
if (tagValues instanceof ArrayList) {
ArrayList<Integer> tagArray = (ArrayList<Integer>)tagValues;
int arrayIndex = valueOffset / 2; // Convert byte offset to array index
if (arrayIndex < tagArray.size()) {
int tagValue = tagArray.get(arrayIndex);
System.out.println("Vertical datum: 4326");
return tagValue;
}
}
}
}
}
}
return 0;
}
/**
* Extracts the Horizontal Datum (EPSG code) from a GeoTIFF FileDirectory.
* Looks for GeographicTypeGeoKey (2048) or ProjectedCSTypeGeoKey (3072).
* @param directory The FileDirectory to search.
* @return The horizontal datum as a string or null if not found.
*/
// Tag: 34736 | Type: ArrayList | Value: [298.257223563, 6378137.0]
// these values ipmly WGS84 ellipsoid
public static int getHorizontalDatum(FileDirectory directory) {
final int GEOKEY_DIRECTORY_TAG = 34735;
final int GEOGRAPHIC_CRS_TAG = 2048; // GeographicTypeGeoKey
final int PROJECTED_CRS_TAG = 3072; // ProjectedCSTypeGeoKey
for (FileDirectoryEntry entry : directory.getEntries()) {
if (entry.getFieldTag() == FieldTagType.getById(GEOKEY_DIRECTORY_TAG)) {
Object value = entry.getValues();
if (value instanceof List) {
List<?> valuesList = (List<?>) value;
// Iterate over the values to find GeographicType or ProjectedCSType keys
for (int i = 0; i < valuesList.size() - 3; i += 4) {
try {
int keyID = ((Number) valuesList.get(i)).intValue();
int tiffTagLocation = ((Number) valuesList.get(i + 1)).intValue();
int count = ((Number) valuesList.get(i + 2)).intValue();
int epsgCode = ((Number) valuesList.get(i + 3)).intValue();
if (keyID == GEOGRAPHIC_CRS_TAG || keyID == PROJECTED_CRS_TAG) {
//return "EPSG:" + epsgCode;
return epsgCode;
}
} catch (ClassCastException e) {
System.err.println("Error casting GeoKey values.");
}
}
}
}
}
return 0;
}
/**
* Extracts the Vertical Datum (EPSG code) from a GeoTIFF FileDirectory.
* Looks for VerticalCSTypeGeoKey (4096).
* @param directory The FileDirectory to search.
* @return The vertical datum as a string or null if not found.
*/
public static int extractVerticalDatum(FileDirectory directory) {
final int GEOKEY_DIRECTORY_TAG = 34735;
final int VERTICAL_CRS_TAG = 4096; // VerticalCSTypeGeoKey
for (FileDirectoryEntry entry : directory.getEntries()) {
if (entry.getFieldTag() == FieldTagType.getById(GEOKEY_DIRECTORY_TAG)) {
Object value = entry.getValues();
if (value instanceof List) {
List<?> valuesList = (List<?>) value;
// Iterate over the values to find the VerticalCSTypeGeoKey
for (int i = 0; i < valuesList.size() - 3; i += 4) {
try {
int keyID = ((Number) valuesList.get(i)).intValue();
int tiffTagLocation = ((Number) valuesList.get(i + 1)).intValue();
int count = ((Number) valuesList.get(i + 2)).intValue();
int epsgCode = ((Number) valuesList.get(i + 3)).intValue();
// Check if the KeyID matches the VerticalCSTypeGeoKey
if (keyID == VERTICAL_CRS_TAG && epsgCode > 0) {
//return "EPSG:" + epsgCode;
return epsgCode;
}
} catch (ClassCastException e) {
System.err.println("Error casting GeoKey values for vertical datum.");
}
}
}
}
}
return 0;
}
public static int searchAllMetadata(TIFFImage tImage)
{
for (int i=0; i<tImage.getFileDirectories().size(); i++) {
FileDirectory aDir = tImage.getFileDirectories().get(i);
System.out.println("dump: verticalDatum is "+extractVerticalDatum(aDir));
System.out.println("dump: horizontalDatum is "+getHorizontalDatum(aDir));
for (FileDirectoryEntry entry: aDir.getEntries()) {
FieldTagType tag = entry.getFieldTag();
Object values = entry.getValues();
System.out.println("Tag: "+tag.getId()+" | Type: "+ values.getClass().getSimpleName() + " | Value: "+values);
}
}
return 0;
}
} // GeoTiffInfo