Skip to content

Commit 25e2f1f

Browse files
committed
Round gridspec tilesize to fix floating point precision issue and related fixes
1 parent ea04613 commit 25e2f1f

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
run: |
7979
conda info
8080
conda list
81-
mamba -V
81+
# mamba -V
8282
conda config --show-sources
8383
conda config --show
8484
printenv | sort

odc/geo/_dask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import partial
2-
from typing import Any, Dict, Optional, Sequence, Tuple, Union
2+
from typing import Any, Dict, Optional, Tuple, Union
33
from uuid import uuid4
44

55
import dask.array as da
@@ -15,7 +15,7 @@
1515

1616

1717
def _do_chunked_reproject(
18-
d2s: Dict[Tuple[int, int], Sequence[Tuple[int, int]]],
18+
d2s: Dict[Tuple[int, int], list[Tuple[int, int]]],
1919
src_gbt: GeoboxTiles,
2020
dst_gbt: GeoboxTiles,
2121
dst_idx: Tuple[int, int],

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)