-
Notifications
You must be signed in to change notification settings - Fork 29
/
mosaic.py
485 lines (410 loc) · 15.1 KB
/
mosaic.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
"""TiTiler.PgSTAC custom Mosaic Backend and Custom STACReader."""
import json
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type
import attr
import rasterio
from cachetools import TTLCache, cached
from cachetools.keys import hashkey
from cogeo_mosaic.backends import BaseBackend
from cogeo_mosaic.errors import MosaicNotFoundError, NoAssetFoundError
from cogeo_mosaic.mosaic import MosaicJSON
from geojson_pydantic import Point, Polygon
from geojson_pydantic.geometries import Geometry, parse_geometry_obj
from morecantile import Tile, TileMatrixSet
from psycopg import errors as pgErrors
from psycopg_pool import ConnectionPool
from rasterio.crs import CRS
from rasterio.warp import transform, transform_bounds, transform_geom
from rio_tiler.constants import MAX_THREADS, WEB_MERCATOR_TMS, WGS84_CRS
from rio_tiler.errors import InvalidAssetName, PointOutsideBounds
from rio_tiler.io import Reader
from rio_tiler.io.base import BaseReader, MultiBaseReader
from rio_tiler.models import ImageData, PointData
from rio_tiler.mosaic import mosaic_reader
from rio_tiler.tasks import create_tasks, filter_tasks
from rio_tiler.types import AssetInfo, BBox
from titiler.pgstac.settings import CacheSettings, RetrySettings
from titiler.pgstac.utils import retry
cache_config = CacheSettings()
retry_config = RetrySettings()
def multi_points_pgstac(
asset_list: Sequence[Dict[str, Any]],
reader: Callable[..., PointData],
*args: Any,
threads: int = MAX_THREADS,
allowed_exceptions: Optional[Tuple] = None,
**kwargs: Any,
) -> Dict:
"""Merge values returned from tasks.
Custom version of `rio_tiler.task.multi_values` which
use constructed `item_id` as dict key.
"""
tasks = create_tasks(reader, asset_list, threads, *args, **kwargs)
out: Dict[str, Any] = {}
for val, asset in filter_tasks(tasks, allowed_exceptions=allowed_exceptions):
item_id = f"{asset['collection']}/{asset['id']}"
out[item_id] = val
return out
@attr.s
class CustomSTACReader(MultiBaseReader):
"""Simplified STAC Reader.
Inputs should be in form of:
{
"id": "IAMASTACITEM",
"collection": "mycollection",
"bbox": (0, 0, 10, 10),
"assets": {
"COG": {
"href": "https://somewhereovertherainbow.io/cog.tif"
}
}
}
"""
input: Dict[str, Any] = attr.ib()
tms: TileMatrixSet = attr.ib(default=WEB_MERCATOR_TMS)
minzoom: int = attr.ib()
maxzoom: int = attr.ib()
reader: Type[BaseReader] = attr.ib(default=Reader)
reader_options: Dict = attr.ib(factory=dict)
ctx: Any = attr.ib(default=rasterio.Env)
def __attrs_post_init__(self) -> None:
"""Set reader spatial infos and list of valid assets."""
self.bounds = self.input["bbox"]
self.crs = WGS84_CRS # Per specification STAC items are in WGS84
self.assets = list(self.input["assets"])
@minzoom.default
def _minzoom(self):
return self.tms.minzoom
@maxzoom.default
def _maxzoom(self):
return self.tms.maxzoom
def _get_asset_info(self, asset: str) -> AssetInfo:
"""Validate asset names and return asset's url.
Args:
asset (str): STAC asset name.
Returns:
str: STAC asset href.
"""
if asset not in self.assets:
raise InvalidAssetName(
f"{asset} is not valid. Should be one of {self.assets}"
)
asset_info = self.input["assets"][asset]
info = AssetInfo(
url=asset_info["href"],
env={},
)
if header_size := asset_info.get("file:header_size"):
info["env"]["GDAL_INGESTED_BYTES_AT_OPEN"] = header_size
if bands := asset_info.get("raster:bands"):
stats = [
(b["statistics"]["minimum"], b["statistics"]["maximum"])
for b in bands
if {"minimum", "maximum"}.issubset(b.get("statistics", {}))
]
if len(stats) == len(bands):
info["dataset_statistics"] = stats
return info
@attr.s
class PGSTACBackend(BaseBackend):
"""PgSTAC Mosaic Backend."""
# Mosaic ID (hash)
input: str = attr.ib()
# Connection POOL to the database
pool: ConnectionPool = attr.ib()
# Because we are not using mosaicjson we are not limited to the WebMercator TMS
tms: TileMatrixSet = attr.ib(default=WEB_MERCATOR_TMS)
minzoom: int = attr.ib()
maxzoom: int = attr.ib()
# Use Custom STAC reader (outside init)
reader: Type[CustomSTACReader] = attr.ib(init=False, default=CustomSTACReader)
reader_options: Dict = attr.ib(factory=dict)
# default values for bounds
bounds: BBox = attr.ib(default=(-180, -90, 180, 90))
crs: CRS = attr.ib(default=WGS84_CRS)
geographic_crs: CRS = attr.ib(default=WGS84_CRS)
# The reader is read-only (outside init)
mosaic_def: MosaicJSON = attr.ib(init=False)
_backend_name = "PgSTAC"
def __attrs_post_init__(self) -> None:
"""Post Init."""
# Construct a FAKE mosaicJSON
# mosaic_def has to be defined.
# we set `tiles` to an empty list.
self.mosaic_def = MosaicJSON(
mosaicjson="0.0.3",
name=self.input,
bounds=self.bounds,
minzoom=self.minzoom,
maxzoom=self.maxzoom,
tiles={},
)
@minzoom.default
def _minzoom(self):
return self.tms.minzoom
@maxzoom.default
def _maxzoom(self):
return self.tms.maxzoom
def write(self, overwrite: bool = True) -> None:
"""This method is not used but is required by the abstract class."""
pass
def update(self) -> None:
"""We overwrite the default method."""
pass
def _read(self) -> MosaicJSON:
"""This method is not used but is required by the abstract class."""
pass
def assets_for_tile(self, x: int, y: int, z: int, **kwargs: Any) -> List[Dict]:
"""Retrieve assets for tile."""
bbox = self.tms.bounds(Tile(x, y, z))
return self.get_assets(Polygon.from_bounds(*bbox), **kwargs)
def assets_for_point(
self,
lng: float,
lat: float,
coord_crs: CRS = WGS84_CRS,
**kwargs: Any,
) -> List[Dict]:
"""Retrieve assets for point."""
# Point search is currently broken within PgSTAC
# in order to return the correct result we need to make sure exitwhenfull and skipcovered options
# are set to `False`
# ref: https://github.com/stac-utils/pgstac/pull/52
kwargs.update(**{"exitwhenfull": False, "skipcovered": False})
if coord_crs != WGS84_CRS:
xs, ys = transform(coord_crs, WGS84_CRS, [lng], [lat])
lng, lat = xs[0], ys[0]
return self.get_assets(Point(type="Point", coordinates=(lng, lat)), **kwargs)
def assets_for_bbox(
self,
xmin: float,
ymin: float,
xmax: float,
ymax: float,
coord_crs: CRS = WGS84_CRS,
**kwargs: Any,
) -> List[Dict]:
"""Retrieve assets for bbox."""
if coord_crs != WGS84_CRS:
xmin, ymin, xmax, ymax = transform_bounds(
coord_crs,
WGS84_CRS,
xmin,
ymin,
xmax,
ymax,
)
return self.get_assets(Polygon.from_bounds(xmin, ymin, xmax, ymax), **kwargs)
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, geom, **kwargs: hashkey(self.input, str(geom), **kwargs),
)
@retry(
tries=retry_config.retry,
delay=retry_config.delay,
exceptions=(
pgErrors.OperationalError,
pgErrors.InterfaceError,
),
)
def get_assets(
self,
geom: Geometry,
fields: Optional[Dict[str, Any]] = None,
scan_limit: Optional[int] = None,
items_limit: Optional[int] = None,
time_limit: Optional[int] = None,
exitwhenfull: Optional[bool] = None,
skipcovered: Optional[bool] = None,
) -> List[Dict]:
"""Find assets."""
fields = fields or {
"include": ["assets", "id", "bbox", "collection"],
}
scan_limit = scan_limit or 10000
items_limit = items_limit or 100
time_limit = time_limit or 5
exitwhenfull = True if exitwhenfull is None else exitwhenfull
skipcovered = True if skipcovered is None else skipcovered
with self.pool.connection() as conn:
with conn.cursor() as cursor:
try:
cursor.execute(
"SELECT * FROM geojsonsearch(%s, %s, %s, %s, %s, %s, %s, %s);",
(
geom.model_dump_json(exclude_none=True),
self.input,
json.dumps(fields),
scan_limit,
items_limit,
f"{time_limit} seconds",
exitwhenfull,
skipcovered,
),
)
resp = cursor.fetchone()[0]
except pgErrors.RaiseException as e:
# Catch Invalid SearchId and raise specific Error
if f"Search with Query Hash {self.input} Not Found" in str(e):
raise MosaicNotFoundError(
f"SearchId `{self.input}` not found"
) from e
else:
raise e
return resp.get("features", [])
@property
def _quadkeys(self) -> List[str]:
return []
def tile(
self,
tile_x: int,
tile_y: int,
tile_z: int,
scan_limit: Optional[int] = None,
items_limit: Optional[int] = None,
time_limit: Optional[int] = None,
exitwhenfull: Optional[bool] = None,
skipcovered: Optional[bool] = None,
**kwargs: Any,
) -> Tuple[ImageData, List[str]]:
"""Get Tile from multiple observation."""
mosaic_assets = self.assets_for_tile(
tile_x,
tile_y,
tile_z,
scan_limit=scan_limit,
items_limit=items_limit,
time_limit=time_limit,
exitwhenfull=exitwhenfull,
skipcovered=skipcovered,
)
if not mosaic_assets:
raise NoAssetFoundError(
f"No assets found for tile {tile_z}-{tile_x}-{tile_y}"
)
def _reader(
item: Dict[str, Any], x: int, y: int, z: int, **kwargs: Any
) -> ImageData:
with self.reader(item, tms=self.tms, **self.reader_options) as src_dst:
return src_dst.tile(x, y, z, **kwargs)
return mosaic_reader(mosaic_assets, _reader, tile_x, tile_y, tile_z, **kwargs)
def point(
self,
lon: float,
lat: float,
coord_crs: CRS = WGS84_CRS,
scan_limit: Optional[int] = None,
items_limit: Optional[int] = None,
time_limit: Optional[int] = None,
exitwhenfull: Optional[bool] = None,
skipcovered: Optional[bool] = None,
**kwargs: Any,
) -> List:
"""Get Point value from multiple observation."""
mosaic_assets = self.assets_for_point(
lon,
lat,
coord_crs=coord_crs,
scan_limit=scan_limit,
items_limit=items_limit,
time_limit=time_limit,
exitwhenfull=exitwhenfull,
skipcovered=skipcovered,
)
if not mosaic_assets:
raise NoAssetFoundError(f"No assets found for point ({lon},{lat})")
def _reader(
item: Dict[str, Any],
lon: float,
lat: float,
coord_crs: CRS = coord_crs,
**kwargs: Any,
) -> PointData:
with self.reader(item, **self.reader_options) as src_dst:
return src_dst.point(lon, lat, coord_crs=coord_crs, **kwargs)
if "allowed_exceptions" not in kwargs:
kwargs.update({"allowed_exceptions": (PointOutsideBounds,)})
return list(
multi_points_pgstac(mosaic_assets, _reader, lon, lat, **kwargs).items()
)
def part(
self,
bbox: BBox,
dst_crs: Optional[CRS] = None,
bounds_crs: CRS = WGS84_CRS,
scan_limit: Optional[int] = None,
items_limit: Optional[int] = None,
time_limit: Optional[int] = None,
exitwhenfull: Optional[bool] = None,
skipcovered: Optional[bool] = None,
**kwargs: Any,
) -> Tuple[ImageData, List[str]]:
"""Create an Image from multiple items for a bbox."""
xmin, ymin, xmax, ymax = bbox
mosaic_assets = self.assets_for_bbox(
xmin,
ymin,
xmax,
ymax,
coord_crs=bounds_crs,
scan_limit=scan_limit,
items_limit=items_limit,
time_limit=time_limit,
exitwhenfull=exitwhenfull,
skipcovered=skipcovered,
)
if not mosaic_assets:
raise NoAssetFoundError("No assets found for bbox input")
def _reader(item: Dict[str, Any], bbox: BBox, **kwargs: Any) -> ImageData:
with self.reader(item, **self.reader_options) as src_dst:
return src_dst.part(bbox, **kwargs)
return mosaic_reader(
mosaic_assets,
_reader,
bbox,
bounds_crs=bounds_crs,
dst_crs=dst_crs or bounds_crs,
**kwargs,
)
def feature(
self,
shape: Dict,
dst_crs: Optional[CRS] = None,
shape_crs: CRS = WGS84_CRS,
max_size: int = 1024,
scan_limit: Optional[int] = None,
items_limit: Optional[int] = None,
time_limit: Optional[int] = None,
exitwhenfull: Optional[bool] = None,
skipcovered: Optional[bool] = None,
**kwargs: Any,
) -> Tuple[ImageData, List[str]]:
"""Create an Image from multiple items for a GeoJSON feature."""
if "geometry" in shape:
shape = shape["geometry"]
# PgSTAC needs geometry in WGS84
shape_wgs84 = shape
if shape_crs != WGS84_CRS:
shape_wgs84 = transform_geom(shape_crs, WGS84_CRS, shape)
mosaic_assets = self.get_assets(
parse_geometry_obj(shape_wgs84),
scan_limit=scan_limit,
items_limit=items_limit,
time_limit=time_limit,
exitwhenfull=exitwhenfull,
skipcovered=skipcovered,
)
if not mosaic_assets:
raise NoAssetFoundError("No assets found for Geometry")
def _reader(item: Dict[str, Any], shape: Dict, **kwargs: Any) -> ImageData:
with self.reader(item, **self.reader_options) as src_dst:
return src_dst.feature(shape, **kwargs)
return mosaic_reader(
mosaic_assets,
_reader,
shape,
shape_crs=shape_crs,
dst_crs=dst_crs or shape_crs,
max_size=max_size,
**kwargs,
)