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

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.
@@ -0,0 +1,23 @@
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>;
7
+ parseSync: typeof parseFlatGeobuf;
8
+ parseInBatchesFromStream: typeof parseFlatGeobufInBatches;
9
+ binary: boolean;
10
+ id: string;
11
+ name: string;
12
+ module: string;
13
+ version: any;
14
+ worker: boolean;
15
+ extensions: string[];
16
+ mimeTypes: string[];
17
+ category: string;
18
+ options: {
19
+ flatgeobuf: {};
20
+ };
21
+ };
22
+ export declare const _typecheckFlatGeobufLoader: LoaderWithParser;
23
+ //# 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,5 +1,5 @@
1
1
  import { FlatGeobufLoader as FlatGeobufWorkerLoader } from './flatgeobuf-loader';
2
- import parseFlatGeobuf, { parseFlatGeobufInBatches } from './lib/parse-flatgeobuf';
2
+ import { parseFlatGeobuf, parseFlatGeobufInBatches } from './lib/parse-flatgeobuf';
3
3
  export { FlatGeobufWorkerLoader };
4
4
  export const FlatGeobufLoader = { ...FlatGeobufWorkerLoader,
5
5
  parse: async (arrayBuffer, options) => parseFlatGeobuf(arrayBuffer, options),
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["FlatGeobufLoader","FlatGeobufWorkerLoader","parseFlatGeobuf","parseFlatGeobufInBatches","parse","arrayBuffer","options","parseSync","parseInBatchesFromStream","binary","_typecheckFlatGeobufLoader"],"mappings":"AACA,SAAQA,gBAAgB,IAAIC,sBAA5B,QAAyD,qBAAzD;AACA,OAAOC,eAAP,IAAyBC,wBAAzB,QAAwD,wBAAxD;AAEA,SAAQF,sBAAR;AAEA,OAAO,MAAMD,gBAAgB,GAAG,EAC9B,GAAGC,sBAD2B;AAE9BG,EAAAA,KAAK,EAAE,OAAOC,WAAP,EAAoBC,OAApB,KAAgCJ,eAAe,CAACG,WAAD,EAAcC,OAAd,CAFxB;AAG9BC,EAAAA,SAAS,EAAEL,eAHmB;AAI9BM,EAAAA,wBAAwB,EAAEL,wBAJI;AAK9BM,EAAAA,MAAM,EAAE;AALsB,CAAzB;AAQP,OAAO,MAAMC,0BAA4C,GAAGV,gBAArD","sourcesContent":["import type {LoaderWithParser} from '@loaders.gl/loader-utils';\nimport {FlatGeobufLoader as FlatGeobufWorkerLoader} from './flatgeobuf-loader';\nimport parseFlatGeobuf, {parseFlatGeobufInBatches} from './lib/parse-flatgeobuf';\n\nexport {FlatGeobufWorkerLoader};\n\nexport const FlatGeobufLoader = {\n ...FlatGeobufWorkerLoader,\n parse: async (arrayBuffer, options) => parseFlatGeobuf(arrayBuffer, options),\n parseSync: parseFlatGeobuf,\n parseInBatchesFromStream: parseFlatGeobufInBatches,\n binary: true\n};\n\nexport const _typecheckFlatGeobufLoader: LoaderWithParser = FlatGeobufLoader;\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../src/index.ts"],"names":["FlatGeobufLoader","FlatGeobufWorkerLoader","parseFlatGeobuf","parseFlatGeobufInBatches","parse","arrayBuffer","options","parseSync","parseInBatchesFromStream","binary","_typecheckFlatGeobufLoader"],"mappings":"AACA,SAAQA,gBAAgB,IAAIC,sBAA5B,QAAyD,qBAAzD;AACA,SAAQC,eAAR,EAAyBC,wBAAzB,QAAwD,wBAAxD;AAEA,SAAQF,sBAAR;AAEA,OAAO,MAAMD,gBAAgB,GAAG,EAC9B,GAAGC,sBAD2B;AAE9BG,EAAAA,KAAK,EAAE,OAAOC,WAAP,EAAoBC,OAApB,KAAgCJ,eAAe,CAACG,WAAD,EAAcC,OAAd,CAFxB;AAG9BC,EAAAA,SAAS,EAAEL,eAHmB;AAI9BM,EAAAA,wBAAwB,EAAEL,wBAJI;AAK9BM,EAAAA,MAAM,EAAE;AALsB,CAAzB;AAQP,OAAO,MAAMC,0BAA4C,GAAGV,gBAArD","sourcesContent":["import type {LoaderWithParser} from '@loaders.gl/loader-utils';\nimport {FlatGeobufLoader as FlatGeobufWorkerLoader} from './flatgeobuf-loader';\nimport {parseFlatGeobuf, parseFlatGeobufInBatches} from './lib/parse-flatgeobuf';\n\nexport {FlatGeobufWorkerLoader};\n\nexport const FlatGeobufLoader = {\n ...FlatGeobufWorkerLoader,\n parse: async (arrayBuffer, options) => parseFlatGeobuf(arrayBuffer, options),\n parseSync: parseFlatGeobuf,\n parseInBatchesFromStream: parseFlatGeobufInBatches,\n binary: true\n};\n\nexport const _typecheckFlatGeobufLoader: LoaderWithParser = FlatGeobufLoader;\n"],"file":"index.js"}
@@ -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,144 @@
1
+ import { GeometryType } from 'flatgeobuf/lib/cjs/header_generated';
2
+
3
+ function parsePoint(geometry) {
4
+ const xy = geometry.xyArray();
5
+ const z = geometry.zArray();
6
+ const positions = blitArrays(xy, z);
7
+ return {
8
+ positions
9
+ };
10
+ }
11
+
12
+ function parseLines(geometry) {
13
+ const xy = geometry.xyArray();
14
+ const z = geometry.zArray();
15
+ const positions = blitArrays(xy, z);
16
+ const ends = geometry.endsArray() && Array.from(geometry.endsArray()) || [xy.length / 2];
17
+ ends.unshift(0);
18
+ const pathIndices = {
19
+ value: new Uint16Array(ends),
20
+ size: 1
21
+ };
22
+ return {
23
+ positions,
24
+ pathIndices
25
+ };
26
+ }
27
+
28
+ function parsePolygons(geometry) {
29
+ const xy = geometry.xyArray();
30
+ const z = geometry.zArray();
31
+ const positions = blitArrays(xy, z);
32
+ const ends = geometry.endsArray() && Array.from(geometry.endsArray()) || [xy.length / 2];
33
+ ends.unshift(0);
34
+ const primitivePolygonIndices = {
35
+ value: new Uint16Array(ends),
36
+ size: 1
37
+ };
38
+ const polygonIndices = {
39
+ value: new Uint16Array([0, xy.length / 2]),
40
+ size: 1
41
+ };
42
+ return {
43
+ positions,
44
+ primitivePolygonIndices,
45
+ polygonIndices
46
+ };
47
+ }
48
+
49
+ function parseMultiPolygons(geometry) {
50
+ const parsedParts = [];
51
+ let nPositions = 0;
52
+ let nPrimitivePolygonIndices = 1;
53
+ let nPolygonIndices = 1;
54
+
55
+ for (let i = 0; i < geometry.partsLength(); i++) {
56
+ const part = geometry.parts(i);
57
+ const polygon = parsePolygons(part);
58
+ nPositions += polygon.positions.value.length;
59
+ nPrimitivePolygonIndices += polygon.primitivePolygonIndices.value.length - 1;
60
+ nPolygonIndices += polygon.polygonIndices.value.length - 1;
61
+ parsedParts.push(polygon);
62
+ }
63
+
64
+ const concatPositions = new Float64Array(nPositions);
65
+ const concatPrimitivePolygonIndices = new Uint32Array(nPrimitivePolygonIndices);
66
+ const concatPolygonIndices = new Uint32Array(nPolygonIndices);
67
+ let positionCounter = 0;
68
+ let primitivePolygonIndicesCounter = 1;
69
+ let polygonIndicesCounter = 1;
70
+ const positionSize = parsedParts[0].positions.size;
71
+
72
+ for (const parsedPart of parsedParts) {
73
+ concatPositions.set(parsedPart.positions.value, positionCounter * positionSize);
74
+ concatPrimitivePolygonIndices.set(parsedPart.primitivePolygonIndices.value.subarray(1).map(x => x + positionCounter), primitivePolygonIndicesCounter);
75
+ concatPolygonIndices.set(parsedPart.polygonIndices.value.subarray(1).map(x => x + positionCounter), polygonIndicesCounter);
76
+ positionCounter += parsedPart.positions.value.length / positionSize;
77
+ primitivePolygonIndicesCounter += parsedPart.primitivePolygonIndices.value.length - 1;
78
+ polygonIndicesCounter += parsedPart.polygonIndices.value.length - 1;
79
+ }
80
+
81
+ return {
82
+ positions: {
83
+ value: concatPositions,
84
+ size: positionSize
85
+ },
86
+ primitivePolygonIndices: {
87
+ value: concatPrimitivePolygonIndices,
88
+ size: 1
89
+ },
90
+ polygonIndices: {
91
+ value: concatPolygonIndices,
92
+ size: 1
93
+ }
94
+ };
95
+ }
96
+
97
+ function blitArrays(xy, z) {
98
+ if (!z) {
99
+ return {
100
+ value: xy,
101
+ size: 2
102
+ };
103
+ }
104
+
105
+ if (z.length * 2 !== xy.length) {
106
+ throw new Error('Z array must be half XY array\'s length');
107
+ }
108
+
109
+ const totalLength = xy.length + z.length;
110
+ const xyz = new Float64Array(totalLength);
111
+
112
+ for (let i = 0; i < xy.length / 2; i++) {
113
+ xyz[i * 3 + 0] = xy[i * 2 + 0];
114
+ xyz[i * 3 + 1] = xy[i * 2 + 1];
115
+ xyz[i * 3 + 2] = z[i];
116
+ }
117
+
118
+ return {
119
+ value: xyz,
120
+ size: 3
121
+ };
122
+ }
123
+
124
+ export function fromGeometry(geometry, type) {
125
+ switch (type) {
126
+ case GeometryType.Point:
127
+ case GeometryType.MultiPoint:
128
+ return parsePoint(geometry);
129
+
130
+ case GeometryType.LineString:
131
+ case GeometryType.MultiLineString:
132
+ return parseLines(geometry);
133
+
134
+ case GeometryType.Polygon:
135
+ return parsePolygons(geometry);
136
+
137
+ case GeometryType.MultiPolygon:
138
+ return parseMultiPolygons(geometry);
139
+
140
+ default:
141
+ throw new Error("Unimplemented geometry type: ".concat(type));
142
+ }
143
+ }
144
+ //# sourceMappingURL=binary-geometries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/binary-geometries.ts"],"names":["GeometryType","parsePoint","geometry","xy","xyArray","z","zArray","positions","blitArrays","parseLines","ends","endsArray","Array","from","length","unshift","pathIndices","value","Uint16Array","size","parsePolygons","primitivePolygonIndices","polygonIndices","parseMultiPolygons","parsedParts","nPositions","nPrimitivePolygonIndices","nPolygonIndices","i","partsLength","part","parts","polygon","push","concatPositions","Float64Array","concatPrimitivePolygonIndices","Uint32Array","concatPolygonIndices","positionCounter","primitivePolygonIndicesCounter","polygonIndicesCounter","positionSize","parsedPart","set","subarray","map","x","Error","totalLength","xyz","fromGeometry","type","Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"],"mappings":"AAAA,SAAQA,YAAR,QAA2B,qCAA3B;;AAGA,SAASC,UAAT,CAAoBC,QAApB,EAA8B;AAC5B,QAAMC,EAAE,GAAGD,QAAQ,CAACE,OAAT,EAAX;AACA,QAAMC,CAAC,GAAGH,QAAQ,CAACI,MAAT,EAAV;AACA,QAAMC,SAAS,GAAGC,UAAU,CAACL,EAAD,EAAKE,CAAL,CAA5B;AACA,SAAO;AAACE,IAAAA;A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AACA,SAAKvD,YAAY,CAACwD,eAAlB;AACE,aAAO/C,UAAU,CAACP,QAAD,CAAjB;;AACF,SAAKF,YAAY,CAACyD,OAAlB;AACE,aAAOrC,aAAa,CAAClB,QAAD,CAApB;;AACF,SAAKF,YAAY,CAAC0D,YAAlB;AACE,aAAOnC,kBAAkB,CAACrB,QAAD,CAAzB;;AACF;AACE,YAAM,IAAI8C,KAAJ,wCAA0CI,IAA1C,EAAN;AAZJ;AAcD","sourcesContent":["import {GeometryType} from 'flatgeobuf/lib/cjs/header_generated';\n\n// Parse Point to flat array\nfunction parsePoint(geometry) {\n const xy = geometry.xyArray();\n const z = geometry.zArray();\n const positions = blitArrays(xy, z);\n return {positions};\n}\n\nfunction parseLines(geometry) {\n const xy = geometry.xyArray();\n const z = geometry.zArray();\n const positions = blitArrays(xy, z);\n\n // If endsArray is null, a single LineString. Otherwise, contains the end\n // indices of each part of the MultiLineString. geometry.endsArray() omits the\n // initial 0 that we have in our internal format.\n const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];\n ends.unshift(0);\n\n const pathIndices = {value: new Uint16Array(ends), size: 1};\n\n return {\n positions,\n pathIndices\n };\n}\n\nfunction parsePolygons(geometry) {\n const xy = geometry.xyArray();\n const z = geometry.zArray();\n const positions = blitArrays(xy, z);\n\n // If endsArray is null, a simple Polygon with no inner rings. Otherwise,\n // contains the end indices of each ring of the Polygon. geometry.endsArray()\n // omits the initial 0 that we have in our internal format.\n const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];\n ends.unshift(0);\n\n const primitivePolygonIndices = {value: new Uint16Array(ends), size: 1};\n const polygonIndices = {value: new Uint16Array([0, xy.length / 2]), size: 1};\n\n return {\n positions,\n primitivePolygonIndices,\n polygonIndices\n };\n}\n\n// eslint-disable-next-line max-statements\nfunction parseMultiPolygons(geometry) {\n // Create arrays for each geometry part, then concatenate\n const parsedParts: any[] = [];\n let nPositions = 0;\n let nPrimitivePolygonIndices = 1;\n let nPolygonIndices = 1;\n\n for (let i = 0; i < geometry.partsLength(); i++) {\n const part = geometry.parts(i);\n const polygon = parsePolygons(part);\n\n nPositions += polygon.positions.value.length;\n nPrimitivePolygonIndices += polygon.primitivePolygonIndices.value.length - 1;\n nPolygonIndices += polygon.polygonIndices.value.length - 1;\n\n parsedParts.push(polygon);\n }\n\n const concatPositions = new Float64Array(nPositions);\n const concatPrimitivePolygonIndices = new Uint32Array(nPrimitivePolygonIndices);\n const concatPolygonIndices = new Uint32Array(nPolygonIndices);\n\n let positionCounter = 0;\n let primitivePolygonIndicesCounter = 1;\n let polygonIndicesCounter = 1;\n\n // Assumes all parts of the multipolygon have the same size\n const positionSize = parsedParts[0].positions.size;\n\n for (const parsedPart of parsedParts) {\n concatPositions.set(parsedPart.positions.value, positionCounter * positionSize);\n\n // For indices, need to add positionCounter so that position indices are\n // correct in the concatenated positions\n concatPrimitivePolygonIndices.set(\n // eslint-disable-next-line\n parsedPart.primitivePolygonIndices.value.subarray(1).map((x) => x + positionCounter),\n primitivePolygonIndicesCounter\n );\n concatPolygonIndices.set(\n // eslint-disable-next-line\n parsedPart.polygonIndices.value.subarray(1).map((x) => x + positionCounter),\n polygonIndicesCounter\n );\n\n positionCounter += parsedPart.positions.value.length / positionSize;\n primitivePolygonIndicesCounter += parsedPart.primitivePolygonIndices.value.length - 1;\n polygonIndicesCounter += parsedPart.polygonIndices.value.length - 1;\n }\n\n return {\n positions: {value: concatPositions, size: positionSize},\n primitivePolygonIndices: {value: concatPrimitivePolygonIndices, size: 1},\n polygonIndices: {value: concatPolygonIndices, size: 1}\n };\n}\n\n// Combine xy and z arrays\nfunction blitArrays(xy, z) {\n if (!z) {\n return {value: xy, size: 2};\n }\n\n if (z.length * 2 !== xy.length) {\n throw new Error('Z array must be half XY array\\'s length');\n }\n const totalLength = xy.length + z.length;\n\n const xyz = new Float64Array(totalLength);\n for (let i = 0; i < xy.length / 2; i++) {\n xyz[i * 3 + 0] = xy[i * 2 + 0];\n xyz[i * 3 + 1] = xy[i * 2 + 1];\n xyz[i * 3 + 2] = z[i];\n }\n return {value: xyz, size: 3};\n}\n\nexport function fromGeometry(geometry, type) {\n switch (type) {\n case GeometryType.Point:\n case GeometryType.MultiPoint:\n return parsePoint(geometry);\n case GeometryType.LineString:\n case GeometryType.MultiLineString:\n return parseLines(geometry);\n case GeometryType.Polygon:\n return parsePolygons(geometry);\n case GeometryType.MultiPolygon:\n return parseMultiPolygons(geometry);\n default:\n throw new Error(`Unimplemented geometry type: ${type}`);\n }\n}\n"],"file":"binary-geometries.js"}
@@ -0,0 +1,3 @@
1
+ export declare function parseFlatGeobuf(arrayBuffer: any, options: any): any;
2
+ export declare function parseFlatGeobufInBatches(stream: any, options: any): any[] | AsyncGenerator<import("flatgeobuf/lib/cjs/generic/feature").IFeature, any, unknown> | AsyncGenerator<any, void, unknown>;
3
+ //# 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":"AAgCA,wBAAgB,eAAe,CAAC,WAAW,KAAA,EAAE,OAAO,KAAA,OAUnD;AA6CD,wBAAgB,wBAAwB,CAAC,MAAM,KAAA,EAAE,OAAO,KAAA,oIAMvD"}
@@ -1,17 +1,110 @@
1
- import { deserialize, deserializeStream } from 'flatgeobuf/dist/flatgeobuf-geojson.min';
2
- export default function parseFlatGeobuf(input, options) {
3
- if (input.byteLength === 0) {
1
+ import { Proj4Projection } from '@math.gl/proj4';
2
+ import { transformGeoJsonCoords } from '@loaders.gl/gis';
3
+ import { deserialize as deserializeGeoJson } from 'flatgeobuf/lib/cjs/geojson';
4
+ import { deserialize as deserializeGeneric } from 'flatgeobuf/lib/cjs/generic';
5
+ import { parseProperties as parsePropertiesBinary } from 'flatgeobuf/lib/cjs/generic/feature';
6
+ import { fromGeometry as binaryFromGeometry } from './binary-geometries';
7
+
8
+ function binaryFromFeature(feature, header) {
9
+ const geometry = feature.geometry();
10
+ const geometryType = header.geometryType || geometry.type();
11
+ const parsedGeometry = binaryFromGeometry(geometry, geometryType);
12
+ parsedGeometry.properties = parsePropertiesBinary(feature, header.columns);
13
+ return parsedGeometry;
14
+ }
15
+
16
+ export function parseFlatGeobuf(arrayBuffer, options) {
17
+ if (arrayBuffer.byteLength === 0) {
4
18
  return [];
5
19
  }
6
20
 
7
- const arr = new Uint8Array(input);
21
+ if (options && options.gis && options.gis.format === 'binary') {
22
+ return parseFlatGeobufToBinary(arrayBuffer, options);
23
+ }
24
+
25
+ return parseFlatGeobufToGeoJSON(arrayBuffer, options);
26
+ }
27
+
28
+ function parseFlatGeobufToBinary(arrayBuffer, options) {
29
+ const arr = new Uint8Array(arrayBuffer);
30
+ return deserializeGeneric(arr, binaryFromFeature);
31
+ }
32
+
33
+ function parseFlatGeobufToGeoJSON(arrayBuffer, options) {
34
+ const {
35
+ reproject = false,
36
+ _targetCrs = 'WGS84'
37
+ } = options && options.gis || {};
38
+ const arr = new Uint8Array(arrayBuffer);
39
+ let headerMeta;
8
40
  const {
9
41
  features
10
- } = deserialize(arr);
42
+ } = deserializeGeoJson(arr, false, header => {
43
+ headerMeta = header;
44
+ });
45
+ const crs = headerMeta && headerMeta.crs;
46
+ let projection;
47
+
48
+ if (reproject && crs) {
49
+ try {
50
+ projection = new Proj4Projection({
51
+ from: crs.wkt,
52
+ to: _targetCrs
53
+ });
54
+ } catch (e) {}
55
+ }
56
+
57
+ if (projection) {
58
+ return transformGeoJsonCoords(features, coords => projection.project(coords));
59
+ }
60
+
11
61
  return features;
12
62
  }
63
+
13
64
  export function parseFlatGeobufInBatches(stream, options) {
14
- const iterator = deserializeStream(stream);
65
+ if (options && options.gis && options.gis.format === 'binary') {
66
+ return parseFlatGeobufInBatchesToBinary(stream, options);
67
+ }
68
+
69
+ return parseFlatGeobufInBatchesToGeoJSON(stream, options);
70
+ }
71
+
72
+ function parseFlatGeobufInBatchesToBinary(stream, options) {
73
+ const iterator = deserializeGeneric(stream, binaryFromFeature);
15
74
  return iterator;
16
75
  }
76
+
77
+ async function* parseFlatGeobufInBatchesToGeoJSON(stream, options) {
78
+ const {
79
+ reproject = false,
80
+ _targetCrs = 'WGS84'
81
+ } = options && options.gis || {};
82
+ let headerMeta;
83
+ const iterator = deserializeGeoJson(stream, false, header => {
84
+ headerMeta = header;
85
+ });
86
+ let projection;
87
+ let firstRecord = true;
88
+
89
+ for await (const feature of iterator) {
90
+ if (firstRecord) {
91
+ const crs = headerMeta && headerMeta.crs;
92
+
93
+ if (reproject && crs) {
94
+ projection = new Proj4Projection({
95
+ from: crs.wkt,
96
+ to: _targetCrs
97
+ });
98
+ }
99
+
100
+ firstRecord = false;
101
+ }
102
+
103
+ if (reproject && projection) {
104
+ yield transformGeoJsonCoords([feature], coords => projection.project(coords));
105
+ } else {
106
+ yield feature;
107
+ }
108
+ }
109
+ }
17
110
  //# sourceMappingURL=parse-flatgeobuf.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/parse-flatgeobuf.ts"],"names":["deserialize","deserializeStream","parseFlatGeobuf","input","options","byteLength","arr","Uint8Array","features","parseFlatGeobufInBatches","stream","iterator"],"mappings":"AACA,SAAQA,WAAR,EAAqBC,iBAArB,QAA6C,wCAA7C;AAQA,eAAe,SAASC,eAAT,CAAyBC,KAAzB,EAAgCC,OAAhC,EAAyC;AACtD,MAAID,KAAK,CAACE,UAAN,KAAqB,CAAzB,EAA4B;AAC1B,WAAO,EAAP;AACD;;AAED,QAAMC,GAAG,GAAG,IAAIC,UAAJ,CAAeJ,KAAf,CAAZ;AACA,QAAM;AAACK,IAAAA;AAAD,MAAaR,WAAW,CAACM,GAAD,CAA9B;AACA,SAAOE,QAAP;AACD;AAQD,OAAO,SAASC,wBAAT,CAAkCC,MAAlC,EAA0CN,OAA1C,EAAmD;AACxD,QAAMO,QAAQ,GAAGV,iBAAiB,CAACS,MAAD,CAAlC;AACA,SAAOC,QAAP;AACD","sourcesContent":["// import {deserialize} from 'flatgeobuf/lib/geojson/featurecollection';\nimport {deserialize, deserializeStream} from 'flatgeobuf/dist/flatgeobuf-geojson.min';\n\n/*\n * Parse FlatGeobuf arrayBuffer and return GeoJSON.\n *\n * @param {arrayBuffer} _ A FlatGeobuf arrayBuffer\n * @return {?Object} A GeoJSON geometry object\n */\nexport default function parseFlatGeobuf(input, options) {\n if (input.byteLength === 0) {\n return [];\n }\n\n const arr = new Uint8Array(input);\n const {features} = deserialize(arr);\n return features;\n}\n\n/*\n * Parse FlatGeobuf arrayBuffer and return GeoJSON.\n *\n * @param {ReadableStream} _ A FlatGeobuf arrayBuffer\n * @return A GeoJSON geometry object iterator\n */\nexport function parseFlatGeobufInBatches(stream, options) {\n const iterator = deserializeStream(stream);\n return iterator;\n}\n"],"file":"parse-flatgeobuf.js"}
1
+ {"version":3,"sources":["../../src/lib/parse-flatgeobuf.ts"],"names":["Proj4Projection","transformGeoJsonCoords","deserialize","deserializeGeoJson","deserializeGeneric","parseProperties","parsePropertiesBinary","fromGeometry","binaryFromGeometry","binaryFromFeature","feature","header","geometry","geometryType","type","parsedGeometry","properties","columns","parseFlatGeobuf","arrayBuffer","options","byteLength","gis","format","parseFlatGeobufToBinary","parseFlatGeobufToGeoJSON","arr","Uint8Array","reproject","_targetCrs","headerMeta","features","crs","projection","from","wkt","to","e","coords","project","parseFlatGeobufInBatches","stream","parseFlatGeobufInBatchesToBinary","parseFlatGeobufInBatchesToGeoJSON","iterator","firstRecord"],"mappings":"AACA,SAAQA,eAAR,QAA8B,gBAA9B;AACA,SAAQC,sBAAR,QAAqC,iBAArC;AAEA,SAAQC,WAAW,IAAIC,kBAAvB,QAAgD,4BAAhD;AACA,SAAQD,WAAW,IAAIE,kBAAvB,QAAgD,4BAAhD;AACA,SAAQC,eAAe,IAAIC,qBAA3B,QAAuD,oCAAvD;AAEA,SAAQC,YAAY,IAAIC,kBAAxB,QAAiD,qBAAjD;;AAGA,SAASC,iBAAT,CAA2BC,OAA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ACA,QAAMc,QAAQ,GAAGzC,kBAAkB,CAACsC,MAAD,EAAS,KAAT,EAAiB9B,MAAD,IAAY;AAC7DmB,IAAAA,UAAU,GAAGnB,MAAb;AACD,GAFkC,CAAnC;AAIA,MAAIsB,UAAJ;AACA,MAAIY,WAAW,GAAG,IAAlB;;AACA,aAAW,MAAMnC,OAAjB,IAA4BkC,QAA5B,EAAsC;AACpC,QAAIC,WAAJ,EAAiB;AACf,YAAMb,GAAG,GAAGF,UAAU,IAAIA,UAAU,CAACE,GAArC;;AACA,UAAIJ,SAAS,IAAII,GAAjB,EAAsB;AACpBC,QAAAA,UAAU,GAAG,IAAIjC,eAAJ,CAAoB;AAACkC,UAAAA,IAAI,EAAEF,GAAG,CAACG,GAAX;AAAgBC,UAAAA,EAAE,EAAEP;AAApB,SAApB,CAAb;AACD;;AAEDgB,MAAAA,WAAW,GAAG,KAAd;AACD;;AAED,QAAIjB,SAAS,IAAIK,UAAjB,EAA6B;AAE3B,YAAMhC,sBAAsB,CAAC,CAACS,OAAD,CAAD,EAAa4B,MAAD,IAAYL,UAAU,CAACM,OAAX,CAAmBD,MAAnB,CAAxB,CAA5B;AACD,KAHD,MAGO;AACL,YAAM5B,OAAN;AACD;AACF;AACF","sourcesContent":["// @ts-nocheck\nimport {Proj4Projection} from '@math.gl/proj4';\nimport {transformGeoJsonCoords} from '@loaders.gl/gis';\n\nimport {deserialize as deserializeGeoJson} from 'flatgeobuf/lib/cjs/geojson';\nimport {deserialize as deserializeGeneric} from 'flatgeobuf/lib/cjs/generic';\nimport {parseProperties as parsePropertiesBinary} from 'flatgeobuf/lib/cjs/generic/feature';\n\nimport {fromGeometry as binaryFromGeometry} from './binary-geometries';\n\n// TODO: reproject binary features\nfunction binaryFromFeature(feature, header) {\n const geometry = feature.geometry();\n\n // FlatGeobuf files can only hold a single geometry type per file, otherwise\n // GeometryType is GeometryCollection\n // I believe geometry.type() is null (0) except when the geometry type isn't\n // known in the header?\n const geometryType = header.geometryType || geometry.type();\n const parsedGeometry = binaryFromGeometry(geometry, geometryType);\n parsedGeometry.properties = parsePropertiesBinary(feature, header.columns);\n\n // TODO: wrap binary data either in points, lines, or polygons key\n return parsedGeometry;\n}\n\n/*\n * Parse FlatGeobuf arrayBuffer and return GeoJSON.\n *\n * @param arrayBuffer A FlatGeobuf arrayBuffer\n * @return A GeoJSON geometry object\n */\nexport function parseFlatGeobuf(arrayBuffer, options) {\n if (arrayBuffer.byteLength === 0) {\n return [];\n }\n\n if (options && options.gis && options.gis.format === 'binary') {\n return parseFlatGeobufToBinary(arrayBuffer, options);\n }\n\n return parseFlatGeobufToGeoJSON(arrayBuffer, options);\n}\n\nfunction parseFlatGeobufToBinary(arrayBuffer, options) {\n // TODO: reproject binary features\n // const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};\n\n const arr = new Uint8Array(arrayBuffer);\n return deserializeGeneric(arr, binaryFromFeature);\n}\n\nfunction parseFlatGeobufToGeoJSON(arrayBuffer, options) {\n const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};\n\n const arr = new Uint8Array(arrayBuffer);\n\n let headerMeta;\n const {features} = deserializeGeoJson(arr, false, (header) => {\n headerMeta = header;\n });\n\n const crs = headerMeta && headerMeta.crs;\n let projection;\n if (reproject && crs) {\n // Constructing the projection may fail for some invalid WKT strings\n try {\n projection = new Proj4Projection({from: crs.wkt, to: _targetCrs});\n } catch (e) {\n // no op\n }\n }\n\n if (projection) {\n return transformGeoJsonCoords(features, (coords) => projection.project(coords));\n }\n\n return features;\n}\n\n/*\n * Parse FlatGeobuf arrayBuffer and return GeoJSON.\n *\n * @param {ReadableStream} _ A FlatGeobuf arrayBuffer\n * @return A GeoJSON geometry object iterator\n */\n// eslint-disable-next-line complexity\nexport function parseFlatGeobufInBatches(stream, options) {\n if (options && options.gis && options.gis.format === 'binary') {\n return parseFlatGeobufInBatchesToBinary(stream, options);\n }\n\n return parseFlatGeobufInBatchesToGeoJSON(stream, options);\n}\n\nfunction parseFlatGeobufInBatchesToBinary(stream, options) {\n // TODO: reproject binary streaming features\n // const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};\n\n const iterator = deserializeGeneric(stream, binaryFromFeature);\n return iterator;\n}\n\n// eslint-disable-next-line complexity\nasync function* parseFlatGeobufInBatchesToGeoJSON(stream, options) {\n const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};\n\n let headerMeta;\n const iterator = deserializeGeoJson(stream, false, (header) => {\n headerMeta = header;\n });\n\n let projection;\n let firstRecord = true;\n for await (const feature of iterator) {\n if (firstRecord) {\n const crs = headerMeta && headerMeta.crs;\n if (reproject && crs) {\n projection = new Proj4Projection({from: crs.wkt, to: _targetCrs});\n }\n\n firstRecord = false;\n }\n\n if (reproject && projection) {\n // eslint-disable-next-line\n yield transformGeoJsonCoords([feature], (coords) => projection.project(coords));\n } else {\n yield feature;\n }\n }\n}\n"],"file":"parse-flatgeobuf.js"}
@@ -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":""}
package/package.json CHANGED
@@ -1,7 +1,7 @@
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.5",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -17,7 +17,7 @@
17
17
  "MVT",
18
18
  "Mapbox Vector Tiles"
19
19
  ],
20
- "types": "src/index.ts",
20
+ "types": "dist/index.d.ts",
21
21
  "main": "dist/index.js",
22
22
  "module": "dist/index.js",
23
23
  "sideEffects": false,
@@ -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.1",
36
+ "@loaders.gl/loader-utils": "4.0.0-alpha.5",
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": "7a71a54bdf1ddf985cc3af3db90b82e7fa97d025"
42
41
  }
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
+ }