Skip to content

Commit

Permalink
remove/add asset_prefix to asset url (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Dec 6, 2023
1 parent 2e12ff1 commit 8d438dc
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 7.1.0

* Automatically remove/add `asset_prefix` in Mosaic Backends

## 7.0.1 (2023-10-17)

Expand Down
6 changes: 5 additions & 1 deletion cogeo_mosaic/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,17 @@ def assets_for_bbox(
def get_assets(self, x: int, y: int, z: int) -> List[str]:
"""Find assets."""
quadkeys = self.find_quadkeys(Tile(x=x, y=y, z=z), self.quadkey_zoom)
return list(
assets = list(
dict.fromkeys(
itertools.chain.from_iterable(
[self.mosaic_def.tiles.get(qk, []) for qk in quadkeys]
)
)
)
if self.mosaic_def.asset_prefix:
assets = [self.mosaic_def.asset_prefix + asset for asset in assets]

return assets

def find_quadkeys(self, tile: Tile, quadkey_zoom: int) -> List[str]:
"""
Expand Down
6 changes: 5 additions & 1 deletion cogeo_mosaic/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,17 @@ def update(
def get_assets(self, x: int, y: int, z: int) -> List[str]:
"""Find assets."""
quadkeys = self.find_quadkeys(Tile(x=x, y=y, z=z), self.quadkey_zoom)
return list(
assets = list(
dict.fromkeys(
itertools.chain.from_iterable(
[self._fetch_dynamodb(qk).get("assets", []) for qk in quadkeys]
)
)
)
if self.mosaic_def.asset_prefix:
assets = [self.mosaic_def.asset_prefix + asset for asset in assets]

return assets

@property
def _quadkeys(self) -> List[str]:
Expand Down
6 changes: 5 additions & 1 deletion cogeo_mosaic/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,15 @@ def get_assets(self, x: int, y: int, z: int) -> List[str]:
"""Find assets."""
mercator_tile = morecantile.Tile(x=x, y=y, z=z)
quadkeys = self.find_quadkeys(mercator_tile, self.quadkey_zoom)
return list(
assets = list(
dict.fromkeys(
itertools.chain.from_iterable([self._fetch(qk) for qk in quadkeys])
)
)
if self.mosaic_def.asset_prefix:
assets = [self.mosaic_def.asset_prefix + asset for asset in assets]

return assets

@property
def _quadkeys(self) -> List[str]:
Expand Down
14 changes: 11 additions & 3 deletions cogeo_mosaic/mosaic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""cogeo_mosaic.mosaic MosaicJSON models and helper functions."""

import os
import re
import sys
import warnings
from contextlib import ExitStack
Expand Down Expand Up @@ -230,9 +231,16 @@ def _create_mosaic(
)

if dataset:
mosaic_definition["tiles"][quadkey] = [
accessor(f) for f in dataset
]
assets = [accessor(f) for f in dataset]
if asset_prefix:
assets = [
re.sub(rf"^{asset_prefix}", "", asset)
if asset.startswith(asset_prefix)
else asset
for asset in assets
]

mosaic_definition["tiles"][quadkey] = assets

return cls(**mosaic_definition)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,15 @@ def test_point_crs_coordinates():
assert ptsR[0][0] == pts[0][0]
assert ptsR[0][1].crs == "epsg:3857"
assert ptsR[0][1].coordinates == (-8200051.8694, 5782905.49327)


def test_InMemoryReader_asset_prefix():
"""Test MemoryBackend."""
assets = [asset1, asset2]
prefix = os.path.join(os.path.dirname(__file__), "fixtures")
mosaicdef = MosaicJSON.from_urls(assets, quiet=False, asset_prefix=prefix)

assert mosaicdef.tiles["0302310"] == ["/cog1.tif", "/cog2.tif"]
with MemoryBackend(mosaic_def=mosaicdef) as mosaic:
assets = mosaic.assets_for_tile(150, 182, 9)
assert assets[0].startswith(prefix)
5 changes: 3 additions & 2 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_mosaic_create_additional_metadata():
quiet=True,
tilematrixset=tms_3857,
asset_type="COG",
asset_prefix="s3://my-bucket/",
asset_prefix=basepath,
data_type="uint16",
layers={
"true-color": {
Expand All @@ -137,6 +137,7 @@ def test_mosaic_create_additional_metadata():
},
)
assert mosaic.asset_type == "COG"
assert mosaic.asset_prefix == "s3://my-bucket/"
assert mosaic.asset_prefix == basepath
assert mosaic.data_type == "uint16"
assert mosaic.layers["true-color"]
assert mosaic.tiles["0302301"] == ["/cog1.tif", "/cog2.tif"]

0 comments on commit 8d438dc

Please sign in to comment.