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

Resample code cleanup #3550

Merged
merged 1 commit into from
Oct 6, 2024
Merged
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
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