diff --git a/spark/src/main/scala/geotrellis/spark/reproject/TileRDDReproject.scala b/spark/src/main/scala/geotrellis/spark/reproject/TileRDDReproject.scala index 142c109208..371f9dfc8a 100644 --- a/spark/src/main/scala/geotrellis/spark/reproject/TileRDDReproject.scala +++ b/spark/src/main/scala/geotrellis/spark/reproject/TileRDDReproject.scala @@ -70,8 +70,16 @@ object TileRDDReproject { implicit val sc = bufferedTiles.context val sourceDataGridExtent = metadata.layout.createAlignedGridExtent(metadata.extent) - val passthroughGridExtent = ReprojectRasterExtent(sourceDataGridExtent, metadata.crs, destCrs) - val targetDataExtent = passthroughGridExtent.extent + val (targetCellSize:CellSize,targetDataExtent:Extent)= + targetLayout match { + case Right(l) => { + (l.cellSize, l.createAlignedGridExtent(ProjectedExtent(metadata.extent,metadata.crs).reproject(destCrs)).extent) + } + case Left(l) => { + val passthroughGridExtent = ReprojectRasterExtent(sourceDataGridExtent, metadata.crs, destCrs) + (passthroughGridExtent.cellSize,passthroughGridExtent.extent) + } + } val targetPartitioner: Option[Partitioner] = partitioner.orElse(bufferedTiles.partitioner) @@ -112,7 +120,7 @@ object TileRDDReproject { layoutScheme.levelFor(targetDataExtent, ct) case None => - layoutScheme.levelFor(targetDataExtent, passthroughGridExtent.cellSize) + layoutScheme.levelFor(targetDataExtent, targetCellSize) } } } diff --git a/spark/src/test/scala/geotrellis/spark/reproject/TileRDDReprojectSpec.scala b/spark/src/test/scala/geotrellis/spark/reproject/TileRDDReprojectSpec.scala index b0492a5d8d..4c261e9681 100644 --- a/spark/src/test/scala/geotrellis/spark/reproject/TileRDDReprojectSpec.scala +++ b/spark/src/test/scala/geotrellis/spark/reproject/TileRDDReprojectSpec.scala @@ -26,10 +26,8 @@ import geotrellis.spark.reproject.Reproject.Options import geotrellis.spark.testkit._ import geotrellis.vector._ import geotrellis.proj4._ - import spire.syntax.cfor._ import org.apache.spark._ - import org.scalatest.funspec.AnyFunSpec class TileRDDReprojectSpec extends AnyFunSpec with TestEnvironment { @@ -328,4 +326,23 @@ class TileRDDReprojectSpec extends AnyFunSpec with TestEnvironment { afterMetadata.layout should be (beforeMetadata.layout) } } + + describe("Very small cube at high latitude") { + it("should reproject without error") { + val layout = LayoutDefinition(RasterExtent(Extent(-120.550992, 71.82628788888888, -120.5421031111111, 71.83517677777778),32,32),32,32) + val targetLayout = LayoutDefinition(RasterExtent(Extent(585180.0, 7971640.0, 585820.0, 7972280.0),32,32),32,32) + val extent = Extent(-120.550992, 71.834899, -120.550792, 71.835099) + val cube: TileLayerRDD[SpatialKey] = TileLayerRDDBuilders.createTileLayerRDD(sc,ByteArrayTile.fill(1,32,32),layout.tileLayout,LatLng) + + val utm = CRS.fromEpsgCode(32610) + + val badMetadata = cube.metadata.copy(crs = LatLng, extent = extent,layout = layout) + val result = TileRDDReproject(ContextRDD(cube,badMetadata), utm, Right(targetLayout), 16,Reproject.Options.DEFAULT,None) + //should not throw an error + val resultMetadata = result._2.metadata + targetLayout should be (resultMetadata.layout) + utm should be (resultMetadata.crs) + + } + } }