@math.gl/geospatial 5.0.0-beta.11 → 5.0.0-beta.12
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 -3
- package/dist/index.cjs +725 -229
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/projection/experimental/core.cjs.map +1 -1
- package/dist/projection/experimental/crs/spatial-reference.d.ts.map +1 -1
- package/dist/projection/experimental/crs/spatial-reference.js.map +1 -1
- package/dist/projection/experimental/crs/types.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/aea.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/aea.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/aea.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/aea.js +4 -1
- package/dist/projection/experimental/lazy-projections/aea.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/bonne.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/bonne.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/bonne.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/bonne.js +4 -1
- package/dist/projection/experimental/lazy-projections/bonne.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/cass.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/cass.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/cass.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/cass.js +4 -1
- package/dist/projection/experimental/lazy-projections/cass.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/eck6.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/eck6.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/eck6.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/eck6.js +4 -1
- package/dist/projection/experimental/lazy-projections/eck6.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/eqdc.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/eqdc.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/eqdc.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/eqdc.js +4 -1
- package/dist/projection/experimental/lazy-projections/eqdc.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/eqearth.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/eqearth.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/eqearth.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/eqearth.js +4 -1
- package/dist/projection/experimental/lazy-projections/eqearth.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/equi.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/equi.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/equi.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/equi.js +4 -1
- package/dist/projection/experimental/lazy-projections/equi.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/geocent.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/geocent.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/geocent.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/geocent.js +4 -1
- package/dist/projection/experimental/lazy-projections/geocent.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/geos.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/geos.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/geos.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/geos.js +4 -1
- package/dist/projection/experimental/lazy-projections/geos.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/gnom.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/gnom.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/gnom.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/gnom.js +4 -1
- package/dist/projection/experimental/lazy-projections/gnom.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/index.cjs +92 -23
- package/dist/projection/experimental/lazy-projections/index.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/krovak.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/krovak.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/krovak.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/krovak.js +4 -1
- package/dist/projection/experimental/lazy-projections/krovak.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/merc.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/merc.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/merc.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/merc.js +4 -1
- package/dist/projection/experimental/lazy-projections/merc.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/mill.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/mill.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/mill.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/mill.js +4 -1
- package/dist/projection/experimental/lazy-projections/mill.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/moll.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/moll.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/moll.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/moll.js +4 -1
- package/dist/projection/experimental/lazy-projections/moll.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/nzmg.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/nzmg.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/nzmg.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/nzmg.js +4 -1
- package/dist/projection/experimental/lazy-projections/nzmg.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/omerc.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/omerc.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/omerc.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/omerc.js +4 -1
- package/dist/projection/experimental/lazy-projections/omerc.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/ortho.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/ortho.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/ortho.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/ortho.js +4 -1
- package/dist/projection/experimental/lazy-projections/ortho.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/poly.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/poly.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/poly.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/poly.js +4 -1
- package/dist/projection/experimental/lazy-projections/poly.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/robin.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/robin.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/robin.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/robin.js +4 -1
- package/dist/projection/experimental/lazy-projections/robin.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/sinu.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/sinu.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/sinu.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/sinu.js +4 -1
- package/dist/projection/experimental/lazy-projections/sinu.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/stere.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/stere.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/stere.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/stere.js +4 -1
- package/dist/projection/experimental/lazy-projections/stere.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/tpers.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/tpers.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/tpers.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/tpers.js +4 -1
- package/dist/projection/experimental/lazy-projections/tpers.js.map +1 -1
- package/dist/projection/experimental/lazy-projections/vandg.cjs +4 -1
- package/dist/projection/experimental/lazy-projections/vandg.cjs.map +1 -1
- package/dist/projection/experimental/lazy-projections/vandg.d.ts.map +1 -1
- package/dist/projection/experimental/lazy-projections/vandg.js +4 -1
- package/dist/projection/experimental/lazy-projections/vandg.js.map +1 -1
- package/dist/projection/index.cjs.map +1 -1
- package/dist/projection/index.d.ts.map +1 -1
- package/dist/projection/index.js.map +1 -1
- package/dist/projection/lib/projection.js.map +1 -1
- package/dist/projection/pipeline.cjs.map +1 -1
- package/dist/projection/transform-analysis.cjs.map +1 -1
- package/dist/tasks/index.cjs.map +1 -1
- package/dist/terrain/cutout-polygons.d.ts +21 -0
- package/dist/terrain/cutout-polygons.d.ts.map +1 -0
- package/dist/terrain/cutout-polygons.js +172 -0
- package/dist/terrain/cutout-polygons.js.map +1 -0
- package/dist/terrain/cutout-region.d.ts +34 -0
- package/dist/terrain/cutout-region.d.ts.map +1 -0
- package/dist/terrain/cutout-region.js +33 -0
- package/dist/terrain/cutout-region.js.map +1 -0
- package/dist/terrain/geographic-cutout.d.ts +38 -0
- package/dist/terrain/geographic-cutout.d.ts.map +1 -0
- package/dist/terrain/geographic-cutout.js +120 -0
- package/dist/terrain/geographic-cutout.js.map +1 -0
- package/dist/terrain/index.cjs +532 -36
- package/dist/terrain/index.cjs.map +3 -3
- package/dist/terrain/index.d.ts +5 -0
- package/dist/terrain/index.d.ts.map +1 -1
- package/dist/terrain/index.js +4 -0
- package/dist/terrain/index.js.map +1 -1
- package/dist/terrain/mesh-cutout.d.ts +10 -5
- package/dist/terrain/mesh-cutout.d.ts.map +1 -1
- package/dist/terrain/mesh-cutout.js +62 -20
- package/dist/terrain/mesh-cutout.js.map +1 -1
- package/dist/terrain/tile-cutout-update.d.ts +73 -0
- package/dist/terrain/tile-cutout-update.d.ts.map +1 -0
- package/dist/terrain/tile-cutout-update.js +181 -0
- package/dist/terrain/tile-cutout-update.js.map +1 -0
- package/dist/terrain/tile-cutout.d.ts +33 -0
- package/dist/terrain/tile-cutout.d.ts.map +1 -0
- package/dist/terrain/tile-cutout.js +93 -0
- package/dist/terrain/tile-cutout.js.map +1 -0
- package/dist/turf/expressions.cjs +4 -4
- package/dist/turf/expressions.cjs.map +2 -2
- package/dist/turf/expressions.d.ts +1 -1
- package/dist/turf/expressions.d.ts.map +1 -1
- package/dist/turf/expressions.js +2 -2
- package/dist/turf/expressions.js.map +1 -1
- package/dist/turf.cjs +25 -25
- package/dist/turf.cjs.map +2 -2
- package/dist/turf.d.ts +1 -1
- package/dist/turf.d.ts.map +1 -1
- package/dist/turf.js +2 -2
- package/dist/turf.js.map +1 -1
- package/dist/web-mercator/assert.d.ts +2 -0
- package/dist/web-mercator/assert.d.ts.map +1 -0
- package/dist/web-mercator/assert.js +12 -0
- package/dist/web-mercator/assert.js.map +1 -0
- package/dist/web-mercator/fit-bounds.d.ts +52 -0
- package/dist/web-mercator/fit-bounds.d.ts.map +1 -0
- package/dist/web-mercator/fit-bounds.js +70 -0
- package/dist/web-mercator/fit-bounds.js.map +1 -0
- package/dist/web-mercator/fly-to-viewport.d.ts +19 -0
- package/dist/web-mercator/fly-to-viewport.d.ts.map +1 -0
- package/dist/web-mercator/fly-to-viewport.js +94 -0
- package/dist/web-mercator/fly-to-viewport.js.map +1 -0
- package/dist/web-mercator/get-bounds.d.ts +3 -0
- package/dist/web-mercator/get-bounds.d.ts.map +1 -0
- package/dist/web-mercator/get-bounds.js +56 -0
- package/dist/web-mercator/get-bounds.js.map +1 -0
- package/dist/web-mercator/index.cjs +705 -0
- package/dist/web-mercator/index.cjs.map +6 -0
- package/dist/web-mercator/index.d.ts +14 -0
- package/dist/web-mercator/index.d.ts.map +1 -0
- package/dist/web-mercator/index.js +14 -0
- package/dist/web-mercator/index.js.map +1 -0
- package/dist/web-mercator/math-utils.d.ts +7 -0
- package/dist/web-mercator/math-utils.d.ts.map +1 -0
- package/dist/web-mercator/math-utils.js +30 -0
- package/dist/web-mercator/math-utils.js.map +1 -0
- package/dist/web-mercator/mercator-meters.d.ts +29 -0
- package/dist/web-mercator/mercator-meters.d.ts.map +1 -0
- package/dist/web-mercator/mercator-meters.js +61 -0
- package/dist/web-mercator/mercator-meters.js.map +1 -0
- package/dist/web-mercator/normalize-viewport-props.d.ts +16 -0
- package/dist/web-mercator/normalize-viewport-props.d.ts.map +1 -0
- package/dist/web-mercator/normalize-viewport-props.js +45 -0
- package/dist/web-mercator/normalize-viewport-props.js.map +1 -0
- package/dist/web-mercator/web-mercator-utils.d.ts +192 -0
- package/dist/web-mercator/web-mercator-utils.d.ts.map +1 -0
- package/dist/web-mercator/web-mercator-utils.js +321 -0
- package/dist/web-mercator/web-mercator-utils.js.map +1 -0
- package/dist/web-mercator/web-mercator-viewport.d.ts +177 -0
- package/dist/web-mercator/web-mercator-viewport.d.ts.map +1 -0
- package/dist/web-mercator/web-mercator-viewport.js +276 -0
- package/dist/web-mercator/web-mercator-viewport.js.map +1 -0
- package/dist/web-mercator.d.ts +1 -1
- package/dist/web-mercator.d.ts.map +1 -1
- package/dist/web-mercator.js +1 -1
- package/dist/web-mercator.js.map +1 -1
- package/package.json +19 -458
- package/src/index.ts +36 -5
- package/src/projection/experimental/crs/spatial-reference.ts +1 -6
- package/src/projection/experimental/crs/types.ts +1 -1
- package/src/projection/experimental/crs/wkt.ts +1 -1
- package/src/projection/experimental/crs-descriptor.ts +1 -1
- package/src/projection/experimental/lazy-projections/aea.ts +4 -1
- package/src/projection/experimental/lazy-projections/bonne.ts +4 -1
- package/src/projection/experimental/lazy-projections/cass.ts +4 -1
- package/src/projection/experimental/lazy-projections/eck6.ts +4 -1
- package/src/projection/experimental/lazy-projections/eqdc.ts +4 -1
- package/src/projection/experimental/lazy-projections/eqearth.ts +4 -1
- package/src/projection/experimental/lazy-projections/equi.ts +4 -1
- package/src/projection/experimental/lazy-projections/geocent.ts +4 -1
- package/src/projection/experimental/lazy-projections/geos.ts +4 -1
- package/src/projection/experimental/lazy-projections/gnom.ts +4 -1
- package/src/projection/experimental/lazy-projections/krovak.ts +4 -1
- package/src/projection/experimental/lazy-projections/merc.ts +4 -1
- package/src/projection/experimental/lazy-projections/mill.ts +4 -1
- package/src/projection/experimental/lazy-projections/moll.ts +4 -1
- package/src/projection/experimental/lazy-projections/nzmg.ts +4 -1
- package/src/projection/experimental/lazy-projections/omerc.ts +4 -1
- package/src/projection/experimental/lazy-projections/ortho.ts +4 -1
- package/src/projection/experimental/lazy-projections/poly.ts +4 -1
- package/src/projection/experimental/lazy-projections/robin.ts +4 -1
- package/src/projection/experimental/lazy-projections/sinu.ts +4 -1
- package/src/projection/experimental/lazy-projections/stere.ts +4 -1
- package/src/projection/experimental/lazy-projections/tpers.ts +4 -1
- package/src/projection/experimental/lazy-projections/vandg.ts +4 -1
- package/src/projection/index.ts +1 -1
- package/src/projection/lib/projection.ts +1 -1
- package/src/terrain/cutout-polygons.ts +202 -0
- package/src/terrain/cutout-region.ts +54 -0
- package/src/terrain/geographic-cutout.ts +154 -0
- package/src/terrain/index.ts +24 -0
- package/src/terrain/mesh-cutout.ts +85 -23
- package/src/terrain/tile-cutout-update.ts +293 -0
- package/src/terrain/tile-cutout.ts +123 -0
- package/src/turf/expressions.ts +2 -2
- package/src/turf.ts +3 -7
- package/src/web-mercator/assert.ts +11 -0
- package/src/web-mercator/fit-bounds.ts +132 -0
- package/src/web-mercator/fly-to-viewport.ts +157 -0
- package/src/web-mercator/get-bounds.ts +62 -0
- package/src/web-mercator/index.ts +49 -0
- package/src/web-mercator/math-utils.ts +35 -0
- package/src/web-mercator/mercator-meters.ts +83 -0
- package/src/web-mercator/normalize-viewport-props.ts +58 -0
- package/src/web-mercator/web-mercator-utils.ts +468 -0
- package/src/web-mercator/web-mercator-viewport.ts +406 -0
- package/src/web-mercator.ts +1 -1
- package/dist/projection/experimental/index.cjs +0 -11707
- package/dist/projection/experimental/index.cjs.map +0 -6
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
copyTerrainCutoutRegion,
|
|
7
|
+
sameTerrainCutoutRegion,
|
|
8
|
+
type TerrainCutoutRegion
|
|
9
|
+
} from './cutout-region';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
splitTerrainMesh,
|
|
13
|
+
validateTerrainCutoutTopology,
|
|
14
|
+
type TerrainCutout,
|
|
15
|
+
type TerrainCutoutInput,
|
|
16
|
+
type TerrainCutoutOptions,
|
|
17
|
+
type TerrainCutoutPiece
|
|
18
|
+
} from './mesh-cutout';
|
|
19
|
+
import {assembleTerrainCutoutTiles} from './tile-cutout';
|
|
20
|
+
|
|
21
|
+
/** Stable resident identity and an explicit revision for in-place buffer edits. */
|
|
22
|
+
export type TerrainCutoutResident = {
|
|
23
|
+
/** Unique resident tile ID. */
|
|
24
|
+
id: string;
|
|
25
|
+
/** Nonnegative safe integer; increment after changing any mesh buffer. */
|
|
26
|
+
revision: number;
|
|
27
|
+
/** Conforming terrain in one shared Cartesian frame. Treat as immutable within a revision. */
|
|
28
|
+
mesh: TerrainCutoutInput;
|
|
29
|
+
};
|
|
30
|
+
/** Per-tile clipping limits plus bounds on resident-set reconciliation. */
|
|
31
|
+
export type TerrainTileCutoutUpdateOptions = TerrainCutoutOptions & {
|
|
32
|
+
/** Maximum residents. Default 512. */
|
|
33
|
+
maxTiles?: number;
|
|
34
|
+
/** Maximum tile-pair broad-phase checks per update. Default 131072. */
|
|
35
|
+
maxPairChecks?: number;
|
|
36
|
+
/** Maximum exposed piece edges across all residents. Default 1000000. */
|
|
37
|
+
maxBoundaryEdges?: number;
|
|
38
|
+
};
|
|
39
|
+
/** Directed boundary edge with exact coordinate keys and local vertex IDs. */
|
|
40
|
+
type Boundary = {
|
|
41
|
+
/** Incident face polygon index; remainder faces use the Uint32 sentinel 0xffffffff. */
|
|
42
|
+
polygon: number;
|
|
43
|
+
key: string;
|
|
44
|
+
start: string;
|
|
45
|
+
end: string;
|
|
46
|
+
a: number;
|
|
47
|
+
b: number;
|
|
48
|
+
side: 0 | 1;
|
|
49
|
+
};
|
|
50
|
+
/** Cached clipping and boundary summaries for one immutable tile revision. */
|
|
51
|
+
type Entry = {
|
|
52
|
+
resident: TerrainCutoutResident;
|
|
53
|
+
split: TerrainCutout;
|
|
54
|
+
displayed: TerrainCutout;
|
|
55
|
+
bounds: readonly [number, number, number, number];
|
|
56
|
+
edges: Boundary[];
|
|
57
|
+
};
|
|
58
|
+
/** Private cache brand; the cache is local to this module instance. */
|
|
59
|
+
const CACHE = Symbol('terrain-cutout-cache');
|
|
60
|
+
/** Opaque reusable cache. Do not mutate cached results or resident buffers without a new revision. */
|
|
61
|
+
export type TerrainTileCutoutCache = {
|
|
62
|
+
readonly [CACHE]: {
|
|
63
|
+
polygon: TerrainCutoutRegion;
|
|
64
|
+
options: Required<TerrainTileCutoutUpdateOptions>;
|
|
65
|
+
entries: ReadonlyMap<string, Entry>;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
/** Detached update result, with unchanged surface buffers retained by identity. */
|
|
69
|
+
export type TerrainTileCutoutUpdate = {
|
|
70
|
+
/** Pass to the next update; no state is committed if an update throws. */
|
|
71
|
+
cache: TerrainTileCutoutCache;
|
|
72
|
+
/** Resident cuts. Source triangle references are local to the corresponding input tile. */
|
|
73
|
+
tiles: ReadonlyMap<string, TerrainCutout>;
|
|
74
|
+
/** Tiles whose surfaces or exposed cut edges changed. */
|
|
75
|
+
changed: string[];
|
|
76
|
+
/** Tiles removed from the previous resident set. */
|
|
77
|
+
removed: string[];
|
|
78
|
+
/** Tiles actually clipped in this update; unchanged tiles are not reclipped. */
|
|
79
|
+
rebuilt: string[];
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Clip changed tiles independently and pair their exposed boundaries.
|
|
83
|
+
* Mixed-level inputs must already be reconciled. Cached source buffers and
|
|
84
|
+
* returned arrays are read-only by contract. Change revision after in-place
|
|
85
|
+
* edits; replacing the mesh object also invalidates its cache. Polygon or
|
|
86
|
+
* option changes invalidate all clips. Limits maxWork/maxVertices/maxTriangles
|
|
87
|
+
* apply per clip and per adjacent-pair validation, not to the entire update.
|
|
88
|
+
* Arrival/eviction can change a neighbor's cutEdges without rebuilding its mesh.
|
|
89
|
+
*/
|
|
90
|
+
export function updateTerrainTileCutouts(
|
|
91
|
+
residents: readonly TerrainCutoutResident[],
|
|
92
|
+
polygon: TerrainCutoutRegion,
|
|
93
|
+
previous?: TerrainTileCutoutCache,
|
|
94
|
+
options: TerrainTileCutoutUpdateOptions = {}
|
|
95
|
+
): TerrainTileCutoutUpdate {
|
|
96
|
+
const limits: Required<TerrainTileCutoutUpdateOptions> = {
|
|
97
|
+
maxTiles: options.maxTiles ?? 512,
|
|
98
|
+
maxPairChecks: options.maxPairChecks ?? 131072,
|
|
99
|
+
maxBoundaryEdges: options.maxBoundaryEdges ?? 1000000,
|
|
100
|
+
maxWork: options.maxWork ?? 4000000,
|
|
101
|
+
maxVertices: options.maxVertices ?? 500000,
|
|
102
|
+
maxTriangles: options.maxTriangles ?? 500000
|
|
103
|
+
};
|
|
104
|
+
if (
|
|
105
|
+
Object.values(limits).some(n => !Number.isSafeInteger(n) || n < 0) ||
|
|
106
|
+
limits.maxVertices > 0xffffffff ||
|
|
107
|
+
limits.maxTriangles > 0xffffffff ||
|
|
108
|
+
residents.length > limits.maxTiles
|
|
109
|
+
)
|
|
110
|
+
throw new RangeError('Invalid terrain cutout update limits');
|
|
111
|
+
const prior = previous?.[CACHE];
|
|
112
|
+
const compatible =
|
|
113
|
+
prior &&
|
|
114
|
+
sameTerrainCutoutRegion(prior.polygon, polygon) &&
|
|
115
|
+
(Object.keys(limits) as (keyof typeof limits)[]).every(
|
|
116
|
+
key => prior.options[key] === limits[key]
|
|
117
|
+
);
|
|
118
|
+
// Validate the ring even for an empty resident set.
|
|
119
|
+
if (!compatible)
|
|
120
|
+
splitTerrainMesh({positions: new Float64Array(), indices: new Uint32Array()}, polygon, limits);
|
|
121
|
+
const entries = new Map<string, Entry>();
|
|
122
|
+
const rebuilt: string[] = [];
|
|
123
|
+
let boundaryCount = 0;
|
|
124
|
+
for (const resident of residents) {
|
|
125
|
+
if (
|
|
126
|
+
!resident.id ||
|
|
127
|
+
typeof resident.id !== 'string' ||
|
|
128
|
+
entries.has(resident.id) ||
|
|
129
|
+
!Number.isSafeInteger(resident.revision) ||
|
|
130
|
+
resident.revision < 0
|
|
131
|
+
)
|
|
132
|
+
throw new RangeError('Invalid or duplicate terrain cutout resident');
|
|
133
|
+
const old = compatible ? prior.entries.get(resident.id) : undefined;
|
|
134
|
+
let entry: Entry;
|
|
135
|
+
if (old && old.resident.mesh === resident.mesh && old.resident.revision === resident.revision)
|
|
136
|
+
entry = old;
|
|
137
|
+
else {
|
|
138
|
+
const split = splitTerrainMesh(resident.mesh, polygon, limits);
|
|
139
|
+
const edges = [...boundaryEdges(split.patch, 0), ...boundaryEdges(split.remainder, 1)];
|
|
140
|
+
const bounds: [number, number, number, number] = [Infinity, Infinity, -Infinity, -Infinity];
|
|
141
|
+
for (const id of resident.mesh.indices) {
|
|
142
|
+
const x = resident.mesh.positions[id * 3],
|
|
143
|
+
y = resident.mesh.positions[id * 3 + 1];
|
|
144
|
+
bounds[0] = Math.min(bounds[0], x);
|
|
145
|
+
bounds[1] = Math.min(bounds[1], y);
|
|
146
|
+
bounds[2] = Math.max(bounds[2], x);
|
|
147
|
+
bounds[3] = Math.max(bounds[3], y);
|
|
148
|
+
}
|
|
149
|
+
entry = {resident: {...resident}, split, displayed: split, bounds, edges};
|
|
150
|
+
rebuilt.push(resident.id);
|
|
151
|
+
}
|
|
152
|
+
boundaryCount += entry.edges.length;
|
|
153
|
+
if (boundaryCount > limits.maxBoundaryEdges)
|
|
154
|
+
throw new RangeError('Terrain cutout boundary budget exceeded');
|
|
155
|
+
entries.set(resident.id, entry);
|
|
156
|
+
}
|
|
157
|
+
const values = [...entries.values()];
|
|
158
|
+
let pairs = 0;
|
|
159
|
+
for (let i = 0; i < values.length; i++)
|
|
160
|
+
for (let j = i + 1; j < values.length; j++) {
|
|
161
|
+
if (++pairs > limits.maxPairChecks)
|
|
162
|
+
throw new RangeError('Terrain cutout pair budget exceeded');
|
|
163
|
+
const a = values[i],
|
|
164
|
+
b = values[j];
|
|
165
|
+
if (
|
|
166
|
+
a.bounds[2] < b.bounds[0] ||
|
|
167
|
+
b.bounds[2] < a.bounds[0] ||
|
|
168
|
+
a.bounds[3] < b.bounds[1] ||
|
|
169
|
+
b.bounds[3] < a.bounds[1]
|
|
170
|
+
)
|
|
171
|
+
continue;
|
|
172
|
+
if (
|
|
173
|
+
compatible &&
|
|
174
|
+
prior.entries.get(a.resident.id) === a &&
|
|
175
|
+
prior.entries.get(b.resident.id) === b
|
|
176
|
+
)
|
|
177
|
+
continue;
|
|
178
|
+
const combined = assembleTerrainCutoutTiles([a.resident.mesh, b.resident.mesh], limits);
|
|
179
|
+
validateTerrainCutoutTopology(combined.mesh, combined.remainingWork);
|
|
180
|
+
}
|
|
181
|
+
const boundaries = new Map<string, {edge: Boundary; paired: boolean}>();
|
|
182
|
+
const cutKeys = new Map<string, number>();
|
|
183
|
+
for (let tile = 0; tile < values.length; tile++)
|
|
184
|
+
for (const edge of values[tile].edges) {
|
|
185
|
+
const other = boundaries.get(edge.key);
|
|
186
|
+
if (!other) {
|
|
187
|
+
boundaries.set(edge.key, {edge, paired: false});
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (other.paired || other.edge.start !== edge.end || other.edge.end !== edge.start)
|
|
191
|
+
throw new RangeError('Nonconforming terrain cutout tile boundary');
|
|
192
|
+
other.paired = true;
|
|
193
|
+
if (other.edge.side !== edge.side) {
|
|
194
|
+
cutKeys.set(edge.key, edge.side === 0 ? edge.polygon : other.edge.polygon);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const tiles = new Map<string, TerrainCutout>();
|
|
198
|
+
const changed: string[] = [];
|
|
199
|
+
for (let i = 0; i < values.length; i++) {
|
|
200
|
+
const entry = values[i];
|
|
201
|
+
const old = prior?.entries.get(entry.resident.id);
|
|
202
|
+
// Emit in cached tile-local order, independent of resident visitation order.
|
|
203
|
+
const cuts: [number[], number[]] = [[], []];
|
|
204
|
+
const polygons: [number[], number[]] = [[], []];
|
|
205
|
+
for (const edge of entry.edges) {
|
|
206
|
+
const polygonIndex = cutKeys.get(edge.key);
|
|
207
|
+
if (polygonIndex !== undefined) {
|
|
208
|
+
cuts[edge.side].push(edge.a, edge.b);
|
|
209
|
+
polygons[edge.side].push(polygonIndex);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const patch = withEdges(entry.split.patch, cuts[0], polygons[0], old?.displayed.patch);
|
|
213
|
+
const remainder = withEdges(
|
|
214
|
+
entry.split.remainder,
|
|
215
|
+
cuts[1],
|
|
216
|
+
polygons[1],
|
|
217
|
+
old?.displayed.remainder
|
|
218
|
+
);
|
|
219
|
+
const displayed =
|
|
220
|
+
old && patch === old.displayed.patch && remainder === old.displayed.remainder
|
|
221
|
+
? old.displayed
|
|
222
|
+
: {patch, remainder};
|
|
223
|
+
if (displayed !== old?.displayed) changed.push(entry.resident.id);
|
|
224
|
+
entries.set(entry.resident.id, displayed === entry.displayed ? entry : {...entry, displayed});
|
|
225
|
+
tiles.set(entry.resident.id, displayed);
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
cache: {[CACHE]: {polygon: copyTerrainCutoutRegion(polygon), options: limits, entries}},
|
|
229
|
+
tiles,
|
|
230
|
+
changed,
|
|
231
|
+
rebuilt,
|
|
232
|
+
removed: prior ? [...prior.entries.keys()].filter(id => !entries.has(id)) : []
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Reuse surface buffers and unchanged boundary descriptors. */
|
|
237
|
+
function withEdges(
|
|
238
|
+
piece: TerrainCutoutPiece,
|
|
239
|
+
edges: number[],
|
|
240
|
+
polygons: number[],
|
|
241
|
+
old?: TerrainCutoutPiece
|
|
242
|
+
) {
|
|
243
|
+
const same =
|
|
244
|
+
old &&
|
|
245
|
+
old.positions === piece.positions &&
|
|
246
|
+
old.cutEdges.length === edges.length &&
|
|
247
|
+
old.cutEdges.every((id, i) => id === edges[i]) &&
|
|
248
|
+
old.cutPolygonIndices.every((id, i) => id === polygons[i]);
|
|
249
|
+
return same
|
|
250
|
+
? old
|
|
251
|
+
: {...piece, cutEdges: Uint32Array.from(edges), cutPolygonIndices: Uint32Array.from(polygons)};
|
|
252
|
+
}
|
|
253
|
+
/** Cache only exposed edges; interior triangulation edges never become walls. */
|
|
254
|
+
function boundaryEdges(piece: TerrainCutoutPiece, side: 0 | 1): Boundary[] {
|
|
255
|
+
const edges = new Map<
|
|
256
|
+
string,
|
|
257
|
+
{
|
|
258
|
+
a: number;
|
|
259
|
+
b: number;
|
|
260
|
+
count: number;
|
|
261
|
+
/** Incident face polygon index, including the remainder sentinel 0xffffffff. */
|
|
262
|
+
polygon: number;
|
|
263
|
+
}
|
|
264
|
+
>();
|
|
265
|
+
for (let i = 0; i < piece.indices.length; i += 3)
|
|
266
|
+
for (let j = 0; j < 3; j++) {
|
|
267
|
+
const a = piece.indices[i + j],
|
|
268
|
+
b = piece.indices[i + ((j + 1) % 3)];
|
|
269
|
+
const key = a < b ? `${a},${b}` : `${b},${a}`;
|
|
270
|
+
const old = edges.get(key);
|
|
271
|
+
if (old) old.count++;
|
|
272
|
+
else edges.set(key, {a, b, count: 1, polygon: piece.polygonIndices[i / 3]});
|
|
273
|
+
}
|
|
274
|
+
const result: Boundary[] = [];
|
|
275
|
+
/** Encode exact XYZ; no snapping or tolerance-based topology decisions. */
|
|
276
|
+
const point = (id: number) =>
|
|
277
|
+
`${piece.positions[id * 3]},${piece.positions[id * 3 + 1]},${piece.positions[id * 3 + 2]}`;
|
|
278
|
+
for (const {a, b, count, polygon} of edges.values())
|
|
279
|
+
if (count === 1) {
|
|
280
|
+
const start = point(a),
|
|
281
|
+
end = point(b);
|
|
282
|
+
result.push({
|
|
283
|
+
polygon,
|
|
284
|
+
a,
|
|
285
|
+
b,
|
|
286
|
+
side,
|
|
287
|
+
start,
|
|
288
|
+
end,
|
|
289
|
+
key: start < end ? `${start}/${end}` : `${end}/${start}`
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
return result;
|
|
293
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TerrainCutoutRegion} from './cutout-region';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
splitTerrainMesh,
|
|
9
|
+
type TerrainCutout,
|
|
10
|
+
type TerrainCutoutInput,
|
|
11
|
+
type TerrainCutoutOptions
|
|
12
|
+
} from './mesh-cutout';
|
|
13
|
+
|
|
14
|
+
/** A resident tile expressed in the same local Cartesian frame as its neighbors. */
|
|
15
|
+
export type TerrainCutoutTile = TerrainCutoutInput;
|
|
16
|
+
|
|
17
|
+
/** A resident-set cut and its input-face provenance. */
|
|
18
|
+
export type TerrainTileCutout = TerrainCutout & {
|
|
19
|
+
/** Tile-array index for each input face referenced by either piece. */
|
|
20
|
+
sourceTiles: Uint32Array;
|
|
21
|
+
/** Original tile triangle index for each input face referenced by either piece. */
|
|
22
|
+
sourceTileFaces: Uint32Array;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Split a conforming resident tile set, with paired walls only along the cut.
|
|
26
|
+
*
|
|
27
|
+
* Vertices with identical XYZ are welded before clipping. Mixed-level neighbors
|
|
28
|
+
* must first be reconciled with stitchTerrainMeshEdges or stitchTerrainLodCut;
|
|
29
|
+
* hanging vertices, overlapping tiles and conflicting elevations are rejected.
|
|
30
|
+
* No snapping, height adjustment, missing-tile filling or tile caching occurs.
|
|
31
|
+
* Recompute from the new resident set after arrival or eviction. Source triangle
|
|
32
|
+
* and barycentric references retain the original tile face's corner order.
|
|
33
|
+
* Limits apply to the total input (including duplicate vertices) and the splitter.
|
|
34
|
+
*/
|
|
35
|
+
export function splitTerrainMeshTiles(
|
|
36
|
+
tiles: readonly TerrainCutoutTile[],
|
|
37
|
+
polygon: TerrainCutoutRegion,
|
|
38
|
+
options: TerrainCutoutOptions = {}
|
|
39
|
+
): TerrainTileCutout {
|
|
40
|
+
const assembled = assembleTerrainCutoutTiles(tiles, options);
|
|
41
|
+
return {
|
|
42
|
+
...splitTerrainMesh(assembled.mesh, polygon, {...options, maxWork: assembled.remainingWork}),
|
|
43
|
+
sourceTiles: assembled.sourceTiles,
|
|
44
|
+
sourceTileFaces: assembled.sourceTileFaces
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @internal Assemble a bounded, exact-welded mesh without clipping it. */
|
|
49
|
+
export function assembleTerrainCutoutTiles(
|
|
50
|
+
tiles: readonly TerrainCutoutInput[],
|
|
51
|
+
options: TerrainCutoutOptions = {}
|
|
52
|
+
) {
|
|
53
|
+
const maxVertices = options.maxVertices ?? 500_000;
|
|
54
|
+
const maxTriangles = options.maxTriangles ?? 500_000;
|
|
55
|
+
const maxWork = options.maxWork ?? 4_000_000;
|
|
56
|
+
if (
|
|
57
|
+
![maxVertices, maxTriangles, maxWork].every(n => Number.isSafeInteger(n) && n >= 0) ||
|
|
58
|
+
maxVertices > 0xffffffff ||
|
|
59
|
+
maxTriangles > 0xffffffff
|
|
60
|
+
)
|
|
61
|
+
throw new RangeError('Invalid terrain tile cutout limits');
|
|
62
|
+
let vertexCount = 0;
|
|
63
|
+
let faceCount = 0;
|
|
64
|
+
let work = 0;
|
|
65
|
+
for (const tile of tiles) {
|
|
66
|
+
if (
|
|
67
|
+
!(tile.positions instanceof Float32Array || tile.positions instanceof Float64Array) ||
|
|
68
|
+
!(tile.indices instanceof Uint16Array || tile.indices instanceof Uint32Array) ||
|
|
69
|
+
tile.positions.length % 3 ||
|
|
70
|
+
tile.indices.length % 3
|
|
71
|
+
)
|
|
72
|
+
throw new RangeError('Invalid terrain tile cutout mesh');
|
|
73
|
+
vertexCount += tile.positions.length / 3;
|
|
74
|
+
faceCount += tile.indices.length / 3;
|
|
75
|
+
work += 1 + tile.positions.length / 3 + tile.indices.length;
|
|
76
|
+
if (vertexCount > maxVertices || faceCount > maxTriangles || work > maxWork)
|
|
77
|
+
throw new RangeError('Terrain tile cutout input budget exceeded');
|
|
78
|
+
}
|
|
79
|
+
const positions = new Float64Array(vertexCount * 3);
|
|
80
|
+
const indices = new Uint32Array(faceCount * 3);
|
|
81
|
+
const sourceTiles = new Uint32Array(faceCount);
|
|
82
|
+
const sourceTileFaces = new Uint32Array(faceCount);
|
|
83
|
+
const vertices = new Map<string, number>();
|
|
84
|
+
let vertexOffset = 0;
|
|
85
|
+
let faceOffset = 0;
|
|
86
|
+
for (let tileIndex = 0; tileIndex < tiles.length; tileIndex++) {
|
|
87
|
+
const tile = tiles[tileIndex];
|
|
88
|
+
const remap = new Uint32Array(tile.positions.length / 3);
|
|
89
|
+
for (let i = 0; i < remap.length; i++) {
|
|
90
|
+
const x = tile.positions[i * 3];
|
|
91
|
+
const y = tile.positions[i * 3 + 1];
|
|
92
|
+
const z = tile.positions[i * 3 + 2];
|
|
93
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z))
|
|
94
|
+
throw new RangeError('Invalid terrain tile cutout coordinate');
|
|
95
|
+
const key = `${x},${y},${z}`;
|
|
96
|
+
let id = vertices.get(key);
|
|
97
|
+
if (id === undefined) {
|
|
98
|
+
id = vertexOffset++;
|
|
99
|
+
vertices.set(key, id);
|
|
100
|
+
positions[id * 3] = x;
|
|
101
|
+
positions[id * 3 + 1] = y;
|
|
102
|
+
positions[id * 3 + 2] = z;
|
|
103
|
+
}
|
|
104
|
+
remap[i] = id;
|
|
105
|
+
}
|
|
106
|
+
for (let i = 0; i < tile.indices.length; i++) {
|
|
107
|
+
const id = tile.indices[i];
|
|
108
|
+
if (id >= remap.length) throw new RangeError('Invalid terrain tile cutout index');
|
|
109
|
+
indices[faceOffset * 3 + i] = remap[id];
|
|
110
|
+
}
|
|
111
|
+
for (let i = 0; i < tile.indices.length / 3; i++) {
|
|
112
|
+
sourceTiles[faceOffset + i] = tileIndex;
|
|
113
|
+
sourceTileFaces[faceOffset + i] = i;
|
|
114
|
+
}
|
|
115
|
+
faceOffset += tile.indices.length / 3;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
mesh: {positions: positions.subarray(0, vertexOffset * 3), indices},
|
|
119
|
+
sourceTiles,
|
|
120
|
+
sourceTileFaces,
|
|
121
|
+
remainingWork: maxWork - work
|
|
122
|
+
};
|
|
123
|
+
}
|
package/src/turf/expressions.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import {DataType, Precision, makeData, makeVector, type Field} from 'apache-arrow';
|
|
6
6
|
import type {ArrowTableFunction} from '@math.gl/sql/arrow';
|
|
7
|
-
import {makeGeoArrowColumnFromArrowVector} from '@math.gl/arrow
|
|
8
|
-
import {isGeoArrowValueValid, type GeoArrowColumn} from '@math.gl/arrow
|
|
7
|
+
import {makeGeoArrowColumnFromArrowVector} from '@math.gl/core/arrow';
|
|
8
|
+
import {isGeoArrowValueValid, type GeoArrowColumn} from '@math.gl/core/arrow';
|
|
9
9
|
import type {TurfBatchResult} from '../turf';
|
|
10
10
|
|
|
11
11
|
/** Adapter for row-preserving numeric/Boolean functions from @math.gl/geospatial/turf. */
|
package/src/turf.ts
CHANGED
|
@@ -2,13 +2,9 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type {GeoArrowArray, GeoArrowColumn} from '@math.gl/arrow
|
|
6
|
-
import {isGeoArrowValueValid} from '@math.gl/arrow
|
|
7
|
-
import {
|
|
8
|
-
getGeoArrowRowBounds,
|
|
9
|
-
mapGeoArrowCoordinates,
|
|
10
|
-
rewindGeoArrow
|
|
11
|
-
} from '@math.gl/arrow/geoarrow-utils';
|
|
5
|
+
import type {GeoArrowArray, GeoArrowColumn} from '@math.gl/core/arrow';
|
|
6
|
+
import {isGeoArrowValueValid} from '@math.gl/core/arrow';
|
|
7
|
+
import {getGeoArrowRowBounds, mapGeoArrowCoordinates, rewindGeoArrow} from '@math.gl/core/arrow';
|
|
12
8
|
|
|
13
9
|
/** A nullable, row-aligned numeric or boolean result. Validity is an Arrow bitmap. */
|
|
14
10
|
export type TurfBatchResult<T extends Float64Array | Uint8Array> = {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
// Replacement for the external assert method to reduce bundle size
|
|
5
|
+
// Note: We don't use the second "message" argument in calling code,
|
|
6
|
+
// so no need to support it here
|
|
7
|
+
export function assert(condition: unknown, message?: string): void {
|
|
8
|
+
if (!condition) {
|
|
9
|
+
throw new Error(message || '@math.gl/geospatial/web-mercator: assertion failed.');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {assert} from './assert';
|
|
5
|
+
import {log2, clamp} from './math-utils';
|
|
6
|
+
import {MAX_LATITUDE, lngLatToWorld, worldToLngLat} from './web-mercator-utils';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Options for fitBounds
|
|
10
|
+
*/
|
|
11
|
+
export type FitBoundsOptions = {
|
|
12
|
+
/** viewport width */
|
|
13
|
+
width: number;
|
|
14
|
+
/** viewport height */
|
|
15
|
+
height: number;
|
|
16
|
+
/** [[lon, lat], [lon, lat]] */
|
|
17
|
+
bounds: [[number, number], [number, number]];
|
|
18
|
+
/** The width/height of the bounded area will never be smaller than this. 0.01 would be about 1000 meters (degree is ~110KM) */
|
|
19
|
+
minExtent?: number;
|
|
20
|
+
/** The maximum zoom level to fit the bounds within. */
|
|
21
|
+
maxZoom?: number; // ~x4,000,000 => About 10 meter extents
|
|
22
|
+
/**
|
|
23
|
+
* padding - The amount of padding in pixels to add to the given bounds.
|
|
24
|
+
* Can also be an object with top, bottom, left and right properties defining the padding.
|
|
25
|
+
*/
|
|
26
|
+
padding?: number | Padding;
|
|
27
|
+
/** Pixel offset from the padded center. Positive x moves right and positive y moves down. */
|
|
28
|
+
offset?: number[];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* An object describing the padding to add to the bounds.
|
|
33
|
+
*/
|
|
34
|
+
export type Padding = {
|
|
35
|
+
/** Padding from top in pixels to add to the given bounds */
|
|
36
|
+
top: number;
|
|
37
|
+
/** Padding from bottom in pixels to add to the given bounds */
|
|
38
|
+
bottom: number;
|
|
39
|
+
/** Padding from left in pixels to add to the given bounds */
|
|
40
|
+
left: number;
|
|
41
|
+
/** Padding from right in pixels to add to the given bounds */
|
|
42
|
+
right: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type ViewportProps = {
|
|
46
|
+
longitude: number;
|
|
47
|
+
latitude: number;
|
|
48
|
+
zoom: number;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns map settings {latitude, longitude, zoom}
|
|
53
|
+
* that will contain the provided corners within the provided width.
|
|
54
|
+
*
|
|
55
|
+
* > _Note: Only supports non-perspective mode._
|
|
56
|
+
*
|
|
57
|
+
* @param options fit bounds parameters
|
|
58
|
+
* @returns - latitude, longitude and zoom
|
|
59
|
+
*/
|
|
60
|
+
export function fitBounds(options: FitBoundsOptions): ViewportProps {
|
|
61
|
+
const {
|
|
62
|
+
width,
|
|
63
|
+
height,
|
|
64
|
+
bounds,
|
|
65
|
+
minExtent = 0, // 0.01 would be about 1000 meters (degree is ~110KM)
|
|
66
|
+
maxZoom = 24, // ~x4,000,000 => About 10 meter extents
|
|
67
|
+
offset = [0, 0]
|
|
68
|
+
} = options;
|
|
69
|
+
|
|
70
|
+
const [[west, south], [east, north]] = bounds;
|
|
71
|
+
const padding = getPaddingObject(options.padding);
|
|
72
|
+
|
|
73
|
+
const nw = lngLatToWorld([west, clamp(north, -MAX_LATITUDE, MAX_LATITUDE)]);
|
|
74
|
+
const se = lngLatToWorld([east, clamp(south, -MAX_LATITUDE, MAX_LATITUDE)]);
|
|
75
|
+
|
|
76
|
+
// width/height on the Web Mercator plane
|
|
77
|
+
const size = [
|
|
78
|
+
Math.max(Math.abs(se[0] - nw[0]), minExtent),
|
|
79
|
+
Math.max(Math.abs(se[1] - nw[1]), minExtent)
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const targetSize = [
|
|
83
|
+
width - padding.left - padding.right - Math.abs(offset[0]) * 2,
|
|
84
|
+
height - padding.top - padding.bottom - Math.abs(offset[1]) * 2
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
assert(targetSize[0] > 0 && targetSize[1] > 0);
|
|
88
|
+
|
|
89
|
+
// scale = screen pixels per unit on the Web Mercator plane
|
|
90
|
+
const scaleX = targetSize[0] / size[0];
|
|
91
|
+
const scaleY = targetSize[1] / size[1];
|
|
92
|
+
|
|
93
|
+
const zoom = Math.min(maxZoom, log2(Math.abs(Math.min(scaleX, scaleY))));
|
|
94
|
+
assert(Number.isFinite(zoom));
|
|
95
|
+
|
|
96
|
+
// Translate the viewport center by the inverse of the desired screen-space shift.
|
|
97
|
+
const scale = 2 ** zoom;
|
|
98
|
+
const shiftX = (padding.left - padding.right) / 2 + offset[0];
|
|
99
|
+
const shiftY = (padding.top - padding.bottom) / 2 + offset[1];
|
|
100
|
+
const centerLngLat = worldToLngLat([
|
|
101
|
+
(se[0] + nw[0]) / 2 - shiftX / scale,
|
|
102
|
+
(se[1] + nw[1]) / 2 + shiftY / scale
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
longitude: centerLngLat[0],
|
|
107
|
+
latitude: centerLngLat[1],
|
|
108
|
+
zoom
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Helpers
|
|
113
|
+
function getPaddingObject(padding: number | Padding = 0): Padding {
|
|
114
|
+
if (typeof padding === 'number') {
|
|
115
|
+
return {
|
|
116
|
+
top: padding,
|
|
117
|
+
bottom: padding,
|
|
118
|
+
left: padding,
|
|
119
|
+
right: padding
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Make sure all the required properties are set
|
|
124
|
+
assert(
|
|
125
|
+
Number.isFinite(padding.top) &&
|
|
126
|
+
Number.isFinite(padding.bottom) &&
|
|
127
|
+
Number.isFinite(padding.left) &&
|
|
128
|
+
Number.isFinite(padding.right)
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
return padding;
|
|
132
|
+
}
|