/
gbox.py
239 lines (189 loc) · 7.58 KB
/
gbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# This file is part of the Open Data Cube, see https://opendatacube.org for more information
#
# Copyright (c) 2015-2020 ODC Contributors
# SPDX-License-Identifier: Apache-2.0
""" Geometric operations on GeoBox class
"""
from typing import Dict, Optional, Tuple, Iterable
import itertools
import math
from affine import Affine
from . import Geometry, GeoBox, BoundingBox
from .tools import align_up
from odc.geo.math import clamp
# pylint: disable=invalid-name
MaybeInt = Optional[int]
MaybeFloat = Optional[float]
def flipy(gbox: GeoBox) -> GeoBox:
"""
:returns: GeoBox covering the same region but with Y-axis flipped
"""
H, W = gbox.shape
A = Affine.translation(0, H)*Affine.scale(1, -1)
A = gbox.affine*A
return GeoBox(W, H, A, gbox.crs)
def flipx(gbox: GeoBox) -> GeoBox:
"""
:returns: GeoBox covering the same region but with X-axis flipped
"""
H, W = gbox.shape
A = Affine.translation(W, 0)*Affine.scale(-1, 1)
A = gbox.affine*A
return GeoBox(W, H, A, gbox.crs)
def translate_pix(gbox: GeoBox, tx: float, ty: float) -> GeoBox:
"""
Shift GeoBox in pixel plane. (0,0) of the new GeoBox will be at the same
location as pixel (tx, ty) in the original GeoBox.
"""
H, W = gbox.shape
A = gbox.affine*Affine.translation(tx, ty)
return GeoBox(W, H, A, gbox.crs)
def pad(gbox: GeoBox, padx: int, pady: MaybeInt = None) -> GeoBox:
"""
Expand GeoBox by fixed number of pixels on each side
"""
pady = padx if pady is None else pady
H, W = gbox.shape
A = gbox.affine*Affine.translation(-padx, -pady)
return GeoBox(W + padx*2, H + pady*2, A, gbox.crs)
def pad_wh(gbox: GeoBox,
alignx: int = 16,
aligny: MaybeInt = None) -> GeoBox:
"""
Expand GeoBox such that width and height are multiples of supplied number.
"""
aligny = alignx if aligny is None else aligny
H, W = gbox.shape
return GeoBox(align_up(W, alignx),
align_up(H, aligny),
gbox.affine, gbox.crs)
def zoom_out(gbox: GeoBox, factor: float) -> GeoBox:
"""
factor > 1 --> smaller width/height, fewer but bigger pixels
factor < 1 --> bigger width/height, more but smaller pixels
:returns: GeoBox covering the same region but with bigger pixels (i.e. lower resolution)
"""
from math import ceil
H, W = (max(1, ceil(s/factor)) for s in gbox.shape)
A = gbox.affine*Affine.scale(factor, factor)
return GeoBox(W, H, A, gbox.crs)
def zoom_to(gbox: GeoBox, shape: Tuple[int, int]) -> GeoBox:
"""
:returns: GeoBox covering the same region but with different number of pixels
and therefore resolution.
"""
H, W = gbox.shape
h, w = shape
sx, sy = W/float(w), H/float(h)
A = gbox.affine*Affine.scale(sx, sy)
return GeoBox(w, h, A, gbox.crs)
def rotate(gbox: GeoBox, deg: float) -> GeoBox:
"""
Rotate GeoBox around the center.
It's as if you stick a needle through the center of the GeoBox footprint
and rotate it counter clock wise by supplied number of degrees.
Note that from pixel point of view image rotates the other way. If you have
source image with an arrow pointing right, and you rotate GeoBox 90 degree,
in that view arrow should point down (this is assuming usual case of inverted
y-axis)
"""
h, w = gbox.shape
c0 = gbox.transform*(w*0.5, h*0.5)
A = Affine.rotation(deg, c0)*gbox.transform
return GeoBox(w, h, A, gbox.crs)
def affine_transform_pix(gbox: GeoBox, transform: Affine) -> GeoBox:
"""
Apply affine transform on pixel side.
:param transform: Affine matrix mapping from new pixel coordinate space to
pixel coordinate space of input gbox
:returns: GeoBox of the same pixel shape but covering different region,
pixels in the output gbox relate to input geobox via `transform`
X_old_pix = transform * X_new_pix
"""
H, W = gbox.shape
A = gbox.affine*transform
return GeoBox(W, H, A, gbox.crs)
class GeoboxTiles():
""" Partition GeoBox into sub geoboxes
"""
def __init__(self, box: GeoBox, tile_shape: Tuple[int, int]):
""" Construct from a ``GeoBox``
:param box: source :class:`datacube.utils.geometry.GeoBox`
:param tile_shape: Shape of sub-tiles in pixels (rows, cols)
"""
self._gbox = box
self._tile_shape = tile_shape
self._shape = tuple(math.ceil(float(N)/n)
for N, n in zip(box.shape, tile_shape))
self._cache: Dict[Tuple[int, int], GeoBox] = {}
@property
def base(self) -> GeoBox:
return self._gbox
@property
def shape(self):
""" Number of tiles along each dimension
"""
return self._shape
def _idx_to_slice(self, idx: Tuple[int, int]) -> Tuple[slice, slice]:
def _slice(i, N, n) -> slice:
_in = i*n
if 0 <= _in < N:
return slice(_in, min(_in + n, N))
else:
raise IndexError("Index ({},{})is out of range".format(*idx))
ir, ic = (_slice(i, N, n)
for i, N, n in zip(idx, self._gbox.shape, self._tile_shape))
return (ir, ic)
def chunk_shape(self, idx: Tuple[int, int]) -> Tuple[int, int]:
""" Chunk shape for a given chunk index.
:param idx: (row, col) index
:returns: (nrow, ncols) shape of a tile (edge tiles might be smaller)
:raises: IndexError when index is outside of [(0,0) -> .shape)
"""
def _sz(i: int, n: int, tile_sz: int, total_sz: int) -> int:
if 0 <= i < n - 1: # not edge tile
return tile_sz
elif i == n - 1: # edge tile
return total_sz - (i*tile_sz)
else: # out of index case
raise IndexError("Index ({},{}) is out of range".format(*idx))
n1, n2 = map(_sz, idx, self._shape, self._tile_shape, self._gbox.shape)
return (n1, n2)
def __getitem__(self, idx: Tuple[int, int]) -> GeoBox:
""" Lookup tile by index, index is in matrix access order: (row, col)
:param idx: (row, col) index
:returns: GeoBox of a tile
:raises: IndexError when index is outside of [(0,0) -> .shape)
"""
sub_gbox = self._cache.get(idx, None)
if sub_gbox is not None:
return sub_gbox
roi = self._idx_to_slice(idx)
return self._cache.setdefault(idx, self._gbox[roi])
def range_from_bbox(self, bbox: BoundingBox) -> Tuple[range, range]:
""" Compute rows and columns overlapping with a given ``BoundingBox``
"""
def clamped_range(v1: float, v2: float, N: int) -> range:
_in = clamp(math.floor(v1), 0, N)
_out = clamp(math.ceil(v2), 0, N)
return range(_in, _out)
sy, sx = self._tile_shape
A = Affine.scale(1.0/sx, 1.0/sy)*(~self._gbox.transform)
# A maps from X,Y in meters to chunk index
bbox = bbox.transform(A)
NY, NX = self.shape
xx = clamped_range(bbox.left, bbox.right, NX)
yy = clamped_range(bbox.bottom, bbox.top, NY)
return (yy, xx)
def tiles(self, polygon: Geometry) -> Iterable[Tuple[int, int]]:
""" Return tile indexes overlapping with a given geometry.
"""
if self._gbox.crs is None:
poly = polygon
else:
poly = polygon.to_crs(self._gbox.crs)
yy, xx = self.range_from_bbox(poly.boundingbox)
for idx in itertools.product(yy, xx):
gbox = self[idx]
if gbox.extent.intersects(poly):
yield idx