@loaders.gl/shapefile 3.1.3 → 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.
- package/dist/bundle.js +2 -2
- package/dist/bundle.js.map +1 -0
- package/dist/dbf-loader.js +20 -29
- package/dist/dbf-loader.js.map +1 -0
- package/dist/dbf-worker.js +1 -1
- package/dist/index.js +4 -11
- package/dist/index.js.map +1 -0
- package/dist/lib/parsers/parse-dbf.js +264 -300
- package/dist/lib/parsers/parse-dbf.js.map +1 -0
- package/dist/lib/parsers/parse-shapefile.js +209 -231
- package/dist/lib/parsers/parse-shapefile.js.map +1 -0
- package/dist/lib/parsers/parse-shp-geometry.js +212 -265
- package/dist/lib/parsers/parse-shp-geometry.js.map +1 -0
- package/dist/lib/parsers/parse-shp-header.js +27 -38
- package/dist/lib/parsers/parse-shp-header.js.map +1 -0
- package/dist/lib/parsers/parse-shp.js +136 -152
- package/dist/lib/parsers/parse-shp.js.map +1 -0
- package/dist/lib/parsers/parse-shx.js +19 -25
- package/dist/lib/parsers/parse-shx.js.map +1 -0
- package/dist/lib/streaming/binary-chunk-reader.js +128 -152
- package/dist/lib/streaming/binary-chunk-reader.js.map +1 -0
- package/dist/lib/streaming/binary-reader.js +33 -50
- package/dist/lib/streaming/binary-reader.js.map +1 -0
- package/dist/lib/streaming/zip-batch-iterators.js +48 -57
- package/dist/lib/streaming/zip-batch-iterators.js.map +1 -0
- package/dist/shapefile-loader.js +22 -30
- package/dist/shapefile-loader.js.map +1 -0
- package/dist/shp-loader.js +22 -32
- package/dist/shp-loader.js.map +1 -0
- package/dist/shp-worker.js +1 -1
- package/dist/workers/dbf-worker.js +4 -5
- package/dist/workers/dbf-worker.js.map +1 -0
- package/dist/workers/shp-worker.js +4 -5
- package/dist/workers/shp-worker.js.map +1 -0
- package/package.json +9 -9
- package/dist/es5/bundle.js +0 -7
- package/dist/es5/bundle.js.map +0 -1
- package/dist/es5/dbf-loader.js +0 -68
- package/dist/es5/dbf-loader.js.map +0 -1
- package/dist/es5/index.js +0 -42
- package/dist/es5/index.js.map +0 -1
- package/dist/es5/lib/parsers/parse-dbf.js +0 -454
- package/dist/es5/lib/parsers/parse-dbf.js.map +0 -1
- package/dist/es5/lib/parsers/parse-shapefile.js +0 -497
- package/dist/es5/lib/parsers/parse-shapefile.js.map +0 -1
- package/dist/es5/lib/parsers/parse-shp-geometry.js +0 -288
- package/dist/es5/lib/parsers/parse-shp-geometry.js.map +0 -1
- package/dist/es5/lib/parsers/parse-shp-header.js +0 -39
- package/dist/es5/lib/parsers/parse-shp-header.js.map +0 -1
- package/dist/es5/lib/parsers/parse-shp.js +0 -283
- package/dist/es5/lib/parsers/parse-shp.js.map +0 -1
- package/dist/es5/lib/parsers/parse-shx.js +0 -31
- package/dist/es5/lib/parsers/parse-shx.js.map +0 -1
- package/dist/es5/lib/streaming/binary-chunk-reader.js +0 -218
- package/dist/es5/lib/streaming/binary-chunk-reader.js.map +0 -1
- package/dist/es5/lib/streaming/binary-reader.js +0 -56
- package/dist/es5/lib/streaming/binary-reader.js.map +0 -1
- package/dist/es5/lib/streaming/zip-batch-iterators.js +0 -118
- package/dist/es5/lib/streaming/zip-batch-iterators.js.map +0 -1
- package/dist/es5/shapefile-loader.js +0 -34
- package/dist/es5/shapefile-loader.js.map +0 -1
- package/dist/es5/shp-loader.js +0 -71
- package/dist/es5/shp-loader.js.map +0 -1
- package/dist/es5/workers/dbf-worker.js +0 -8
- package/dist/es5/workers/dbf-worker.js.map +0 -1
- package/dist/es5/workers/shp-worker.js +0 -8
- package/dist/es5/workers/shp-worker.js.map +0 -1
- package/dist/esm/bundle.js +0 -5
- package/dist/esm/bundle.js.map +0 -1
- package/dist/esm/dbf-loader.js +0 -23
- package/dist/esm/dbf-loader.js.map +0 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/lib/parsers/parse-dbf.js +0 -299
- package/dist/esm/lib/parsers/parse-dbf.js.map +0 -1
- package/dist/esm/lib/parsers/parse-shapefile.js +0 -226
- package/dist/esm/lib/parsers/parse-shapefile.js.map +0 -1
- package/dist/esm/lib/parsers/parse-shp-geometry.js +0 -234
- package/dist/esm/lib/parsers/parse-shp-geometry.js.map +0 -1
- package/dist/esm/lib/parsers/parse-shp-header.js +0 -32
- package/dist/esm/lib/parsers/parse-shp-header.js.map +0 -1
- package/dist/esm/lib/parsers/parse-shp.js +0 -154
- package/dist/esm/lib/parsers/parse-shp.js.map +0 -1
- package/dist/esm/lib/parsers/parse-shx.js +0 -22
- package/dist/esm/lib/parsers/parse-shx.js.map +0 -1
- package/dist/esm/lib/streaming/binary-chunk-reader.js +0 -137
- package/dist/esm/lib/streaming/binary-chunk-reader.js.map +0 -1
- package/dist/esm/lib/streaming/binary-reader.js +0 -35
- package/dist/esm/lib/streaming/binary-reader.js.map +0 -1
- package/dist/esm/lib/streaming/zip-batch-iterators.js +0 -52
- package/dist/esm/lib/streaming/zip-batch-iterators.js.map +0 -1
- package/dist/esm/shapefile-loader.js +0 -23
- package/dist/esm/shapefile-loader.js.map +0 -1
- package/dist/esm/shp-loader.js +0 -25
- package/dist/esm/shp-loader.js.map +0 -1
- package/dist/esm/workers/dbf-worker.js +0 -4
- package/dist/esm/workers/dbf-worker.js.map +0 -1
- package/dist/esm/workers/shp-worker.js +0 -4
- package/dist/esm/workers/shp-worker.js.map +0 -1
|
@@ -1,170 +1,154 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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");
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import BinaryChunkReader from '../streaming/binary-chunk-reader';
|
|
3
|
+
import { parseSHPHeader } from './parse-shp-header';
|
|
4
|
+
import { parseRecord } from './parse-shp-geometry';
|
|
10
5
|
const LITTLE_ENDIAN = true;
|
|
11
6
|
const BIG_ENDIAN = false;
|
|
12
7
|
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
8
|
const SHP_RECORD_HEADER_SIZE = 12;
|
|
16
9
|
const STATE = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
EXPECTING_HEADER: 0,
|
|
11
|
+
EXPECTING_RECORD: 1,
|
|
12
|
+
END: 2,
|
|
13
|
+
ERROR: 3
|
|
21
14
|
};
|
|
15
|
+
|
|
22
16
|
class SHPParser {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
17
|
+
constructor(options) {
|
|
18
|
+
_defineProperty(this, "options", {});
|
|
19
|
+
|
|
20
|
+
_defineProperty(this, "binaryReader", new BinaryChunkReader({
|
|
21
|
+
maxRewindBytes: SHP_RECORD_HEADER_SIZE
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
_defineProperty(this, "state", STATE.EXPECTING_HEADER);
|
|
25
|
+
|
|
26
|
+
_defineProperty(this, "result", {
|
|
27
|
+
geometries: []
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
write(arrayBuffer) {
|
|
34
|
+
this.binaryReader.write(arrayBuffer);
|
|
35
|
+
this.state = parseState(this.state, this.result, this.binaryReader, this.options);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
end() {
|
|
39
|
+
this.binaryReader.end();
|
|
40
|
+
this.state = parseState(this.state, this.result, this.binaryReader, this.options);
|
|
41
|
+
|
|
42
|
+
if (this.state !== STATE.END) {
|
|
43
|
+
this.state = STATE.ERROR;
|
|
44
|
+
this.result.error = 'SHP incomplete file';
|
|
44
45
|
}
|
|
46
|
+
}
|
|
47
|
+
|
|
45
48
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
|
|
50
|
+
export function parseSHP(arrayBuffer, options) {
|
|
51
|
+
const shpParser = new SHPParser(options);
|
|
52
|
+
shpParser.write(arrayBuffer);
|
|
53
|
+
shpParser.end();
|
|
54
|
+
return shpParser.result;
|
|
52
55
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
parser.write(arrayBuffer);
|
|
64
|
-
if (!headerReturned && parser.result.header) {
|
|
65
|
-
headerReturned = true;
|
|
66
|
-
yield parser.result.header;
|
|
67
|
-
}
|
|
68
|
-
if (parser.result.geometries.length > 0) {
|
|
69
|
-
yield parser.result.geometries;
|
|
70
|
-
parser.result.geometries = [];
|
|
71
|
-
}
|
|
56
|
+
export async function* parseSHPInBatches(asyncIterator, options) {
|
|
57
|
+
const parser = new SHPParser(options);
|
|
58
|
+
let headerReturned = false;
|
|
59
|
+
|
|
60
|
+
for await (const arrayBuffer of asyncIterator) {
|
|
61
|
+
parser.write(arrayBuffer);
|
|
62
|
+
|
|
63
|
+
if (!headerReturned && parser.result.header) {
|
|
64
|
+
headerReturned = true;
|
|
65
|
+
yield parser.result.header;
|
|
72
66
|
}
|
|
73
|
-
|
|
67
|
+
|
|
74
68
|
if (parser.result.geometries.length > 0) {
|
|
75
|
-
|
|
69
|
+
yield parser.result.geometries;
|
|
70
|
+
parser.result.geometries = [];
|
|
76
71
|
}
|
|
77
|
-
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
parser.end();
|
|
75
|
+
|
|
76
|
+
if (parser.result.geometries.length > 0) {
|
|
77
|
+
yield parser.result.geometries;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return;
|
|
78
81
|
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* State-machine parser for SHP data
|
|
82
|
-
*
|
|
83
|
-
* Note that whenever more data is needed, a `return`, not a `break`, is
|
|
84
|
-
* necessary, as the `break` keeps the context within `parseState`, while
|
|
85
|
-
* `return` releases context so that more data can be written into the
|
|
86
|
-
* BinaryChunkReader.
|
|
87
|
-
*
|
|
88
|
-
* @param state Current state
|
|
89
|
-
* @param result An object to hold result data
|
|
90
|
-
* @param binaryReader
|
|
91
|
-
* @return State at end of current parsing
|
|
92
|
-
*/
|
|
93
|
-
/* eslint-disable complexity, max-depth */
|
|
82
|
+
|
|
94
83
|
function parseState(state, result, binaryReader, options) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return state;
|
|
107
|
-
}
|
|
108
|
-
result.header = (0, parse_shp_header_1.parseSHPHeader)(dataView);
|
|
109
|
-
result.progress = {
|
|
110
|
-
bytesUsed: 0,
|
|
111
|
-
bytesTotal: result.header.length,
|
|
112
|
-
rows: 0
|
|
113
|
-
};
|
|
114
|
-
// index numbering starts at 1
|
|
115
|
-
result.currentIndex = 1;
|
|
116
|
-
state = STATE.EXPECTING_RECORD;
|
|
117
|
-
break;
|
|
118
|
-
case STATE.EXPECTING_RECORD:
|
|
119
|
-
while (binaryReader.hasAvailableBytes(SHP_RECORD_HEADER_SIZE)) {
|
|
120
|
-
const recordHeaderView = binaryReader.getDataView(SHP_RECORD_HEADER_SIZE);
|
|
121
|
-
const recordHeader = {
|
|
122
|
-
recordNumber: recordHeaderView.getInt32(0, BIG_ENDIAN),
|
|
123
|
-
// 2 byte words; includes the four words of record header
|
|
124
|
-
byteLength: recordHeaderView.getInt32(4, BIG_ENDIAN) * 2,
|
|
125
|
-
// This is actually part of the record, not the header...
|
|
126
|
-
type: recordHeaderView.getInt32(8, LITTLE_ENDIAN)
|
|
127
|
-
};
|
|
128
|
-
if (!binaryReader.hasAvailableBytes(recordHeader.byteLength - 4)) {
|
|
129
|
-
binaryReader.rewind(SHP_RECORD_HEADER_SIZE);
|
|
130
|
-
return state;
|
|
131
|
-
}
|
|
132
|
-
const invalidRecord = recordHeader.byteLength < 4 ||
|
|
133
|
-
recordHeader.type !== result.header.type ||
|
|
134
|
-
recordHeader.recordNumber !== result.currentIndex;
|
|
135
|
-
// All records must have at least four bytes (for the record shape type)
|
|
136
|
-
if (invalidRecord) {
|
|
137
|
-
// Malformed record, try again, advancing just 4 bytes
|
|
138
|
-
// Note: this is a rewind because binaryReader.getDataView above
|
|
139
|
-
// moved the pointer forward 12 bytes, so rewinding 8 bytes still
|
|
140
|
-
// leaves us 4 bytes ahead
|
|
141
|
-
binaryReader.rewind(SHP_RECORD_HEADER_SIZE - 4);
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
// Note: type is actually part of the record, not the header, so
|
|
145
|
-
// rewind 4 bytes before reading record
|
|
146
|
-
binaryReader.rewind(4);
|
|
147
|
-
const recordView = binaryReader.getDataView(recordHeader.byteLength);
|
|
148
|
-
const geometry = (0, parse_shp_geometry_1.parseRecord)(recordView, options);
|
|
149
|
-
result.geometries.push(geometry);
|
|
150
|
-
result.currentIndex++;
|
|
151
|
-
result.progress.rows = result.currentIndex - 1;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
if (binaryReader.ended) {
|
|
155
|
-
state = STATE.END;
|
|
156
|
-
}
|
|
157
|
-
return state;
|
|
158
|
-
default:
|
|
159
|
-
state = STATE.ERROR;
|
|
160
|
-
result.error = `illegal parser state ${state}`;
|
|
161
|
-
return state;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
catch (error) {
|
|
165
|
-
state = STATE.ERROR;
|
|
166
|
-
result.error = `SHP parsing failed: ${error?.message}`;
|
|
84
|
+
while (true) {
|
|
85
|
+
try {
|
|
86
|
+
switch (state) {
|
|
87
|
+
case STATE.ERROR:
|
|
88
|
+
case STATE.END:
|
|
89
|
+
return state;
|
|
90
|
+
|
|
91
|
+
case STATE.EXPECTING_HEADER:
|
|
92
|
+
const dataView = binaryReader.getDataView(SHP_HEADER_SIZE);
|
|
93
|
+
|
|
94
|
+
if (!dataView) {
|
|
167
95
|
return state;
|
|
168
|
-
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
result.header = parseSHPHeader(dataView);
|
|
99
|
+
result.progress = {
|
|
100
|
+
bytesUsed: 0,
|
|
101
|
+
bytesTotal: result.header.length,
|
|
102
|
+
rows: 0
|
|
103
|
+
};
|
|
104
|
+
result.currentIndex = 1;
|
|
105
|
+
state = STATE.EXPECTING_RECORD;
|
|
106
|
+
break;
|
|
107
|
+
|
|
108
|
+
case STATE.EXPECTING_RECORD:
|
|
109
|
+
while (binaryReader.hasAvailableBytes(SHP_RECORD_HEADER_SIZE)) {
|
|
110
|
+
const recordHeaderView = binaryReader.getDataView(SHP_RECORD_HEADER_SIZE);
|
|
111
|
+
const recordHeader = {
|
|
112
|
+
recordNumber: recordHeaderView.getInt32(0, BIG_ENDIAN),
|
|
113
|
+
byteLength: recordHeaderView.getInt32(4, BIG_ENDIAN) * 2,
|
|
114
|
+
type: recordHeaderView.getInt32(8, LITTLE_ENDIAN)
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (!binaryReader.hasAvailableBytes(recordHeader.byteLength - 4)) {
|
|
118
|
+
binaryReader.rewind(SHP_RECORD_HEADER_SIZE);
|
|
119
|
+
return state;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const invalidRecord = recordHeader.byteLength < 4 || recordHeader.type !== result.header.type || recordHeader.recordNumber !== result.currentIndex;
|
|
123
|
+
|
|
124
|
+
if (invalidRecord) {
|
|
125
|
+
binaryReader.rewind(SHP_RECORD_HEADER_SIZE - 4);
|
|
126
|
+
} else {
|
|
127
|
+
binaryReader.rewind(4);
|
|
128
|
+
const recordView = binaryReader.getDataView(recordHeader.byteLength);
|
|
129
|
+
const geometry = parseRecord(recordView, options);
|
|
130
|
+
result.geometries.push(geometry);
|
|
131
|
+
result.currentIndex++;
|
|
132
|
+
result.progress.rows = result.currentIndex - 1;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (binaryReader.ended) {
|
|
137
|
+
state = STATE.END;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return state;
|
|
141
|
+
|
|
142
|
+
default:
|
|
143
|
+
state = STATE.ERROR;
|
|
144
|
+
result.error = "illegal parser state ".concat(state);
|
|
145
|
+
return state;
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
state = STATE.ERROR;
|
|
149
|
+
result.error = "SHP parsing failed: ".concat(error === null || error === void 0 ? void 0 : error.message);
|
|
150
|
+
return state;
|
|
169
151
|
}
|
|
152
|
+
}
|
|
170
153
|
}
|
|
154
|
+
//# sourceMappingURL=parse-shp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/parsers/parse-shp.ts"],"names":["BinaryChunkReader","parseSHPHeader","parseRecord","LITTLE_ENDIAN","BIG_ENDIAN","SHP_HEADER_SIZE","SHP_RECORD_HEADER_SIZE","STATE","EXPECTING_HEADER","EXPECTING_RECORD","END","ERROR","SHPParser","constructor","options","maxRewindBytes","geometries","write","arrayBuffer","binaryReader","state","parseState","result","end","error","parseSHP","shpParser","parseSHPInBatches","asyncIterator","parser","headerReturned","header","length","dataView","getDataView","progress","bytesUsed","bytesTotal","rows","currentIndex","hasAvailableBytes","recordHeaderView","recordHeader","recordNumber","getInt32","byteLength","type","rewind","invalidRecord","recordView","geometry","push","ended","message"],"mappings":";AAEA,OAAOA,iBAAP,MAA8B,kCAA9B;AACA,SAAQC,cAAR,QAA6B,oBAA7B;AACA,SAAQC,WAAR,QAA0B,sBAA1B;AAEA,MAAMC,aAAa,GAAG,IAAtB;AACA,MAAMC,UAAU,GAAG,KAAnB;AAEA,MAAMC,eAAe,GAAG,GAAxB;AAGA,MAAMC,sBAAsB,GAAG,EAA/B;AAEA,MAAMC,KAAK,GAAG;AACZC,EAAAA,gBAAgB,EAAE,CADN;AAEZC,EAAAA,gBAAgB,EAAE,CAFN;AAGZC,EAAAA,GAAG,EAAE,CAHO;AAIZC,EAAAA,KAAK,EAAE;AAJK,CAAd;;AAaA,MAAMC,SAAN,CAAgB;AAQdC,EAAAA,WAAW,CAACC,OAAD,EAA0B;AAAA,qCAPrB,EAOqB;;AAAA,0CANtB,IAAId,iBAAJ,CAAsB;AAACe,MAAAA,cAAc,EAAET;AAAjB,KAAtB,CAMsB;;AAAA,mCAL7BC,KAAK,CAACC,gBAKuB;;AAAA,oCAJjB;AAClBQ,MAAAA,UAAU,EAAE;AADM,KAIiB;;AACnC,SAAKF,OAAL,GAAeA,OAAf;AACD;;AAEDG,EAAAA,KAAK,CAACC,WAAD,EAA2B;AAC9B,SAAKC,YAAL,CAAkBF,KAAlB,CAAwBC,WAAxB;AACA,SAAKE,KAAL,GAAaC,UAAU,CAAC,KAAKD,KAAN,EAAa,KAAKE,MAAlB,EAA0B,KAAKH,YAA/B,EAA6C,KAAKL,OAAlD,CAAvB;AACD;;AAEDS,EAAAA,GAAG,GAAG;AACJ,SAAKJ,YAAL,CAAkBI,GAAlB;AACA,SAAKH,KAAL,GAAaC,UAAU,CAAC,KAAKD,KAAN,EAAa,KAAKE,MAAlB,EAA0B,KAAKH,YAA/B,EAA6C,KAAKL,OAAlD,CAAvB;;AAEA,QAAI,KAAKM,KAAL,K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{LoaderOptions} from '@loaders.gl/loader-utils';\nimport type {BinaryGeometry} from '@loaders.gl/schema';\nimport BinaryChunkReader from '../streaming/binary-chunk-reader';\nimport {parseSHPHeader} from './parse-shp-header';\nimport {parseRecord} from './parse-shp-geometry';\n\nconst LITTLE_ENDIAN = true;\nconst BIG_ENDIAN = false;\n\nconst SHP_HEADER_SIZE = 100;\n// According to the spec, the record header is just 8 bytes, but here we set it\n// to 12 so that we can also access the record's type\nconst SHP_RECORD_HEADER_SIZE = 12;\n\nconst STATE = {\n EXPECTING_HEADER: 0,\n EXPECTING_RECORD: 1,\n END: 2,\n ERROR: 3\n};\n\ntype SHPResult = {\n geometries: [];\n header?: {};\n error?: string;\n};\n\nclass SHPParser {\n options?: any = {};\n binaryReader = new BinaryChunkReader({maxRewindBytes: SHP_RECORD_HEADER_SIZE});\n state = STATE.EXPECTING_HEADER;\n result: SHPResult = {\n geometries: []\n };\n\n constructor(options?: LoaderOptions) {\n this.options = options;\n }\n\n write(arrayBuffer: ArrayBuffer) {\n this.binaryReader.write(arrayBuffer);\n this.state = parseState(this.state, this.result, this.binaryReader, this.options);\n }\n\n end() {\n this.binaryReader.end();\n this.state = parseState(this.state, this.result, this.binaryReader, this.options);\n // this.result.progress.bytesUsed = this.binaryReader.bytesUsed();\n if (this.state !== STATE.END) {\n this.state = STATE.ERROR;\n this.result.error = 'SHP incomplete file';\n }\n }\n}\n\nexport function parseSHP(arrayBuffer: ArrayBuffer, options?: object): BinaryGeometry[] {\n const shpParser = new SHPParser(options);\n shpParser.write(arrayBuffer);\n shpParser.end();\n\n // @ts-ignore\n return shpParser.result;\n}\n\n/**\n * @param asyncIterator\n * @param options\n * @returns\n */\nexport async function* parseSHPInBatches(\n asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,\n options?: object\n): AsyncIterable<BinaryGeometry | object> {\n const parser = new SHPParser(options);\n let headerReturned = false;\n for await (const arrayBuffer of asyncIterator) {\n parser.write(arrayBuffer);\n if (!headerReturned && parser.result.header) {\n headerReturned = true;\n yield parser.result.header;\n }\n\n if (parser.result.geometries.length > 0) {\n yield parser.result.geometries;\n parser.result.geometries = [];\n }\n }\n parser.end();\n if (parser.result.geometries.length > 0) {\n yield parser.result.geometries;\n }\n\n return;\n}\n\n/**\n * State-machine parser for SHP data\n *\n * Note that whenever more data is needed, a `return`, not a `break`, is\n * necessary, as the `break` keeps the context within `parseState`, while\n * `return` releases context so that more data can be written into the\n * BinaryChunkReader.\n *\n * @param state Current state\n * @param result An object to hold result data\n * @param binaryReader\n * @return State at end of current parsing\n */\n/* eslint-disable complexity, max-depth */\nfunction parseState(\n state: number,\n result: {[key: string]: any},\n binaryReader: BinaryChunkReader,\n options: {shp?: any}\n): number {\n // eslint-disable-next-line no-constant-condition\n while (true) {\n try {\n switch (state) {\n case STATE.ERROR:\n case STATE.END:\n return state;\n\n case STATE.EXPECTING_HEADER:\n // Parse initial file header\n const dataView = binaryReader.getDataView(SHP_HEADER_SIZE);\n if (!dataView) {\n return state;\n }\n\n result.header = parseSHPHeader(dataView);\n result.progress = {\n bytesUsed: 0,\n bytesTotal: result.header.length,\n rows: 0\n };\n // index numbering starts at 1\n result.currentIndex = 1;\n state = STATE.EXPECTING_RECORD;\n break;\n\n case STATE.EXPECTING_RECORD:\n while (binaryReader.hasAvailableBytes(SHP_RECORD_HEADER_SIZE)) {\n const recordHeaderView = binaryReader.getDataView(SHP_RECORD_HEADER_SIZE) as DataView;\n const recordHeader = {\n recordNumber: recordHeaderView.getInt32(0, BIG_ENDIAN),\n // 2 byte words; includes the four words of record header\n byteLength: recordHeaderView.getInt32(4, BIG_ENDIAN) * 2,\n // This is actually part of the record, not the header...\n type: recordHeaderView.getInt32(8, LITTLE_ENDIAN)\n };\n\n if (!binaryReader.hasAvailableBytes(recordHeader.byteLength - 4)) {\n binaryReader.rewind(SHP_RECORD_HEADER_SIZE);\n return state;\n }\n\n const invalidRecord =\n recordHeader.byteLength < 4 ||\n recordHeader.type !== result.header.type ||\n recordHeader.recordNumber !== result.currentIndex;\n\n // All records must have at least four bytes (for the record shape type)\n if (invalidRecord) {\n // Malformed record, try again, advancing just 4 bytes\n // Note: this is a rewind because binaryReader.getDataView above\n // moved the pointer forward 12 bytes, so rewinding 8 bytes still\n // leaves us 4 bytes ahead\n binaryReader.rewind(SHP_RECORD_HEADER_SIZE - 4);\n } else {\n // Note: type is actually part of the record, not the header, so\n // rewind 4 bytes before reading record\n binaryReader.rewind(4);\n\n const recordView = binaryReader.getDataView(recordHeader.byteLength) as DataView;\n const geometry = parseRecord(recordView, options);\n result.geometries.push(geometry);\n\n result.currentIndex++;\n result.progress.rows = result.currentIndex - 1;\n }\n }\n\n if (binaryReader.ended) {\n state = STATE.END;\n }\n\n return state;\n\n default:\n state = STATE.ERROR;\n result.error = `illegal parser state ${state}`;\n return state;\n }\n } catch (error) {\n state = STATE.ERROR;\n result.error = `SHP parsing failed: ${(error as Error)?.message}`;\n return state;\n }\n }\n}\n"],"file":"parse-shp.js"}
|
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseShx = void 0;
|
|
4
|
-
const parse_shp_header_1 = require("./parse-shp-header");
|
|
1
|
+
import { parseSHPHeader } from './parse-shp-header';
|
|
5
2
|
const SHX_HEADER_SIZE = 100;
|
|
6
3
|
const BIG_ENDIAN = false;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
offsets,
|
|
25
|
-
lengths
|
|
26
|
-
};
|
|
4
|
+
export function parseShx(arrayBuffer) {
|
|
5
|
+
const headerView = new DataView(arrayBuffer, 0, SHX_HEADER_SIZE);
|
|
6
|
+
const header = parseSHPHeader(headerView);
|
|
7
|
+
const contentLength = header.length - SHX_HEADER_SIZE;
|
|
8
|
+
const contentView = new DataView(arrayBuffer, SHX_HEADER_SIZE, contentLength);
|
|
9
|
+
const offsets = new Int32Array(contentLength);
|
|
10
|
+
const lengths = new Int32Array(contentLength);
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < contentLength / 8; i++) {
|
|
13
|
+
offsets[i] = contentView.getInt32(i * 8, BIG_ENDIAN);
|
|
14
|
+
lengths[i] = contentView.getInt32(i * 8 + 4, BIG_ENDIAN);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
offsets,
|
|
19
|
+
lengths
|
|
20
|
+
};
|
|
27
21
|
}
|
|
28
|
-
|
|
22
|
+
//# sourceMappingURL=parse-shx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/parsers/parse-shx.ts"],"names":["parseSHPHeader","SHX_HEADER_SIZE","BIG_ENDIAN","parseShx","arrayBuffer","headerView","DataView","header","contentLength","length","contentView","offsets","Int32Array","lengths","i","getInt32"],"mappings":"AAAA,SAAQA,cAAR,QAA6B,oBAA7B;AAOA,MAAMC,eAAe,GAAG,GAAxB;AACA,MAAMC,UAAU,GAAG,KAAnB;AAMA,OAAO,SAASC,QAAT,CAAkBC,WAAlB,EAAuD;AAE5D,QAAMC,UAAU,GAAG,IAAIC,QAAJ,CAAaF,WAAb,EAA0B,CAA1B,EAA6BH,eAA7B,CAAnB;AACA,QAAMM,MAAM,GAAGP,cAAc,CAACK,UAAD,CAA7B;AACA,QAAMG,aAAa,GAAGD,MAAM,CAACE,MAAP,GAAgBR,eAAtC;AAEA,QAAMS,WAAW,GAAG,IAAIJ,QAAJ,CAAaF,WAAb,EAA0BH,eAA1B,EAA2CO,aAA3C,CAApB;AAEA,QAAMG,OAAO,GAAG,IAAIC,UAAJ,CAAeJ,aAAf,CAAhB;AACA,QAAMK,OAAO,GAAG,IAAID,UAAJ,CAAeJ,aAAf,CAAhB;;AAEA,OAAK,IAAIM,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGN,aAAa,GAAG,CAApC,EAAuCM,CAAC,EAAxC,EAA4C;AAC1CH,IAAAA,OAAO,CAACG,CAAD,CAAP,GAAaJ,WAAW,CAACK,QAAZ,CAAqBD,CAAC,GAAG,CAAzB,EAA4BZ,UAA5B,CAAb;AACAW,IAAAA,OAAO,CAACC,CAAD,CAAP,GAAaJ,WAAW,CAACK,QAAZ,CAAqBD,CAAC,GAAG,CAAJ,GAAQ,CAA7B,EAAgCZ,UAAhC,CAAb;AACD;;AAED,SAAO;AACLS,IAAAA,OADK;AAELE,IAAAA;AAFK,GAAP;AAID","sourcesContent":["import {parseSHPHeader} from './parse-shp-header';\n\nexport interface SHXOutput {\n offsets: Int32Array;\n lengths: Int32Array;\n}\n\nconst SHX_HEADER_SIZE = 100;\nconst BIG_ENDIAN = false;\n\n/**\n * @param arrayBuffer\n * @returns SHXOutput\n */\nexport function parseShx(arrayBuffer: ArrayBuffer): SHXOutput {\n // SHX header is identical to SHP Header\n const headerView = new DataView(arrayBuffer, 0, SHX_HEADER_SIZE);\n const header = parseSHPHeader(headerView);\n const contentLength = header.length - SHX_HEADER_SIZE;\n\n const contentView = new DataView(arrayBuffer, SHX_HEADER_SIZE, contentLength);\n\n const offsets = new Int32Array(contentLength);\n const lengths = new Int32Array(contentLength);\n\n for (let i = 0; i < contentLength / 8; i++) {\n offsets[i] = contentView.getInt32(i * 8, BIG_ENDIAN);\n lengths[i] = contentView.getInt32(i * 8 + 4, BIG_ENDIAN);\n }\n\n return {\n offsets,\n lengths\n };\n}\n"],"file":"parse-shx.js"}
|