@loaders.gl/flatgeobuf 4.0.0-beta.5 → 4.0.0-beta.7

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.
@@ -1,21 +1,72 @@
1
- import {GeometryType} from 'flatgeobuf/lib/cjs/header_generated';
1
+ // loaders.gl, MIT license
2
+
3
+ import {Geometry as FGBGeometry, Feature as FGBFeature} from 'flatgeobuf';
4
+ // import {GeometryType} from 'flatgeobuf/generic';
5
+ // Copy geometry type as it is hard to access the export
6
+ export declare enum GeometryType {
7
+ Unknown = 0,
8
+ Point = 1,
9
+ LineString = 2,
10
+ Polygon = 3,
11
+ MultiPoint = 4,
12
+ MultiLineString = 5,
13
+ MultiPolygon = 6,
14
+ GeometryCollection = 7,
15
+ CircularString = 8,
16
+ CompoundCurve = 9,
17
+ CurvePolygon = 10,
18
+ MultiCurve = 11,
19
+ MultiSurface = 12,
20
+ Curve = 13,
21
+ Surface = 14,
22
+ PolyhedralSurface = 15,
23
+ TIN = 16,
24
+ Triangle = 17
25
+ }
26
+
27
+ export function fgbToBinaryFeature(geometry: FGBFeature | null, type: GeometryType) {
28
+ const fgbGeometry: FGBGeometry | null = geometry?.geometry() || null;
29
+ return fgbToBinaryGeometry(fgbGeometry, type);
30
+ }
31
+
32
+ export function fgbToBinaryGeometry(geometry: FGBGeometry | null, type: GeometryType) {
33
+ if (geometry === null) {
34
+ return null;
35
+ }
36
+ switch (type) {
37
+ case GeometryType.Point:
38
+ case GeometryType.MultiPoint:
39
+ return parsePoint(geometry);
40
+ case GeometryType.LineString:
41
+ case GeometryType.MultiLineString:
42
+ return parseLines(geometry);
43
+ case GeometryType.Polygon:
44
+ return parsePolygons(geometry);
45
+ case GeometryType.MultiPolygon:
46
+ return parseMultiPolygons(geometry);
47
+ default:
48
+ throw new Error(`Unimplemented geometry type: ${type}`);
49
+ }
50
+ }
2
51
 
3
52
  // Parse Point to flat array
4
- function parsePoint(geometry) {
53
+ function parsePoint(geometry: FGBGeometry) {
5
54
  const xy = geometry.xyArray();
6
55
  const z = geometry.zArray();
56
+ // @ts-expect-error TODO handle null geometries
7
57
  const positions = blitArrays(xy, z);
8
58
  return {positions};
9
59
  }
10
60
 
11
- function parseLines(geometry) {
61
+ function parseLines(geometry: FGBGeometry) {
12
62
  const xy = geometry.xyArray();
13
63
  const z = geometry.zArray();
14
- const positions = blitArrays(xy, z);
64
+ const positions = blitArrays(xy!, z!);
15
65
 
16
66
  // If endsArray is null, a single LineString. Otherwise, contains the end
17
67
  // indices of each part of the MultiLineString. geometry.endsArray() omits the
18
68
  // initial 0 that we have in our internal format.
69
+ // @ts-expect-error TODO handle null geometries
19
70
  const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];
20
71
  ends.unshift(0);
21
72
 
@@ -27,19 +78,21 @@ function parseLines(geometry) {
27
78
  };
28
79
  }
29
80
 
30
- function parsePolygons(geometry) {
81
+ function parsePolygons(geometry: FGBGeometry) {
31
82
  const xy = geometry.xyArray();
32
83
  const z = geometry.zArray();
84
+ // @ts-expect-error TODO handle null geometries
33
85
  const positions = blitArrays(xy, z);
34
86
 
35
87
  // If endsArray is null, a simple Polygon with no inner rings. Otherwise,
36
88
  // contains the end indices of each ring of the Polygon. geometry.endsArray()
37
89
  // omits the initial 0 that we have in our internal format.
90
+ // @ts-expect-error TODO handle null geometries
38
91
  const ends = (geometry.endsArray() && Array.from(geometry.endsArray())) || [xy.length / 2];
39
92
  ends.unshift(0);
40
93
 
41
94
  const primitivePolygonIndices = {value: new Uint16Array(ends), size: 1};
42
- const polygonIndices = {value: new Uint16Array([0, xy.length / 2]), size: 1};
95
+ const polygonIndices = {value: new Uint16Array([0, xy!.length / 2]), size: 1};
43
96
 
44
97
  return {
45
98
  positions,
@@ -49,7 +102,7 @@ function parsePolygons(geometry) {
49
102
  }
50
103
 
51
104
  // eslint-disable-next-line max-statements
52
- function parseMultiPolygons(geometry) {
105
+ function parseMultiPolygons(geometry: FGBGeometry) {
53
106
  // Create arrays for each geometry part, then concatenate
54
107
  const parsedParts: any[] = [];
55
108
  let nPositions = 0;
@@ -58,6 +111,7 @@ function parseMultiPolygons(geometry) {
58
111
 
59
112
  for (let i = 0; i < geometry.partsLength(); i++) {
60
113
  const part = geometry.parts(i);
114
+ // @ts-expect-error TODO handle null geometries
61
115
  const polygon = parsePolygons(part);
62
116
 
63
117
  nPositions += polygon.positions.value.length;
@@ -107,7 +161,7 @@ function parseMultiPolygons(geometry) {
107
161
  }
108
162
 
109
163
  // Combine xy and z arrays
110
- function blitArrays(xy, z) {
164
+ function blitArrays(xy: Float64Array, z: Float64Array): {value: Float64Array; size: number} {
111
165
  if (!z) {
112
166
  return {value: xy, size: 2};
113
167
  }
@@ -125,20 +179,3 @@ function blitArrays(xy, z) {
125
179
  }
126
180
  return {value: xyz, size: 3};
127
181
  }
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
- }
@@ -1,25 +1,30 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  import {Proj4Projection} from '@math.gl/proj4';
2
4
  import {transformGeoJsonCoords} from '@loaders.gl/gis';
3
5
 
4
- import {deserialize as deserializeGeoJson} from 'flatgeobuf/lib/cjs/geojson';
5
- import {deserialize as deserializeGeneric} from 'flatgeobuf/lib/cjs/generic';
6
- import {parseProperties as parsePropertiesBinary} from 'flatgeobuf/lib/cjs/generic/feature';
7
-
8
6
  import type {FlatGeobufLoaderOptions} from '../flatgeobuf-loader';
9
7
  import type {GeoJSONTable, Feature, Table} from '@loaders.gl/schema';
10
- import {fromGeometry as binaryFromGeometry} from './binary-geometries';
11
- // import {Feature} from 'flatgeobuf/lib/cjs/feature_generated';
8
+ import {fgbToBinaryGeometry} from './binary-geometries';
9
+
10
+ import {Feature as FBGFeature, HeaderMeta as FGBHeader} from 'flatgeobuf';
11
+ import * as geojson from 'flatgeobuf/lib/mjs/geojson.js';
12
+ import * as generic from 'flatgeobuf/lib/mjs/generic.js';
13
+ import {parseProperties as parsePropertiesBinary} from 'flatgeobuf/lib/mjs/generic/feature';
14
+ const deserializeGeoJson = geojson.deserialize;
15
+ const deserializeGeneric = generic.deserialize;
16
+ // const parsePropertiesBinary = FlatgeobufFeature.parseProperties;
12
17
 
13
18
  // TODO: reproject binary features
14
- function binaryFromFeature(feature, header) {
19
+ function binaryFromFeature(feature: FBGFeature, header: FGBHeader) {
15
20
  const geometry = feature.geometry();
16
21
 
17
22
  // FlatGeobuf files can only hold a single geometry type per file, otherwise
18
23
  // GeometryType is GeometryCollection
19
24
  // I believe geometry.type() is null (0) except when the geometry type isn't
20
25
  // known in the header?
21
- const geometryType = header.geometryType || geometry.type();
22
- const parsedGeometry = binaryFromGeometry(geometry, geometryType);
26
+ const geometryType = header.geometryType || geometry?.type();
27
+ const parsedGeometry = fgbToBinaryGeometry(geometry, geometryType!);
23
28
  // @ts-expect-error this looks wrong
24
29
  parsedGeometry.properties = parsePropertiesBinary(feature, header.columns);
25
30
 
@@ -37,10 +42,9 @@ export function parseFlatGeobuf(
37
42
  arrayBuffer: ArrayBuffer,
38
43
  options?: FlatGeobufLoaderOptions
39
44
  ): Table {
40
- const shape = options?.gis?.format || options?.flatgeobuf?.shape;
45
+ const shape = options?.flatgeobuf?.shape;
41
46
 
42
47
  switch (shape) {
43
- case 'geojson':
44
48
  case 'geojson-table': {
45
49
  const features = parseFlatGeobufToGeoJSON(arrayBuffer, options);
46
50
  const table: GeoJSONTable = {
@@ -70,8 +74,8 @@ function parseFlatGeobufToBinary(arrayBuffer: ArrayBuffer, options: FlatGeobufLo
70
74
  // const {reproject = false, _targetCrs = 'WGS84'} = (options && options.gis) || {};
71
75
 
72
76
  const array = new Uint8Array(arrayBuffer);
73
- // @ts-expect-error this looks wrong
74
- return deserializeGeneric(array, binaryFromFeature);
77
+ // @ts-expect-error
78
+ return deserializeGeneric(array, fgbToBinaryGeometry);
75
79
  }
76
80
 
77
81
  function parseFlatGeobufToGeoJSON(
@@ -118,11 +122,15 @@ function parseFlatGeobufToGeoJSON(
118
122
  */
119
123
  // eslint-disable-next-line complexity
120
124
  export function parseFlatGeobufInBatches(stream, options: FlatGeobufLoaderOptions) {
121
- if (options && options.gis && options.gis.format === 'binary') {
122
- return parseFlatGeobufInBatchesToBinary(stream, options);
125
+ const shape = options.flatgeobuf?.shape;
126
+ switch (shape) {
127
+ case 'binary':
128
+ return parseFlatGeobufInBatchesToBinary(stream, options);
129
+ case 'geojson-table':
130
+ return parseFlatGeobufInBatchesToGeoJSON(stream, options);
131
+ default:
132
+ throw new Error(shape);
123
133
  }
124
-
125
- return parseFlatGeobufInBatchesToGeoJSON(stream, options);
126
134
  }
127
135
 
128
136
  function parseFlatGeobufInBatchesToBinary(stream, options: FlatGeobufLoaderOptions) {