Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoTIFF fixes + add test #324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions MetadataExtractor.Tests/Data/GeogToWGS84GeoKey5.lgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Geotiff_Information:
Version: 1
Key_Revision: 1.0
Tagged_Information:
ModelTiepointTag (2,3):
50.5 50.5 0
9.00105738 52.001376 0
ModelPixelScaleTag (1,3):
2.77777778e-005 2.77777778e-005 1
End_Of_Tags.
Keyed_Information:
GTModelTypeGeoKey (Short,1): ModelTypeGeographic
GTRasterTypeGeoKey (Short,1): RasterPixelIsArea
GeographicTypeGeoKey (Short,1): User-Defined
GeogGeodeticDatumGeoKey (Short,1): User-Defined
GeogAngularUnitsGeoKey (Short,1): Angular_Degree
GeogEllipsoidGeoKey (Short,1): Ellipse_Bessel_1841
Unknown-2062 (Double,7): 598.1 73.7 418.2
0.202 0.045 -2.455
6.7
End_Of_Keys.
End_Of_Geotiff.

Ellipsoid: 7004/(unknown) (0.00,0.00)

Corner Coordinates:
Upper Left ( 8d59'58.76"E, 52d 0'10.00"N)
Lower Left ( 8d59'58.76"E, 51d59'59.90"N)
Upper Right ( 9d 0' 8.86"E, 52d 0'10.00"N)
Lower Right ( 9d 0' 8.86"E, 51d59'59.90"N)
Center ( 9d 0' 3.81"E, 52d 0' 4.95"N)
Binary file not shown.
10 changes: 10 additions & 0 deletions MetadataExtractor.Tests/Data/GeogToWGS84GeoKey5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GeogToWGS84GeoKey5.tif is an sample GeoTIFF file for the GeogToWGS84GeoKey
geo key. After datum transformation to the WGS84 reference system the center
of the image (marked by a red pixel) should be exactly at N 52° E 9° in a
geographic coordinate system.

The output of listgeo is included as GeogToWGS84GeoKey5.lgo.

The files are placed in the public domain.

Christian Zietz <czietz@gmx.net>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added MetadataExtractor.Tests/Data/libgeotiff/byte.tif
Binary file not shown.
Binary file added MetadataExtractor.Tests/Data/libgeotiff/byte_v11.tif
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added MetadataExtractor.Tests/Data/libgeotiff/lcc1sp.tif
Binary file not shown.
Binary file added MetadataExtractor.Tests/Data/libgeotiff/lcc2sp.tif
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
77 changes: 77 additions & 0 deletions MetadataExtractor.Tests/Formats/GeoTiff/GeoTiffTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using MetadataExtractor.Formats.Exif;
using MetadataExtractor.Formats.GeoTiff;
using MetadataExtractor.Formats.Tiff;
using Xunit;

namespace MetadataExtractor.Tests.Formats.GeoTiff
{
public sealed class GeoTiffTest
{
#region Helpers
private static ExifIfd0Directory CheckExif(IEnumerable<Directory> metadata, int numberOfTags)
{
var exif = metadata.OfType<ExifIfd0Directory>().FirstOrDefault();
Assert.NotNull(exif);
Assert.False(exif.HasError);
Assert.Equal(numberOfTags, exif.TagCount);
return exif;
}

private static GeoTiffDirectory CheckGeoTiff(IEnumerable<Directory> metadata)
{
var geotiff = metadata.OfType<GeoTiffDirectory>().FirstOrDefault();
Assert.NotNull(geotiff);
Assert.False(geotiff.HasError);
Assert.True(geotiff.TagCount > 0);
return geotiff;
}

#endregion

[Fact]
public void TestGeogToWGS84GeoKey5()
{
IEnumerable<Directory> metadata = TiffMetadataReader.ReadMetadata("Data/GeogToWGS84GeoKey5.tif");
Assert.NotNull(metadata);

ExifIfd0Directory exif = CheckExif(metadata, 23);

//Assert.Equal("[32 values]", exif.GetDescription(ExifDirectoryBase.TagGeoTiffGeoKeys));
Assert.Equal("[768 values]", exif.GetDescription(ExifDirectoryBase.TagColorMap));
Assert.Equal("0 0 1", exif.GetDescription(ExifDirectoryBase.TagPixelScale));
Assert.Equal("50.5 50.5 0 9.001 52.001 0", exif.GetDescription(ExifDirectoryBase.TagModelTiePoint).Replace(',', '.'));
Assert.Null(exif.GetDescription(ExifDirectoryBase.TagGeoTiffGeoAsciiParams));
Assert.Null(exif.GetDescription(ExifDirectoryBase.TagGeoTiffGeoDoubleParams));
Assert.Null(exif.GetDescription(ExifDirectoryBase.TagGdalMetadata));
Assert.Null(exif.GetDescription(ExifDirectoryBase.TagGdalNoData));

GeoTiffDirectory geotiff = CheckGeoTiff(metadata);

Assert.Equal("Geographic", geotiff.GetDescription(GeoTiffDirectory.TagModelType));
Assert.Equal("PixelIsArea", geotiff.GetDescription(GeoTiffDirectory.TagRasterType));
Assert.Equal("User Defined", geotiff.GetDescription(GeoTiffDirectory.TagGeographicType));
Assert.Equal("User Defined", geotiff.GetDescription(GeoTiffDirectory.TagGeodeticDatum));
Assert.Equal("Angular Degree", geotiff.GetDescription(GeoTiffDirectory.TagGeographicAngularUnits));
Assert.Equal("Bessel 1841", geotiff.GetDescription(GeoTiffDirectory.TagGeographicEllipsoid));
Assert.Equal("598.1 73.7 418.2 0.202 0.045 -2.455 6.7", geotiff.GetDescription(GeoTiffDirectory.TagGeographicToWgs84).Replace(',', '.'));
Assert.Equal(7, geotiff.TagCount);
}

[Fact]
public void TestLibgeotiff()
{
foreach (string tiffFile in System.IO.Directory.GetFiles("Data/libgeotiff", "*.tif"))
{
IEnumerable<Directory> metadata = TiffMetadataReader.ReadMetadata(tiffFile);
Assert.NotNull(metadata);
CheckExif(metadata, 13);
var description = tiffFile + "\n " + CheckGeoTiff(metadata).Tags;
Assert.DoesNotContain(description, "Unknown");
}
}
}
}
8 changes: 8 additions & 0 deletions MetadataExtractor/Formats/Exif/ExifDirectoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public abstract class ExifDirectoryBase : Directory

public const int TagPrimaryChromaticities = 0x013F;

/// <summary>
/// A color map for palette color images.
/// This field defines a Red-Green-Blue color map(often called a lookup table) for palette-color images.
/// In a palette-color image, a pixel value is used to index into an RGB lookup table.
/// </summary>
public const int TagColorMap = 0x0140;

public const int TagTileWidth = 0x0142;

public const int TagTileLength = 0x0143;
Expand Down Expand Up @@ -778,6 +785,7 @@ protected static void AddExifTagNames(Dictionary<int, string> map)
map[TagHostComputer] = "Host Computer";
map[TagWhitePoint] = "White Point";
map[TagPrimaryChromaticities] = "Primary Chromaticities";
map[TagColorMap] = "Color Map";
map[TagTileWidth] = "Tile Width";
map[TagTileLength] = "Tile Length";
map[TagTileOffsets] = "Tile Offsets";
Expand Down
2 changes: 1 addition & 1 deletion MetadataExtractor/Formats/Exif/ExifTiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private void ProcessGeoTiff(ushort[] geoKeys, ExifIfd0Directory sourceDirectory)
}
else if (sourceValue is Array sourceArray)
{
if (valueOffset + valueCount < sourceArray.Length)
if (valueOffset + valueCount <= sourceArray.Length)
{
var array = Array.CreateInstance(sourceArray.GetType().GetElementType(), valueCount);
Array.Copy(sourceArray, valueOffset, array, 0, valueCount);
Expand Down
Loading