@celljs/ta-geo 0.0.0-stage → 4.31.0
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.
- package/README.en.md +13 -0
- package/README.md +12 -2
- package/esm/geo-elevation.d.ts +42 -0
- package/esm/geo-elevation.js +165 -0
- package/esm/geo-grid.d.ts +50 -0
- package/esm/geo-grid.js +90 -0
- package/esm/geo-mvt.d.ts +17 -0
- package/esm/geo-mvt.js +213 -0
- package/esm/geo-pixels.d.ts +27 -0
- package/esm/geo-pixels.js +138 -0
- package/esm/geo-places.d.ts +18 -0
- package/esm/geo-places.js +82 -0
- package/esm/geo-raster.d.ts +57 -0
- package/esm/geo-raster.js +259 -0
- package/esm/geo-tiles.d.ts +29 -0
- package/esm/geo-tiles.js +79 -0
- package/esm/geo-weather.d.ts +42 -0
- package/esm/geo-weather.js +99 -0
- package/esm/index.d.ts +8 -0
- package/esm/index.js +9 -0
- package/esm/package.json +3 -0
- package/lib/geo-elevation.d.ts +43 -0
- package/lib/geo-elevation.js +170 -0
- package/lib/geo-grid.d.ts +51 -0
- package/lib/geo-grid.js +93 -0
- package/lib/geo-mvt.d.ts +18 -0
- package/lib/geo-mvt.js +216 -0
- package/lib/geo-pixels.d.ts +28 -0
- package/lib/geo-pixels.js +141 -0
- package/lib/geo-places.d.ts +19 -0
- package/lib/geo-places.js +85 -0
- package/lib/geo-raster.d.ts +58 -0
- package/lib/geo-raster.js +262 -0
- package/lib/geo-tiles.d.ts +30 -0
- package/lib/geo-tiles.js +83 -0
- package/lib/geo-weather.d.ts +43 -0
- package/lib/geo-weather.js +103 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +25 -0
- package/package.json +45 -4
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { GeoRaster } from './geo-raster.js';
|
|
2
|
+
// A map tile as pixels (GeoPixels), for a map drawn as pixel art at any zoom: what covers each of its size × size
|
|
3
|
+
// pixels (land, water, woods, sand, ice, towns, roads) from the layers of a vector tile in the schema of OpenMapTiles,
|
|
4
|
+
// and the names of the places on it, where they are. A tile the map has nothing of is the open sea.
|
|
5
|
+
export const MapPixel = {
|
|
6
|
+
LAND: 0,
|
|
7
|
+
WATER: 1,
|
|
8
|
+
WOOD: 2,
|
|
9
|
+
GRASS: 3,
|
|
10
|
+
SAND: 4,
|
|
11
|
+
ICE: 5,
|
|
12
|
+
TOWN: 6,
|
|
13
|
+
ROAD: 7,
|
|
14
|
+
MAJOR: 8,
|
|
15
|
+
};
|
|
16
|
+
const COVER = {
|
|
17
|
+
wood: MapPixel.WOOD,
|
|
18
|
+
forest: MapPixel.WOOD,
|
|
19
|
+
grass: MapPixel.GRASS,
|
|
20
|
+
farmland: MapPixel.GRASS,
|
|
21
|
+
wetland: MapPixel.GRASS,
|
|
22
|
+
sand: MapPixel.SAND,
|
|
23
|
+
beach: MapPixel.SAND,
|
|
24
|
+
ice: MapPixel.ICE,
|
|
25
|
+
glacier: MapPixel.ICE,
|
|
26
|
+
};
|
|
27
|
+
const TOWNS = new Set(['residential', 'commercial', 'industrial', 'retail', 'suburb', 'neighbourhood', 'quarter']);
|
|
28
|
+
const MAJOR = new Set(['motorway', 'trunk']);
|
|
29
|
+
const ROADS = new Set(['primary', 'secondary']);
|
|
30
|
+
const PLACES = { continent: -2, country: -1, state: 1, city: 2, town: 4, village: 6, suburb: 7 };
|
|
31
|
+
/** Names in a script of their own, by the start of a language tag. */
|
|
32
|
+
const NAME_SCRIPTS = [
|
|
33
|
+
['zh-tw', ['name:zh-Hant']],
|
|
34
|
+
['zh-hk', ['name:zh-Hant']],
|
|
35
|
+
['zh-hant', ['name:zh-Hant']],
|
|
36
|
+
['zh', ['name:zh-Hans']],
|
|
37
|
+
];
|
|
38
|
+
export var GeoPixels;
|
|
39
|
+
(function (GeoPixels) {
|
|
40
|
+
/** The pixels of a tile: `zoom` the zoom it is of (towns from 9, motorways from 9, the smaller roads from 11). */
|
|
41
|
+
function tile(layers, zoom, size = 64) {
|
|
42
|
+
const out = new Uint8Array(size * size);
|
|
43
|
+
if (!layers) {
|
|
44
|
+
return out.fill(MapPixel.WATER);
|
|
45
|
+
}
|
|
46
|
+
const each = (name, run) => {
|
|
47
|
+
const layer = layers.get(name);
|
|
48
|
+
if (!layer) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const scale = size / layer.extent;
|
|
52
|
+
for (const feature of layer.features) {
|
|
53
|
+
run(feature.type, feature.properties, feature.geometry.map(part => part.map(([u, v]) => [u * scale, v * scale])));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const set = (kind) => (index) => {
|
|
57
|
+
out[index] = kind;
|
|
58
|
+
};
|
|
59
|
+
each('landcover', (type, properties, parts) => {
|
|
60
|
+
var _a;
|
|
61
|
+
const kind = (_a = COVER[String(properties.class)]) !== null && _a !== void 0 ? _a : COVER[String(properties.subclass)];
|
|
62
|
+
if (kind !== undefined && type === 3) {
|
|
63
|
+
GeoRaster.fill(parts, size, set(kind));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
if (zoom >= 9) {
|
|
67
|
+
each('landuse', (type, properties, parts) => {
|
|
68
|
+
if (type === 3 && TOWNS.has(String(properties.class))) {
|
|
69
|
+
GeoRaster.fill(parts, size, set(MapPixel.TOWN));
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
each('park', (type, _properties, parts) => {
|
|
74
|
+
if (type === 3 && zoom >= 8) {
|
|
75
|
+
GeoRaster.fill(parts, size, set(MapPixel.WOOD));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
each('water', (type, _properties, parts) => {
|
|
79
|
+
if (type === 3) {
|
|
80
|
+
GeoRaster.fill(parts, size, set(MapPixel.WATER));
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (zoom >= 8) {
|
|
84
|
+
each('waterway', (type, properties, parts) => {
|
|
85
|
+
if (type === 2 && (properties.class === 'river' || zoom >= 12)) {
|
|
86
|
+
for (const line of parts) {
|
|
87
|
+
GeoRaster.stroke(line, 1, size, set(MapPixel.WATER));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (zoom >= 9) {
|
|
93
|
+
each('transportation', (type, properties, parts) => {
|
|
94
|
+
const kind = String(properties.class);
|
|
95
|
+
const major = MAJOR.has(kind);
|
|
96
|
+
if (type !== 2 || properties.brunnel === 'tunnel' || !(major || (zoom >= 11 && ROADS.has(kind)) || (zoom >= 13 && kind === 'tertiary'))) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
for (const line of parts) {
|
|
100
|
+
GeoRaster.stroke(line, 1, size, set(major ? MapPixel.MAJOR : MapPixel.ROAD));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
GeoPixels.tile = tile;
|
|
107
|
+
/** The names of the places on a tile, in `language` where the map has it (`name:zh`), the most important first. */
|
|
108
|
+
function names(layers, language) {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
var _c;
|
|
111
|
+
const layer = layers === null || layers === void 0 ? void 0 : layers.get('place');
|
|
112
|
+
if (!layer) {
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
// The keys of names the map has for a language (OpenMapTiles): its own script first, then the language.
|
|
116
|
+
const lower = language.toLowerCase();
|
|
117
|
+
const script = (_c = (_a = NAME_SCRIPTS.find(([prefix]) => lower.startsWith(prefix))) === null || _a === void 0 ? void 0 : _a[1]) !== null && _c !== void 0 ? _c : [];
|
|
118
|
+
const keys = [...script, `name:${lower.slice(0, 2)}`];
|
|
119
|
+
const out = [];
|
|
120
|
+
for (const feature of layer.features) {
|
|
121
|
+
const point = (_b = feature.geometry[0]) === null || _b === void 0 ? void 0 : _b[0];
|
|
122
|
+
const kind = String(feature.properties.class);
|
|
123
|
+
const base = PLACES[kind];
|
|
124
|
+
if (feature.type !== 1 || !point || base === undefined) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const name = [...keys, 'name'].map(key => feature.properties[key]).find(value => typeof value === 'string' && value.trim());
|
|
128
|
+
if (typeof name !== 'string') {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const rank = base + (Number(feature.properties.rank) || 5) / 10 + (feature.properties.capital === 2 ? -1 : 0);
|
|
132
|
+
out.push({ name: name.trim(), x: point[0] / layer.extent, y: point[1] / layer.extent, rank, kind });
|
|
133
|
+
}
|
|
134
|
+
return out.sort((a, b) => a.rank - b.rank);
|
|
135
|
+
}
|
|
136
|
+
GeoPixels.names = names;
|
|
137
|
+
})(GeoPixels || (GeoPixels = {}));
|
|
138
|
+
//# sourceMappingURL=geo-pixels.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LonLat } from './geo-grid.js';
|
|
2
|
+
import type { Fetcher } from './geo-tiles.js';
|
|
3
|
+
export interface GeoPlace extends LonLat {
|
|
4
|
+
name: string;
|
|
5
|
+
/** Where it is: the rest of its address. */
|
|
6
|
+
detail: string;
|
|
7
|
+
}
|
|
8
|
+
export declare namespace GeoPlaces {
|
|
9
|
+
function search(query: string, language: string, fetcher?: Fetcher): Promise<GeoPlace[]>;
|
|
10
|
+
/** What a place is called: the name of what is there, or its street; none if the search knows nothing. */
|
|
11
|
+
function name(place: LonLat, language: string, fetcher?: Fetcher): Promise<GeoPlace | undefined>;
|
|
12
|
+
/**
|
|
13
|
+
* Where the device is, if the user lets the page know. It fails with `denied` (the user or the system said no),
|
|
14
|
+
* `unavailable` or `timeout`; never waits longer than `deadline`, as a browser that cannot ask (an app without
|
|
15
|
+
* leave to use the location) may never answer, and its own timeout only starts once it is allowed.
|
|
16
|
+
*/
|
|
17
|
+
function here(timeout?: number, deadline?: number): Promise<LonLat>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export var GeoPlaces;
|
|
2
|
+
(function (GeoPlaces) {
|
|
3
|
+
async function search(query, language, fetcher = (url, init) => fetch(url, init)) {
|
|
4
|
+
const text = query.trim();
|
|
5
|
+
if (!text) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
const url = `https://nominatim.openstreetmap.org/search?format=jsonv2&limit=6&q=${encodeURIComponent(text)}&accept-language=${encodeURIComponent(language)}`;
|
|
9
|
+
const response = await fetcher(url, { headers: { Accept: 'application/json' } });
|
|
10
|
+
if (!response.ok) {
|
|
11
|
+
throw new Error(`Search: HTTP ${response.status}`);
|
|
12
|
+
}
|
|
13
|
+
const found = (await response.json());
|
|
14
|
+
return found
|
|
15
|
+
.map(each => {
|
|
16
|
+
var _a;
|
|
17
|
+
const lat = Number(each.lat);
|
|
18
|
+
const lon = Number(each.lon);
|
|
19
|
+
const whole = (_a = each.display_name) !== null && _a !== void 0 ? _a : '';
|
|
20
|
+
const name = each.name || whole.split(',')[0] || text;
|
|
21
|
+
const detail = whole
|
|
22
|
+
.split(',')
|
|
23
|
+
.slice(1)
|
|
24
|
+
.map(part => part.trim())
|
|
25
|
+
.filter(Boolean)
|
|
26
|
+
.slice(0, 3)
|
|
27
|
+
.join(', ');
|
|
28
|
+
return { lat, lon, name, detail };
|
|
29
|
+
})
|
|
30
|
+
.filter(each => Number.isFinite(each.lat) && Number.isFinite(each.lon));
|
|
31
|
+
}
|
|
32
|
+
GeoPlaces.search = search;
|
|
33
|
+
/** What a place is called: the name of what is there, or its street; none if the search knows nothing. */
|
|
34
|
+
async function name(place, language, fetcher = (url, init) => fetch(url, init)) {
|
|
35
|
+
var _a, _b, _c;
|
|
36
|
+
var _d, _e, _f;
|
|
37
|
+
const url = `https://nominatim.openstreetmap.org/reverse?format=jsonv2&zoom=17&lat=${place.lat}&lon=${place.lon}&accept-language=${encodeURIComponent(language)}`;
|
|
38
|
+
const response = await fetcher(url, { headers: { Accept: 'application/json' } });
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
const found = (await response.json());
|
|
43
|
+
const whole = (_d = found.display_name) !== null && _d !== void 0 ? _d : '';
|
|
44
|
+
const street = (_f = (_e = (_a = found.address) === null || _a === void 0 ? void 0 : _a.road) !== null && _e !== void 0 ? _e : (_b = found.address) === null || _b === void 0 ? void 0 : _b.neighbourhood) !== null && _f !== void 0 ? _f : (_c = found.address) === null || _c === void 0 ? void 0 : _c.suburb;
|
|
45
|
+
const called = found.name || street || whole.split(',')[0];
|
|
46
|
+
if (!called) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const detail = whole
|
|
50
|
+
.split(',')
|
|
51
|
+
.map(part => part.trim())
|
|
52
|
+
.filter(part => part && part !== called)
|
|
53
|
+
.slice(0, 3)
|
|
54
|
+
.join(', ');
|
|
55
|
+
return { ...place, name: called, detail };
|
|
56
|
+
}
|
|
57
|
+
GeoPlaces.name = name;
|
|
58
|
+
/**
|
|
59
|
+
* Where the device is, if the user lets the page know. It fails with `denied` (the user or the system said no),
|
|
60
|
+
* `unavailable` or `timeout`; never waits longer than `deadline`, as a browser that cannot ask (an app without
|
|
61
|
+
* leave to use the location) may never answer, and its own timeout only starts once it is allowed.
|
|
62
|
+
*/
|
|
63
|
+
function here(timeout = 15000, deadline = 45000) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const geo = typeof navigator === 'undefined' ? undefined : navigator.geolocation;
|
|
66
|
+
if (!geo) {
|
|
67
|
+
reject(new Error('unavailable'));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const timer = setTimeout(() => reject(new Error('timeout')), deadline);
|
|
71
|
+
geo.getCurrentPosition(position => {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
resolve({ lon: position.coords.longitude, lat: position.coords.latitude });
|
|
74
|
+
}, error => {
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
reject(new Error(error.code === 1 ? 'denied' : error.code === 3 ? 'timeout' : 'unavailable'));
|
|
77
|
+
}, { enableHighAccuracy: false, timeout, maximumAge: 600000 });
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
GeoPlaces.here = here;
|
|
81
|
+
})(GeoPlaces || (GeoPlaces = {}));
|
|
82
|
+
//# sourceMappingURL=geo-places.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { MvtLayer } from './geo-mvt.js';
|
|
2
|
+
export declare const Ground: {
|
|
3
|
+
readonly LAND: 0;
|
|
4
|
+
readonly GRASS: 1;
|
|
5
|
+
readonly PARK: 2;
|
|
6
|
+
readonly WOOD: 3;
|
|
7
|
+
readonly FARM: 4;
|
|
8
|
+
readonly SAND: 5;
|
|
9
|
+
readonly WATER: 6;
|
|
10
|
+
readonly PAVED: 7;
|
|
11
|
+
readonly ROAD: 8;
|
|
12
|
+
readonly STREET: 9;
|
|
13
|
+
readonly PATH: 10;
|
|
14
|
+
readonly RAIL: 11;
|
|
15
|
+
readonly PITCH: 12;
|
|
16
|
+
};
|
|
17
|
+
export type GroundKind = (typeof Ground)[keyof typeof Ground];
|
|
18
|
+
export interface RasterBuilding {
|
|
19
|
+
/** Its top above the ground, and its bottom (a part on others, a bridge of a building), in metres. */
|
|
20
|
+
height: number;
|
|
21
|
+
min: number;
|
|
22
|
+
/** Its colour as the map has it (`#rrggbb`), if any. */
|
|
23
|
+
colour?: string;
|
|
24
|
+
/** How many columns of the plot it covers. */
|
|
25
|
+
area: number;
|
|
26
|
+
/** The middle of what it covers, in columns of the plot. */
|
|
27
|
+
x: number;
|
|
28
|
+
z: number;
|
|
29
|
+
}
|
|
30
|
+
export interface PlotRaster {
|
|
31
|
+
size: number;
|
|
32
|
+
/** The kind of ground of each column, by `z * size + x`. */
|
|
33
|
+
ground: Uint8Array;
|
|
34
|
+
/** The building on each column, by its index in `buildings` plus one; 0 none. */
|
|
35
|
+
building: Uint16Array;
|
|
36
|
+
buildings: RasterBuilding[];
|
|
37
|
+
}
|
|
38
|
+
/** Where in its tile a plot is: its corner and its side, fractions of the tile. */
|
|
39
|
+
export interface PlotWindow {
|
|
40
|
+
left: number;
|
|
41
|
+
top: number;
|
|
42
|
+
size: number;
|
|
43
|
+
}
|
|
44
|
+
type Ring = [number, number][];
|
|
45
|
+
export declare namespace GeoRaster {
|
|
46
|
+
/**
|
|
47
|
+
* The ground of a plot from the layers of the tile it is in: `window` where in the tile, `metres` the metres
|
|
48
|
+
* a column is across there (GeoGrid.metresPerColumn); `size` columns across, fewer for a map seen from far (each
|
|
49
|
+
* then as many metres more).
|
|
50
|
+
*/
|
|
51
|
+
function plot(layers: ReadonlyMap<string, MvtLayer> | undefined, window: PlotWindow, metres: number, size?: number): PlotRaster;
|
|
52
|
+
/** Runs `set` on each column whose middle is inside the rings (even-odd). */
|
|
53
|
+
function fill(rings: readonly Ring[], size: number, set: (index: number) => void): void;
|
|
54
|
+
/** Runs `set` on each column whose middle is within `width / 2` of the line. */
|
|
55
|
+
function stroke(line: Ring, width: number, size: number, set: (index: number) => void): void;
|
|
56
|
+
}
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { PLOT_SIZE } from './geo-grid.js';
|
|
2
|
+
// What is on the ground of a plot (GeoRaster), from the layers of a vector tile in the schema of OpenMapTiles: a
|
|
3
|
+
// kind of ground for each column (grass, a park, a wood, water, a road…), and the buildings, each a footprint of
|
|
4
|
+
// columns with its height in metres. Polygons fill the columns whose middle is in them (even-odd, so holes stay
|
|
5
|
+
// open); lines are as wide as what they are (a motorway, a footpath), in columns of the plot's latitude.
|
|
6
|
+
export const Ground = {
|
|
7
|
+
LAND: 0,
|
|
8
|
+
GRASS: 1,
|
|
9
|
+
PARK: 2,
|
|
10
|
+
WOOD: 3,
|
|
11
|
+
FARM: 4,
|
|
12
|
+
SAND: 5,
|
|
13
|
+
WATER: 6,
|
|
14
|
+
PAVED: 7,
|
|
15
|
+
ROAD: 8,
|
|
16
|
+
STREET: 9,
|
|
17
|
+
PATH: 10,
|
|
18
|
+
RAIL: 11,
|
|
19
|
+
PITCH: 12,
|
|
20
|
+
};
|
|
21
|
+
/** How wide each class of transportation is, in metres, and the ground it is. */
|
|
22
|
+
const WAYS = {
|
|
23
|
+
motorway: { metres: 18, ground: Ground.ROAD },
|
|
24
|
+
trunk: { metres: 16, ground: Ground.ROAD },
|
|
25
|
+
primary: { metres: 14, ground: Ground.ROAD },
|
|
26
|
+
secondary: { metres: 12, ground: Ground.ROAD },
|
|
27
|
+
tertiary: { metres: 10, ground: Ground.ROAD },
|
|
28
|
+
minor: { metres: 7, ground: Ground.STREET },
|
|
29
|
+
service: { metres: 4.5, ground: Ground.STREET },
|
|
30
|
+
track: { metres: 3, ground: Ground.PATH },
|
|
31
|
+
path: { metres: 2.5, ground: Ground.PATH },
|
|
32
|
+
rail: { metres: 4, ground: Ground.RAIL },
|
|
33
|
+
transit: { metres: 4, ground: Ground.RAIL },
|
|
34
|
+
pier: { metres: 4, ground: Ground.PAVED },
|
|
35
|
+
};
|
|
36
|
+
const WATERWAYS = { river: 10, canal: 8, stream: 2.5, drain: 1.5, ditch: 1.5 };
|
|
37
|
+
const LANDCOVER = {
|
|
38
|
+
wood: Ground.WOOD,
|
|
39
|
+
forest: Ground.WOOD,
|
|
40
|
+
farmland: Ground.FARM,
|
|
41
|
+
grass: Ground.GRASS,
|
|
42
|
+
wetland: Ground.GRASS,
|
|
43
|
+
sand: Ground.SAND,
|
|
44
|
+
beach: Ground.SAND,
|
|
45
|
+
};
|
|
46
|
+
const LANDUSE = {
|
|
47
|
+
commercial: Ground.PAVED,
|
|
48
|
+
retail: Ground.PAVED,
|
|
49
|
+
industrial: Ground.PAVED,
|
|
50
|
+
railway: Ground.PAVED,
|
|
51
|
+
pitch: Ground.PITCH,
|
|
52
|
+
playground: Ground.GRASS,
|
|
53
|
+
cemetery: Ground.GRASS,
|
|
54
|
+
stadium: Ground.PAVED,
|
|
55
|
+
garden: Ground.PARK,
|
|
56
|
+
};
|
|
57
|
+
/** The height of a building the map does not know, in metres: two storeys. */
|
|
58
|
+
const DEFAULT_HEIGHT = 6;
|
|
59
|
+
const MAX_BUILDINGS = 65535;
|
|
60
|
+
export var GeoRaster;
|
|
61
|
+
(function (GeoRaster) {
|
|
62
|
+
/**
|
|
63
|
+
* The ground of a plot from the layers of the tile it is in: `window` where in the tile, `metres` the metres
|
|
64
|
+
* a column is across there (GeoGrid.metresPerColumn); `size` columns across, fewer for a map seen from far (each
|
|
65
|
+
* then as many metres more).
|
|
66
|
+
*/
|
|
67
|
+
function plot(layers, window, metres, size = PLOT_SIZE) {
|
|
68
|
+
const across = metres * (PLOT_SIZE / size);
|
|
69
|
+
const ground = new Uint8Array(size * size);
|
|
70
|
+
const building = new Uint16Array(size * size);
|
|
71
|
+
const buildings = [];
|
|
72
|
+
if (!layers) {
|
|
73
|
+
// Nothing on the map: the open sea.
|
|
74
|
+
ground.fill(Ground.WATER);
|
|
75
|
+
return { size, ground, building, buildings };
|
|
76
|
+
}
|
|
77
|
+
const toPlot = (layer) => (part) => part.map(([u, v]) => [((u / layer.extent - window.left) / window.size) * size, ((v / layer.extent - window.top) / window.size) * size]);
|
|
78
|
+
const each = (name, run) => {
|
|
79
|
+
const layer = layers.get(name);
|
|
80
|
+
if (!layer) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const map = toPlot(layer);
|
|
84
|
+
for (const feature of layer.features) {
|
|
85
|
+
run(feature, feature.geometry.map(map));
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const set = (kind) => (i) => {
|
|
89
|
+
ground[i] = kind;
|
|
90
|
+
};
|
|
91
|
+
// What covers the land, then what is on it, the water, the ways over all of it, the buildings last.
|
|
92
|
+
each('landuse', (feature, parts) => {
|
|
93
|
+
const kind = LANDUSE[String(feature.properties.class)];
|
|
94
|
+
if (kind !== undefined && feature.type === 3) {
|
|
95
|
+
fill(parts, size, set(kind));
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
each('landcover', (feature, parts) => {
|
|
99
|
+
var _a;
|
|
100
|
+
const kind = (_a = LANDCOVER[String(feature.properties.class)]) !== null && _a !== void 0 ? _a : LANDCOVER[String(feature.properties.subclass)];
|
|
101
|
+
if (kind !== undefined && feature.type === 3) {
|
|
102
|
+
fill(parts, size, set(kind));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
each('park', (feature, parts) => {
|
|
106
|
+
const kind = String(feature.properties.class);
|
|
107
|
+
if (feature.type === 3 && kind !== 'national_park' && kind !== 'nature_reserve' && kind !== 'protected_area') {
|
|
108
|
+
fill(parts, size, set(Ground.PARK));
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
each('water', (feature, parts) => {
|
|
112
|
+
if (feature.type === 3 && feature.properties.brunnel !== 'tunnel') {
|
|
113
|
+
fill(parts, size, set(Ground.WATER));
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
each('waterway', (feature, parts) => {
|
|
117
|
+
const wide = WATERWAYS[String(feature.properties.class)];
|
|
118
|
+
if (wide && feature.type === 2 && feature.properties.brunnel !== 'tunnel') {
|
|
119
|
+
for (const part of parts) {
|
|
120
|
+
stroke(part, Math.max(1, wide / across), size, set(Ground.WATER));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
each('aeroway', (feature, parts) => {
|
|
125
|
+
if (feature.type === 3) {
|
|
126
|
+
fill(parts, size, set(Ground.PAVED));
|
|
127
|
+
}
|
|
128
|
+
else if (feature.type === 2) {
|
|
129
|
+
for (const part of parts) {
|
|
130
|
+
stroke(part, 40 / across, size, set(Ground.PAVED));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
// The smaller ways first, so that a main road crossing a street is the road.
|
|
135
|
+
const ways = [];
|
|
136
|
+
each('transportation', (feature, parts) => {
|
|
137
|
+
var _a;
|
|
138
|
+
if (feature.properties.brunnel === 'tunnel') {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const className = String(feature.properties.class);
|
|
142
|
+
const way = (_a = WAYS[className]) !== null && _a !== void 0 ? _a : (className.endsWith('_construction') ? undefined : WAYS.service);
|
|
143
|
+
if (!way) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const footway = feature.properties.subclass === 'footway' || feature.properties.subclass === 'pedestrian';
|
|
147
|
+
ways.push({ width: way.metres / across, kind: footway ? Ground.PATH : way.ground, parts, area: feature.type === 3 });
|
|
148
|
+
});
|
|
149
|
+
ways.sort((a, b) => a.width - b.width);
|
|
150
|
+
for (const way of ways) {
|
|
151
|
+
if (way.area) {
|
|
152
|
+
fill(way.parts, size, set(Ground.PAVED));
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
for (const part of way.parts) {
|
|
156
|
+
stroke(part, Math.max(1, way.width), size, set(way.kind));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
each('building', (feature, parts) => {
|
|
161
|
+
var _a, _b;
|
|
162
|
+
if (feature.type !== 3 || feature.properties.hide_3d === true || buildings.length >= MAX_BUILDINGS) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const height = Number((_a = feature.properties.render_height) !== null && _a !== void 0 ? _a : feature.properties.height);
|
|
166
|
+
const min = Number((_b = feature.properties.render_min_height) !== null && _b !== void 0 ? _b : feature.properties.min_height);
|
|
167
|
+
const colour = typeof feature.properties.colour === 'string' && /^#[0-9a-f]{6}$/i.test(feature.properties.colour) ? feature.properties.colour : undefined;
|
|
168
|
+
const made = {
|
|
169
|
+
height: Number.isFinite(height) && height > 0 ? height : DEFAULT_HEIGHT,
|
|
170
|
+
min: Number.isFinite(min) && min > 0 ? min : 0,
|
|
171
|
+
area: 0,
|
|
172
|
+
x: 0,
|
|
173
|
+
z: 0,
|
|
174
|
+
...(colour ? { colour } : {}),
|
|
175
|
+
};
|
|
176
|
+
const id = buildings.length + 1;
|
|
177
|
+
let sx = 0;
|
|
178
|
+
let sz = 0;
|
|
179
|
+
fill(parts, size, i => {
|
|
180
|
+
building[i] = id;
|
|
181
|
+
made.area++;
|
|
182
|
+
sx += i % size;
|
|
183
|
+
sz += Math.floor(i / size);
|
|
184
|
+
});
|
|
185
|
+
if (made.area > 0) {
|
|
186
|
+
made.x = sx / made.area + 0.5;
|
|
187
|
+
made.z = sz / made.area + 0.5;
|
|
188
|
+
buildings.push(made);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
return { size, ground, building, buildings };
|
|
192
|
+
}
|
|
193
|
+
GeoRaster.plot = plot;
|
|
194
|
+
/** Runs `set` on each column whose middle is inside the rings (even-odd). */
|
|
195
|
+
function fill(rings, size, set) {
|
|
196
|
+
let top = Number.POSITIVE_INFINITY;
|
|
197
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
198
|
+
for (const ring of rings) {
|
|
199
|
+
for (const [, z] of ring) {
|
|
200
|
+
top = Math.min(top, z);
|
|
201
|
+
bottom = Math.max(bottom, z);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const from = Math.max(0, Math.floor(top));
|
|
205
|
+
const to = Math.min(size - 1, Math.ceil(bottom));
|
|
206
|
+
const xs = [];
|
|
207
|
+
for (let row = from; row <= to; row++) {
|
|
208
|
+
const z = row + 0.5;
|
|
209
|
+
xs.length = 0;
|
|
210
|
+
for (const ring of rings) {
|
|
211
|
+
for (let k = 0; k + 1 < ring.length; k++) {
|
|
212
|
+
const [x1, z1] = ring[k];
|
|
213
|
+
const [x2, z2] = ring[k + 1];
|
|
214
|
+
if (z1 <= z !== z2 <= z) {
|
|
215
|
+
xs.push(x1 + ((z - z1) / (z2 - z1)) * (x2 - x1));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
xs.sort((a, b) => a - b);
|
|
220
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
221
|
+
const start = Math.max(0, Math.ceil(xs[k] - 0.5));
|
|
222
|
+
const end = Math.min(size - 1, Math.floor(xs[k + 1] - 0.5));
|
|
223
|
+
for (let x = start; x <= end; x++) {
|
|
224
|
+
set(row * size + x);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
GeoRaster.fill = fill;
|
|
230
|
+
/** Runs `set` on each column whose middle is within `width / 2` of the line. */
|
|
231
|
+
function stroke(line, width, size, set) {
|
|
232
|
+
const half = width / 2;
|
|
233
|
+
for (let k = 0; k + 1 < line.length; k++) {
|
|
234
|
+
const [x1, z1] = line[k];
|
|
235
|
+
const [x2, z2] = line[k + 1];
|
|
236
|
+
const left = Math.max(0, Math.floor(Math.min(x1, x2) - half));
|
|
237
|
+
const right = Math.min(size - 1, Math.ceil(Math.max(x1, x2) + half));
|
|
238
|
+
const top = Math.max(0, Math.floor(Math.min(z1, z2) - half));
|
|
239
|
+
const bottom = Math.min(size - 1, Math.ceil(Math.max(z1, z2) + half));
|
|
240
|
+
const dx = x2 - x1;
|
|
241
|
+
const dz = z2 - z1;
|
|
242
|
+
const length = dx * dx + dz * dz;
|
|
243
|
+
for (let z = top; z <= bottom; z++) {
|
|
244
|
+
for (let x = left; x <= right; x++) {
|
|
245
|
+
const px = x + 0.5;
|
|
246
|
+
const pz = z + 0.5;
|
|
247
|
+
const t = length > 0 ? Math.max(0, Math.min(1, ((px - x1) * dx + (pz - z1) * dz) / length)) : 0;
|
|
248
|
+
const ex = px - (x1 + t * dx);
|
|
249
|
+
const ez = pz - (z1 + t * dz);
|
|
250
|
+
if (ex * ex + ez * ez <= half * half) {
|
|
251
|
+
set(z * size + x);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
GeoRaster.stroke = stroke;
|
|
258
|
+
})(GeoRaster || (GeoRaster = {}));
|
|
259
|
+
//# sourceMappingURL=geo-raster.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MapTile } from './geo-grid.js';
|
|
2
|
+
import { type MvtLayer } from './geo-mvt.js';
|
|
3
|
+
export type Fetcher = (url: string, init?: RequestInit) => Promise<Response>;
|
|
4
|
+
export interface GeoTileSource {
|
|
5
|
+
/** The version read now: the latest of the source. */
|
|
6
|
+
latest(): Promise<string>;
|
|
7
|
+
/** The layers of a tile of a version; none where the map has nothing (the open sea). */
|
|
8
|
+
tile(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
|
|
9
|
+
/** The highest zoom it has tiles of. */
|
|
10
|
+
readonly maxZoom: number;
|
|
11
|
+
}
|
|
12
|
+
export declare class OpenFreeMapTiles implements GeoTileSource {
|
|
13
|
+
protected readonly fetcher: Fetcher;
|
|
14
|
+
protected readonly base: string;
|
|
15
|
+
readonly maxZoom = 14;
|
|
16
|
+
protected version?: Promise<{
|
|
17
|
+
version: string;
|
|
18
|
+
template: string;
|
|
19
|
+
}>;
|
|
20
|
+
protected readonly kept: Map<string, Promise<Map<string, MvtLayer> | undefined>>;
|
|
21
|
+
constructor(fetcher?: Fetcher, base?: string);
|
|
22
|
+
latest(): Promise<string>;
|
|
23
|
+
tile(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
|
|
24
|
+
protected load(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
|
|
25
|
+
protected read(): Promise<{
|
|
26
|
+
version: string;
|
|
27
|
+
template: string;
|
|
28
|
+
}>;
|
|
29
|
+
}
|
package/esm/geo-tiles.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { GeoMvt } from './geo-mvt.js';
|
|
2
|
+
const GZIP = [0x1f, 0x8b];
|
|
3
|
+
/** Tiles kept decoded, the latest read. */
|
|
4
|
+
const KEPT = 8;
|
|
5
|
+
async function gunzip(bytes) {
|
|
6
|
+
if (bytes[0] !== GZIP[0] || bytes[1] !== GZIP[1]) {
|
|
7
|
+
return bytes;
|
|
8
|
+
}
|
|
9
|
+
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'));
|
|
10
|
+
return new Uint8Array(await new Response(stream).arrayBuffer());
|
|
11
|
+
}
|
|
12
|
+
export class OpenFreeMapTiles {
|
|
13
|
+
constructor(fetcher = (url, init) => fetch(url, init), base = 'https://tiles.openfreemap.org/planet') {
|
|
14
|
+
this.fetcher = fetcher;
|
|
15
|
+
this.base = base;
|
|
16
|
+
this.maxZoom = 14;
|
|
17
|
+
this.kept = new Map();
|
|
18
|
+
}
|
|
19
|
+
latest() {
|
|
20
|
+
return this.read().then(each => each.version);
|
|
21
|
+
}
|
|
22
|
+
tile(tile, version) {
|
|
23
|
+
const key = `${version}/${tile.z}/${tile.x}/${tile.y}`;
|
|
24
|
+
const kept = this.kept.get(key);
|
|
25
|
+
if (kept) {
|
|
26
|
+
this.kept.delete(key);
|
|
27
|
+
this.kept.set(key, kept);
|
|
28
|
+
return kept;
|
|
29
|
+
}
|
|
30
|
+
const made = this.load(tile, version);
|
|
31
|
+
made.catch(() => this.kept.delete(key));
|
|
32
|
+
this.kept.set(key, made);
|
|
33
|
+
while (this.kept.size > KEPT) {
|
|
34
|
+
this.kept.delete(this.kept.keys().next().value);
|
|
35
|
+
}
|
|
36
|
+
return made;
|
|
37
|
+
}
|
|
38
|
+
async load(tile, version) {
|
|
39
|
+
const { template, version: latest } = await this.read();
|
|
40
|
+
const url = (of) => template.replace(latest, of).replace('{z}', String(tile.z)).replace('{x}', String(tile.x)).replace('{y}', String(tile.y));
|
|
41
|
+
let response = await this.fetcher(url(version));
|
|
42
|
+
// A version the source no longer keeps: the latest, which differs a little at most.
|
|
43
|
+
if (response.status === 404 && version !== latest) {
|
|
44
|
+
response = await this.fetcher(url(latest));
|
|
45
|
+
}
|
|
46
|
+
if (response.status === 204 || response.status === 404) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
if (!response.ok) {
|
|
50
|
+
throw new Error(`Map tile ${tile.z}/${tile.x}/${tile.y}: HTTP ${response.status}`);
|
|
51
|
+
}
|
|
52
|
+
const bytes = await gunzip(new Uint8Array(await response.arrayBuffer()));
|
|
53
|
+
return bytes.length > 0 ? GeoMvt.decode(bytes) : undefined;
|
|
54
|
+
}
|
|
55
|
+
read() {
|
|
56
|
+
var _a;
|
|
57
|
+
(_a = this.version) !== null && _a !== void 0 ? _a : (this.version = this.fetcher(this.base)
|
|
58
|
+
.then(response => {
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
throw new Error(`Map source: HTTP ${response.status}`);
|
|
61
|
+
}
|
|
62
|
+
return response.json();
|
|
63
|
+
})
|
|
64
|
+
.then(json => {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
const template = (_a = json.tiles) === null || _a === void 0 ? void 0 : _a[0];
|
|
67
|
+
const version = template && ((_b = /\/([^/]+)\/\{z\}/.exec(template)) === null || _b === void 0 ? void 0 : _b[1]);
|
|
68
|
+
if (!template || !version) {
|
|
69
|
+
throw new Error('Map source: no tiles');
|
|
70
|
+
}
|
|
71
|
+
return { template, version };
|
|
72
|
+
}));
|
|
73
|
+
this.version.catch(() => {
|
|
74
|
+
this.version = undefined;
|
|
75
|
+
});
|
|
76
|
+
return this.version;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=geo-tiles.js.map
|