-
Notifications
You must be signed in to change notification settings - Fork 18
/
geobox.py
1437 lines (1130 loc) · 44.2 KB
/
geobox.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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
import importlib
import itertools
import math
from collections import OrderedDict, namedtuple
from enum import Enum
from typing import (
Callable,
Dict,
Iterator,
List,
Literal,
Mapping,
Optional,
Sequence,
Tuple,
Union,
)
import numpy
from affine import Affine
from . import geom
from .crs import CRS, MaybeCRS, SomeCRS, norm_crs
from .geom import BoundingBox, Geometry, bbox_intersection, bbox_union
from .math import (
clamp,
is_affine_st,
is_almost_int,
maybe_zero,
resolution_from_affine,
snap_affine,
snap_grid,
split_translation,
)
from .roi import (
RoiTiles,
align_up,
clip_tiles,
roi_boundary,
roi_normalise,
roi_shape,
roi_tiles,
)
from .types import (
ROI,
XY,
Chunks2d,
MaybeInt,
NormalizedROI,
OutlineMode,
Resolution,
Shape2d,
SomeIndex2d,
SomeResolution,
SomeShape,
Unset,
func2map,
res_,
shape_,
xy_,
)
class AnchorEnum(Enum):
"""
Defines which way to snap geobox pixel grid.
"""
EDGE = 0
"""Snap pixel edges to multiples of pixel size."""
CENTER = 1
"""Snap pixel centers to multiples of pixel size."""
FLOATING = 2
"""Turn off pixel snapping."""
GeoboxAnchor = Union[AnchorEnum, XY[float]]
# pylint: disable=invalid-name,too-many-public-methods,too-many-lines
Coordinate = namedtuple("Coordinate", ("values", "units", "resolution"))
class GeoBoxBase:
"""
Defines the location and resolution of a rectangular grid of data,
including it's :py:class:`~odc.geo.crs.CRS`.
:param shape: Shape in pixels ``(ny, nx)``
:param crs: Coordinate Reference System
:param affine: Affine transformation defining the location of the geobox
"""
__slots__ = ("_shape", "_affine", "_crs", "_extent", "_lazy_ui")
def __init__(self, shape: SomeShape, affine: Affine, crs: MaybeCRS):
shape = shape_(shape)
self._shape = shape
self._affine = affine
self._crs = norm_crs(crs)
self._extent: Optional[Geometry] = None
self._lazy_ui = None
@property
def width(self) -> int:
"""Width in pixels (nx)."""
return self._shape.x
@property
def height(self) -> int:
"""Height in pixels (ny)."""
return self._shape.y
@property
def shape(self) -> Shape2d:
"""Shape in pixels ``(height, width)``."""
return self._shape
@property
def aspect(self) -> float:
"""Aspect ratio (X/Y in pixel space)."""
return self._shape.aspect
@property
def crs(self) -> Optional[CRS]:
"""Coordinate Reference System of the GeoBox."""
return self._crs
@property
def dimensions(self) -> Tuple[str, str]:
"""List of dimension names of the GeoBox."""
crs = self._crs
if crs is None:
return ("y", "x")
return crs.dimensions
dims = dimensions
def is_empty(self) -> bool:
"""Check if geobox is "empty"."""
return 0 in self._shape
def __bool__(self) -> bool:
return not self.is_empty()
@property
def resolution(self) -> Resolution:
"""Resolution, pixel size in CRS units."""
return resolution_from_affine(self._affine)
def boundary(self, pts_per_side: int = 16) -> numpy.ndarray:
"""
Boundary of a :py:class:`~odc.geo.geobox.GeoBox`.
Construct a ring of points in pixel space along the edge of the geobox.
:param pts_per_side: Number of points per side, default is 16.
:return:
Points in pixel space along the perimeter of a GeoBox as a ``Nx2`` array
in pixel coordinates.
"""
ny, nx = self._shape.yx
return roi_boundary(numpy.s_[0:ny, 0:nx], pts_per_side)
@property
def alignment(self) -> XY[float]:
"""
Alignment of pixel boundaries in CRS units.
This is usally ``(0,0)``.
"""
rx, _, tx, _, ry, ty, *_ = self._affine
return xy_(tx % abs(rx), ty % abs(ry))
@property
def linear(self) -> bool:
return True
def wld2pix(self, x, y):
return (~self._affine) * (x, y)
def pix2wld(self, x, y):
return self._affine * (x, y)
@property
def extent(self) -> Geometry:
"""GeoBox footprint in native CRS."""
if self._extent is not None:
return self._extent
if self.linear:
_extent = geom.polygon_from_transform(self._shape, self._affine, self._crs)
else:
_extent = geom.polygon(self.boundary(16).tolist(), self._crs).transform(
self.pix2wld
)
self._extent = _extent
return _extent
@property
def boundingbox(self) -> BoundingBox:
"""GeoBox bounding box in the native CRS."""
return BoundingBox.from_transform(self._shape, self._affine, crs=self._crs)
def _reproject_resolution(self, npoints: int = 100):
bbox = self.extent.boundingbox
span = max(bbox.span_x, bbox.span_y)
return span / npoints
def footprint(
self, crs: SomeCRS, buffer: float = 0, npoints: int = 100
) -> Geometry:
"""
Compute footprint in foreign CRS.
:param crs: CRS of the destination
:param buffer: amount to buffer in source pixels before transforming
:param npoints: number of points per-side to use, higher number
is slower but more accurate
"""
assert self.crs is not None
ext = self.extent
if buffer != 0:
buffer = buffer * max(*self.resolution.xy)
ext = ext.buffer(buffer)
return ext.to_crs(crs, resolution=self._reproject_resolution(npoints)).dropna()
@property
def geographic_extent(self) -> Geometry:
"""GeoBox extent in EPSG:4326."""
if self._crs is None or self._crs.geographic:
return self.extent
return self.footprint("epsg:4326")
@property
def _ui(self):
# pylint: disable=import-outside-toplevel
from .ui import PixelGridDisplay
if self._lazy_ui is not None:
return self._lazy_ui
gsd = max(*self.resolution.map(abs).xy)
self._lazy_ui = PixelGridDisplay(self, self.pix2wld, gsd)
return self._lazy_ui
def svg(
self,
scale_factor: float = 1.0,
mode: OutlineMode = "auto",
notch: float = 0.0,
grid_stroke: str = "pink",
) -> str:
"""
Produce SVG paths.
:param mode: One of pixel, native, geo (default is geo)
:return: SVG path
"""
return self._ui.svg(
scale_factor=scale_factor, mode=mode, notch=notch, grid_stroke=grid_stroke
)
def grid_lines(self, step: int = 0, mode: OutlineMode = "native") -> Geometry:
"""
Construct pixel edge aligned grid lines.
"""
return self._ui.grid_lines(step=step, mode=mode)
def outline(self, mode: OutlineMode = "native", notch: float = 0.1) -> Geometry:
return self._ui.outline(mode, notch=notch)
def _repr_svg_(self):
# pylint: disable=protected-access
return self._ui._render_svg()
def _repr_html_(self):
# pylint: disable=protected-access
return self._ui._repr_html_()
def compute_crop(self, roi) -> Tuple[Shape2d, Affine]:
if isinstance(roi, BoundingBox):
roi = roi.polygon
if isinstance(roi, GeoBoxBase):
roi = roi.extent
if isinstance(roi, Geometry):
if roi.crs is not None:
roi = self.project(roi)
pix_bbox = roi.boundingbox.round() & BoundingBox(
0, 0, self.width, self.height
)
nx, ny = (max(1, int(span)) for span in (pix_bbox.span_x, pix_bbox.span_y))
tx, ty = map(int, pix_bbox.bbox[:2])
roi = numpy.s_[ty : ty + ny, tx : tx + nx]
if isinstance(roi, int):
roi = (slice(roi, roi + 1), slice(None, None))
if isinstance(roi, slice):
roi = (roi, slice(None, None))
if len(roi) > 2:
raise ValueError("Expect 2d slice")
roi = roi_normalise(roi, self._shape.shape)
if not all(s.step is None or s.step == 1 for s in roi):
raise NotImplementedError("scaling not implemented, yet")
ty, tx = (s.start for s in roi)
ny, nx = roi_shape(roi)
affine = self._affine * Affine.translation(tx, ty)
return shape_((ny, nx)), affine
def compute_zoom_out(self, factor: float) -> Tuple[Shape2d, Affine]:
ny, nx = (max(1, math.ceil(s / factor)) for s in self.shape)
A = self._affine * Affine.scale(factor, factor)
return (shape_((ny, nx)), A)
def compute_zoom_to(
self,
shape: Union[SomeShape, int, float, None] = None,
*,
resolution: Optional[SomeResolution] = None,
) -> Tuple[Shape2d, Affine]:
"""
Change GeoBox shape.
When supplied a single integer scale longest dimension to match that.
:returns:
GeoBox covering the same region but with different number of pixels and therefore resolution.
"""
if shape is None:
if resolution is None:
raise ValueError("Have to supply shape or resolution")
new_geobox = GeoBox.from_bbox(
self.boundingbox, resolution=resolution, tight=True
)
return new_geobox.shape, new_geobox.affine
if isinstance(shape, (int, float)):
nmax = max(*self._shape)
return self.compute_zoom_out(nmax / shape)
shape = shape_(shape)
sy, sx = (N / float(n) for N, n in zip(self._shape, shape.shape))
A = self._affine * Affine.scale(sx, sy)
return (shape, A)
def project(self, g: Geometry) -> Geometry:
"""
Map Geometry between world and pixel coords.
When input geometry has no CRS, map from pixels to the world.
When input geometry has CRS (can be different from GeoBox), project
geometry into pixel coordinates, note that result is not clipped to the
image bounds.
"""
if g.crs is None: # assume pixel plane
g = g.transform(self.pix2wld)
return Geometry(g.geom, self._crs)
assert self._crs is not None
if g.crs != self._crs:
g = g.to_crs(self._crs)
g = g.transform(self.wld2pix)
return Geometry(g.geom, crs=None)
def qr2sample(
self,
n: int,
padding: Optional[float] = None,
with_edges: bool = False,
offset: int = 0,
) -> Geometry:
"""
Generate quasi-random sample of image locations.
:param n:
Number of points
:param padding:
In pixels, minimal distance from the edge
:param offset:
Offset into quasi-random sequence from where to start
:param edges:
Also include samples along the edge and corners
References: http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/
"""
nx, ny = self._shape.xy
return BoundingBox(0, 0, nx, ny, None).qr2sample(
n, padding=padding, with_edges=with_edges, offset=offset
)
def __getitem__(self, roi) -> "GeoBoxBase":
raise NotImplementedError()
class GeoBox(GeoBoxBase):
"""
Defines the location and resolution of a rectangular grid of data,
including it's :py:class:`~odc.geo.crs.CRS`.
:param shape: Shape in pixels ``(ny, nx)``
:param crs: Coordinate Reference System
:param affine: Affine transformation defining the location of the geobox
"""
__slots__ = ()
def __init__(self, shape: SomeShape, affine: Affine, crs: MaybeCRS):
GeoBoxBase.__init__(self, shape, affine, crs)
@staticmethod
def from_bbox(
bbox: Union[BoundingBox, Tuple[float, float, float, float]],
crs: MaybeCRS = None,
*,
tight: bool = False,
shape: Union[SomeShape, int, None] = None,
resolution: Optional[SomeResolution] = None,
anchor: GeoboxAnchor = AnchorEnum.EDGE,
tol: float = 0.01,
) -> "GeoBox":
"""
Construct :py:class:`~odc.geo.geobox.GeoBox` from a bounding box.
:param bbox: Bounding box in CRS units, lonlat is assumed when ``crs`` is not supplied
:param crs: CRS of the bounding box (defaults to EPSG:4326)
:param shape:
Span that many pixels, if it's a single number then span that many pixels along the
longest dimension, other dimension will be computed to maintain roughly square pixels.
:param resolution: Use specified resolution
:param tight: Supplying ``tight=True`` turns off pixel snapping.
:param anchor:
By default snaps grid such that pixel edges fall on X/Y axis. Ignored when tight mode is
used.
:param tol:
Fraction of a pixel that can be ignored, defaults to 1/100. Bounding box of the output
geobox is allowed to be smaller than supplied bounding box by that amount.
:return:
:py:class:`~odc.geo.geobox.GeoBox` that covers supplied bounding box.
"""
# pylint: disable=too-many-locals, too-many-branches
_snap: Optional[XY[float]] = None
if tight:
anchor = AnchorEnum.FLOATING
if isinstance(anchor, XY):
_snap = anchor
if anchor == AnchorEnum.EDGE:
_snap = xy_(0, 0)
elif anchor == AnchorEnum.CENTER:
_snap = xy_(0.5, 0.5)
def _norm_bbox(
bbox: Tuple[float, float, float, float], crs: MaybeCRS
) -> BoundingBox:
if isinstance(crs, str):
if crs.lower().startswith("utm"):
return BoundingBox(*bbox, crs="epsg:4326").to_crs(crs)
return BoundingBox(*bbox, crs=(crs or "epsg:4326"))
if not isinstance(bbox, BoundingBox):
bbox = _norm_bbox(bbox, crs)
elif bbox.crs is None:
bbox = _norm_bbox(bbox.bbox, crs)
if isinstance(shape, (int, float)):
if bbox.aspect > 1:
resolution = bbox.span_x / shape
else:
resolution = bbox.span_y / shape
shape = None
if resolution is not None:
rx, ry = res_(resolution).xy
if _snap is None:
offx, nx = snap_grid(bbox.left, bbox.right, rx, None, tol=tol)
offy, ny = snap_grid(bbox.bottom, bbox.top, ry, None, tol=tol)
else:
offx, nx = snap_grid(bbox.left, bbox.right, rx, _snap.x, tol=tol)
offy, ny = snap_grid(bbox.bottom, bbox.top, ry, _snap.y, tol=tol)
affine = Affine.translation(offx, offy) * Affine.scale(rx, ry)
return GeoBox((ny, nx), crs=bbox.crs, affine=affine)
if shape is None:
raise ValueError("Must supply shape or resolution")
shape = shape_(shape)
nx, ny = shape.wh
rx = bbox.span_x / nx
ry = -bbox.span_y / ny
if _snap is None:
offx, offy = bbox.left, bbox.top
else:
offx, _ = snap_grid(bbox.left, bbox.right, rx, _snap.x, tol=tol)
offy, _ = snap_grid(bbox.bottom, bbox.top, ry, _snap.y, tol=tol)
affine = Affine.translation(offx, offy) * Affine.scale(rx, ry)
return GeoBox((ny, nx), crs=bbox.crs, affine=affine)
@staticmethod
def from_geopolygon(
geopolygon: Geometry,
resolution: Optional[SomeResolution] = None,
crs: MaybeCRS = None,
align: Optional[XY[float]] = None,
*,
shape: Union[SomeShape, int, None] = None,
tight: bool = False,
anchor: GeoboxAnchor = AnchorEnum.EDGE,
tol: float = 0.01,
) -> "GeoBox":
"""
Construct :py:class:`~odc.geo.geobox.GeoBox` from a polygon.
:param resolution:
Either a single number or a :py:class:`~odc.geo.types.Resolution` object.
:param shape:
Span that many pixels, if it's a single number then span that many pixels along the
longest dimension, other dimension will be computed to maintain roughly square pixels.
:param crs:
CRS to use, if different from the geopolygon
:param align:
Deprecated: please switch to ``anchor=``
:param anchor:
By default snaps grid such that pixel edges fall on X/Y axis.
:param tol:
Fraction of a pixel that can be ignored, defaults to 1/100. Bounding box of the output
geobox is allowed to be smaller than supplied bounding box by that amount.
:param tight: Supplying ``tight=True`` turns off pixel snapping.
"""
if align is not None:
# support old-style "align", which is basically anchor but in CRS units
ax, ay = align.xy
if ax == 0 and ay == 0:
anchor = AnchorEnum.EDGE
else:
assert resolution is not None
resolution = res_(resolution)
anchor = xy_(ax / abs(resolution.x), ay / abs(resolution.y))
if crs is None or isinstance(crs, Unset):
crs = geopolygon.crs
else:
geopolygon = geopolygon.to_crs(crs)
return GeoBox.from_bbox(
geopolygon.boundingbox,
crs,
shape=shape,
resolution=resolution,
anchor=anchor,
tol=tol,
tight=tight,
)
@staticmethod
def from_rio(rdr) -> "GeoBox":
"""
Construct GeoBox from rasterio.
:param rdr: Openned :py:class:`rasterio.DatasetReader`
:returns:
:py:class:`~odc.geo.geobox.GeoBox`
"""
return GeoBox(rdr.shape, rdr.transform, rdr.crs)
def buffered(self, xbuff: float, ybuff: Optional[float] = None) -> "GeoBox":
"""
Produce a tile buffered by ``xbuff, ybuff`` (in CRS units).
"""
if ybuff is None:
ybuff = xbuff
by, bx = (
_round_to_res(buf, res)
for buf, res in zip((ybuff, xbuff), self.resolution.yx)
)
affine = self._affine * Affine.translation(-bx, -by)
ny, nx = (sz + 2 * b for sz, b in zip(self._shape, (by, bx)))
return GeoBox(
(ny, nx),
affine=affine,
crs=self._crs,
)
def enclosing(self, region: Union[Geometry, BoundingBox]) -> "GeoBox":
"""
Construct compatible geobox covering given ``region``.
Output GeoBox shares exactly the same pixel grid as source, but has
different shape and location in the world.
:param region: Region to be covered by the new GeoBox.
"""
if isinstance(region, BoundingBox):
region = region.polygon
if region.crs is None:
raise ValueError("Must supply geo-resgistered region")
pix_bbox = self.project(region).boundingbox.round()
nx, ny = (max(1, int(span)) for span in (pix_bbox.span_x, pix_bbox.span_y))
tx, ty, *_ = pix_bbox.bbox
A = self.translate_pix(tx, ty).affine
return GeoBox(shape_((ny, nx)), A, self._crs)
def __getitem__(self, roi) -> "GeoBox":
_shape, _affine = self.compute_crop(roi)
return GeoBox(shape=_shape, affine=_affine, crs=self._crs)
def __or__(self, other) -> "GeoBox":
"""A geobox that encompasses both self and other."""
return geobox_union_conservative([self, other])
def __and__(self, other) -> "GeoBox":
"""A geobox that is contained in both self and other."""
return geobox_intersection_conservative([self, other])
def __hash__(self):
return hash((*self._shape, self._crs, self._affine))
def overlap_roi(self, other: "GeoBox", tol: float = 1e-8) -> NormalizedROI:
"""
Compute overlap as ROI.
Figure out slice into this geobox that shares pixels with the ``other`` geobox with
consistent pixel grid.
:raises:
:py:class:`ValueError` when two geoboxes are not pixel-aligned.
"""
nx, ny = self._shape.xy
x0, y0, x1, y1 = map(int, bounding_box_in_pixel_domain(other, self, tol))
x0, y0 = max(0, x0), max(0, y0)
x1, y1 = min(x1, nx), min(y1, ny)
return numpy.s_[y0:y1, x0:x1]
@property
def transform(self) -> Affine:
"""Linear mapping from pixel space to CRS."""
return self._affine
@property
def affine(self) -> Affine:
"""
Linear mapping from pixel space to CRS.
alias for :py:attr:`~odc.geo.geobox.GeoBox.transform`
"""
return self._affine
@property
def coordinates(self) -> Dict[str, Coordinate]:
"""
Query coordinates.
This method only works with axis-aligned boxes. It will raise :py:class:`ValueError` if called
on non-axis aligned :py:class:`~odc.geo.geobox.GeoBox`.
:raises: :py:class:`ValueError` if not axis aligned.
:return:
Mapping from coordinate name to :py:class:`~odc.geo.geobox.Coordinate`.
"""
self._confirm_axis_aligned("Only axis aligned GeoBox can do this.")
rx, _, tx, _, ry, ty, *_ = self._affine
ny, nx = self._shape
xs = numpy.arange(nx) * rx + (tx + rx / 2)
ys = numpy.arange(ny) * ry + (ty + ry / 2)
crs_units = self._crs.units if self._crs is not None else ("1", "1")
return OrderedDict(
(dim, Coordinate(labels, units, res))
for dim, labels, units, res in zip(
self.dimensions, (ys, xs), crs_units, (ry, rx)
)
)
coords = coordinates
def map_bounds(self) -> Tuple[Tuple[float, float], Tuple[float, float]]:
"""
Query bounds in folium/ipyleaflet style.
Returns SW, and NE corners in lat/lon order.
``((lat_w, lon_s), (lat_e, lon_n))``.
"""
if self._crs is not None:
(x0, y0), _, (x1, y1) = self.extent.exterior.to_crs("epsg:4326").points[:3]
else:
(x0, y0), _, (x1, y1) = self.extent.exterior.points[:3]
return (y0, x0), (y1, x1)
def to_crs(
self,
crs: SomeCRS,
*,
resolution: Literal["auto", "fit", "same"] = "auto",
tight: bool = False,
round_resolution: Union[None, bool, Callable[[float, str], float]] = None,
) -> "GeoBox":
"""
Compute GeoBox covering the same region in a different projection.
:param crs:
Desired CRS of the output
:param resolution:
* "same" use exactly the same resolution as src
* "fit" use center pixel to determine scale change between the two
* | "auto" is to use the same resolution on the output if CRS units are the same
| between the source and destination and otherwise use "fit"
:param tight:
By default output pixel grid is adjusted to align pixel edges to X/Y axis, suppling
``tight=True`` produces unaligned geobox on the output.
:return:
Similar resolution, axis aligned geobox that fully encloses this one but in a different
projection.
"""
# pylint: disable=import-outside-toplevel
# can't be up-top due to circular imports issues
from .overlap import compute_output_geobox
return compute_output_geobox(
self,
crs,
resolution=resolution,
tight=tight,
round_resolution=round_resolution,
)
def __str__(self):
return self.__repr__()
def __repr__(self):
return f"GeoBox({self._shape.yx!r}, {self._affine!r}, {self._crs!r})"
def __eq__(self, other):
if not isinstance(other, GeoBox):
return False
return (
self._shape == other._shape
and self._affine == other._affine
and self._crs == other._crs
)
def __rmul__(self, transform: Affine) -> "GeoBox":
"""
Apply affine transform on CRS side.
This has effect of transforming footprint of the source via ``transform``.
:param transform:
Affine matrix that shifts footprint of the source geobox.
:return:
:py:class:`~odc.geo.gebox.GeoBox` of the same pixel shape but
covering different region.
"""
return GeoBox(self._shape, transform * self._affine, self._crs)
def __mul__(self, transform: Affine) -> "GeoBox":
"""
Apply affine transform on pixel side.
``X_old_pix = transform * X_new_pix``
:param transform:
Affine matrix mapping from new pixel coordinate space to pixel coordinate
space of input geobox.
:returns:
:py:class:`~odc.geo.gebox.GeoBox` of the same pixel shape but covering different
region. Pixel coordinates in the output relate to input coordinates via ``transform``.
"""
return GeoBox(self._shape, self._affine * transform, self._crs)
def snap_to(self, other: "GeoBox") -> "GeoBox":
"""
Snap pixel grid to ``other``.
Find smallest sub-pixel translation to apply to this geobox such that
pixel edges align with ``other``.
:param other: GeoBox to snap to, must be related by translation only, no
change in scale or rotation.
:raises: ``ValueError`` when ``other`` is in a different projection or
has different resolution or orientation.
"""
_, subpix = split_translation(pixel_translation(other, self))
tx, ty = subpix.map(lambda x: maybe_zero(x, 1e-8)).xy
return self.translate_pix(tx, ty)
def pad(self, padx: int, pady: MaybeInt = None) -> "GeoBox":
"""
Pad geobox.
Expand GeoBox by fixed number of pixels on each side
"""
# false positive for -pady, it's never None by the time it runs
# pylint: disable=invalid-unary-operand-type
pady = padx if pady is None else pady
ny, nx = self._shape.yx
A = self._affine * Affine.translation(-padx, -pady)
shape = (ny + pady * 2, nx + padx * 2)
return GeoBox(shape, A, self._crs)
def pad_wh(self, alignx: int = 16, aligny: MaybeInt = None) -> "GeoBox":
"""
Possibly expand :py:class:`~odc.geo.geobox.GeoBox` by a few pixels.
Find nearest ``width``/``height`` that are multiples of the desired factor. And return a new
geobox that is slighly taller and/or wider covering roughly the same region. The new geobox
will have the same CRS and transform but possibly larger shape.
"""
aligny = alignx if aligny is None else aligny
ny, nx = (align_up(sz, n) for sz, n in zip(self._shape.yx, (aligny, alignx)))
return GeoBox((ny, nx), self._affine, self._crs)
def crop(self, shape: SomeShape) -> "GeoBox":
"""
Crop or expand to a given shape.
:returns: New :py:class:`~odc.geo.geobox.GeoBox` with a new shape,
top left pixel remains at the same location and scale.
"""
return GeoBox(shape, self._affine, self._crs)
expand = crop
def zoom_out(self, factor: float) -> "GeoBox":
"""
Compute :py:class:`~odc.geo.geobox.GeoBox` with changed resolution.
- ``factor > 1`` implies smaller width/height, fewer but bigger pixels
- ``factor < 1`` implies bigger width/height, more but smaller pixels
:returns:
GeoBox covering the same region but with different pixels (i.e. lower or higher resolution)
"""
_shape, _affine = self.compute_zoom_out(factor)
return GeoBox(_shape, _affine, self._crs)
def zoom_to(
self,
shape: Union[SomeShape, int, float, None] = None,
*,
resolution: Optional[SomeResolution] = None,
) -> "GeoBox":
"""
Change GeoBox shape.
When supplied a single integer scale longest dimension to match that.
:returns:
GeoBox covering the same region but with different number of pixels and therefore resolution.
"""
_shape, _affine = self.compute_zoom_to(shape, resolution=resolution)
return GeoBox(_shape, _affine, self._crs)
def flipy(self) -> "GeoBox":
"""
Flip along Y axis.
:returns: GeoBox covering the same region but with Y-axis flipped
"""
ny, _ = self._shape
A = Affine.translation(0, ny) * Affine.scale(1, -1)
return self * A
def flipx(self) -> "GeoBox":
"""
Flip along X axis.
:returns: GeoBox covering the same region but with X-axis flipped
"""
_, nx = self._shape
A = Affine.translation(nx, 0) * Affine.scale(-1, 1)
return self * A
def translate_pix(self, 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.
"""
return self * Affine.translation(tx, ty)
@property
def left(self) -> "GeoBox":
"""Same size geobox to the left of this one."""
return self.translate_pix(-self.shape.x, 0)
@property
def right(self) -> "GeoBox":
"""Same size geobox to the right of this one."""
return self.translate_pix(self.shape.x, 0)
@property
def top(self) -> "GeoBox":
"""Same size geobox directly above this one."""
return self.translate_pix(0, -self.shape.y)
@property
def bottom(self) -> "GeoBox":
"""Same size geobox directly below this one."""
return self.translate_pix(0, self.shape.y)
def rotate(self, 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 the pixel point of view image rotates the other way. If you have
source image with an arrow pointing right, and you rotate GeoBox 90 degrees,
in that view arrow should point down (this is assuming usual case of inverted
y-axis)
"""
ny, nx = self._shape
c0 = self._affine * (nx * 0.5, ny * 0.5)
return Affine.rotation(deg, c0) * self
def _confirm_axis_aligned(self, raise_error: Optional[str] = None) -> bool:
if is_affine_st(self._affine):
return True
if raise_error is not None:
raise ValueError(raise_error)
return False
@property
def axis_aligned(self):
"""
Check if Geobox is axis-aligned (not rotated).
"""
return self._confirm_axis_aligned()
@property
def center_pixel(self) -> "GeoBox":
"""
GeoBox of a center pixel.
"""
return self[self.shape.map(lambda x: x // 2).yx]
@property
def compat(self):
"""
Convert to :py:class:`datacube.utils.geometry.GeoBox`
"""
try:
dc_geom = importlib.import_module("datacube.utils.geometry")
except ModuleNotFoundError:
return None