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

RDD reproject: GeoAttrsError #3559

Open
jdries opened this issue Jan 13, 2025 · 7 comments · May be fixed by #3560 or #3561
Open

RDD reproject: GeoAttrsError #3559

jdries opened this issue Jan 13, 2025 · 7 comments · May be fixed by #3560 or #3561
Labels

Comments

@jdries
Copy link
Contributor

jdries commented Jan 13, 2025

Describe the bug

Original source: Open-EO/openeo-geopyspark-driver#984
This method call:

val sourceDataGridExtent = metadata.layout.createAlignedGridExtent(metadata.extent)

Can result in:
geotrellis.raster.GeoAttrsError: invalid cols: 0

in case of very specific inputs:

  • an input cube with a small extent, like 1x1 pixel
  • high latitudes, where square pixels in EPSG:4326 are basically rectangular pixels in UTM

Stack trace:

geotrellis.raster.GeoAttrsError: invalid cols: 0

at geotrellis.raster.GridExtent.<init>(GridExtent.scala:45)
at geotrellis.raster.GridExtent.<init>(GridExtent.scala:58)
at geotrellis.raster.reproject.ReprojectRasterExtent$.apply(ReprojectRasterExtent.scala:71)
at geotrellis.raster.reproject.ReprojectRasterExtent$.apply(ReprojectRasterExtent.scala:82)
at geotrellis.raster.reproject.ReprojectRasterExtent$.apply(ReprojectRasterExtent.scala:86)
at geotrellis.spark.reproject.TileRDDReproject$.apply(TileRDDReproject.scala:73)
at geotrellis.spark.reproject.TileRDDReproject$.apply(TileRDDReproject.scala:285)
at geotrellis.spark.reproject.TileRDDReprojectMethods.reproject(TileRDDReprojectMethods.scala:92)
at org.openeo.geotrellis.OpenEOProcessesSpec.testEdgeCase(OpenEOProcessesSpec.scala:668)

To Reproduce

This example shows the full reprojection case and throws the error:

import geotrellis.spark._
import geotrellis.spark.reproject._

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(OpenEOProcessesSpec.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)
ContextRDD(cube,badMetadata).reproject(utm, targetLayout, Reproject.Options.DEFAULT)

The code below shows inputs to TileRDDReproject and the specific call to ReprojectRasterExtent which subsequently fails:

val layout = LayoutDefinition(RasterExtent(Extent(-120.550992, 71.82628788888888, -120.5421031111111, 71.83517677777778),32,32),32,32)
val extent = Extent(-120.550992, 71.834899, -120.550792, 71.835099)
var sourceDataGridExtent = layout.createAlignedGridExtent(extent)
val utm = CRS.fromEpsgCode(32610)
ReprojectRasterExtent(sourceDataGridExtent, LatLng, utm)

Note: I'm also looking into a test case at the level of TileRDDReproject...

Expected behavior

While this is clearly an edge case, it seems that it could be solved easily in the case where
targetLayout: Either[LayoutScheme, LayoutDefinition],
is actually a 'LayoutDefinition'.
In that case, the call to ReprojectRasterExtent is not needed at all, and we can compute a target extent that is aligned with the layout definition.

More specifically something like this:

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)
      }
    }

Environment

any

@pomadchin
Copy link
Member

Thanks for reporting! Yes, sounds like a not convenient edge case.

@jdries
Copy link
Contributor Author

jdries commented Jan 13, 2025

Thanks, I'm looking into creating a PR myself.

@pomadchin
Copy link
Member

Thank you! Also I wonder that in some of these edge cases a slim math solution may work 🤔 That's not the first time smth like that arises.

@jdries
Copy link
Contributor Author

jdries commented Jan 13, 2025

Indeed, there are in fact options at this level:

val newCols = (newExtent.width / pixelSizeX + 0.5).toLong

The problem occurs if the new extent width or height is smaller than 0.5 * pixelSize

In that case however, there's alternatives to throwing an exception:
A. if target cellsize is not set:
A.1 use non-square pixels, set X or Y pixel size so that it matches target extent width/height, resulting in exactly one pixel
A.2 use square pixel, but set it to min(targetExtent.width, targetExtent.height)
B. If target cellsize is set:
B.1 Still throw the error in this case
B.2 Return a gridextent with correct cellsize, but set the extent accordingly.

The solution I'll be testing first is at the level of TileRDDReproject and focuses on the case where a LayoutDefinition is available, I really think we can go for a more robust way of constructing an aligned target extent in that case.

jdries added a commit to VitoTAP/geotrellis that referenced this issue Jan 14, 2025
@jdries
Copy link
Contributor Author

jdries commented Jan 21, 2025

We're hitting another variant of this exception. Also here, we only need the resolution and not the GridExtent, which causes the failure.
In this case, we are indeed loading very low resolution data, so indeed the output gridextent would be less than 0.5 pixels.


Caused by: geotrellis.raster.GeoAttrsError: invalid rows: 0
	at geotrellis.raster.GridExtent.<init>(GridExtent.scala:46)
	at geotrellis.raster.GridExtent.<init>(GridExtent.scala:58)
	at geotrellis.raster.reproject.ReprojectRasterExtent$.apply(ReprojectRasterExtent.scala:71)
	at geotrellis.raster.reproject.ReprojectRasterExtent$.apply(ReprojectRasterExtent.scala:76)
	at geotrellis.raster.geotiff.GeoTiffReprojectRasterSource.closestTiffOverview$lzycompute(GeoTiffReprojectRasterSource.scala:86)
	at geotrellis.raster.geotiff.GeoTiffReprojectRasterSource.closestTiffOverview(GeoTiffReprojectRasterSource.scala:81)
	at geotrellis.raster.geotiff.GeoTiffReprojectRasterSource.readBounds(GeoTiffReprojectRasterSource.scala:110)
	at geotrellis.raster.geotiff.GeoTiffReprojectRasterSource.read(GeoTiffReprojectRasterSource.scala:98)

@pomadchin
Copy link
Member

@jdries is it captured in the PR? Apologizes for the long review times, will take a look later this week!

jdries added a commit to VitoTAP/geotrellis that referenced this issue Jan 23, 2025
@jdries jdries linked a pull request Jan 23, 2025 that will close this issue
5 tasks
@jdries
Copy link
Contributor Author

jdries commented Jan 23, 2025

I created a second PR that addresses the problem more broadly. It basically implements option A1 above, trying to make a minimal change that preserves existing behaviour as much as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants