Skip to content

Commit

Permalink
Resample code cleanup (#3550)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin authored Oct 6, 2024
1 parent cfa8e76 commit a43cc60
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
32 changes: 20 additions & 12 deletions raster/src/main/scala/geotrellis/raster/resample/Resample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit a43cc60

Please sign in to comment.