Skip to content

Commit

Permalink
Remove some more plain tuples from the interface
Browse files Browse the repository at this point in the history
geobox/gridspec use Shape2d now
  • Loading branch information
Kirill888 committed Feb 8, 2022
1 parent 4f242f0 commit 1a17224
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
23 changes: 13 additions & 10 deletions odc/geo/geobox.py
Expand Up @@ -25,12 +25,14 @@
Index2d,
MaybeInt,
Resolution,
Shape2d,
SomeIndex2d,
SomeResolution,
SomeShape,
iyx_,
res_,
resxy_,
shape_,
xy_,
yx_,
)
Expand Down Expand Up @@ -64,7 +66,7 @@ def __init__(self, shape: SomeShape, affine: Affine, crs: MaybeCRS):
assert is_affine_st(
affine
), "Only axis-aligned geoboxes are currently supported"
shape = yx_(shape)
shape = shape_(shape)

self._shape = shape
self.affine = affine
Expand Down Expand Up @@ -146,7 +148,7 @@ def __getitem__(self, roi) -> "GeoBox":
if not all(s.step is None or s.step == 1 for s in roi):
raise NotImplementedError("scaling not implemented, yet")

roi = roi_normalise(roi, self.shape)
roi = roi_normalise(roi, self._shape.shape)
ty, tx = (s.start for s in roi)
ny, nx = roi_shape(roi)

Expand Down Expand Up @@ -188,9 +190,9 @@ def height(self) -> int:
return self._shape.y

@property
def shape(self) -> Tuple[int, int]:
def shape(self) -> Shape2d:
"""Shape in pixels ``(height, width)``."""
return self._shape.shape
return self._shape

@property
def crs(self) -> Optional[CRS]:
Expand Down Expand Up @@ -278,7 +280,7 @@ def gbox_boundary(gbox: GeoBox, pts_per_side: int = 16) -> numpy.ndarray:
:return:
Points in pixel space along the perimeter of a GeoBox as a 2d array.
"""
ny, nx = gbox.shape[:2]
ny, nx = gbox.shape
xx = numpy.linspace(0, nx, pts_per_side, dtype="float32")
yy = numpy.linspace(0, ny, pts_per_side, dtype="float32")

Expand Down Expand Up @@ -572,7 +574,7 @@ def _slice(i, N, n) -> slice:
)
return (ir, ic)

def chunk_shape(self, idx: SomeIndex2d) -> Tuple[int, int]:
def chunk_shape(self, idx: SomeIndex2d) -> Shape2d:
"""
Query chunk shape for a given chunk.
Expand All @@ -591,14 +593,15 @@ def _sz(i: int, n: int, tile_sz: int, total_sz: int) -> int:
raise IndexError(f"Index {idx} is out of range")

n1, n2 = map(_sz, idx.yx, self._shape.yx, self._tile_shape.yx, self._gbox.shape)
return (n1, n2)
return shape_((n1, n2))

def __getitem__(self, idx: SomeIndex2d) -> GeoBox:
"""Lookup tile by index, index is in matrix access order: (row, col)
"""
Lookup tile by index, index is in matrix access order: ``(row, col)``.
:param idx: (row, col) index
:param idx: ``(row, col)`` index
:returns: GeoBox of a tile
:raises: IndexError when index is outside of [(0,0) -> .shape)
:raises: IndexError when index is outside of ``[(0,0) -> .shape)``
"""
idx = iyx_(idx)
sub_gbox = self._cache.get(idx, None)
Expand Down
20 changes: 11 additions & 9 deletions odc/geo/gridspec.py
Expand Up @@ -16,12 +16,14 @@
from .types import (
XY,
Index2d,
Shape2d,
SomeIndex2d,
SomeResolution,
SomeShape,
ixy_,
res_,
resyx_,
shape_,
xy_,
yx_,
)
Expand Down Expand Up @@ -53,16 +55,14 @@ def __init__(
flipx: bool = False,
flipy: bool = False,
):
tile_shape = shape_(tile_shape)
resolution = res_(resolution)

if origin is None:
origin = xy_(0.0, 0.0)
else:
assert isinstance(origin, XY)

if isinstance(tile_shape, tuple):
tile_shape = yx_(tile_shape)

self.crs = norm_crs_or_error(crs)
self._shape = tile_shape
self.resolution = resolution
Expand Down Expand Up @@ -101,9 +101,9 @@ def alignment(self) -> XY[float]:
return yx_(y, x)

@property
def tile_shape(self) -> Tuple[int, int]:
def tile_shape(self) -> Shape2d:
"""Tile shape in pixels (Y,X order, like numpy)."""
return self._shape.shape
return self._shape

def pt2idx(self, x: float, y: float) -> Index2d:
"""
Expand Down Expand Up @@ -272,8 +272,8 @@ def geojson(
def from_sample_tile(
box: Geometry,
*,
shape: Tuple[int, int] = (-1, -1),
idx: Tuple[int, int] = (0, 0),
shape: SomeShape = (-1, -1),
idx: SomeIndex2d = (0, 0),
flipx: bool = False,
flipy: bool = False,
) -> "GridSpec":
Expand All @@ -291,10 +291,12 @@ def from_sample_tile(
"""
if shape == (-1, -1):
raise ValueError("Must specify shape of the tile in pixels")
shape = shape_(shape)
idx = ixy_(idx)

crs = norm_crs_or_error(box.crs)
ix, iy = idx
ny, nx = shape
ix, iy = idx.xy
nx, ny = shape.xy
bbox = box.boundingbox

xbin = Bin1D.from_sample_bin(ix, bbox.range_x, -1 if flipx else 1)
Expand Down

0 comments on commit 1a17224

Please sign in to comment.