@vim3d/geojson 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,4 +48,32 @@ comes out of `maxPositions: 5000` with all 41 polygons kept.
48
48
  - `toSceneCurves(input, origin, { elevation })`: lines and polygon rings as
49
49
  `curve` geometries in the Vim3D `world` frame around `origin` (x east, y up,
50
50
  -z north), with each feature's `properties` and `id`.
51
+ - `toScenePoints(input, origin, { elevation })`: every `Point` and
52
+ `MultiPoint` position in the same frame, with its feature's `properties`
53
+ and `id`, for labels, trees and other markers.
54
+ - `toSceneFills(input, origin, { elevation })`: each polygon (each
55
+ `MultiPolygon` part on its own) as a flat, upward-facing `mesh` geometry
56
+ with its holes cut out, triangulated with
57
+ [earcut](https://www.npmjs.com/package/earcut). It has the same
58
+ `positions`/`indices` shape the `geo:mesh` node outputs.
59
+ - `toSceneRibbons(input, origin, { width, widthProperty, elevation })`: each
60
+ line as a flat, upward-facing strip around its centerline, for streets.
61
+ The width is in metres: `properties[widthProperty]` when it is a positive
62
+ number, else `width` (default 1). Corners are mitered up to twice the half
63
+ width and beveled past that, or when a short segment would make the strip
64
+ fold. A line that ends where it starts joins up.
51
65
  - `simplifyLine`, `simplifyRing`, `countPositions`, `bbox`, `eachPosition`.
66
+
67
+ ### Drape points, curves, and ribbons
68
+
69
+ Pass `{ drape: { heightfield, spacing, offset, onMissing } }` to
70
+ `toScenePoints`, `toSceneCurves`, or `toSceneRibbons`. Build
71
+ `heightfield` with `new Heightfield(grid)` from `@vim3d/core`; the grid
72
+ must already be in the projected scene frame used by the GeoJSON
73
+ conversion. Draping replaces projected Y (including input altitudes)
74
+ with the sampled Y plus `offset` (default 0). `spacing` is the maximum
75
+ horizontal step in metres and defaults to the smaller grid spacing.
76
+ It inserts additional samples at terrain triangle boundaries and keeps
77
+ the feature's properties and ID. Missing terrain throws by default; use
78
+ `onMissing: 'omit'` to omit the affected point, curve or ribbon without
79
+ bridging holes. Only ribbon edges conform; `toSceneFills` does not drape.
package/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export const name = '@vim3d/geojson';
2
2
  export { loadGeoJSON } from './fetch.js';
3
3
  export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
4
- export { toSceneCurves } from './scene.js';
4
+ export { toSceneCurves, toSceneFills, toScenePoints, toSceneRibbons } from './scene.js';
5
5
  export { simplifyLine, simplifyRing } from './simplify.js';
6
6
  export { borders } from './topology.js';
package/esm/scene.js CHANGED
@@ -1,25 +1,45 @@
1
- import { geodeticToScene } from '@vim3d/core';
2
- function lines(geometry, emit) {
1
+ import { drapeFeature, geodeticToScene, ribbonMesh } from '@vim3d/core';
2
+ import earcut from 'earcut';
3
+ function eachFeature(input, emit) {
4
+ if (input.type === 'FeatureCollection')
5
+ input.features.forEach(feature => emit(feature.geometry, feature.properties, feature.id));
6
+ else if (input.type === 'Feature')
7
+ emit(input.geometry, input.properties, input.id);
8
+ else
9
+ emit(input, null);
10
+ }
11
+ function eachGeometry(geometry, visit) {
3
12
  if (!geometry)
4
13
  return;
5
- switch (geometry.type) {
6
- case 'LineString':
7
- emit(geometry.coordinates, false);
8
- break;
9
- case 'MultiLineString':
10
- geometry.coordinates.forEach(line => emit(line, false));
11
- break;
12
- case 'Polygon':
13
- geometry.coordinates.forEach(ring => emit(ring, true));
14
- break;
15
- case 'MultiPolygon':
16
- geometry.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
17
- break;
18
- case 'GeometryCollection':
19
- geometry.geometries.forEach(child => lines(child, emit));
20
- break;
21
- default: break;
22
- }
14
+ if (geometry.type === 'GeometryCollection')
15
+ geometry.geometries.forEach(child => eachGeometry(child, visit));
16
+ else
17
+ visit(geometry);
18
+ }
19
+ const finite = ([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat);
20
+ function projector(origin, options) {
21
+ const elevation = options.elevation ?? origin.elevation;
22
+ return (positions) => positions.filter(finite)
23
+ .map(([lon, lat, height]) => geodeticToScene(origin, lat, lon, Number.isFinite(height) ? height : elevation));
24
+ }
25
+ function lines(geometry, emit) {
26
+ eachGeometry(geometry, child => {
27
+ switch (child.type) {
28
+ case 'LineString':
29
+ emit(child.coordinates, false);
30
+ break;
31
+ case 'MultiLineString':
32
+ child.coordinates.forEach(line => emit(line, false));
33
+ break;
34
+ case 'Polygon':
35
+ child.coordinates.forEach(ring => emit(ring, true));
36
+ break;
37
+ case 'MultiPolygon':
38
+ child.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
39
+ break;
40
+ default: break;
41
+ }
42
+ });
23
43
  }
24
44
  /**
25
45
  * Turns every line and polygon ring into a scene-space curve around `origin`,
@@ -28,22 +48,157 @@ function lines(geometry, emit) {
28
48
  */
29
49
  export function toSceneCurves(input, origin, options = {}) {
30
50
  const curves = [];
31
- const elevation = options.elevation ?? origin.elevation;
32
- const add = (geometry, properties, id) => lines(geometry, (line, closed) => {
33
- const vertices = line.filter(([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat))
34
- .map(([lon, lat, height]) => geodeticToScene(origin, lat, lon, Number.isFinite(height) ? height : elevation));
51
+ const project = projector(origin, options);
52
+ eachFeature(input, (geometry, properties, id) => lines(geometry, (line, closed) => {
53
+ const vertices = project(line);
35
54
  if (vertices.length < 2)
36
55
  return;
37
56
  const [first, last] = [vertices[0], vertices[vertices.length - 1]];
38
57
  if (closed && first.some((v, i) => v !== last[i]))
39
58
  vertices.push(first);
40
- curves.push({ geometry: { primitive: 'curve', args: [], vertices }, properties, id, closed });
41
- });
42
- if (input.type === 'FeatureCollection')
43
- input.features.forEach(feature => add(feature.geometry, feature.properties, feature.id));
44
- else if (input.type === 'Feature')
45
- add(input.geometry, input.properties, input.id);
46
- else
47
- add(input, null);
59
+ const { drape } = options;
60
+ const sampled = drape ? drapeFeature(drape.heightfield, { kind: 'curve', vertices }, drape)?.vertices : vertices;
61
+ if (sampled)
62
+ curves.push({ geometry: { primitive: 'curve', args: [], vertices: sampled }, properties, id, closed });
63
+ }));
48
64
  return curves;
49
65
  }
66
+ /**
67
+ * Every `Point` and `MultiPoint` position as a scene-space point with its
68
+ * feature's `properties` and `id`, for labels, trees and other markers.
69
+ */
70
+ export function toScenePoints(input, origin, options = {}) {
71
+ const points = [];
72
+ const project = projector(origin, options);
73
+ eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
74
+ const positions = child.type === 'Point' ? [child.coordinates] : child.type === 'MultiPoint' ? child.coordinates : [];
75
+ for (const position of project(positions)) {
76
+ const { drape } = options;
77
+ const sampled = drape ? drapeFeature(drape.heightfield, { kind: 'point', position }, drape)?.position : position;
78
+ if (sampled)
79
+ points.push({ position: sampled, properties, id });
80
+ }
81
+ }));
82
+ return points;
83
+ }
84
+ function mesh(positions, indices) {
85
+ for (let i = 0; i < indices.length; i += 3) {
86
+ const [a, b, c] = [indices[i] * 3, indices[i + 1] * 3, indices[i + 2] * 3];
87
+ const up = (positions[b + 2] - positions[a + 2]) * (positions[c] - positions[a])
88
+ - (positions[b] - positions[a]) * (positions[c + 2] - positions[a + 2]);
89
+ if (up < 0)
90
+ [indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
91
+ }
92
+ return { primitive: 'mesh', args: [], positions, indices };
93
+ }
94
+ function openRing(ring) {
95
+ const [first, last] = [ring[0], ring[ring.length - 1]];
96
+ return ring.length > 1 && first[0] === last[0] && first[2] === last[2] ? ring.slice(0, -1) : ring;
97
+ }
98
+ /**
99
+ * Every polygon (each part of a `MultiPolygon` on its own) as a flat,
100
+ * upward-facing triangle mesh with its holes cut out.
101
+ */
102
+ export function toSceneFills(input, origin, options = {}) {
103
+ const meshes = [];
104
+ const project = projector(origin, options);
105
+ const fill = (rings, properties, id) => {
106
+ const [outer, ...holes] = rings.map(ring => openRing(project(ring)));
107
+ if (!outer || outer.length < 3)
108
+ return;
109
+ const kept = [outer, ...holes.filter(hole => hole.length >= 3)];
110
+ const positions = [];
111
+ const flat = [];
112
+ const starts = [];
113
+ for (const ring of kept) {
114
+ if (ring !== outer)
115
+ starts.push(positions.length / 3);
116
+ for (const [x, y, z] of ring) {
117
+ positions.push(x, y, z);
118
+ flat.push(x, z);
119
+ }
120
+ }
121
+ const indices = earcut(flat, starts.length ? starts : undefined, 2);
122
+ if (indices.length)
123
+ meshes.push({ geometry: mesh(positions, indices), properties, id });
124
+ };
125
+ eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
126
+ if (child.type === 'Polygon')
127
+ fill(child.coordinates, properties, id);
128
+ else if (child.type === 'MultiPolygon')
129
+ child.coordinates.forEach(polygon => fill(polygon, properties, id));
130
+ }));
131
+ return meshes;
132
+ }
133
+ const MITER_LIMIT = 2;
134
+ function ribbon(line, width) {
135
+ let points = line.filter((p, i) => i === 0 || p[0] !== line[i - 1][0] || p[2] !== line[i - 1][2]);
136
+ const [first, last] = [points[0], points[points.length - 1]];
137
+ const closed = points.length > 3 && first[0] === last[0] && first[2] === last[2];
138
+ if (closed)
139
+ points = points.slice(0, -1);
140
+ if (points.length < 2)
141
+ return null;
142
+ const count = points.length;
143
+ const half = width / 2;
144
+ const segments = closed ? count : count - 1;
145
+ const normals = [];
146
+ const lengths = [];
147
+ for (let i = 0; i < segments; i++) {
148
+ const [p, q] = [points[i], points[(i + 1) % count]];
149
+ const [dx, dz] = [q[0] - p[0], q[2] - p[2]];
150
+ const length = Math.hypot(dx, dz);
151
+ normals.push([-dz / length, dx / length]);
152
+ lengths.push(length);
153
+ }
154
+ const sections = [];
155
+ const section = ([x, y, z], [nx, nz], scale) => sections.push([[x + nx * scale, y, z + nz * scale], [x - nx * scale, y, z - nz * scale]]);
156
+ points.forEach((point, i) => {
157
+ const before = closed ? (i + count - 1) % count : i - 1;
158
+ const after = i < segments ? i : -1;
159
+ if (before < 0 || after < 0)
160
+ return section(point, normals[Math.max(before, after)], half);
161
+ const [a, b] = [normals[before], normals[after]];
162
+ const [mx, mz] = [a[0] + b[0], a[1] + b[1]];
163
+ const length = Math.hypot(mx, mz);
164
+ const cos = length / 2;
165
+ const reach = cos > 0 ? half * Math.sqrt(Math.max(0, 1 - cos * cos)) / cos : Infinity;
166
+ if (cos >= 1 / MITER_LIMIT && reach <= Math.min(lengths[before], lengths[after])) {
167
+ section(point, [mx / length, mz / length], half / cos);
168
+ }
169
+ else {
170
+ section(point, a, half);
171
+ section(point, b, half);
172
+ }
173
+ });
174
+ if (closed)
175
+ sections.push(sections[0]);
176
+ return sections;
177
+ }
178
+ /**
179
+ * Every line (not polygon rings) as a flat, upward-facing strip of the given
180
+ * width around its centerline, for streets and paths. Corners are mitered;
181
+ * corners sharper than the miter limit, or whose miter would reach past the
182
+ * next vertex, are beveled. A line that ends where it starts joins up.
183
+ */
184
+ export function toSceneRibbons(input, origin, options = {}) {
185
+ const meshes = [];
186
+ const project = projector(origin, options);
187
+ eachFeature(input, (geometry, properties, id) => {
188
+ const value = options.widthProperty ? properties?.[options.widthProperty] : undefined;
189
+ const width = typeof value === 'number' && value > 0 && Number.isFinite(value) ? value : options.width ?? 1;
190
+ eachGeometry(geometry, child => {
191
+ const parts = child.type === 'LineString' ? [child.coordinates] : child.type === 'MultiLineString' ? child.coordinates : [];
192
+ for (const part of parts) {
193
+ const sections = ribbon(project(part), width);
194
+ if (!sections)
195
+ continue;
196
+ const { drape } = options;
197
+ const sampled = drape ? drapeFeature(drape.heightfield, { kind: 'ribbon', sections }, drape)?.sections : sections;
198
+ if (sampled)
199
+ meshes.push({ geometry: ribbonMesh(sampled), properties, id });
200
+ }
201
+ });
202
+ });
203
+ return meshes;
204
+ }
package/index.d.ts CHANGED
@@ -3,8 +3,8 @@ export type { LoadGeoJSONOptions } from './fetch.js';
3
3
  export { loadGeoJSON } from './fetch.js';
4
4
  export type { ResampleOptions } from './resample.js';
5
5
  export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
6
- export type { SceneCurve, SceneCurveOptions } from './scene.js';
7
- export { toSceneCurves } from './scene.js';
6
+ export type { MeshGeometry, SceneCurve, SceneCurveOptions, SceneMesh, ScenePoint, SceneRibbonOptions } from './scene.js';
7
+ export { toSceneCurves, toSceneFills, toScenePoints, toSceneRibbons } from './scene.js';
8
8
  export { simplifyLine, simplifyRing } from './simplify.js';
9
9
  export { borders } from './topology.js';
10
10
  export type * from './types.js';
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.borders = exports.simplifyRing = exports.simplifyLine = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
3
+ exports.borders = exports.simplifyRing = exports.simplifyLine = exports.toSceneRibbons = exports.toScenePoints = exports.toSceneFills = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
4
4
  exports.name = '@vim3d/geojson';
5
5
  var fetch_js_1 = require("./fetch.js");
6
6
  Object.defineProperty(exports, "loadGeoJSON", { enumerable: true, get: function () { return fetch_js_1.loadGeoJSON; } });
@@ -11,6 +11,9 @@ Object.defineProperty(exports, "eachPosition", { enumerable: true, get: function
11
11
  Object.defineProperty(exports, "resampleGeoJSON", { enumerable: true, get: function () { return resample_js_1.resampleGeoJSON; } });
12
12
  var scene_js_1 = require("./scene.js");
13
13
  Object.defineProperty(exports, "toSceneCurves", { enumerable: true, get: function () { return scene_js_1.toSceneCurves; } });
14
+ Object.defineProperty(exports, "toSceneFills", { enumerable: true, get: function () { return scene_js_1.toSceneFills; } });
15
+ Object.defineProperty(exports, "toScenePoints", { enumerable: true, get: function () { return scene_js_1.toScenePoints; } });
16
+ Object.defineProperty(exports, "toSceneRibbons", { enumerable: true, get: function () { return scene_js_1.toSceneRibbons; } });
14
17
  var simplify_js_1 = require("./simplify.js");
15
18
  Object.defineProperty(exports, "simplifyLine", { enumerable: true, get: function () { return simplify_js_1.simplifyLine; } });
16
19
  Object.defineProperty(exports, "simplifyRing", { enumerable: true, get: function () { return simplify_js_1.simplifyRing; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vim3d/geojson",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Fetch GeoJSON at load time and resample it for Vim3D scenes.",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/geojson",
@@ -22,9 +22,11 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@constructive-io/fetch": "^1.2.2",
25
- "@vim3d/core": "^0.6.0"
25
+ "earcut": "^2.2.4",
26
+ "@vim3d/core": "^0.10.0"
26
27
  },
27
28
  "devDependencies": {
29
+ "@types/earcut": "^2.1.4",
28
30
  "makage": "^0.8.0"
29
31
  },
30
32
  "scripts": {
package/scene.d.ts CHANGED
@@ -1,19 +1,68 @@
1
- import { type GeometrySnapshot, type GeoOrigin } from '@vim3d/core';
1
+ import { type DrapeOptions, type GeometrySnapshot, type GeoOrigin, type Heightfield, type Vec3 } from '@vim3d/core';
2
2
  import type { GeoJSON, Properties } from './types.js';
3
3
  export interface SceneCurve {
4
- /** A curve geometry @vim3d/threejs `createGeometry` draws as a line. */
4
+ /** A curve geometry @vim3d/threejs draws as a line. */
5
5
  readonly geometry: GeometrySnapshot;
6
6
  readonly properties: Properties;
7
7
  readonly id?: string | number;
8
8
  readonly closed: boolean;
9
9
  }
10
- export interface SceneCurveOptions {
10
+ interface ProjectionOptions {
11
11
  /** Elevation in metres for positions without one; defaults to the origin's. */
12
12
  elevation?: number;
13
13
  }
14
+ export interface SceneCurveOptions extends ProjectionOptions {
15
+ /** Drape projected XZ positions; replaces their Y with sampled terrain plus offset. */
16
+ drape?: DrapeOptions & {
17
+ readonly heightfield: Heightfield;
18
+ };
19
+ }
20
+ export interface ScenePoint {
21
+ readonly position: Vec3;
22
+ readonly properties: Properties;
23
+ readonly id?: string | number;
24
+ }
25
+ export interface MeshGeometry {
26
+ readonly primitive: 'mesh';
27
+ readonly args: readonly number[];
28
+ /** Flat x, y, z vertices. */
29
+ readonly positions: number[];
30
+ /** Vertex triples, wound counter-clockwise seen from above (+y). */
31
+ readonly indices: number[];
32
+ }
33
+ export interface SceneMesh {
34
+ /** A `mesh` geometry, the same shape the `geo:mesh` node outputs. */
35
+ readonly geometry: MeshGeometry;
36
+ readonly properties: Properties;
37
+ readonly id?: string | number;
38
+ }
39
+ export interface SceneRibbonOptions extends SceneCurveOptions {
40
+ /** Ribbon width in metres; defaults to 1. */
41
+ width?: number;
42
+ /** Feature property holding a width in metres, used when it is a positive number. */
43
+ widthProperty?: string;
44
+ }
14
45
  /**
15
46
  * Turns every line and polygon ring into a scene-space curve around `origin`,
16
47
  * using the same local frame as Vim3D `world` (x east, y up, -z north).
17
48
  * Positions without a finite longitude and latitude are skipped.
18
49
  */
19
50
  export declare function toSceneCurves(input: GeoJSON, origin: GeoOrigin, options?: SceneCurveOptions): SceneCurve[];
51
+ /**
52
+ * Every `Point` and `MultiPoint` position as a scene-space point with its
53
+ * feature's `properties` and `id`, for labels, trees and other markers.
54
+ */
55
+ export declare function toScenePoints(input: GeoJSON, origin: GeoOrigin, options?: SceneCurveOptions): ScenePoint[];
56
+ /**
57
+ * Every polygon (each part of a `MultiPolygon` on its own) as a flat,
58
+ * upward-facing triangle mesh with its holes cut out.
59
+ */
60
+ export declare function toSceneFills(input: GeoJSON, origin: GeoOrigin, options?: ProjectionOptions): SceneMesh[];
61
+ /**
62
+ * Every line (not polygon rings) as a flat, upward-facing strip of the given
63
+ * width around its centerline, for streets and paths. Corners are mitered;
64
+ * corners sharper than the miter limit, or whose miter would reach past the
65
+ * next vertex, are beveled. A line that ends where it starts joins up.
66
+ */
67
+ export declare function toSceneRibbons(input: GeoJSON, origin: GeoOrigin, options?: SceneRibbonOptions): SceneMesh[];
68
+ export {};
package/scene.js CHANGED
@@ -1,28 +1,54 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.toSceneCurves = toSceneCurves;
7
+ exports.toScenePoints = toScenePoints;
8
+ exports.toSceneFills = toSceneFills;
9
+ exports.toSceneRibbons = toSceneRibbons;
4
10
  const core_1 = require("@vim3d/core");
5
- function lines(geometry, emit) {
11
+ const earcut_1 = __importDefault(require("earcut"));
12
+ function eachFeature(input, emit) {
13
+ if (input.type === 'FeatureCollection')
14
+ input.features.forEach(feature => emit(feature.geometry, feature.properties, feature.id));
15
+ else if (input.type === 'Feature')
16
+ emit(input.geometry, input.properties, input.id);
17
+ else
18
+ emit(input, null);
19
+ }
20
+ function eachGeometry(geometry, visit) {
6
21
  if (!geometry)
7
22
  return;
8
- switch (geometry.type) {
9
- case 'LineString':
10
- emit(geometry.coordinates, false);
11
- break;
12
- case 'MultiLineString':
13
- geometry.coordinates.forEach(line => emit(line, false));
14
- break;
15
- case 'Polygon':
16
- geometry.coordinates.forEach(ring => emit(ring, true));
17
- break;
18
- case 'MultiPolygon':
19
- geometry.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
20
- break;
21
- case 'GeometryCollection':
22
- geometry.geometries.forEach(child => lines(child, emit));
23
- break;
24
- default: break;
25
- }
23
+ if (geometry.type === 'GeometryCollection')
24
+ geometry.geometries.forEach(child => eachGeometry(child, visit));
25
+ else
26
+ visit(geometry);
27
+ }
28
+ const finite = ([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat);
29
+ function projector(origin, options) {
30
+ const elevation = options.elevation ?? origin.elevation;
31
+ return (positions) => positions.filter(finite)
32
+ .map(([lon, lat, height]) => (0, core_1.geodeticToScene)(origin, lat, lon, Number.isFinite(height) ? height : elevation));
33
+ }
34
+ function lines(geometry, emit) {
35
+ eachGeometry(geometry, child => {
36
+ switch (child.type) {
37
+ case 'LineString':
38
+ emit(child.coordinates, false);
39
+ break;
40
+ case 'MultiLineString':
41
+ child.coordinates.forEach(line => emit(line, false));
42
+ break;
43
+ case 'Polygon':
44
+ child.coordinates.forEach(ring => emit(ring, true));
45
+ break;
46
+ case 'MultiPolygon':
47
+ child.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
48
+ break;
49
+ default: break;
50
+ }
51
+ });
26
52
  }
27
53
  /**
28
54
  * Turns every line and polygon ring into a scene-space curve around `origin`,
@@ -31,22 +57,157 @@ function lines(geometry, emit) {
31
57
  */
32
58
  function toSceneCurves(input, origin, options = {}) {
33
59
  const curves = [];
34
- const elevation = options.elevation ?? origin.elevation;
35
- const add = (geometry, properties, id) => lines(geometry, (line, closed) => {
36
- const vertices = line.filter(([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat))
37
- .map(([lon, lat, height]) => (0, core_1.geodeticToScene)(origin, lat, lon, Number.isFinite(height) ? height : elevation));
60
+ const project = projector(origin, options);
61
+ eachFeature(input, (geometry, properties, id) => lines(geometry, (line, closed) => {
62
+ const vertices = project(line);
38
63
  if (vertices.length < 2)
39
64
  return;
40
65
  const [first, last] = [vertices[0], vertices[vertices.length - 1]];
41
66
  if (closed && first.some((v, i) => v !== last[i]))
42
67
  vertices.push(first);
43
- curves.push({ geometry: { primitive: 'curve', args: [], vertices }, properties, id, closed });
44
- });
45
- if (input.type === 'FeatureCollection')
46
- input.features.forEach(feature => add(feature.geometry, feature.properties, feature.id));
47
- else if (input.type === 'Feature')
48
- add(input.geometry, input.properties, input.id);
49
- else
50
- add(input, null);
68
+ const { drape } = options;
69
+ const sampled = drape ? (0, core_1.drapeFeature)(drape.heightfield, { kind: 'curve', vertices }, drape)?.vertices : vertices;
70
+ if (sampled)
71
+ curves.push({ geometry: { primitive: 'curve', args: [], vertices: sampled }, properties, id, closed });
72
+ }));
51
73
  return curves;
52
74
  }
75
+ /**
76
+ * Every `Point` and `MultiPoint` position as a scene-space point with its
77
+ * feature's `properties` and `id`, for labels, trees and other markers.
78
+ */
79
+ function toScenePoints(input, origin, options = {}) {
80
+ const points = [];
81
+ const project = projector(origin, options);
82
+ eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
83
+ const positions = child.type === 'Point' ? [child.coordinates] : child.type === 'MultiPoint' ? child.coordinates : [];
84
+ for (const position of project(positions)) {
85
+ const { drape } = options;
86
+ const sampled = drape ? (0, core_1.drapeFeature)(drape.heightfield, { kind: 'point', position }, drape)?.position : position;
87
+ if (sampled)
88
+ points.push({ position: sampled, properties, id });
89
+ }
90
+ }));
91
+ return points;
92
+ }
93
+ function mesh(positions, indices) {
94
+ for (let i = 0; i < indices.length; i += 3) {
95
+ const [a, b, c] = [indices[i] * 3, indices[i + 1] * 3, indices[i + 2] * 3];
96
+ const up = (positions[b + 2] - positions[a + 2]) * (positions[c] - positions[a])
97
+ - (positions[b] - positions[a]) * (positions[c + 2] - positions[a + 2]);
98
+ if (up < 0)
99
+ [indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
100
+ }
101
+ return { primitive: 'mesh', args: [], positions, indices };
102
+ }
103
+ function openRing(ring) {
104
+ const [first, last] = [ring[0], ring[ring.length - 1]];
105
+ return ring.length > 1 && first[0] === last[0] && first[2] === last[2] ? ring.slice(0, -1) : ring;
106
+ }
107
+ /**
108
+ * Every polygon (each part of a `MultiPolygon` on its own) as a flat,
109
+ * upward-facing triangle mesh with its holes cut out.
110
+ */
111
+ function toSceneFills(input, origin, options = {}) {
112
+ const meshes = [];
113
+ const project = projector(origin, options);
114
+ const fill = (rings, properties, id) => {
115
+ const [outer, ...holes] = rings.map(ring => openRing(project(ring)));
116
+ if (!outer || outer.length < 3)
117
+ return;
118
+ const kept = [outer, ...holes.filter(hole => hole.length >= 3)];
119
+ const positions = [];
120
+ const flat = [];
121
+ const starts = [];
122
+ for (const ring of kept) {
123
+ if (ring !== outer)
124
+ starts.push(positions.length / 3);
125
+ for (const [x, y, z] of ring) {
126
+ positions.push(x, y, z);
127
+ flat.push(x, z);
128
+ }
129
+ }
130
+ const indices = (0, earcut_1.default)(flat, starts.length ? starts : undefined, 2);
131
+ if (indices.length)
132
+ meshes.push({ geometry: mesh(positions, indices), properties, id });
133
+ };
134
+ eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
135
+ if (child.type === 'Polygon')
136
+ fill(child.coordinates, properties, id);
137
+ else if (child.type === 'MultiPolygon')
138
+ child.coordinates.forEach(polygon => fill(polygon, properties, id));
139
+ }));
140
+ return meshes;
141
+ }
142
+ const MITER_LIMIT = 2;
143
+ function ribbon(line, width) {
144
+ let points = line.filter((p, i) => i === 0 || p[0] !== line[i - 1][0] || p[2] !== line[i - 1][2]);
145
+ const [first, last] = [points[0], points[points.length - 1]];
146
+ const closed = points.length > 3 && first[0] === last[0] && first[2] === last[2];
147
+ if (closed)
148
+ points = points.slice(0, -1);
149
+ if (points.length < 2)
150
+ return null;
151
+ const count = points.length;
152
+ const half = width / 2;
153
+ const segments = closed ? count : count - 1;
154
+ const normals = [];
155
+ const lengths = [];
156
+ for (let i = 0; i < segments; i++) {
157
+ const [p, q] = [points[i], points[(i + 1) % count]];
158
+ const [dx, dz] = [q[0] - p[0], q[2] - p[2]];
159
+ const length = Math.hypot(dx, dz);
160
+ normals.push([-dz / length, dx / length]);
161
+ lengths.push(length);
162
+ }
163
+ const sections = [];
164
+ const section = ([x, y, z], [nx, nz], scale) => sections.push([[x + nx * scale, y, z + nz * scale], [x - nx * scale, y, z - nz * scale]]);
165
+ points.forEach((point, i) => {
166
+ const before = closed ? (i + count - 1) % count : i - 1;
167
+ const after = i < segments ? i : -1;
168
+ if (before < 0 || after < 0)
169
+ return section(point, normals[Math.max(before, after)], half);
170
+ const [a, b] = [normals[before], normals[after]];
171
+ const [mx, mz] = [a[0] + b[0], a[1] + b[1]];
172
+ const length = Math.hypot(mx, mz);
173
+ const cos = length / 2;
174
+ const reach = cos > 0 ? half * Math.sqrt(Math.max(0, 1 - cos * cos)) / cos : Infinity;
175
+ if (cos >= 1 / MITER_LIMIT && reach <= Math.min(lengths[before], lengths[after])) {
176
+ section(point, [mx / length, mz / length], half / cos);
177
+ }
178
+ else {
179
+ section(point, a, half);
180
+ section(point, b, half);
181
+ }
182
+ });
183
+ if (closed)
184
+ sections.push(sections[0]);
185
+ return sections;
186
+ }
187
+ /**
188
+ * Every line (not polygon rings) as a flat, upward-facing strip of the given
189
+ * width around its centerline, for streets and paths. Corners are mitered;
190
+ * corners sharper than the miter limit, or whose miter would reach past the
191
+ * next vertex, are beveled. A line that ends where it starts joins up.
192
+ */
193
+ function toSceneRibbons(input, origin, options = {}) {
194
+ const meshes = [];
195
+ const project = projector(origin, options);
196
+ eachFeature(input, (geometry, properties, id) => {
197
+ const value = options.widthProperty ? properties?.[options.widthProperty] : undefined;
198
+ const width = typeof value === 'number' && value > 0 && Number.isFinite(value) ? value : options.width ?? 1;
199
+ eachGeometry(geometry, child => {
200
+ const parts = child.type === 'LineString' ? [child.coordinates] : child.type === 'MultiLineString' ? child.coordinates : [];
201
+ for (const part of parts) {
202
+ const sections = ribbon(project(part), width);
203
+ if (!sections)
204
+ continue;
205
+ const { drape } = options;
206
+ const sampled = drape ? (0, core_1.drapeFeature)(drape.heightfield, { kind: 'ribbon', sections }, drape)?.sections : sections;
207
+ if (sampled)
208
+ meshes.push({ geometry: (0, core_1.ribbonMesh)(sampled), properties, id });
209
+ }
210
+ });
211
+ });
212
+ return meshes;
213
+ }