@loaders.gl/flatgeobuf 4.0.0-alpha.4 → 4.0.0-alpha.6

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.
Files changed (62) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/dist.min.js +9551 -0
  5. package/dist/es5/bundle.js +6 -0
  6. package/dist/es5/bundle.js.map +1 -0
  7. package/dist/es5/flatgeobuf-loader.js +26 -0
  8. package/dist/es5/flatgeobuf-loader.js.map +1 -0
  9. package/dist/es5/index.js +47 -0
  10. package/dist/es5/index.js.map +1 -0
  11. package/dist/es5/lib/binary-geometries.js +137 -0
  12. package/dist/es5/lib/binary-geometries.js.map +1 -0
  13. package/dist/es5/lib/parse-flatgeobuf.js +189 -0
  14. package/dist/es5/lib/parse-flatgeobuf.js.map +1 -0
  15. package/dist/es5/lib/types.js +2 -0
  16. package/dist/es5/lib/types.js.map +1 -0
  17. package/dist/es5/workers/flatgeobuf-worker.js +6 -0
  18. package/dist/es5/workers/flatgeobuf-worker.js.map +1 -0
  19. package/dist/esm/bundle.js +4 -0
  20. package/dist/esm/bundle.js.map +1 -0
  21. package/dist/esm/flatgeobuf-loader.js +18 -0
  22. package/dist/esm/flatgeobuf-loader.js.map +1 -0
  23. package/dist/esm/index.js +12 -0
  24. package/dist/esm/index.js.map +1 -0
  25. package/dist/esm/lib/binary-geometries.js +126 -0
  26. package/dist/esm/lib/binary-geometries.js.map +1 -0
  27. package/dist/esm/lib/parse-flatgeobuf.js +112 -0
  28. package/dist/esm/lib/parse-flatgeobuf.js.map +1 -0
  29. package/dist/esm/lib/types.js +2 -0
  30. package/dist/esm/lib/types.js.map +1 -0
  31. package/dist/esm/workers/flatgeobuf-worker.js +4 -0
  32. package/dist/esm/workers/flatgeobuf-worker.js.map +1 -0
  33. package/dist/flatgeobuf-loader.d.ts +18 -0
  34. package/dist/flatgeobuf-loader.d.ts.map +1 -0
  35. package/dist/flatgeobuf-loader.js +22 -15
  36. package/dist/flatgeobuf-worker.js +8814 -4235
  37. package/dist/index.d.ts +28 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +13 -10
  40. package/dist/lib/binary-geometries.d.ts +20 -0
  41. package/dist/lib/binary-geometries.d.ts.map +1 -0
  42. package/dist/lib/binary-geometries.js +120 -0
  43. package/dist/lib/parse-flatgeobuf.d.ts +8 -0
  44. package/dist/lib/parse-flatgeobuf.d.ts.map +1 -0
  45. package/dist/lib/parse-flatgeobuf.js +126 -15
  46. package/dist/lib/types.d.ts +13 -0
  47. package/dist/lib/types.d.ts.map +1 -0
  48. package/dist/lib/types.js +2 -0
  49. package/dist/workers/flatgeobuf-worker.d.ts +2 -0
  50. package/dist/workers/flatgeobuf-worker.d.ts.map +1 -0
  51. package/dist/workers/flatgeobuf-worker.js +5 -4
  52. package/package.json +12 -13
  53. package/src/flatgeobuf-loader.ts +4 -1
  54. package/src/index.ts +1 -1
  55. package/src/lib/binary-geometries.ts +144 -0
  56. package/src/lib/parse-flatgeobuf.ts +132 -10
  57. package/src/lib/types.ts +13 -0
  58. package/dist/bundle.js.map +0 -1
  59. package/dist/flatgeobuf-loader.js.map +0 -1
  60. package/dist/index.js.map +0 -1
  61. package/dist/lib/parse-flatgeobuf.js.map +0 -1
  62. package/dist/workers/flatgeobuf-worker.js.map +0 -1
@@ -0,0 +1,28 @@
1
+ import type { LoaderWithParser } from '@loaders.gl/loader-utils';
2
+ import { FlatGeobufLoader as FlatGeobufWorkerLoader } from './flatgeobuf-loader';
3
+ import { parseFlatGeobuf, parseFlatGeobufInBatches } from './lib/parse-flatgeobuf';
4
+ export { FlatGeobufWorkerLoader };
5
+ export declare const FlatGeobufLoader: {
6
+ parse: (arrayBuffer: any, options: any) => Promise<any[] | import("@loaders.gl/schema").GeoJSONRowTable | AsyncGenerator<import("flatgeobuf/lib/cjs/generic/feature").IFeature, any, unknown> | {
7
+ shape: string;
8
+ data: any[] | AsyncGenerator<import("flatgeobuf/lib/cjs/generic/feature").IFeature, any, unknown>;
9
+ }>;
10
+ parseSync: typeof parseFlatGeobuf;
11
+ parseInBatchesFromStream: typeof parseFlatGeobufInBatches;
12
+ binary: boolean;
13
+ id: string;
14
+ name: string;
15
+ module: string;
16
+ version: any;
17
+ worker: boolean;
18
+ extensions: string[];
19
+ mimeTypes: string[];
20
+ category: string;
21
+ options: {
22
+ flatgeobuf: {
23
+ shape: string;
24
+ };
25
+ };
26
+ };
27
+ export declare const _typecheckFlatGeobufLoader: LoaderWithParser;
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAC,gBAAgB,IAAI,sBAAsB,EAAC,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAC,eAAe,EAAE,wBAAwB,EAAC,MAAM,wBAAwB,CAAC;AAEjF,OAAO,EAAC,sBAAsB,EAAC,CAAC;AAEhC,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;CAM5B,CAAC;AAEF,eAAO,MAAM,0BAA0B,EAAE,gBAAmC,CAAC"}
package/dist/index.js CHANGED
@@ -1,11 +1,14 @@
1
- import { FlatGeobufLoader as FlatGeobufWorkerLoader } from './flatgeobuf-loader';
2
- import parseFlatGeobuf, { parseFlatGeobufInBatches } from './lib/parse-flatgeobuf';
3
- export { FlatGeobufWorkerLoader };
4
- export const FlatGeobufLoader = { ...FlatGeobufWorkerLoader,
5
- parse: async (arrayBuffer, options) => parseFlatGeobuf(arrayBuffer, options),
6
- parseSync: parseFlatGeobuf,
7
- parseInBatchesFromStream: parseFlatGeobufInBatches,
8
- binary: true
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._typecheckFlatGeobufLoader = exports.FlatGeobufLoader = exports.FlatGeobufWorkerLoader = void 0;
4
+ const flatgeobuf_loader_1 = require("./flatgeobuf-loader");
5
+ Object.defineProperty(exports, "FlatGeobufWorkerLoader", { enumerable: true, get: function () { return flatgeobuf_loader_1.FlatGeobufLoader; } });
6
+ const parse_flatgeobuf_1 = require("./lib/parse-flatgeobuf");
7
+ exports.FlatGeobufLoader = {
8
+ ...flatgeobuf_loader_1.FlatGeobufLoader,
9
+ parse: async (arrayBuffer, options) => (0, parse_flatgeobuf_1.parseFlatGeobuf)(arrayBuffer, options),
10
+ parseSync: parse_flatgeobuf_1.parseFlatGeobuf,
11
+ parseInBatchesFromStream: parse_flatgeobuf_1.parseFlatGeobufInBatches,
12
+ binary: true
9
13
  };
10
- export const _typecheckFlatGeobufLoader = FlatGeobufLoader;
11
- //# sourceMappingURL=index.js.map
14
+ exports._typecheckFlatGeobufLoader = exports.FlatGeobufLoader;
@@ -0,0 +1,20 @@
1
+ export declare function fromGeometry(geometry: any, type: any): {
2
+ positions: {
3
+ value: any;
4
+ size: number;
5
+ };
6
+ } | {
7
+ positions: {
8
+ value: Float64Array;
9
+ size: any;
10
+ };
11
+ primitivePolygonIndices: {
12
+ value: Uint32Array;
13
+ size: number;
14
+ };
15
+ polygonIndices: {
16
+ value: Uint32Array;
17
+ size: number;
18
+ };
19
+ };
20
+ //# sourceMappingURL=binary-geometries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binary-geometries.d.ts","sourceRoot":"","sources":["../../src/lib/binary-geometries.ts"],"names":[],"mappings":"AAgIA,wBAAgB,YAAY,CAAC,QAAQ,KAAA,EAAE,IAAI,KAAA;;;;;;;;;;;;;;;;;;EAe1C"}
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromGeometry = void 0;
4
+ const header_generated_1 = require("flatgeobuf/lib/cjs/header_generated");
5
+ // Parse Point to flat array
6
+ function parsePoint(geometry) {
7
+ const xy = geometry.xyArray();
8
+ const z = geometry.zArray();
9
+ const positions = blitArrays(xy, z);
10
+ return { positions };
11
+ }
12
+ function parseLines(geometry) {
13
+ const xy = geometry.xyArray();
14
+ const z = geometry.zArray();
15
+ const positions = blitArrays(xy, z);
16
+ // If endsArray is null, a single LineString. Otherwise, contains the end
17
+ // indices of each part of the MultiLineString. geometry.endsArray() omits the
18
+ // initial 0 that we have in our internal format.
19
+ const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];
20
+ ends.unshift(0);
21
+ const pathIndices = { value: new Uint16Array(ends), size: 1 };
22
+ return {
23
+ positions,
24
+ pathIndices
25
+ };
26
+ }
27
+ function parsePolygons(geometry) {
28
+ const xy = geometry.xyArray();
29
+ const z = geometry.zArray();
30
+ const positions = blitArrays(xy, z);
31
+ // If endsArray is null, a simple Polygon with no inner rings. Otherwise,
32
+ // contains the end indices of each ring of the Polygon. geometry.endsArray()
33
+ // omits the initial 0 that we have in our internal format.
34
+ const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];
35
+ ends.unshift(0);
36
+ const primitivePolygonIndices = { value: new Uint16Array(ends), size: 1 };
37
+ const polygonIndices = { value: new Uint16Array([0, xy.length / 2]), size: 1 };
38
+ return {
39
+ positions,
40
+ primitivePolygonIndices,
41
+ polygonIndices
42
+ };
43
+ }
44
+ // eslint-disable-next-line max-statements
45
+ function parseMultiPolygons(geometry) {
46
+ // Create arrays for each geometry part, then concatenate
47
+ const parsedParts = [];
48
+ let nPositions = 0;
49
+ let nPrimitivePolygonIndices = 1;
50
+ let nPolygonIndices = 1;
51
+ for (let i = 0; i < geometry.partsLength(); i++) {
52
+ const part = geometry.parts(i);
53
+ const polygon = parsePolygons(part);
54
+ nPositions += polygon.positions.value.length;
55
+ nPrimitivePolygonIndices += polygon.primitivePolygonIndices.value.length - 1;
56
+ nPolygonIndices += polygon.polygonIndices.value.length - 1;
57
+ parsedParts.push(polygon);
58
+ }
59
+ const concatPositions = new Float64Array(nPositions);
60
+ const concatPrimitivePolygonIndices = new Uint32Array(nPrimitivePolygonIndices);
61
+ const concatPolygonIndices = new Uint32Array(nPolygonIndices);
62
+ let positionCounter = 0;
63
+ let primitivePolygonIndicesCounter = 1;
64
+ let polygonIndicesCounter = 1;
65
+ // Assumes all parts of the multipolygon have the same size
66
+ const positionSize = parsedParts[0].positions.size;
67
+ for (const parsedPart of parsedParts) {
68
+ concatPositions.set(parsedPart.positions.value, positionCounter * positionSize);
69
+ // For indices, need to add positionCounter so that position indices are
70
+ // correct in the concatenated positions
71
+ concatPrimitivePolygonIndices.set(
72
+ // eslint-disable-next-line
73
+ parsedPart.primitivePolygonIndices.value.subarray(1).map((x) => x + positionCounter), primitivePolygonIndicesCounter);
74
+ concatPolygonIndices.set(
75
+ // eslint-disable-next-line
76
+ parsedPart.polygonIndices.value.subarray(1).map((x) => x + positionCounter), polygonIndicesCounter);
77
+ positionCounter += parsedPart.positions.value.length / positionSize;
78
+ primitivePolygonIndicesCounter += parsedPart.primitivePolygonIndices.value.length - 1;
79
+ polygonIndicesCounter += parsedPart.polygonIndices.value.length - 1;
80
+ }
81
+ return {
82
+ positions: { value: concatPositions, size: positionSize },
83
+ primitivePolygonIndices: { value: concatPrimitivePolygonIndices, size: 1 },
84
+ polygonIndices: { value: concatPolygonIndices, size: 1 }
85
+ };
86
+ }
87
+ // Combine xy and z arrays
88
+ function blitArrays(xy, z) {
89
+ if (!z) {
90
+ return { value: xy, size: 2 };
91
+ }
92
+ if (z.length * 2 !== xy.length) {
93
+ throw new Error('Z array must be half XY array\'s length');
94
+ }
95
+ const totalLength = xy.length + z.length;
96
+ const xyz = new Float64Array(totalLength);
97
+ for (let i = 0; i < xy.length / 2; i++) {
98
+ xyz[i * 3 + 0] = xy[i * 2 + 0];
99
+ xyz[i * 3 + 1] = xy[i * 2 + 1];
100
+ xyz[i * 3 + 2] = z[i];
101
+ }
102
+ return { value: xyz, size: 3 };
103
+ }
104
+ function fromGeometry(geometry, type) {
105
+ switch (type) {
106
+ case header_generated_1.GeometryType.Point:
107
+ case header_generated_1.GeometryType.MultiPoint:
108
+ return parsePoint(geometry);
109
+ case header_generated_1.GeometryType.LineString:
110
+ case header_generated_1.GeometryType.MultiLineString:
111
+ return parseLines(geometry);
112
+ case header_generated_1.GeometryType.Polygon:
113
+ return parsePolygons(geometry);
114
+ case header_generated_1.GeometryType.MultiPolygon:
115
+ return parseMultiPolygons(geometry);
116
+ default:
117
+ throw new Error(`Unimplemented geometry type: ${type}`);
118
+ }
119
+ }
120
+ exports.fromGeometry = fromGeometry;
@@ -0,0 +1,8 @@
1
+ import { FlatGeobufLoaderOptions } from './types';
2
+ import { GeoJSONRowTable } from '@loaders.gl/schema';
3
+ export declare function parseFlatGeobuf(arrayBuffer: ArrayBuffer, options?: FlatGeobufLoaderOptions): any[] | GeoJSONRowTable | AsyncGenerator<import("flatgeobuf/lib/cjs/generic/feature").IFeature, any, unknown> | {
4
+ shape: string;
5
+ data: any[] | AsyncGenerator<import("flatgeobuf/lib/cjs/generic/feature").IFeature, any, unknown>;
6
+ };
7
+ export declare function parseFlatGeobufInBatches(stream: any, options: FlatGeobufLoaderOptions): any[] | AsyncGenerator<import("flatgeobuf/lib/cjs/generic/feature").IFeature, any, unknown> | AsyncGenerator<any, void, unknown>;
8
+ //# sourceMappingURL=parse-flatgeobuf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-flatgeobuf.d.ts","sourceRoot":"","sources":["../../src/lib/parse-flatgeobuf.ts"],"names":[],"mappings":"AASA,OAAO,EAAC,uBAAuB,EAAC,MAAM,SAAS,CAAC;AAChD,OAAO,EAAC,eAAe,EAAU,MAAM,oBAAoB,CAAC;AAwB5D,wBAAgB,eAAe,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,uBAAuB;;;EAoB1F;AAoDD,wBAAgB,wBAAwB,CAAC,MAAM,KAAA,EAAE,OAAO,EAAE,uBAAuB,oIAMhF"}
@@ -1,17 +1,128 @@
1
- import { deserialize, deserializeStream } from 'flatgeobuf/dist/flatgeobuf-geojson.min';
2
- export default function parseFlatGeobuf(input, options) {
3
- if (input.byteLength === 0) {
4
- return [];
5
- }
6
-
7
- const arr = new Uint8Array(input);
8
- const {
9
- features
10
- } = deserialize(arr);
11
- return features;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseFlatGeobufInBatches = exports.parseFlatGeobuf = void 0;
4
+ // @ts-nocheck
5
+ const proj4_1 = require("@math.gl/proj4");
6
+ const gis_1 = require("@loaders.gl/gis");
7
+ const geojson_1 = require("flatgeobuf/lib/cjs/geojson");
8
+ const generic_1 = require("flatgeobuf/lib/cjs/generic");
9
+ const feature_1 = require("flatgeobuf/lib/cjs/generic/feature");
10
+ const binary_geometries_1 = require("./binary-geometries");
11
+ // TODO: reproject binary features
12
+ function binaryFromFeature(feature, header) {
13
+ const geometry = feature.geometry();
14
+ // FlatGeobuf files can only hold a single geometry type per file, otherwise
15
+ // GeometryType is GeometryCollection
16
+ // I believe geometry.type() is null (0) except when the geometry type isn't
17
+ // known in the header?
18
+ const geometryType = header.geometryType || geometry.type();
19
+ const parsedGeometry = (0, binary_geometries_1.fromGeometry)(geometry, geometryType);
20
+ parsedGeometry.properties = (0, feature_1.parseProperties)(feature, header.columns);
21
+ // TODO: wrap binary data either in points, lines, or polygons key
22
+ return parsedGeometry;
12
23
  }
13
- export function parseFlatGeobufInBatches(stream, options) {
14
- const iterator = deserializeStream(stream);
15
- return iterator;
24
+ /*
25
+ * Parse FlatGeobuf arrayBuffer and return GeoJSON.
26
+ *
27
+ * @param arrayBuffer A FlatGeobuf arrayBuffer
28
+ * @return A GeoJSON geometry object
29
+ */
30
+ function parseFlatGeobuf(arrayBuffer, options) {
31
+ const shape = options?.gis?.format || options?.flatgeobuf?.shape;
32
+ switch (shape) {
33
+ case 'geojson-row-table': {
34
+ const table = {
35
+ shape: 'geojson-row-table',
36
+ data: parseFlatGeobufToGeoJSON(arrayBuffer, options)
37
+ };
38
+ return table;
39
+ }
40
+ case 'columnar-table': // binary + some JS arrays
41
+ return { shape: 'columnar-table', data: parseFlatGeobufToBinary(arrayBuffer, options) };
42
+ case 'geojson':
43
+ return parseFlatGeobufToGeoJSON(arrayBuffer, options);
44
+ case 'binary':
45
+ return parseFlatGeobufToBinary(arrayBuffer, options);
46
+ default:
47
+ throw new Error(shape);
48
+ }
49
+ }
50
+ exports.parseFlatGeobuf = parseFlatGeobuf;
51
+ function parseFlatGeobufToBinary(arrayBuffer, options) {
52
+ // TODO: reproject binary features
53
+ // const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};
54
+ const array = new Uint8Array(arrayBuffer);
55
+ return (0, generic_1.deserialize)(array, binaryFromFeature);
56
+ }
57
+ function parseFlatGeobufToGeoJSON(arrayBuffer, options) {
58
+ if (arrayBuffer.byteLength === 0) {
59
+ return [];
60
+ }
61
+ const { reproject = false, _targetCrs = 'WGS84' } = (options && options.gis) || {};
62
+ const arr = new Uint8Array(arrayBuffer);
63
+ let headerMeta;
64
+ const { features } = (0, geojson_1.deserialize)(arr, false, (header) => {
65
+ headerMeta = header;
66
+ });
67
+ const crs = headerMeta && headerMeta.crs;
68
+ let projection;
69
+ if (reproject && crs) {
70
+ // Constructing the projection may fail for some invalid WKT strings
71
+ try {
72
+ projection = new proj4_1.Proj4Projection({ from: crs.wkt, to: _targetCrs });
73
+ }
74
+ catch (e) {
75
+ // no op
76
+ }
77
+ }
78
+ if (projection) {
79
+ return (0, gis_1.transformGeoJsonCoords)(features, (coords) => projection.project(coords));
80
+ }
81
+ return features;
82
+ }
83
+ /*
84
+ * Parse FlatGeobuf arrayBuffer and return GeoJSON.
85
+ *
86
+ * @param {ReadableStream} _ A FlatGeobuf arrayBuffer
87
+ * @return A GeoJSON geometry object iterator
88
+ */
89
+ // eslint-disable-next-line complexity
90
+ function parseFlatGeobufInBatches(stream, options) {
91
+ if (options && options.gis && options.gis.format === 'binary') {
92
+ return parseFlatGeobufInBatchesToBinary(stream, options);
93
+ }
94
+ return parseFlatGeobufInBatchesToGeoJSON(stream, options);
95
+ }
96
+ exports.parseFlatGeobufInBatches = parseFlatGeobufInBatches;
97
+ function parseFlatGeobufInBatchesToBinary(stream, options) {
98
+ // TODO: reproject binary streaming features
99
+ // const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};
100
+ const iterator = (0, generic_1.deserialize)(stream, binaryFromFeature);
101
+ return iterator;
102
+ }
103
+ // eslint-disable-next-line complexity
104
+ async function* parseFlatGeobufInBatchesToGeoJSON(stream, options) {
105
+ const { reproject = false, _targetCrs = 'WGS84' } = (options && options.gis) || {};
106
+ let headerMeta;
107
+ const iterator = (0, geojson_1.deserialize)(stream, false, (header) => {
108
+ headerMeta = header;
109
+ });
110
+ let projection;
111
+ let firstRecord = true;
112
+ for await (const feature of iterator) {
113
+ if (firstRecord) {
114
+ const crs = headerMeta && headerMeta.crs;
115
+ if (reproject && crs) {
116
+ projection = new proj4_1.Proj4Projection({ from: crs.wkt, to: _targetCrs });
117
+ }
118
+ firstRecord = false;
119
+ }
120
+ if (reproject && projection) {
121
+ // eslint-disable-next-line
122
+ yield (0, gis_1.transformGeoJsonCoords)([feature], (coords) => projection.project(coords));
123
+ }
124
+ else {
125
+ yield feature;
126
+ }
127
+ }
16
128
  }
17
- //# sourceMappingURL=parse-flatgeobuf.js.map
@@ -0,0 +1,13 @@
1
+ import type { LoaderOptions } from '@loaders.gl/loader-utils';
2
+ export type FlatGeobufLoaderOptions = LoaderOptions & {
3
+ flatgeobuf?: {
4
+ shape?: 'geojson-row-table' | 'columnar-table' | 'geojson' | 'binary';
5
+ };
6
+ gis?: {
7
+ reproject?: boolean;
8
+ _targetCrs?: string;
9
+ /** @deprecated Use options.flatgeobuf.shape */
10
+ format?: 'geojson-row-table' | 'columnar-table' | 'geojson' | 'binary';
11
+ };
12
+ };
13
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAE5D,MAAM,MAAM,uBAAuB,GAAG,aAAa,GAAG;IACpD,UAAU,CAAC,EAAE;QACX,KAAK,CAAC,EAAE,mBAAmB,GAAG,gBAAgB,GAAG,SAAS,GAAG,QAAQ,CAAC;KACvE,CAAC;IACF,GAAG,CAAC,EAAE;QACJ,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,+CAA+C;QAC/C,MAAM,CAAC,EAAE,mBAAmB,GAAG,gBAAgB,GAAG,SAAS,GAAG,QAAQ,CAAC;KACxE,CAAC;CACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=flatgeobuf-worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flatgeobuf-worker.d.ts","sourceRoot":"","sources":["../../src/workers/flatgeobuf-worker.ts"],"names":[],"mappings":""}
@@ -1,4 +1,5 @@
1
- import { createLoaderWorker } from '@loaders.gl/loader-utils';
2
- import { FlatGeobufLoader } from '../index';
3
- createLoaderWorker(FlatGeobufLoader);
4
- //# sourceMappingURL=flatgeobuf-worker.js.map
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
4
+ const index_1 = require("../index");
5
+ (0, loader_utils_1.createLoaderWorker)(index_1.FlatGeobufLoader);
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@loaders.gl/flatgeobuf",
3
3
  "description": "Loader for FlatGeobuf",
4
- "version": "4.0.0-alpha.4",
4
+ "version": "4.0.0-alpha.6",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/uber-web/loaders.gl"
11
+ "url": "https://github.com/visgl/loaders.gl"
12
12
  },
13
13
  "keywords": [
14
14
  "geometry",
@@ -17,9 +17,9 @@
17
17
  "MVT",
18
18
  "Mapbox Vector Tiles"
19
19
  ],
20
- "types": "src/index.ts",
21
- "main": "dist/index.js",
22
- "module": "dist/index.js",
20
+ "types": "dist/index.d.ts",
21
+ "main": "dist/es5/index.js",
22
+ "module": "dist/esm/index.js",
23
23
  "sideEffects": false,
24
24
  "files": [
25
25
  "src",
@@ -28,15 +28,14 @@
28
28
  ],
29
29
  "scripts": {
30
30
  "pre-build": "npm run build-worker && npm run build-worker --env.dev && npm run build-bundle",
31
- "build-bundle": "esbuild src/bundle.ts --bundle --outfile=dist/bundle.js",
32
- "build-worker": "esbuild src/workers/flatgeobuf-worker.ts --bundle --outfile=dist/flatgeobuf-worker.js"
31
+ "build-bundle": "esbuild src/bundle.ts --bundle --outfile=dist/dist.min.js",
32
+ "build-worker": "esbuild src/workers/flatgeobuf-worker.ts --bundle --outfile=dist/flatgeobuf-worker.js --define:__VERSION__=\\\"$npm_package_version\\\""
33
33
  },
34
34
  "dependencies": {
35
- "@loaders.gl/loader-utils": "4.0.0-alpha.4",
36
- "flatgeobuf": "~3.1.0"
35
+ "@loaders.gl/gis": "4.0.0-alpha.6",
36
+ "@loaders.gl/loader-utils": "4.0.0-alpha.6",
37
+ "@math.gl/proj4": "^3.3.1",
38
+ "flatgeobuf": "3.6.5"
37
39
  },
38
- "devDependencies": {
39
- "esm": "^3.2.25"
40
- },
41
- "gitHead": "53026061b3c8871f7e96d3a5826125cc6613bddc"
40
+ "gitHead": "acc1985050dfaa0f1f0c066f8da5bce7454a046c"
42
41
  }
@@ -14,7 +14,10 @@ export const FlatGeobufLoader = {
14
14
  mimeTypes: ['application/octet-stream'],
15
15
  category: 'geometry',
16
16
  options: {
17
- flatgeobuf: {}
17
+ flatgeobuf: {
18
+ // Set to GeoJSON for backwards compatibility
19
+ shape: 'geojson'
20
+ }
18
21
  }
19
22
  };
20
23
 
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type {LoaderWithParser} from '@loaders.gl/loader-utils';
2
2
  import {FlatGeobufLoader as FlatGeobufWorkerLoader} from './flatgeobuf-loader';
3
- import parseFlatGeobuf, {parseFlatGeobufInBatches} from './lib/parse-flatgeobuf';
3
+ import {parseFlatGeobuf, parseFlatGeobufInBatches} from './lib/parse-flatgeobuf';
4
4
 
5
5
  export {FlatGeobufWorkerLoader};
6
6
 
@@ -0,0 +1,144 @@
1
+ import {GeometryType} from 'flatgeobuf/lib/cjs/header_generated';
2
+
3
+ // Parse Point to flat array
4
+ function parsePoint(geometry) {
5
+ const xy = geometry.xyArray();
6
+ const z = geometry.zArray();
7
+ const positions = blitArrays(xy, z);
8
+ return {positions};
9
+ }
10
+
11
+ function parseLines(geometry) {
12
+ const xy = geometry.xyArray();
13
+ const z = geometry.zArray();
14
+ const positions = blitArrays(xy, z);
15
+
16
+ // If endsArray is null, a single LineString. Otherwise, contains the end
17
+ // indices of each part of the MultiLineString. geometry.endsArray() omits the
18
+ // initial 0 that we have in our internal format.
19
+ const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];
20
+ ends.unshift(0);
21
+
22
+ const pathIndices = {value: new Uint16Array(ends), size: 1};
23
+
24
+ return {
25
+ positions,
26
+ pathIndices
27
+ };
28
+ }
29
+
30
+ function parsePolygons(geometry) {
31
+ const xy = geometry.xyArray();
32
+ const z = geometry.zArray();
33
+ const positions = blitArrays(xy, z);
34
+
35
+ // If endsArray is null, a simple Polygon with no inner rings. Otherwise,
36
+ // contains the end indices of each ring of the Polygon. geometry.endsArray()
37
+ // omits the initial 0 that we have in our internal format.
38
+ const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];
39
+ ends.unshift(0);
40
+
41
+ const primitivePolygonIndices = {value: new Uint16Array(ends), size: 1};
42
+ const polygonIndices = {value: new Uint16Array([0, xy.length / 2]), size: 1};
43
+
44
+ return {
45
+ positions,
46
+ primitivePolygonIndices,
47
+ polygonIndices
48
+ };
49
+ }
50
+
51
+ // eslint-disable-next-line max-statements
52
+ function parseMultiPolygons(geometry) {
53
+ // Create arrays for each geometry part, then concatenate
54
+ const parsedParts: any[] = [];
55
+ let nPositions = 0;
56
+ let nPrimitivePolygonIndices = 1;
57
+ let nPolygonIndices = 1;
58
+
59
+ for (let i = 0; i < geometry.partsLength(); i++) {
60
+ const part = geometry.parts(i);
61
+ const polygon = parsePolygons(part);
62
+
63
+ nPositions += polygon.positions.value.length;
64
+ nPrimitivePolygonIndices += polygon.primitivePolygonIndices.value.length - 1;
65
+ nPolygonIndices += polygon.polygonIndices.value.length - 1;
66
+
67
+ parsedParts.push(polygon);
68
+ }
69
+
70
+ const concatPositions = new Float64Array(nPositions);
71
+ const concatPrimitivePolygonIndices = new Uint32Array(nPrimitivePolygonIndices);
72
+ const concatPolygonIndices = new Uint32Array(nPolygonIndices);
73
+
74
+ let positionCounter = 0;
75
+ let primitivePolygonIndicesCounter = 1;
76
+ let polygonIndicesCounter = 1;
77
+
78
+ // Assumes all parts of the multipolygon have the same size
79
+ const positionSize = parsedParts[0].positions.size;
80
+
81
+ for (const parsedPart of parsedParts) {
82
+ concatPositions.set(parsedPart.positions.value, positionCounter * positionSize);
83
+
84
+ // For indices, need to add positionCounter so that position indices are
85
+ // correct in the concatenated positions
86
+ concatPrimitivePolygonIndices.set(
87
+ // eslint-disable-next-line
88
+ parsedPart.primitivePolygonIndices.value.subarray(1).map((x) => x + positionCounter),
89
+ primitivePolygonIndicesCounter
90
+ );
91
+ concatPolygonIndices.set(
92
+ // eslint-disable-next-line
93
+ parsedPart.polygonIndices.value.subarray(1).map((x) => x + positionCounter),
94
+ polygonIndicesCounter
95
+ );
96
+
97
+ positionCounter += parsedPart.positions.value.length / positionSize;
98
+ primitivePolygonIndicesCounter += parsedPart.primitivePolygonIndices.value.length - 1;
99
+ polygonIndicesCounter += parsedPart.polygonIndices.value.length - 1;
100
+ }
101
+
102
+ return {
103
+ positions: {value: concatPositions, size: positionSize},
104
+ primitivePolygonIndices: {value: concatPrimitivePolygonIndices, size: 1},
105
+ polygonIndices: {value: concatPolygonIndices, size: 1}
106
+ };
107
+ }
108
+
109
+ // Combine xy and z arrays
110
+ function blitArrays(xy, z) {
111
+ if (!z) {
112
+ return {value: xy, size: 2};
113
+ }
114
+
115
+ if (z.length * 2 !== xy.length) {
116
+ throw new Error('Z array must be half XY array\'s length');
117
+ }
118
+ const totalLength = xy.length + z.length;
119
+
120
+ const xyz = new Float64Array(totalLength);
121
+ for (let i = 0; i < xy.length / 2; i++) {
122
+ xyz[i * 3 + 0] = xy[i * 2 + 0];
123
+ xyz[i * 3 + 1] = xy[i * 2 + 1];
124
+ xyz[i * 3 + 2] = z[i];
125
+ }
126
+ return {value: xyz, size: 3};
127
+ }
128
+
129
+ export function fromGeometry(geometry, type) {
130
+ switch (type) {
131
+ case GeometryType.Point:
132
+ case GeometryType.MultiPoint:
133
+ return parsePoint(geometry);
134
+ case GeometryType.LineString:
135
+ case GeometryType.MultiLineString:
136
+ return parseLines(geometry);
137
+ case GeometryType.Polygon:
138
+ return parsePolygons(geometry);
139
+ case GeometryType.MultiPolygon:
140
+ return parseMultiPolygons(geometry);
141
+ default:
142
+ throw new Error(`Unimplemented geometry type: ${type}`);
143
+ }
144
+ }