diff --git a/website/package-lock.json b/website/package-lock.json
index a2da57f4f..442bdf0e1 100644
--- a/website/package-lock.json
+++ b/website/package-lock.json
@@ -26,6 +26,7 @@
"@fontsource-variable/inter": "^5.2.8",
"@internationalized/date": "^3.12.0",
"@lucide/svelte": "^1.7.0",
+ "@mapbox/vector-tile": "^3.0.0",
"@sveltejs/adapter-static": "^3.0.8",
"@sveltejs/enhanced-img": "^0.6.0",
"@sveltejs/kit": "^2.21.2",
@@ -56,6 +57,7 @@
"mdsvex": "^0.12.6",
"mode-watcher": "^1.1.0",
"paneforge": "^1.0.0-next.5",
+ "pbf": "^5.2.1",
"postcss": "^8.4.47",
"prettier": "^3.5.3",
"prettier-plugin-svelte": "^3.4.0",
@@ -1749,18 +1751,6 @@
"pbf": "^5.0.0"
}
},
- "node_modules/@mapbox/vector-tile/node_modules/pbf": {
- "version": "5.1.2",
- "resolved": "https://registry.npmjs.org/pbf/-/pbf-5.1.2.tgz",
- "integrity": "sha512-mnvGdvOrIvJOBGUEdGkrVXjN8E/VkIJCkf2eS1DH2yv82ORUlLttmDt0rWY38yYZmVwciZwBUvHM20qxBZf40w==",
- "license": "BSD-3-Clause",
- "dependencies": {
- "resolve-protobuf-schema": "^2.1.0"
- },
- "bin": {
- "pbf": "bin/pbf"
- }
- },
"node_modules/@maplibre/geojson-vt": {
"version": "6.3.0",
"resolved": "https://registry.npmjs.org/@maplibre/geojson-vt/-/geojson-vt-6.3.0.tgz",
@@ -1826,18 +1816,6 @@
"pbf": "^5.1.2"
}
},
- "node_modules/@maplibre/vt-pbf/node_modules/pbf": {
- "version": "5.1.2",
- "resolved": "https://registry.npmjs.org/pbf/-/pbf-5.1.2.tgz",
- "integrity": "sha512-mnvGdvOrIvJOBGUEdGkrVXjN8E/VkIJCkf2eS1DH2yv82ORUlLttmDt0rWY38yYZmVwciZwBUvHM20qxBZf40w==",
- "license": "BSD-3-Clause",
- "dependencies": {
- "resolve-protobuf-schema": "^2.1.0"
- },
- "bin": {
- "pbf": "bin/pbf"
- }
- },
"node_modules/@polka/url": {
"version": "1.0.0-next.29",
"dev": true,
@@ -4057,6 +4035,8 @@
},
"node_modules/ieee754": {
"version": "1.2.1",
+ "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
+ "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
"funding": [
{
"type": "github",
@@ -4673,6 +4653,19 @@
"version": "2.1.0",
"license": "(MIT AND Zlib)"
},
+ "node_modules/mapillary-js/node_modules/pbf": {
+ "version": "3.3.0",
+ "resolved": "https://registry.npmjs.org/pbf/-/pbf-3.3.0.tgz",
+ "integrity": "sha512-XDF38WCH3z5OV/OVa8GKUNtLAyneuzbCisx7QUCF8Q6Nutx0WnJrQe5O+kOtBlLfRNUws98Y58Lblp+NJG5T4Q==",
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "ieee754": "^1.1.12",
+ "resolve-protobuf-schema": "^2.1.0"
+ },
+ "bin": {
+ "pbf": "bin/pbf"
+ }
+ },
"node_modules/maplibre-gl": {
"version": "6.18.0",
"resolved": "https://registry.npmjs.org/maplibre-gl/-/maplibre-gl-6.18.0.tgz",
@@ -4712,18 +4705,6 @@
"integrity": "sha512-FHc6yNpnTORB0tM5kmZ03iD2mDuNakDE2lvf4Hw1XkfxtEHIGVi01B/GqUSwx+NcC1WGjidAfpTXzeALtPtfpg==",
"license": "ISC"
},
- "node_modules/maplibre-gl/node_modules/pbf": {
- "version": "5.1.2",
- "resolved": "https://registry.npmjs.org/pbf/-/pbf-5.1.2.tgz",
- "integrity": "sha512-mnvGdvOrIvJOBGUEdGkrVXjN8E/VkIJCkf2eS1DH2yv82ORUlLttmDt0rWY38yYZmVwciZwBUvHM20qxBZf40w==",
- "license": "BSD-3-Clause",
- "dependencies": {
- "resolve-protobuf-schema": "^2.1.0"
- },
- "bin": {
- "pbf": "bin/pbf"
- }
- },
"node_modules/martinez-polygon-clipping": {
"version": "0.7.4",
"license": "MIT",
@@ -5085,11 +5066,12 @@
}
},
"node_modules/pbf": {
- "version": "3.3.0",
+ "version": "5.2.1",
+ "resolved": "https://registry.npmjs.org/pbf/-/pbf-5.2.1.tgz",
+ "integrity": "sha512-5s6y6g3akGMF98eemb+3QZAWTCUHyox40TJOwcY4M1w40RvoXrfbRUVmY02RlRiEPyE6KVaM0xxfF4+4vjSJTQ==",
"license": "BSD-3-Clause",
"dependencies": {
- "ieee754": "^1.1.12",
- "resolve-protobuf-schema": "^2.1.0"
+ "protocol-buffers-schema": "^3.6.1"
},
"bin": {
"pbf": "bin/pbf"
@@ -5313,7 +5295,9 @@
"license": "MIT"
},
"node_modules/protocol-buffers-schema": {
- "version": "3.6.0",
+ "version": "3.6.1",
+ "resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.1.tgz",
+ "integrity": "sha512-VG2K63Igkiv9p76tk1lilczEK1cT+kCjKtkdhw1dQZV3k3IXJbd3o6Ho8b9zJZaHSnT2hKe4I+ObmX9w6m5SmQ==",
"license": "MIT"
},
"node_modules/punycode": {
@@ -5395,6 +5379,8 @@
},
"node_modules/resolve-protobuf-schema": {
"version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/resolve-protobuf-schema/-/resolve-protobuf-schema-2.1.0.tgz",
+ "integrity": "sha512-kI5ffTiZWmJaS/huM8wZfEMer1eRd7oJQhDuxeCLe3t7N7mX3z94CN0xPxBQxFYQTSNz9T0i+v6inKqSdK8xrQ==",
"license": "MIT",
"dependencies": {
"protocol-buffers-schema": "^3.3.1"
diff --git a/website/package.json b/website/package.json
index 6911ffff3..1c925d33f 100644
--- a/website/package.json
+++ b/website/package.json
@@ -21,6 +21,7 @@
"@fontsource-variable/inter": "^5.2.8",
"@internationalized/date": "^3.12.0",
"@lucide/svelte": "^1.7.0",
+ "@mapbox/vector-tile": "^3.0.0",
"@sveltejs/adapter-static": "^3.0.8",
"@sveltejs/enhanced-img": "^0.6.0",
"@sveltejs/kit": "^2.21.2",
@@ -51,6 +52,7 @@
"mdsvex": "^0.12.6",
"mode-watcher": "^1.1.0",
"paneforge": "^1.0.0-next.5",
+ "pbf": "^5.2.1",
"postcss": "^8.4.47",
"prettier": "^3.5.3",
"prettier-plugin-svelte": "^3.4.0",
diff --git a/website/src/lib/components/map/open-satellite.test.ts b/website/src/lib/components/map/open-satellite.test.ts
index 4f5455430..dcdc065a3 100644
--- a/website/src/lib/components/map/open-satellite.test.ts
+++ b/website/src/lib/components/map/open-satellite.test.ts
@@ -1,7 +1,25 @@
-// Which imagery each tile of the open satellite basemap is made of.
+// Which imagery each tile of the open satellite basemap is made of, from saved tiles of the
+// tileset of the countries.
import assert from 'node:assert/strict';
+import { readFileSync } from 'node:fs';
import { describe, it } from 'node:test';
-import { planTile } from './open-satellite';
+import { planTile, type CountryTiles } from './open-satellite';
+
+const fixtures = new URL('../../../test/fixtures/satellite-countries/', import.meta.url);
+
+/** The saved tiles, none where there is no file, and the ones that were asked for. */
+function savedTiles() {
+ const asked: string[] = [];
+ const tiles: CountryTiles = async (z, x, y) => {
+ asked.push(`${z}/${x}/${y}`);
+ try {
+ return new Uint8Array(readFileSync(new URL(`${z}-${x}-${y}.mvt`, fixtures))).buffer;
+ } catch {
+ return null;
+ }
+ };
+ return { asked, tiles };
+}
function tile(lon: number, lat: number, z: number): [number, number, number] {
const n = 2 ** z;
@@ -13,46 +31,68 @@ function tile(lon: number, lat: number, z: number): [number, number, number] {
];
}
-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 };
+async function plan([z, x, y]: [number, number, number], tiles: CountryTiles) {
+ const { parts, fallback } = await planTile(z, x, y, tiles);
+ return { parts: parts.map((p) => `${p.source.id}:${p.relation}`).sort(), 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 every tile up to zoom 12 from the imagery of the whole world', async () => {
- assert.deepEqual(await plan(2.35, 48.85, 12), { parts: [], fallback: true });
- assert.deepEqual(await plan(7.589, 47.5895, 12), { parts: [], fallback: true });
+ const { asked, tiles } = savedTiles();
+ assert.deepEqual(await plan(tile(2.35, 48.85, 12), tiles), { parts: [], fallback: true });
+ assert.deepEqual(asked, []);
});
- 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, 13), { parts: [], fallback: true });
+ it('takes a tile far from every country from the fallback, without asking for countries', async () => {
+ const { asked, tiles } = savedTiles();
+ // London, and the Atlantic
+ assert.deepEqual(await plan(tile(-0.12, 51.5, 14), tiles), { parts: [], fallback: true });
+ assert.deepEqual(await plan(tile(-30, 40, 13), tiles), { parts: [], fallback: true });
+ assert.deepEqual(asked, []);
});
- 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.78, 13), {
- 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'],
+ it('takes a tile inside a country from its agency alone', async () => {
+ const { tiles } = savedTiles();
+ assert.deepEqual(await plan(tile(2.35, 48.85, 15), tiles), {
+ parts: ['FR:full'],
fallback: false,
});
});
+
+ it('puts the countries of a border tile together, over the fallback where none is', async () => {
+ const { tiles } = savedTiles();
+ // Basel: France, Switzerland and Germany, which is not in the patchwork
+ assert.deepEqual(await plan([13, 4268, 2861], tiles), {
+ parts: ['CH:partial', 'FR:partial'],
+ fallback: true,
+ });
+ // only Germany
+ assert.deepEqual(await plan([13, 4269, 2860], tiles), { parts: [], fallback: true });
+ });
+
+ it('needs no fallback where the countries cover the tile between them', async () => {
+ const { tiles } = savedTiles();
+ // Wattrelos: France and Belgium
+ assert.deepEqual(await plan([13, 4166, 2750], tiles), {
+ parts: ['BE:partial', 'FR:partial'],
+ fallback: false,
+ });
+ // Baarle, with its enclaves of Belgium and the Netherlands
+ assert.deepEqual(await plan([13, 4208, 2726], tiles), {
+ parts: ['BE:partial', 'NL:partial'],
+ fallback: false,
+ });
+ });
+
+ it('asks for a tile of the countries once for all the tiles of imagery inside it', async () => {
+ const { asked, tiles } = savedTiles();
+ // around Lausanne, which no other test asks for
+ const [, x, y] = tile(6.63, 46.52, 12);
+ for (let dx = 0; dx < 4; dx++) {
+ for (let dy = 0; dy < 4; dy++) {
+ await planTile(14, x * 4 + dx, y * 4 + dy, tiles);
+ }
+ }
+ assert.deepEqual(asked, [`12/${x}/${y}`]);
+ });
});
diff --git a/website/src/lib/components/map/open-satellite.ts b/website/src/lib/components/map/open-satellite.ts
index ed74ae3e5..8802ecb18 100644
--- a/website/src/lib/components/map/open-satellite.ts
+++ b/website/src/lib/components/map/open-satellite.ts
@@ -1,4 +1,6 @@
import * as maplibregl from 'maplibre-gl';
+import { VectorTile } from '@mapbox/vector-tile';
+import { PbfReader } from 'pbf';
/**
* A satellite basemap made of the open imagery of national mapping agencies, drawn through a
@@ -12,7 +14,7 @@ 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 */
+ /** ISO code of the country, in the tileset of the countries */
id: string;
url: TileUrl;
/** Above it, the tile of this zoom level is scaled up. */
@@ -37,61 +39,61 @@ const world: ImagerySource = { id: 'world', url: ignOrthophotos, maxzoom: 12 };
/** In order of priority, the first one covering a point is used. */
const sources: ImagerySource[] = [
{
- id: 'ch',
+ id: 'CH',
url: xyz(
'https://wmts.geo.admin.ch/1.0.0/ch.swisstopo.swissimage/default/current/3857/{z}/{x}/{y}.jpeg'
),
maxzoom: 20,
},
{
- id: 'fr',
+ id: 'FR',
url: ignOrthophotos,
maxzoom: 19,
},
{
- id: 'es',
+ 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}'
),
maxzoom: 19,
},
{
- id: 'at',
+ id: 'AT',
url: xyz(
'https://mapsneu.wien.gv.at/basemap/bmaporthofoto30cm/normal/google3857/{z}/{y}/{x}.jpeg'
),
maxzoom: 19,
},
{
- id: 'nl',
+ id: 'NL',
url: xyz(
'https://service.pdok.nl/hwh/luchtfotorgb/wmts/v1_0/Actueel_orthoHR/EPSG:3857/{z}/{x}/{y}.jpeg'
),
maxzoom: 19,
},
{
- id: 'lu',
+ id: 'LU',
url: xyz(
'https://wmts1.geoportail.lu/opendata/wmts/ortho_latest/GLOBAL_WEBMERCATOR_4_V3/{z}/{x}/{y}.jpeg'
),
maxzoom: 19,
},
{
- id: 'cz',
+ id: 'CZ',
url: xyz(
'https://ags.cuzk.gov.cz/arcgis1/rest/services/ORTOFOTO_WM/MapServer/tile/{z}/{y}/{x}'
),
maxzoom: 19,
},
{
- id: 'be',
+ id: 'BE',
url: xyz(
'https://wmts.ngi.be/inspire/ortho/1.0.0/orthoimage_coverage/default/3857/latest/{z}/{y}/{x}.png'
),
maxzoom: 19,
},
{
- id: 'pl',
+ 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}'
),
@@ -112,157 +114,135 @@ export const openSatelliteAttribution = [
'© MapTiler',
].join(', ');
-// Geometry, in Web Mercator coordinates between 0 and 1
+// Countries, from the tileset of their polygons, built from the borders of OpenStreetMap
-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
+const COUNTRIES_URL = xyz('https://tiles.gpx.studio/satellite-countries/{z}/{x}/{y}.mvt');
+/** The only zoom level of the tileset. */
+const COUNTRIES_ZOOM = 12;
-function mercatorX(lon: number) {
- return (lon + 180) / 360;
-}
+const sourceByCountry = new Map(sources.map((source) => [source.id, source]));
-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);
-}
+/** The tiles of the countries, from their tileset: null where there is none. */
+export type CountryTiles = (z: number, x: number, y: number) => Promise;
-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);
+async function fetchCountryTile(z: number, x: number, y: number): Promise {
+ try {
+ const response = await fetch(COUNTRIES_URL(z, x, y));
+ if (!response.ok) return null;
+ const bytes = await response.arrayBuffer();
+ return bytes.byteLength > 0 ? bytes : null;
+ } catch {
+ return null;
}
- 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;
- }
- }
+/** A tile of the countries is shared by the 4^(z - 12) tiles of imagery inside it. */
+const countryTiles = new Map>();
+
+function getCountryTile(x: number, y: number, tiles: CountryTiles) {
+ const key = `${x}/${y}`;
+ let tile = countryTiles.get(key);
+ if (tile) {
+ // the last used, at the end of the map
+ countryTiles.delete(key);
+ } else {
+ tile = tiles(COUNTRIES_ZOOM, x, y);
+ if (countryTiles.size >= 256) countryTiles.delete(countryTiles.keys().next().value!);
}
- return inside;
+ countryTiles.set(key, tile);
+ return tile;
}
-/** 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;
+/** The tiles of zoom `zoom` where there is a country, as y * 2^zoom + x. */
+let areas: Promise<{ zoom: number; tiles: Set }> | null = null;
+
+function getAreas() {
+ areas ??= import('$lib/data/satellite-countries.json').then((module) => ({
+ zoom: module.default.zoom,
+ tiles: new Set(module.default.tiles),
+ }));
+ return areas;
}
-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