Skip to content

Commit

Permalink
Use odc.geo code for geobox anchor
Browse files Browse the repository at this point in the history
GeoBox.anchor is a thing since odc-geo>=0.4.4
  • Loading branch information
Kirill888 committed May 11, 2024
1 parent c0f76c3 commit 9b2a90a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 33 deletions.
19 changes: 2 additions & 17 deletions odc/stac/_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,6 @@ def mk_1x1_geobox(g: Geometry) -> GeoBox:
return GeoBox((1, 1), Affine((x2 - x1), 0, x1, 0, (y1 - y2), y2), g.crs)


def _gbox_anchor(gbox: GeoBox, tol: float = 1e-3) -> GeoboxAnchor:
def _anchor(px: float, tol: float) -> float:
_, x = split_float(px) # x in (-0.5, +0.5)
x = (1 + x) if x < 0 else x # x in [0, 1)
x = snap_scale(maybe_zero(x, tol), tol)
return 0 if x >= 1 else x

anchor = tuple(_anchor(px, tol) for px in (~gbox.transform) * (0, 0))
if anchor == (0, 0):
return AnchorEnum.EDGE
if anchor == (0.5, 0.5):
return AnchorEnum.CENTER
return xy_(anchor)


def asset_geobox(asset: pystac.asset.Asset) -> GeoBox:
"""
Compute GeoBox from STAC Asset.
Expand Down Expand Up @@ -762,10 +747,10 @@ def _most_common_gbox(
_gboxes = set(gboxes)
if len(_gboxes) == 1:
g = _gboxes.pop()
return (g.crs, g.resolution, _gbox_anchor(g), g)
return (g.crs, g.resolution, g.anchor, g)

# Most common shared CRS, Resolution, Anchor
gg = [(g.crs, g.resolution, _gbox_anchor(g)) for g in gboxes]
gg = [(g.crs, g.resolution, g.anchor) for g in gboxes]
hist = Counter(gg)
(best, n), *_ = hist.most_common(1)
if n / len(gg) > thresh:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tests_require =

install_requires =
affine
odc-geo>=0.3.0
odc-geo>=0.4.4
rasterio>=1.0.0,!=1.3.0,!=1.3.1
dask[array]
numpy>=1.20.0
Expand Down
15 changes: 0 additions & 15 deletions tests/test_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from odc.loader.types import FixedCoord, RasterBandMetadata, RasterGroupMetadata
from odc.stac._mdtools import (
_auto_load_params,
_gbox_anchor,
_most_common_gbox,
_normalize_geometry,
asset_geobox,
Expand Down Expand Up @@ -635,20 +634,6 @@ def test_usgs_v1_1_1_aliases(usgs_landsat_stac_v1_1_1: pystac.Item) -> None:
}


@pytest.mark.parametrize(
"gbox",
[
GeoBox.from_bbox((0, 0, 100, 200), resolution=10, crs=3857),
GeoBox.from_bbox((-10, 0, 100, 200), resolution=10, crs=3857),
],
)
def test_gbox_anchor(gbox: GeoBox):
assert _gbox_anchor(gbox) == AnchorEnum.EDGE
assert _gbox_anchor(gbox.translate_pix(-1e-5, 1e-5)) == AnchorEnum.EDGE
assert _gbox_anchor(gbox.translate_pix(0.5, 0.5)) == AnchorEnum.CENTER
assert _gbox_anchor(gbox.translate_pix(-1 / 4, -1 / 8)) == xy_(1 / 4, 1 / 8)


def test_most_common_gbox():
gbox = GeoBox.from_bbox((0, 0, 100, 200), resolution=10, crs=3857)
assert _most_common_gbox(
Expand Down

0 comments on commit 9b2a90a

Please sign in to comment.