Skip to content

Commit

Permalink
Properly support signed/unsigned field types for GeoTIFF tags
Browse files Browse the repository at this point in the history
* Particularly noticeable for sample min/max metadata
  • Loading branch information
WeatherGod committed May 6, 2024
1 parent bdc03af commit b5a17bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 5 additions & 5 deletions cdm/misc/src/main/java/ucar/nc2/geotiff/GeotiffWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,14 @@ private void writeMetadata(boolean greyScale, double xStart, double yStart, doub
FieldType ftype;
DataType sdtype = dtype.withSignedness(DataType.Signedness.SIGNED);
if (sdtype == DataType.BYTE) {
ftype = FieldType.BYTE;
ftype = dtype.isUnsigned() ? FieldType.BYTE : FieldType.SBYTE;
} else if (sdtype == DataType.SHORT) {
ftype = FieldType.SHORT;
ftype = dtype.isUnsigned() ? FieldType.SHORT : FieldType.SSHORT;
} else if (sdtype == DataType.INT) {
// A geotiff LONG is really a 4-byte regular integer
ftype = FieldType.LONG;
// A geotiff LONG/SLONG is really a 4-byte regular integer
ftype = dtype.isUnsigned() ? FieldType.LONG : FieldType.SLONG;
} else {
throw new IllegalArgumentException("Unsupported dtype: " + dtype + " sdtype: " + sdtype);
throw new IllegalArgumentException("Unsupported dtype: " + dtype);
}
geotiff.addTag(new IFDEntry(Tag.SMinSampleValue, ftype).setValue(min));
geotiff.addTag(new IFDEntry(Tag.SMaxSampleValue, ftype).setValue(max));
Expand Down
Binary file modified cdm/misc/src/test/data/ucar/nc2/geotiff/baseline_palette.tif
Binary file not shown.
21 changes: 19 additions & 2 deletions cdm/misc/src/test/java/ucar/nc2/geotiff/TestGeoTiffPalette.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Testing the color palette features of GeotiffWriter
*
* @author WeatherGod
* @since 8/12/20224
* @since 8/12/2022
*/
public class TestGeoTiffPalette {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
Expand Down Expand Up @@ -150,9 +150,26 @@ public void testWritePalette() throws IOException {
Assert.assertNotNull(colorTableTag);
Assert.assertEquals(3 * 256, colorTableTag.count);
Assert.assertArrayEquals(colorTable, colorTableTag.value);

IFDEntry sMinTag = geotiff.findTag(Tag.SMinSampleValue);
Assert.assertNotNull(sMinTag);
Assert.assertEquals(FieldType.BYTE, sMinTag.type);
Assert.assertEquals(1, sMinTag.count);
Assert.assertEquals(1, sMinTag.value[0]);

IFDEntry sMaxTag = geotiff.findTag(Tag.SMaxSampleValue);
Assert.assertNotNull(sMaxTag);
Assert.assertEquals(FieldType.BYTE, sMaxTag.type);
Assert.assertEquals(1, sMaxTag.count);
Assert.assertEquals(255, sMaxTag.value[0]);

// When doing a color paletted geotiff, no assumption is made
// about the NoData value and is therefore not encoded.
IFDEntry noDataTag = geotiff.findTag(Tag.GDALNoData);
Assert.assertNull(noDataTag);
}

// compare file s are equal
// compare files are equal
File file1 = new File(gridOut);
File file2 = new File(baseline);

Expand Down

0 comments on commit b5a17bb

Please sign in to comment.