Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ dependencies = [
"httpx>=0.28.1",
"setuptools>=65.5.0",
"backoff>=2.1.0",
"pyyaml>=6.0.3"
"pyyaml>=6.0.3",
"owslib>=0.35.0"
]
description = ""
license = "GPL-3.0+"
Expand Down
2 changes: 2 additions & 0 deletions src/datasync/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
dms,
gbif_backbone,
grass,
maps,
ninagen,
nva,
pit_registering_salmon,
Expand All @@ -25,6 +26,7 @@
app.add_typer(pit_registering_salmon.app, name="pit-registering-salmon")
app.add_typer(grass.app, name="grass-gis")
app.add_typer(services.app, name="services")
app.add_typer(maps.app, name="maps")
app.add_typer(gbif_backbone.app, name="gbif-backbone")

if __name__ == "__main__":
Expand Down
132 changes: 132 additions & 0 deletions src/datasync/maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import json
from pathlib import Path
from urllib import parse

import typer
from owslib.wms import WebMapService

from .settings import log

app = typer.Typer(help="Scripts related to the generation of NINA maps")


@app.command(help="convert a wms resource to a configuration for NINA maps")
def wms_to_map(
get_capabilities_url: str = typer.Argument(
help="The URL of the get capabilities of a WMS service"
),
output: str = typer.Option(
default="output.json", help="Path of the output json file"
),
):
wms = WebMapService(get_capabilities_url)
log.info(wms.contents)

parsed = parse.urlparse(get_capabilities_url)
qs = parse.parse_qs(parsed.query)
map_param = qs.get("map", [None])[0]

items = {}

cn = wms.__class__.__name__

for key, value in wms.contents.items():
log.info(key, value=value)

req = getattr(wms, f"_{cn}__build_getmap_request")(
layers=[key],
styles=[""],
srs="EPSG:3857",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's assuming that the server can do EPSG:3857.

format="image/png",
size=(256, 256),
transparent=True,
bbox=(0, 0, 0, 0),
)
if map_param:
req["map"] = map_param

del req["bbox"]

legend = {
"service": "WMS",
"version": wms.version,
"request": "GetLegendGraphic",
"layer": key,
"style": "",
"format": "image/png",
"transparent": "true",
}

if map_param:
legend["map"] = map_param

items[key] = {
"name": value.title,
"type": "layer",
"layer": {
"type": "raster",
"tiles": [
parse.urlunsplit(
(
parsed.scheme,
parsed.netloc,
parsed.path,
parse.urlencode(req, doseq=True) + "&bbox={bbox-epsg-3857}",
"",
)
)
],
"legend": {
"type": "image",
"url": parse.urlunsplit(
(
parsed.scheme,
parsed.netloc,
parsed.path,
parse.urlencode(
legend,
doseq=True,
),
"",
)
),
},
},
}

conf = {
"id": parsed.hostname,
"title": wms.identification.title,
"subtitle": "",
"description": wms.identification.abstract,
"icon": "https://s3-ext-1.nina.no/dms/maps/logosmall-BFfoJdyr.png",
"baseMap": "positron",
"layerOrder": [],
# TODO: make this configurable or automatic based on the resource

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be automatically computed? mercantile did that, but it looks unmaintained. It might be possible to use morecantile, to get the minimum zoom level required to show the entire bbox, using https://developmentseed.org/morecantile/api/morecantile/models/#morecantile.models.TileMatrixSet.zoom_for_res

"viewState": {"longitude": 17.8, "latitude": 65.6, "zoom": 2},
"expandedItems": [],
"items": {
"root": {
"name": "Layers",
"type": "folder",
"children": list(items.keys()),
},
**items,
},
"config": {
"titiler_api_url": "/titiler",
"theme": "nina",
"language": "en",
"footer": {
"items": [
"![](https://s3-ext-1.nina.no/dms/maps/logowhite-6HSLYLYZ.png)",
"Norsk institutt for naturforskning - [www.nina.no](https://www.nina.no)",
],
"justify": "justify-between",
"align": "items-baseline",
},
},
}

with Path(output).open("w") as f:
json.dump(conf, f, indent=4)
2 changes: 2 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.