|
| 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 | + "", |
| 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) |
0 commit comments