@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,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeoRaster = exports.Ground = void 0;
|
|
4
|
+
const geo_grid_1 = require("./geo-grid");
|
|
5
|
+
// What is on the ground of a plot (GeoRaster), from the layers of a vector tile in the schema of OpenMapTiles: a
|
|
6
|
+
// kind of ground for each column (grass, a park, a wood, water, a road…), and the buildings, each a footprint of
|
|
7
|
+
// columns with its height in metres. Polygons fill the columns whose middle is in them (even-odd, so holes stay
|
|
8
|
+
// open); lines are as wide as what they are (a motorway, a footpath), in columns of the plot's latitude.
|
|
9
|
+
exports.Ground = {
|
|
10
|
+
LAND: 0,
|
|
11
|
+
GRASS: 1,
|
|
12
|
+
PARK: 2,
|
|
13
|
+
WOOD: 3,
|
|
14
|
+
FARM: 4,
|
|
15
|
+
SAND: 5,
|
|
16
|
+
WATER: 6,
|
|
17
|
+
PAVED: 7,
|
|
18
|
+
ROAD: 8,
|
|
19
|
+
STREET: 9,
|
|
20
|
+
PATH: 10,
|
|
21
|
+
RAIL: 11,
|
|
22
|
+
PITCH: 12,
|
|
23
|
+
};
|
|
24
|
+
/** How wide each class of transportation is, in metres, and the ground it is. */
|
|
25
|
+
const WAYS = {
|
|
26
|
+
motorway: { metres: 18, ground: exports.Ground.ROAD },
|
|
27
|
+
trunk: { metres: 16, ground: exports.Ground.ROAD },
|
|
28
|
+
primary: { metres: 14, ground: exports.Ground.ROAD },
|
|
29
|
+
secondary: { metres: 12, ground: exports.Ground.ROAD },
|
|
30
|
+
tertiary: { metres: 10, ground: exports.Ground.ROAD },
|
|
31
|
+
minor: { metres: 7, ground: exports.Ground.STREET },
|
|
32
|
+
service: { metres: 4.5, ground: exports.Ground.STREET },
|
|
33
|
+
track: { metres: 3, ground: exports.Ground.PATH },
|
|
34
|
+
path: { metres: 2.5, ground: exports.Ground.PATH },
|
|
35
|
+
rail: { metres: 4, ground: exports.Ground.RAIL },
|
|
36
|
+
transit: { metres: 4, ground: exports.Ground.RAIL },
|
|
37
|
+
pier: { metres: 4, ground: exports.Ground.PAVED },
|
|
38
|
+
};
|
|
39
|
+
const WATERWAYS = { river: 10, canal: 8, stream: 2.5, drain: 1.5, ditch: 1.5 };
|
|
40
|
+
const LANDCOVER = {
|
|
41
|
+
wood: exports.Ground.WOOD,
|
|
42
|
+
forest: exports.Ground.WOOD,
|
|
43
|
+
farmland: exports.Ground.FARM,
|
|
44
|
+
grass: exports.Ground.GRASS,
|
|
45
|
+
wetland: exports.Ground.GRASS,
|
|
46
|
+
sand: exports.Ground.SAND,
|
|
47
|
+
beach: exports.Ground.SAND,
|
|
48
|
+
};
|
|
49
|
+
const LANDUSE = {
|
|
50
|
+
commercial: exports.Ground.PAVED,
|
|
51
|
+
retail: exports.Ground.PAVED,
|
|
52
|
+
industrial: exports.Ground.PAVED,
|
|
53
|
+
railway: exports.Ground.PAVED,
|
|
54
|
+
pitch: exports.Ground.PITCH,
|
|
55
|
+
playground: exports.Ground.GRASS,
|
|
56
|
+
cemetery: exports.Ground.GRASS,
|
|
57
|
+
stadium: exports.Ground.PAVED,
|
|
58
|
+
garden: exports.Ground.PARK,
|
|
59
|
+
};
|
|
60
|
+
/** The height of a building the map does not know, in metres: two storeys. */
|
|
61
|
+
const DEFAULT_HEIGHT = 6;
|
|
62
|
+
const MAX_BUILDINGS = 65535;
|
|
63
|
+
var GeoRaster;
|
|
64
|
+
(function (GeoRaster) {
|
|
65
|
+
/**
|
|
66
|
+
* The ground of a plot from the layers of the tile it is in: `window` where in the tile, `metres` the metres
|
|
67
|
+
* a column is across there (GeoGrid.metresPerColumn); `size` columns across, fewer for a map seen from far (each
|
|
68
|
+
* then as many metres more).
|
|
69
|
+
*/
|
|
70
|
+
function plot(layers, window, metres, size = geo_grid_1.PLOT_SIZE) {
|
|
71
|
+
const across = metres * (geo_grid_1.PLOT_SIZE / size);
|
|
72
|
+
const ground = new Uint8Array(size * size);
|
|
73
|
+
const building = new Uint16Array(size * size);
|
|
74
|
+
const buildings = [];
|
|
75
|
+
if (!layers) {
|
|
76
|
+
// Nothing on the map: the open sea.
|
|
77
|
+
ground.fill(exports.Ground.WATER);
|
|
78
|
+
return { size, ground, building, buildings };
|
|
79
|
+
}
|
|
80
|
+
const toPlot = (layer) => (part) => part.map(([u, v]) => [((u / layer.extent - window.left) / window.size) * size, ((v / layer.extent - window.top) / window.size) * size]);
|
|
81
|
+
const each = (name, run) => {
|
|
82
|
+
const layer = layers.get(name);
|
|
83
|
+
if (!layer) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const map = toPlot(layer);
|
|
87
|
+
for (const feature of layer.features) {
|
|
88
|
+
run(feature, feature.geometry.map(map));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const set = (kind) => (i) => {
|
|
92
|
+
ground[i] = kind;
|
|
93
|
+
};
|
|
94
|
+
// What covers the land, then what is on it, the water, the ways over all of it, the buildings last.
|
|
95
|
+
each('landuse', (feature, parts) => {
|
|
96
|
+
const kind = LANDUSE[String(feature.properties.class)];
|
|
97
|
+
if (kind !== undefined && feature.type === 3) {
|
|
98
|
+
fill(parts, size, set(kind));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
each('landcover', (feature, parts) => {
|
|
102
|
+
var _a;
|
|
103
|
+
const kind = (_a = LANDCOVER[String(feature.properties.class)]) !== null && _a !== void 0 ? _a : LANDCOVER[String(feature.properties.subclass)];
|
|
104
|
+
if (kind !== undefined && feature.type === 3) {
|
|
105
|
+
fill(parts, size, set(kind));
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
each('park', (feature, parts) => {
|
|
109
|
+
const kind = String(feature.properties.class);
|
|
110
|
+
if (feature.type === 3 && kind !== 'national_park' && kind !== 'nature_reserve' && kind !== 'protected_area') {
|
|
111
|
+
fill(parts, size, set(exports.Ground.PARK));
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
each('water', (feature, parts) => {
|
|
115
|
+
if (feature.type === 3 && feature.properties.brunnel !== 'tunnel') {
|
|
116
|
+
fill(parts, size, set(exports.Ground.WATER));
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
each('waterway', (feature, parts) => {
|
|
120
|
+
const wide = WATERWAYS[String(feature.properties.class)];
|
|
121
|
+
if (wide && feature.type === 2 && feature.properties.brunnel !== 'tunnel') {
|
|
122
|
+
for (const part of parts) {
|
|
123
|
+
stroke(part, Math.max(1, wide / across), size, set(exports.Ground.WATER));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
each('aeroway', (feature, parts) => {
|
|
128
|
+
if (feature.type === 3) {
|
|
129
|
+
fill(parts, size, set(exports.Ground.PAVED));
|
|
130
|
+
}
|
|
131
|
+
else if (feature.type === 2) {
|
|
132
|
+
for (const part of parts) {
|
|
133
|
+
stroke(part, 40 / across, size, set(exports.Ground.PAVED));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
// The smaller ways first, so that a main road crossing a street is the road.
|
|
138
|
+
const ways = [];
|
|
139
|
+
each('transportation', (feature, parts) => {
|
|
140
|
+
var _a;
|
|
141
|
+
if (feature.properties.brunnel === 'tunnel') {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const className = String(feature.properties.class);
|
|
145
|
+
const way = (_a = WAYS[className]) !== null && _a !== void 0 ? _a : (className.endsWith('_construction') ? undefined : WAYS.service);
|
|
146
|
+
if (!way) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const footway = feature.properties.subclass === 'footway' || feature.properties.subclass === 'pedestrian';
|
|
150
|
+
ways.push({ width: way.metres / across, kind: footway ? exports.Ground.PATH : way.ground, parts, area: feature.type === 3 });
|
|
151
|
+
});
|
|
152
|
+
ways.sort((a, b) => a.width - b.width);
|
|
153
|
+
for (const way of ways) {
|
|
154
|
+
if (way.area) {
|
|
155
|
+
fill(way.parts, size, set(exports.Ground.PAVED));
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
for (const part of way.parts) {
|
|
159
|
+
stroke(part, Math.max(1, way.width), size, set(way.kind));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
each('building', (feature, parts) => {
|
|
164
|
+
var _a, _b;
|
|
165
|
+
if (feature.type !== 3 || feature.properties.hide_3d === true || buildings.length >= MAX_BUILDINGS) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const height = Number((_a = feature.properties.render_height) !== null && _a !== void 0 ? _a : feature.properties.height);
|
|
169
|
+
const min = Number((_b = feature.properties.render_min_height) !== null && _b !== void 0 ? _b : feature.properties.min_height);
|
|
170
|
+
const colour = typeof feature.properties.colour === 'string' && /^#[0-9a-f]{6}$/i.test(feature.properties.colour) ? feature.properties.colour : undefined;
|
|
171
|
+
const made = {
|
|
172
|
+
height: Number.isFinite(height) && height > 0 ? height : DEFAULT_HEIGHT,
|
|
173
|
+
min: Number.isFinite(min) && min > 0 ? min : 0,
|
|
174
|
+
area: 0,
|
|
175
|
+
x: 0,
|
|
176
|
+
z: 0,
|
|
177
|
+
...(colour ? { colour } : {}),
|
|
178
|
+
};
|
|
179
|
+
const id = buildings.length + 1;
|
|
180
|
+
let sx = 0;
|
|
181
|
+
let sz = 0;
|
|
182
|
+
fill(parts, size, i => {
|
|
183
|
+
building[i] = id;
|
|
184
|
+
made.area++;
|
|
185
|
+
sx += i % size;
|
|
186
|
+
sz += Math.floor(i / size);
|
|
187
|
+
});
|
|
188
|
+
if (made.area > 0) {
|
|
189
|
+
made.x = sx / made.area + 0.5;
|
|
190
|
+
made.z = sz / made.area + 0.5;
|
|
191
|
+
buildings.push(made);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return { size, ground, building, buildings };
|
|
195
|
+
}
|
|
196
|
+
GeoRaster.plot = plot;
|
|
197
|
+
/** Runs `set` on each column whose middle is inside the rings (even-odd). */
|
|
198
|
+
function fill(rings, size, set) {
|
|
199
|
+
let top = Number.POSITIVE_INFINITY;
|
|
200
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
201
|
+
for (const ring of rings) {
|
|
202
|
+
for (const [, z] of ring) {
|
|
203
|
+
top = Math.min(top, z);
|
|
204
|
+
bottom = Math.max(bottom, z);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const from = Math.max(0, Math.floor(top));
|
|
208
|
+
const to = Math.min(size - 1, Math.ceil(bottom));
|
|
209
|
+
const xs = [];
|
|
210
|
+
for (let row = from; row <= to; row++) {
|
|
211
|
+
const z = row + 0.5;
|
|
212
|
+
xs.length = 0;
|
|
213
|
+
for (const ring of rings) {
|
|
214
|
+
for (let k = 0; k + 1 < ring.length; k++) {
|
|
215
|
+
const [x1, z1] = ring[k];
|
|
216
|
+
const [x2, z2] = ring[k + 1];
|
|
217
|
+
if (z1 <= z !== z2 <= z) {
|
|
218
|
+
xs.push(x1 + ((z - z1) / (z2 - z1)) * (x2 - x1));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
xs.sort((a, b) => a - b);
|
|
223
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
224
|
+
const start = Math.max(0, Math.ceil(xs[k] - 0.5));
|
|
225
|
+
const end = Math.min(size - 1, Math.floor(xs[k + 1] - 0.5));
|
|
226
|
+
for (let x = start; x <= end; x++) {
|
|
227
|
+
set(row * size + x);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
GeoRaster.fill = fill;
|
|
233
|
+
/** Runs `set` on each column whose middle is within `width / 2` of the line. */
|
|
234
|
+
function stroke(line, width, size, set) {
|
|
235
|
+
const half = width / 2;
|
|
236
|
+
for (let k = 0; k + 1 < line.length; k++) {
|
|
237
|
+
const [x1, z1] = line[k];
|
|
238
|
+
const [x2, z2] = line[k + 1];
|
|
239
|
+
const left = Math.max(0, Math.floor(Math.min(x1, x2) - half));
|
|
240
|
+
const right = Math.min(size - 1, Math.ceil(Math.max(x1, x2) + half));
|
|
241
|
+
const top = Math.max(0, Math.floor(Math.min(z1, z2) - half));
|
|
242
|
+
const bottom = Math.min(size - 1, Math.ceil(Math.max(z1, z2) + half));
|
|
243
|
+
const dx = x2 - x1;
|
|
244
|
+
const dz = z2 - z1;
|
|
245
|
+
const length = dx * dx + dz * dz;
|
|
246
|
+
for (let z = top; z <= bottom; z++) {
|
|
247
|
+
for (let x = left; x <= right; x++) {
|
|
248
|
+
const px = x + 0.5;
|
|
249
|
+
const pz = z + 0.5;
|
|
250
|
+
const t = length > 0 ? Math.max(0, Math.min(1, ((px - x1) * dx + (pz - z1) * dz) / length)) : 0;
|
|
251
|
+
const ex = px - (x1 + t * dx);
|
|
252
|
+
const ez = pz - (z1 + t * dz);
|
|
253
|
+
if (ex * ex + ez * ez <= half * half) {
|
|
254
|
+
set(z * size + x);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
GeoRaster.stroke = stroke;
|
|
261
|
+
})(GeoRaster || (exports.GeoRaster = GeoRaster = {}));
|
|
262
|
+
//# sourceMappingURL=geo-raster.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MapTile } from './geo-grid';
|
|
2
|
+
import { type MvtLayer } from './geo-mvt';
|
|
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
|
+
}
|
|
30
|
+
//# sourceMappingURL=geo-tiles.d.ts.map
|
package/lib/geo-tiles.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenFreeMapTiles = void 0;
|
|
4
|
+
const geo_mvt_1 = require("./geo-mvt");
|
|
5
|
+
const GZIP = [0x1f, 0x8b];
|
|
6
|
+
/** Tiles kept decoded, the latest read. */
|
|
7
|
+
const KEPT = 8;
|
|
8
|
+
async function gunzip(bytes) {
|
|
9
|
+
if (bytes[0] !== GZIP[0] || bytes[1] !== GZIP[1]) {
|
|
10
|
+
return bytes;
|
|
11
|
+
}
|
|
12
|
+
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'));
|
|
13
|
+
return new Uint8Array(await new Response(stream).arrayBuffer());
|
|
14
|
+
}
|
|
15
|
+
class OpenFreeMapTiles {
|
|
16
|
+
constructor(fetcher = (url, init) => fetch(url, init), base = 'https://tiles.openfreemap.org/planet') {
|
|
17
|
+
this.fetcher = fetcher;
|
|
18
|
+
this.base = base;
|
|
19
|
+
this.maxZoom = 14;
|
|
20
|
+
this.kept = new Map();
|
|
21
|
+
}
|
|
22
|
+
latest() {
|
|
23
|
+
return this.read().then(each => each.version);
|
|
24
|
+
}
|
|
25
|
+
tile(tile, version) {
|
|
26
|
+
const key = `${version}/${tile.z}/${tile.x}/${tile.y}`;
|
|
27
|
+
const kept = this.kept.get(key);
|
|
28
|
+
if (kept) {
|
|
29
|
+
this.kept.delete(key);
|
|
30
|
+
this.kept.set(key, kept);
|
|
31
|
+
return kept;
|
|
32
|
+
}
|
|
33
|
+
const made = this.load(tile, version);
|
|
34
|
+
made.catch(() => this.kept.delete(key));
|
|
35
|
+
this.kept.set(key, made);
|
|
36
|
+
while (this.kept.size > KEPT) {
|
|
37
|
+
this.kept.delete(this.kept.keys().next().value);
|
|
38
|
+
}
|
|
39
|
+
return made;
|
|
40
|
+
}
|
|
41
|
+
async load(tile, version) {
|
|
42
|
+
const { template, version: latest } = await this.read();
|
|
43
|
+
const url = (of) => template.replace(latest, of).replace('{z}', String(tile.z)).replace('{x}', String(tile.x)).replace('{y}', String(tile.y));
|
|
44
|
+
let response = await this.fetcher(url(version));
|
|
45
|
+
// A version the source no longer keeps: the latest, which differs a little at most.
|
|
46
|
+
if (response.status === 404 && version !== latest) {
|
|
47
|
+
response = await this.fetcher(url(latest));
|
|
48
|
+
}
|
|
49
|
+
if (response.status === 204 || response.status === 404) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
throw new Error(`Map tile ${tile.z}/${tile.x}/${tile.y}: HTTP ${response.status}`);
|
|
54
|
+
}
|
|
55
|
+
const bytes = await gunzip(new Uint8Array(await response.arrayBuffer()));
|
|
56
|
+
return bytes.length > 0 ? geo_mvt_1.GeoMvt.decode(bytes) : undefined;
|
|
57
|
+
}
|
|
58
|
+
read() {
|
|
59
|
+
var _a;
|
|
60
|
+
(_a = this.version) !== null && _a !== void 0 ? _a : (this.version = this.fetcher(this.base)
|
|
61
|
+
.then(response => {
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(`Map source: HTTP ${response.status}`);
|
|
64
|
+
}
|
|
65
|
+
return response.json();
|
|
66
|
+
})
|
|
67
|
+
.then(json => {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
const template = (_a = json.tiles) === null || _a === void 0 ? void 0 : _a[0];
|
|
70
|
+
const version = template && ((_b = /\/([^/]+)\/\{z\}/.exec(template)) === null || _b === void 0 ? void 0 : _b[1]);
|
|
71
|
+
if (!template || !version) {
|
|
72
|
+
throw new Error('Map source: no tiles');
|
|
73
|
+
}
|
|
74
|
+
return { template, version };
|
|
75
|
+
}));
|
|
76
|
+
this.version.catch(() => {
|
|
77
|
+
this.version = undefined;
|
|
78
|
+
});
|
|
79
|
+
return this.version;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.OpenFreeMapTiles = OpenFreeMapTiles;
|
|
83
|
+
//# sourceMappingURL=geo-tiles.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { LonLat } from './geo-grid';
|
|
2
|
+
import type { Fetcher } from './geo-tiles';
|
|
3
|
+
export type WeatherKind = 'clear' | 'cloudy' | 'overcast' | 'fog' | 'drizzle' | 'rain' | 'snow' | 'storm';
|
|
4
|
+
export interface GeoWeatherNow {
|
|
5
|
+
kind: WeatherKind;
|
|
6
|
+
/** The code of the World Meteorological Organization. */
|
|
7
|
+
code: number;
|
|
8
|
+
/** °C. */
|
|
9
|
+
temperature: number;
|
|
10
|
+
/** How much of the sky is cloud, 0 to 1. */
|
|
11
|
+
cloud: number;
|
|
12
|
+
/** Km/h. */
|
|
13
|
+
wind: number;
|
|
14
|
+
/** Mm in the last hour. */
|
|
15
|
+
precipitation: number;
|
|
16
|
+
/** Metres of snow on the ground. */
|
|
17
|
+
snowDepth: number;
|
|
18
|
+
day: boolean;
|
|
19
|
+
/** The hours of the place's clock the sun rises and sets at. */
|
|
20
|
+
sunrise: number;
|
|
21
|
+
sunset: number;
|
|
22
|
+
/** Seconds the place's clock is ahead of UTC. */
|
|
23
|
+
offset: number;
|
|
24
|
+
/** When it was read (ms). */
|
|
25
|
+
at: number;
|
|
26
|
+
}
|
|
27
|
+
export declare namespace GeoWeatherUtil {
|
|
28
|
+
function kindOf(code: number, cloud?: number): WeatherKind;
|
|
29
|
+
/** The hour of the place's clock (0 to 24). */
|
|
30
|
+
function hoursAt(offset: number, now?: number): number;
|
|
31
|
+
/** `2026-11-23T06:31` as hours (6.52); none for what is not a time. */
|
|
32
|
+
function hoursOf(local: unknown): number | undefined;
|
|
33
|
+
}
|
|
34
|
+
export declare class GeoWeather {
|
|
35
|
+
protected readonly fetcher: Fetcher;
|
|
36
|
+
protected readonly base: string;
|
|
37
|
+
protected readonly clock: () => number;
|
|
38
|
+
protected readonly kept: Map<string, Promise<GeoWeatherNow>>;
|
|
39
|
+
constructor(fetcher?: Fetcher, base?: string, clock?: () => number);
|
|
40
|
+
now(place: LonLat): Promise<GeoWeatherNow>;
|
|
41
|
+
protected read(key: string, lat: string, lon: string): Promise<GeoWeatherNow>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=geo-weather.d.ts.map
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeoWeather = exports.GeoWeatherUtil = void 0;
|
|
4
|
+
/** Kept this long for the same square. */
|
|
5
|
+
const KEPT_MS = 15 * 60000;
|
|
6
|
+
var GeoWeatherUtil;
|
|
7
|
+
(function (GeoWeatherUtil) {
|
|
8
|
+
function kindOf(code, cloud = 0) {
|
|
9
|
+
if (code >= 95) {
|
|
10
|
+
return 'storm';
|
|
11
|
+
}
|
|
12
|
+
if ((code >= 71 && code <= 77) || code === 85 || code === 86) {
|
|
13
|
+
return 'snow';
|
|
14
|
+
}
|
|
15
|
+
if ((code >= 61 && code <= 67) || (code >= 80 && code <= 82)) {
|
|
16
|
+
return 'rain';
|
|
17
|
+
}
|
|
18
|
+
if (code >= 51 && code <= 57) {
|
|
19
|
+
return 'drizzle';
|
|
20
|
+
}
|
|
21
|
+
if (code === 45 || code === 48) {
|
|
22
|
+
return 'fog';
|
|
23
|
+
}
|
|
24
|
+
if (code === 3 || cloud >= 0.85) {
|
|
25
|
+
return 'overcast';
|
|
26
|
+
}
|
|
27
|
+
return code === 2 || cloud >= 0.4 ? 'cloudy' : 'clear';
|
|
28
|
+
}
|
|
29
|
+
GeoWeatherUtil.kindOf = kindOf;
|
|
30
|
+
/** The hour of the place's clock (0 to 24). */
|
|
31
|
+
function hoursAt(offset, now = Date.now()) {
|
|
32
|
+
const seconds = now / 1000 + offset;
|
|
33
|
+
return (((seconds / 3600) % 24) + 24) % 24;
|
|
34
|
+
}
|
|
35
|
+
GeoWeatherUtil.hoursAt = hoursAt;
|
|
36
|
+
/** `2026-11-23T06:31` as hours (6.52); none for what is not a time. */
|
|
37
|
+
function hoursOf(local) {
|
|
38
|
+
const match = typeof local === 'string' ? /T(\d{2}):(\d{2})/.exec(local) : null;
|
|
39
|
+
return match ? Number(match[1]) + Number(match[2]) / 60 : undefined;
|
|
40
|
+
}
|
|
41
|
+
GeoWeatherUtil.hoursOf = hoursOf;
|
|
42
|
+
})(GeoWeatherUtil || (exports.GeoWeatherUtil = GeoWeatherUtil = {}));
|
|
43
|
+
class GeoWeather {
|
|
44
|
+
constructor(fetcher = (url, init) => fetch(url, init), base = 'https://api.open-meteo.com/v1/forecast', clock = Date.now) {
|
|
45
|
+
this.fetcher = fetcher;
|
|
46
|
+
this.base = base;
|
|
47
|
+
this.clock = clock;
|
|
48
|
+
this.kept = new Map();
|
|
49
|
+
}
|
|
50
|
+
now(place) {
|
|
51
|
+
const lat = place.lat.toFixed(2);
|
|
52
|
+
const lon = place.lon.toFixed(2);
|
|
53
|
+
const key = `${lat},${lon}`;
|
|
54
|
+
const kept = this.kept.get(key);
|
|
55
|
+
if (kept) {
|
|
56
|
+
return kept.then(weather => (this.clock() - weather.at < KEPT_MS ? weather : this.read(key, lat, lon)));
|
|
57
|
+
}
|
|
58
|
+
return this.read(key, lat, lon);
|
|
59
|
+
}
|
|
60
|
+
read(key, lat, lon) {
|
|
61
|
+
const fields = 'temperature_2m,weather_code,cloud_cover,wind_speed_10m,precipitation,snow_depth,is_day';
|
|
62
|
+
const url = `${this.base}?latitude=${lat}&longitude=${lon}¤t=${fields}&daily=sunrise,sunset&forecast_days=1&timezone=auto`;
|
|
63
|
+
const made = this.fetcher(url)
|
|
64
|
+
.then(response => {
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
throw new Error(`Weather: HTTP ${response.status}`);
|
|
67
|
+
}
|
|
68
|
+
return response.json();
|
|
69
|
+
})
|
|
70
|
+
.then(reply => {
|
|
71
|
+
var _a, _b, _c, _d;
|
|
72
|
+
var _e, _f, _g, _h, _j, _k, _l, _m;
|
|
73
|
+
const now = reply.current;
|
|
74
|
+
if (!now || typeof now.weather_code !== 'number') {
|
|
75
|
+
throw new Error('Weather: nothing now');
|
|
76
|
+
}
|
|
77
|
+
const cloud = Math.max(0, Math.min(1, ((_e = now.cloud_cover) !== null && _e !== void 0 ? _e : 0) / 100));
|
|
78
|
+
return {
|
|
79
|
+
kind: GeoWeatherUtil.kindOf(now.weather_code, cloud),
|
|
80
|
+
code: now.weather_code,
|
|
81
|
+
temperature: (_f = now.temperature_2m) !== null && _f !== void 0 ? _f : 0,
|
|
82
|
+
cloud,
|
|
83
|
+
wind: (_g = now.wind_speed_10m) !== null && _g !== void 0 ? _g : 0,
|
|
84
|
+
precipitation: (_h = now.precipitation) !== null && _h !== void 0 ? _h : 0,
|
|
85
|
+
snowDepth: (_j = now.snow_depth) !== null && _j !== void 0 ? _j : 0,
|
|
86
|
+
day: now.is_day !== 0,
|
|
87
|
+
sunrise: (_k = GeoWeatherUtil.hoursOf((_b = (_a = reply.daily) === null || _a === void 0 ? void 0 : _a.sunrise) === null || _b === void 0 ? void 0 : _b[0])) !== null && _k !== void 0 ? _k : 6,
|
|
88
|
+
sunset: (_l = GeoWeatherUtil.hoursOf((_d = (_c = reply.daily) === null || _c === void 0 ? void 0 : _c.sunset) === null || _d === void 0 ? void 0 : _d[0])) !== null && _l !== void 0 ? _l : 18.5,
|
|
89
|
+
offset: (_m = reply.utc_offset_seconds) !== null && _m !== void 0 ? _m : 0,
|
|
90
|
+
at: this.clock(),
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
made.catch(() => this.kept.delete(key));
|
|
94
|
+
this.kept.delete(key);
|
|
95
|
+
this.kept.set(key, made);
|
|
96
|
+
while (this.kept.size > 64) {
|
|
97
|
+
this.kept.delete(this.kept.keys().next().value);
|
|
98
|
+
}
|
|
99
|
+
return made;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.GeoWeather = GeoWeather;
|
|
103
|
+
//# sourceMappingURL=geo-weather.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './geo-elevation';
|
|
2
|
+
export * from './geo-grid';
|
|
3
|
+
export * from './geo-mvt';
|
|
4
|
+
export * from './geo-pixels';
|
|
5
|
+
export * from './geo-places';
|
|
6
|
+
export * from './geo-raster';
|
|
7
|
+
export * from './geo-tiles';
|
|
8
|
+
export * from './geo-weather';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./geo-elevation"), exports);
|
|
18
|
+
__exportStar(require("./geo-grid"), exports);
|
|
19
|
+
__exportStar(require("./geo-mvt"), exports);
|
|
20
|
+
__exportStar(require("./geo-pixels"), exports);
|
|
21
|
+
__exportStar(require("./geo-places"), exports);
|
|
22
|
+
__exportStar(require("./geo-raster"), exports);
|
|
23
|
+
__exportStar(require("./geo-tiles"), exports);
|
|
24
|
+
__exportStar(require("./geo-weather"), exports);
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-geo",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.31.0",
|
|
4
|
+
"description": "Real places for Ta's extensions: a grid of plots over the earth, vector tiles of OpenStreetMap decoded, places searched, the device's location, and what is on the ground of a plot as a grid",
|
|
5
|
+
"homepage": "https://github.com/cellbang/ta",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/cellbang/ta/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"typings": "lib/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"lib",
|
|
18
|
+
"esm",
|
|
19
|
+
"!lib/**/*.spec.*",
|
|
20
|
+
"!**/*.map"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc -b && bun ../../scripts/build-esm.ts",
|
|
24
|
+
"clean": "rm -rf lib esm *.tsbuildinfo",
|
|
25
|
+
"test": "bun test ./lib",
|
|
26
|
+
"watch": "tsc -b -w --preserveWatchOutput"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"import": "./esm/index.js",
|
|
34
|
+
"default": "./lib/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json",
|
|
37
|
+
"./lib/*.js": {
|
|
38
|
+
"import": "./esm/*.js",
|
|
39
|
+
"default": "./lib/*.js"
|
|
40
|
+
},
|
|
41
|
+
"./lib/*": {
|
|
42
|
+
"import": "./esm/*.js",
|
|
43
|
+
"default": "./lib/*.js"
|
|
44
|
+
},
|
|
45
|
+
"./*": "./*"
|
|
46
|
+
}
|
|
47
|
+
}
|