Skip to content

Commit e97f158

Browse files
committed
feat: generate a map configuration based on a wms
1 parent b014628 commit e97f158

4 files changed

Lines changed: 130 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ dependencies = [
3939
"httpx>=0.28.1",
4040
"setuptools>=65.5.0",
4141
"backoff>=2.1.0",
42-
"pyyaml>=6.0.3"
42+
"pyyaml>=6.0.3",
43+
"owslib>=0.35.0"
4344
]
4445
description = ""
4546
license = "GPL-3.0+"

src/datasync/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import typer
66

7-
from . import dms, grass, ninagen, nva, pit_registering_salmon, services, ubw
7+
from . import dms, grass, maps, ninagen, nva, pit_registering_salmon, services, ubw
88

99
app = typer.Typer(
1010
help="Provide subcommands for synchronizing different resources, see subcommands"
@@ -16,6 +16,7 @@
1616
app.add_typer(pit_registering_salmon.app, name="pit-registering-salmon")
1717
app.add_typer(grass.app, name="grass-gis")
1818
app.add_typer(services.app, name="services")
19+
app.add_typer(maps.app, name="maps")
1920

2021
if __name__ == "__main__":
2122
app()

src/datasync/maps.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import json
2+
from pathlib import Path
3+
from urllib import parse
4+
5+
import typer
6+
from owslib.wms import WebMapService
7+
8+
from .settings import log
9+
10+
app = typer.Typer()
11+
12+
13+
@app.command()
14+
def wms_to_map(get_capabilities_url: str, output: str = "output.json"):
15+
wms = WebMapService(get_capabilities_url)
16+
log.info(wms.contents)
17+
18+
parsed = parse.urlparse(get_capabilities_url)
19+
qs = parse.parse_qs(parsed.query)
20+
map_param = qs.get("map", [None])[0]
21+
22+
items = {}
23+
24+
cn = wms.__class__.__name__
25+
26+
for key, value in wms.contents.items():
27+
log.info(key, value=value)
28+
29+
req = getattr(wms, f"_{cn}__build_getmap_request")(
30+
layers=[key],
31+
styles=[""],
32+
srs="EPSG:3857",
33+
format="image/png",
34+
size=(256, 256),
35+
transparent=True,
36+
bbox=(0, 0, 0, 0),
37+
)
38+
if map_param:
39+
req["map"] = map_param
40+
41+
del req["bbox"]
42+
43+
legend = {
44+
"service": "WMS",
45+
"version": wms.version,
46+
"request": "GetLegendGraphic",
47+
"layer": key,
48+
"style": "",
49+
"format": "image/png",
50+
"transparent": "true",
51+
}
52+
53+
if map_param:
54+
legend["map"] = map_param
55+
56+
items[key] = {
57+
"name": value.title,
58+
"type": "layer",
59+
"layer": {
60+
"type": "raster",
61+
"tiles": [
62+
parse.urlunsplit(
63+
(
64+
parsed.scheme,
65+
parsed.netloc,
66+
parsed.path,
67+
parse.urlencode(req, doseq=True) + "&bbox={bbox-epsg-3857}",
68+
"",
69+
)
70+
)
71+
],
72+
"legend": {
73+
"type": "image",
74+
"url": parse.urlunsplit(
75+
(
76+
parsed.scheme,
77+
parsed.netloc,
78+
parsed.path,
79+
parse.urlencode(
80+
legend,
81+
doseq=True,
82+
),
83+
"",
84+
)
85+
),
86+
},
87+
},
88+
}
89+
90+
conf = {
91+
"id": parsed.hostname,
92+
"title": wms.identification.title,
93+
"subtitle": "",
94+
"description": wms.identification.abstract,
95+
"icon": "https://s3-ext-1.nina.no/dms/maps/logosmall-BFfoJdyr.png",
96+
"baseMap": "positron",
97+
"layerOrder": [],
98+
"viewState": {"longitude": 17.8, "latitude": 65.6, "zoom": 2},
99+
"expandedItems": [],
100+
"items": {
101+
"root": {
102+
"name": "Layers",
103+
"type": "folder",
104+
"children": list(items.keys()),
105+
},
106+
**items,
107+
},
108+
"config": {
109+
"titiler_api_url": "/titiler",
110+
"theme": "nina",
111+
"language": "en",
112+
"footer": {
113+
"items": [
114+
"![](https://s3-ext-1.nina.no/dms/maps/logowhite-6HSLYLYZ.png)",
115+
"Norsk institutt for naturforskning - [www.nina.no](https://www.nina.no)",
116+
],
117+
"justify": "justify-between",
118+
"align": "items-baseline",
119+
},
120+
},
121+
}
122+
123+
with Path(output).open("w") as f:
124+
json.dump(conf, f, indent=4)

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)