@maptiler/client 1.7.0 → 1.8.1
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/.github/pull_request_template.md +11 -0
- package/.github/workflows/format-lint.yml +24 -0
- package/CHANGELOG.md +32 -0
- package/dist/maptiler-client.cjs +680 -306
- package/dist/maptiler-client.cjs.map +1 -1
- package/dist/maptiler-client.d.ts +270 -100
- package/dist/maptiler-client.min.mjs +1 -1
- package/dist/maptiler-client.mjs +380 -294
- package/dist/maptiler-client.mjs.map +1 -1
- package/package.json +10 -8
- package/readme.md +133 -1
- package/rollup.config.js +3 -3
- package/dist/maptiler-client.umd.js +0 -1541
- package/dist/maptiler-client.umd.js.map +0 -1
- package/dist/maptiler-client.umd.min.js +0 -1
- package/examples/test-browser-es.html +0 -103
- package/examples/test-browser-umd.html +0 -90
- package/examples/test-node.js +0 -52
- package/src/callFetch.ts +0 -18
- package/src/config.ts +0 -68
- package/src/defaults.ts +0 -11
- package/src/demProfiler/getElevation.ts +0 -46
- package/src/demProfiler/index.ts +0 -240
- package/src/demProfiler/tileCover.ts +0 -96
- package/src/geometry/area.ts +0 -96
- package/src/geometry/distance.ts +0 -45
- package/src/geometry/index.ts +0 -3
- package/src/geometry/util.ts +0 -34
- package/src/index.ts +0 -114
- package/src/language.ts +0 -87
- package/src/mapstyle.ts +0 -968
- package/src/services/ServiceError.ts +0 -13
- package/src/services/coordinates.ts +0 -263
- package/src/services/data.ts +0 -63
- package/src/services/geocoding.ts +0 -461
- package/src/services/geolocation.ts +0 -139
- package/src/services/simplify.ts +0 -87
- package/src/services/staticMaps.ts +0 -463
package/examples/test-node.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
config,
|
|
3
|
-
geocoding,
|
|
4
|
-
geolocation,
|
|
5
|
-
coordinates,
|
|
6
|
-
data,
|
|
7
|
-
LanguageGeocoding,
|
|
8
|
-
} from '../dist/maptiler-client.mjs';
|
|
9
|
-
|
|
10
|
-
// For this examople to work, you must bring your own node-compatible fetch,
|
|
11
|
-
// unles you are using a version of Nodejs that already contains fetch (>=18)
|
|
12
|
-
// import fetch from 'node-fetch';
|
|
13
|
-
|
|
14
|
-
// config.fetch = fetch;
|
|
15
|
-
config.apiKey = 'YOUR_MAPTILER_CLOUD_API_KEY';
|
|
16
|
-
|
|
17
|
-
async function testGeocoding() {
|
|
18
|
-
// const result1 = await geocoding.forward('bordeaux', {language: [LanguageGeocoding.AUTO, LanguageGeocoding.ENGLISH]});
|
|
19
|
-
// console.log(result1);
|
|
20
|
-
|
|
21
|
-
const result2 = await geocoding.reverse([6.249638, 46.402056], {language: ['es', 'en']});
|
|
22
|
-
console.log(result2);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async function testGeolocation() {
|
|
26
|
-
const result = await geolocation.info();
|
|
27
|
-
console.log(result);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async function testCoordinates() {
|
|
31
|
-
// searching
|
|
32
|
-
console.log(await coordinates.search('mercator', {transformations: true}));
|
|
33
|
-
console.log(await coordinates.search('plate carree'));
|
|
34
|
-
console.log(await coordinates.search('france'));
|
|
35
|
-
console.log(await coordinates.search('4326', {transformations: true}));
|
|
36
|
-
console.log(await coordinates.search('4326'));
|
|
37
|
-
|
|
38
|
-
// Transforming from wgs84 (default) to lambert93
|
|
39
|
-
console.log(await coordinates.transform({lng: 1, lat: 45}, {targetCrs: 9793}));
|
|
40
|
-
console.log(await coordinates.transform([{lng: 10, lat: 48},{lng: 1, lat: 45}], {targetCrs: 9793}));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async function testData() {
|
|
44
|
-
console.log(await data.get('2dd5ecc4-3ae1-4d1e-99a2-182256486357'));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
(async () => {
|
|
48
|
-
await testGeocoding();
|
|
49
|
-
// await testGeolocation();
|
|
50
|
-
// await testCoordinates();
|
|
51
|
-
// await testData();
|
|
52
|
-
})()
|
package/src/callFetch.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { config } from "./config";
|
|
2
|
-
|
|
3
|
-
export async function callFetch(resource, options = {}) {
|
|
4
|
-
if (config.fetch === null) {
|
|
5
|
-
throw new Error(
|
|
6
|
-
"The fetch function was not found. If on NodeJS < 18 please specify the fetch function with config.fetch",
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// Control if URL contains the api key
|
|
11
|
-
if (new URL(resource).searchParams.get("key").trim() === "") {
|
|
12
|
-
throw new Error(
|
|
13
|
-
"The MapTiler Cloud API key is missing. Set it in `config.apiKey` or get one for free at https://maptiler.com",
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return config.fetch(resource, options);
|
|
18
|
-
}
|
package/src/config.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
export type FetchFunction = (
|
|
2
|
-
input: RequestInfo | URL,
|
|
3
|
-
init?: RequestInit,
|
|
4
|
-
) => Promise<Response>;
|
|
5
|
-
|
|
6
|
-
function tryGettingFetch() {
|
|
7
|
-
// this is browser, fetch exists
|
|
8
|
-
if (typeof self !== "undefined") {
|
|
9
|
-
return fetch.bind(self);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (typeof global !== "undefined" && global.fetch) {
|
|
13
|
-
return global.fetch;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The configuration object definition
|
|
21
|
-
*/
|
|
22
|
-
class ClientConfig {
|
|
23
|
-
/**
|
|
24
|
-
* MapTiler Cloud API key
|
|
25
|
-
*/
|
|
26
|
-
private _apiKey = "";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The fetch function. To be set if in Node < 18, otherwise
|
|
30
|
-
* will be automatically resolved.
|
|
31
|
-
*/
|
|
32
|
-
private _fetch: FetchFunction | null = tryGettingFetch();
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Set the MapTiler Cloud API key
|
|
36
|
-
*/
|
|
37
|
-
set apiKey(k: string) {
|
|
38
|
-
this._apiKey = k;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Get the MapTiler Cloud API key
|
|
43
|
-
*/
|
|
44
|
-
get apiKey(): string {
|
|
45
|
-
return this._apiKey;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Set a the custom fetch function to replace the default one
|
|
50
|
-
*/
|
|
51
|
-
set fetch(f: FetchFunction) {
|
|
52
|
-
this._fetch = f;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Get the fetch fucntion
|
|
57
|
-
*/
|
|
58
|
-
get fetch(): FetchFunction | null {
|
|
59
|
-
return this._fetch;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Configuration object
|
|
65
|
-
*/
|
|
66
|
-
const config = new ClientConfig();
|
|
67
|
-
|
|
68
|
-
export { ClientConfig, config };
|
package/src/defaults.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { llToTilePx } from "web-merc-projection";
|
|
2
|
-
|
|
3
|
-
import type { TileImage } from "./";
|
|
4
|
-
|
|
5
|
-
export type ElevationParser = (
|
|
6
|
-
r: number,
|
|
7
|
-
g: number,
|
|
8
|
-
b: number,
|
|
9
|
-
a: number,
|
|
10
|
-
) => number;
|
|
11
|
-
|
|
12
|
-
/** Given an RGB or RBGA image and lon-lat coordinate, determine the elevation */
|
|
13
|
-
export default function getElevation(
|
|
14
|
-
coord: [lon: number, lat: number],
|
|
15
|
-
tile: [zoom: number, x: number, y: number],
|
|
16
|
-
tileSize: number,
|
|
17
|
-
tileImage: TileImage,
|
|
18
|
-
elevationParser: ElevationParser = defaultElevationParser,
|
|
19
|
-
): number {
|
|
20
|
-
const { channels, image } = tileImage;
|
|
21
|
-
let [x, y] = llToTilePx(coord, tile, tileSize);
|
|
22
|
-
x = clampPixel(x, tileSize);
|
|
23
|
-
y = clampPixel(y, tileSize);
|
|
24
|
-
const index = (y * tileSize + x) * channels;
|
|
25
|
-
|
|
26
|
-
return elevationParser(
|
|
27
|
-
image[index],
|
|
28
|
-
image[index + 1],
|
|
29
|
-
image[index + 2],
|
|
30
|
-
channels === 4 ? image[index + 3] : 0,
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Clamp the pixels to the 0-tileSize bounds */
|
|
35
|
-
function clampPixel(n: number, tileSize: number): number {
|
|
36
|
-
return Math.max(0, Math.min(tileSize, Math.floor(n * tileSize)));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Default elevation parser used by Mapbox and Maplibre. Result is in meters */
|
|
40
|
-
export function defaultElevationParser(
|
|
41
|
-
r: number,
|
|
42
|
-
g: number,
|
|
43
|
-
b: number,
|
|
44
|
-
): number {
|
|
45
|
-
return -10000 + (r * 256 * 256 + g * 256 + b) * 0.1;
|
|
46
|
-
}
|
package/src/demProfiler/index.ts
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import tileCover from "./tileCover";
|
|
2
|
-
import getElevation from "./getElevation";
|
|
3
|
-
import { pointDistance, mToFt } from "../geometry";
|
|
4
|
-
|
|
5
|
-
import type { TileCoverCoordinates, TileID } from "./tileCover";
|
|
6
|
-
import type { ElevationParser } from "./getElevation";
|
|
7
|
-
import type { Feature, LineString } from "geojson";
|
|
8
|
-
|
|
9
|
-
export * from "./getElevation";
|
|
10
|
-
export * from "./tileCover";
|
|
11
|
-
|
|
12
|
-
export interface TileImage {
|
|
13
|
-
channels: 1 | 2 | 3 | 4;
|
|
14
|
-
image: Uint8ClampedArray;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type TileRequest = (
|
|
18
|
-
x: number,
|
|
19
|
-
y: number,
|
|
20
|
-
zoom: number,
|
|
21
|
-
) => Promise<TileImage>;
|
|
22
|
-
|
|
23
|
-
export interface Options {
|
|
24
|
-
/** type of metric to use. Meters or feet. Default: meters */
|
|
25
|
-
metric?: "m" | "ft";
|
|
26
|
-
/** Zoom that is queried from the server. Default: 13 */
|
|
27
|
-
zoom?: number;
|
|
28
|
-
/** Tile size of the images returned. Default 512 */
|
|
29
|
-
tileSize?: number;
|
|
30
|
-
/** Tile Request method */
|
|
31
|
-
tileRequest: TileRequest;
|
|
32
|
-
/** Elevation parser. Default: elevation = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1) */
|
|
33
|
-
elevationParser?: ElevationParser;
|
|
34
|
-
/** smooth out the elevation to make the visual aesthetic nicer */
|
|
35
|
-
smooth?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface ElevPoint {
|
|
39
|
-
/** Distance along path from the starting point in the metric defind by options */
|
|
40
|
-
distance: number;
|
|
41
|
-
/** Elevation of the point */
|
|
42
|
-
elevation: number;
|
|
43
|
-
/** Coordinates of the point in lat-lon */
|
|
44
|
-
coordinate: [lon: number, lat: number];
|
|
45
|
-
/** Tile that the point is in */
|
|
46
|
-
tile: TileID;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface Output {
|
|
50
|
-
/** Total length of the path in the metric defined by options */
|
|
51
|
-
distance: number;
|
|
52
|
-
/** Minimum elevation of the path */
|
|
53
|
-
minElevation: number;
|
|
54
|
-
/** Maximum elevation of the path */
|
|
55
|
-
maxElevation: number;
|
|
56
|
-
/** Average elevation of the path */
|
|
57
|
-
avgElevation: number;
|
|
58
|
-
/** Elevation at the start of the path */
|
|
59
|
-
startElevation: number;
|
|
60
|
-
/** Elevation at the end of the path */
|
|
61
|
-
endElevation: number;
|
|
62
|
-
/** Array of points along the path */
|
|
63
|
-
points: ElevPoint[];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Given a GeoJSON LineString or Feature<Linestring>, return the elevation data for the path.
|
|
68
|
-
* This algorithm will automatically break the path into denser segments relative to the zoom level if necessary.
|
|
69
|
-
*/
|
|
70
|
-
export async function profileLineString(
|
|
71
|
-
path: Feature<LineString> | LineString,
|
|
72
|
-
options: Options,
|
|
73
|
-
): Promise<Output> {
|
|
74
|
-
// get the tile cover
|
|
75
|
-
const coordinates =
|
|
76
|
-
"geometry" in path ? path.geometry.coordinates : path.coordinates;
|
|
77
|
-
const { coords, tiles } = tileCover(
|
|
78
|
-
coordinates,
|
|
79
|
-
options.zoom ?? 13,
|
|
80
|
-
options.tileSize ?? 512,
|
|
81
|
-
);
|
|
82
|
-
// get tiles
|
|
83
|
-
const tileCache = await getTiles(tiles, options.tileRequest);
|
|
84
|
-
// get the elevation data
|
|
85
|
-
let points = getElevations(
|
|
86
|
-
coords,
|
|
87
|
-
tileCache,
|
|
88
|
-
options.tileSize ?? 512,
|
|
89
|
-
options.elevationParser,
|
|
90
|
-
);
|
|
91
|
-
// smooth the elevation data if needed
|
|
92
|
-
if (options.smooth === true) points = smoothElevation(points);
|
|
93
|
-
// calculate the output
|
|
94
|
-
let output = buildOutput(points);
|
|
95
|
-
// convert to miles if needed
|
|
96
|
-
if (options.metric === "ft") output = toFeet(output);
|
|
97
|
-
|
|
98
|
-
return output;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Request all the tiles we need */
|
|
102
|
-
async function getTiles(
|
|
103
|
-
tiles: TileID[],
|
|
104
|
-
tileRequest: TileRequest,
|
|
105
|
-
): Promise<Map<number, TileImage>> {
|
|
106
|
-
const tileCache = new Map<number, TileImage>();
|
|
107
|
-
|
|
108
|
-
// request all the tiles we need
|
|
109
|
-
const requests: Array<Promise<TileImage | undefined>> = [];
|
|
110
|
-
for (const tile of tiles) {
|
|
111
|
-
requests.push(
|
|
112
|
-
tileRequest(tile.x, tile.y, tile.z)
|
|
113
|
-
.then((res: TileImage): TileImage => {
|
|
114
|
-
tileCache.set(tile.id, res);
|
|
115
|
-
return res;
|
|
116
|
-
})
|
|
117
|
-
.catch((err: unknown) => {
|
|
118
|
-
console.error(err);
|
|
119
|
-
return undefined;
|
|
120
|
-
}),
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
// wait for all the requests to finish
|
|
124
|
-
await Promise.allSettled(requests);
|
|
125
|
-
|
|
126
|
-
return tileCache;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Get the elevation data for each coordinate */
|
|
130
|
-
function getElevations(
|
|
131
|
-
coords: TileCoverCoordinates[],
|
|
132
|
-
tileCache: Map<number, TileImage>,
|
|
133
|
-
tileSize: number,
|
|
134
|
-
elevationParser?: ElevationParser,
|
|
135
|
-
): ElevPoint[] {
|
|
136
|
-
const points: ElevPoint[] = [];
|
|
137
|
-
|
|
138
|
-
// get the elevation data for each coordinate
|
|
139
|
-
let curDistance = 0;
|
|
140
|
-
let prevCoord: [lon: number, lat: number] | undefined;
|
|
141
|
-
for (const { tile, coordinate } of coords) {
|
|
142
|
-
const cTile = tileCache.get(tile.id);
|
|
143
|
-
if (cTile === undefined)
|
|
144
|
-
throw new Error(
|
|
145
|
-
`Missing tile ${tile.id} (${tile.x}-${tile.y}-${tile.z})`,
|
|
146
|
-
);
|
|
147
|
-
const elevation = getElevation(
|
|
148
|
-
coordinate,
|
|
149
|
-
[tile.z, tile.x, tile.y],
|
|
150
|
-
tileSize,
|
|
151
|
-
cTile,
|
|
152
|
-
elevationParser,
|
|
153
|
-
);
|
|
154
|
-
points.push({
|
|
155
|
-
distance: curDistance,
|
|
156
|
-
elevation,
|
|
157
|
-
coordinate,
|
|
158
|
-
tile,
|
|
159
|
-
});
|
|
160
|
-
// calculate the distance between the points
|
|
161
|
-
if (prevCoord !== undefined) {
|
|
162
|
-
curDistance += pointDistance(prevCoord, coordinate);
|
|
163
|
-
}
|
|
164
|
-
prevCoord = coordinate;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return points;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** Improve the output to include metrics about the max/min/avg elevation */
|
|
171
|
-
function buildOutput(points: ElevPoint[]): Output {
|
|
172
|
-
// setup variables
|
|
173
|
-
let minElevation = Infinity;
|
|
174
|
-
let maxElevation = -Infinity;
|
|
175
|
-
let totalElevation = 0;
|
|
176
|
-
// calcuate min, max, and average
|
|
177
|
-
for (const point of points) {
|
|
178
|
-
if (point.elevation < minElevation) minElevation = point.elevation;
|
|
179
|
-
if (point.elevation > maxElevation) maxElevation = point.elevation;
|
|
180
|
-
totalElevation += point.elevation;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return {
|
|
184
|
-
distance: points[points.length - 1].distance,
|
|
185
|
-
minElevation,
|
|
186
|
-
maxElevation,
|
|
187
|
-
avgElevation: totalElevation / points.length,
|
|
188
|
-
startElevation: points[0].elevation,
|
|
189
|
-
endElevation: points[points.length - 1].elevation,
|
|
190
|
-
points,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Smooth function to make the elevation appear more aesthetically
|
|
196
|
-
* pleaseing without changing the values greatly
|
|
197
|
-
*/
|
|
198
|
-
function smoothElevation(points: ElevPoint[]): ElevPoint[] {
|
|
199
|
-
const newPoints: ElevPoint[] = [];
|
|
200
|
-
let prevPoint: ElevPoint | undefined;
|
|
201
|
-
for (let i = 0; i < points.length; i++) {
|
|
202
|
-
const point = points[i];
|
|
203
|
-
const nextPoint = points[i + 1] ?? points[i];
|
|
204
|
-
if (prevPoint === undefined) {
|
|
205
|
-
newPoints.push(point);
|
|
206
|
-
} else {
|
|
207
|
-
const newPoint: ElevPoint = {
|
|
208
|
-
distance: point.distance,
|
|
209
|
-
elevation:
|
|
210
|
-
(prevPoint.elevation + point.elevation + nextPoint.elevation) / 3,
|
|
211
|
-
coordinate: point.coordinate,
|
|
212
|
-
tile: point.tile,
|
|
213
|
-
};
|
|
214
|
-
newPoints.push(newPoint);
|
|
215
|
-
}
|
|
216
|
-
prevPoint = point;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return newPoints;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/** Convert all output properties from km to miles */
|
|
223
|
-
function toFeet(input: Output): Output {
|
|
224
|
-
const output: Output = {
|
|
225
|
-
distance: mToFt(input.distance),
|
|
226
|
-
minElevation: mToFt(input.minElevation),
|
|
227
|
-
maxElevation: mToFt(input.maxElevation),
|
|
228
|
-
avgElevation: mToFt(input.avgElevation),
|
|
229
|
-
startElevation: mToFt(input.startElevation),
|
|
230
|
-
endElevation: mToFt(input.endElevation),
|
|
231
|
-
points: input.points.map(({ distance, elevation, coordinate, tile }) => ({
|
|
232
|
-
distance: mToFt(distance),
|
|
233
|
-
elevation: mToFt(elevation),
|
|
234
|
-
coordinate,
|
|
235
|
-
tile,
|
|
236
|
-
})),
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
return output;
|
|
240
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { llToTile } from "web-merc-projection";
|
|
2
|
-
import {
|
|
3
|
-
pointDistance,
|
|
4
|
-
getZoomLevelResolution,
|
|
5
|
-
xyzToTileID,
|
|
6
|
-
} from "../geometry";
|
|
7
|
-
|
|
8
|
-
import type { Point } from "web-merc-projection";
|
|
9
|
-
|
|
10
|
-
export interface TileID {
|
|
11
|
-
id: number;
|
|
12
|
-
x: number;
|
|
13
|
-
y: number;
|
|
14
|
-
z: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface TileCoverCoordinates {
|
|
18
|
-
/** Store the coordinates as an ll-pair */
|
|
19
|
-
coordinate: Point;
|
|
20
|
-
/** Track the tile relative to the point */
|
|
21
|
-
tile: TileID;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface TileCoverOutput {
|
|
25
|
-
coords: TileCoverCoordinates[];
|
|
26
|
-
tiles: TileID[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Given LineString coordinates, return the web mercator tiles that all the points contain.
|
|
31
|
-
* Breaks the coordinates down into points that are relative to the zoom.
|
|
32
|
-
*/
|
|
33
|
-
export default function tileCover(
|
|
34
|
-
coordinates: GeoJSON.Position[],
|
|
35
|
-
zoom: number,
|
|
36
|
-
tileSize: number,
|
|
37
|
-
): TileCoverOutput {
|
|
38
|
-
const tileHash = new Map<number, TileID>(); // tileHash => { id, x, y, z }
|
|
39
|
-
const tiles: TileID[] = [];
|
|
40
|
-
const coords: TileCoverCoordinates[] = [];
|
|
41
|
-
|
|
42
|
-
// get the resolution of the zoom level
|
|
43
|
-
const resolution = getZoomLevelResolution(coordinates[0][1], zoom, tileSize);
|
|
44
|
-
|
|
45
|
-
// migrate from coordinates to "samples" that are relative to the zoom
|
|
46
|
-
const samples = sampleProfileLine(coordinates, resolution);
|
|
47
|
-
|
|
48
|
-
for (const sample of samples) {
|
|
49
|
-
const coordinate = sample as unknown as [lon: number, lat: number];
|
|
50
|
-
// file the file that the coordinate is in
|
|
51
|
-
const [tileX, tileY] = llToTile(coordinate, zoom, tileSize);
|
|
52
|
-
// create the tile and store it
|
|
53
|
-
const id = xyzToTileID(tileX, tileY, zoom);
|
|
54
|
-
const tile: TileID = { id, x: tileX, y: tileY, z: zoom };
|
|
55
|
-
tileHash.set(id, tile);
|
|
56
|
-
// store the coordinates
|
|
57
|
-
coords.push({ coordinate, tile });
|
|
58
|
-
}
|
|
59
|
-
// store the tiles we've found
|
|
60
|
-
for (const tile of tileHash.values()) tiles.push(tile);
|
|
61
|
-
|
|
62
|
-
return { coords, tiles };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** convert coordinates to samples that have a max distance of the zoom level resolution */
|
|
66
|
-
function sampleProfileLine(
|
|
67
|
-
coordinates: GeoJSON.Position[],
|
|
68
|
-
resolution: number,
|
|
69
|
-
): GeoJSON.Position[] {
|
|
70
|
-
const samples: GeoJSON.Position[] = [];
|
|
71
|
-
|
|
72
|
-
let prevCoord: GeoJSON.Position | undefined;
|
|
73
|
-
for (const coord of coordinates) {
|
|
74
|
-
if (prevCoord !== undefined) {
|
|
75
|
-
const dist = pointDistance(
|
|
76
|
-
prevCoord as unknown as [lon: number, lat: number],
|
|
77
|
-
coord as unknown as [lon: number, lat: number],
|
|
78
|
-
);
|
|
79
|
-
const numSamples = Math.ceil(dist / resolution);
|
|
80
|
-
|
|
81
|
-
for (let i = 0; i <= numSamples - 1; i++) {
|
|
82
|
-
const sample = [
|
|
83
|
-
prevCoord[0] + (coord[0] - prevCoord[0]) * (i / numSamples),
|
|
84
|
-
prevCoord[1] + (coord[1] - prevCoord[1]) * (i / numSamples),
|
|
85
|
-
];
|
|
86
|
-
samples.push(sample);
|
|
87
|
-
}
|
|
88
|
-
} else {
|
|
89
|
-
samples.push(coord);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
prevCoord = coord;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return samples;
|
|
96
|
-
}
|
package/src/geometry/area.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
// ripped from https://github.com/Turfjs/turf/blob/master/packages/turf-area/index.ts
|
|
2
|
-
import { degToRad, earthRadius } from "./util";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Finds the area of a Polygon or MultiPolygon in square meters.
|
|
6
|
-
*/
|
|
7
|
-
export function area(
|
|
8
|
-
area:
|
|
9
|
-
| GeoJSON.Feature<GeoJSON.Polygon | GeoJSON.MultiPolygon>
|
|
10
|
-
| GeoJSON.Polygon
|
|
11
|
-
| GeoJSON.MultiPolygon,
|
|
12
|
-
) {
|
|
13
|
-
const geometry = "geometry" in area ? area.geometry : area;
|
|
14
|
-
const type = geometry.type;
|
|
15
|
-
if (type === "MultiPolygon") {
|
|
16
|
-
return multiPolygonArea(geometry.coordinates);
|
|
17
|
-
}
|
|
18
|
-
return polygonArea(geometry.coordinates);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Finds the area of a MultiPolygon in square meters.
|
|
23
|
-
*/
|
|
24
|
-
export function multiPolygonArea(
|
|
25
|
-
multiPoly: GeoJSON.MultiPolygon | GeoJSON.Position[][][],
|
|
26
|
-
): number {
|
|
27
|
-
const coords = "coordinates" in multiPoly ? multiPoly.coordinates : multiPoly;
|
|
28
|
-
let total = 0;
|
|
29
|
-
for (const polygon of coords) {
|
|
30
|
-
total += polygonArea(polygon);
|
|
31
|
-
}
|
|
32
|
-
return total;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Finds the area of a Polygon in square meters.
|
|
37
|
-
*/
|
|
38
|
-
export function polygonArea(
|
|
39
|
-
poly: GeoJSON.Polygon | GeoJSON.Position[][],
|
|
40
|
-
): number {
|
|
41
|
-
const coords = "coordinates" in poly ? poly.coordinates : poly;
|
|
42
|
-
let total = 0;
|
|
43
|
-
for (const ring of coords) {
|
|
44
|
-
total += ringArea(ring);
|
|
45
|
-
}
|
|
46
|
-
return total;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Calculate the approximate area of the polygon were it projected onto the earth.
|
|
51
|
-
* Note that this area will be positive if ring is oriented clockwise, otherwise it will be negative.
|
|
52
|
-
*
|
|
53
|
-
* Reference:
|
|
54
|
-
* Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for Polygons on a Sphere",
|
|
55
|
-
* JPL Publication 07-03, Jet Propulsion
|
|
56
|
-
* Laboratory, Pasadena, CA, June 2007 https://trs.jpl.nasa.gov/handle/2014/40409
|
|
57
|
-
*/
|
|
58
|
-
function ringArea(coords: GeoJSON.Position[]): number {
|
|
59
|
-
let p1;
|
|
60
|
-
let p2;
|
|
61
|
-
let p3;
|
|
62
|
-
let lowerIndex;
|
|
63
|
-
let middleIndex;
|
|
64
|
-
let upperIndex;
|
|
65
|
-
let i;
|
|
66
|
-
let total = 0;
|
|
67
|
-
const coordsLength = coords.length;
|
|
68
|
-
|
|
69
|
-
if (coordsLength > 2) {
|
|
70
|
-
for (i = 0; i < coordsLength; i++) {
|
|
71
|
-
if (i === coordsLength - 2) {
|
|
72
|
-
// i = N-2
|
|
73
|
-
lowerIndex = coordsLength - 2;
|
|
74
|
-
middleIndex = coordsLength - 1;
|
|
75
|
-
upperIndex = 0;
|
|
76
|
-
} else if (i === coordsLength - 1) {
|
|
77
|
-
// i = N-1
|
|
78
|
-
lowerIndex = coordsLength - 1;
|
|
79
|
-
middleIndex = 0;
|
|
80
|
-
upperIndex = 1;
|
|
81
|
-
} else {
|
|
82
|
-
// i = 0 to N-3
|
|
83
|
-
lowerIndex = i;
|
|
84
|
-
middleIndex = i + 1;
|
|
85
|
-
upperIndex = i + 2;
|
|
86
|
-
}
|
|
87
|
-
p1 = coords[lowerIndex];
|
|
88
|
-
p2 = coords[middleIndex];
|
|
89
|
-
p3 = coords[upperIndex];
|
|
90
|
-
total += (degToRad(p3[0]) - degToRad(p1[0])) * Math.sin(degToRad(p2[1]));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
total = (total * earthRadius * earthRadius) / 2;
|
|
94
|
-
}
|
|
95
|
-
return Math.abs(total);
|
|
96
|
-
}
|
package/src/geometry/distance.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { earthRadius, degToRad } from "./util";
|
|
2
|
-
|
|
3
|
-
/** Get the distance of a LineString in meters */
|
|
4
|
-
export function lineDistance(
|
|
5
|
-
line:
|
|
6
|
-
| GeoJSON.Feature<GeoJSON.LineString>
|
|
7
|
-
| GeoJSON.LineString
|
|
8
|
-
| GeoJSON.Position[],
|
|
9
|
-
) {
|
|
10
|
-
// grab the coordinates
|
|
11
|
-
const coordinates =
|
|
12
|
-
"geometry" in line
|
|
13
|
-
? line.geometry.coordinates
|
|
14
|
-
: "coordinates" in line
|
|
15
|
-
? line.coordinates
|
|
16
|
-
: line;
|
|
17
|
-
// iterate through the coordinates and calculate the distance
|
|
18
|
-
let distance = 0;
|
|
19
|
-
let prevCoord: GeoJSON.Position | undefined;
|
|
20
|
-
for (const coordinate of coordinates) {
|
|
21
|
-
if (prevCoord !== undefined) {
|
|
22
|
-
distance += pointDistance(prevCoord, coordinate);
|
|
23
|
-
}
|
|
24
|
-
prevCoord = coordinate;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return distance;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Get the distance between two lon-lat pairs in meters */
|
|
31
|
-
export function pointDistance(
|
|
32
|
-
from: GeoJSON.Position,
|
|
33
|
-
to: GeoJSON.Position,
|
|
34
|
-
): number {
|
|
35
|
-
const { pow, sin, cos, sqrt, atan2 } = Math;
|
|
36
|
-
const dLat = degToRad(to[1] - from[1]);
|
|
37
|
-
const dLon = degToRad(to[0] - from[0]);
|
|
38
|
-
const lat1 = degToRad(from[1]);
|
|
39
|
-
const lat2 = degToRad(to[1]);
|
|
40
|
-
|
|
41
|
-
const a =
|
|
42
|
-
pow(sin(dLat / 2), 2) + pow(sin(dLon / 2), 2) * cos(lat1) * cos(lat2);
|
|
43
|
-
|
|
44
|
-
return 2 * atan2(sqrt(a), sqrt(1 - a)) * earthRadius;
|
|
45
|
-
}
|
package/src/geometry/index.ts
DELETED