tilearray 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ """WMS (Web Map Service) tile request functionality."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+ from ..types import CRS, BoundingBox, Format, ServiceTypeEnum, TileRequest
8
+ from .base import BaseService, TileGeometry, register_service
9
+
10
+
11
+ def format_wms_bbox(bbox: BoundingBox, crs: CRS, version: str) -> str:
12
+ """
13
+ Format a bounding box for WMS GetMap.
14
+
15
+ WMS 1.3.0 follows CRS axis order (latitude-first for EPSG:4326).
16
+ Earlier versions always use minx,miny,maxx,maxy.
17
+ """
18
+
19
+ if version.startswith("1.3"):
20
+ if crs in {CRS.EPSG_4326}:
21
+ return f"{bbox.min_y},{bbox.min_x},{bbox.max_y},{bbox.max_x}"
22
+ return f"{bbox.min_x},{bbox.min_y},{bbox.max_x},{bbox.max_y}"
23
+
24
+ return f"{bbox.min_x},{bbox.min_y},{bbox.max_x},{bbox.max_y}"
25
+
26
+
27
+ @register_service(ServiceTypeEnum.WMS)
28
+ class WMSService(BaseService):
29
+ """Client for WMS GetMap endpoints."""
30
+
31
+ def __init__(
32
+ self,
33
+ base_url: str,
34
+ *,
35
+ layers: str,
36
+ version: str = "1.3.0",
37
+ styles: str = "",
38
+ output_format: Format | None = Format.PNG,
39
+ crs: CRS | None = CRS.EPSG_4326,
40
+ **config: Any,
41
+ ) -> None:
42
+ super().__init__(
43
+ base_url,
44
+ layers=layers,
45
+ version=version,
46
+ styles=styles,
47
+ **config,
48
+ )
49
+ if not layers:
50
+ raise ValueError("WMS layers must be provided")
51
+ self.layers = layers
52
+ self.version = version
53
+ self.styles = styles
54
+ self.output_format = self._coerce_format(
55
+ output_format or config.get("format") or Format.PNG
56
+ )
57
+ self.native_crs = self._coerce_crs(crs or config.get("crs") or CRS.EPSG_4326)
58
+
59
+ @classmethod
60
+ def from_url(cls, url: str, **config: Any) -> WMSService:
61
+ layers = config.get("layers")
62
+ if not layers:
63
+ raise ValueError("WMS services require layers")
64
+ return cls(url, **config)
65
+
66
+ def build_tile_request(self, tile: TileGeometry, **options: Any) -> TileRequest:
67
+ layers = str(options.get("layers") or self.layers)
68
+ if not layers:
69
+ raise ValueError("WMS layers must be provided")
70
+
71
+ version = str(options.get("version") or self.version)
72
+ styles = str(
73
+ options.get("styles") if options.get("styles") is not None else self.styles
74
+ )
75
+ fmt = self._coerce_format(options.get("output_format") or self.output_format)
76
+ crs = self._coerce_crs(options.get("crs") or tile.crs or self.native_crs)
77
+
78
+ params: dict[str, Any] = {
79
+ "service": "WMS",
80
+ "request": "GetMap",
81
+ "version": version,
82
+ "layers": layers,
83
+ "styles": styles,
84
+ "crs" if version.startswith("1.3") else "srs": crs.value,
85
+ "bbox": format_wms_bbox(tile.bbox, crs, version),
86
+ "width": str(tile.width),
87
+ "height": str(tile.height),
88
+ "format": fmt.value,
89
+ "transparent": "true",
90
+ }
91
+
92
+ passthrough = {
93
+ key: value
94
+ for key, value in options.items()
95
+ if key not in {"output_format", "crs", "layers", "styles", "version"}
96
+ }
97
+ return self.compose_tile_request(
98
+ tile,
99
+ url=self.base_url,
100
+ params=params,
101
+ output_format=fmt,
102
+ crs=crs,
103
+ **passthrough,
104
+ )
105
+
106
+ def _coerce_format(self, fmt: Any) -> Format:
107
+ if isinstance(fmt, Format):
108
+ return fmt
109
+ if isinstance(fmt, str):
110
+ try:
111
+ return Format(fmt)
112
+ except ValueError as exc:
113
+ raise ValueError(f"Unsupported WMS format: {fmt}") from exc
114
+ raise ValueError(f"Invalid WMS format value: {fmt!r}")
115
+
116
+ def _coerce_crs(self, crs: Any) -> CRS:
117
+ if isinstance(crs, CRS):
118
+ return crs
119
+ if isinstance(crs, str):
120
+ return (
121
+ CRS.from_epsg(crs)
122
+ if crs.upper().startswith("EPSG:")
123
+ else CRS.from_integer(int(crs))
124
+ )
125
+ if isinstance(crs, int):
126
+ return CRS.from_integer(crs)
127
+ raise ValueError(f"Invalid CRS value: {crs!r}")