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

Add tms parameter to cli for MosaicJSON #233

Merged
merged 11 commits into from
Oct 4, 2024
10 changes: 8 additions & 2 deletions cogeo_mosaic/mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,13 @@ def from_urls(
>>> MosaicJSON.from_urls(["1.tif", "2.tif"])

"""
features = get_footprints(urls, max_threads=max_threads, quiet=quiet)
tms = kwargs.pop('tilematrixset', WEB_MERCATOR_TMS)
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
features = get_footprints(
urls,
max_threads=max_threads,
tms = tms,
quiet=quiet
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tms will default to WebMercatorQuad directly in the get_footprints method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you wanted changed here and in related comments. Do you want to add an inline comment?


if minzoom is None:
data_minzoom = {feat["properties"]["minzoom"] for feat in features}
Expand All @@ -301,7 +307,7 @@ def from_urls(
raise MultipleDataTypeError("Dataset should have the same data type")

return cls._create_mosaic(
features, minzoom=minzoom, maxzoom=maxzoom, quiet=quiet, **kwargs
features, minzoom=minzoom, maxzoom=maxzoom, quiet=quiet, tilematrixset=tms, **kwargs
)

@classmethod
Expand Down
77 changes: 67 additions & 10 deletions cogeo_mosaic/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from cogeo_mosaic.mosaic import MosaicJSON
from cogeo_mosaic.utils import get_footprints

tms = morecantile.tms.get("WebMercatorQuad")

default_tms = morecantile.tms.get("WebMercatorQuad")


@with_plugins(iter_entry_points("cogeo_mosaic.plugins"))
Expand Down Expand Up @@ -54,6 +55,11 @@ def cogeo_cli():
default=lambda: os.environ.get("MAX_THREADS", multiprocessing.cpu_count() * 5),
help="threads",
)
@click.option(
"--tms",
help="Path to TileMatrixSet JSON file or valid TMS id.",
type=str,
)
@click.option("--name", type=str, help="Mosaic name")
@click.option("--description", type=str, help="Mosaic description")
@click.option("--attribution", type=str, help="Image attibution")
Expand All @@ -73,12 +79,20 @@ def create(
min_tile_cover,
tile_cover_sort,
threads,
tms,
name,
description,
attribution,
quiet,
):
"""Create mosaic definition file."""
tilematrixset = default_tms
if tms:
if tms.endswith('.json'):
with open(tms, "r") as f:
tilematrixset = morecantile.TileMatrixSet(**json.load(f))
else:
tilematrixset = morecantile.tms.get(tms)
input_files = [file.strip() for file in input_files if file.strip()]
mosaicjson = MosaicJSON.from_urls(
input_files,
Expand All @@ -87,6 +101,7 @@ def create(
quadkey_zoom=quadkey_zoom,
minimum_tile_cover=min_tile_cover,
tile_cover_sort=tile_cover_sort,
tilematrixset=tilematrixset,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default will be None and will only be set when passing --tms ...

internally, cogeo-mosaic will default to WebMercatorQuad but without setting it in the MosaicJSON file

max_threads=threads,
quiet=quiet,
)
Expand All @@ -99,7 +114,7 @@ def create(
mosaicjson.attribution = attribution

if output:
with MosaicBackend(output, mosaic_def=mosaicjson) as mosaic:
with MosaicBackend(output, mosaic_def=mosaicjson, tms=tilematrixset) as mosaic:
mosaic.write(overwrite=True)
else:
click.echo(mosaicjson.model_dump_json(exclude_none=True))
Expand All @@ -110,11 +125,23 @@ def create(
@click.option(
"--url", type=str, required=True, help="URL to which the mosaic should be uploaded."
)
def upload(file, url):
@click.option(
"--tms",
help="Path to TileMatrixSet JSON file or valid TMS id.",
type=str,
)
def upload(file, url, tms):
"""Upload mosaic definition file."""
tilematrixset = default_tms
if tms:
if tms.endswith('.json'):
with open(tms, "r") as f:
tilematrixset = morecantile.TileMatrixSet(**json.load(f))
else:
tilematrixset = morecantile.tms.get(tms)
mosaicjson = json.load(file)

with MosaicBackend(url, mosaic_def=mosaicjson) as mosaic:
with MosaicBackend(url, mosaic_def=mosaicjson, tms=tilematrixset) as mosaic:
mosaic.write(overwrite=True)


Expand All @@ -135,6 +162,11 @@ def upload(file, url):
@click.option(
"--tile-cover-sort", help="Sort files by covering %", is_flag=True, default=False
)
@click.option(
"--tms",
help="Path to TileMatrixSet JSON file or valid TMS id.",
type=str,
)
@click.option("--name", type=str, help="Mosaic name")
@click.option("--description", type=str, help="Mosaic description")
@click.option("--attribution", type=str, help="Image attibution")
Expand All @@ -154,12 +186,20 @@ def create_from_features(
quadkey_zoom,
min_tile_cover,
tile_cover_sort,
tms,
name,
description,
attribution,
quiet,
):
"""Create mosaic definition file."""
tilematrixset = default_tms
if tms:
if tms.endswith('.json'):
with open(tms, "r") as f:
tilematrixset = morecantile.TileMatrixSet(**json.load(f))
else:
tilematrixset = morecantile.tms.get(tms)
mosaicjson = MosaicJSON.from_features(
list(features),
minzoom,
Expand All @@ -168,6 +208,7 @@ def create_from_features(
accessor=lambda feature: feature["properties"][property],
minimum_tile_cover=min_tile_cover,
tile_cover_sort=tile_cover_sort,
tilematrixset=tilematrixset,
quiet=quiet,
)

Expand All @@ -179,7 +220,7 @@ def create_from_features(
mosaicjson.attribution = attribution

if output:
with MosaicBackend(output, mosaic_def=mosaicjson) as mosaic:
with MosaicBackend(output, mosaic_def=mosaicjson, tms=tilematrixset) as mosaic:
mosaic.write(overwrite=True)
else:
click.echo(mosaicjson.model_dump_json(exclude_none=True))
Expand All @@ -195,6 +236,11 @@ def create_from_features(
is_flag=True,
default=True,
)
@click.option(
"--tms",
help="Path to TileMatrixSet JSON file or valid TMS id.",
type=str,
)
@click.option(
"--threads",
type=int,
Expand All @@ -208,10 +254,21 @@ def create_from_features(
is_flag=True,
default=False,
)
def update(input_files, input_mosaic, min_tile_cover, add_first, threads, quiet):
def update(input_files, input_mosaic, min_tile_cover, tms, add_first, threads, quiet):
"""Update mosaic definition file."""
tilematrixset = default_tms
if tms:
if tms.endswith('.json'):
with open(tms, "r") as f:
tilematrixset = morecantile.TileMatrixSet(**json.load(f))
else:
tilematrixset = morecantile.tms.get(tms)
input_files = input_files.read().splitlines()
features = get_footprints(input_files, max_threads=threads)
features = get_footprints(
input_files,
tms=tilematrixset,
max_threads=threads
)
with MosaicBackend(input_mosaic) as mosaic:
mosaic.update(
features,
Expand Down Expand Up @@ -280,7 +337,7 @@ def info(input, to_json):
}

geo = {
"TileMatrixSet": "WebMercatorQuad",
"TileMatrixSet": mosaic.tms.id, # TODO this may require deeper and more changes
"BoundingBox": mosaic.mosaic_def.bounds,
"Center": mosaic.mosaic_def.center,
"Min Zoom": mosaic.mosaic_def.minzoom,
Expand Down Expand Up @@ -347,8 +404,8 @@ def to_geojson(input, collect):
features = []
with MosaicBackend(input) as mosaic:
for qk, assets in mosaic.mosaic_def.tiles.items():
tile = tms.quadkey_to_tile(qk)
west, south, east, north = tms.bounds(tile)
tile = mosaic.tms.quadkey_to_tile(qk)
west, south, east, north = mosaic.tms.bounds(tile)

geom = {
"type": "Polygon",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_create_valid():
result = runner.invoke(cogeo_cli, ["create", "list.txt", "--quiet"])
assert not result.exception
assert result.exit_code == 0
#breakpoint()
assert mosaic_content == MosaicJSON(**json.loads(result.output))

result = runner.invoke(cogeo_cli, ["create", "list.txt", "-o", "mosaic.json"])
Expand All @@ -41,6 +42,8 @@ def test_create_valid():
"list.txt",
"-o",
"mosaic.json",
"--tms",
"WorldMercatorWGS84Quad",
"--name",
"my_mosaic",
"--description",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_mosaic_create():
assert mosaic.minzoom == mosaic_content["minzoom"]
assert list(mosaic.tiles.keys()) == list(mosaic_content["tiles"].keys())
assert mosaic.tiles == mosaic_content["tiles"]
assert not mosaic.tilematrixset
assert mosaic.tilematrixset.id == "WebMercatorQuad"

mosaic = MosaicJSON.from_urls(assets, minzoom=7, maxzoom=9)
assert [round(b, 3) for b in list(mosaic.bounds)] == [
Expand Down
Loading