Skip to content

Commit 931123c

Browse files
committed
Add test and change force_over to only be used when doing geobox transforms
1 parent 6aa4153 commit 931123c

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

odc/geo/crs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ def _make_crs(
7676
return (crs, crs_str, epsg)
7777

7878

79-
def _make_crs_transform_key(from_crs, to_crs, always_xy):
80-
return (id(from_crs), id(to_crs), always_xy)
79+
def _make_crs_transform_key(from_crs, to_crs, always_xy, force_over=False):
80+
return (id(from_crs), id(to_crs), always_xy, force_over)
8181

8282

8383
@cachetools.cached({}, key=_make_crs_transform_key)
84-
def _make_crs_transform(from_crs: _CRS, to_crs: _CRS, always_xy: bool) -> Transformer:
84+
def _make_crs_transform(from_crs: _CRS, to_crs: _CRS, always_xy: bool, force_over: bool = False) -> Transformer:
8585
return Transformer.from_crs(from_crs, to_crs, always_xy=always_xy, force_over=force_over)
8686

8787

@@ -299,7 +299,7 @@ def crs_str(self) -> str:
299299
return self._str
300300

301301
def transformer_to_crs(
302-
self, other: "CRS", always_xy: bool = True
302+
self, other: "CRS", always_xy: bool = True, force_over: bool = False
303303
) -> Callable[[Any, Any], Tuple[Any, Any]]:
304304
"""
305305
Build coordinate transformer to other projection.
@@ -318,7 +318,7 @@ def transformer_to_crs(
318318
"""
319319

320320
# pylint: disable=protected-access
321-
tr = _make_crs_transform(self._crs, other._crs, always_xy=always_xy)
321+
tr = _make_crs_transform(self._crs, other._crs, always_xy=always_xy, force_over=force_over)
322322

323323
def result(x, y, **kw):
324324
rx, ry = tr.transform(x, y, **kw) # pylint: disable=unpacking-non-sequence

odc/geo/overlap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(
103103
self._src = src
104104
self._dst = dst
105105
self._back = back
106-
self._tr = src.crs.transformer_to_crs(dst.crs)
106+
self._tr = src.crs.transformer_to_crs(dst.crs, force_over=True)
107107
self._clamps: Optional[Tuple[Tuple[float, float], Tuple[float, float]]] = None
108108
if src.crs.geographic:
109109
self._clamps = ((-180, 180), (-90, 90))

tests/test_geom.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,53 @@ def test_gen_test_image_xy():
522522
assert isinstance(A, Affine)
523523

524524

525+
def test_geobox_overlap():
526+
from odc.geo.types import xy_
527+
from odc.geo.overlap import (
528+
roi_boundary,
529+
unstack_xy,
530+
stack_xy,
531+
gbox_boundary,
532+
roi_from_points,
533+
native_pix_transform,
534+
)
535+
536+
pts_per_side = 5
537+
padding = 1
538+
align = True
539+
540+
dst_affine = Affine(
541+
152.87405657034833,
542+
0.0,
543+
-20037508.342789244,
544+
0.0,
545+
-152.87405657034833,
546+
-1995923.6825825237,
547+
)
548+
dst = GeoBox((256, 256), dst_affine, "EPSG:3857")
549+
550+
src_affine = Affine(10.0, 0.0, 99960.0, 0.0, -10.0, 8100040.0)
551+
src = GeoBox((10980, 10980), src_affine, "EPSG:32701")
552+
553+
tr = native_pix_transform(src, dst)
554+
555+
xy = tr.back(unstack_xy(gbox_boundary(dst, pts_per_side)))
556+
roi_src = roi_from_points(stack_xy(xy), src.shape, padding, align=align)
557+
558+
xy_pix_src = unstack_xy(roi_boundary(roi_src, pts_per_side))
559+
560+
xx, yy = np.asarray([pt.xy for pt in xy_pix_src]).T
561+
562+
# This goes via world transform to a pixel space
563+
xys = tr([xy_(x, y) for x, y in zip(xx, yy)])
564+
565+
# Results should be within a resonable range in pixel space
566+
# Not sure how to test it better.
567+
for xy in xys:
568+
assert xy.x >= 0 - 25.6
569+
assert xy.y <= 256 + 25.6
570+
571+
525572
def test_fixed_point():
526573
aa = np.asarray([0, 0.5, 1])
527574
uu = to_fixed_point(aa, "uint8")

0 commit comments

Comments
 (0)