@loaders.gl/shapefile 3.4.10 → 3.4.12

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,178 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseSHPInBatches = exports.parseSHP = void 0;
7
- const binary_chunk_reader_1 = __importDefault(require("../streaming/binary-chunk-reader"));
8
- const parse_shp_header_1 = require("./parse-shp-header");
9
- const parse_shp_geometry_1 = require("./parse-shp-geometry");
10
- const LITTLE_ENDIAN = true;
11
- const BIG_ENDIAN = false;
12
- const SHP_HEADER_SIZE = 100;
13
- // According to the spec, the record header is just 8 bytes, but here we set it
14
- // to 12 so that we can also access the record's type
15
- const SHP_RECORD_HEADER_SIZE = 12;
16
- const STATE = {
17
- EXPECTING_HEADER: 0,
18
- EXPECTING_RECORD: 1,
19
- END: 2,
20
- ERROR: 3
21
- };
22
- class SHPParser {
23
- constructor(options) {
24
- this.options = {};
25
- this.binaryReader = new binary_chunk_reader_1.default({ maxRewindBytes: SHP_RECORD_HEADER_SIZE });
26
- this.state = STATE.EXPECTING_HEADER;
27
- this.result = {
28
- geometries: [],
29
- // Initialize with number values to make TS happy
30
- // These are initialized for real in STATE.EXPECTING_HEADER
31
- progress: {
32
- bytesTotal: NaN,
33
- bytesUsed: NaN,
34
- rows: NaN
35
- },
36
- currentIndex: NaN
37
- };
38
- this.options = options;
39
- }
40
- write(arrayBuffer) {
41
- this.binaryReader.write(arrayBuffer);
42
- this.state = parseState(this.state, this.result, this.binaryReader, this.options);
43
- }
44
- end() {
45
- this.binaryReader.end();
46
- this.state = parseState(this.state, this.result, this.binaryReader, this.options);
47
- // this.result.progress.bytesUsed = this.binaryReader.bytesUsed();
48
- if (this.state !== STATE.END) {
49
- this.state = STATE.ERROR;
50
- this.result.error = 'SHP incomplete file';
51
- }
52
- }
53
- }
54
- function parseSHP(arrayBuffer, options) {
55
- const shpParser = new SHPParser(options);
56
- shpParser.write(arrayBuffer);
57
- shpParser.end();
58
- // @ts-ignore
59
- return shpParser.result;
60
- }
61
- exports.parseSHP = parseSHP;
62
- /**
63
- * @param asyncIterator
64
- * @param options
65
- * @returns
66
- */
67
- async function* parseSHPInBatches(asyncIterator, options) {
68
- const parser = new SHPParser(options);
69
- let headerReturned = false;
70
- for await (const arrayBuffer of asyncIterator) {
71
- parser.write(arrayBuffer);
72
- if (!headerReturned && parser.result.header) {
73
- headerReturned = true;
74
- yield parser.result.header;
75
- }
76
- if (parser.result.geometries.length > 0) {
77
- yield parser.result.geometries;
78
- parser.result.geometries = [];
79
- }
80
- }
81
- parser.end();
82
- if (parser.result.geometries.length > 0) {
83
- yield parser.result.geometries;
84
- }
85
- return;
86
- }
87
- exports.parseSHPInBatches = parseSHPInBatches;
88
- /**
89
- * State-machine parser for SHP data
90
- *
91
- * Note that whenever more data is needed, a `return`, not a `break`, is
92
- * necessary, as the `break` keeps the context within `parseState`, while
93
- * `return` releases context so that more data can be written into the
94
- * BinaryChunkReader.
95
- *
96
- * @param state Current state
97
- * @param result An object to hold result data
98
- * @param binaryReader
99
- * @return State at end of current parsing
100
- */
101
- /* eslint-disable complexity, max-depth */
102
- function parseState(state, result, binaryReader, options) {
103
- // eslint-disable-next-line no-constant-condition
104
- while (true) {
105
- try {
106
- switch (state) {
107
- case STATE.ERROR:
108
- case STATE.END:
109
- return state;
110
- case STATE.EXPECTING_HEADER:
111
- // Parse initial file header
112
- const dataView = binaryReader.getDataView(SHP_HEADER_SIZE);
113
- if (!dataView) {
114
- return state;
115
- }
116
- result.header = (0, parse_shp_header_1.parseSHPHeader)(dataView);
117
- result.progress = {
118
- bytesUsed: 0,
119
- bytesTotal: result.header.length,
120
- rows: 0
121
- };
122
- // index numbering starts at 1
123
- result.currentIndex = 1;
124
- state = STATE.EXPECTING_RECORD;
125
- break;
126
- case STATE.EXPECTING_RECORD:
127
- while (binaryReader.hasAvailableBytes(SHP_RECORD_HEADER_SIZE)) {
128
- const recordHeaderView = binaryReader.getDataView(SHP_RECORD_HEADER_SIZE);
129
- const recordHeader = {
130
- recordNumber: recordHeaderView.getInt32(0, BIG_ENDIAN),
131
- // 2 byte words; includes the four words of record header
132
- byteLength: recordHeaderView.getInt32(4, BIG_ENDIAN) * 2,
133
- // This is actually part of the record, not the header...
134
- type: recordHeaderView.getInt32(8, LITTLE_ENDIAN)
135
- };
136
- if (!binaryReader.hasAvailableBytes(recordHeader.byteLength - 4)) {
137
- binaryReader.rewind(SHP_RECORD_HEADER_SIZE);
138
- return state;
139
- }
140
- const invalidRecord = recordHeader.byteLength < 4 ||
141
- recordHeader.type !== result.header?.type ||
142
- recordHeader.recordNumber !== result.currentIndex;
143
- // All records must have at least four bytes (for the record shape type)
144
- if (invalidRecord) {
145
- // Malformed record, try again, advancing just 4 bytes
146
- // Note: this is a rewind because binaryReader.getDataView above
147
- // moved the pointer forward 12 bytes, so rewinding 8 bytes still
148
- // leaves us 4 bytes ahead
149
- binaryReader.rewind(SHP_RECORD_HEADER_SIZE - 4);
150
- }
151
- else {
152
- // Note: type is actually part of the record, not the header, so
153
- // rewind 4 bytes before reading record
154
- binaryReader.rewind(4);
155
- const recordView = binaryReader.getDataView(recordHeader.byteLength);
156
- const geometry = (0, parse_shp_geometry_1.parseRecord)(recordView, options);
157
- result.geometries.push(geometry);
158
- result.currentIndex++;
159
- result.progress.rows = result.currentIndex - 1;
160
- }
161
- }
162
- if (binaryReader.ended) {
163
- state = STATE.END;
164
- }
165
- return state;
166
- default:
167
- state = STATE.ERROR;
168
- result.error = `illegal parser state ${state}`;
169
- return state;
170
- }
171
- }
172
- catch (error) {
173
- state = STATE.ERROR;
174
- result.error = `SHP parsing failed: ${error?.message}`;
175
- return state;
176
- }
177
- }
178
- }
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseShx = void 0;
4
- const parse_shp_header_1 = require("./parse-shp-header");
5
- const SHX_HEADER_SIZE = 100;
6
- const BIG_ENDIAN = false;
7
- /**
8
- * @param arrayBuffer
9
- * @returns SHXOutput
10
- */
11
- function parseShx(arrayBuffer) {
12
- // SHX header is identical to SHP Header
13
- const headerView = new DataView(arrayBuffer, 0, SHX_HEADER_SIZE);
14
- const header = (0, parse_shp_header_1.parseSHPHeader)(headerView);
15
- const contentLength = header.length - SHX_HEADER_SIZE;
16
- const contentView = new DataView(arrayBuffer, SHX_HEADER_SIZE, contentLength);
17
- const offsets = new Int32Array(contentLength);
18
- const lengths = new Int32Array(contentLength);
19
- for (let i = 0; i < contentLength / 8; i++) {
20
- offsets[i] = contentView.getInt32(i * 8, BIG_ENDIAN);
21
- lengths[i] = contentView.getInt32(i * 8 + 4, BIG_ENDIAN);
22
- }
23
- return {
24
- offsets,
25
- lengths
26
- };
27
- }
28
- exports.parseShx = parseShx;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,161 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class BinaryChunkReader {
4
- constructor(options) {
5
- const { maxRewindBytes = 0 } = options || {};
6
- /** current global offset into current array buffer*/
7
- this.offset = 0;
8
- /** current buffer from iterator */
9
- this.arrayBuffers = [];
10
- this.ended = false;
11
- /** bytes behind offset to hold on to */
12
- this.maxRewindBytes = maxRewindBytes;
13
- }
14
- /**
15
- * @param arrayBuffer
16
- */
17
- write(arrayBuffer) {
18
- this.arrayBuffers.push(arrayBuffer);
19
- }
20
- end() {
21
- this.arrayBuffers = [];
22
- this.ended = true;
23
- }
24
- /**
25
- * Has enough bytes available in array buffers
26
- *
27
- * @param bytes Number of bytes
28
- * @return boolean
29
- */
30
- hasAvailableBytes(bytes) {
31
- let bytesAvailable = -this.offset;
32
- for (const arrayBuffer of this.arrayBuffers) {
33
- bytesAvailable += arrayBuffer.byteLength;
34
- if (bytesAvailable >= bytes) {
35
- return true;
36
- }
37
- }
38
- return false;
39
- }
40
- /**
41
- * Find offsets of byte ranges within this.arrayBuffers
42
- *
43
- * @param bytes Byte length to read
44
- * @return Arrays with byte ranges pointing to this.arrayBuffers, Output type is nested array, e.g. [ [0, [1, 2]], ...]
45
- */
46
- findBufferOffsets(bytes) {
47
- let offset = -this.offset;
48
- const selectedBuffers = [];
49
- for (let i = 0; i < this.arrayBuffers.length; i++) {
50
- const buf = this.arrayBuffers[i];
51
- // Current buffer isn't long enough to reach global offset
52
- if (offset + buf.byteLength <= 0) {
53
- offset += buf.byteLength;
54
- // eslint-disable-next-line no-continue
55
- continue;
56
- }
57
- // Find start/end offsets for this buffer
58
- // When offset < 0, need to skip over Math.abs(offset) bytes
59
- // When offset > 0, implies bytes in previous buffer, start at 0
60
- const start = offset <= 0 ? Math.abs(offset) : 0;
61
- let end;
62
- // Length of requested bytes is contained in current buffer
63
- if (start + bytes <= buf.byteLength) {
64
- end = start + bytes;
65
- selectedBuffers.push([i, [start, end]]);
66
- return selectedBuffers;
67
- }
68
- // Will need to look into next buffer
69
- end = buf.byteLength;
70
- selectedBuffers.push([i, [start, end]]);
71
- // Need to read fewer bytes in next iter
72
- bytes -= buf.byteLength - start;
73
- offset += buf.byteLength;
74
- }
75
- // Should only finish loop if exhausted all arrays
76
- return null;
77
- }
78
- /**
79
- * Get the required number of bytes from the iterator
80
- *
81
- * @param bytes Number of bytes
82
- * @return DataView with data
83
- */
84
- getDataView(bytes) {
85
- const bufferOffsets = this.findBufferOffsets(bytes);
86
- // return `null` if not enough data, except if end() already called, in
87
- // which case throw an error.
88
- if (!bufferOffsets && this.ended) {
89
- throw new Error('binary data exhausted');
90
- }
91
- if (!bufferOffsets) {
92
- // @ts-ignore
93
- return null;
94
- }
95
- // If only one arrayBuffer needed, return DataView directly
96
- if (bufferOffsets.length === 1) {
97
- const [bufferIndex, [start, end]] = bufferOffsets[0];
98
- const arrayBuffer = this.arrayBuffers[bufferIndex];
99
- const view = new DataView(arrayBuffer, start, end - start);
100
- this.offset += bytes;
101
- this.disposeBuffers();
102
- return view;
103
- }
104
- // Concatenate portions of multiple ArrayBuffers
105
- const view = new DataView(this._combineArrayBuffers(bufferOffsets));
106
- this.offset += bytes;
107
- this.disposeBuffers();
108
- return view;
109
- }
110
- /**
111
- * Dispose of old array buffers
112
- */
113
- disposeBuffers() {
114
- while (this.arrayBuffers.length > 0 &&
115
- this.offset - this.maxRewindBytes >= this.arrayBuffers[0].byteLength) {
116
- this.offset -= this.arrayBuffers[0].byteLength;
117
- this.arrayBuffers.shift();
118
- }
119
- }
120
- /**
121
- * Copy multiple ArrayBuffers into one contiguous ArrayBuffer
122
- *
123
- * In contrast to concatenateArrayBuffers, this only copies the necessary
124
- * portions of the source arrays, rather than first copying the entire arrays
125
- * then taking a part of them.
126
- *
127
- * @param bufferOffsets List of internal array offsets
128
- * @return New contiguous ArrayBuffer
129
- */
130
- _combineArrayBuffers(bufferOffsets) {
131
- let byteLength = 0;
132
- for (const bufferOffset of bufferOffsets) {
133
- const [start, end] = bufferOffset[1];
134
- byteLength += end - start;
135
- }
136
- const result = new Uint8Array(byteLength);
137
- // Copy the subarrays
138
- let resultOffset = 0;
139
- for (const bufferOffset of bufferOffsets) {
140
- const [bufferIndex, [start, end]] = bufferOffset;
141
- const sourceArray = new Uint8Array(this.arrayBuffers[bufferIndex]);
142
- result.set(sourceArray.subarray(start, end), resultOffset);
143
- resultOffset += end - start;
144
- }
145
- return result.buffer;
146
- }
147
- /**
148
- * @param bytes
149
- */
150
- skip(bytes) {
151
- this.offset += bytes;
152
- }
153
- /**
154
- * @param bytes
155
- */
156
- rewind(bytes) {
157
- // TODO - only works if offset is already set
158
- this.offset -= bytes;
159
- }
160
- }
161
- exports.default = BinaryChunkReader;
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class BinaryReader {
4
- constructor(arrayBuffer) {
5
- /** current global (stream) offset */
6
- this.offset = 0;
7
- /** current buffer from iterator */
8
- this.arrayBuffer = arrayBuffer;
9
- }
10
- /**
11
- * Checks if there are available bytes in data
12
- *
13
- * @param bytes
14
- * @returns boolean
15
- */
16
- hasAvailableBytes(bytes) {
17
- return this.arrayBuffer.byteLength - this.offset >= bytes;
18
- }
19
- /**
20
- * Get the required number of bytes from the iterator
21
- *
22
- * @param bytes
23
- * @returns Dataview
24
- */
25
- getDataView(bytes) {
26
- if (bytes && !this.hasAvailableBytes(bytes)) {
27
- throw new Error('binary data exhausted');
28
- }
29
- const dataView = bytes
30
- ? new DataView(this.arrayBuffer, this.offset, bytes)
31
- : new DataView(this.arrayBuffer, this.offset);
32
- this.offset += bytes;
33
- return dataView;
34
- }
35
- /**
36
- * Skipping
37
- *
38
- * @param bytes
39
- */
40
- skip(bytes) {
41
- this.offset += bytes;
42
- }
43
- /**
44
- * Rewinding
45
- *
46
- * @param bytes
47
- */
48
- rewind(bytes) {
49
- this.offset -= bytes;
50
- }
51
- }
52
- exports.default = BinaryReader;
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.zipBatchIterators = void 0;
4
- /**
5
- * Zip two iterators together
6
- *
7
- * @param iterator1
8
- * @param iterator2
9
- */
10
- async function* zipBatchIterators(iterator1, iterator2) {
11
- let batch1 = [];
12
- let batch2 = [];
13
- let iterator1Done = false;
14
- let iterator2Done = false;
15
- // TODO - one could let all iterators flow at full speed using `Promise.race`
16
- // however we might end up with a big temporary buffer
17
- while (!iterator1Done && !iterator2Done) {
18
- if (batch1.length === 0 && !iterator1Done) {
19
- const { value, done } = await iterator1.next();
20
- if (done) {
21
- iterator1Done = true;
22
- }
23
- else {
24
- batch1 = value;
25
- }
26
- }
27
- else if (batch2.length === 0 && !iterator2Done) {
28
- const { value, done } = await iterator2.next();
29
- if (done) {
30
- iterator2Done = true;
31
- }
32
- else {
33
- batch2 = value;
34
- }
35
- }
36
- const batch = extractBatch(batch1, batch2);
37
- if (batch) {
38
- yield batch;
39
- }
40
- }
41
- }
42
- exports.zipBatchIterators = zipBatchIterators;
43
- /**
44
- * Extract batch of same length from two batches
45
- *
46
- * @param batch1
47
- * @param batch2
48
- * @return array | null
49
- */
50
- function extractBatch(batch1, batch2) {
51
- const batchLength = Math.min(batch1.length, batch2.length);
52
- if (batchLength === 0) {
53
- return null;
54
- }
55
- // Non interleaved arrays
56
- const batch = [batch1.slice(0, batchLength), batch2.slice(0, batchLength)];
57
- // Modify the 2 batches
58
- batch1.splice(0, batchLength);
59
- batch2.splice(0, batchLength);
60
- return batch;
61
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._typecheckShapefileLoader = exports.ShapefileLoader = void 0;
4
- const shp_loader_1 = require("./shp-loader");
5
- const parse_shapefile_1 = require("./lib/parsers/parse-shapefile");
6
- // __VERSION__ is injected by babel-plugin-version-inline
7
- // @ts-ignore TS2304: Cannot find name '__VERSION__'.
8
- const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
9
- /**
10
- * Shapefile loader
11
- * @note Shapefile is multifile format and requires providing additional files
12
- */
13
- exports.ShapefileLoader = {
14
- name: 'Shapefile',
15
- id: 'shapefile',
16
- module: 'shapefile',
17
- version: VERSION,
18
- category: 'geometry',
19
- extensions: ['shp'],
20
- mimeTypes: ['application/octet-stream'],
21
- tests: [new Uint8Array(shp_loader_1.SHP_MAGIC_NUMBER).buffer],
22
- options: {
23
- shapefile: {},
24
- shp: {
25
- _maxDimensions: 4
26
- }
27
- },
28
- parse: parse_shapefile_1.parseShapefile,
29
- parseInBatches: parse_shapefile_1.parseShapefileInBatches
30
- };
31
- exports._typecheckShapefileLoader = exports.ShapefileLoader;
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SHPLoader = exports.SHPWorkerLoader = exports.SHP_MAGIC_NUMBER = void 0;
4
- const parse_shp_1 = require("./lib/parsers/parse-shp");
5
- // __VERSION__ is injected by babel-plugin-version-inline
6
- // @ts-ignore TS2304: Cannot find name '__VERSION__'.
7
- const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
8
- exports.SHP_MAGIC_NUMBER = [0x00, 0x00, 0x27, 0x0a];
9
- /**
10
- * SHP file loader
11
- */
12
- exports.SHPWorkerLoader = {
13
- name: 'SHP',
14
- id: 'shp',
15
- module: 'shapefile',
16
- version: VERSION,
17
- worker: true,
18
- category: 'geometry',
19
- extensions: ['shp'],
20
- mimeTypes: ['application/octet-stream'],
21
- // ISSUE: This also identifies SHX files, which are identical to SHP for the first 100 bytes...
22
- tests: [new Uint8Array(exports.SHP_MAGIC_NUMBER).buffer],
23
- options: {
24
- shp: {
25
- _maxDimensions: 4
26
- }
27
- }
28
- };
29
- /** SHP file loader */
30
- exports.SHPLoader = {
31
- ...exports.SHPWorkerLoader,
32
- parse: async (arrayBuffer, options) => (0, parse_shp_1.parseSHP)(arrayBuffer, options),
33
- parseSync: parse_shp_1.parseSHP,
34
- parseInBatches: parse_shp_1.parseSHPInBatches
35
- };
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const dbf_loader_1 = require("../dbf-loader");
4
- const loader_utils_1 = require("@loaders.gl/loader-utils");
5
- (0, loader_utils_1.createLoaderWorker)(dbf_loader_1.DBFLoader);
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const shp_loader_1 = require("../shp-loader");
4
- const loader_utils_1 = require("@loaders.gl/loader-utils");
5
- (0, loader_utils_1.createLoaderWorker)(shp_loader_1.SHPLoader);