Skip to content

Commit

Permalink
Round gridspec tilesize to fix floating point precision issue and rel…
Browse files Browse the repository at this point in the history
…ated fixes
  • Loading branch information
alexgleith committed Oct 1, 2024
1 parent ea04613 commit 25e2f1f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
run: |
conda info
conda list
mamba -V
# mamba -V
conda config --show-sources
conda config --show
printenv | sort
Expand Down
4 changes: 2 additions & 2 deletions odc/geo/_dask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import partial
from typing import Any, Dict, Optional, Sequence, Tuple, Union
from typing import Any, Dict, Optional, Tuple, Union
from uuid import uuid4

import dask.array as da
Expand All @@ -15,7 +15,7 @@


def _do_chunked_reproject(
d2s: Dict[Tuple[int, int], Sequence[Tuple[int, int]]],
d2s: Dict[Tuple[int, int], list[Tuple[int, int]]],
src_gbt: GeoboxTiles,
dst_gbt: GeoboxTiles,
dst_idx: Tuple[int, int],
Expand Down
7 changes: 5 additions & 2 deletions odc/geo/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ def __init__(
self.crs = norm_crs_or_error(crs)
self._shape = tile_shape
self.resolution = resolution

# This is an arbitrary rounding of 12 decimal places
# I don't know how to make it more robust
self.tile_size = xy_(
tile_shape.x * abs(resolution.x),
tile_shape.y * abs(resolution.y),
round(tile_shape.x * abs(resolution.x), 12),
round(tile_shape.y * abs(resolution.y), 12),
)
self.origin = origin

Expand Down
32 changes: 31 additions & 1 deletion tests/test_gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from pytest import approx

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


def test_gridspec_small():
print("Starting test for GridSpec")
WGS84GRID30 = GridSpec(
"EPSG:4326", tile_shape=(5000, 5000), resolution=0.0003, origin=XY(-180, -90)
)

assert WGS84GRID30.tile_shape == (5000, 5000)
assert WGS84GRID30.tile_size == XY(1.5, 1.5)

# Tile is at (-180 + 50*1.5) and (-90 + 50*1.5)
tile = (50, 50)
geobox = WGS84GRID30.tile_geobox(tile)
affine = geobox.affine

# Affine should be like this: (0.0003, 0, -105.0, 0, -0.0003, -13.5)
assert affine.a == 0.0003
assert affine.c == -105.0 # -180 + 50 * 1.5 * 0.0003
assert affine.f == -13.50 # -90 + 50 * 1.5 * 0.0003

# Tile is at (-180 + 200*1.5) and (-90 + 75*1.5)
tile = (200, 75)
geobox = WGS84GRID30.tile_geobox(tile)
affine = geobox.affine

# Affine should be like this: (0.0003, 0, 120.0, 0, -0.0003, 24.0)
assert affine.a == 0.0003
assert affine.c == 120.0 # -180 + 200 * 1.5 * 0.0003
assert affine.f == 24.0 # -90 + 75 * 1.5 * 0.0003


def test_gridspec():
gs = GridSpec(
crs=CRS("EPSG:4326"),
Expand Down

0 comments on commit 25e2f1f

Please sign in to comment.