From d6721e8a869504e5a11e1aa847bdb725efb467a2 Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Sun, 6 Oct 2024 12:08:08 -0400 Subject: [PATCH] Resample code cleanup --- .../reproject/RasterRegionReproject.scala | 10 +++--- .../geotrellis/raster/resample/Resample.scala | 32 ++++++++++++------- .../raster/reproject/ReprojectSpec.scala | 8 ++--- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/raster/src/main/scala/geotrellis/raster/reproject/RasterRegionReproject.scala b/raster/src/main/scala/geotrellis/raster/reproject/RasterRegionReproject.scala index b302668f0a..3fdfd55f62 100644 --- a/raster/src/main/scala/geotrellis/raster/reproject/RasterRegionReproject.scala +++ b/raster/src/main/scala/geotrellis/raster/reproject/RasterRegionReproject.scala @@ -193,12 +193,12 @@ object RasterRegionReproject { val trans = Proj4Transform(dest, src) val resampler = resampleMethod match { - case resampleMethod1: PointResampleMethod => - Resample(resampleMethod1, raster.tile, raster.extent) - case _ => - //throws GeoAttrsError when applied for invalid extent + case pointResampleMethod: PointResampleMethod => + Resample(pointResampleMethod, raster.tile, raster.extent) + case aggregateResampleMethod: AggregateResampleMethod => + // throws GeoAttrsError when applied for invalid extent val targetCellSizeInSrcCRS = rasterExtent.reproject(dest, src).cellSize - Resample(resampleMethod, raster.tile, raster.extent, targetCellSizeInSrcCRS) + Resample(aggregateResampleMethod, raster.tile, raster.extent, targetCellSizeInSrcCRS) } val rowcoords = rowCoords(region, rasterExtent, trans, errorThreshold) diff --git a/raster/src/main/scala/geotrellis/raster/resample/Resample.scala b/raster/src/main/scala/geotrellis/raster/resample/Resample.scala index 85533c830d..1209856d50 100644 --- a/raster/src/main/scala/geotrellis/raster/resample/Resample.scala +++ b/raster/src/main/scala/geotrellis/raster/resample/Resample.scala @@ -79,7 +79,7 @@ abstract class Resample(tile: Tile, extent: Extent) { object Resample { /** Create a resampler. * - * @param method The method [[ResampleMethod]] to use. + * @param method The method [[PointResampleMethod]] to use. * @param tile The tile that is the source of the resample * @param extent The extent of source tile. */ @@ -92,6 +92,23 @@ object Resample { case Lanczos => new LanczosResample(tile, extent) } + /** Create a resampler. + * + * @param method The method [[AggregateResampleMethod]] to use. + * @param tile The tile that is the source of the resample + * @param extent The extent of source tile. + * @param cs The cell size of the target, for usage with Aggregate resample methods. + */ + def apply(method: AggregateResampleMethod, tile: Tile, extent: Extent, cs: CellSize): Resample = + method match { + case Average => new AverageResample(tile, extent, cs) + case Mode => new ModeResample(tile, extent, cs) + case Median => new MedianResample(tile, extent, cs) + case Max => new MaxResample(tile, extent, cs) + case Min => new MinResample(tile, extent, cs) + case Sum => new SumResample(tile, extent, cs) + } + /** Create a resampler. * * @param method The method [[ResampleMethod]] to use. @@ -101,16 +118,7 @@ object Resample { */ def apply(method: ResampleMethod, tile: Tile, extent: Extent, cs: CellSize): Resample = method match { - case NearestNeighbor => new NearestNeighborResample(tile, extent) - case Bilinear => new BilinearResample(tile, extent) - case CubicConvolution => new BicubicConvolutionResample(tile, extent) - case CubicSpline => new BicubicSplineResample(tile, extent) - case Lanczos => new LanczosResample(tile, extent) - case Average => new AverageResample(tile, extent, cs) - case Mode => new ModeResample(tile, extent, cs) - case Median => new MedianResample(tile, extent, cs) - case Max => new MaxResample(tile, extent, cs) - case Min => new MinResample(tile, extent, cs) - case Sum => new SumResample(tile, extent, cs) + case pointResampleMethod: PointResampleMethod => apply(pointResampleMethod, tile, extent) + case aggregateResampleMethod: AggregateResampleMethod => apply(aggregateResampleMethod, tile, extent, cs) } } diff --git a/raster/src/test/scala/geotrellis/raster/reproject/ReprojectSpec.scala b/raster/src/test/scala/geotrellis/raster/reproject/ReprojectSpec.scala index 88bdae1a82..4f1086a873 100644 --- a/raster/src/test/scala/geotrellis/raster/reproject/ReprojectSpec.scala +++ b/raster/src/test/scala/geotrellis/raster/reproject/ReprojectSpec.scala @@ -65,17 +65,17 @@ class ReprojectSpec extends AnyFunSpec it("should (approximately) match a GDAL average interpolation on nlcd tile") { - val raster = this.createRaster(Array(1,4,1,4,1,4,1,4),4,2,CellSize(10,10)) - val tempTiff = File.createTempFile("toReproject",".tif") + val raster = createRaster(Array(1,4,1,4,1,4,1,4),4,2,CellSize(10,10)) + val tempTiff = File.createTempFile("gdal-average-to-reproject",".tif") GeoTiff(raster, CRS.fromEpsgCode(32631)).write(tempTiff.getPath) val rs = GeoTiffRasterSource(tempTiff.getPath).reproject(CRS.fromEpsgCode(3035), TargetCellSize(CellSize(20.0,20.0)), Average) val reprojected = raster.reproject(CRS.fromEpsgCode(32631),CRS.fromEpsgCode(3035), Options(method = Average, errorThreshold = 0.0,targetCellSize = Some(CellSize(20,20)))) - val refTile = this.createTile(Array(2.5,2.5),2,1) + val refTile = createTile(Array(2.5,2.5),2,1) assertEqual(refTile,reprojected.tile,0.1) - assertEqual(rs.read().get.tile.band(0),reprojected.tile,0.1) + assertEqual(rs.read().get.tile.band(0), reprojected.tile, 0.1) } it("should (approximately) match a GDAL nearest neighbor interpolation on slope tif") {