This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stac.py
126 lines (106 loc) · 3.3 KB
/
stac.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
"""rio-tiler-crs.stac."""
from dataclasses import dataclass
from typing import Any, Optional, Sequence, Tuple, Union
import morecantile
import numpy
from rio_tiler.expression import apply_expression
from rio_tiler.io import STACReader as RioTilerReader
from .cogeo import multi_tile
TMS = morecantile.tms.get("WebMercatorQuad")
@dataclass
class STACReader(RioTilerReader):
"""
STAC + Cloud Optimized GeoTIFF Reader.
Examples
--------
with STACReader(stac_path) as stac:
stac.tile(...)
my_stac = {
"type": "Feature",
"stac_version": "1.0.0",
...
}
with STACReader(None, item=my_stac) as stac:
stac.tile(...)
Attributes
----------
filepath: str
STAC Item path, URL or S3 URL.
item: Dict, optional
STAC Item dict.
tms: morecantile.TileMatrixSet, optional
TileMatrixSet to use, default is WebMercatorQuad.
minzoom: int, optional
Set minzoom for the tiles.
minzoom: int, optional
Set maxzoom for the tiles.
include_assets: Set, optional
Only accept some assets.
exclude_assets: Set, optional
Exclude some assets.
include_asset_types: Set, optional
Only include some assets base on their type
include_asset_types: Set, optional
Exclude some assets base on their type
Properties
----------
bounds: tuple[float]
STAC bounds in WGS84 crs.
center: tuple[float, float, int]
STAC item center + minzoom
Methods
-------
tile(0, 0, 0, assets="B01", expression="B01/B02")
Read a map tile from the COG.
part((0,10,0,10), assets="B01", expression="B1/B20", max_size=1024)
Read part of the COG.
preview(assets="B01", max_size=1024)
Read preview of the COG.
point((10, 10), assets="B01")
Read a point value from the COG.
stats(assets="B01", pmin=5, pmax=95)
Get Raster statistics.
info(assets="B01")
Get Assets raster info.
metadata(assets="B01", pmin=5, pmax=95)
info + stats
"""
tms: morecantile.TileMatrixSet = TMS
maxzoom: int = TMS.maxzoom
minzoom: int = TMS.minzoom
def tile(
self,
tile_x: int,
tile_y: int,
tile_z: int,
tilesize: int = 256,
assets: Union[Sequence[str], str] = None,
expression: Optional[str] = "", # Expression based on asset names
asset_expression: Optional[
str
] = "", # Expression for each asset based on index names
**kwargs: Any,
) -> Tuple[numpy.ndarray, numpy.ndarray]:
"""Read a TMS map tile from COGs."""
if isinstance(assets, str):
assets = (assets,)
if expression:
assets = self._parse_expression(expression)
if not assets:
raise Exception(
"assets must be passed either via expression or assets options."
)
asset_urls = self._get_href(assets)
data, mask = multi_tile(
asset_urls,
tile_x,
tile_y,
tile_z,
expression=asset_expression,
tms=self.tms,
**kwargs,
)
if expression:
blocks = expression.split(",")
data = apply_expression(blocks, assets, data)
return data, mask