Skip to content

Commit 221fd8c

Browse files
committed
Round gridspec tilesize to fix floating point precision issue
1 parent ea04613 commit 221fd8c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,41 @@
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
1616

1717
# pylint: disable=protected-access,use-implicit-booleaness-not-comparison
1818
# pylint: disable=comparison-with-itself,unnecessary-comprehension
1919

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

2148
def test_gridspec():
2249
gs = GridSpec(

0 commit comments

Comments
 (0)