@celljs/ta-geo 0.0.0-stage → 4.30.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 +11 -0
- package/README.md +10 -2
- package/esm/geo-grid.d.ts +50 -0
- package/esm/geo-grid.js +89 -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 +14 -0
- package/esm/geo-places.js +71 -0
- package/esm/geo-raster.d.ts +56 -0
- package/esm/geo-raster.js +258 -0
- package/esm/geo-tiles.d.ts +29 -0
- package/esm/geo-tiles.js +79 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.js +7 -0
- package/esm/package.json +3 -0
- package/lib/geo-grid.d.ts +51 -0
- package/lib/geo-grid.js +92 -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 +15 -0
- package/lib/geo-places.js +74 -0
- package/lib/geo-raster.d.ts +57 -0
- package/lib/geo-raster.js +261 -0
- package/lib/geo-tiles.d.ts +30 -0
- package/lib/geo-tiles.js +83 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +23 -0
- package/package.json +45 -4
|
@@ -0,0 +1,56 @@
|
|
|
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).
|
|
49
|
+
*/
|
|
50
|
+
function plot(layers: ReadonlyMap<string, MvtLayer> | undefined, window: PlotWindow, metres: number): PlotRaster;
|
|
51
|
+
/** Runs `set` on each column whose middle is inside the rings (even-odd). */
|
|
52
|
+
function fill(rings: readonly Ring[], size: number, set: (index: number) => void): void;
|
|
53
|
+
/** Runs `set` on each column whose middle is within `width / 2` of the line. */
|
|
54
|
+
function stroke(line: Ring, width: number, size: number, set: (index: number) => void): void;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,258 @@
|
|
|
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).
|
|
65
|
+
*/
|
|
66
|
+
function plot(layers, window, metres) {
|
|
67
|
+
const size = PLOT_SIZE;
|
|
68
|
+
const ground = new Uint8Array(size * size);
|
|
69
|
+
const building = new Uint16Array(size * size);
|
|
70
|
+
const buildings = [];
|
|
71
|
+
if (!layers) {
|
|
72
|
+
// Nothing on the map: the open sea.
|
|
73
|
+
ground.fill(Ground.WATER);
|
|
74
|
+
return { size, ground, building, buildings };
|
|
75
|
+
}
|
|
76
|
+
const toPlot = (layer) => (part) => part.map(([u, v]) => [((u / layer.extent - window.left) / window.size) * size, ((v / layer.extent - window.top) / window.size) * size]);
|
|
77
|
+
const each = (name, run) => {
|
|
78
|
+
const layer = layers.get(name);
|
|
79
|
+
if (!layer) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const map = toPlot(layer);
|
|
83
|
+
for (const feature of layer.features) {
|
|
84
|
+
run(feature, feature.geometry.map(map));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const set = (kind) => (i) => {
|
|
88
|
+
ground[i] = kind;
|
|
89
|
+
};
|
|
90
|
+
// What covers the land, then what is on it, the water, the ways over all of it, the buildings last.
|
|
91
|
+
each('landuse', (feature, parts) => {
|
|
92
|
+
const kind = LANDUSE[String(feature.properties.class)];
|
|
93
|
+
if (kind !== undefined && feature.type === 3) {
|
|
94
|
+
fill(parts, size, set(kind));
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
each('landcover', (feature, parts) => {
|
|
98
|
+
var _a;
|
|
99
|
+
const kind = (_a = LANDCOVER[String(feature.properties.class)]) !== null && _a !== void 0 ? _a : LANDCOVER[String(feature.properties.subclass)];
|
|
100
|
+
if (kind !== undefined && feature.type === 3) {
|
|
101
|
+
fill(parts, size, set(kind));
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
each('park', (feature, parts) => {
|
|
105
|
+
const kind = String(feature.properties.class);
|
|
106
|
+
if (feature.type === 3 && kind !== 'national_park' && kind !== 'nature_reserve' && kind !== 'protected_area') {
|
|
107
|
+
fill(parts, size, set(Ground.PARK));
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
each('water', (feature, parts) => {
|
|
111
|
+
if (feature.type === 3 && feature.properties.brunnel !== 'tunnel') {
|
|
112
|
+
fill(parts, size, set(Ground.WATER));
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
each('waterway', (feature, parts) => {
|
|
116
|
+
const wide = WATERWAYS[String(feature.properties.class)];
|
|
117
|
+
if (wide && feature.type === 2 && feature.properties.brunnel !== 'tunnel') {
|
|
118
|
+
for (const part of parts) {
|
|
119
|
+
stroke(part, Math.max(1, wide / metres), size, set(Ground.WATER));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
each('aeroway', (feature, parts) => {
|
|
124
|
+
if (feature.type === 3) {
|
|
125
|
+
fill(parts, size, set(Ground.PAVED));
|
|
126
|
+
}
|
|
127
|
+
else if (feature.type === 2) {
|
|
128
|
+
for (const part of parts) {
|
|
129
|
+
stroke(part, 40 / metres, size, set(Ground.PAVED));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
// The smaller ways first, so that a main road crossing a street is the road.
|
|
134
|
+
const ways = [];
|
|
135
|
+
each('transportation', (feature, parts) => {
|
|
136
|
+
var _a;
|
|
137
|
+
if (feature.properties.brunnel === 'tunnel') {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const className = String(feature.properties.class);
|
|
141
|
+
const way = (_a = WAYS[className]) !== null && _a !== void 0 ? _a : (className.endsWith('_construction') ? undefined : WAYS.service);
|
|
142
|
+
if (!way) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const footway = feature.properties.subclass === 'footway' || feature.properties.subclass === 'pedestrian';
|
|
146
|
+
ways.push({ width: way.metres / metres, kind: footway ? Ground.PATH : way.ground, parts, area: feature.type === 3 });
|
|
147
|
+
});
|
|
148
|
+
ways.sort((a, b) => a.width - b.width);
|
|
149
|
+
for (const way of ways) {
|
|
150
|
+
if (way.area) {
|
|
151
|
+
fill(way.parts, size, set(Ground.PAVED));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
for (const part of way.parts) {
|
|
155
|
+
stroke(part, Math.max(1, way.width), size, set(way.kind));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
each('building', (feature, parts) => {
|
|
160
|
+
var _a, _b;
|
|
161
|
+
if (feature.type !== 3 || feature.properties.hide_3d === true || buildings.length >= MAX_BUILDINGS) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const height = Number((_a = feature.properties.render_height) !== null && _a !== void 0 ? _a : feature.properties.height);
|
|
165
|
+
const min = Number((_b = feature.properties.render_min_height) !== null && _b !== void 0 ? _b : feature.properties.min_height);
|
|
166
|
+
const colour = typeof feature.properties.colour === 'string' && /^#[0-9a-f]{6}$/i.test(feature.properties.colour) ? feature.properties.colour : undefined;
|
|
167
|
+
const made = {
|
|
168
|
+
height: Number.isFinite(height) && height > 0 ? height : DEFAULT_HEIGHT,
|
|
169
|
+
min: Number.isFinite(min) && min > 0 ? min : 0,
|
|
170
|
+
area: 0,
|
|
171
|
+
x: 0,
|
|
172
|
+
z: 0,
|
|
173
|
+
...(colour ? { colour } : {}),
|
|
174
|
+
};
|
|
175
|
+
const id = buildings.length + 1;
|
|
176
|
+
let sx = 0;
|
|
177
|
+
let sz = 0;
|
|
178
|
+
fill(parts, size, i => {
|
|
179
|
+
building[i] = id;
|
|
180
|
+
made.area++;
|
|
181
|
+
sx += i % size;
|
|
182
|
+
sz += Math.floor(i / size);
|
|
183
|
+
});
|
|
184
|
+
if (made.area > 0) {
|
|
185
|
+
made.x = sx / made.area + 0.5;
|
|
186
|
+
made.z = sz / made.area + 0.5;
|
|
187
|
+
buildings.push(made);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
return { size, ground, building, buildings };
|
|
191
|
+
}
|
|
192
|
+
GeoRaster.plot = plot;
|
|
193
|
+
/** Runs `set` on each column whose middle is inside the rings (even-odd). */
|
|
194
|
+
function fill(rings, size, set) {
|
|
195
|
+
let top = Number.POSITIVE_INFINITY;
|
|
196
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
197
|
+
for (const ring of rings) {
|
|
198
|
+
for (const [, z] of ring) {
|
|
199
|
+
top = Math.min(top, z);
|
|
200
|
+
bottom = Math.max(bottom, z);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const from = Math.max(0, Math.floor(top));
|
|
204
|
+
const to = Math.min(size - 1, Math.ceil(bottom));
|
|
205
|
+
const xs = [];
|
|
206
|
+
for (let row = from; row <= to; row++) {
|
|
207
|
+
const z = row + 0.5;
|
|
208
|
+
xs.length = 0;
|
|
209
|
+
for (const ring of rings) {
|
|
210
|
+
for (let k = 0; k + 1 < ring.length; k++) {
|
|
211
|
+
const [x1, z1] = ring[k];
|
|
212
|
+
const [x2, z2] = ring[k + 1];
|
|
213
|
+
if (z1 <= z !== z2 <= z) {
|
|
214
|
+
xs.push(x1 + ((z - z1) / (z2 - z1)) * (x2 - x1));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
xs.sort((a, b) => a - b);
|
|
219
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
220
|
+
const start = Math.max(0, Math.ceil(xs[k] - 0.5));
|
|
221
|
+
const end = Math.min(size - 1, Math.floor(xs[k + 1] - 0.5));
|
|
222
|
+
for (let x = start; x <= end; x++) {
|
|
223
|
+
set(row * size + x);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
GeoRaster.fill = fill;
|
|
229
|
+
/** Runs `set` on each column whose middle is within `width / 2` of the line. */
|
|
230
|
+
function stroke(line, width, size, set) {
|
|
231
|
+
const half = width / 2;
|
|
232
|
+
for (let k = 0; k + 1 < line.length; k++) {
|
|
233
|
+
const [x1, z1] = line[k];
|
|
234
|
+
const [x2, z2] = line[k + 1];
|
|
235
|
+
const left = Math.max(0, Math.floor(Math.min(x1, x2) - half));
|
|
236
|
+
const right = Math.min(size - 1, Math.ceil(Math.max(x1, x2) + half));
|
|
237
|
+
const top = Math.max(0, Math.floor(Math.min(z1, z2) - half));
|
|
238
|
+
const bottom = Math.min(size - 1, Math.ceil(Math.max(z1, z2) + half));
|
|
239
|
+
const dx = x2 - x1;
|
|
240
|
+
const dz = z2 - z1;
|
|
241
|
+
const length = dx * dx + dz * dz;
|
|
242
|
+
for (let z = top; z <= bottom; z++) {
|
|
243
|
+
for (let x = left; x <= right; x++) {
|
|
244
|
+
const px = x + 0.5;
|
|
245
|
+
const pz = z + 0.5;
|
|
246
|
+
const t = length > 0 ? Math.max(0, Math.min(1, ((px - x1) * dx + (pz - z1) * dz) / length)) : 0;
|
|
247
|
+
const ex = px - (x1 + t * dx);
|
|
248
|
+
const ez = pz - (z1 + t * dz);
|
|
249
|
+
if (ex * ex + ez * ez <= half * half) {
|
|
250
|
+
set(z * size + x);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
GeoRaster.stroke = stroke;
|
|
257
|
+
})(GeoRaster || (GeoRaster = {}));
|
|
258
|
+
//# 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
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
package/esm/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** The zoom of a plot. */
|
|
2
|
+
export declare const PLOT_ZOOM = 16;
|
|
3
|
+
/** Columns across a plot. */
|
|
4
|
+
export declare const PLOT_SIZE = 256;
|
|
5
|
+
/** The zoom of a column: a pixel of a plot. */
|
|
6
|
+
export declare const COLUMN_ZOOM: number;
|
|
7
|
+
export interface LonLat {
|
|
8
|
+
lon: number;
|
|
9
|
+
lat: number;
|
|
10
|
+
}
|
|
11
|
+
/** A point of the grid, in columns: x east, z south, from the north-west corner of the earth. */
|
|
12
|
+
export interface GridPoint {
|
|
13
|
+
x: number;
|
|
14
|
+
z: number;
|
|
15
|
+
}
|
|
16
|
+
/** A plot: a tile of zoom 16. */
|
|
17
|
+
export interface Plot {
|
|
18
|
+
px: number;
|
|
19
|
+
pz: number;
|
|
20
|
+
}
|
|
21
|
+
/** A tile of a zoom of the maps of the web. */
|
|
22
|
+
export interface MapTile {
|
|
23
|
+
z: number;
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
}
|
|
27
|
+
export declare namespace GeoGrid {
|
|
28
|
+
/** Where a place is on the grid, in columns (not whole). */
|
|
29
|
+
function toGrid({ lon, lat }: LonLat): GridPoint;
|
|
30
|
+
/** The place at a point of the grid. */
|
|
31
|
+
function toLonLat({ x, z }: GridPoint): LonLat;
|
|
32
|
+
/** The plot a point is in. */
|
|
33
|
+
function plotOf({ x, z }: GridPoint): Plot;
|
|
34
|
+
/** A plot by its name, `16/x/y` as the maps of the web name its tile. */
|
|
35
|
+
function key({ px, pz }: Plot): string;
|
|
36
|
+
function parse(key: string): Plot | undefined;
|
|
37
|
+
/** The tile of a lower zoom the plot is in, and where in it, as a fraction of the tile (0 to 1). */
|
|
38
|
+
function tileOf({ px, pz }: Plot, zoom: number): {
|
|
39
|
+
tile: MapTile;
|
|
40
|
+
left: number;
|
|
41
|
+
top: number;
|
|
42
|
+
size: number;
|
|
43
|
+
};
|
|
44
|
+
/** The metres a column is across at a latitude. */
|
|
45
|
+
function metresPerColumn(lat: number): number;
|
|
46
|
+
/** The plots around one, `ring` away at most, nearest first. */
|
|
47
|
+
function around(plot: Plot, ring: number): Plot[];
|
|
48
|
+
/** A place made coarse: the middle of its tile of `zoom` (12: about ten kilometres), to tell others roughly where. */
|
|
49
|
+
function coarse(place: LonLat, zoom: number): LonLat;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=geo-grid.d.ts.map
|
package/lib/geo-grid.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The earth as one grid (GeoGrid): Web Mercator, as the maps of the web draw it, cut into plots, the tiles of
|
|
3
|
+
// zoom 16, each of 256 × 256 columns. A column is a pixel of zoom 24, so that every device names the same place by
|
|
4
|
+
// the same numbers and what each makes of it fits beside what the others make, with nothing to agree on.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.GeoGrid = exports.COLUMN_ZOOM = exports.PLOT_SIZE = exports.PLOT_ZOOM = void 0;
|
|
7
|
+
/** The zoom of a plot. */
|
|
8
|
+
exports.PLOT_ZOOM = 16;
|
|
9
|
+
/** Columns across a plot. */
|
|
10
|
+
exports.PLOT_SIZE = 256;
|
|
11
|
+
/** The zoom of a column: a pixel of a plot. */
|
|
12
|
+
exports.COLUMN_ZOOM = exports.PLOT_ZOOM + 8;
|
|
13
|
+
const COLUMNS = 2 ** exports.COLUMN_ZOOM;
|
|
14
|
+
/** The earth's circumference at the equator, in metres (WGS 84). */
|
|
15
|
+
const EQUATOR = 40075016.686;
|
|
16
|
+
/** How far north and south Web Mercator goes. */
|
|
17
|
+
const MAX_LAT = 85.05112878;
|
|
18
|
+
var GeoGrid;
|
|
19
|
+
(function (GeoGrid) {
|
|
20
|
+
/** Where a place is on the grid, in columns (not whole). */
|
|
21
|
+
function toGrid({ lon, lat }) {
|
|
22
|
+
const clamped = Math.max(-MAX_LAT, Math.min(MAX_LAT, lat));
|
|
23
|
+
const rad = (clamped * Math.PI) / 180;
|
|
24
|
+
const x = ((lon + 180) / 360) * COLUMNS;
|
|
25
|
+
const z = ((1 - Math.log(Math.tan(rad) + 1 / Math.cos(rad)) / Math.PI) / 2) * COLUMNS;
|
|
26
|
+
return { x, z };
|
|
27
|
+
}
|
|
28
|
+
GeoGrid.toGrid = toGrid;
|
|
29
|
+
/** The place at a point of the grid. */
|
|
30
|
+
function toLonLat({ x, z }) {
|
|
31
|
+
const lon = (x / COLUMNS) * 360 - 180;
|
|
32
|
+
const n = Math.PI - (2 * Math.PI * z) / COLUMNS;
|
|
33
|
+
const lat = (180 / Math.PI) * Math.atan(Math.sinh(n));
|
|
34
|
+
return { lon, lat };
|
|
35
|
+
}
|
|
36
|
+
GeoGrid.toLonLat = toLonLat;
|
|
37
|
+
/** The plot a point is in. */
|
|
38
|
+
function plotOf({ x, z }) {
|
|
39
|
+
return { px: Math.floor(x / exports.PLOT_SIZE), pz: Math.floor(z / exports.PLOT_SIZE) };
|
|
40
|
+
}
|
|
41
|
+
GeoGrid.plotOf = plotOf;
|
|
42
|
+
/** A plot by its name, `16/x/y` as the maps of the web name its tile. */
|
|
43
|
+
function key({ px, pz }) {
|
|
44
|
+
return `${exports.PLOT_ZOOM}/${px}/${pz}`;
|
|
45
|
+
}
|
|
46
|
+
GeoGrid.key = key;
|
|
47
|
+
function parse(key) {
|
|
48
|
+
const match = /^16\/(\d+)\/(\d+)$/.exec(key);
|
|
49
|
+
if (!match) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const plot = { px: Number(match[1]), pz: Number(match[2]) };
|
|
53
|
+
return plot.px < 2 ** exports.PLOT_ZOOM && plot.pz < 2 ** exports.PLOT_ZOOM ? plot : undefined;
|
|
54
|
+
}
|
|
55
|
+
GeoGrid.parse = parse;
|
|
56
|
+
/** The tile of a lower zoom the plot is in, and where in it, as a fraction of the tile (0 to 1). */
|
|
57
|
+
function tileOf({ px, pz }, zoom) {
|
|
58
|
+
const shift = exports.PLOT_ZOOM - zoom;
|
|
59
|
+
const per = 2 ** shift;
|
|
60
|
+
return {
|
|
61
|
+
tile: { z: zoom, x: Math.floor(px / per), y: Math.floor(pz / per) },
|
|
62
|
+
left: (px % per) / per,
|
|
63
|
+
top: (pz % per) / per,
|
|
64
|
+
size: 1 / per,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
GeoGrid.tileOf = tileOf;
|
|
68
|
+
/** The metres a column is across at a latitude. */
|
|
69
|
+
function metresPerColumn(lat) {
|
|
70
|
+
return (EQUATOR * Math.cos((lat * Math.PI) / 180)) / COLUMNS;
|
|
71
|
+
}
|
|
72
|
+
GeoGrid.metresPerColumn = metresPerColumn;
|
|
73
|
+
/** The plots around one, `ring` away at most, nearest first. */
|
|
74
|
+
function around(plot, ring) {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (let dz = -ring; dz <= ring; dz++) {
|
|
77
|
+
for (let dx = -ring; dx <= ring; dx++) {
|
|
78
|
+
out.push({ px: plot.px + dx, pz: plot.pz + dz });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out.sort((a, b) => Math.hypot(a.px - plot.px, a.pz - plot.pz) - Math.hypot(b.px - plot.px, b.pz - plot.pz));
|
|
82
|
+
}
|
|
83
|
+
GeoGrid.around = around;
|
|
84
|
+
/** A place made coarse: the middle of its tile of `zoom` (12: about ten kilometres), to tell others roughly where. */
|
|
85
|
+
function coarse(place, zoom) {
|
|
86
|
+
const per = 2 ** (exports.COLUMN_ZOOM - zoom);
|
|
87
|
+
const { x, z } = toGrid(place);
|
|
88
|
+
return toLonLat({ x: (Math.floor(x / per) + 0.5) * per, z: (Math.floor(z / per) + 0.5) * per });
|
|
89
|
+
}
|
|
90
|
+
GeoGrid.coarse = coarse;
|
|
91
|
+
})(GeoGrid || (exports.GeoGrid = GeoGrid = {}));
|
|
92
|
+
//# sourceMappingURL=geo-grid.js.map
|
package/lib/geo-mvt.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type MvtValue = string | number | boolean;
|
|
2
|
+
export interface MvtFeature {
|
|
3
|
+
/** 1 a point, 2 a line, 3 a polygon. */
|
|
4
|
+
type: 1 | 2 | 3;
|
|
5
|
+
properties: Record<string, MvtValue>;
|
|
6
|
+
/** Points: one part of points; lines: a part each; polygons: rings, each outer one before its holes. */
|
|
7
|
+
geometry: [number, number][][];
|
|
8
|
+
}
|
|
9
|
+
export interface MvtLayer {
|
|
10
|
+
name: string;
|
|
11
|
+
extent: number;
|
|
12
|
+
features: MvtFeature[];
|
|
13
|
+
}
|
|
14
|
+
export declare namespace GeoMvt {
|
|
15
|
+
/** The layers of a tile, by name. */
|
|
16
|
+
function decode(bytes: Uint8Array): Map<string, MvtLayer>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=geo-mvt.d.ts.map
|