mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-11-30 09:34:29 +00:00
use border tiles to cut patchwork precisely
This commit is contained in:
1 parent
657cf8786c
commit
4d4804203f
10 files changed
+282
-273
No files matched your search
@@ -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}`]);
|
||||
});
|
||||
});
|
||||
@@ -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 = [
|
||||
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a>',
|
||||
].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<ArrayBuffer | null>;
|
||||
|
||||
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<ArrayBuffer | null> {
|
||||
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<string, Promise<ArrayBuffer | null>>();
|
||||
|
||||
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<number> }> | 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<Map<string, Coverage>> | null = null;
|
||||
|
||||
function getCoverages() {
|
||||
coverages ??= import('$lib/data/satellite-coverage.json').then((module) => {
|
||||
const data = module.default as unknown as Record<string, number[][][][]>;
|
||||
const result = new Map<string, Coverage>();
|
||||
for (const source of sources) {
|
||||
result.set(source.id, projectCoverage(data[source.id] ?? []));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
return coverages;
|
||||
}
|
||||
|
||||
// Tiles
|
||||
// Geometry, in pixels of the tile of imagery
|
||||
|
||||
const TILE_SIZE = 256;
|
||||
const TILE_AREA = TILE_SIZE * TILE_SIZE;
|
||||
|
||||
type Ring = [number, number][];
|
||||
|
||||
/** The ring cut to the tile (Sutherland–Hodgman), which keeps its orientation. */
|
||||
function clipRing(ring: Ring): Ring {
|
||||
let points = ring;
|
||||
const edges: [(p: [number, number]) => number, number][] = [
|
||||
[(p) => p[0], 0],
|
||||
[(p) => -p[0], -TILE_SIZE],
|
||||
[(p) => p[1], 0],
|
||||
[(p) => -p[1], -TILE_SIZE],
|
||||
];
|
||||
for (const [value, min] of edges) {
|
||||
if (points.length === 0) break;
|
||||
const clipped: Ring = [];
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
const current = points[i];
|
||||
const previous = points[(i + points.length - 1) % points.length];
|
||||
const inCurrent = value(current) >= min;
|
||||
const inPrevious = value(previous) >= min;
|
||||
if (inCurrent !== inPrevious) {
|
||||
const t = (min - value(previous)) / (value(current) - value(previous));
|
||||
clipped.push([
|
||||
previous[0] + t * (current[0] - previous[0]),
|
||||
previous[1] + t * (current[1] - previous[1]),
|
||||
]);
|
||||
}
|
||||
if (inCurrent) clipped.push(current);
|
||||
}
|
||||
points = clipped;
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
/** Positive for an outer ring of a vector tile, negative for a hole. */
|
||||
function signedArea(ring: Ring) {
|
||||
let area = 0;
|
||||
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
||||
area += ring[j][0] * ring[i][1] - ring[i][0] * ring[j][1];
|
||||
}
|
||||
return area / 2;
|
||||
}
|
||||
|
||||
/** The polygons of the countries with imagery in the tile, cut to it, with their area. */
|
||||
function countriesInTile(
|
||||
bytes: ArrayBuffer,
|
||||
z: number,
|
||||
x: number,
|
||||
y: number
|
||||
): { source: ImagerySource; rings: Ring[]; area: number }[] {
|
||||
const layer = new VectorTile(new PbfReader(bytes)).layers.countries;
|
||||
if (!layer) return [];
|
||||
const d = z - COUNTRIES_ZOOM;
|
||||
const scale = (TILE_SIZE * 2 ** d) / layer.extent;
|
||||
const offsetX = (x - ((x >> d) << d)) * TILE_SIZE;
|
||||
const offsetY = (y - ((y >> d) << d)) * TILE_SIZE;
|
||||
const countries = [];
|
||||
for (let i = 0; i < layer.length; i++) {
|
||||
const feature = layer.feature(i);
|
||||
const source = sourceByCountry.get(String(feature.properties.country));
|
||||
if (!source) continue;
|
||||
const rings = feature
|
||||
.loadGeometry()
|
||||
.map((ring) =>
|
||||
clipRing(ring.map((p) => [p.x * scale - offsetX, p.y * scale - offsetY]))
|
||||
)
|
||||
.filter((ring) => ring.length >= 3);
|
||||
const area = rings.reduce((sum, ring) => sum + signedArea(ring), 0);
|
||||
// less than a pixel: a polygon of the tile of the countries outside this one
|
||||
if (area >= 1) countries.push({ source, rings, area });
|
||||
}
|
||||
return countries;
|
||||
}
|
||||
|
||||
// Imagery
|
||||
|
||||
async function fetchBytes(url: string, signal: AbortSignal): Promise<ArrayBuffer | null> {
|
||||
try {
|
||||
@@ -270,13 +250,39 @@ async function fetchBytes(url: string, signal: AbortSignal): Promise<ArrayBuffer
|
||||
if (!response.ok) return null;
|
||||
const contentType = response.headers.get('content-type') ?? '';
|
||||
if (!contentType.startsWith('image/')) return null;
|
||||
return await response.arrayBuffer();
|
||||
const bytes = await response.arrayBuffer();
|
||||
return (await isBlank(bytes)) ? null : bytes;
|
||||
} catch (e) {
|
||||
if (signal.aborted) throw e;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the image is all white or transparent: what some agencies give where they have no
|
||||
* imagery, such as IGN on the French part of Lake Geneva. Such an image is small, the others
|
||||
* are not even decoded.
|
||||
*/
|
||||
async function isBlank(bytes: ArrayBuffer) {
|
||||
if (bytes.byteLength > 4096) return false;
|
||||
try {
|
||||
const image = await createImageBitmap(new Blob([bytes]));
|
||||
const canvas = new OffscreenCanvas(16, 16);
|
||||
const context = canvas.getContext('2d')!;
|
||||
context.drawImage(image, 0, 0, 16, 16);
|
||||
image.close();
|
||||
const pixels = context.getImageData(0, 0, 16, 16).data;
|
||||
for (let i = 0; i < pixels.length; i += 4) {
|
||||
const transparent = pixels[i + 3] < 8;
|
||||
const white = pixels[i] >= 250 && pixels[i + 1] >= 250 && pixels[i + 2] >= 250;
|
||||
if (!transparent && !white) return false;
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** The image of the tile, or of the part of its ancestor at the max zoom of the source. */
|
||||
async function fetchImage(
|
||||
url: TileUrl,
|
||||
@@ -303,47 +309,16 @@ async function fetchImage(
|
||||
}
|
||||
}
|
||||
|
||||
function clipTo(
|
||||
context: OffscreenCanvasRenderingContext2D,
|
||||
polygons: Polygon[],
|
||||
z: number,
|
||||
x: number,
|
||||
y: number
|
||||
) {
|
||||
const scale = 2 ** z * TILE_SIZE;
|
||||
function clipTo(context: OffscreenCanvasRenderingContext2D, rings: Ring[]) {
|
||||
context.beginPath();
|
||||
for (const polygon of polygons) {
|
||||
for (const ring of polygon.rings) {
|
||||
for (let i = 0; i < ring.length; i += 2) {
|
||||
const px = ring[i] * scale - x * TILE_SIZE;
|
||||
const py = ring[i + 1] * scale - y * TILE_SIZE;
|
||||
if (i === 0) context.moveTo(px, py);
|
||||
else context.lineTo(px, py);
|
||||
}
|
||||
context.closePath();
|
||||
}
|
||||
for (const ring of rings) {
|
||||
ring.forEach(([px, py], i) => (i === 0 ? context.moveTo(px, py) : context.lineTo(px, py)));
|
||||
context.closePath();
|
||||
}
|
||||
context.clip('evenodd');
|
||||
}
|
||||
|
||||
/** Whether every point of a grid over the tile is in one of the partial coverages. */
|
||||
function coveredBy(partials: CoverageRelation[], rect: Rect) {
|
||||
const steps = 6;
|
||||
for (let i = 0; i < steps; i++) {
|
||||
for (let j = 0; j < steps; j++) {
|
||||
const px = rect[0] + ((i + 0.5) / steps) * (rect[2] - rect[0]);
|
||||
const py = rect[1] + ((j + 0.5) / steps) * (rect[3] - rect[1]);
|
||||
if (
|
||||
!partials.some((p) => p.polygons.some((polygon) => pointInPolygon(px, py, polygon)))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
type TilePart = CoverageRelation & { source: ImagerySource };
|
||||
type TilePart = { source: ImagerySource; relation: 'full' | 'partial'; rings: Ring[] };
|
||||
|
||||
/**
|
||||
* The sources of the tile, in drawing order: the one covering the whole tile, or those
|
||||
@@ -352,22 +327,28 @@ type TilePart = CoverageRelation & { source: ImagerySource };
|
||||
export async function planTile(
|
||||
z: number,
|
||||
x: number,
|
||||
y: number
|
||||
y: number,
|
||||
tiles: CountryTiles = fetchCountryTile
|
||||
): Promise<{ parts: TilePart[]; fallback: boolean }> {
|
||||
if (z <= world.maxzoom) return { parts: [], fallback: true };
|
||||
const n = 2 ** z;
|
||||
const rect: Rect = [x / n, y / n, (x + 1) / n, (y + 1) / n];
|
||||
const coverageBySource = await getCoverages();
|
||||
const parts: TilePart[] = [];
|
||||
for (const source of sources) {
|
||||
const relation = relate(coverageBySource.get(source.id)!, rect);
|
||||
if (relation.relation === 'full') {
|
||||
return { parts: [{ ...relation, source }], fallback: false };
|
||||
} else if (relation.relation === 'partial') {
|
||||
parts.push({ ...relation, source });
|
||||
}
|
||||
const none = { parts: [], fallback: true };
|
||||
if (z <= world.maxzoom) return none;
|
||||
const { zoom, tiles: withCountries } = await getAreas();
|
||||
const d = z - zoom;
|
||||
if (!withCountries.has((y >> d) * 2 ** zoom + (x >> d))) return none;
|
||||
|
||||
const bytes = await getCountryTile(x >> (z - COUNTRIES_ZOOM), y >> (z - COUNTRIES_ZOOM), tiles);
|
||||
if (!bytes) return none;
|
||||
const countries = countriesInTile(bytes, z, x, y);
|
||||
// a pixel of margin, for the rounding of the coordinates of the tiles
|
||||
const whole = countries.find((country) => country.area >= TILE_AREA - TILE_SIZE);
|
||||
if (whole) {
|
||||
return { parts: [{ source: whole.source, relation: 'full', rings: [] }], fallback: false };
|
||||
}
|
||||
return { parts, fallback: parts.length === 0 || !coveredBy(parts, rect) };
|
||||
const covered = countries.reduce((sum, country) => sum + country.area, 0);
|
||||
return {
|
||||
parts: countries.map(({ source, rings }) => ({ source, relation: 'partial', rings })),
|
||||
fallback: covered < TILE_AREA - TILE_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
export function maptilerSource(maptilerKey: string): ImagerySource {
|
||||
@@ -421,7 +402,7 @@ export async function loadTile(
|
||||
const result = images[i];
|
||||
if (!result) return;
|
||||
context.save();
|
||||
if (p.relation === 'partial') clipTo(context, p.polygons, z, x, y);
|
||||
if (p.relation === 'partial') clipTo(context, p.rings);
|
||||
const { image, sx, sy, size } = result;
|
||||
context.drawImage(image, sx, sy, size, size, 0, 0, TILE_SIZE, TILE_SIZE);
|
||||
context.restore();
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"zoom":10,"tiles":[332328,332329,332330,332331,332332,332333,332334,332335,332336,332337,332338,332339,332340,332341,332342,332343,332344,332345,332346,332347,332348,332349,332350,332351,332352,332353,332354,332355,332356,333352,333353,333354,333355,333356,333357,333358,333359,333360,333361,333362,333363,333364,333365,333366,333367,333368,333369,333370,333371,333372,333373,333374,333375,333376,333377,333378,333379,333380,334376,334377,334378,334379,334380,334381,334382,334383,334384,334385,334386,334387,334388,334389,334390,334391,334392,334393,334394,334395,334396,334397,334398,334399,334400,334401,334402,334403,334404,335400,335401,335402,335403,335404,335405,335406,335407,335408,335409,335410,335411,335412,335413,335414,335415,335416,335417,335418,335419,335420,335421,335422,335423,335424,335425,335426,335427,335428,336424,336425,336426,336427,336428,336429,336430,336431,336432,336433,336434,336435,336436,336437,336438,336439,336440,336441,336442,336443,336444,336445,336446,336447,336448,336449,336450,336451,336452,337448,337449,337450,337451,337452,337453,337454,337455,337456,337457,337458,337459,337460,337461,337462,337463,337464,337465,337466,337467,337468,337469,337470,337471,337472,337473,337474,337475,337476,338450,338472,338473,338474,338475,338476,338477,338478,338479,338480,338481,338482,338483,338484,338485,338486,338487,338488,338489,338490,338491,338492,338493,338494,338495,338496,338497,338498,338499,338500,339465,339466,339467,339468,339469,339470,339471,339472,339473,339474,339475,339476,339496,339497,339498,339499,339500,339501,339502,339503,339504,339505,339506,339507,339508,339509,339510,339511,339512,339513,339514,339515,339516,339517,339518,339519,339520,339521,339522,339523,339524,340489,340490,340491,340492,340493,340494,340495,340496,340497,340498,340499,340500,340520,340521,340522,340523,340524,340525,340526,340527,340528,340529,340530,340531,340532,340533,340534,340535,340536,340537,340538,340539,340540,340541,340542,340543,340544,340545,340546,340547,340548,341513,341514,341515,341516,341517,341518,341519,341520,341521,341522,341523,341524,341544,341545,341546,341547,341548,341549,341550,341551,341552,341553,341554,341555,341556,341557,341558,341559,341560,341561,341562,341563,341564,341565,341566,341567,341568,341569,341570,341571,341572,342537,342538,342539,342540,342541,342542,342543,342544,342545,342546,342547,342548,342568,342569,342570,342571,342572,342573,342574,342575,342576,342577,342578,342579,342580,342581,342582,342583,342584,342585,342586,342587,342588,342589,342590,342591,342592,342593,342594,342595,342596,343561,343562,343563,343564,343565,343566,343567,343568,343569,343570,343571,343572,343592,343593,343594,343595,343596,343597,343598,343599,343600,343601,343602,343603,343604,343605,343606,343607,343608,343609,343610,343611,343612,343613,343614,343615,343616,343617,343618,343619,343620,344585,344586,344587,344588,344589,344590,344591,344592,344593,344594,344595,344596,344616,344617,344618,344619,344620,344621,344622,344623,344624,344625,344626,344627,344628,344629,344630,344631,344632,344633,344634,344635,344636,344637,344638,344639,344640,344641,344642,344643,344644,345609,345610,345611,345612,345613,345614,345615,345616,345617,345618,345619,345620,345640,345641,345642,345643,345644,345645,345646,345647,345648,345649,345650,345651,345652,345653,345654,345655,345656,345657,345658,345659,345660,345661,345662,345663,345664,345665,345666,345667,345668,346633,346634,346635,346636,346637,346638,346639,346640,346641,346642,346643,346644,346664,346665,346666,346667,346668,346669,346670,346671,346672,346673,346674,346675,346676,346677,346678,346679,346680,346681,346682,346683,346684,346685,346686,346687,346688,346689,346690,346691,346692,347657,347658,347659,347660,347661,347662,347663,347664,347665,347666,347667,347668,347688,347689,347690,347691,347692,347693,347694,347695,347696,347697,347698,347699,347700,347701,347702,347703,347704,347705,347706,347707,347708,347709,347710,347711,347712,347713,347714,347715,347716,348679,348680,348681,348682,348683,348684,348685,348686,348687,348688,348689,348690,348691,348692,348712,348713,348714,348715,348716,348717,348718,348719,348720,348721,348722,348723,348724,348725,348726,348727,348728,348729,348730,348731,348732,348733,348734,348735,348736,348737,348738,348739,348740,349703,349704,349705,349706,349707,349708,349709,349710,349711,349712,349713,349714,349715,349716,349736,349737,349738,349739,349740,349741,349742,349743,349744,349745,349746,349747,349748,349749,349750,349751,349752,349753,349754,349755,349756,349757,349758,349759,349760,349761,349762,349763,349764,350706,350707,350708,350709,350710,350711,350712,350713,350714,350715,350716,350717,350718,350719,350720,350721,350722,350723,350724,350725,350726,350727,350728,350729,350730,350731,350732,350733,350734,350735,350736,350737,350738,350739,350740,350741,350742,350743,350754,350755,350756,350757,350758,350759,350760,350761,350762,350763,350764,350765,350766,350767,35Line truncated
|
||||
@@ -1 +0,0 @@
|
||||
{"fr":[[[[-54.1115,2.1143],[-54.1349,2.1107],[-54.1481,2.1144],[-54.1574,2.1217],[-54.1885,2.1613],[-54.1945,2.1631],[-54.21,2.1616],[-54.2638,2.1482],[-54.314,2.1549],[-54.3229,2.1573],[-54.3351,2.1639],[-54.3609,2.1882],[-54.3739,2.1965],[-54.3805,2.1977],[-54.3878,2.1963],[-54.4025,2.1971],[-54.434,2.2093],[-54.4766,2.2141],[-54.4927,2.2227],[-54.5114,2.2434],[-54.5458,2.2686],[-54.5491,2.2794],[-54.5264,2.2827],[-54.5443,2.3002],[-54.5646,2.3148],[-54.5882,2.3244],[-54.6153,2.3263],[-54.5992,2.3458],[-54.5848,2.3483],[-54.5512,2.3381],[-54.5316,2.3402],[-54.5203,2.3489],[-54.4934,2.4171],[-54.4726,2.4287],[-54.4505,2.4289],[-54.4355,2.4311],[-54.4235,2.4359],[-54.3756,2.4838],[-54.3594,2.508],[-54.3204,2.6067],[-54.2853,2.678],[-54.2125,2.7764],[-54.2047,2.7918],[-54.1889,2.8383],[-54.1822,2.8465],[-54.1702,2.8531],[-54.1896,2.8706],[-54.1913,2.8773],[-54.1907,2.8878],[-54.187,2.8993],[-54.1702,2.9254],[-54.163,2.956],[-54.1634,2.9697],[-54.1688,2.9714],[-54.1798,2.9837],[-54.1851,2.9978],[-54.1736,3.0045],[-54.1692,3.0107],[-54.1709,3.0248],[-54.177,3.0489],[-54.1907,3.0591],[-54.1885,3.0646],[-54.177,3.0728],[-54.1747,3.0939],[-54.1771,3.1166],[-54.188,3.131],[-54.2112,3.1274],[-54.1905,3.1781],[-54.1766,3.2006],[-54.14,3.245],[-54.1143,3.2854],[-54.0802,3.3097],[-54.0695,3.3271],[-54.0627,3.3467],[-54.0603,3.3642],[-54.0557,3.3777],[-54.0194,3.4154],[-54.003,3.4554],[-54.0063,3.531],[-53.9983,3.573],[-53.9903,3.5957],[-53.9888,3.611],[-53.9948,3.6236],[-54.0088,3.6379],[-54.0179,3.6415],[-54.0501,3.6345],[-54.0507,3.6406],[-54.074,3.6761],[-54.0944,3.7471],[-54.1048,3.758],[-54.1255,3.7887],[-54.1361,3.7957],[-54.1702,3.8058],[-54.1874,3.8147],[-54.1975,3.8269],[-54.2145,3.8582],[-54.218,3.8576],[-54.2222,3.8662],[-54.2411,3.8764],[-54.2658,3.9225],[-54.2865,3.9407],[-54.2931,3.9498],[-54.3039,3.9845],[-54.3102,3.9942],[-54.3444,4.0249],[-54.3539,4.0417],[-54.3552,4.0665],[-54.351,4.0821],[-54.3376,4.1166],[-54.3347,4.1317],[-54.3386,4.1523],[-54.3489,4.1605],[-54.3825,4.1696],[-54.3949,4.1779],[-54.4066,4.1915],[-54.4106,4.2086],[-54.3996,4.2273],[-54.3932,4.2444],[-54.3941,4.2698],[-54.403,4.3129],[-54.4334,4.3764],[-54.4508,4.4842],[-54.448,4.5095],[-54.4282,4.5462],[-54.4235,4.5634],[-54.4235,4.6043],[-54.4253,4.6161],[-54.4377,4.6487],[-54.4386,4.6733],[-54.4351,4.6924],[-54.4355,4.7094],[-54.4473,4.7279],[-54.457,4.7333],[-54.4673,4.7362],[-54.4754,4.7417],[-54.4787,4.7552],[-54.4787,4.8787],[-54.485,4.8928],[-54.4867,4.903],[-54.4821,4.9128],[-54.4555,4.9314],[-54.4508,4.9367],[-54.4488,4.9515],[-54.4576,4.9913],[-54.4548,5.0085],[-54.4486,5.0243],[-54.4302,5.0534],[-54.3756,5.1149],[-54.3723,5.1211],[-54.3699,5.1324],[-54.3654,5.1388],[-54.35,5.1496],[-54.3436,5.1564],[-54.332,5.1962],[-54.3113,5.2258],[-54.2692,5.2691],[-54.1905,5.3257],[-54.171,5.3484],[-54.1375,5.3601],[-54.1067,5.3812],[-54.0804,5.4079],[-54.0611,5.4365],[-54.0331,5.5054],[-54.013,5.5406],[-54.0072,5.5582],[-53.9919,5.6613],[-53.9444,5.7446],[-53.934,5.7448],[-53.8589,5.7338],[-53.8157,5.7159],[-53.8083,5.7148],[-53.8114,5.7206],[-53.8324,5.7317],[-53.8245,5.7332],[-53.8103,5.7311],[-53.7767,5.7142],[-53.7324,5.6842],[-53.6956,5.6635],[-53.5826,5.6178],[-53.5471,5.5999],[-53.5173,5.5808],[-53.4952,5.5721],[-53.4482,5.5609],[-53.4078,5.5618],[-53.3027,5.5355],[-53.2775,5.5382],[-53.2639,5.5299],[-53.2477,5.5098],[-53.2301,5.4953],[-53.2079,5.5023],[-53.2078,5.514],[-53.2252,5.5171],[-53.2648,5.5458],[-53.2827,5.5503],[-53.2895,5.5489],[-53.2914,5.5547],[-53.2812,5.5619],[-53.2715,5.5604],[-53.2517,5.5486],[-53.2212,5.5363],[-53.1776,5.5244],[-53.1302,5.5048],[-53.0825,5.4722],[-53.0462,5.4597],[-53.0058,5.4508],[-53.0014,5.4586],[-52.9946,5.4576],[-52.9651,5.4439],[-52.8965,5.4057],[-52.8291,5.3419],[-52.7832,5.3169],[-52.7529,5.2674],[-52.6812,5.2104],[-52.5409,5.079],[-52.5261,5.0707],[-52.528,5.0579],[-52.5267,5.0536],[-52.505,5.0536],[-52.4797,5.0396],[-52.4023,4.9738],[-52.38,4.9368],[-52.389,4.8959],[-52.3707,4.9072],[-52.3451,4.9429],[-52.3237,4.9506],[-52.3,4.9451],[-52.2791,4.9312],[-52.2643,4.9125],[-52.2586,4.8925],[-52.2765,4.8593],[-52.3131,4.8108],[-52.343,4.7612],[-52.3405,4.7246],[-52.3343,4.7246],[-52.3317,4.7383],[-52.3172,4.7552],[-52.3138,4.7619],[-52.3086,4.7873],[-52.3002,4.8134],[-52.2858,4.8296],[-52.2427,4.8562],[-52.2319,4.8754],[-52.2135,4.8667],[-52.1929,4.8497],[-52.1761,4.8292],[-52.1693,4.81],[-52.1595,4.7956],[-52.0941,4.7581],[-52.0602,4.7305],[-52.0245,4.6935],[-51.9963,4.6523],[-51.9849,4.6113],[-51.9796,4.5714],[-51.956,4.494],[-51.9508,4.454],[-51.9618,4.4084],[-51.9884,4.3857],[-52.0199,4.367],[-52.0463,4.3336],[-51.9856,4.3526],[-51.9514,4.3707],[-51.9365,4.3919],[-51.9249,4.4293],[-51.9228,4.4471],[-51.9313,4.5904],[-51.9254,4.6262],[-51.9092,4.6557],[-51.8964,4.6647],[-51.8812,4.6678],[-51.8648,4.6648],[-51.8483,4.6557],[-51.8015,4.6127],[-51.7931,4.6011],[-51.8008,4.5897],[-51.7735,4.552],[-51.7651,4.529],[-51.7636,4.5061],[-51.758,4.4778],[-51.7475,4.4572],[-51.731,4.4571],[-51.7Line truncated
|
||||
Reference in new issue
Block a user