Coverage for app / documents / visuals / maps / projection.py: 100%
64 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""Web Mercator, as Leaflet projects a map (``EPSG3857``), and the view a
2map is drawn in: fitted to its features, or centred where the fence says.
4World units are Leaflet's at zoom 0: the world is 256 across, y down. At
5zoom ``z`` one unit is ``2^z`` CSS pixels, drawn at ``PX`` points each. The
6zoom is continuous: print has no tiles to snap to.
7"""
9from __future__ import annotations
11import math
12from dataclasses import dataclass
14from ..theme import PX
16MAX_LATITUDE = 85.0511287798
17WORLD = 256.0
18#: fe-core's ``fitBounds`` padding, and the closest a fit zooms in.
19FIT_PADDING = 24 * PX
20MAX_ZOOM = 16.0
21#: A lone point's view. Leaflet zooms to the street, where tiles show what
22#: is around it; a PDF has no tiles, and a dot on a plain ground says
23#: nothing, so a lone point is shown in its country, over the outlines.
24LONE_POINT_ZOOM = 6.0
25#: fe-core's view when the fence names a centre and no zoom, and when it
26#: has nothing to fit: London.
27CENTRE_ZOOM = 13.0
28FALLBACK_CENTRE = (51.505, -0.09)
29FALLBACK_ZOOM = 10.0
32def project(lng: float, lat: float) -> tuple[float, float]:
33 """``(lng, lat)`` in world units."""
34 lat = max(-MAX_LATITUDE, min(MAX_LATITUDE, lat))
35 s = math.sin(math.radians(lat))
36 x = (lng + 180) / 360 * WORLD
37 y = (0.5 - math.log((1 + s) / (1 - s)) / (4 * math.pi)) * WORLD
38 return x, y
41def unproject(x: float, y: float) -> tuple[float, float]:
42 lng = x / WORLD * 360 - 180
43 n = math.pi - 2 * math.pi * y / WORLD
44 lat = math.degrees(math.atan(math.sinh(n)))
45 return lng, lat
48@dataclass(frozen=True)
49class View:
50 """What the map shows: its centre in world units, its zoom, and the
51 frame in points."""
53 cx: float
54 cy: float
55 zoom: float
56 width: float
57 height: float
59 @property
60 def scale(self) -> float:
61 """Points per world unit."""
62 return 2**self.zoom * PX
64 def place(self, lng: float, lat: float) -> tuple[float, float]:
65 """A position in the frame, y up as a drawing's runs."""
66 x, y = project(lng, lat)
67 return (
68 (x - self.cx) * self.scale + self.width / 2,
69 self.height / 2 - (y - self.cy) * self.scale,
70 )
72 def bounds(self) -> tuple[float, float, float, float]:
73 """What the frame shows: ``(lng0, lat0, lng1, lat1)``."""
74 half_w = self.width / 2 / self.scale
75 half_h = self.height / 2 / self.scale
76 lng0, lat1 = unproject(self.cx - half_w, self.cy - half_h)
77 lng1, lat0 = unproject(self.cx + half_w, self.cy + half_h)
78 return lng0, lat0, lng1, lat1
80 def metres_per_point(self) -> float:
81 _, lat = unproject(self.cx, self.cy)
82 return 40075016.686 * math.cos(math.radians(lat)) / (WORLD * 2**self.zoom) / PX
85def frame_height(width: float, aspect: float | None, max_height: float) -> float:
86 """A map's height: the features' shape where it fits, between half and
87 four fifths of the width, and no taller than the page allows."""
88 if aspect is None:
89 height = 0.6 * width
90 else:
91 height = max(0.5 * width, min(0.8 * width, width * aspect))
92 return min(height, max_height)
95def fit(points: list[tuple[float, float]], width: float, max_height: float) -> View:
96 """The view that shows every point, padded as fe-core pads it."""
97 xs, ys = zip(*(project(lng, lat) for lng, lat in points))
98 x0, x1, y0, y1 = min(xs), max(xs), min(ys), max(ys)
99 bw, bh = x1 - x0, y1 - y0
100 height = frame_height(width, bh / bw if bw > 0 else None, max_height)
101 room_w, room_h = width - 2 * FIT_PADDING, height - 2 * FIT_PADDING
102 scales = [
103 s
104 for s in (room_w / bw if bw > 0 else None, room_h / bh if bh > 0 else None)
105 if s
106 ]
107 zoom = LONE_POINT_ZOOM if not scales else math.log2(min(scales) / PX)
108 zoom = max(0.0, min(MAX_ZOOM, zoom))
109 return View((x0 + x1) / 2, (y0 + y1) / 2, zoom, width, height)
112def centred(
113 lat: float, lng: float, zoom: float, width: float, max_height: float
114) -> View:
115 x, y = project(lng, lat)
116 zoom = max(0.0, min(MAX_ZOOM, zoom))
117 return View(x, y, zoom, width, frame_height(width, None, max_height))