@loaders.gl/shapefile 4.0.0-alpha.5 → 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 (124) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/dbf-loader.js +29 -20
  3. package/dist/dbf-worker.js +73 -447
  4. package/dist/dist.min.js +130 -489
  5. package/dist/es5/bundle.js +6 -0
  6. package/dist/es5/bundle.js.map +1 -0
  7. package/dist/es5/dbf-loader.js +53 -0
  8. package/dist/es5/dbf-loader.js.map +1 -0
  9. package/dist/es5/index.js +39 -0
  10. package/dist/es5/index.js.map +1 -0
  11. package/dist/es5/lib/parsers/parse-dbf.js +394 -0
  12. package/dist/es5/lib/parsers/parse-dbf.js.map +1 -0
  13. package/dist/es5/lib/parsers/parse-shapefile.js +373 -0
  14. package/dist/es5/lib/parsers/parse-shapefile.js.map +1 -0
  15. package/dist/es5/lib/parsers/parse-shp-geometry.js +220 -0
  16. package/dist/es5/lib/parsers/parse-shp-geometry.js.map +1 -0
  17. package/dist/es5/lib/parsers/parse-shp-header.js +35 -0
  18. package/dist/es5/lib/parsers/parse-shp-header.js.map +1 -0
  19. package/dist/es5/lib/parsers/parse-shp.js +227 -0
  20. package/dist/es5/lib/parsers/parse-shp.js.map +1 -0
  21. package/dist/es5/lib/parsers/parse-shx.js +26 -0
  22. package/dist/es5/lib/parsers/parse-shx.js.map +1 -0
  23. package/dist/es5/lib/parsers/types.js +2 -0
  24. package/dist/es5/lib/parsers/types.js.map +1 -0
  25. package/dist/es5/lib/streaming/binary-chunk-reader.js +178 -0
  26. package/dist/es5/lib/streaming/binary-chunk-reader.js.map +1 -0
  27. package/dist/es5/lib/streaming/binary-reader.js +48 -0
  28. package/dist/es5/lib/streaming/binary-reader.js.map +1 -0
  29. package/dist/es5/lib/streaming/zip-batch-iterators.js +91 -0
  30. package/dist/es5/lib/streaming/zip-batch-iterators.js.map +1 -0
  31. package/dist/es5/shapefile-loader.js +31 -0
  32. package/dist/es5/shapefile-loader.js.map +1 -0
  33. package/dist/es5/shp-loader.js +56 -0
  34. package/dist/es5/shp-loader.js.map +1 -0
  35. package/dist/es5/workers/dbf-worker.js +6 -0
  36. package/dist/es5/workers/dbf-worker.js.map +1 -0
  37. package/dist/es5/workers/shp-worker.js +6 -0
  38. package/dist/es5/workers/shp-worker.js.map +1 -0
  39. package/dist/esm/bundle.js +4 -0
  40. package/dist/esm/bundle.js.map +1 -0
  41. package/dist/esm/dbf-loader.js +24 -0
  42. package/dist/esm/dbf-loader.js.map +1 -0
  43. package/dist/esm/index.js +4 -0
  44. package/dist/esm/index.js.map +1 -0
  45. package/dist/esm/lib/parsers/parse-dbf.js +296 -0
  46. package/dist/esm/lib/parsers/parse-dbf.js.map +1 -0
  47. package/dist/esm/lib/parsers/parse-shapefile.js +187 -0
  48. package/dist/esm/lib/parsers/parse-shapefile.js.map +1 -0
  49. package/dist/esm/lib/parsers/parse-shp-geometry.js +191 -0
  50. package/dist/esm/lib/parsers/parse-shp-geometry.js.map +1 -0
  51. package/dist/esm/lib/parsers/parse-shp-header.js +29 -0
  52. package/dist/esm/lib/parsers/parse-shp-header.js.map +1 -0
  53. package/dist/esm/lib/parsers/parse-shp.js +134 -0
  54. package/dist/esm/lib/parsers/parse-shp.js.map +1 -0
  55. package/dist/esm/lib/parsers/parse-shx.js +20 -0
  56. package/dist/esm/lib/parsers/parse-shx.js.map +1 -0
  57. package/dist/esm/lib/parsers/types.js +2 -0
  58. package/dist/esm/lib/parsers/types.js.map +1 -0
  59. package/dist/esm/lib/streaming/binary-chunk-reader.js +106 -0
  60. package/dist/esm/lib/streaming/binary-chunk-reader.js.map +1 -0
  61. package/dist/esm/lib/streaming/binary-reader.js +27 -0
  62. package/dist/esm/lib/streaming/binary-reader.js.map +1 -0
  63. package/dist/esm/lib/streaming/zip-batch-iterators.js +44 -0
  64. package/dist/esm/lib/streaming/zip-batch-iterators.js.map +1 -0
  65. package/dist/esm/shapefile-loader.js +23 -0
  66. package/dist/esm/shapefile-loader.js.map +1 -0
  67. package/dist/esm/shp-loader.js +26 -0
  68. package/dist/esm/shp-loader.js.map +1 -0
  69. package/dist/esm/workers/dbf-worker.js +4 -0
  70. package/dist/esm/workers/dbf-worker.js.map +1 -0
  71. package/dist/esm/workers/shp-worker.js +4 -0
  72. package/dist/esm/workers/shp-worker.js.map +1 -0
  73. package/dist/index.js +11 -4
  74. package/dist/lib/parsers/parse-dbf.d.ts +4 -18
  75. package/dist/lib/parsers/parse-dbf.d.ts.map +1 -1
  76. package/dist/lib/parsers/parse-dbf.js +309 -264
  77. package/dist/lib/parsers/parse-shapefile.d.ts +3 -8
  78. package/dist/lib/parsers/parse-shapefile.d.ts.map +1 -1
  79. package/dist/lib/parsers/parse-shapefile.js +227 -209
  80. package/dist/lib/parsers/parse-shp-geometry.d.ts +2 -3
  81. package/dist/lib/parsers/parse-shp-geometry.d.ts.map +1 -1
  82. package/dist/lib/parsers/parse-shp-geometry.js +265 -212
  83. package/dist/lib/parsers/parse-shp-header.js +38 -27
  84. package/dist/lib/parsers/parse-shp.d.ts +3 -2
  85. package/dist/lib/parsers/parse-shp.d.ts.map +1 -1
  86. package/dist/lib/parsers/parse-shp.js +160 -136
  87. package/dist/lib/parsers/parse-shx.js +25 -19
  88. package/dist/lib/parsers/types.d.ts +68 -0
  89. package/dist/lib/parsers/types.d.ts.map +1 -0
  90. package/dist/lib/parsers/types.js +2 -0
  91. package/dist/lib/streaming/binary-chunk-reader.d.ts +5 -3
  92. package/dist/lib/streaming/binary-chunk-reader.d.ts.map +1 -1
  93. package/dist/lib/streaming/binary-chunk-reader.js +152 -128
  94. package/dist/lib/streaming/binary-reader.js +50 -33
  95. package/dist/lib/streaming/zip-batch-iterators.js +57 -48
  96. package/dist/shapefile-loader.js +30 -22
  97. package/dist/shp-loader.js +32 -22
  98. package/dist/shp-worker.js +57 -19
  99. package/dist/workers/dbf-worker.js +5 -4
  100. package/dist/workers/shp-worker.js +5 -4
  101. package/package.json +7 -7
  102. package/src/lib/parsers/parse-dbf.ts +41 -67
  103. package/src/lib/parsers/parse-shapefile.ts +3 -6
  104. package/src/lib/parsers/parse-shp-geometry.ts +3 -2
  105. package/src/lib/parsers/parse-shp.ts +26 -12
  106. package/src/lib/parsers/types.ts +79 -0
  107. package/src/lib/streaming/binary-chunk-reader.ts +5 -1
  108. package/src/lib/streaming/zip-batch-iterators.ts +2 -2
  109. package/dist/bundle.js.map +0 -1
  110. package/dist/dbf-loader.js.map +0 -1
  111. package/dist/index.js.map +0 -1
  112. package/dist/lib/parsers/parse-dbf.js.map +0 -1
  113. package/dist/lib/parsers/parse-shapefile.js.map +0 -1
  114. package/dist/lib/parsers/parse-shp-geometry.js.map +0 -1
  115. package/dist/lib/parsers/parse-shp-header.js.map +0 -1
  116. package/dist/lib/parsers/parse-shp.js.map +0 -1
  117. package/dist/lib/parsers/parse-shx.js.map +0 -1
  118. package/dist/lib/streaming/binary-chunk-reader.js.map +0 -1
  119. package/dist/lib/streaming/binary-reader.js.map +0 -1
  120. package/dist/lib/streaming/zip-batch-iterators.js.map +0 -1
  121. package/dist/shapefile-loader.js.map +0 -1
  122. package/dist/shp-loader.js.map +0 -1
  123. package/dist/workers/dbf-worker.js.map +0 -1
  124. package/dist/workers/shp-worker.js.map +0 -1
@@ -1,234 +1,287 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRecord = void 0;
1
4
  const LITTLE_ENDIAN = true;
2
- export function parseRecord(view, options) {
3
- const {
4
- _maxDimensions
5
- } = (options === null || options === void 0 ? void 0 : options.shp) || {};
6
- let offset = 0;
7
- const type = view.getInt32(offset, LITTLE_ENDIAN);
8
- offset += Int32Array.BYTES_PER_ELEMENT;
9
-
10
- switch (type) {
11
- case 0:
12
- return parseNull();
13
-
14
- case 1:
15
- return parsePoint(view, offset, Math.min(2, _maxDimensions));
16
-
17
- case 3:
18
- return parsePoly(view, offset, Math.min(2, _maxDimensions), 'LineString');
19
-
20
- case 5:
21
- return parsePoly(view, offset, Math.min(2, _maxDimensions), 'Polygon');
22
-
23
- case 8:
24
- return parseMultiPoint(view, offset, Math.min(2, _maxDimensions));
25
-
26
- case 11:
27
- return parsePoint(view, offset, Math.min(4, _maxDimensions));
28
-
29
- case 13:
30
- return parsePoly(view, offset, Math.min(4, _maxDimensions), 'LineString');
31
-
32
- case 15:
33
- return parsePoly(view, offset, Math.min(4, _maxDimensions), 'Polygon');
34
-
35
- case 18:
36
- return parseMultiPoint(view, offset, Math.min(4, _maxDimensions));
37
-
38
- case 21:
39
- return parsePoint(view, offset, Math.min(3, _maxDimensions));
40
-
41
- case 23:
42
- return parsePoly(view, offset, Math.min(3, _maxDimensions), 'LineString');
43
-
44
- case 25:
45
- return parsePoly(view, offset, Math.min(3, _maxDimensions), 'Polygon');
46
-
47
- case 28:
48
- return parseMultiPoint(view, offset, Math.min(3, _maxDimensions));
49
-
50
- default:
51
- throw new Error("unsupported shape type: ".concat(type));
52
- }
5
+ /**
6
+ * Parse individual record
7
+ *
8
+ * @param view Record data
9
+ * @return Binary Geometry Object
10
+ */
11
+ // eslint-disable-next-line complexity
12
+ function parseRecord(view, options) {
13
+ const { _maxDimensions = 4 } = options?.shp || {};
14
+ let offset = 0;
15
+ const type = view.getInt32(offset, LITTLE_ENDIAN);
16
+ offset += Int32Array.BYTES_PER_ELEMENT;
17
+ switch (type) {
18
+ case 0:
19
+ // Null Shape
20
+ return parseNull();
21
+ case 1:
22
+ // Point
23
+ return parsePoint(view, offset, Math.min(2, _maxDimensions));
24
+ case 3:
25
+ // PolyLine
26
+ return parsePoly(view, offset, Math.min(2, _maxDimensions), 'LineString');
27
+ case 5:
28
+ // Polygon
29
+ return parsePoly(view, offset, Math.min(2, _maxDimensions), 'Polygon');
30
+ case 8:
31
+ // MultiPoint
32
+ return parseMultiPoint(view, offset, Math.min(2, _maxDimensions));
33
+ // GeometryZ can have 3 or 4 dimensions, since the M is not required to
34
+ // exist
35
+ case 11:
36
+ // PointZ
37
+ return parsePoint(view, offset, Math.min(4, _maxDimensions));
38
+ case 13:
39
+ // PolyLineZ
40
+ return parsePoly(view, offset, Math.min(4, _maxDimensions), 'LineString');
41
+ case 15:
42
+ // PolygonZ
43
+ return parsePoly(view, offset, Math.min(4, _maxDimensions), 'Polygon');
44
+ case 18:
45
+ // MultiPointZ
46
+ return parseMultiPoint(view, offset, Math.min(4, _maxDimensions));
47
+ case 21:
48
+ // PointM
49
+ return parsePoint(view, offset, Math.min(3, _maxDimensions));
50
+ case 23:
51
+ // PolyLineM
52
+ return parsePoly(view, offset, Math.min(3, _maxDimensions), 'LineString');
53
+ case 25:
54
+ // PolygonM
55
+ return parsePoly(view, offset, Math.min(3, _maxDimensions), 'Polygon');
56
+ case 28:
57
+ // MultiPointM
58
+ return parseMultiPoint(view, offset, Math.min(3, _maxDimensions));
59
+ default:
60
+ throw new Error(`unsupported shape type: ${type}`);
61
+ }
53
62
  }
54
-
63
+ exports.parseRecord = parseRecord;
64
+ // TODO handle null
65
+ /**
66
+ * Parse Null geometry
67
+ *
68
+ * @return null
69
+ */
55
70
  function parseNull() {
56
- return null;
71
+ return null;
57
72
  }
58
-
73
+ /**
74
+ * Parse point geometry
75
+ *
76
+ * @param view Geometry data
77
+ * @param offset Offset in view
78
+ * @param dim Dimension size
79
+ */
59
80
  function parsePoint(view, offset, dim) {
60
- let positions;
61
- [positions, offset] = parsePositions(view, offset, 1, dim);
62
- return {
63
- positions: {
64
- value: positions,
65
- size: dim
66
- },
67
- type: 'Point'
68
- };
81
+ let positions;
82
+ [positions, offset] = parsePositions(view, offset, 1, dim);
83
+ return {
84
+ positions: { value: positions, size: dim },
85
+ type: 'Point'
86
+ };
69
87
  }
70
-
88
+ /**
89
+ * Parse MultiPoint geometry
90
+ *
91
+ * @param view Geometry data
92
+ * @param offset Offset in view
93
+ * @param dim Input dimension
94
+ * @return Binary geometry object
95
+ */
71
96
  function parseMultiPoint(view, offset, dim) {
72
- offset += 4 * Float64Array.BYTES_PER_ELEMENT;
73
- const nPoints = view.getInt32(offset, LITTLE_ENDIAN);
74
- offset += Int32Array.BYTES_PER_ELEMENT;
75
- let xyPositions = null;
76
- let mPositions = null;
77
- let zPositions = null;
78
- [xyPositions, offset] = parsePositions(view, offset, nPoints, 2);
79
-
80
- if (dim === 4) {
81
- offset += 2 * Float64Array.BYTES_PER_ELEMENT;
82
- [zPositions, offset] = parsePositions(view, offset, nPoints, 1);
83
- }
84
-
85
- if (dim >= 3) {
86
- offset += 2 * Float64Array.BYTES_PER_ELEMENT;
87
- [mPositions, offset] = parsePositions(view, offset, nPoints, 1);
88
- }
89
-
90
- const positions = concatPositions(xyPositions, mPositions, zPositions);
91
- return {
92
- positions: {
93
- value: positions,
94
- size: dim
95
- },
96
- type: 'Point'
97
- };
98
- }
99
-
100
- function parsePoly(view, offset, dim, type) {
101
- offset += 4 * Float64Array.BYTES_PER_ELEMENT;
102
- const nParts = view.getInt32(offset, LITTLE_ENDIAN);
103
- offset += Int32Array.BYTES_PER_ELEMENT;
104
- const nPoints = view.getInt32(offset, LITTLE_ENDIAN);
105
- offset += Int32Array.BYTES_PER_ELEMENT;
106
- const bufferOffset = view.byteOffset + offset;
107
- const bufferLength = nParts * Int32Array.BYTES_PER_ELEMENT;
108
- const ringIndices = new Int32Array(nParts + 1);
109
- ringIndices.set(new Int32Array(view.buffer.slice(bufferOffset, bufferOffset + bufferLength)));
110
- ringIndices[nParts] = nPoints;
111
- offset += nParts * Int32Array.BYTES_PER_ELEMENT;
112
- let xyPositions = null;
113
- let mPositions = null;
114
- let zPositions = null;
115
- [xyPositions, offset] = parsePositions(view, offset, nPoints, 2);
116
-
117
- if (dim === 4) {
118
- offset += 2 * Float64Array.BYTES_PER_ELEMENT;
119
- [zPositions, offset] = parsePositions(view, offset, nPoints, 1);
120
- }
121
-
122
- if (dim >= 3) {
123
- offset += 2 * Float64Array.BYTES_PER_ELEMENT;
124
- [mPositions, offset] = parsePositions(view, offset, nPoints, 1);
125
- }
126
-
127
- const positions = concatPositions(xyPositions, mPositions, zPositions);
128
-
129
- if (type === 'LineString') {
97
+ // skip parsing box
98
+ offset += 4 * Float64Array.BYTES_PER_ELEMENT;
99
+ const nPoints = view.getInt32(offset, LITTLE_ENDIAN);
100
+ offset += Int32Array.BYTES_PER_ELEMENT;
101
+ let xyPositions = null;
102
+ let mPositions = null;
103
+ let zPositions = null;
104
+ [xyPositions, offset] = parsePositions(view, offset, nPoints, 2);
105
+ // Parse Z coordinates
106
+ if (dim === 4) {
107
+ // skip parsing range
108
+ offset += 2 * Float64Array.BYTES_PER_ELEMENT;
109
+ [zPositions, offset] = parsePositions(view, offset, nPoints, 1);
110
+ }
111
+ // Parse M coordinates
112
+ if (dim >= 3) {
113
+ // skip parsing range
114
+ offset += 2 * Float64Array.BYTES_PER_ELEMENT;
115
+ [mPositions, offset] = parsePositions(view, offset, nPoints, 1);
116
+ }
117
+ const positions = concatPositions(xyPositions, mPositions, zPositions);
130
118
  return {
131
- type,
132
- positions: {
133
- value: positions,
134
- size: dim
135
- },
136
- pathIndices: {
137
- value: ringIndices,
138
- size: 1
139
- }
119
+ positions: { value: positions, size: dim },
120
+ type: 'Point'
140
121
  };
141
- }
142
-
143
- const polygonIndices = [];
144
-
145
- for (let i = 1; i < ringIndices.length; i++) {
146
- const startRingIndex = ringIndices[i - 1];
147
- const endRingIndex = ringIndices[i];
148
- const ring = xyPositions.subarray(startRingIndex * 2, endRingIndex * 2);
149
- const sign = getWindingDirection(ring);
150
-
151
- if (sign > 0) {
152
- polygonIndices.push(startRingIndex);
122
+ }
123
+ /**
124
+ * Polygon and PolyLine parsing
125
+ *
126
+ * @param view Geometry data
127
+ * @param offset Offset in view
128
+ * @param dim Input dimension
129
+ * @param type Either 'Polygon' or 'Polyline'
130
+ * @return Binary geometry object
131
+ */
132
+ // eslint-disable-next-line max-statements
133
+ function parsePoly(view, offset, dim, type) {
134
+ // skip parsing bounding box
135
+ offset += 4 * Float64Array.BYTES_PER_ELEMENT;
136
+ const nParts = view.getInt32(offset, LITTLE_ENDIAN);
137
+ offset += Int32Array.BYTES_PER_ELEMENT;
138
+ const nPoints = view.getInt32(offset, LITTLE_ENDIAN);
139
+ offset += Int32Array.BYTES_PER_ELEMENT;
140
+ // Create longer indices array by 1 because output format is expected to
141
+ // include the last index as the total number of positions
142
+ const bufferOffset = view.byteOffset + offset;
143
+ const bufferLength = nParts * Int32Array.BYTES_PER_ELEMENT;
144
+ const ringIndices = new Int32Array(nParts + 1);
145
+ ringIndices.set(new Int32Array(view.buffer.slice(bufferOffset, bufferOffset + bufferLength)));
146
+ ringIndices[nParts] = nPoints;
147
+ offset += nParts * Int32Array.BYTES_PER_ELEMENT;
148
+ let xyPositions = null;
149
+ let mPositions = null;
150
+ let zPositions = null;
151
+ [xyPositions, offset] = parsePositions(view, offset, nPoints, 2);
152
+ // Parse Z coordinates
153
+ if (dim === 4) {
154
+ // skip parsing range
155
+ offset += 2 * Float64Array.BYTES_PER_ELEMENT;
156
+ [zPositions, offset] = parsePositions(view, offset, nPoints, 1);
153
157
  }
154
- }
155
-
156
- polygonIndices.push(nPoints);
157
- return {
158
- type,
159
- positions: {
160
- value: positions,
161
- size: dim
162
- },
163
- primitivePolygonIndices: {
164
- value: ringIndices,
165
- size: 1
166
- },
167
- polygonIndices: {
168
- value: new Uint32Array(polygonIndices),
169
- size: 1
158
+ // Parse M coordinates
159
+ if (dim >= 3) {
160
+ // skip parsing range
161
+ offset += 2 * Float64Array.BYTES_PER_ELEMENT;
162
+ [mPositions, offset] = parsePositions(view, offset, nPoints, 1);
170
163
  }
171
- };
164
+ const positions = concatPositions(xyPositions, mPositions, zPositions);
165
+ // parsePoly only accepts type = LineString or Polygon
166
+ if (type === 'LineString') {
167
+ return {
168
+ type,
169
+ positions: { value: positions, size: dim },
170
+ pathIndices: { value: ringIndices, size: 1 }
171
+ };
172
+ }
173
+ // for every ring, determine sign of polygon
174
+ // Use only 2D positions for ring calc
175
+ const polygonIndices = [];
176
+ for (let i = 1; i < ringIndices.length; i++) {
177
+ const startRingIndex = ringIndices[i - 1];
178
+ const endRingIndex = ringIndices[i];
179
+ // @ts-ignore
180
+ const ring = xyPositions.subarray(startRingIndex * 2, endRingIndex * 2);
181
+ const sign = getWindingDirection(ring);
182
+ // A positive sign implies clockwise
183
+ // A clockwise ring is a filled ring
184
+ if (sign > 0) {
185
+ polygonIndices.push(startRingIndex);
186
+ }
187
+ }
188
+ polygonIndices.push(nPoints);
189
+ return {
190
+ type,
191
+ positions: { value: positions, size: dim },
192
+ primitivePolygonIndices: { value: ringIndices, size: 1 },
193
+ // TODO: Dynamically choose Uint32Array over Uint16Array only when
194
+ // necessary. I believe the implementation requires nPoints to be the
195
+ // largest value in the array, so you should be able to use Uint32Array only
196
+ // when nPoints > 65535.
197
+ polygonIndices: { value: new Uint32Array(polygonIndices), size: 1 }
198
+ };
172
199
  }
173
-
200
+ /**
201
+ * Parse a contiguous block of positions into a Float64Array
202
+ *
203
+ * @param view Geometry data
204
+ * @param offset Offset in view
205
+ * @param nPoints Number of points
206
+ * @param dim Input dimension
207
+ * @return Data and offset
208
+ */
174
209
  function parsePositions(view, offset, nPoints, dim) {
175
- const bufferOffset = view.byteOffset + offset;
176
- const bufferLength = nPoints * dim * Float64Array.BYTES_PER_ELEMENT;
177
- return [new Float64Array(view.buffer.slice(bufferOffset, bufferOffset + bufferLength)), offset + bufferLength];
210
+ const bufferOffset = view.byteOffset + offset;
211
+ const bufferLength = nPoints * dim * Float64Array.BYTES_PER_ELEMENT;
212
+ return [
213
+ new Float64Array(view.buffer.slice(bufferOffset, bufferOffset + bufferLength)),
214
+ offset + bufferLength
215
+ ];
178
216
  }
179
-
217
+ /**
218
+ * Concatenate and interleave positions arrays
219
+ * xy positions are interleaved; mPositions, zPositions are their own arrays
220
+ *
221
+ * @param xyPositions 2d positions
222
+ * @param mPositions M positions
223
+ * @param zPositions Z positions
224
+ * @return Combined interleaved positions
225
+ */
226
+ // eslint-disable-next-line complexity
180
227
  function concatPositions(xyPositions, mPositions, zPositions) {
181
- if (!(mPositions || zPositions)) {
182
- return xyPositions;
183
- }
184
-
185
- let arrayLength = xyPositions.length;
186
- let nDim = 2;
187
-
188
- if (zPositions && zPositions.length) {
189
- arrayLength += zPositions.length;
190
- nDim++;
191
- }
192
-
193
- if (mPositions && mPositions.length) {
194
- arrayLength += mPositions.length;
195
- nDim++;
196
- }
197
-
198
- const positions = new Float64Array(arrayLength);
199
-
200
- for (let i = 0; i < xyPositions.length / 2; i++) {
201
- positions[nDim * i] = xyPositions[i * 2];
202
- positions[nDim * i + 1] = xyPositions[i * 2 + 1];
203
- }
204
-
205
- if (zPositions && zPositions.length) {
206
- for (let i = 0; i < zPositions.length; i++) {
207
- positions[nDim * i + 2] = zPositions[i];
228
+ if (!(mPositions || zPositions)) {
229
+ return xyPositions;
230
+ }
231
+ let arrayLength = xyPositions.length;
232
+ let nDim = 2;
233
+ if (zPositions && zPositions.length) {
234
+ arrayLength += zPositions.length;
235
+ nDim++;
208
236
  }
209
- }
210
-
211
- if (mPositions && mPositions.length) {
212
- for (let i = 0; i < mPositions.length; i++) {
213
- positions[nDim * i + (nDim - 1)] = mPositions[i];
237
+ if (mPositions && mPositions.length) {
238
+ arrayLength += mPositions.length;
239
+ nDim++;
214
240
  }
215
- }
216
-
217
- return positions;
241
+ const positions = new Float64Array(arrayLength);
242
+ for (let i = 0; i < xyPositions.length / 2; i++) {
243
+ positions[nDim * i] = xyPositions[i * 2];
244
+ positions[nDim * i + 1] = xyPositions[i * 2 + 1];
245
+ }
246
+ if (zPositions && zPositions.length) {
247
+ for (let i = 0; i < zPositions.length; i++) {
248
+ // If Z coordinates exist; used as third coord in positions array
249
+ positions[nDim * i + 2] = zPositions[i];
250
+ }
251
+ }
252
+ if (mPositions && mPositions.length) {
253
+ for (let i = 0; i < mPositions.length; i++) {
254
+ // M is always last, either 3rd or 4th depending on if Z exists
255
+ positions[nDim * i + (nDim - 1)] = mPositions[i];
256
+ }
257
+ }
258
+ return positions;
218
259
  }
219
-
260
+ /**
261
+ * Returns the direction of the polygon path
262
+ * A positive number is clockwise.
263
+ * A negative number is counter clockwise.
264
+ *
265
+ * @param positions
266
+ * @return Sign of polygon ring
267
+ */
220
268
  function getWindingDirection(positions) {
221
- return Math.sign(getSignedArea(positions));
269
+ return Math.sign(getSignedArea(positions));
222
270
  }
223
-
271
+ /**
272
+ * Get signed area of flat typed array of 2d positions
273
+ *
274
+ * @param positions
275
+ * @return Signed area of polygon ring
276
+ */
224
277
  function getSignedArea(positions) {
225
- let area = 0;
226
- const nCoords = positions.length / 2 - 1;
227
-
228
- for (let i = 0; i < nCoords; i++) {
229
- area += (positions[i * 2] + positions[(i + 1) * 2]) * (positions[i * 2 + 1] - positions[(i + 1) * 2 + 1]);
230
- }
231
-
232
- return area / 2;
278
+ let area = 0;
279
+ // Rings are closed according to shapefile spec
280
+ const nCoords = positions.length / 2 - 1;
281
+ for (let i = 0; i < nCoords; i++) {
282
+ area +=
283
+ (positions[i * 2] + positions[(i + 1) * 2]) *
284
+ (positions[i * 2 + 1] - positions[(i + 1) * 2 + 1]);
285
+ }
286
+ return area / 2;
233
287
  }
234
- //# sourceMappingURL=parse-shp-geometry.js.map
@@ -1,32 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseSHPHeader = void 0;
1
4
  const LITTLE_ENDIAN = true;
2
5
  const BIG_ENDIAN = false;
3
6
  const SHP_MAGIC_NUMBER = 0x0000270a;
4
- export function parseSHPHeader(headerView) {
5
- const header = {
6
- magic: headerView.getInt32(0, BIG_ENDIAN),
7
- length: headerView.getInt32(24, BIG_ENDIAN) * 2,
8
- version: headerView.getInt32(28, LITTLE_ENDIAN),
9
- type: headerView.getInt32(32, LITTLE_ENDIAN),
10
- bbox: {
11
- minX: headerView.getFloat64(36, LITTLE_ENDIAN),
12
- minY: headerView.getFloat64(44, LITTLE_ENDIAN),
13
- minZ: headerView.getFloat64(68, LITTLE_ENDIAN),
14
- minM: headerView.getFloat64(84, LITTLE_ENDIAN),
15
- maxX: headerView.getFloat64(52, LITTLE_ENDIAN),
16
- maxY: headerView.getFloat64(60, LITTLE_ENDIAN),
17
- maxZ: headerView.getFloat64(76, LITTLE_ENDIAN),
18
- maxM: headerView.getFloat64(92, LITTLE_ENDIAN)
7
+ /**
8
+ * Extract the binary header
9
+ * Note: Also used by SHX
10
+ * @param headerView
11
+ * @returns SHPHeader
12
+ */
13
+ function parseSHPHeader(headerView) {
14
+ // Note: The SHP format switches endianness between fields!
15
+ // https://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
16
+ const header = {
17
+ magic: headerView.getInt32(0, BIG_ENDIAN),
18
+ // Length is stored as # of 2-byte words; multiply by 2 to get # of bytes
19
+ length: headerView.getInt32(24, BIG_ENDIAN) * 2,
20
+ version: headerView.getInt32(28, LITTLE_ENDIAN),
21
+ type: headerView.getInt32(32, LITTLE_ENDIAN),
22
+ bbox: {
23
+ minX: headerView.getFloat64(36, LITTLE_ENDIAN),
24
+ minY: headerView.getFloat64(44, LITTLE_ENDIAN),
25
+ minZ: headerView.getFloat64(68, LITTLE_ENDIAN),
26
+ minM: headerView.getFloat64(84, LITTLE_ENDIAN),
27
+ maxX: headerView.getFloat64(52, LITTLE_ENDIAN),
28
+ maxY: headerView.getFloat64(60, LITTLE_ENDIAN),
29
+ maxZ: headerView.getFloat64(76, LITTLE_ENDIAN),
30
+ maxM: headerView.getFloat64(92, LITTLE_ENDIAN)
31
+ }
32
+ };
33
+ if (header.magic !== SHP_MAGIC_NUMBER) {
34
+ // eslint-disable-next-line
35
+ console.error(`SHP file: bad magic number ${header.magic}`);
19
36
  }
20
- };
21
-
22
- if (header.magic !== SHP_MAGIC_NUMBER) {
23
- console.error("SHP file: bad magic number ".concat(header.magic));
24
- }
25
-
26
- if (header.version !== 1000) {
27
- console.error("SHP file: bad version ".concat(header.version));
28
- }
29
-
30
- return header;
37
+ if (header.version !== 1000) {
38
+ // eslint-disable-next-line
39
+ console.error(`SHP file: bad version ${header.version}`);
40
+ }
41
+ return header;
31
42
  }
32
- //# sourceMappingURL=parse-shp-header.js.map
43
+ exports.parseSHPHeader = parseSHPHeader;
@@ -1,9 +1,10 @@
1
1
  import type { BinaryGeometry } from '@loaders.gl/schema';
2
- export declare function parseSHP(arrayBuffer: ArrayBuffer, options?: object): BinaryGeometry[];
2
+ import { SHPLoaderOptions } from './types';
3
+ export declare function parseSHP(arrayBuffer: ArrayBuffer, options?: SHPLoaderOptions): BinaryGeometry[];
3
4
  /**
4
5
  * @param asyncIterator
5
6
  * @param options
6
7
  * @returns
7
8
  */
8
- export declare function parseSHPInBatches(asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>, options?: object): AsyncIterable<BinaryGeometry | object>;
9
+ export declare function parseSHPInBatches(asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>, options?: SHPLoaderOptions): AsyncIterable<BinaryGeometry | object>;
9
10
  //# sourceMappingURL=parse-shp.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-shp.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-shp.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAsDvD,wBAAgB,QAAQ,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,cAAc,EAAE,CAOrF;AAED;;;;GAIG;AACH,wBAAuB,iBAAiB,CACtC,aAAa,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,EACjE,OAAO,CAAC,EAAE,MAAM,GACf,aAAa,CAAC,cAAc,GAAG,MAAM,CAAC,CAqBxC"}
1
+ {"version":3,"file":"parse-shp.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-shp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAIvD,OAAO,EAAC,gBAAgB,EAAC,MAAM,SAAS,CAAC;AAiEzC,wBAAgB,QAAQ,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,cAAc,EAAE,CAO/F;AAED;;;;GAIG;AACH,wBAAuB,iBAAiB,CACtC,aAAa,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,EACjE,OAAO,CAAC,EAAE,gBAAgB,GACzB,aAAa,CAAC,cAAc,GAAG,MAAM,CAAC,CAqBxC"}