@loaders.gl/gis 3.4.11 → 3.4.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/package.json +4 -4
- package/dist/bundle.js +0 -5
- package/dist/index.js +0 -18
- package/dist/lib/binary-to-geojson.js +0 -233
- package/dist/lib/extract-geometry-info.js +0 -96
- package/dist/lib/flat-geojson-to-binary-types.js +0 -2
- package/dist/lib/flat-geojson-to-binary.js +0 -385
- package/dist/lib/geojson-to-binary.js +0 -25
- package/dist/lib/geojson-to-flat-geojson.js +0 -128
- package/dist/lib/transform.js +0 -59
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loaders.gl/gis",
|
|
3
3
|
"description": "Helpers for GIS category data",
|
|
4
|
-
"version": "3.4.
|
|
4
|
+
"version": "3.4.12",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@loaders.gl/loader-utils": "3.4.
|
|
28
|
-
"@loaders.gl/schema": "3.4.
|
|
27
|
+
"@loaders.gl/loader-utils": "3.4.12",
|
|
28
|
+
"@loaders.gl/schema": "3.4.12",
|
|
29
29
|
"@mapbox/vector-tile": "^1.3.1",
|
|
30
30
|
"@math.gl/polygon": "^3.5.1",
|
|
31
31
|
"pbf": "^3.2.1"
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@math.gl/proj4": "^3.5.1"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "85254762ae3082f608a863291deef09f2b40c6cf"
|
|
37
37
|
}
|
package/dist/bundle.js
DELETED
package/dist/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Types from `@loaders.gl/schema`
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.transformGeoJsonCoords = exports.transformBinaryCoords = exports.binaryToGeometry = exports.binaryToGeoJson = exports.binaryToGeojson = exports.geojsonToFlatGeojson = exports.geojsonToBinary = exports.flatGeojsonToBinary = void 0;
|
|
5
|
-
// Functions
|
|
6
|
-
var flat_geojson_to_binary_1 = require("./lib/flat-geojson-to-binary");
|
|
7
|
-
Object.defineProperty(exports, "flatGeojsonToBinary", { enumerable: true, get: function () { return flat_geojson_to_binary_1.flatGeojsonToBinary; } });
|
|
8
|
-
var geojson_to_binary_1 = require("./lib/geojson-to-binary");
|
|
9
|
-
Object.defineProperty(exports, "geojsonToBinary", { enumerable: true, get: function () { return geojson_to_binary_1.geojsonToBinary; } });
|
|
10
|
-
var geojson_to_flat_geojson_1 = require("./lib/geojson-to-flat-geojson");
|
|
11
|
-
Object.defineProperty(exports, "geojsonToFlatGeojson", { enumerable: true, get: function () { return geojson_to_flat_geojson_1.geojsonToFlatGeojson; } });
|
|
12
|
-
var binary_to_geojson_1 = require("./lib/binary-to-geojson");
|
|
13
|
-
Object.defineProperty(exports, "binaryToGeojson", { enumerable: true, get: function () { return binary_to_geojson_1.binaryToGeojson; } });
|
|
14
|
-
Object.defineProperty(exports, "binaryToGeoJson", { enumerable: true, get: function () { return binary_to_geojson_1.binaryToGeoJson; } });
|
|
15
|
-
Object.defineProperty(exports, "binaryToGeometry", { enumerable: true, get: function () { return binary_to_geojson_1.binaryToGeometry; } });
|
|
16
|
-
var transform_1 = require("./lib/transform");
|
|
17
|
-
Object.defineProperty(exports, "transformBinaryCoords", { enumerable: true, get: function () { return transform_1.transformBinaryCoords; } });
|
|
18
|
-
Object.defineProperty(exports, "transformGeoJsonCoords", { enumerable: true, get: function () { return transform_1.transformGeoJsonCoords; } });
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.binaryToGeometry = exports.binaryToGeoJson = exports.binaryToGeojson = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Convert binary geometry representation to GeoJSON
|
|
6
|
-
* @param data geometry data in binary representation
|
|
7
|
-
* @param options
|
|
8
|
-
* @param options.type Input data type: Point, LineString, or Polygon
|
|
9
|
-
* @param options.featureId Global feature id. If specified, only a single feature is extracted
|
|
10
|
-
* @return GeoJSON objects
|
|
11
|
-
*/
|
|
12
|
-
function binaryToGeojson(data, options) {
|
|
13
|
-
const globalFeatureId = options?.globalFeatureId;
|
|
14
|
-
if (globalFeatureId !== undefined) {
|
|
15
|
-
return getSingleFeature(data, globalFeatureId);
|
|
16
|
-
}
|
|
17
|
-
return parseFeatures(data, options?.type);
|
|
18
|
-
}
|
|
19
|
-
exports.binaryToGeojson = binaryToGeojson;
|
|
20
|
-
/** @deprecated use `binaryToGeojson` or `binaryToGeometry` instead */
|
|
21
|
-
function binaryToGeoJson(data, type, format = 'feature') {
|
|
22
|
-
switch (format) {
|
|
23
|
-
case 'feature':
|
|
24
|
-
return parseFeatures(data, type);
|
|
25
|
-
case 'geometry':
|
|
26
|
-
return binaryToGeometry(data);
|
|
27
|
-
default:
|
|
28
|
-
throw new Error(format);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
exports.binaryToGeoJson = binaryToGeoJson;
|
|
32
|
-
/**
|
|
33
|
-
* Return a single feature from a binary geometry representation as GeoJSON
|
|
34
|
-
* @param data geometry data in binary representation
|
|
35
|
-
* @return GeoJSON feature
|
|
36
|
-
*/
|
|
37
|
-
function getSingleFeature(data, globalFeatureId) {
|
|
38
|
-
const dataArray = normalizeInput(data);
|
|
39
|
-
for (const data of dataArray) {
|
|
40
|
-
let lastIndex = 0;
|
|
41
|
-
let lastValue = data.featureIds.value[0];
|
|
42
|
-
// Scan through data until we find matching feature
|
|
43
|
-
for (let i = 0; i < data.featureIds.value.length; i++) {
|
|
44
|
-
const currValue = data.featureIds.value[i];
|
|
45
|
-
if (currValue === lastValue) {
|
|
46
|
-
// eslint-disable-next-line no-continue
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
|
|
50
|
-
return parseFeature(data, lastIndex, i);
|
|
51
|
-
}
|
|
52
|
-
lastIndex = i;
|
|
53
|
-
lastValue = currValue;
|
|
54
|
-
}
|
|
55
|
-
if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
|
|
56
|
-
return parseFeature(data, lastIndex, data.featureIds.value.length);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
throw new Error(`featureId:${globalFeatureId} not found`);
|
|
60
|
-
}
|
|
61
|
-
function parseFeatures(data, type) {
|
|
62
|
-
const dataArray = normalizeInput(data, type);
|
|
63
|
-
return parseFeatureCollection(dataArray);
|
|
64
|
-
}
|
|
65
|
-
/** Parse input binary data and return a valid GeoJSON geometry object */
|
|
66
|
-
function binaryToGeometry(data, startIndex, endIndex) {
|
|
67
|
-
switch (data.type) {
|
|
68
|
-
case 'Point':
|
|
69
|
-
return pointToGeoJson(data, startIndex, endIndex);
|
|
70
|
-
case 'LineString':
|
|
71
|
-
return lineStringToGeoJson(data, startIndex, endIndex);
|
|
72
|
-
case 'Polygon':
|
|
73
|
-
return polygonToGeoJson(data, startIndex, endIndex);
|
|
74
|
-
default:
|
|
75
|
-
const unexpectedInput = data;
|
|
76
|
-
throw new Error(`Unsupported geometry type: ${unexpectedInput?.type}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
exports.binaryToGeometry = binaryToGeometry;
|
|
80
|
-
// Normalize features
|
|
81
|
-
// Return an array of data objects, each of which have a type key
|
|
82
|
-
function normalizeInput(data, type) {
|
|
83
|
-
const isHeterogeneousType = Boolean(data.points || data.lines || data.polygons);
|
|
84
|
-
if (!isHeterogeneousType) {
|
|
85
|
-
// @ts-expect-error This is a legacy check which allowed `data` to be an instance of the values
|
|
86
|
-
// here. Aka the new data.points, data.lines, or data.polygons.
|
|
87
|
-
data.type = type || parseType(data);
|
|
88
|
-
return [data];
|
|
89
|
-
}
|
|
90
|
-
const features = [];
|
|
91
|
-
if (data.points) {
|
|
92
|
-
data.points.type = 'Point';
|
|
93
|
-
features.push(data.points);
|
|
94
|
-
}
|
|
95
|
-
if (data.lines) {
|
|
96
|
-
data.lines.type = 'LineString';
|
|
97
|
-
features.push(data.lines);
|
|
98
|
-
}
|
|
99
|
-
if (data.polygons) {
|
|
100
|
-
data.polygons.type = 'Polygon';
|
|
101
|
-
features.push(data.polygons);
|
|
102
|
-
}
|
|
103
|
-
return features;
|
|
104
|
-
}
|
|
105
|
-
/** Parse input binary data and return an array of GeoJSON Features */
|
|
106
|
-
function parseFeatureCollection(dataArray) {
|
|
107
|
-
const features = [];
|
|
108
|
-
for (const data of dataArray) {
|
|
109
|
-
if (data.featureIds.value.length === 0) {
|
|
110
|
-
// eslint-disable-next-line no-continue
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
let lastIndex = 0;
|
|
114
|
-
let lastValue = data.featureIds.value[0];
|
|
115
|
-
// Need to deduce start, end indices of each feature
|
|
116
|
-
for (let i = 0; i < data.featureIds.value.length; i++) {
|
|
117
|
-
const currValue = data.featureIds.value[i];
|
|
118
|
-
if (currValue === lastValue) {
|
|
119
|
-
// eslint-disable-next-line no-continue
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
features.push(parseFeature(data, lastIndex, i));
|
|
123
|
-
lastIndex = i;
|
|
124
|
-
lastValue = currValue;
|
|
125
|
-
}
|
|
126
|
-
// Last feature
|
|
127
|
-
features.push(parseFeature(data, lastIndex, data.featureIds.value.length));
|
|
128
|
-
}
|
|
129
|
-
return features;
|
|
130
|
-
}
|
|
131
|
-
/** Parse input binary data and return a single GeoJSON Feature */
|
|
132
|
-
function parseFeature(data, startIndex, endIndex) {
|
|
133
|
-
const geometry = binaryToGeometry(data, startIndex, endIndex);
|
|
134
|
-
const properties = parseProperties(data, startIndex, endIndex);
|
|
135
|
-
const fields = parseFields(data, startIndex, endIndex);
|
|
136
|
-
return { type: 'Feature', geometry, properties, ...fields };
|
|
137
|
-
}
|
|
138
|
-
/** Parse input binary data and return an object of fields */
|
|
139
|
-
function parseFields(data, startIndex = 0, endIndex) {
|
|
140
|
-
return data.fields && data.fields[data.featureIds.value[startIndex]];
|
|
141
|
-
}
|
|
142
|
-
/** Parse input binary data and return an object of properties */
|
|
143
|
-
function parseProperties(data, startIndex = 0, endIndex) {
|
|
144
|
-
const properties = Object.assign({}, data.properties[data.featureIds.value[startIndex]]);
|
|
145
|
-
for (const key in data.numericProps) {
|
|
146
|
-
properties[key] = data.numericProps[key].value[startIndex];
|
|
147
|
-
}
|
|
148
|
-
return properties;
|
|
149
|
-
}
|
|
150
|
-
/** Parse binary data of type Polygon */
|
|
151
|
-
function polygonToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
|
|
152
|
-
const { positions } = data;
|
|
153
|
-
const polygonIndices = data.polygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);
|
|
154
|
-
const primitivePolygonIndices = data.primitivePolygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);
|
|
155
|
-
const multi = polygonIndices.length > 2;
|
|
156
|
-
// Polygon
|
|
157
|
-
if (!multi) {
|
|
158
|
-
const coordinates = [];
|
|
159
|
-
for (let i = 0; i < primitivePolygonIndices.length - 1; i++) {
|
|
160
|
-
const startRingIndex = primitivePolygonIndices[i];
|
|
161
|
-
const endRingIndex = primitivePolygonIndices[i + 1];
|
|
162
|
-
const ringCoordinates = ringToGeoJson(positions, startRingIndex, endRingIndex);
|
|
163
|
-
coordinates.push(ringCoordinates);
|
|
164
|
-
}
|
|
165
|
-
return { type: 'Polygon', coordinates };
|
|
166
|
-
}
|
|
167
|
-
// MultiPolygon
|
|
168
|
-
const coordinates = [];
|
|
169
|
-
for (let i = 0; i < polygonIndices.length - 1; i++) {
|
|
170
|
-
const startPolygonIndex = polygonIndices[i];
|
|
171
|
-
const endPolygonIndex = polygonIndices[i + 1];
|
|
172
|
-
const polygonCoordinates = polygonToGeoJson(data, startPolygonIndex, endPolygonIndex).coordinates;
|
|
173
|
-
coordinates.push(polygonCoordinates);
|
|
174
|
-
}
|
|
175
|
-
return { type: 'MultiPolygon', coordinates };
|
|
176
|
-
}
|
|
177
|
-
/** Parse binary data of type LineString */
|
|
178
|
-
function lineStringToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
|
|
179
|
-
const { positions } = data;
|
|
180
|
-
const pathIndices = data.pathIndices.value.filter((x) => x >= startIndex && x <= endIndex);
|
|
181
|
-
const multi = pathIndices.length > 2;
|
|
182
|
-
if (!multi) {
|
|
183
|
-
const coordinates = ringToGeoJson(positions, pathIndices[0], pathIndices[1]);
|
|
184
|
-
return { type: 'LineString', coordinates };
|
|
185
|
-
}
|
|
186
|
-
const coordinates = [];
|
|
187
|
-
for (let i = 0; i < pathIndices.length - 1; i++) {
|
|
188
|
-
const ringCoordinates = ringToGeoJson(positions, pathIndices[i], pathIndices[i + 1]);
|
|
189
|
-
coordinates.push(ringCoordinates);
|
|
190
|
-
}
|
|
191
|
-
return { type: 'MultiLineString', coordinates };
|
|
192
|
-
}
|
|
193
|
-
/** Parse binary data of type Point */
|
|
194
|
-
function pointToGeoJson(data, startIndex, endIndex) {
|
|
195
|
-
const { positions } = data;
|
|
196
|
-
const coordinates = ringToGeoJson(positions, startIndex, endIndex);
|
|
197
|
-
const multi = coordinates.length > 1;
|
|
198
|
-
if (multi) {
|
|
199
|
-
return { type: 'MultiPoint', coordinates };
|
|
200
|
-
}
|
|
201
|
-
return { type: 'Point', coordinates: coordinates[0] };
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Parse a linear ring of positions to a GeoJSON linear ring
|
|
205
|
-
*
|
|
206
|
-
* @param positions Positions TypedArray
|
|
207
|
-
* @param startIndex Start index to include in ring
|
|
208
|
-
* @param endIndex End index to include in ring
|
|
209
|
-
* @returns GeoJSON ring
|
|
210
|
-
*/
|
|
211
|
-
function ringToGeoJson(positions, startIndex, endIndex) {
|
|
212
|
-
startIndex = startIndex || 0;
|
|
213
|
-
endIndex = endIndex || positions.value.length / positions.size;
|
|
214
|
-
const ringCoordinates = [];
|
|
215
|
-
for (let j = startIndex; j < endIndex; j++) {
|
|
216
|
-
const coord = Array();
|
|
217
|
-
for (let k = j * positions.size; k < (j + 1) * positions.size; k++) {
|
|
218
|
-
coord.push(Number(positions.value[k]));
|
|
219
|
-
}
|
|
220
|
-
ringCoordinates.push(coord);
|
|
221
|
-
}
|
|
222
|
-
return ringCoordinates;
|
|
223
|
-
}
|
|
224
|
-
// Deduce geometry type of data object
|
|
225
|
-
function parseType(data) {
|
|
226
|
-
if (data.pathIndices) {
|
|
227
|
-
return 'LineString';
|
|
228
|
-
}
|
|
229
|
-
if (data.polygonIndices) {
|
|
230
|
-
return 'Polygon';
|
|
231
|
-
}
|
|
232
|
-
return 'Point';
|
|
233
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractGeometryInfo = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Initial scan over GeoJSON features
|
|
6
|
-
* Counts number of coordinates of each geometry type and
|
|
7
|
-
* keeps track of the max coordinate dimensions
|
|
8
|
-
*/
|
|
9
|
-
// eslint-disable-next-line complexity, max-statements
|
|
10
|
-
function extractGeometryInfo(features) {
|
|
11
|
-
// Counts the number of _positions_, so [x, y, z] counts as one
|
|
12
|
-
let pointPositionsCount = 0;
|
|
13
|
-
let pointFeaturesCount = 0;
|
|
14
|
-
let linePositionsCount = 0;
|
|
15
|
-
let linePathsCount = 0;
|
|
16
|
-
let lineFeaturesCount = 0;
|
|
17
|
-
let polygonPositionsCount = 0;
|
|
18
|
-
let polygonObjectsCount = 0;
|
|
19
|
-
let polygonRingsCount = 0;
|
|
20
|
-
let polygonFeaturesCount = 0;
|
|
21
|
-
const coordLengths = new Set();
|
|
22
|
-
for (const feature of features) {
|
|
23
|
-
const geometry = feature.geometry;
|
|
24
|
-
switch (geometry.type) {
|
|
25
|
-
case 'Point':
|
|
26
|
-
pointFeaturesCount++;
|
|
27
|
-
pointPositionsCount++;
|
|
28
|
-
coordLengths.add(geometry.coordinates.length);
|
|
29
|
-
break;
|
|
30
|
-
case 'MultiPoint':
|
|
31
|
-
pointFeaturesCount++;
|
|
32
|
-
pointPositionsCount += geometry.coordinates.length;
|
|
33
|
-
for (const point of geometry.coordinates) {
|
|
34
|
-
coordLengths.add(point.length);
|
|
35
|
-
}
|
|
36
|
-
break;
|
|
37
|
-
case 'LineString':
|
|
38
|
-
lineFeaturesCount++;
|
|
39
|
-
linePositionsCount += geometry.coordinates.length;
|
|
40
|
-
linePathsCount++;
|
|
41
|
-
for (const coord of geometry.coordinates) {
|
|
42
|
-
coordLengths.add(coord.length);
|
|
43
|
-
}
|
|
44
|
-
break;
|
|
45
|
-
case 'MultiLineString':
|
|
46
|
-
lineFeaturesCount++;
|
|
47
|
-
for (const line of geometry.coordinates) {
|
|
48
|
-
linePositionsCount += line.length;
|
|
49
|
-
linePathsCount++;
|
|
50
|
-
// eslint-disable-next-line max-depth
|
|
51
|
-
for (const coord of line) {
|
|
52
|
-
coordLengths.add(coord.length);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
break;
|
|
56
|
-
case 'Polygon':
|
|
57
|
-
polygonFeaturesCount++;
|
|
58
|
-
polygonObjectsCount++;
|
|
59
|
-
polygonRingsCount += geometry.coordinates.length;
|
|
60
|
-
const flattened = geometry.coordinates.flat();
|
|
61
|
-
polygonPositionsCount += flattened.length;
|
|
62
|
-
for (const coord of flattened) {
|
|
63
|
-
coordLengths.add(coord.length);
|
|
64
|
-
}
|
|
65
|
-
break;
|
|
66
|
-
case 'MultiPolygon':
|
|
67
|
-
polygonFeaturesCount++;
|
|
68
|
-
for (const polygon of geometry.coordinates) {
|
|
69
|
-
polygonObjectsCount++;
|
|
70
|
-
polygonRingsCount += polygon.length;
|
|
71
|
-
const flattened = polygon.flat();
|
|
72
|
-
polygonPositionsCount += flattened.length;
|
|
73
|
-
// eslint-disable-next-line max-depth
|
|
74
|
-
for (const coord of flattened) {
|
|
75
|
-
coordLengths.add(coord.length);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
break;
|
|
79
|
-
default:
|
|
80
|
-
throw new Error(`Unsupported geometry type: ${geometry.type}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return {
|
|
84
|
-
coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
|
|
85
|
-
pointPositionsCount,
|
|
86
|
-
pointFeaturesCount,
|
|
87
|
-
linePositionsCount,
|
|
88
|
-
linePathsCount,
|
|
89
|
-
lineFeaturesCount,
|
|
90
|
-
polygonPositionsCount,
|
|
91
|
-
polygonObjectsCount,
|
|
92
|
-
polygonRingsCount,
|
|
93
|
-
polygonFeaturesCount
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
exports.extractGeometryInfo = extractGeometryInfo;
|
|
@@ -1,385 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TEST_EXPORTS = exports.flatGeojsonToBinary = void 0;
|
|
4
|
-
/* eslint-disable indent */
|
|
5
|
-
const polygon_1 = require("@math.gl/polygon");
|
|
6
|
-
/**
|
|
7
|
-
* Convert binary features to flat binary arrays. Similar to
|
|
8
|
-
* `geojsonToBinary` helper function, except that it expects
|
|
9
|
-
* a binary representation of the feature data, which enables
|
|
10
|
-
* 2X-3X speed increase in parse speed, compared to using
|
|
11
|
-
* geoJSON. See `binary-vector-tile/VectorTileFeature` for
|
|
12
|
-
* data format detais
|
|
13
|
-
*
|
|
14
|
-
* @param features
|
|
15
|
-
* @param geometryInfo
|
|
16
|
-
* @param options
|
|
17
|
-
* @returns filled arrays
|
|
18
|
-
*/
|
|
19
|
-
function flatGeojsonToBinary(features, geometryInfo, options) {
|
|
20
|
-
const propArrayTypes = extractNumericPropTypes(features);
|
|
21
|
-
const numericPropKeys = Object.keys(propArrayTypes).filter((k) => propArrayTypes[k] !== Array);
|
|
22
|
-
return fillArrays(features, {
|
|
23
|
-
propArrayTypes,
|
|
24
|
-
...geometryInfo
|
|
25
|
-
}, {
|
|
26
|
-
numericPropKeys: (options && options.numericPropKeys) || numericPropKeys,
|
|
27
|
-
PositionDataType: options ? options.PositionDataType : Float32Array,
|
|
28
|
-
triangulate: options ? options.triangulate : true
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
exports.flatGeojsonToBinary = flatGeojsonToBinary;
|
|
32
|
-
exports.TEST_EXPORTS = {
|
|
33
|
-
extractNumericPropTypes
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Extracts properties that are always numeric
|
|
37
|
-
*
|
|
38
|
-
* @param features
|
|
39
|
-
* @returns object with numeric types
|
|
40
|
-
*/
|
|
41
|
-
function extractNumericPropTypes(features) {
|
|
42
|
-
const propArrayTypes = {};
|
|
43
|
-
for (const feature of features) {
|
|
44
|
-
if (feature.properties) {
|
|
45
|
-
for (const key in feature.properties) {
|
|
46
|
-
// If property has not been seen before, or if property has been numeric
|
|
47
|
-
// in all previous features, check if numeric in this feature
|
|
48
|
-
// If not numeric, Array is stored to prevent rechecking in the future
|
|
49
|
-
// Additionally, detects if 64 bit precision is required
|
|
50
|
-
const val = feature.properties[key];
|
|
51
|
-
propArrayTypes[key] = deduceArrayType(val, propArrayTypes[key]);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return propArrayTypes;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Fills coordinates into pre-allocated typed arrays
|
|
59
|
-
*
|
|
60
|
-
* @param features
|
|
61
|
-
* @param geometryInfo
|
|
62
|
-
* @param options
|
|
63
|
-
* @returns an accessor object with value and size keys
|
|
64
|
-
*/
|
|
65
|
-
// eslint-disable-next-line complexity
|
|
66
|
-
function fillArrays(features, geometryInfo, options) {
|
|
67
|
-
const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount, propArrayTypes, coordLength } = geometryInfo;
|
|
68
|
-
const { numericPropKeys = [], PositionDataType = Float32Array, triangulate = true } = options;
|
|
69
|
-
const hasGlobalId = features[0] && 'id' in features[0];
|
|
70
|
-
const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
|
|
71
|
-
const points = {
|
|
72
|
-
type: 'Point',
|
|
73
|
-
positions: new PositionDataType(pointPositionsCount * coordLength),
|
|
74
|
-
globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
|
|
75
|
-
featureIds: pointFeaturesCount > 65535
|
|
76
|
-
? new Uint32Array(pointPositionsCount)
|
|
77
|
-
: new Uint16Array(pointPositionsCount),
|
|
78
|
-
numericProps: {},
|
|
79
|
-
properties: [],
|
|
80
|
-
fields: []
|
|
81
|
-
};
|
|
82
|
-
const lines = {
|
|
83
|
-
type: 'LineString',
|
|
84
|
-
pathIndices: linePositionsCount > 65535
|
|
85
|
-
? new Uint32Array(linePathsCount + 1)
|
|
86
|
-
: new Uint16Array(linePathsCount + 1),
|
|
87
|
-
positions: new PositionDataType(linePositionsCount * coordLength),
|
|
88
|
-
globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
|
|
89
|
-
featureIds: lineFeaturesCount > 65535
|
|
90
|
-
? new Uint32Array(linePositionsCount)
|
|
91
|
-
: new Uint16Array(linePositionsCount),
|
|
92
|
-
numericProps: {},
|
|
93
|
-
properties: [],
|
|
94
|
-
fields: []
|
|
95
|
-
};
|
|
96
|
-
const polygons = {
|
|
97
|
-
type: 'Polygon',
|
|
98
|
-
polygonIndices: polygonPositionsCount > 65535
|
|
99
|
-
? new Uint32Array(polygonObjectsCount + 1)
|
|
100
|
-
: new Uint16Array(polygonObjectsCount + 1),
|
|
101
|
-
primitivePolygonIndices: polygonPositionsCount > 65535
|
|
102
|
-
? new Uint32Array(polygonRingsCount + 1)
|
|
103
|
-
: new Uint16Array(polygonRingsCount + 1),
|
|
104
|
-
positions: new PositionDataType(polygonPositionsCount * coordLength),
|
|
105
|
-
globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
|
|
106
|
-
featureIds: polygonFeaturesCount > 65535
|
|
107
|
-
? new Uint32Array(polygonPositionsCount)
|
|
108
|
-
: new Uint16Array(polygonPositionsCount),
|
|
109
|
-
numericProps: {},
|
|
110
|
-
properties: [],
|
|
111
|
-
fields: []
|
|
112
|
-
};
|
|
113
|
-
if (triangulate) {
|
|
114
|
-
polygons.triangles = [];
|
|
115
|
-
}
|
|
116
|
-
// Instantiate numeric properties arrays; one value per vertex
|
|
117
|
-
for (const object of [points, lines, polygons]) {
|
|
118
|
-
for (const propName of numericPropKeys) {
|
|
119
|
-
// If property has been numeric in all previous features in which the property existed, check
|
|
120
|
-
// if numeric in this feature
|
|
121
|
-
const T = propArrayTypes[propName];
|
|
122
|
-
object.numericProps[propName] = new T(object.positions.length / coordLength);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
// Set last element of path/polygon indices as positions length
|
|
126
|
-
lines.pathIndices[linePathsCount] = linePositionsCount;
|
|
127
|
-
polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
|
|
128
|
-
polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
|
|
129
|
-
const indexMap = {
|
|
130
|
-
pointPosition: 0,
|
|
131
|
-
pointFeature: 0,
|
|
132
|
-
linePosition: 0,
|
|
133
|
-
linePath: 0,
|
|
134
|
-
lineFeature: 0,
|
|
135
|
-
polygonPosition: 0,
|
|
136
|
-
polygonObject: 0,
|
|
137
|
-
polygonRing: 0,
|
|
138
|
-
polygonFeature: 0,
|
|
139
|
-
feature: 0
|
|
140
|
-
};
|
|
141
|
-
for (const feature of features) {
|
|
142
|
-
const geometry = feature.geometry;
|
|
143
|
-
const properties = feature.properties || {};
|
|
144
|
-
switch (geometry.type) {
|
|
145
|
-
case 'Point':
|
|
146
|
-
handlePoint(geometry, points, indexMap, coordLength, properties);
|
|
147
|
-
points.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
148
|
-
if (hasGlobalId) {
|
|
149
|
-
points.fields.push({ id: feature.id });
|
|
150
|
-
}
|
|
151
|
-
indexMap.pointFeature++;
|
|
152
|
-
break;
|
|
153
|
-
case 'LineString':
|
|
154
|
-
handleLineString(geometry, lines, indexMap, coordLength, properties);
|
|
155
|
-
lines.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
156
|
-
if (hasGlobalId) {
|
|
157
|
-
lines.fields.push({ id: feature.id });
|
|
158
|
-
}
|
|
159
|
-
indexMap.lineFeature++;
|
|
160
|
-
break;
|
|
161
|
-
case 'Polygon':
|
|
162
|
-
handlePolygon(geometry, polygons, indexMap, coordLength, properties);
|
|
163
|
-
polygons.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
164
|
-
if (hasGlobalId) {
|
|
165
|
-
polygons.fields.push({ id: feature.id });
|
|
166
|
-
}
|
|
167
|
-
indexMap.polygonFeature++;
|
|
168
|
-
break;
|
|
169
|
-
default:
|
|
170
|
-
throw new Error('Invalid geometry type');
|
|
171
|
-
}
|
|
172
|
-
indexMap.feature++;
|
|
173
|
-
}
|
|
174
|
-
// Wrap each array in an accessor object with value and size keys
|
|
175
|
-
return makeAccessorObjects(points, lines, polygons, coordLength);
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Fills (Multi)Point coordinates into points object of arrays
|
|
179
|
-
*
|
|
180
|
-
* @param geometry
|
|
181
|
-
* @param points
|
|
182
|
-
* @param indexMap
|
|
183
|
-
* @param coordLength
|
|
184
|
-
* @param properties
|
|
185
|
-
*/
|
|
186
|
-
function handlePoint(geometry, points, indexMap, coordLength, properties) {
|
|
187
|
-
points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
|
|
188
|
-
const nPositions = geometry.data.length / coordLength;
|
|
189
|
-
fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
|
|
190
|
-
points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
|
|
191
|
-
points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
|
|
192
|
-
indexMap.pointPosition += nPositions;
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Fills (Multi)LineString coordinates into lines object of arrays
|
|
196
|
-
*
|
|
197
|
-
* @param geometry
|
|
198
|
-
* @param lines
|
|
199
|
-
* @param indexMap
|
|
200
|
-
* @param coordLength
|
|
201
|
-
* @param properties
|
|
202
|
-
*/
|
|
203
|
-
function handleLineString(geometry, lines, indexMap, coordLength, properties) {
|
|
204
|
-
lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
|
|
205
|
-
const nPositions = geometry.data.length / coordLength;
|
|
206
|
-
fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
|
|
207
|
-
lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
|
|
208
|
-
lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
|
|
209
|
-
for (let i = 0, il = geometry.indices.length; i < il; ++i) {
|
|
210
|
-
// Extract range of data we are working with, defined by start
|
|
211
|
-
// and end indices (these index into the geometry.data array)
|
|
212
|
-
const start = geometry.indices[i];
|
|
213
|
-
const end = i === il - 1
|
|
214
|
-
? geometry.data.length // last line, so read to end of data
|
|
215
|
-
: geometry.indices[i + 1]; // start index for next line
|
|
216
|
-
lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
|
|
217
|
-
indexMap.linePosition += (end - start) / coordLength;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Fills (Multi)Polygon coordinates into polygons object of arrays
|
|
222
|
-
*
|
|
223
|
-
* @param geometry
|
|
224
|
-
* @param polygons
|
|
225
|
-
* @param indexMap
|
|
226
|
-
* @param coordLength
|
|
227
|
-
* @param properties
|
|
228
|
-
*/
|
|
229
|
-
function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
|
|
230
|
-
polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
|
|
231
|
-
const nPositions = geometry.data.length / coordLength;
|
|
232
|
-
fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
|
|
233
|
-
polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
|
|
234
|
-
polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
|
|
235
|
-
// Unlike Point & LineString geometry.indices is a 2D array
|
|
236
|
-
for (let l = 0, ll = geometry.indices.length; l < ll; ++l) {
|
|
237
|
-
const startPosition = indexMap.polygonPosition;
|
|
238
|
-
polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
|
|
239
|
-
const areas = geometry.areas[l];
|
|
240
|
-
const indices = geometry.indices[l];
|
|
241
|
-
const nextIndices = geometry.indices[l + 1];
|
|
242
|
-
for (let i = 0, il = indices.length; i < il; ++i) {
|
|
243
|
-
const start = indices[i];
|
|
244
|
-
const end = i === il - 1
|
|
245
|
-
? // last line, so either read to:
|
|
246
|
-
nextIndices === undefined
|
|
247
|
-
? geometry.data.length // end of data (no next indices)
|
|
248
|
-
: nextIndices[0] // start of first line in nextIndices
|
|
249
|
-
: indices[i + 1]; // start index for next line
|
|
250
|
-
polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
|
|
251
|
-
indexMap.polygonPosition += (end - start) / coordLength;
|
|
252
|
-
}
|
|
253
|
-
const endPosition = indexMap.polygonPosition;
|
|
254
|
-
triangulatePolygon(polygons, areas, indices, { startPosition, endPosition, coordLength });
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Triangulate polygon using earcut
|
|
259
|
-
*
|
|
260
|
-
* @param polygons
|
|
261
|
-
* @param areas
|
|
262
|
-
* @param indices
|
|
263
|
-
* @param param3
|
|
264
|
-
*/
|
|
265
|
-
function triangulatePolygon(polygons, areas, indices, { startPosition, endPosition, coordLength }) {
|
|
266
|
-
if (!polygons.triangles) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const start = startPosition * coordLength;
|
|
270
|
-
const end = endPosition * coordLength;
|
|
271
|
-
// Extract positions and holes for just this polygon
|
|
272
|
-
const polygonPositions = polygons.positions.subarray(start, end);
|
|
273
|
-
// Holes are referenced relative to outer polygon
|
|
274
|
-
const offset = indices[0];
|
|
275
|
-
const holes = indices.slice(1).map((n) => (n - offset) / coordLength);
|
|
276
|
-
// Compute triangulation
|
|
277
|
-
// @ts-expect-error TODO can earcut handle binary arrays? Add tests?
|
|
278
|
-
const triangles = (0, polygon_1.earcut)(polygonPositions, holes, coordLength, areas);
|
|
279
|
-
// Indices returned by triangulation are relative to start
|
|
280
|
-
// of polygon, so we need to offset
|
|
281
|
-
for (let t = 0, tl = triangles.length; t < tl; ++t) {
|
|
282
|
-
polygons.triangles.push(startPosition + triangles[t]);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* Wraps an object containing array into accessors
|
|
287
|
-
*
|
|
288
|
-
* @param obj
|
|
289
|
-
* @param size
|
|
290
|
-
*/
|
|
291
|
-
function wrapProps(obj, size) {
|
|
292
|
-
const returnObj = {};
|
|
293
|
-
for (const key in obj) {
|
|
294
|
-
returnObj[key] = { value: obj[key], size };
|
|
295
|
-
}
|
|
296
|
-
return returnObj;
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Wrap each array in an accessor object with value and size keys
|
|
300
|
-
*
|
|
301
|
-
* @param points
|
|
302
|
-
* @param lines
|
|
303
|
-
* @param polygons
|
|
304
|
-
* @param coordLength
|
|
305
|
-
* @returns object
|
|
306
|
-
*/
|
|
307
|
-
function makeAccessorObjects(points, lines, polygons, coordLength) {
|
|
308
|
-
const binaryFeatures = {
|
|
309
|
-
points: {
|
|
310
|
-
...points,
|
|
311
|
-
positions: { value: points.positions, size: coordLength },
|
|
312
|
-
globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
|
|
313
|
-
featureIds: { value: points.featureIds, size: 1 },
|
|
314
|
-
numericProps: wrapProps(points.numericProps, 1)
|
|
315
|
-
},
|
|
316
|
-
lines: {
|
|
317
|
-
...lines,
|
|
318
|
-
positions: { value: lines.positions, size: coordLength },
|
|
319
|
-
pathIndices: { value: lines.pathIndices, size: 1 },
|
|
320
|
-
globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
|
|
321
|
-
featureIds: { value: lines.featureIds, size: 1 },
|
|
322
|
-
numericProps: wrapProps(lines.numericProps, 1)
|
|
323
|
-
},
|
|
324
|
-
polygons: {
|
|
325
|
-
...polygons,
|
|
326
|
-
positions: { value: polygons.positions, size: coordLength },
|
|
327
|
-
polygonIndices: { value: polygons.polygonIndices, size: 1 },
|
|
328
|
-
primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
|
|
329
|
-
globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
|
|
330
|
-
featureIds: { value: polygons.featureIds, size: 1 },
|
|
331
|
-
numericProps: wrapProps(polygons.numericProps, 1)
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
if (polygons.triangles) {
|
|
335
|
-
binaryFeatures.polygons.triangles = { value: new Uint32Array(polygons.triangles), size: 1 };
|
|
336
|
-
}
|
|
337
|
-
return binaryFeatures;
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Add numeric properties to object
|
|
341
|
-
*
|
|
342
|
-
* @param object
|
|
343
|
-
* @param properties
|
|
344
|
-
* @param index
|
|
345
|
-
* @param length
|
|
346
|
-
*/
|
|
347
|
-
function fillNumericProperties(object, properties, index, length) {
|
|
348
|
-
for (const numericPropName in object.numericProps) {
|
|
349
|
-
if (numericPropName in properties) {
|
|
350
|
-
const value = properties[numericPropName];
|
|
351
|
-
object.numericProps[numericPropName].fill(value, index, index + length);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Keep string properties in object
|
|
357
|
-
*
|
|
358
|
-
* @param properties
|
|
359
|
-
* @param numericKeys
|
|
360
|
-
* @returns object
|
|
361
|
-
*/
|
|
362
|
-
function keepStringProperties(properties, numericKeys) {
|
|
363
|
-
const props = {};
|
|
364
|
-
for (const key in properties) {
|
|
365
|
-
if (!numericKeys.includes(key)) {
|
|
366
|
-
props[key] = properties[key];
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
return props;
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
*
|
|
373
|
-
* Deduce correct array constructor to use for a given value
|
|
374
|
-
*
|
|
375
|
-
* @param x value to test
|
|
376
|
-
* @param constructor previous constructor deduced
|
|
377
|
-
* @returns PropArrayConstructor
|
|
378
|
-
*/
|
|
379
|
-
function deduceArrayType(x, constructor) {
|
|
380
|
-
if (constructor === Array || !Number.isFinite(x)) {
|
|
381
|
-
return Array;
|
|
382
|
-
}
|
|
383
|
-
// If this or previous value required 64bits use Float64Array
|
|
384
|
-
return constructor === Float64Array || Math.fround(x) !== x ? Float64Array : Float32Array;
|
|
385
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.geojsonToBinary = void 0;
|
|
4
|
-
const extract_geometry_info_1 = require("./extract-geometry-info");
|
|
5
|
-
const geojson_to_flat_geojson_1 = require("./geojson-to-flat-geojson");
|
|
6
|
-
const flat_geojson_to_binary_1 = require("./flat-geojson-to-binary");
|
|
7
|
-
/**
|
|
8
|
-
* Convert GeoJSON features to flat binary arrays
|
|
9
|
-
*
|
|
10
|
-
* @param features
|
|
11
|
-
* @param options
|
|
12
|
-
* @returns features in binary format, grouped by geometry type
|
|
13
|
-
*/
|
|
14
|
-
function geojsonToBinary(features, options = { fixRingWinding: true, triangulate: true }) {
|
|
15
|
-
const geometryInfo = (0, extract_geometry_info_1.extractGeometryInfo)(features);
|
|
16
|
-
const coordLength = geometryInfo.coordLength;
|
|
17
|
-
const { fixRingWinding } = options;
|
|
18
|
-
const flatFeatures = (0, geojson_to_flat_geojson_1.geojsonToFlatGeojson)(features, { coordLength, fixRingWinding });
|
|
19
|
-
return (0, flat_geojson_to_binary_1.flatGeojsonToBinary)(flatFeatures, geometryInfo, {
|
|
20
|
-
numericPropKeys: options.numericPropKeys,
|
|
21
|
-
PositionDataType: options.PositionDataType || Float32Array,
|
|
22
|
-
triangulate: options.triangulate
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
exports.geojsonToBinary = geojsonToBinary;
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.geojsonToFlatGeojson = void 0;
|
|
4
|
-
const polygon_1 = require("@math.gl/polygon");
|
|
5
|
-
/**
|
|
6
|
-
* Convert GeoJSON features to Flat GeoJSON features
|
|
7
|
-
*
|
|
8
|
-
* @param features
|
|
9
|
-
* @param options
|
|
10
|
-
* @returns an Array of Flat GeoJSON features
|
|
11
|
-
*/
|
|
12
|
-
function geojsonToFlatGeojson(features, options = { coordLength: 2, fixRingWinding: true }) {
|
|
13
|
-
return features.map((feature) => flattenFeature(feature, options));
|
|
14
|
-
}
|
|
15
|
-
exports.geojsonToFlatGeojson = geojsonToFlatGeojson;
|
|
16
|
-
/**
|
|
17
|
-
* Helper function to copy Point values from `coordinates` into `data` & `indices`
|
|
18
|
-
*
|
|
19
|
-
* @param coordinates
|
|
20
|
-
* @param data
|
|
21
|
-
* @param indices
|
|
22
|
-
* @param options
|
|
23
|
-
*/
|
|
24
|
-
function flattenPoint(coordinates, data, indices, options) {
|
|
25
|
-
indices.push(data.length);
|
|
26
|
-
data.push(...coordinates);
|
|
27
|
-
// Pad up to coordLength
|
|
28
|
-
for (let i = coordinates.length; i < options.coordLength; i++) {
|
|
29
|
-
data.push(0);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Helper function to copy LineString values from `coordinates` into `data` & `indices`
|
|
34
|
-
*
|
|
35
|
-
* @param coordinates
|
|
36
|
-
* @param data
|
|
37
|
-
* @param indices
|
|
38
|
-
* @param options
|
|
39
|
-
*/
|
|
40
|
-
function flattenLineString(coordinates, data, indices, options) {
|
|
41
|
-
indices.push(data.length);
|
|
42
|
-
for (const c of coordinates) {
|
|
43
|
-
data.push(...c);
|
|
44
|
-
// Pad up to coordLength
|
|
45
|
-
for (let i = c.length; i < options.coordLength; i++) {
|
|
46
|
-
data.push(0);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Helper function to copy Polygon values from `coordinates` into `data` & `indices` & `areas`
|
|
52
|
-
*
|
|
53
|
-
* @param coordinates
|
|
54
|
-
* @param data
|
|
55
|
-
* @param indices
|
|
56
|
-
* @param areas
|
|
57
|
-
* @param options
|
|
58
|
-
*/
|
|
59
|
-
function flattenPolygon(coordinates, data, indices, areas, options) {
|
|
60
|
-
let count = 0;
|
|
61
|
-
const ringAreas = [];
|
|
62
|
-
const polygons = [];
|
|
63
|
-
for (const lineString of coordinates) {
|
|
64
|
-
const lineString2d = lineString.map((p) => p.slice(0, 2));
|
|
65
|
-
let area = (0, polygon_1.getPolygonSignedArea)(lineString2d.flat());
|
|
66
|
-
const ccw = area < 0;
|
|
67
|
-
// Exterior ring must be CCW and interior rings CW
|
|
68
|
-
if (options.fixRingWinding && ((count === 0 && !ccw) || (count > 0 && ccw))) {
|
|
69
|
-
lineString.reverse();
|
|
70
|
-
area = -area;
|
|
71
|
-
}
|
|
72
|
-
ringAreas.push(area);
|
|
73
|
-
flattenLineString(lineString, data, polygons, options);
|
|
74
|
-
count++;
|
|
75
|
-
}
|
|
76
|
-
if (count > 0) {
|
|
77
|
-
areas.push(ringAreas);
|
|
78
|
-
indices.push(polygons);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Flatten single GeoJSON feature into Flat GeoJSON
|
|
83
|
-
*
|
|
84
|
-
* @param feature
|
|
85
|
-
* @param options
|
|
86
|
-
* @returns A Flat GeoJSON feature
|
|
87
|
-
*/
|
|
88
|
-
function flattenFeature(feature, options) {
|
|
89
|
-
const { geometry } = feature;
|
|
90
|
-
if (geometry.type === 'GeometryCollection') {
|
|
91
|
-
throw new Error('GeometryCollection type not supported');
|
|
92
|
-
}
|
|
93
|
-
const data = [];
|
|
94
|
-
const indices = [];
|
|
95
|
-
let areas;
|
|
96
|
-
let type;
|
|
97
|
-
switch (geometry.type) {
|
|
98
|
-
case 'Point':
|
|
99
|
-
type = 'Point';
|
|
100
|
-
flattenPoint(geometry.coordinates, data, indices, options);
|
|
101
|
-
break;
|
|
102
|
-
case 'MultiPoint':
|
|
103
|
-
type = 'Point';
|
|
104
|
-
geometry.coordinates.map((c) => flattenPoint(c, data, indices, options));
|
|
105
|
-
break;
|
|
106
|
-
case 'LineString':
|
|
107
|
-
type = 'LineString';
|
|
108
|
-
flattenLineString(geometry.coordinates, data, indices, options);
|
|
109
|
-
break;
|
|
110
|
-
case 'MultiLineString':
|
|
111
|
-
type = 'LineString';
|
|
112
|
-
geometry.coordinates.map((c) => flattenLineString(c, data, indices, options));
|
|
113
|
-
break;
|
|
114
|
-
case 'Polygon':
|
|
115
|
-
type = 'Polygon';
|
|
116
|
-
areas = [];
|
|
117
|
-
flattenPolygon(geometry.coordinates, data, indices, areas, options);
|
|
118
|
-
break;
|
|
119
|
-
case 'MultiPolygon':
|
|
120
|
-
type = 'Polygon';
|
|
121
|
-
areas = [];
|
|
122
|
-
geometry.coordinates.map((c) => flattenPolygon(c, data, indices, areas, options));
|
|
123
|
-
break;
|
|
124
|
-
default:
|
|
125
|
-
throw new Error(`Unknown type: ${type}`);
|
|
126
|
-
}
|
|
127
|
-
return { ...feature, geometry: { type, indices, data, areas } };
|
|
128
|
-
}
|
package/dist/lib/transform.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformGeoJsonCoords = exports.transformBinaryCoords = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Apply transformation to every coordinate of binary features
|
|
6
|
-
* @param binaryFeatures binary features
|
|
7
|
-
* @param transformCoordinate Function to call on each coordinate
|
|
8
|
-
* @return Transformed binary features
|
|
9
|
-
*/
|
|
10
|
-
function transformBinaryCoords(binaryFeatures, transformCoordinate) {
|
|
11
|
-
if (binaryFeatures.points) {
|
|
12
|
-
transformBinaryGeometryPositions(binaryFeatures.points, transformCoordinate);
|
|
13
|
-
}
|
|
14
|
-
if (binaryFeatures.lines) {
|
|
15
|
-
transformBinaryGeometryPositions(binaryFeatures.lines, transformCoordinate);
|
|
16
|
-
}
|
|
17
|
-
if (binaryFeatures.polygons) {
|
|
18
|
-
transformBinaryGeometryPositions(binaryFeatures.polygons, transformCoordinate);
|
|
19
|
-
}
|
|
20
|
-
return binaryFeatures;
|
|
21
|
-
}
|
|
22
|
-
exports.transformBinaryCoords = transformBinaryCoords;
|
|
23
|
-
/** Transform one binary geometry */
|
|
24
|
-
function transformBinaryGeometryPositions(binaryGeometry, fn) {
|
|
25
|
-
const { positions } = binaryGeometry;
|
|
26
|
-
for (let i = 0; i < positions.value.length; i += positions.size) {
|
|
27
|
-
// @ts-ignore inclusion of bigint causes problems
|
|
28
|
-
const coord = Array.from(positions.value.subarray(i, i + positions.size));
|
|
29
|
-
const transformedCoord = fn(coord);
|
|
30
|
-
// @ts-ignore typescript typing for .set seems to require bigint?
|
|
31
|
-
positions.value.set(transformedCoord, i);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Apply transformation to every coordinate of GeoJSON features
|
|
36
|
-
*
|
|
37
|
-
* @param features Array of GeoJSON features
|
|
38
|
-
* @param fn Function to call on each coordinate
|
|
39
|
-
* @return Transformed GeoJSON features
|
|
40
|
-
*/
|
|
41
|
-
function transformGeoJsonCoords(features, fn) {
|
|
42
|
-
for (const feature of features) {
|
|
43
|
-
// @ts-ignore
|
|
44
|
-
feature.geometry.coordinates = coordMap(feature.geometry.coordinates, fn);
|
|
45
|
-
}
|
|
46
|
-
return features;
|
|
47
|
-
}
|
|
48
|
-
exports.transformGeoJsonCoords = transformGeoJsonCoords;
|
|
49
|
-
function coordMap(array, fn) {
|
|
50
|
-
if (isCoord(array)) {
|
|
51
|
-
return fn(array);
|
|
52
|
-
}
|
|
53
|
-
return array.map((item) => {
|
|
54
|
-
return coordMap(item, fn);
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
function isCoord(array) {
|
|
58
|
-
return Number.isFinite(array[0]) && Number.isFinite(array[1]);
|
|
59
|
-
}
|