@mapcairn/core 0.1.0-rc.1 → 0.1.0-rc.3
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.md +3 -1
- package/dist/contracts/mapAdapter.contract.js +29 -0
- package/dist/contracts/mapAdapter.contract.js.map +1 -1
- package/dist/directions/directions.js +10 -21
- package/dist/directions/directions.js.map +1 -1
- package/dist/fakes/FakeLayerSource.js +3 -3
- package/dist/fakes/FakeLayerSource.js.map +1 -1
- package/dist/fakes/FakeMapAdapter.js +1 -1
- package/dist/fakes/FakeMapAdapter.js.map +1 -1
- package/dist/index.d.ts +78 -10
- package/dist/index.js +4 -3
- package/dist/layers/feed.js +86 -9
- package/dist/layers/feed.js.map +1 -1
- package/dist/layers/query.js +82 -8
- package/dist/layers/query.js.map +1 -1
- package/dist/spatial/boundsOfPaths.js +122 -0
- package/dist/spatial/boundsOfPaths.js.map +1 -0
- package/dist/spatial/boundsOfPoints.js +26 -13
- package/dist/spatial/boundsOfPoints.js.map +1 -1
- package/package.json +1 -1
package/dist/layers/feed.js
CHANGED
|
@@ -3,6 +3,8 @@ import { resolveControlValues, templateValues } from "./controls.js";
|
|
|
3
3
|
import { decodeWith } from "./decoders.js";
|
|
4
4
|
import { isFeedLayer, isShapeStyle, loadsFeatures } from "./definition.js";
|
|
5
5
|
import { matchesFilter } from "./filter.js";
|
|
6
|
+
import { jsonEqual } from "./jsonEqual.js";
|
|
7
|
+
import { queryBoxes } from "./query.js";
|
|
6
8
|
import { fillUrlTemplate } from "./template.js";
|
|
7
9
|
// src/layers/feed.ts
|
|
8
10
|
var GEOMETRIES = {
|
|
@@ -26,7 +28,67 @@ function decodeFeed(definition, raw, decoders) {
|
|
|
26
28
|
const drawn = isFeedLayer(definition) && isShapeStyle(definition.style) ? GEOMETRIES[definition.style.geometry] : GEOMETRIES.points;
|
|
27
29
|
const features = decodeWith(decoders, decoder, raw, fields).filter((feature) => drawn.includes(feature.geometry.type) && (!filter || matchesFilter(feature, filter)));
|
|
28
30
|
if (features.length === 0) throw new LayerError("empty");
|
|
29
|
-
return definition
|
|
31
|
+
return capQuery(definition, features);
|
|
32
|
+
}
|
|
33
|
+
/** A query layer's features cut to one more than its `maxFeatures`; any other layer's, whole. */
|
|
34
|
+
function capQuery(definition, features) {
|
|
35
|
+
return loadsFeatures(definition) && definition.source.kind === "query" ? features.slice(0, definition.source.maxFeatures + 1) : features;
|
|
36
|
+
}
|
|
37
|
+
/** What decoding one of several responses gives: its features, or none when it held none. */
|
|
38
|
+
function decodeOrNothing(definition, raw, decoders) {
|
|
39
|
+
try {
|
|
40
|
+
return decodeFeed(definition, raw, decoders);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof LayerError && error.kind === "empty") return [];
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Joins decoded responses. A feature with the id and the geometry of one
|
|
48
|
+
* already kept is the same feature, seen from both boxes, and is dropped.
|
|
49
|
+
* A different feature that has the same id (a provider that reuses one, or
|
|
50
|
+
* the position a decoder gives a feature with no id of its own, which starts
|
|
51
|
+
* again in each response) is kept under a new id, so ids stay unique in the
|
|
52
|
+
* layer.
|
|
53
|
+
*/
|
|
54
|
+
function joinResponses(responses) {
|
|
55
|
+
const kept = /* @__PURE__ */ new Map();
|
|
56
|
+
responses.forEach((features, response) => {
|
|
57
|
+
for (const feature of features) {
|
|
58
|
+
const same = kept.get(feature.id);
|
|
59
|
+
if (same && jsonEqual(same.geometry, feature.geometry)) continue;
|
|
60
|
+
let id = feature.id;
|
|
61
|
+
for (let n = response; kept.has(id); n += 1) id = `${feature.id}#${n}`;
|
|
62
|
+
kept.set(id, {
|
|
63
|
+
...feature,
|
|
64
|
+
id,
|
|
65
|
+
fields: {
|
|
66
|
+
...feature.fields,
|
|
67
|
+
id
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return [...kept.values()];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Decodes the responses to the requests `feedUrls` lists, as `decodeFeed`
|
|
76
|
+
* does for one, and joins them in the order they came. A feature that more
|
|
77
|
+
* than one response holds (one on the antimeridian can be in the box on each
|
|
78
|
+
* side) is kept once, when its id and geometry are the same; a different
|
|
79
|
+
* feature with the same id is kept under a new id. Each response is capped
|
|
80
|
+
* at one more than a query layer's `maxFeatures` before they are joined, and
|
|
81
|
+
* the cap applies to the whole again.
|
|
82
|
+
*
|
|
83
|
+
* @throws `LayerError` `invalid` when any response cannot be decoded, and
|
|
84
|
+
* `empty` when no feature is left in any of them.
|
|
85
|
+
*/
|
|
86
|
+
function decodeFeeds(definition, raws, decoders) {
|
|
87
|
+
const [only, ...others] = raws;
|
|
88
|
+
if (others.length === 0) return decodeFeed(definition, only, decoders);
|
|
89
|
+
const features = joinResponses(raws.map((raw) => decodeOrNothing(definition, raw, decoders)));
|
|
90
|
+
if (features.length === 0) throw new LayerError("empty");
|
|
91
|
+
return capQuery(definition, features);
|
|
30
92
|
}
|
|
31
93
|
/**
|
|
32
94
|
* Decodes a detail response (such as an event's affected area) and keeps its
|
|
@@ -48,21 +110,36 @@ function decodeDetail(definition, raw, decoders) {
|
|
|
48
110
|
* The URL for a feed or query layer's controls. Missing or invalid values use
|
|
49
111
|
* their defaults. A query layer needs the `area` of the map it asks for
|
|
50
112
|
* (see `queryWindow`), which fills its box and its limit.
|
|
113
|
+
*
|
|
114
|
+
* @throws when the `area` crosses the antimeridian, which takes two
|
|
115
|
+
* requests: use `feedUrls`. It once gave one URL with the box clamped to
|
|
116
|
+
* the edge of the world, which asked for the wrong area.
|
|
51
117
|
*/
|
|
52
118
|
function feedUrl(definition, controls, now, area) {
|
|
119
|
+
const [url, ...more] = feedUrls(definition, controls, now, area);
|
|
120
|
+
if (more.length > 0) throw new Error(`The area for ${definition.id} crosses the antimeridian.`);
|
|
121
|
+
return url;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The URLs to ask for a feed or query layer's controls: one, except for a
|
|
125
|
+
* query layer whose `area` crosses the antimeridian, which gets one for each
|
|
126
|
+
* side of the line (`queryBoxes`), because a provider takes a box inside
|
|
127
|
+
* -180 to 180. Join the answers with `decodeFeeds`.
|
|
128
|
+
*/
|
|
129
|
+
function feedUrls(definition, controls, now, area) {
|
|
53
130
|
if (!loadsFeatures(definition)) throw new Error(`${definition.id} is not a feed layer.`);
|
|
54
131
|
const values = resolveControlValues(definition, controls, now);
|
|
55
132
|
const { source } = definition;
|
|
56
|
-
if (source.kind === "feed") return fillUrlTemplate(source.url, templateValues(definition, values));
|
|
133
|
+
if (source.kind === "feed") return [fillUrlTemplate(source.url, templateValues(definition, values))];
|
|
57
134
|
if (!area) throw new Error(`${definition.id} is a query layer and needs an area.`);
|
|
58
|
-
return fillUrlTemplate(source.url, {
|
|
135
|
+
return queryBoxes(area).map((box) => fillUrlTemplate(source.url, {
|
|
59
136
|
...templateValues(definition, values),
|
|
60
|
-
west: String(
|
|
61
|
-
south: String(
|
|
62
|
-
east: String(
|
|
63
|
-
north: String(
|
|
137
|
+
west: String(box.west),
|
|
138
|
+
south: String(box.south),
|
|
139
|
+
east: String(box.east),
|
|
140
|
+
north: String(box.north),
|
|
64
141
|
limit: String(source.maxFeatures + 1)
|
|
65
|
-
});
|
|
142
|
+
}));
|
|
66
143
|
}
|
|
67
144
|
/**
|
|
68
145
|
* The tile URL template for a raster or vector tile layer's controls, with
|
|
@@ -86,6 +163,6 @@ function detailUrl(definition, feature) {
|
|
|
86
163
|
const url = new URL(value);
|
|
87
164
|
return url.protocol === "https:" && definition.detail.origins.includes(url.origin) ? url.href : null;
|
|
88
165
|
}
|
|
89
|
-
export { decodeDetail, decodeFeed, detailUrl, feedUrl, tileUrl };
|
|
166
|
+
export { decodeDetail, decodeFeed, decodeFeeds, detailUrl, feedUrl, feedUrls, tileUrl };
|
|
90
167
|
|
|
91
168
|
//# sourceMappingURL=feed.js.map
|
package/dist/layers/feed.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feed.js","names":[],"sources":["../../src/layers/feed.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { LayerError } from \"../ports/errors\";\nimport type { Bounds } from \"../ports/geometry\";\nimport type { ControlValues, LayerFeature } from \"../ports/layers\";\nimport { resolveControlValues, templateValues } from \"./controls\";\nimport { type DecoderRegistry, decodeWith } from \"./decoders\";\nimport {\n isFeedLayer,\n isShapeStyle,\n type LayerDefinition,\n loadsFeatures,\n} from \"./definition\";\nimport { matchesFilter } from \"./filter\";\nimport { fillUrlTemplate } from \"./template\";\n\nconst GEOMETRIES: Readonly<\n Record<\"points\" | \"area\" | \"line\", readonly string[]>\n> = {\n points: [\"Point\"],\n area: [\"Polygon\", \"MultiPolygon\"],\n line: [\"LineString\", \"MultiLineString\"],\n};\n\n/**\n * Decodes a feed response with the definition's decoder, applies its filter\n * and keeps the geometry the layer draws: points for a point style or a\n * heatmap, polygons for an area style and lines for a line style. A query\n * layer keeps at most one feature more than its `maxFeatures`, so the caller\n * can tell that more exist without holding a runaway response.\n *\n * @throws `LayerError` `invalid` when it cannot be decoded (or the layer has\n * no feed), and `empty` when no feature is left.\n */\nexport function decodeFeed(\n definition: LayerDefinition,\n raw: unknown,\n decoders: DecoderRegistry,\n): LayerFeature[] {\n if (!loadsFeatures(definition)) {\n throw new LayerError(\"invalid\", `${definition.id} has no feed.`);\n }\n const { decoder, fields, filter } = definition.source;\n const drawn =\n isFeedLayer(definition) && isShapeStyle(definition.style)\n ? GEOMETRIES[definition.style.geometry]\n : GEOMETRIES.points;\n const features = decodeWith(decoders, decoder, raw, fields).filter(\n (feature) =>\n drawn.includes(feature.geometry.type) &&\n (!filter || matchesFilter(feature, filter)),\n );\n if (features.length === 0) {\n throw new LayerError(\"empty\");\n }\n return definition.source.kind === \"query\"\n ? features.slice(0, definition.source.maxFeatures + 1)\n : features;\n}\n\n/**\n * Decodes a detail response (such as an event's affected area) and keeps its\n * lines and polygons.\n *\n * @throws `LayerError` `invalid` when it cannot be decoded (or the layer has\n * no detail), and `empty` when it has no shape.\n */\nexport function decodeDetail(\n definition: LayerDefinition,\n raw: unknown,\n decoders: DecoderRegistry,\n): LayerFeature[] {\n if (!isFeedLayer(definition) || !definition.detail) {\n throw new LayerError(\"invalid\", `${definition.id} has no detail source.`);\n }\n const shapes = decodeWith(decoders, definition.detail.decoder, raw, {\n id: \"id\",\n title: \"properties.name\",\n }).filter((shape) => shape.geometry.type !== \"Point\");\n if (shapes.length === 0) {\n throw new LayerError(\"empty\");\n }\n return shapes;\n}\n\n/**\n * The URL for a feed or query layer's controls. Missing or invalid values use\n * their defaults. A query layer needs the `area` of the map it asks for\n * (see `queryWindow`), which fills its box and its limit.\n */\nexport function feedUrl(\n definition: LayerDefinition,\n controls: ControlValues,\n now: number,\n area?: Bounds,\n): string {\n if (!loadsFeatures(definition)) {\n throw new Error(`${definition.id} is not a feed layer.`);\n }\n const values = resolveControlValues(definition, controls, now);\n const { source } = definition;\n if (source.kind === \"feed\") {\n return fillUrlTemplate(source.url, templateValues(definition, values));\n }\n if (!area) {\n throw new Error(`${definition.id} is a query layer and needs an area.`);\n }\n return fillUrlTemplate(source.url, {\n ...templateValues(definition, values),\n west: String(area.west),\n south: String(area.south),\n east: String(area.east),\n north: String(area.north),\n limit: String(source.maxFeatures + 1),\n });\n}\n\n/**\n * The tile URL template for a raster or vector tile layer's controls, with\n * the engine's own placeholders (`{z}`, `{bbox-epsg-3857}`) left in.\n */\nexport function tileUrl(\n definition: LayerDefinition,\n controls: ControlValues,\n now: number,\n): string {\n if (loadsFeatures(definition)) {\n throw new Error(`${definition.id} is not a tile layer.`);\n }\n const values = resolveControlValues(definition, controls, now);\n return fillUrlTemplate(\n definition.source.tiles,\n templateValues(definition, values),\n );\n}\n\n/**\n * The URL of a feature's detail, or null when the layer has none or the\n * feature's URL is not `https` on one of the origins the definition declares.\n * The URL comes from the provider's data, so it is checked before any\n * request is made, not only by the Content-Security-Policy.\n */\nexport function detailUrl(\n definition: LayerDefinition,\n feature: LayerFeature,\n): string | null {\n if (!isFeedLayer(definition) || !definition.detail) {\n return null;\n }\n const value = feature.fields[definition.detail.urlField];\n if (typeof value !== \"string\" || !URL.canParse(value)) {\n return null;\n }\n const url = new URL(value);\n return url.protocol === \"https:\" &&\n definition.detail.origins.includes(url.origin)\n ? url.href\n : null;\n}\n"],"mappings":";;;;;;;AAgBA,IAAM,aAEF;CACF,QAAQ,CAAC,OAAO;CAChB,MAAM,CAAC,WAAW,cAAc;CAChC,MAAM,CAAC,cAAc,iBAAiB;AACxC;;;;;;;;;;;AAYA,SAAgB,WACd,YACA,KACA,UACgB;CAChB,IAAI,CAAC,cAAc,UAAU,GAC3B,MAAM,IAAI,WAAW,WAAW,GAAG,WAAW,GAAG,cAAc;CAEjE,MAAM,EAAE,SAAS,QAAQ,WAAW,WAAW;CAC/C,MAAM,QACJ,YAAY,UAAU,KAAK,aAAa,WAAW,KAAK,IACpD,WAAW,WAAW,MAAM,YAC5B,WAAW;CACjB,MAAM,WAAW,WAAW,UAAU,SAAS,KAAK,MAAM,CAAC,CAAC,QACzD,YACC,MAAM,SAAS,QAAQ,SAAS,IAAI,MACnC,CAAC,UAAU,cAAc,SAAS,MAAM,EAC7C;CACA,IAAI,SAAS,WAAW,GACtB,MAAM,IAAI,WAAW,OAAO;CAE9B,OAAO,WAAW,OAAO,SAAS,UAC9B,SAAS,MAAM,GAAG,WAAW,OAAO,cAAc,CAAC,IACnD;AACN;;;;;;;;AASA,SAAgB,aACd,YACA,KACA,UACgB;CAChB,IAAI,CAAC,YAAY,UAAU,KAAK,CAAC,WAAW,QAC1C,MAAM,IAAI,WAAW,WAAW,GAAG,WAAW,GAAG,uBAAuB;CAE1E,MAAM,SAAS,WAAW,UAAU,WAAW,OAAO,SAAS,KAAK;EAClE,IAAI;EACJ,OAAO;CACT,CAAC,CAAC,CAAC,QAAQ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OACJ;AACN"}
|
|
1
|
+
{"version":3,"file":"feed.js","names":[],"sources":["../../src/layers/feed.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { LayerError } from \"../ports/errors\";\nimport type { Bounds } from \"../ports/geometry\";\nimport type { ControlValues, LayerFeature } from \"../ports/layers\";\nimport { resolveControlValues, templateValues } from \"./controls\";\nimport { type DecoderRegistry, decodeWith } from \"./decoders\";\nimport {\n isFeedLayer,\n isShapeStyle,\n type LayerDefinition,\n loadsFeatures,\n} from \"./definition\";\nimport { matchesFilter } from \"./filter\";\nimport { jsonEqual } from \"./jsonEqual\";\nimport { queryBoxes } from \"./query\";\nimport { fillUrlTemplate } from \"./template\";\n\nconst GEOMETRIES: Readonly<\n Record<\"points\" | \"area\" | \"line\", readonly string[]>\n> = {\n points: [\"Point\"],\n area: [\"Polygon\", \"MultiPolygon\"],\n line: [\"LineString\", \"MultiLineString\"],\n};\n\n/**\n * Decodes a feed response with the definition's decoder, applies its filter\n * and keeps the geometry the layer draws: points for a point style or a\n * heatmap, polygons for an area style and lines for a line style. A query\n * layer keeps at most one feature more than its `maxFeatures`, so the caller\n * can tell that more exist without holding a runaway response.\n *\n * @throws `LayerError` `invalid` when it cannot be decoded (or the layer has\n * no feed), and `empty` when no feature is left.\n */\nexport function decodeFeed(\n definition: LayerDefinition,\n raw: unknown,\n decoders: DecoderRegistry,\n): LayerFeature[] {\n if (!loadsFeatures(definition)) {\n throw new LayerError(\"invalid\", `${definition.id} has no feed.`);\n }\n const { decoder, fields, filter } = definition.source;\n const drawn =\n isFeedLayer(definition) && isShapeStyle(definition.style)\n ? GEOMETRIES[definition.style.geometry]\n : GEOMETRIES.points;\n const features = decodeWith(decoders, decoder, raw, fields).filter(\n (feature) =>\n drawn.includes(feature.geometry.type) &&\n (!filter || matchesFilter(feature, filter)),\n );\n if (features.length === 0) {\n throw new LayerError(\"empty\");\n }\n return capQuery(definition, features);\n}\n\n/** A query layer's features cut to one more than its `maxFeatures`; any other layer's, whole. */\nfunction capQuery(\n definition: LayerDefinition,\n features: LayerFeature[],\n): LayerFeature[] {\n return loadsFeatures(definition) && definition.source.kind === \"query\"\n ? features.slice(0, definition.source.maxFeatures + 1)\n : features;\n}\n\n/** What decoding one of several responses gives: its features, or none when it held none. */\nfunction decodeOrNothing(\n definition: LayerDefinition,\n raw: unknown,\n decoders: DecoderRegistry,\n): LayerFeature[] {\n try {\n return decodeFeed(definition, raw, decoders);\n } catch (error) {\n if (error instanceof LayerError && error.kind === \"empty\") {\n return [];\n }\n throw error;\n }\n}\n\n/**\n * Joins decoded responses. A feature with the id and the geometry of one\n * already kept is the same feature, seen from both boxes, and is dropped.\n * A different feature that has the same id (a provider that reuses one, or\n * the position a decoder gives a feature with no id of its own, which starts\n * again in each response) is kept under a new id, so ids stay unique in the\n * layer.\n */\nfunction joinResponses(responses: readonly LayerFeature[][]): LayerFeature[] {\n const kept = new Map<string, LayerFeature>();\n responses.forEach((features, response) => {\n for (const feature of features) {\n const same = kept.get(feature.id);\n if (same && jsonEqual(same.geometry, feature.geometry)) {\n continue;\n }\n let id = feature.id;\n for (let n = response; kept.has(id); n += 1) {\n id = `${feature.id}#${n}`;\n }\n kept.set(id, { ...feature, id, fields: { ...feature.fields, id } });\n }\n });\n return [...kept.values()];\n}\n\n/**\n * Decodes the responses to the requests `feedUrls` lists, as `decodeFeed`\n * does for one, and joins them in the order they came. A feature that more\n * than one response holds (one on the antimeridian can be in the box on each\n * side) is kept once, when its id and geometry are the same; a different\n * feature with the same id is kept under a new id. Each response is capped\n * at one more than a query layer's `maxFeatures` before they are joined, and\n * the cap applies to the whole again.\n *\n * @throws `LayerError` `invalid` when any response cannot be decoded, and\n * `empty` when no feature is left in any of them.\n */\nexport function decodeFeeds(\n definition: LayerDefinition,\n raws: readonly unknown[],\n decoders: DecoderRegistry,\n): LayerFeature[] {\n const [only, ...others] = raws;\n if (others.length === 0) {\n return decodeFeed(definition, only, decoders);\n }\n const features = joinResponses(\n raws.map((raw) => decodeOrNothing(definition, raw, decoders)),\n );\n if (features.length === 0) {\n throw new LayerError(\"empty\");\n }\n return capQuery(definition, features);\n}\n\n/**\n * Decodes a detail response (such as an event's affected area) and keeps its\n * lines and polygons.\n *\n * @throws `LayerError` `invalid` when it cannot be decoded (or the layer has\n * no detail), and `empty` when it has no shape.\n */\nexport function decodeDetail(\n definition: LayerDefinition,\n raw: unknown,\n decoders: DecoderRegistry,\n): LayerFeature[] {\n if (!isFeedLayer(definition) || !definition.detail) {\n throw new LayerError(\"invalid\", `${definition.id} has no detail source.`);\n }\n const shapes = decodeWith(decoders, definition.detail.decoder, raw, {\n id: \"id\",\n title: \"properties.name\",\n }).filter((shape) => shape.geometry.type !== \"Point\");\n if (shapes.length === 0) {\n throw new LayerError(\"empty\");\n }\n return shapes;\n}\n\n/**\n * The URL for a feed or query layer's controls. Missing or invalid values use\n * their defaults. A query layer needs the `area` of the map it asks for\n * (see `queryWindow`), which fills its box and its limit.\n *\n * @throws when the `area` crosses the antimeridian, which takes two\n * requests: use `feedUrls`. It once gave one URL with the box clamped to\n * the edge of the world, which asked for the wrong area.\n */\nexport function feedUrl(\n definition: LayerDefinition,\n controls: ControlValues,\n now: number,\n area?: Bounds,\n): string {\n const [url, ...more] = feedUrls(definition, controls, now, area);\n if (more.length > 0) {\n throw new Error(`The area for ${definition.id} crosses the antimeridian.`);\n }\n return url as string;\n}\n\n/**\n * The URLs to ask for a feed or query layer's controls: one, except for a\n * query layer whose `area` crosses the antimeridian, which gets one for each\n * side of the line (`queryBoxes`), because a provider takes a box inside\n * -180 to 180. Join the answers with `decodeFeeds`.\n */\nexport function feedUrls(\n definition: LayerDefinition,\n controls: ControlValues,\n now: number,\n area?: Bounds,\n): string[] {\n if (!loadsFeatures(definition)) {\n throw new Error(`${definition.id} is not a feed layer.`);\n }\n const values = resolveControlValues(definition, controls, now);\n const { source } = definition;\n if (source.kind === \"feed\") {\n return [fillUrlTemplate(source.url, templateValues(definition, values))];\n }\n if (!area) {\n throw new Error(`${definition.id} is a query layer and needs an area.`);\n }\n return queryBoxes(area).map((box) =>\n fillUrlTemplate(source.url, {\n ...templateValues(definition, values),\n west: String(box.west),\n south: String(box.south),\n east: String(box.east),\n north: String(box.north),\n limit: String(source.maxFeatures + 1),\n }),\n );\n}\n\n/**\n * The tile URL template for a raster or vector tile layer's controls, with\n * the engine's own placeholders (`{z}`, `{bbox-epsg-3857}`) left in.\n */\nexport function tileUrl(\n definition: LayerDefinition,\n controls: ControlValues,\n now: number,\n): string {\n if (loadsFeatures(definition)) {\n throw new Error(`${definition.id} is not a tile layer.`);\n }\n const values = resolveControlValues(definition, controls, now);\n return fillUrlTemplate(\n definition.source.tiles,\n templateValues(definition, values),\n );\n}\n\n/**\n * The URL of a feature's detail, or null when the layer has none or the\n * feature's URL is not `https` on one of the origins the definition declares.\n * The URL comes from the provider's data, so it is checked before any\n * request is made, not only by the Content-Security-Policy.\n */\nexport function detailUrl(\n definition: LayerDefinition,\n feature: LayerFeature,\n): string | null {\n if (!isFeedLayer(definition) || !definition.detail) {\n return null;\n }\n const value = feature.fields[definition.detail.urlField];\n if (typeof value !== \"string\" || !URL.canParse(value)) {\n return null;\n }\n const url = new URL(value);\n return url.protocol === \"https:\" &&\n definition.detail.origins.includes(url.origin)\n ? url.href\n : null;\n}\n"],"mappings":";;;;;;;;;AAkBA,IAAM,aAEF;CACF,QAAQ,CAAC,OAAO;CAChB,MAAM,CAAC,WAAW,cAAc;CAChC,MAAM,CAAC,cAAc,iBAAiB;AACxC;;;;;;;;;;;AAYA,SAAgB,WACd,YACA,KACA,UACgB;CAChB,IAAI,CAAC,cAAc,UAAU,GAC3B,MAAM,IAAI,WAAW,WAAW,GAAG,WAAW,GAAG,cAAc;CAEjE,MAAM,EAAE,SAAS,QAAQ,WAAW,WAAW;CAC/C,MAAM,QACJ,YAAY,UAAU,KAAK,aAAa,WAAW,KAAK,IACpD,WAAW,WAAW,MAAM,YAC5B,WAAW;CACjB,MAAM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AI,KAAK;EACvB,MAAM,OAAO,IAAI,IAAI;EACrB,OAAO,OAAO,IAAI,KAAK;EACvB,OAAO,OAAO,OAAO,cAAc,CAAC;CACtC,CAAC,CACH;AACF;;;;;AAMA,SAAgB,QACd,YACA,UACA,KACQ;CACR,IAAI,cAAc,UAAU,GAC1B,MAAM,IAAI,MAAM,GAAG,WAAW,GAAG,sBAAsB;CAEzD,MAAM,SAAS,qBAAqB,YAAY,UAAU,GAAG;CAC7D,OAAO,gBACL,WAAW,OAAO,OAClB,eAAe,YAAY,MAAM,CACnC;AACF;;;;;;;AAQA,SAAgB,UACd,YACA,SACe;CACf,IAAI,CAAC,YAAY,UAAU,KAAK,CAAC,WAAW,QAC1C,OAAO;CAET,MAAM,QAAQ,QAAQ,OAAO,WAAW,OAAO;CAC/C,IAAI,OAAO,UAAU,YAAY,CAAC,IAAI,SAAS,KAAK,GAClD,OAAO;CAET,MAAM,MAAM,IAAI,IAAI,KAAK;CACzB,OAAO,IAAI,aAAa,YACtB,WAAW,OAAO,QAAQ,SAAS,IAAI,MAAM,IAC3C,IAAI,OACJ;AACN"}
|
package/dist/layers/query.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { wrapLongitude } from "../globe/longitude.js";
|
|
1
2
|
import { clamp } from "../numbers/clamp.js";
|
|
2
3
|
// src/layers/query.ts
|
|
3
4
|
/** The furthest latitude a Web Mercator map shows. */
|
|
@@ -11,32 +12,105 @@ var MAX_WINDOW_RATIO = 4;
|
|
|
11
12
|
function round(value) {
|
|
12
13
|
return Number(value.toFixed(5));
|
|
13
14
|
}
|
|
15
|
+
/** The width of the circle of longitude, in degrees. */
|
|
16
|
+
var TURN = 360;
|
|
14
17
|
function span(bounds) {
|
|
15
18
|
return Math.max(bounds.east - bounds.west, bounds.north - bounds.south);
|
|
16
19
|
}
|
|
17
20
|
/**
|
|
21
|
+
* `bounds` as the short box: a width of a turn or more is the whole circle,
|
|
22
|
+
* and any other has `west` inside -180 to 180 and `east` past 180 when it
|
|
23
|
+
* crosses the antimeridian (a map reports such a view as 170 to 190, or as
|
|
24
|
+
* -190 to -170, and both mean the same).
|
|
25
|
+
*/
|
|
26
|
+
function shortBox(bounds) {
|
|
27
|
+
if (bounds.east - bounds.west >= TURN) return {
|
|
28
|
+
...bounds,
|
|
29
|
+
west: -180,
|
|
30
|
+
east: 180
|
|
31
|
+
};
|
|
32
|
+
const west = wrapLongitude(bounds.west);
|
|
33
|
+
return {
|
|
34
|
+
...bounds,
|
|
35
|
+
west,
|
|
36
|
+
east: west + (bounds.east - bounds.west)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
18
40
|
* The box a query layer asks for when the map shows `view`: the view with a
|
|
19
41
|
* margin, snapped outward to a grid in proportion to its size. A few pixels
|
|
20
42
|
* of panning give the same window, so they send the same request (and the
|
|
21
43
|
* same cache entry), and a small pan stays inside the window and needs none.
|
|
22
|
-
* Latitudes
|
|
44
|
+
* Latitudes are kept within what a map shows, and longitudes within -180 to
|
|
45
|
+
* 180, except that a view across the antimeridian gets a window across it:
|
|
46
|
+
* the short box, with `west` inside -180 to 180 and `east` past 180 (see
|
|
47
|
+
* `queryBoxes` for the boxes to ask a provider). A view that does not cross
|
|
48
|
+
* the line gets a window that stops at it, margin or not.
|
|
23
49
|
*/
|
|
24
50
|
function queryWindow(view) {
|
|
25
|
-
const
|
|
51
|
+
const short = shortBox(view);
|
|
52
|
+
const size = Math.max(span(short), MIN_SPAN);
|
|
26
53
|
const margin = size * MARGIN;
|
|
27
54
|
const step = 10 ** Math.floor(Math.log10(size)) / 2;
|
|
28
55
|
const down = (value) => Math.floor(value / step) * step;
|
|
29
56
|
const up = (value) => Math.ceil(value / step) * step;
|
|
57
|
+
const south = round(clamp(down(short.south - margin), -85, MAX_LATITUDE));
|
|
58
|
+
const north = round(clamp(up(short.north + margin), -85, MAX_LATITUDE));
|
|
59
|
+
if (short.east > 180) {
|
|
60
|
+
const width = up(short.east + margin) - down(short.west - margin);
|
|
61
|
+
if (width < TURN) {
|
|
62
|
+
const west = wrapLongitude(down(short.west - margin));
|
|
63
|
+
return {
|
|
64
|
+
west: round(west),
|
|
65
|
+
south,
|
|
66
|
+
east: round(west + width),
|
|
67
|
+
north
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
west: -180,
|
|
72
|
+
south,
|
|
73
|
+
east: 180,
|
|
74
|
+
north
|
|
75
|
+
};
|
|
76
|
+
}
|
|
30
77
|
return {
|
|
31
|
-
west: round(clamp(down(
|
|
32
|
-
south
|
|
33
|
-
east: round(clamp(up(
|
|
34
|
-
north
|
|
78
|
+
west: round(clamp(down(short.west - margin), -180, 180)),
|
|
79
|
+
south,
|
|
80
|
+
east: round(clamp(up(short.east + margin), -180, 180)),
|
|
81
|
+
north
|
|
35
82
|
};
|
|
36
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* The boxes to ask a provider for `window`, each inside -180 to 180: the
|
|
86
|
+
* window itself, or when it crosses the antimeridian (`east` past 180) the
|
|
87
|
+
* part from `west` to 180 and then the part from -180 to the rest. The
|
|
88
|
+
* boxes meet at the line and cover the window exactly.
|
|
89
|
+
*/
|
|
90
|
+
function queryBoxes(window) {
|
|
91
|
+
const short = shortBox(window);
|
|
92
|
+
if (short.east <= 180) return [short];
|
|
93
|
+
return [{
|
|
94
|
+
...short,
|
|
95
|
+
east: 180
|
|
96
|
+
}, {
|
|
97
|
+
...short,
|
|
98
|
+
west: -180,
|
|
99
|
+
east: round(short.east - TURN)
|
|
100
|
+
}];
|
|
101
|
+
}
|
|
37
102
|
function contains(outer, inner) {
|
|
38
103
|
return outer.west <= inner.west && outer.south <= inner.south && outer.east >= inner.east && outer.north >= inner.north;
|
|
39
104
|
}
|
|
105
|
+
/** Whether `outer` holds `view` in its own copy of the world or the one east of it. */
|
|
106
|
+
function containsView(outer, view) {
|
|
107
|
+
const short = shortBox(view);
|
|
108
|
+
return contains(outer, short) || contains(outer, {
|
|
109
|
+
...short,
|
|
110
|
+
west: short.west + TURN,
|
|
111
|
+
east: short.east + TURN
|
|
112
|
+
});
|
|
113
|
+
}
|
|
40
114
|
/**
|
|
41
115
|
* The window a query layer should have for `view`: the last one while the
|
|
42
116
|
* view is still inside it and not much smaller than it, otherwise a new one.
|
|
@@ -44,9 +118,9 @@ function contains(outer, inner) {
|
|
|
44
118
|
* in far asks for a smaller area, so fewer features and less data.
|
|
45
119
|
*/
|
|
46
120
|
function nextQueryWindow(previous, view) {
|
|
47
|
-
if (previous &&
|
|
121
|
+
if (previous && containsView(previous, view) && span(previous) <= MAX_WINDOW_RATIO * Math.max(span(view), MIN_SPAN)) return previous;
|
|
48
122
|
return queryWindow(view);
|
|
49
123
|
}
|
|
50
|
-
export { nextQueryWindow, queryWindow };
|
|
124
|
+
export { nextQueryWindow, queryBoxes, queryWindow };
|
|
51
125
|
|
|
52
126
|
//# sourceMappingURL=query.js.map
|
package/dist/layers/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","names":[],"sources":["../../src/layers/query.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { clamp } from \"../numbers/clamp\";\nimport type { Bounds } from \"../ports/geometry\";\n\n/** The furthest latitude a Web Mercator map shows. */\nconst MAX_LATITUDE = 85;\n\n/** The smallest span, in degrees, worked with, so a zero-size view still has a window. */\nconst MIN_SPAN = 1e-4;\n\n/** How much wider than the view a window is on each side, as a share of the view. */\nconst MARGIN = 0.2;\n\n/** The most times larger than the view a window may be before a new one is taken. */\nconst MAX_WINDOW_RATIO = 4;\n\nfunction round(value: number): number {\n return Number(value.toFixed(5));\n}\n\nfunction span(bounds: Bounds): number {\n return Math.max(bounds.east - bounds.west, bounds.north - bounds.south);\n}\n\n/**\n * The box a query layer asks for when the map shows `view`: the view with a\n * margin, snapped outward to a grid in proportion to its size. A few pixels\n * of panning give the same window, so they send the same request (and the\n * same cache entry), and a small pan stays inside the window and needs none.\n * Latitudes
|
|
1
|
+
{"version":3,"file":"query.js","names":[],"sources":["../../src/layers/query.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { wrapLongitude } from \"../globe/longitude\";\nimport { clamp } from \"../numbers/clamp\";\nimport type { Bounds } from \"../ports/geometry\";\n\n/** The furthest latitude a Web Mercator map shows. */\nconst MAX_LATITUDE = 85;\n\n/** The smallest span, in degrees, worked with, so a zero-size view still has a window. */\nconst MIN_SPAN = 1e-4;\n\n/** How much wider than the view a window is on each side, as a share of the view. */\nconst MARGIN = 0.2;\n\n/** The most times larger than the view a window may be before a new one is taken. */\nconst MAX_WINDOW_RATIO = 4;\n\nfunction round(value: number): number {\n return Number(value.toFixed(5));\n}\n\n/** The width of the circle of longitude, in degrees. */\nconst TURN = 360;\n\nfunction span(bounds: Bounds): number {\n return Math.max(bounds.east - bounds.west, bounds.north - bounds.south);\n}\n\n/**\n * `bounds` as the short box: a width of a turn or more is the whole circle,\n * and any other has `west` inside -180 to 180 and `east` past 180 when it\n * crosses the antimeridian (a map reports such a view as 170 to 190, or as\n * -190 to -170, and both mean the same).\n */\nfunction shortBox(bounds: Bounds): Bounds {\n if (bounds.east - bounds.west >= TURN) {\n return { ...bounds, west: -180, east: 180 };\n }\n const west = wrapLongitude(bounds.west);\n return { ...bounds, west, east: west + (bounds.east - bounds.west) };\n}\n\n/**\n * The box a query layer asks for when the map shows `view`: the view with a\n * margin, snapped outward to a grid in proportion to its size. A few pixels\n * of panning give the same window, so they send the same request (and the\n * same cache entry), and a small pan stays inside the window and needs none.\n * Latitudes are kept within what a map shows, and longitudes within -180 to\n * 180, except that a view across the antimeridian gets a window across it:\n * the short box, with `west` inside -180 to 180 and `east` past 180 (see\n * `queryBoxes` for the boxes to ask a provider). A view that does not cross\n * the line gets a window that stops at it, margin or not.\n */\nexport function queryWindow(view: Bounds): Bounds {\n const short = shortBox(view);\n const size = Math.max(span(short), MIN_SPAN);\n const margin = size * MARGIN;\n const step = 10 ** Math.floor(Math.log10(size)) / 2;\n const down = (value: number) => Math.floor(value / step) * step;\n const up = (value: number) => Math.ceil(value / step) * step;\n const south = round(\n clamp(down(short.south - margin), -MAX_LATITUDE, MAX_LATITUDE),\n );\n const north = round(\n clamp(up(short.north + margin), -MAX_LATITUDE, MAX_LATITUDE),\n );\n if (short.east > 180) {\n const width = up(short.east + margin) - down(short.west - margin);\n if (width < TURN) {\n const west = wrapLongitude(down(short.west - margin));\n return { west: round(west), south, east: round(west + width), north };\n }\n return { west: -180, south, east: 180, north };\n }\n return {\n west: round(clamp(down(short.west - margin), -180, 180)),\n south,\n east: round(clamp(up(short.east + margin), -180, 180)),\n north,\n };\n}\n\n/**\n * The boxes to ask a provider for `window`, each inside -180 to 180: the\n * window itself, or when it crosses the antimeridian (`east` past 180) the\n * part from `west` to 180 and then the part from -180 to the rest. The\n * boxes meet at the line and cover the window exactly.\n */\nexport function queryBoxes(window: Bounds): Bounds[] {\n const short = shortBox(window);\n if (short.east <= 180) {\n return [short];\n }\n return [\n { ...short, east: 180 },\n { ...short, west: -180, east: round(short.east - TURN) },\n ];\n}\n\nfunction contains(outer: Bounds, inner: Bounds): boolean {\n return (\n outer.west <= inner.west &&\n outer.south <= inner.south &&\n outer.east >= inner.east &&\n outer.north >= inner.north\n );\n}\n\n/** Whether `outer` holds `view` in its own copy of the world or the one east of it. */\nfunction containsView(outer: Bounds, view: Bounds): boolean {\n const short = shortBox(view);\n return (\n contains(outer, short) ||\n contains(outer, {\n ...short,\n west: short.west + TURN,\n east: short.east + TURN,\n })\n );\n}\n\n/**\n * The window a query layer should have for `view`: the last one while the\n * view is still inside it and not much smaller than it, otherwise a new one.\n * Panning a little or zooming in a little then costs no request, and zooming\n * in far asks for a smaller area, so fewer features and less data.\n */\nexport function nextQueryWindow(previous: Bounds | null, view: Bounds): Bounds {\n if (\n previous &&\n containsView(previous, view) &&\n span(previous) <= MAX_WINDOW_RATIO * Math.max(span(view), MIN_SPAN)\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spatial/boundsOfPaths.ts
|
|
4
|
+
var TURN = 360;
|
|
5
|
+
var ANTIMERIDIAN = TURN / 2;
|
|
6
|
+
/** Degrees below which two runs count as touching and a gap as closed, so rounding in a longitude cannot open a hairline gap. */
|
|
7
|
+
var GAP_EPSILON = 1e-9;
|
|
8
|
+
function isFinitePoint(point) {
|
|
9
|
+
return Number.isFinite(point.lat) && Number.isFinite(point.lng);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The signed longitude step from one vertex to the next, the short way round.
|
|
13
|
+
* An edge of exactly half the globe has no short way, so it goes east, as
|
|
14
|
+
* `nearestLongitude` does for the drawing (`unwrapLongitudes`) and for
|
|
15
|
+
* `measureLineDistance`.
|
|
16
|
+
*/
|
|
17
|
+
function shortestStep(from, to) {
|
|
18
|
+
const step = wrapLongitude(to - from);
|
|
19
|
+
return step === -180 ? ANTIMERIDIAN : step;
|
|
20
|
+
}
|
|
21
|
+
/** The runs one path covers: each vertex, and each edge taken the short way round. */
|
|
22
|
+
function arcsOfPath(path) {
|
|
23
|
+
const arcs = [];
|
|
24
|
+
path.forEach((point, index) => {
|
|
25
|
+
arcs.push({
|
|
26
|
+
start: wrapLongitude(point.lng),
|
|
27
|
+
length: 0
|
|
28
|
+
});
|
|
29
|
+
const next = path[index + 1];
|
|
30
|
+
if (!next) return;
|
|
31
|
+
const step = shortestStep(point.lng, next.lng);
|
|
32
|
+
const from = step >= 0 ? point.lng : next.lng;
|
|
33
|
+
arcs.push({
|
|
34
|
+
start: wrapLongitude(from),
|
|
35
|
+
length: Math.abs(step)
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
return arcs;
|
|
39
|
+
}
|
|
40
|
+
/** Splits a run that passes 180 into the part before it and the part after, so every run lies inside [-180, 180]. */
|
|
41
|
+
function withoutWrap(arc) {
|
|
42
|
+
const end = arc.start + arc.length;
|
|
43
|
+
if (end <= ANTIMERIDIAN) return [[arc.start, end]];
|
|
44
|
+
return [[arc.start, ANTIMERIDIAN], [-180, end - TURN]];
|
|
45
|
+
}
|
|
46
|
+
/** Joins runs that overlap or touch into the fewest runs, in order. */
|
|
47
|
+
function mergeRuns(runs) {
|
|
48
|
+
const merged = [];
|
|
49
|
+
for (const [start, end] of [...runs].sort((a, b) => a[0] - b[0])) {
|
|
50
|
+
const last = merged.at(-1);
|
|
51
|
+
if (last && start <= last[1] + GAP_EPSILON) last[1] = Math.max(last[1], end);
|
|
52
|
+
else merged.push([start, end]);
|
|
53
|
+
}
|
|
54
|
+
return merged;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The longitude run going east that is left once the widest uncovered gap in
|
|
58
|
+
* the circle is taken out, or the whole circle when nothing is uncovered.
|
|
59
|
+
*/
|
|
60
|
+
function runOutsideWidestGap(merged) {
|
|
61
|
+
const first = merged[0];
|
|
62
|
+
const last = merged[merged.length - 1];
|
|
63
|
+
let gap = first[0] + TURN - last[1];
|
|
64
|
+
let run = {
|
|
65
|
+
west: first[0],
|
|
66
|
+
east: last[1]
|
|
67
|
+
};
|
|
68
|
+
for (let index = 1; index < merged.length; index += 1) {
|
|
69
|
+
const before = merged[index - 1];
|
|
70
|
+
const after = merged[index];
|
|
71
|
+
if (after[0] - before[1] > gap) {
|
|
72
|
+
gap = after[0] - before[1];
|
|
73
|
+
run = {
|
|
74
|
+
west: after[0],
|
|
75
|
+
east: before[1] + TURN
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return gap > GAP_EPSILON ? run : {
|
|
80
|
+
west: -180,
|
|
81
|
+
east: ANTIMERIDIAN
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The smallest latitude and longitude box holding every vertex of every path
|
|
86
|
+
* and every edge between neighbouring vertices of one path, each edge taken
|
|
87
|
+
* the short way round the globe (the way a path is drawn and measured). Used
|
|
88
|
+
* to fit the map to a polygon, a line or a feature with several parts.
|
|
89
|
+
* @param paths The rings and lines of a shape. A ring repeats its first
|
|
90
|
+
* vertex at the end, as GeoJSON does; no edge joins two paths.
|
|
91
|
+
* @returns `undefined` when no point has finite coordinates.
|
|
92
|
+
* @remarks Longitude is a circle, so the box is the circle less its widest
|
|
93
|
+
* gap that no vertex or edge covers. A shape wider than half the globe with
|
|
94
|
+
* few vertices is therefore boxed on the side its edges cover, which
|
|
95
|
+
* {@link boundsOfPoints} cannot know from vertices alone. A shape that
|
|
96
|
+
* covers the whole circle, such as a ring round a pole, gives a `west` of
|
|
97
|
+
* -180 and an `east` of 180. Otherwise the box has the form of
|
|
98
|
+
* {@link boundsOfPoints}: `west` inside [-180, 180) and `east` not before it,
|
|
99
|
+
* past 180 across the antimeridian. An edge of exactly half the globe has
|
|
100
|
+
* no short way and is taken eastward, as it is drawn and measured. Latitudes are clamped to valid
|
|
101
|
+
* coordinates, and an edge is a straight step in latitude.
|
|
102
|
+
*/
|
|
103
|
+
function boundsOfPaths(paths) {
|
|
104
|
+
const usable = paths.map((path) => path.filter(isFinitePoint)).filter((path) => path.length > 0);
|
|
105
|
+
if (usable.length === 0) return;
|
|
106
|
+
const { west, east } = runOutsideWidestGap(mergeRuns(usable.flatMap(arcsOfPath).flatMap(withoutWrap)));
|
|
107
|
+
let south = Number.POSITIVE_INFINITY;
|
|
108
|
+
let north = Number.NEGATIVE_INFINITY;
|
|
109
|
+
for (const { lat } of usable.flat()) {
|
|
110
|
+
south = Math.min(south, lat);
|
|
111
|
+
north = Math.max(north, lat);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
south: clamp(south, -90, 90),
|
|
115
|
+
west,
|
|
116
|
+
north: clamp(north, -90, 90),
|
|
117
|
+
east
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
export { boundsOfPaths };
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=boundsOfPaths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boundsOfPaths.js","names":[],"sources":["../../src/spatial/boundsOfPaths.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { wrapLongitude } from \"../globe/longitude\";\nimport { clamp } from \"../numbers/clamp\";\nimport type { Bounds, LatLng } from \"../ports/geometry\";\n\nconst TURN = 360;\nconst ANTIMERIDIAN = TURN / 2;\n/** Degrees below which two runs count as touching and a gap as closed, so rounding in a longitude cannot open a hairline gap. */\nconst GAP_EPSILON = 1e-9;\n\n/** A run of longitude going east from `start` (in [-180, 180)) for `length` degrees. */\ninterface Arc {\n readonly start: number;\n readonly length: number;\n}\n\nfunction isFinitePoint(point: LatLng): boolean {\n return Number.isFinite(point.lat) && Number.isFinite(point.lng);\n}\n\n/**\n * The signed longitude step from one vertex to the next, the short way round.\n * An edge of exactly half the globe has no short way, so it goes east, as\n * `nearestLongitude` does for the drawing (`unwrapLongitudes`) and for\n * `measureLineDistance`.\n */\nfunction shortestStep(from: number, to: number): number {\n const step = wrapLongitude(to - from);\n return step === -ANTIMERIDIAN ? ANTIMERIDIAN : step;\n}\n\n/** The runs one path covers: each vertex, and each edge taken the short way round. */\nfunction arcsOfPath(path: readonly LatLng[]): Arc[] {\n const arcs: Arc[] = [];\n path.forEach((point, index) => {\n arcs.push({ start: wrapLongitude(point.lng), length: 0 });\n const next = path[index + 1];\n if (!next) {\n return;\n }\n const step = shortestStep(point.lng, next.lng);\n const from = step >= 0 ? point.lng : next.lng;\n arcs.push({ start: wrapLongitude(from), length: Math.abs(step) });\n });\n return arcs;\n}\n\n/** Splits a run that passes 180 into the part before it and the part after, so every run lies inside [-180, 180]. */\nfunction withoutWrap(arc: Arc): [number, number][] {\n const end = arc.start + arc.length;\n if (end <= ANTIMERIDIAN) {\n return [[arc.start, end]];\n }\n return [\n [arc.start, ANTIMERIDIAN],\n [-ANTIMERIDIAN, end - TURN],\n ];\n}\n\n/** Joins runs that overlap or touch into the fewest runs, in order. */\nfunction mergeRuns(runs: readonly [number, number][]): [number, number][] {\n const merged: [number, number][] = [];\n for (const [start, end] of [...runs].sort((a, b) => a[0] - b[0])) {\n const last = merged.at(-1);\n if (last && start <= last[1] + GAP_EPSILON) {\n last[1] = Math.max(last[1], end);\n } else {\n merged.push([start, end]);\n }\n }\n return merged;\n}\n\n/**\n * The longitude run going east that is left once the widest uncovered gap in\n * the circle is taken out, or the whole circle when nothing is uncovered.\n */\nfunction runOutsideWidestGap(merged: readonly [number, number][]): {\n west: number;\n east: number;\n} {\n const first = merged[0] as [number, number];\n const last = merged[merged.length - 1] as [number, number];\n let gap = first[0] + TURN - last[1];\n let run = { west: first[0], east: last[1] };\n for (let index = 1; index < merged.length; index += 1) {\n const before = merged[index - 1] as [number, number];\n const after = merged[index] as [number, number];\n if (after[0] - before[1] > gap) {\n gap = after[0] - before[1];\n run = { west: after[0], east: before[1] + TURN };\n }\n }\n return gap > GAP_EPSILON ? run : { west: -ANTIMERIDIAN, east: ANTIMERIDIAN };\n}\n\n/**\n * The smallest latitude and longitude box holding every vertex of every path\n * and every edge between neighbouring vertices of one path, each edge taken\n * the short way round the globe (the way a path is drawn and measured). Used\n * to fit the map to a polygon, a line or a feature with several parts.\n * @param paths The rings and lines of a shape. A ring repeats its first\n * vertex at the end, as GeoJSON does; no edge joins two paths.\n * @returns `undefined` when no point has finite coordinates.\n * @remarks Longitude is a circle, so the box is the circle less its widest\n * gap that no vertex or edge covers. A shape wider than half the globe with\n * few vertices is therefore boxed on the side its edges cover, which\n * {@link boundsOfPoints} cannot know from vertices alone. A shape that\n * covers the whole circle, such as a ring round a pole, gives a `west` of\n * -180 and an `east` of 180. Otherwise the box has the form of\n * {@link boundsOfPoints}: `west` inside [-180, 180) and `east` not before it,\n * past 180 across the antimeridian. An edge of exactly half the globe has\n * no short way and is taken eastward, as it is drawn and measured. Latitudes are clamped to valid\n * coordinates, and an edge is a straight step in latitude.\n */\nexport function boundsOfPaths(\n paths: readonly (readonly LatLng[])[],\n): Bounds | undefined {\n const usable = paths\n .map((path) => path.filter(isFinitePoint))\n .filter((path) => path.length > 0);\n if (usable.length === 0) {\n return undefined;\n }\n const { west, east } = runOutsideWidestGap(\n mergeRuns(usable.flatMap(arcsOfPath).flatMap(withoutWrap)),\n );\n let south = Number.POSITIVE_INFINITY;\n let north = Number.NEGATIVE_INFINITY;\n for (const { lat } of usable.flat()) {\n south = Math.min(south, lat);\n north = Math.max(north, lat);\n }\n return {\n south: clamp(south, -90, 90),\n west,\n north: clamp(north, -90, 90),\n east,\n };\n}\n"],"mappings":";;;AAMA,IAAM,OAAO;AACb,IAAM,eAAe,OAAO;;AAE5B,IAAM,cAAc;AAQpB,SAAS,cAAc,OAAwB;CAC7C,OAAO,OAAO,SAAS,MAAM,GAAG,KAAK,OAAO,SAAS,MAAM,GAAG;AAChE;;;;;;;AAQA,SAAS,aAAa,MAAc,IAAoB;CACtD,MAAM,OAAO,cAAc,KAAK,IAAI;CACpC,OAAO,SAAS,OAAgB,eAAe;AACjD;;AAGA,SAAS,WAAW,MAAgC;CAClD,MAAM,OAAc,CAAC;CACrB,KAAK,SAAS,OAAO,UAAU;EAC7B,KAAK,KAAK;GAAE,OAAO,cAAc,MAAM,GAAG;GAAG,QAAQ;EAAE,CAAC;EACxD,MAAM,OAAO,KAAK,QAAQ;EAC1B,IAAI,CAAC,MACH;EAEF,MAAM,OAAO,aAAa,MAAM,KAAK,KAAK,GAAG;EAC7C,MAAM,OAAO,QAAQ,IAAI,MAAM,MAAM,KAAK;EAC1C,KAAK,KAAK;GAAE,OAAO,cAAc,IAAI;GAAG,QAAQ,KAAK,IAAI,IAAI;EAAE,CAAC;CAClE,CAAC;CACD,OAAO;AACT;;AAGA,SAAS,YAAY,KAA8B;CACjD,MAAM,MAAM,IAAI,QAAQ,IAAI;CAC5B,IAAI,OAAO,cACT,OAAO,CAAC,CAAC,IAAI,OAAO,GAAG,CAAC;CAE1B,OAAO,CACL,CAAC,IAAI,OAAO,YAAY,GACxB,CAAC,MAAe,MAAM,IAAI,CAC5B;AACF;;AAGA,SAAS,UAAU,MAAuD;CACxE,MAAM,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QAAQ,KAAK,IAAI,OAAO,GAAG;EAC3B,QAAQ,KAAK,IAAI,OAAO,GAAG;CAC7B;CACA,OAAO;EACL,OAAO,MAAM,OAAO,KAAK,EAAE;EAC3B;EACA,OAAO,MAAM,OAAO,KAAK,EAAE;EAC3B;CACF;AACF"}
|
|
@@ -1,24 +1,37 @@
|
|
|
1
|
+
import { shortestLongitudeSpan } from "../globe/longitude.js";
|
|
1
2
|
import { clamp } from "../numbers/clamp.js";
|
|
2
3
|
// src/spatial/boundsOfPoints.ts
|
|
3
4
|
/**
|
|
4
|
-
* The smallest latitude and longitude box holding every point,
|
|
5
|
-
* valid coordinates. Used to
|
|
6
|
-
* search bias box.
|
|
5
|
+
* The smallest latitude and longitude box holding every point, with its
|
|
6
|
+
* latitudes clamped to valid coordinates. Used to fit the map to a feature.
|
|
7
7
|
* @returns `undefined` when no point has finite coordinates.
|
|
8
|
-
* @remarks
|
|
9
|
-
* antimeridian
|
|
10
|
-
*
|
|
8
|
+
* @remarks The box takes the short way round the globe. Points on both sides
|
|
9
|
+
* of the antimeridian give a `west` inside [-180, 180) and an `east` past
|
|
10
|
+
* 180 (179 and -179 give 179 to 181), the form `shapeInputBounds` and
|
|
11
|
+
* {@link shortestLongitudeSpan} already use, so `west` is never greater than
|
|
12
|
+
* `east` and a caller that reads width as `east - west` stays correct. A box
|
|
13
|
+
* that does not cross the line keeps both edges inside [-180, 180]. Take the
|
|
14
|
+
* middle with `wrapLongitude((west + east) / 2)`. The engine's `fitBounds`
|
|
15
|
+
* accepts the unwrapped `east`. A set of points wider than half the globe
|
|
16
|
+
* with a wide gap in it may be boxed on the other side of the gap, because
|
|
17
|
+
* points say nothing about what lies between them. For a polygon or a line,
|
|
18
|
+
* use {@link boundsOfPaths}, which also holds every edge.
|
|
11
19
|
*/
|
|
12
20
|
function boundsOfPoints(points) {
|
|
13
21
|
const usable = points.filter((point) => Number.isFinite(point.lat) && Number.isFinite(point.lng));
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
const span = shortestLongitudeSpan(usable.map((point) => point.lng));
|
|
23
|
+
if (!span) return;
|
|
24
|
+
let south = Number.POSITIVE_INFINITY;
|
|
25
|
+
let north = Number.NEGATIVE_INFINITY;
|
|
26
|
+
for (const { lat } of usable) {
|
|
27
|
+
south = Math.min(south, lat);
|
|
28
|
+
north = Math.max(north, lat);
|
|
29
|
+
}
|
|
17
30
|
return {
|
|
18
|
-
south: clamp(
|
|
19
|
-
west:
|
|
20
|
-
north: clamp(
|
|
21
|
-
east:
|
|
31
|
+
south: clamp(south, -90, 90),
|
|
32
|
+
west: span.west,
|
|
33
|
+
north: clamp(north, -90, 90),
|
|
34
|
+
east: span.east
|
|
22
35
|
};
|
|
23
36
|
}
|
|
24
37
|
export { boundsOfPoints };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boundsOfPoints.js","names":[],"sources":["../../src/spatial/boundsOfPoints.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { clamp } from \"../numbers/clamp\";\nimport type { Bounds, LatLng } from \"../ports/geometry\";\n\n/**\n * The smallest latitude and longitude box holding every point,
|
|
1
|
+
{"version":3,"file":"boundsOfPoints.js","names":[],"sources":["../../src/spatial/boundsOfPoints.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { shortestLongitudeSpan } from \"../globe/longitude\";\nimport { clamp } from \"../numbers/clamp\";\nimport type { Bounds, LatLng } from \"../ports/geometry\";\n\n/**\n * The smallest latitude and longitude box holding every point, with its\n * latitudes clamped to valid coordinates. Used to fit the map to a feature.\n * @returns `undefined` when no point has finite coordinates.\n * @remarks The box takes the short way round the globe. Points on both sides\n * of the antimeridian give a `west` inside [-180, 180) and an `east` past\n * 180 (179 and -179 give 179 to 181), the form `shapeInputBounds` and\n * {@link shortestLongitudeSpan} already use, so `west` is never greater than\n * `east` and a caller that reads width as `east - west` stays correct. A box\n * that does not cross the line keeps both edges inside [-180, 180]. Take the\n * middle with `wrapLongitude((west + east) / 2)`. The engine's `fitBounds`\n * accepts the unwrapped `east`. A set of points wider than half the globe\n * with a wide gap in it may be boxed on the other side of the gap, because\n * points say nothing about what lies between them. For a polygon or a line,\n * use {@link boundsOfPaths}, which also holds every edge.\n */\nexport function boundsOfPoints(points: readonly LatLng[]): Bounds | undefined {\n const usable = points.filter(\n (point) => Number.isFinite(point.lat) && Number.isFinite(point.lng),\n );\n const span = shortestLongitudeSpan(usable.map((point) => point.lng));\n if (!span) {\n return undefined;\n }\n let south = Number.POSITIVE_INFINITY;\n let north = Number.NEGATIVE_INFINITY;\n for (const { lat } of usable) {\n south = Math.min(south, lat);\n north = Math.max(north, lat);\n }\n return {\n south: clamp(south, -90, 90),\n west: span.west,\n north: clamp(north, -90, 90),\n east: span.east,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,eAAe,QAA+C;CAC5E,MAAM,SAAS,OAAO,QACnB,UAAU,OAAO,SAAS,MAAM,GAAG,KAAK,OAAO,SAAS,MAAM,GAAG,CACpE;CACA,MAAM,OAAO,sBAAsB,OAAO,KAAK,UAAU,MAAM,GAAG,CAAC;CACnE,IAAI,CAAC,MACH;CAEF,IAAI,QAAQ,OAAO;CACnB,IAAI,QAAQ,OAAO;CACnB,KAAK,MAAM,EAAE,SAAS,QAAQ;EAC5B,QAAQ,KAAK,IAAI,OAAO,GAAG;EAC3B,QAAQ,KAAK,IAAI,OAAO,GAAG;CAC7B;CACA,OAAO;EACL,OAAO,MAAM,OAAO,KAAK,EAAE;EAC3B,MAAM,KAAK;EACX,OAAO,MAAM,OAAO,KAAK,EAAE;EAC3B,MAAM,KAAK;CACb;AACF"}
|