diff --git a/website/src/lib/components/map/map.ts b/website/src/lib/components/map/map.ts
index cc4c97c30..24122bfcf 100644
--- a/website/src/lib/components/map/map.ts
+++ b/website/src/lib/components/map/map.ts
@@ -10,6 +10,7 @@ import { settings } from '$lib/state/settings';
import { tick } from 'svelte';
import { ANCHOR_LAYER_KEY, StyleManager } from '$lib/components/map/style';
import { MapLayerEventManager } from '$lib/components/map/map-layer-event-manager';
+import { registerOpenSatelliteProtocol } from '$lib/components/map/open-satellite';
const { treeFileView, elevationProfile, bottomPanelSize, rightPanelSize, distanceUnits } = settings;
@@ -41,6 +42,7 @@ export class MapLibreGLMap {
geolocate: boolean
) {
maplibregl.setWorkerUrl(workerUrl);
+ registerOpenSatelliteProtocol(maptilerKey);
this._maptilerKey = maptilerKey;
this._styleManager = new StyleManager(this._mapStore, this._maptilerKey);
// MapLibre gives Safari more workers, but only recognizes it by its user agent: not
diff --git a/website/src/lib/components/map/open-satellite.test.ts b/website/src/lib/components/map/open-satellite.test.ts
new file mode 100644
index 000000000..c1a867aa6
--- /dev/null
+++ b/website/src/lib/components/map/open-satellite.test.ts
@@ -0,0 +1,57 @@
+// Which imagery each tile of the open satellite basemap is made of.
+import assert from 'node:assert/strict';
+import { describe, it } from 'node:test';
+import { planTile } from './open-satellite';
+
+function tile(lon: number, lat: number, z: number): [number, number, number] {
+ const n = 2 ** z;
+ const sin = Math.sin((lat * Math.PI) / 180);
+ return [
+ z,
+ Math.floor(((lon + 180) / 360) * n),
+ Math.floor((0.5 - Math.log((1 + sin) / (1 - sin)) / (4 * Math.PI)) * n),
+ ];
+}
+
+async function plan(lon: number, lat: number, z: number) {
+ const { parts, fallback } = await planTile(...tile(lon, lat, z));
+ return { parts: parts.map((p) => `${p.source.id}:${p.relation}`), fallback };
+}
+
+describe('planTile', () => {
+ it('takes a tile inside a country from its agency alone', async () => {
+ assert.deepEqual(await plan(2.35, 48.85, 15), { parts: ['fr:full'], fallback: false });
+ assert.deepEqual(await plan(6.63, 46.52, 15), { parts: ['ch:full'], fallback: false });
+ assert.deepEqual(await plan(4.35, 50.85, 16), { parts: ['be:full'], fallback: false });
+ assert.deepEqual(await plan(21.01, 52.23, 16), { parts: ['pl:full'], fallback: false });
+ });
+
+ it('takes a tile outside every country from the fallback', async () => {
+ assert.deepEqual(await plan(-0.12, 51.5, 14), { parts: [], fallback: true });
+ // Germany, which is not in the patchwork
+ assert.deepEqual(await plan(9.73, 52.37, 14), { parts: [], fallback: true });
+ assert.deepEqual(await plan(-30, 40, 10), { parts: [], fallback: true });
+ });
+
+ it('puts the countries of a border tile together, over the fallback where none is', async () => {
+ // Menton: France and Italy, which is not in the patchwork
+ assert.deepEqual(await plan(7.5, 43.8, 12), {
+ parts: ['fr:partial'],
+ fallback: true,
+ });
+ // Basel: Switzerland, France and Germany
+ assert.deepEqual(await plan(7.589, 47.5895, 14), {
+ parts: ['ch:partial', 'fr:partial'],
+ fallback: true,
+ });
+ // Somport: France and Spain, between them the whole tile
+ assert.deepEqual(await plan(-0.52, 42.8, 13), {
+ parts: ['fr:partial', 'es:partial'],
+ fallback: false,
+ });
+ });
+
+ it('does not take a tile covering several countries from their agencies', async () => {
+ assert.deepEqual(await plan(2.35, 48.85, 6), { parts: [], fallback: true });
+ });
+});
diff --git a/website/src/lib/components/map/open-satellite.ts b/website/src/lib/components/map/open-satellite.ts
new file mode 100644
index 000000000..80cd3293d
--- /dev/null
+++ b/website/src/lib/components/map/open-satellite.ts
@@ -0,0 +1,444 @@
+import * as maplibregl from 'maplibre-gl';
+
+/**
+ * A satellite basemap made of the open imagery of national mapping agencies, drawn through a
+ * custom protocol: each tile is taken from the agency whose country covers it, the tiles on a
+ * border are put together on a canvas clipped to the border of each country, and only the parts
+ * no open source covers are taken from the commercial fallback.
+ */
+
+export const OPEN_SATELLITE_PROTOCOL = 'opensat';
+
+type TileUrl = (z: number, x: number, y: number) => string;
+
+type ImagerySource = {
+ /** Key of the coverage in satellite-coverage.json */
+ id: string;
+ url: TileUrl;
+ /** Below it, a tile spans too many sources and the fallback is used. */
+ minzoom: number;
+ /** Above it, the tile of this zoom level is scaled up. */
+ maxzoom: number;
+};
+
+const xyz =
+ (template: string): TileUrl =>
+ (z, x, y) =>
+ template.replace('{z}', String(z)).replace('{x}', String(x)).replace('{y}', String(y));
+
+/** In order of priority, the first one covering a point is used. */
+const sources: ImagerySource[] = [
+ {
+ id: 'ch',
+ url: xyz(
+ 'https://wmts.geo.admin.ch/1.0.0/ch.swisstopo.swissimage/default/current/3857/{z}/{x}/{y}.jpeg'
+ ),
+ minzoom: 8,
+ maxzoom: 20,
+ },
+ {
+ id: 'fr',
+ url: xyz(
+ 'https://data.geopf.fr/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ORTHOIMAGERY.ORTHOPHOTOS&STYLE=normal&FORMAT=image/jpeg&TILEMATRIXSET=PM&TILEMATRIX={z}&TILECOL={x}&TILEROW={y}'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'es',
+ url: xyz(
+ 'https://www.ign.es/wmts/pnoa-ma?layer=OI.OrthoimageCoverage&style=default&tilematrixset=GoogleMapsCompatible&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/jpeg&TileMatrix={z}&TileCol={x}&TileRow={y}'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'at',
+ url: xyz(
+ 'https://mapsneu.wien.gv.at/basemap/bmaporthofoto30cm/normal/google3857/{z}/{y}/{x}.jpeg'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'nl',
+ url: xyz(
+ 'https://service.pdok.nl/hwh/luchtfotorgb/wmts/v1_0/Actueel_orthoHR/EPSG:3857/{z}/{x}/{y}.jpeg'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'lu',
+ url: xyz(
+ 'https://wmts1.geoportail.lu/opendata/wmts/ortho_latest/GLOBAL_WEBMERCATOR_4_V3/{z}/{x}/{y}.jpeg'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'cz',
+ url: xyz(
+ 'https://ags.cuzk.gov.cz/arcgis1/rest/services/ORTOFOTO_WM/MapServer/tile/{z}/{y}/{x}'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'be',
+ url: xyz(
+ 'https://wmts.ngi.be/inspire/ortho/1.0.0/orthoimage_coverage/default/3857/latest/{z}/{y}/{x}.png'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+ {
+ id: 'pl',
+ url: xyz(
+ 'https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/StandardResolution?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ORTOFOTOMAPA&STYLE=default&FORMAT=image/jpeg&TILEMATRIXSET=EPSG:3857&TILEMATRIX=EPSG:3857:{z}&TILEROW={y}&TILECOL={x}'
+ ),
+ minzoom: 8,
+ maxzoom: 19,
+ },
+];
+
+export const openSatelliteAttribution = [
+ 'swisstopo',
+ 'IGN-F/Géoplateforme',
+ 'PNOA © IGN España',
+ 'basemap.at',
+ 'PDOK',
+ 'geoportail.lu',
+ 'ČÚZK',
+ 'NGI/IGN',
+ 'GUGiK',
+ '© MapTiler',
+].join(', ');
+
+// Geometry, in Web Mercator coordinates between 0 and 1
+
+type Ring = Float64Array; // x0, y0, x1, y1, ...
+type Polygon = { rings: Ring[]; bbox: [number, number, number, number] };
+type Coverage = { polygons: Polygon[]; bbox: [number, number, number, number] };
+type Rect = [number, number, number, number]; // minX, minY, maxX, maxY
+
+function mercatorX(lon: number) {
+ return (lon + 180) / 360;
+}
+
+function mercatorY(lat: number) {
+ const sin = Math.sin((Math.max(-85.0511, Math.min(85.0511, lat)) * Math.PI) / 180);
+ return 0.5 - Math.log((1 + sin) / (1 - sin)) / (4 * Math.PI);
+}
+
+function emptyBBox(): [number, number, number, number] {
+ return [Infinity, Infinity, -Infinity, -Infinity];
+}
+
+function extend(bbox: [number, number, number, number], other: Rect) {
+ bbox[0] = Math.min(bbox[0], other[0]);
+ bbox[1] = Math.min(bbox[1], other[1]);
+ bbox[2] = Math.max(bbox[2], other[2]);
+ bbox[3] = Math.max(bbox[3], other[3]);
+}
+
+function intersects(a: Rect, b: Rect) {
+ return a[0] <= b[2] && b[0] <= a[2] && a[1] <= b[3] && b[1] <= a[3];
+}
+
+function projectCoverage(polygons: number[][][][]): Coverage {
+ const coverage: Coverage = { polygons: [], bbox: emptyBBox() };
+ for (const polygon of polygons) {
+ const projected: Polygon = { rings: [], bbox: emptyBBox() };
+ for (const ring of polygon) {
+ const array = new Float64Array(ring.length * 2);
+ ring.forEach(([lon, lat], i) => {
+ array[2 * i] = mercatorX(lon);
+ array[2 * i + 1] = mercatorY(lat);
+ extend(projected.bbox, [
+ array[2 * i],
+ array[2 * i + 1],
+ array[2 * i],
+ array[2 * i + 1],
+ ]);
+ });
+ projected.rings.push(array);
+ }
+ coverage.polygons.push(projected);
+ extend(coverage.bbox, projected.bbox);
+ }
+ return coverage;
+}
+
+function pointInPolygon(x: number, y: number, polygon: Polygon) {
+ let inside = false;
+ for (const ring of polygon.rings) {
+ for (let i = 0, j = ring.length - 2; i < ring.length; j = i, i += 2) {
+ const xi = ring[i],
+ yi = ring[i + 1],
+ xj = ring[j],
+ yj = ring[j + 1];
+ if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) {
+ inside = !inside;
+ }
+ }
+ }
+ return inside;
+}
+
+/** Whether the segment touches the rectangle (Liang–Barsky). */
+function segmentIntersectsRect(
+ x0: number,
+ y0: number,
+ x1: number,
+ y1: number,
+ [minX, minY, maxX, maxY]: Rect
+) {
+ let t0 = 0,
+ t1 = 1;
+ const dx = x1 - x0,
+ dy = y1 - y0;
+ const p = [-dx, dx, -dy, dy];
+ const q = [x0 - minX, maxX - x0, y0 - minY, maxY - y0];
+ for (let i = 0; i < 4; i++) {
+ if (p[i] === 0) {
+ if (q[i] < 0) return false;
+ } else {
+ const t = q[i] / p[i];
+ if (p[i] < 0) {
+ if (t > t1) return false;
+ t0 = Math.max(t0, t);
+ } else {
+ if (t < t0) return false;
+ t1 = Math.min(t1, t);
+ }
+ }
+ }
+ return true;
+}
+
+type CoverageRelation = { relation: 'full' | 'partial' | 'none'; polygons: Polygon[] };
+
+function relate(coverage: Coverage, rect: Rect): CoverageRelation {
+ if (!intersects(coverage.bbox, rect)) return { relation: 'none', polygons: [] };
+ const touching: Polygon[] = [];
+ let crossed = false;
+ for (const polygon of coverage.polygons) {
+ if (!intersects(polygon.bbox, rect)) continue;
+ let polygonCrossed = false;
+ for (const ring of polygon.rings) {
+ for (let i = 2; i < ring.length && !polygonCrossed; i += 2) {
+ if (segmentIntersectsRect(ring[i - 2], ring[i - 1], ring[i], ring[i + 1], rect)) {
+ polygonCrossed = true;
+ }
+ }
+ if (polygonCrossed) break;
+ }
+ if (polygonCrossed) {
+ crossed = true;
+ touching.push(polygon);
+ } else if (pointInPolygon((rect[0] + rect[2]) / 2, (rect[1] + rect[3]) / 2, polygon)) {
+ // No border inside the tile, and its center is inside: the whole tile is
+ return { relation: 'full', polygons: [polygon] };
+ }
+ }
+ return crossed
+ ? { relation: 'partial', polygons: touching }
+ : { relation: 'none', polygons: [] };
+}
+
+// Coverage, loaded with the first tile
+
+let coverages: Promise