Skip to content

Commit ff8f323

Browse files
committed
Round gridspec tilesize to fix floating point precision issue
1 parent 84650c4 commit ff8f323

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

odc/geo/gridspec.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ def __init__(
6666
self.crs = norm_crs_or_error(crs)
6767
self._shape = tile_shape
6868
self.resolution = resolution
69+
70+
# This is an arbitrary rounding of 12 decimal places
71+
# I don't know how to make it more robust
6972
self.tile_size = xy_(
70-
tile_shape.x * abs(resolution.x),
71-
tile_shape.y * abs(resolution.y),
73+
round(tile_shape.x * abs(resolution.x), 12),
74+
round(tile_shape.y * abs(resolution.y), 12),
7275
)
7376
self.origin = origin
7477

tests/test_gridspec.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010
from pytest import approx
1111

12-
from odc.geo import CRS, res_, resyx_, xy_, yx_
12+
from odc.geo import CRS, res_, resyx_, xy_, yx_, XY
1313
from odc.geo.geom import polygon
1414
from odc.geo.gridspec import GridSpec
1515
from odc.geo.testutils import SAMPLE_WKT_WITHOUT_AUTHORITY
@@ -18,6 +18,36 @@
1818
# pylint: disable=comparison-with-itself,unnecessary-comprehension
1919

2020

21+
def test_gridspec_small():
22+
print("Starting test for GridSpec")
23+
WGS84GRID30 = GridSpec(
24+
"EPSG:4326", tile_shape=(5000, 5000), resolution=0.0003, origin=XY(-180, -90)
25+
)
26+
27+
assert WGS84GRID30.tile_shape == (5000, 5000)
28+
assert WGS84GRID30.tile_size == XY(1.5, 1.5)
29+
30+
# Tile is at (-180 + 50*1.5) and (-90 + 50*1.5)
31+
tile = (50, 50)
32+
geobox = WGS84GRID30.tile_geobox(tile)
33+
affine = geobox.affine
34+
35+
# Affine should be like this: (0.0003, 0, -105.0, 0, -0.0003, -13.5)
36+
assert affine.a == 0.0003
37+
assert affine.c == -105.0 # -180 + 50 * 1.5 * 0.0003
38+
assert affine.f == -13.50 # -90 + 50 * 1.5 * 0.0003
39+
40+
# Tile is at (-180 + 200*1.5) and (-90 + 75*1.5)
41+
tile = (200, 75)
42+
geobox = WGS84GRID30.tile_geobox(tile)
43+
affine = geobox.affine
44+
45+
# Affine should be like this: (0.0003, 0, 120.0, 0, -0.0003, 24.0)
46+
assert affine.a == 0.0003
47+
assert affine.c == 120.0 # -180 + 200 * 1.5 * 0.0003
48+
assert affine.f == 24.0 # -90 + 75 * 1.5 * 0.0003
49+
50+
2151
def test_gridspec():
2252
gs = GridSpec(
2353
crs=CRS("EPSG:4326"),

0 commit comments

Comments
 (0)