|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 Drew Noakes and contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * More information about this project is available at: |
| 17 | + * |
| 18 | + * https://drewnoakes.com/code/exif/ |
| 19 | + * https://github.com/drewnoakes/metadata-extractor |
| 20 | + */ |
| 21 | +package com.drew.metadata.geotiff; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertFalse; |
| 25 | +import static org.junit.Assert.assertNotNull; |
| 26 | +import static org.junit.Assert.assertNull; |
| 27 | +import static org.junit.Assert.assertTrue; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.io.FilenameFilter; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import com.drew.imaging.tiff.TiffMetadataReader; |
| 36 | +import com.drew.metadata.Metadata; |
| 37 | +import com.drew.metadata.exif.ExifDirectoryBase; |
| 38 | +import com.drew.metadata.exif.ExifIFD0Directory; |
| 39 | + |
| 40 | +public class GeoTiffTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testGeogToWGS84GeoKey5() throws Exception { |
| 44 | + Metadata metadata = TiffMetadataReader.readMetadata(new File("Tests/Data/GeogToWGS84GeoKey5.tif")); |
| 45 | + assertNotNull(metadata); |
| 46 | + |
| 47 | + ExifIFD0Directory exif = checkExif(metadata, 24); |
| 48 | + |
| 49 | + assertEquals("[32 values]", exif.getDescription(ExifDirectoryBase.TAG_GEOTIFF_GEO_KEYS)); |
| 50 | + assertEquals("[768 values]", exif.getDescription(ExifDirectoryBase.TAG_COLOR_MAP)); |
| 51 | + assertEquals("0 0 1", exif.getDescription(ExifDirectoryBase.TAG_PIXEL_SCALE)); |
| 52 | + assertEquals("50.5 50.5 0 9.001 52.001 0", exif.getDescription(ExifDirectoryBase.TAG_MODEL_TIE_POINT).replace(',', '.')); |
| 53 | + assertNull(exif.getDescription(ExifDirectoryBase.TAG_GEOTIFF_GEO_ASCII_PARAMS)); |
| 54 | + assertNull(exif.getDescription(ExifDirectoryBase.TAG_GEOTIFF_GEO_DOUBLE_PARAMS)); |
| 55 | + assertNull(exif.getDescription(ExifDirectoryBase.TAG_GDAL_METADATA)); |
| 56 | + assertNull(exif.getDescription(ExifDirectoryBase.TAG_GDAL_NO_DATA)); |
| 57 | + |
| 58 | + GeoTiffDirectory geotiff = checkGeoTiff(metadata); |
| 59 | + |
| 60 | + assertEquals("Geographic", geotiff.getDescription(GeoTiffDirectory.TAG_MODEL_TYPE)); |
| 61 | + assertEquals("PixelIsArea", geotiff.getDescription(GeoTiffDirectory.TAG_RASTER_TYPE)); |
| 62 | + assertEquals("User Defined", geotiff.getDescription(GeoTiffDirectory.TAG_GEOGRAPHIC_TYPE)); |
| 63 | + assertEquals("User Defined", geotiff.getDescription(GeoTiffDirectory.TAG_GEODETIC_DATUM)); |
| 64 | + assertEquals("Angular Degree", geotiff.getDescription(GeoTiffDirectory.TAG_GEOGRAPHIC_ANGULAR_UNITS)); |
| 65 | + assertEquals("Bessel 1841", geotiff.getDescription(GeoTiffDirectory.TAG_GEOGRAPHIC_ELLIPSOID)); |
| 66 | + assertEquals("598.1 73.7 418.2 0.202 0.045 -2.455 6.7", geotiff.getDescription(GeoTiffDirectory.TAG_GEOGRAPHIC_TO_WGS84).replace(',', '.')); |
| 67 | + assertEquals(7, geotiff.getTagCount()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testLibgeotiff() throws Exception { |
| 72 | + for (File tiffFile : new File("Tests/Data/libgeotiff").listFiles(new FilenameFilter() { |
| 73 | + @Override |
| 74 | + public boolean accept(File dir, String name) { |
| 75 | + return name.endsWith(".tif"); |
| 76 | + } |
| 77 | + })) |
| 78 | + { |
| 79 | + Metadata metadata = TiffMetadataReader.readMetadata(tiffFile); |
| 80 | + assertNotNull(tiffFile.getName(), metadata); |
| 81 | + checkExif(metadata, 14); |
| 82 | + String description = tiffFile.getName() + "\n " + checkGeoTiff(metadata).getTags(); |
| 83 | + assertFalse(description, description.contains("Unknown")); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static ExifIFD0Directory checkExif(Metadata metadata, int numberOfTags) { |
| 88 | + ExifIFD0Directory exif = metadata.getDirectoriesOfType(ExifIFD0Directory.class).iterator().next(); |
| 89 | + assertNotNull(exif); |
| 90 | + assertFalse(Objects.toString(exif.getErrors()), exif.hasErrors()); |
| 91 | + assertEquals(numberOfTags, exif.getTagCount()); |
| 92 | + return exif; |
| 93 | + } |
| 94 | + |
| 95 | + private static GeoTiffDirectory checkGeoTiff(Metadata metadata) { |
| 96 | + GeoTiffDirectory geotiff = metadata.getDirectoriesOfType(GeoTiffDirectory.class).iterator().next(); |
| 97 | + assertNotNull(geotiff); |
| 98 | + assertFalse(Objects.toString(geotiff.getErrors()), geotiff.hasErrors()); |
| 99 | + assertTrue(geotiff.getTagCount() > 0); |
| 100 | + return geotiff; |
| 101 | + } |
| 102 | +} |
0 commit comments