Skip to content

Commit

Permalink
Add helper function to read geoTiff file from filesystem in raster ja…
Browse files Browse the repository at this point in the history
…va test cases.

Add example test case
  • Loading branch information
iGN5117 committed Jul 29, 2023
1 parent 2c490d5 commit 4a9136b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.locationtech.jts.geom.Geometry;
import org.opengis.referencing.FactoryException;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class RasterAccessorsTest extends RasterTestBase
Expand All @@ -48,6 +50,14 @@ public void testWidthAndHeight() throws FactoryException {
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 10, 20, 0, 0, 8);
assertEquals(20, RasterAccessors.getHeight(emptyRaster));
assertEquals(10, RasterAccessors.getWidth(emptyRaster));

}

@Test
public void testWidthAndHeightFromRasterFile() throws IOException {
GridCoverage2D raster = rasterFromGeoTiff(resourceFolder + "raster/test1.tiff");
assertEquals(512, RasterAccessors.getWidth(raster));
assertEquals(517, RasterAccessors.getHeight(raster));
}

@Test
Expand All @@ -67,7 +77,7 @@ public void testScaleY() throws UnsupportedOperationException, FactoryException
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(2, 10, 15, 0, 0, 1, 2, 0, 0, 0);
assertEquals(-2, RasterAccessors.getScaleY(emptyRaster), 1e-9);
}

@Test
public void testMetaData()
throws FactoryException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.data.DataSourceException;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.gce.geotiff.GeoTiffWriter;
import org.geotools.geometry.Envelope2D;
import org.geotools.geometry.jts.ReferencedEnvelope;
Expand All @@ -30,11 +32,15 @@
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class RasterTestBase {
String arc = "NCOLS 2\nNROWS 2\nXLLCORNER 378922\nYLLCORNER 4072345\nCELLSIZE 30\nNODATA_VALUE 0\n0 1 2 3\n";

String resourceFolder = System.getProperty("user.dir") + "/../core/src/test/resources/";

GridCoverage2D oneBandRaster;
GridCoverage2D multiBandRaster;
byte[] geoTiff;
Expand Down Expand Up @@ -101,4 +107,10 @@ GridCoverage2D createMultibandRaster() throws IOException {
}
return factory.create("test", image, new Envelope2D(DefaultGeographicCRS.WGS84, 0, 0, 10, 10));
}

GridCoverage2D rasterFromGeoTiff(String filePath) throws IOException {
File geoTiffFile = new File(filePath);
GridCoverage2D raster = new GeoTiffReader(geoTiffFile).read(null);
return raster;
}
}

0 comments on commit 4a9136b

Please sign in to comment.