-
Notifications
You must be signed in to change notification settings - Fork 0
feat: generate a map configuration based on a wms #20
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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", | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": [ | ||
| "", | ||
| "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) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.