@loaders.gl/gis 3.1.0-alpha.4 → 3.1.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +5 -0
- package/dist/es5/bundle.js +1 -1
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/index.js +6 -6
- package/dist/es5/lib/binary-to-geojson.js +93 -139
- package/dist/es5/lib/binary-to-geojson.js.map +1 -1
- package/dist/es5/lib/geojson-to-binary.js +196 -366
- package/dist/es5/lib/geojson-to-binary.js.map +1 -1
- package/dist/es5/lib/transform.js +9 -24
- package/dist/es5/lib/transform.js.map +1 -1
- package/dist/esm/lib/binary-to-geojson.js +2 -2
- package/dist/esm/lib/binary-to-geojson.js.map +1 -1
- package/dist/esm/lib/geojson-to-binary.js +1 -1
- package/dist/esm/lib/geojson-to-binary.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/lib/binary-to-geojson.d.ts +21 -0
- package/dist/lib/binary-to-geojson.d.ts.map +1 -0
- package/dist/lib/binary-to-geojson.js +233 -0
- package/dist/lib/geojson-to-binary.d.ts +39 -0
- package/dist/lib/geojson-to-binary.d.ts.map +1 -0
- package/dist/lib/geojson-to-binary.js +362 -0
- package/dist/lib/transform.d.ts +19 -0
- package/dist/lib/transform.d.ts.map +1 -0
- package/dist/lib/transform.js +59 -0
- package/package.json +5 -5
|
@@ -0,0 +1,233 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Feature } from '@loaders.gl/schema';
|
|
2
|
+
import type { BinaryFeatures } from '@loaders.gl/schema';
|
|
3
|
+
export declare type GeojsonToBinaryOptions = {
|
|
4
|
+
coordLength?: number;
|
|
5
|
+
numericPropKeys?: string[];
|
|
6
|
+
PositionDataType?: Function;
|
|
7
|
+
};
|
|
8
|
+
/** Convert GeoJSON features to flat binary arrays */
|
|
9
|
+
export declare function geojsonToBinary(features: Feature[], options?: GeojsonToBinaryOptions): BinaryFeatures;
|
|
10
|
+
export declare const TEST_EXPORTS: {
|
|
11
|
+
firstPass: typeof firstPass;
|
|
12
|
+
secondPass: typeof secondPass;
|
|
13
|
+
};
|
|
14
|
+
declare type FirstPassData = {
|
|
15
|
+
coordLength: number;
|
|
16
|
+
numericPropKeys: string[];
|
|
17
|
+
pointPositionsCount: number;
|
|
18
|
+
pointFeaturesCount: number;
|
|
19
|
+
linePositionsCount: number;
|
|
20
|
+
linePathsCount: number;
|
|
21
|
+
lineFeaturesCount: number;
|
|
22
|
+
polygonPositionsCount: number;
|
|
23
|
+
polygonObjectsCount: number;
|
|
24
|
+
polygonRingsCount: number;
|
|
25
|
+
polygonFeaturesCount: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Initial scan over GeoJSON features
|
|
29
|
+
* Counts number of coordinates of each geometry type and
|
|
30
|
+
* keeps track of the max coordinate dimensions
|
|
31
|
+
*/
|
|
32
|
+
declare function firstPass(features: Feature[]): FirstPassData;
|
|
33
|
+
/**
|
|
34
|
+
* Second scan over GeoJSON features
|
|
35
|
+
* Fills coordinates into pre-allocated typed arrays
|
|
36
|
+
*/
|
|
37
|
+
declare function secondPass(features: any, firstPassData: FirstPassData, options: Required<GeojsonToBinaryOptions>): BinaryFeatures;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=geojson-to-binary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geojson-to-binary.d.ts","sourceRoot":"","sources":["../../src/lib/geojson-to-binary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAoB,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAEvD,oBAAY,sBAAsB,GAAG;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,gBAAgB,CAAC,EAAE,QAAQ,CAAC;CAC7B,CAAC;AAEF,qDAAqD;AACrD,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,OAAO,EAAE,EACnB,OAAO,GAAE,sBAA2B,GACnC,cAAc,CAOhB;AAED,eAAO,MAAM,YAAY;;;CAGxB,CAAC;AAEF,aAAK,aAAa,GAAG;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,EAAE,CAAC;IAE1B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;;;;GAIG;AAEH,iBAAS,SAAS,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,aAAa,CA4GrD;AAED;;;GAGG;AAEH,iBAAS,UAAU,CACjB,QAAQ,KAAA,EACR,aAAa,EAAE,aAAa,EAC5B,OAAO,EAAE,QAAQ,CAAC,sBAAsB,CAAC,kBAuI1C"}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TEST_EXPORTS = exports.geojsonToBinary = void 0;
|
|
4
|
+
/** Convert GeoJSON features to flat binary arrays */
|
|
5
|
+
function geojsonToBinary(features, options = {}) {
|
|
6
|
+
const firstPassData = firstPass(features);
|
|
7
|
+
return secondPass(features, firstPassData, {
|
|
8
|
+
coordLength: options.coordLength || firstPassData.coordLength,
|
|
9
|
+
numericPropKeys: options.numericPropKeys || firstPassData.numericPropKeys,
|
|
10
|
+
PositionDataType: options.PositionDataType || Float32Array
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
exports.geojsonToBinary = geojsonToBinary;
|
|
14
|
+
exports.TEST_EXPORTS = {
|
|
15
|
+
firstPass,
|
|
16
|
+
secondPass
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Initial scan over GeoJSON features
|
|
20
|
+
* Counts number of coordinates of each geometry type and
|
|
21
|
+
* keeps track of the max coordinate dimensions
|
|
22
|
+
*/
|
|
23
|
+
// eslint-disable-next-line complexity, max-statements
|
|
24
|
+
function firstPass(features) {
|
|
25
|
+
// Counts the number of _positions_, so [x, y, z] counts as one
|
|
26
|
+
let pointPositionsCount = 0;
|
|
27
|
+
let pointFeaturesCount = 0;
|
|
28
|
+
let linePositionsCount = 0;
|
|
29
|
+
let linePathsCount = 0;
|
|
30
|
+
let lineFeaturesCount = 0;
|
|
31
|
+
let polygonPositionsCount = 0;
|
|
32
|
+
let polygonObjectsCount = 0;
|
|
33
|
+
let polygonRingsCount = 0;
|
|
34
|
+
let polygonFeaturesCount = 0;
|
|
35
|
+
const coordLengths = new Set();
|
|
36
|
+
const numericPropKeys = {};
|
|
37
|
+
for (const feature of features) {
|
|
38
|
+
const geometry = feature.geometry;
|
|
39
|
+
switch (geometry.type) {
|
|
40
|
+
case 'Point':
|
|
41
|
+
pointFeaturesCount++;
|
|
42
|
+
pointPositionsCount++;
|
|
43
|
+
coordLengths.add(geometry.coordinates.length);
|
|
44
|
+
break;
|
|
45
|
+
case 'MultiPoint':
|
|
46
|
+
pointFeaturesCount++;
|
|
47
|
+
pointPositionsCount += geometry.coordinates.length;
|
|
48
|
+
for (const point of geometry.coordinates) {
|
|
49
|
+
coordLengths.add(point.length);
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case 'LineString':
|
|
53
|
+
lineFeaturesCount++;
|
|
54
|
+
linePositionsCount += geometry.coordinates.length;
|
|
55
|
+
linePathsCount++;
|
|
56
|
+
for (const coord of geometry.coordinates) {
|
|
57
|
+
coordLengths.add(coord.length);
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 'MultiLineString':
|
|
61
|
+
lineFeaturesCount++;
|
|
62
|
+
for (const line of geometry.coordinates) {
|
|
63
|
+
linePositionsCount += line.length;
|
|
64
|
+
linePathsCount++;
|
|
65
|
+
// eslint-disable-next-line max-depth
|
|
66
|
+
for (const coord of line) {
|
|
67
|
+
coordLengths.add(coord.length);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case 'Polygon':
|
|
72
|
+
polygonFeaturesCount++;
|
|
73
|
+
polygonObjectsCount++;
|
|
74
|
+
polygonRingsCount += geometry.coordinates.length;
|
|
75
|
+
polygonPositionsCount += flatten(geometry.coordinates).length;
|
|
76
|
+
for (const coord of flatten(geometry.coordinates)) {
|
|
77
|
+
coordLengths.add(coord.length);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case 'MultiPolygon':
|
|
81
|
+
polygonFeaturesCount++;
|
|
82
|
+
for (const polygon of geometry.coordinates) {
|
|
83
|
+
polygonObjectsCount++;
|
|
84
|
+
polygonRingsCount += polygon.length;
|
|
85
|
+
polygonPositionsCount += flatten(polygon).length;
|
|
86
|
+
// eslint-disable-next-line max-depth
|
|
87
|
+
for (const coord of flatten(polygon)) {
|
|
88
|
+
coordLengths.add(coord.length);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
default:
|
|
93
|
+
throw new Error(`Unsupported geometry type: ${geometry.type}`);
|
|
94
|
+
}
|
|
95
|
+
if (feature.properties) {
|
|
96
|
+
for (const key in feature.properties) {
|
|
97
|
+
const val = feature.properties[key];
|
|
98
|
+
// If property has not been seen before, or if property has been numeric
|
|
99
|
+
// in all previous features, check if numeric in this feature
|
|
100
|
+
// If not numeric, false is stored to prevent rechecking in the future
|
|
101
|
+
numericPropKeys[key] =
|
|
102
|
+
numericPropKeys[key] || numericPropKeys[key] === undefined
|
|
103
|
+
? isNumeric(val)
|
|
104
|
+
: numericPropKeys[key];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
|
|
110
|
+
pointPositionsCount,
|
|
111
|
+
pointFeaturesCount,
|
|
112
|
+
linePositionsCount,
|
|
113
|
+
linePathsCount,
|
|
114
|
+
lineFeaturesCount,
|
|
115
|
+
polygonPositionsCount,
|
|
116
|
+
polygonObjectsCount,
|
|
117
|
+
polygonRingsCount,
|
|
118
|
+
polygonFeaturesCount,
|
|
119
|
+
// Array of keys whose values are always numeric
|
|
120
|
+
numericPropKeys: Object.keys(numericPropKeys).filter((k) => numericPropKeys[k])
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Second scan over GeoJSON features
|
|
125
|
+
* Fills coordinates into pre-allocated typed arrays
|
|
126
|
+
*/
|
|
127
|
+
// eslint-disable-next-line complexity
|
|
128
|
+
function secondPass(features, firstPassData, options) {
|
|
129
|
+
const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount } = firstPassData;
|
|
130
|
+
const { coordLength, numericPropKeys, PositionDataType = Float32Array } = options;
|
|
131
|
+
const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
|
|
132
|
+
const points = {
|
|
133
|
+
// @ts-ignore Typescript doesn't like dynamic constructors
|
|
134
|
+
positions: new PositionDataType(pointPositionsCount * coordLength),
|
|
135
|
+
globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
|
|
136
|
+
featureIds: pointFeaturesCount > 65535
|
|
137
|
+
? new Uint32Array(pointPositionsCount)
|
|
138
|
+
: new Uint16Array(pointPositionsCount),
|
|
139
|
+
numericProps: {},
|
|
140
|
+
properties: Array(),
|
|
141
|
+
fields: Array()
|
|
142
|
+
};
|
|
143
|
+
const lines = {
|
|
144
|
+
// @ts-ignore Typescript doesn't like dynamic constructors
|
|
145
|
+
positions: new PositionDataType(linePositionsCount * coordLength),
|
|
146
|
+
pathIndices: linePositionsCount > 65535
|
|
147
|
+
? new Uint32Array(linePathsCount + 1)
|
|
148
|
+
: new Uint16Array(linePathsCount + 1),
|
|
149
|
+
globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
|
|
150
|
+
featureIds: lineFeaturesCount > 65535
|
|
151
|
+
? new Uint32Array(linePositionsCount)
|
|
152
|
+
: new Uint16Array(linePositionsCount),
|
|
153
|
+
numericProps: {},
|
|
154
|
+
properties: Array(),
|
|
155
|
+
fields: Array()
|
|
156
|
+
};
|
|
157
|
+
const polygons = {
|
|
158
|
+
// @ts-ignore Typescript doesn't like dynamic constructors
|
|
159
|
+
positions: new PositionDataType(polygonPositionsCount * coordLength),
|
|
160
|
+
polygonIndices: polygonPositionsCount > 65535
|
|
161
|
+
? new Uint32Array(polygonObjectsCount + 1)
|
|
162
|
+
: new Uint16Array(polygonObjectsCount + 1),
|
|
163
|
+
primitivePolygonIndices: polygonPositionsCount > 65535
|
|
164
|
+
? new Uint32Array(polygonRingsCount + 1)
|
|
165
|
+
: new Uint16Array(polygonRingsCount + 1),
|
|
166
|
+
globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
|
|
167
|
+
featureIds: polygonFeaturesCount > 65535
|
|
168
|
+
? new Uint32Array(polygonPositionsCount)
|
|
169
|
+
: new Uint16Array(polygonPositionsCount),
|
|
170
|
+
numericProps: {},
|
|
171
|
+
properties: Array(),
|
|
172
|
+
fields: Array()
|
|
173
|
+
};
|
|
174
|
+
// Instantiate numeric properties arrays; one value per vertex
|
|
175
|
+
for (const object of [points, lines, polygons]) {
|
|
176
|
+
for (const propName of numericPropKeys || []) {
|
|
177
|
+
// If property has been numeric in all previous features in which the property existed, check
|
|
178
|
+
// if numeric in this feature
|
|
179
|
+
object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Set last element of path/polygon indices as positions length
|
|
183
|
+
lines.pathIndices[linePathsCount] = linePositionsCount;
|
|
184
|
+
polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
|
|
185
|
+
polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
|
|
186
|
+
const indexMap = {
|
|
187
|
+
pointPosition: 0,
|
|
188
|
+
pointFeature: 0,
|
|
189
|
+
linePosition: 0,
|
|
190
|
+
linePath: 0,
|
|
191
|
+
lineFeature: 0,
|
|
192
|
+
polygonPosition: 0,
|
|
193
|
+
polygonObject: 0,
|
|
194
|
+
polygonRing: 0,
|
|
195
|
+
polygonFeature: 0,
|
|
196
|
+
feature: 0
|
|
197
|
+
};
|
|
198
|
+
for (const feature of features) {
|
|
199
|
+
const geometry = feature.geometry;
|
|
200
|
+
const properties = feature.properties || {};
|
|
201
|
+
switch (geometry.type) {
|
|
202
|
+
case 'Point':
|
|
203
|
+
handlePoint(geometry.coordinates, points, indexMap, coordLength, properties);
|
|
204
|
+
points.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
205
|
+
indexMap.pointFeature++;
|
|
206
|
+
break;
|
|
207
|
+
case 'MultiPoint':
|
|
208
|
+
handleMultiPoint(geometry.coordinates, points, indexMap, coordLength, properties);
|
|
209
|
+
points.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
210
|
+
indexMap.pointFeature++;
|
|
211
|
+
break;
|
|
212
|
+
case 'LineString':
|
|
213
|
+
handleLineString(geometry.coordinates, lines, indexMap, coordLength, properties);
|
|
214
|
+
lines.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
215
|
+
indexMap.lineFeature++;
|
|
216
|
+
break;
|
|
217
|
+
case 'MultiLineString':
|
|
218
|
+
handleMultiLineString(geometry.coordinates, lines, indexMap, coordLength, properties);
|
|
219
|
+
lines.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
220
|
+
indexMap.lineFeature++;
|
|
221
|
+
break;
|
|
222
|
+
case 'Polygon':
|
|
223
|
+
handlePolygon(geometry.coordinates, polygons, indexMap, coordLength, properties);
|
|
224
|
+
polygons.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
225
|
+
indexMap.polygonFeature++;
|
|
226
|
+
break;
|
|
227
|
+
case 'MultiPolygon':
|
|
228
|
+
handleMultiPolygon(geometry.coordinates, polygons, indexMap, coordLength, properties);
|
|
229
|
+
polygons.properties.push(keepStringProperties(properties, numericPropKeys));
|
|
230
|
+
indexMap.polygonFeature++;
|
|
231
|
+
break;
|
|
232
|
+
default:
|
|
233
|
+
throw new Error('Invalid geometry type');
|
|
234
|
+
}
|
|
235
|
+
indexMap.feature++;
|
|
236
|
+
}
|
|
237
|
+
// Wrap each array in an accessor object with value and size keys
|
|
238
|
+
return makeAccessorObjects(points, lines, polygons, coordLength);
|
|
239
|
+
}
|
|
240
|
+
/** Fills Point coordinates into points object of arrays */
|
|
241
|
+
function handlePoint(coords, points, indexMap, coordLength, properties) {
|
|
242
|
+
points.positions.set(coords, indexMap.pointPosition * coordLength);
|
|
243
|
+
points.globalFeatureIds[indexMap.pointPosition] = indexMap.feature;
|
|
244
|
+
points.featureIds[indexMap.pointPosition] = indexMap.pointFeature;
|
|
245
|
+
fillNumericProperties(points, properties, indexMap.pointPosition, 1);
|
|
246
|
+
indexMap.pointPosition++;
|
|
247
|
+
}
|
|
248
|
+
/** Fills MultiPoint coordinates into points object of arrays */
|
|
249
|
+
function handleMultiPoint(coords, points, indexMap, coordLength, properties) {
|
|
250
|
+
for (const point of coords) {
|
|
251
|
+
handlePoint(point, points, indexMap, coordLength, properties);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Fills LineString coordinates into lines object of arrays */
|
|
255
|
+
function handleLineString(coords, lines, indexMap, coordLength, properties) {
|
|
256
|
+
lines.pathIndices[indexMap.linePath] = indexMap.linePosition;
|
|
257
|
+
indexMap.linePath++;
|
|
258
|
+
fillCoords(lines.positions, coords, indexMap.linePosition, coordLength);
|
|
259
|
+
const nPositions = coords.length;
|
|
260
|
+
fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
|
|
261
|
+
lines.globalFeatureIds.set(new Uint32Array(nPositions).fill(indexMap.feature), indexMap.linePosition);
|
|
262
|
+
lines.featureIds.set(new Uint32Array(nPositions).fill(indexMap.lineFeature), indexMap.linePosition);
|
|
263
|
+
indexMap.linePosition += nPositions;
|
|
264
|
+
}
|
|
265
|
+
/** Fills MultiLineString coordinates into lines object of arrays */
|
|
266
|
+
function handleMultiLineString(coords, lines, indexMap, coordLength, properties) {
|
|
267
|
+
for (const line of coords) {
|
|
268
|
+
handleLineString(line, lines, indexMap, coordLength, properties);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** Fills Polygon coordinates into polygons object of arrays */
|
|
272
|
+
function handlePolygon(coords, polygons, indexMap, coordLength, properties) {
|
|
273
|
+
polygons.polygonIndices[indexMap.polygonObject] = indexMap.polygonPosition;
|
|
274
|
+
indexMap.polygonObject++;
|
|
275
|
+
for (const ring of coords) {
|
|
276
|
+
polygons.primitivePolygonIndices[indexMap.polygonRing] = indexMap.polygonPosition;
|
|
277
|
+
indexMap.polygonRing++;
|
|
278
|
+
fillCoords(polygons.positions, ring, indexMap.polygonPosition, coordLength);
|
|
279
|
+
const nPositions = ring.length;
|
|
280
|
+
fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
|
|
281
|
+
polygons.globalFeatureIds.set(new Uint32Array(nPositions).fill(indexMap.feature), indexMap.polygonPosition);
|
|
282
|
+
polygons.featureIds.set(new Uint32Array(nPositions).fill(indexMap.polygonFeature), indexMap.polygonPosition);
|
|
283
|
+
indexMap.polygonPosition += nPositions;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Fills MultiPolygon coordinates into polygons object of arrays */
|
|
287
|
+
function handleMultiPolygon(coords, polygons, indexMap, coordLength, properties) {
|
|
288
|
+
for (const polygon of coords) {
|
|
289
|
+
handlePolygon(polygon, polygons, indexMap, coordLength, properties);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/** Wrap each array in an accessor object with value and size keys */
|
|
293
|
+
function makeAccessorObjects(points, lines, polygons, coordLength) {
|
|
294
|
+
const returnObj = {
|
|
295
|
+
points: {
|
|
296
|
+
...points,
|
|
297
|
+
positions: { value: points.positions, size: coordLength },
|
|
298
|
+
globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
|
|
299
|
+
featureIds: { value: points.featureIds, size: 1 },
|
|
300
|
+
type: 'Point'
|
|
301
|
+
},
|
|
302
|
+
lines: {
|
|
303
|
+
...lines,
|
|
304
|
+
pathIndices: { value: lines.pathIndices, size: 1 },
|
|
305
|
+
positions: { value: lines.positions, size: coordLength },
|
|
306
|
+
globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
|
|
307
|
+
featureIds: { value: lines.featureIds, size: 1 },
|
|
308
|
+
type: 'LineString'
|
|
309
|
+
},
|
|
310
|
+
polygons: {
|
|
311
|
+
...polygons,
|
|
312
|
+
polygonIndices: { value: polygons.polygonIndices, size: 1 },
|
|
313
|
+
primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
|
|
314
|
+
positions: { value: polygons.positions, size: coordLength },
|
|
315
|
+
globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
|
|
316
|
+
featureIds: { value: polygons.featureIds, size: 1 },
|
|
317
|
+
type: 'Polygon'
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
for (const geomType in returnObj) {
|
|
321
|
+
for (const numericProp in returnObj[geomType].numericProps) {
|
|
322
|
+
returnObj[geomType].numericProps[numericProp] = {
|
|
323
|
+
value: returnObj[geomType].numericProps[numericProp],
|
|
324
|
+
size: 1
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return returnObj;
|
|
329
|
+
}
|
|
330
|
+
/** Add numeric properties to object */
|
|
331
|
+
function fillNumericProperties(object, properties, index, length) {
|
|
332
|
+
for (const numericPropName in object.numericProps) {
|
|
333
|
+
if (numericPropName in properties) {
|
|
334
|
+
object.numericProps[numericPropName].set(new Array(length).fill(properties[numericPropName]), index);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/** Keep string properties in object */
|
|
339
|
+
function keepStringProperties(properties, numericKeys) {
|
|
340
|
+
const props = {};
|
|
341
|
+
for (const key in properties) {
|
|
342
|
+
if (!numericKeys.includes(key)) {
|
|
343
|
+
props[key] = properties[key];
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return props;
|
|
347
|
+
}
|
|
348
|
+
/** @param coords is expected to be a list of arrays, each with length 2-3 */
|
|
349
|
+
function fillCoords(array, coords, startVertex, coordLength) {
|
|
350
|
+
let index = startVertex * coordLength;
|
|
351
|
+
for (const coord of coords) {
|
|
352
|
+
array.set(coord, index);
|
|
353
|
+
index += coordLength;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// TODO - how does this work? Different `coordinates` have different nesting
|
|
357
|
+
function flatten(arrays) {
|
|
358
|
+
return [].concat(...arrays);
|
|
359
|
+
}
|
|
360
|
+
function isNumeric(x) {
|
|
361
|
+
return Number.isFinite(x);
|
|
362
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { BinaryFeatures } from '@loaders.gl/schema';
|
|
2
|
+
declare type TransformCoordinate = (coord: number[]) => number[];
|
|
3
|
+
/**
|
|
4
|
+
* Apply transformation to every coordinate of binary features
|
|
5
|
+
* @param binaryFeatures binary features
|
|
6
|
+
* @param transformCoordinate Function to call on each coordinate
|
|
7
|
+
* @return Transformed binary features
|
|
8
|
+
*/
|
|
9
|
+
export declare function transformBinaryCoords(binaryFeatures: BinaryFeatures, transformCoordinate: TransformCoordinate): BinaryFeatures;
|
|
10
|
+
/**
|
|
11
|
+
* Apply transformation to every coordinate of GeoJSON features
|
|
12
|
+
*
|
|
13
|
+
* @param features Array of GeoJSON features
|
|
14
|
+
* @param fn Function to call on each coordinate
|
|
15
|
+
* @return Transformed GeoJSON features
|
|
16
|
+
*/
|
|
17
|
+
export declare function transformGeoJsonCoords(features: object[], fn: (coord: number[]) => number[]): object[];
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/lib/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,cAAc,EAAiB,MAAM,oBAAoB,CAAC;AAEvE,aAAK,mBAAmB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,MAAM,EAAE,CAAC;AAEzD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,cAAc,EAAE,cAAc,EAC9B,mBAAmB,EAAE,mBAAmB,GACvC,cAAc,CAWhB;AAcD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,MAAM,EAAE,EAClB,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,MAAM,EAAE,GAChC,MAAM,EAAE,CAMV"}
|