99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, List, Mapping, Optional
|
|
|
|
from qgis.core import (
|
|
QgsDataSourceUri,
|
|
QgsLayerMetadata,
|
|
QgsProject,
|
|
QgsVectorTileLayer,
|
|
)
|
|
|
|
from . import config
|
|
from .errors import GeoDonkeyError
|
|
from .i18n import tr
|
|
|
|
|
|
def style_url(api_url: str, style_id: str) -> str:
|
|
return config.api_endpoint(api_url, "/map-styles/%s.json" % style_id)
|
|
|
|
|
|
def tile_url(api_url: str) -> str:
|
|
return config.api_endpoint(api_url, "/tiles/{z}/{x}/{y}.pbf")
|
|
|
|
|
|
def build_uri(api_url: str, style_id: str,
|
|
zmin: int = config.TILE_MIN_ZOOM,
|
|
zmax: int = config.TILE_MAX_ZOOM) -> str:
|
|
uri = QgsDataSourceUri()
|
|
uri.setParam("type", "xyz")
|
|
uri.setParam("url", tile_url(api_url))
|
|
uri.setParam("zmin", str(int(zmin)))
|
|
uri.setParam("zmax", str(int(zmax)))
|
|
uri.setParam("styleUrl", style_url(api_url, style_id))
|
|
return bytes(uri.encodedUri()).decode("utf-8")
|
|
|
|
|
|
def catalogue(payload: Optional[Mapping[str, Any]] = None) -> List[Dict[str, Any]]:
|
|
styles = (payload or {}).get("styles")
|
|
if not styles:
|
|
return [dict(item) for item in config.FALLBACK_STYLES]
|
|
out = []
|
|
for item in styles:
|
|
if not isinstance(item, Mapping) or not item.get("id"):
|
|
continue
|
|
out.append({
|
|
"id": str(item["id"]),
|
|
"name": str(item.get("name") or item["id"]),
|
|
"dark": bool(item.get("dark")),
|
|
"swatch": list(item.get("swatch") or [])[:3],
|
|
"temporary": bool(item.get("temporary")),
|
|
})
|
|
return out or [dict(item) for item in config.FALLBACK_STYLES]
|
|
|
|
|
|
def zoom_range(tilejson: Optional[Mapping[str, Any]] = None) -> "tuple":
|
|
data = tilejson or {}
|
|
try:
|
|
zmin = int(data.get("minzoom", config.TILE_MIN_ZOOM))
|
|
zmax = int(data.get("maxzoom", config.TILE_MAX_ZOOM))
|
|
except (TypeError, ValueError):
|
|
return config.TILE_MIN_ZOOM, config.TILE_MAX_ZOOM
|
|
if zmax <= zmin:
|
|
return config.TILE_MIN_ZOOM, config.TILE_MAX_ZOOM
|
|
return zmin, zmax
|
|
|
|
|
|
def add_basemap(api_url: str, style: Mapping[str, Any],
|
|
zmin: int = config.TILE_MIN_ZOOM,
|
|
zmax: int = config.TILE_MAX_ZOOM,
|
|
attribution: str = "",
|
|
project: Optional[QgsProject] = None) -> QgsVectorTileLayer:
|
|
project = project or QgsProject.instance()
|
|
style_id = str(style.get("id") or "")
|
|
if not style_id:
|
|
raise GeoDonkeyError(tr("No map style selected."))
|
|
|
|
layer = QgsVectorTileLayer(build_uri(api_url, style_id, zmin, zmax),
|
|
"GeoDonkey %s" % (style.get("name") or style_id))
|
|
if not layer.isValid():
|
|
raise GeoDonkeyError(
|
|
tr("The basemap could not be loaded. Check the API address and your connection."))
|
|
|
|
name = layer.name()
|
|
layer.loadDefaultStyle()
|
|
layer.setName(name)
|
|
|
|
md = QgsLayerMetadata()
|
|
md.setTitle(name)
|
|
md.setRights([attribution or config.DEFAULT_ATTRIBUTION])
|
|
md.setLicenses([config.OSM_LICENSE])
|
|
md.setAbstract(tr("GeoDonkey vector tiles, style “{style}”. {license}").format(
|
|
style=style.get("name") or style_id, license=config.OSM_LICENSE_URL))
|
|
layer.setMetadata(md)
|
|
|
|
project.addMapLayer(layer, False)
|
|
root = project.layerTreeRoot()
|
|
root.insertLayer(len(root.children()), layer)
|
|
return layer
|