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

Fix numerical errors #3520

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Fix FileRangeReaderProvider parsing URI in windows [#3507](https://github.com/locationtech/geotrellis/pull/3507)
- Regrid: force crop to avoid going out of memory [#3518](https://github.com/locationtech/geotrellis/pull/3518)
- Fix rounding errors/numerical instability in GridExtent and LayoutTileSource [#3520](https://github.com/locationtech/geotrellis/pull/3520)

## [3.7.0] - 2023-02-26

Expand Down
4 changes: 2 additions & 2 deletions layer/src/main/scala/geotrellis/layer/LayoutTileSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class LayoutTileSource[K: SpatialComponent](
) {
LayoutTileSource.requireGridAligned(source.gridExtent, layout)

def sourceColOffset: Long = ((source.extent.xmin - layout.extent.xmin) / layout.cellwidth).toLong
def sourceRowOffset: Long = ((layout.extent.ymax - source.extent.ymax) / layout.cellheight).toLong
def sourceColOffset: Long = GridExtent.floorWithTolerance((source.extent.xmin - layout.extent.xmin) / layout.cellwidth).toLong
def sourceRowOffset: Long = GridExtent.floorWithTolerance((layout.extent.ymax - source.extent.ymax) / layout.cellheight).toLong

def rasterRegionForKey(key: K): Option[RasterRegion] = {
val spatialComponent = key.getComponent[SpatialKey]
Expand Down
8 changes: 4 additions & 4 deletions raster/src/main/scala/geotrellis/raster/GridExtent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,18 @@ class GridExtent[@specialized(Int, Long) N: Integral](
val colMaxDouble = mapXToGridDouble(subExtent.xmax)

if (math.abs(colMaxDouble - floorWithTolerance(colMaxDouble)) < GridExtent.epsilon)
colMaxDouble.toLong - 1L
floorWithTolerance(colMaxDouble).toLong - 1L
else
colMaxDouble.toLong
floorWithTolerance(colMaxDouble).toLong
}

val rowMax: N = Integral[N].fromLong {
val rowMaxDouble = mapYToGridDouble(subExtent.ymin)

if (math.abs(rowMaxDouble - floorWithTolerance(rowMaxDouble)) < GridExtent.epsilon)
rowMaxDouble.toLong - 1L
floorWithTolerance(rowMaxDouble).toLong - 1L
else
rowMaxDouble.toLong
floorWithTolerance(rowMaxDouble).toLong
}

if (clamp)
Expand Down
15 changes: 15 additions & 0 deletions raster/src/test/scala/geotrellis/raster/GridExtentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,20 @@ class GridExtentSpec extends AnyFunSpec with Matchers {
val actualGridExtent = gridExtent.withResolution(CellSize(1.4, 1.4))
expectedGridExtent should be (actualGridExtent)
}

it("should support roundtrip from grid bounds to raster extent and back") {
val targetBounds = GridBounds(55L, 103, 55 + 73, 103 + 111)
val ge = GridExtent[Long](Extent(1.8973214288275528, 49.352678585684544, 6.299107143119465, 51.7276785856839), CellSize(0.008928571428584, 0.008928571428568996))
val targetExtent = ge.extentFor(targetBounds)

//second, aligned extent based on a LayoutDefinition
val ge2 = GridExtent[Long](Extent(2.3883928573996727, 49.66517858568254, 3.5312500002584244, 50.80803572854129), CellSize(0.008928571428583998, 0.008928571428583998))

//gridextents have to be aligned for test to be valid
val resultingBounds = ge2.gridBoundsFor(targetExtent)

targetBounds.height should be resultingBounds.height
targetBounds.width should be resultingBounds.width
Comment on lines +194 to +195
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests don't compile!

Suggested change
targetBounds.height should be resultingBounds.height
targetBounds.width should be resultingBounds.width
targetBounds.height shouldBe resultingBounds.height
targetBounds.width shouldBe resultingBounds.width

}
}
}
Loading