Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sketch supermorecado #203

Merged
merged 10 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
language_version: python

- repo: https://github.com/PyCQA/isort
rev: 5.4.2
rev: 5.12.0
hooks:
- id: isort
language_version: python
Expand All @@ -26,7 +26,11 @@ repos:
- toml

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
rev: v0.991
hooks:
- id: mypy
language_version: python
additional_dependencies:
- types-attrs
- types-cachetools
- types-setuptools
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ pip install -e .["test,az"]
You can then run the tests with the following command:

```sh
python -m pytest --cov cogeo_mosaic --cov-report term-missing --benchmark-skip
python -m pytest --cov cogeo_mosaic --cov-report term-missing
```

### pre-commit
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/az.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def write(self, overwrite: bool = False, **kwargs: Any):

self._put_object(self.key, self.container, body, **kwargs)

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def assets_for_bbox(
)
)

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, x, y, z: hashkey(self.input, x, y, z, self.mosaicid),
)
Expand Down
4 changes: 2 additions & 2 deletions cogeo_mosaic/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __attrs_post_init__(self):
self.table = self.client.Table(self.table_name)
super().__attrs_post_init__()

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down Expand Up @@ -202,7 +202,7 @@ def update(

self._write_items(items)

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, x, y, z: hashkey(self.input, x, y, z, self.mosaicid),
)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def write(self, overwrite: bool = False):
exc = _FILE_EXCEPTIONS.get(e, MosaicError) # type: ignore
raise exc(str(e)) from e

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/gs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def write(self, overwrite: bool = False, **kwargs: Any):

self._put_object(self.key, self.bucket, body, **kwargs)

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def write(self, overwrite: bool = False, **kwargs: Any):

self._put_object(self.key, self.bucket, body, **kwargs)

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down
4 changes: 2 additions & 2 deletions cogeo_mosaic/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __exit__(self, exc_type, exc_value, traceback):
"""Support using with Context Managers."""
self.close()

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down Expand Up @@ -267,7 +267,7 @@ def update(
[(assets, qk) for qk, assets in new_mosaic.tiles.items()],
)

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, x, y, z: hashkey(self.input, x, y, z, self.mosaicid),
)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def query_from_link(link: Dict, query: Dict):
return q


@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda url, query, **kwargs: hashkey(url, json.dumps(query), **kwargs),
)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/backends/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __attrs_post_init__(self):
self.maxzoom = self.mosaic_def.maxzoom
self.bounds = self.mosaic_def.bounds

@cached(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
)
Expand Down
4 changes: 2 additions & 2 deletions cogeo_mosaic/mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from pydantic import BaseModel, Field, root_validator
from shapely import linearrings, polygons, total_bounds
from shapely.strtree import STRtree
from supermercado import burntiles

from cogeo_mosaic.errors import MosaicError
from cogeo_mosaic.supermorecado import burntiles
from cogeo_mosaic.utils import _intersect_percent, get_footprints

tms = morecantile.tms.get("WebMercatorQuad")
Expand Down Expand Up @@ -142,7 +142,7 @@ def _create_mosaic(

bounds = tuple(total_bounds(dataset_geoms))

tiles = burntiles.burn(features, quadkey_zoom)
tiles = burntiles(tms).burn(features, quadkey_zoom)
tiles = [morecantile.Tile(*tile) for tile in tiles]

mosaic_definition: Dict[str, Any] = dict(
Expand Down
129 changes: 129 additions & 0 deletions cogeo_mosaic/supermorecado.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Supermercado.burntiles but for other TMS."""
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

from typing import Any, Dict, Sequence, Tuple

import attr
import morecantile
import numpy as np
from affine import Affine
from rasterio import features
from rio_tiler.constants import WEB_MERCATOR_TMS


def _feature_extrema(geometry: Dict) -> Tuple[float, float, float, float]:
"""Get bounds extrema for a feature."""
if geometry["type"] == "Polygon":
x, y = zip(*[c for part in geometry["coordinates"] for c in part])

elif geometry["type"] == "LineString":
x, y = zip(*[c for c in geometry["coordinates"]])

elif geometry["type"] == "Point":
x, y = geometry["coordinates"]
return x, y, x, y
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

return min(x), min(y), max(x), max(y)


def find_extrema(
features: Sequence[Dict[Any, Any]]
) -> Tuple[float, float, float, float]:
"""Get bounds extrema for a set of features."""
epsilon = 1.0e-10
min_x, min_y, max_x, max_y = zip(
*[_feature_extrema(f["geometry"]) for f in features]
)

return (
min(min_x) + epsilon,
max(min(min_y) + epsilon, -85.0511287798066),
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
max(max_x) - epsilon,
min(max(max_y) - epsilon, 85.0511287798066),
)


@attr.s
class burntiles:
"""Burntiles."""

tms: morecantile.TileMatrixSet = attr.ib(default=WEB_MERCATOR_TMS)

# TODO: Add check for quadkey support

def project_geom(self, geom: Dict) -> Dict:
"""reproject geom in TMS CRS."""
if geom["type"] == "Polygon":
return {
"type": geom["type"],
"coordinates": [
[self.tms.xy(*coords) for coords in part]
for part in geom["coordinates"]
],
}

elif geom["type"] == "LineString":
return {
"type": geom["type"],
"coordinates": [self.tms.xy(*coords) for coords in geom["coordinates"]],
}

elif geom["type"] == "Point":
return {
"type": geom["type"],
"coordinates": self.tms.xy(*geom["coordinates"]),
}
else:
raise Exception(f"Invalid geometry type {geom['type']}")

def tile_extrema(self, bounds, zoom):
"""Tiles min/max at the given zoom for bounds."""
minimumTile = self.tms.tile(bounds[0], bounds[3], zoom)
maximumTile = self.tms.tile(bounds[2], bounds[1], zoom)

return {
"x": {"min": minimumTile.x, "max": maximumTile.x + 1},
"y": {"min": minimumTile.y, "max": maximumTile.y + 1},
}

def make_transform(self, tilerange: Dict, zoom: int) -> Affine:
"""Create Affine Transform from extrema."""
ulx, uly = self.tms.xy(
*self.tms.ul(tilerange["x"]["min"], tilerange["y"]["min"], zoom)
)

lrx, lry = self.tms.xy(
*self.tms.ul(tilerange["x"]["max"], tilerange["y"]["max"], zoom)
)

xcell = (lrx - ulx) / float(tilerange["x"]["max"] - tilerange["x"]["min"])
ycell = (uly - lry) / float(tilerange["y"]["max"] - tilerange["y"]["min"])

return Affine(xcell, 0, ulx, 0, -ycell, uly)

def burn(self, polys: Sequence[Dict[Any, Any]], zoom: int):
"""Burn geometries to Tiles."""
bounds = find_extrema(polys)

tilerange = self.tile_extrema(bounds, zoom)
afftrans = self.make_transform(tilerange, zoom)

burn = features.rasterize(
((self.project_geom(geom["geometry"]), 255) for geom in polys),
out_shape=(
(
tilerange["y"]["max"] - tilerange["y"]["min"],
tilerange["x"]["max"] - tilerange["x"]["min"],
)
),
transform=afftrans,
all_touched=True,
)

xys = np.fliplr(np.dstack(np.where(burn))[0])

xys[:, 0] += tilerange["x"]["min"]
xys[:, 1] += tilerange["y"]["min"]

return np.append(
xys, np.zeros((xys.shape[0], 1), dtype=np.uint8) + zoom, axis=1
)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ dependencies = [
"rasterio",
"rio-tiler>=4.0.0a0,<5.0",
"cachetools",
"supermercado",
]

[project.optional-dependencies]
Expand Down