@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,248 +1,226 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
else {
|
|
64
|
-
[geometries, properties] = item;
|
|
65
|
-
}
|
|
66
|
-
const geojsonGeometries = parseGeometries(geometries);
|
|
67
|
-
let features = joinProperties(geojsonGeometries, properties);
|
|
68
|
-
if (reproject) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
features = reprojectFeatures(features, prj, _targetCrs);
|
|
71
|
-
}
|
|
72
|
-
yield {
|
|
73
|
-
encoding: cpg,
|
|
74
|
-
prj,
|
|
75
|
-
shx,
|
|
76
|
-
header: shapeHeader,
|
|
77
|
-
data: features
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
exports.parseShapefileInBatches = parseShapefileInBatches;
|
|
82
|
-
/**
|
|
83
|
-
* Parse shapefile
|
|
84
|
-
*
|
|
85
|
-
* @param arrayBuffer
|
|
86
|
-
* @param options
|
|
87
|
-
* @param context
|
|
88
|
-
* @returns output of shapefile
|
|
89
|
-
*/
|
|
90
|
-
async function parseShapefile(arrayBuffer, options, context) {
|
|
91
|
-
const { reproject = false, _targetCrs = 'WGS84' } = options?.gis || {};
|
|
92
|
-
const { shx, cpg, prj } = await loadShapefileSidecarFiles(options, context);
|
|
93
|
-
// parse geometries
|
|
94
|
-
// @ts-ignore context must be defined
|
|
95
|
-
const { header, geometries } = await context.parse(arrayBuffer, shp_loader_1.SHPLoader, options); // {shp: shx}
|
|
1
|
+
import { binaryToGeometry, transformGeoJsonCoords } from '@loaders.gl/gis';
|
|
2
|
+
import { Proj4Projection } from '@math.gl/proj4';
|
|
3
|
+
import { parseShx } from './parse-shx';
|
|
4
|
+
import { zipBatchIterators } from '../streaming/zip-batch-iterators';
|
|
5
|
+
import { SHPLoader } from '../../shp-loader';
|
|
6
|
+
import { DBFLoader } from '../../dbf-loader';
|
|
7
|
+
export async function* parseShapefileInBatches(asyncIterator, options, context) {
|
|
8
|
+
const {
|
|
9
|
+
reproject = false,
|
|
10
|
+
_targetCrs = 'WGS84'
|
|
11
|
+
} = (options === null || options === void 0 ? void 0 : options.gis) || {};
|
|
12
|
+
const {
|
|
13
|
+
shx,
|
|
14
|
+
cpg,
|
|
15
|
+
prj
|
|
16
|
+
} = await loadShapefileSidecarFiles(options, context);
|
|
17
|
+
const shapeIterable = await context.parseInBatches(asyncIterator, SHPLoader, options);
|
|
18
|
+
let propertyIterable;
|
|
19
|
+
const dbfResponse = await context.fetch(replaceExtension((context === null || context === void 0 ? void 0 : context.url) || '', 'dbf'));
|
|
20
|
+
|
|
21
|
+
if (dbfResponse.ok) {
|
|
22
|
+
propertyIterable = await context.parseInBatches(dbfResponse, DBFLoader, { ...options,
|
|
23
|
+
dbf: {
|
|
24
|
+
encoding: cpg || 'latin1'
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let shapeHeader = (await shapeIterable.next()).value;
|
|
30
|
+
|
|
31
|
+
if (shapeHeader && shapeHeader.batchType === 'metadata') {
|
|
32
|
+
shapeHeader = (await shapeIterable.next()).value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let dbfHeader = {};
|
|
36
|
+
|
|
37
|
+
if (propertyIterable) {
|
|
38
|
+
dbfHeader = (await propertyIterable.next()).value;
|
|
39
|
+
|
|
40
|
+
if (dbfHeader && dbfHeader.batchType === 'metadata') {
|
|
41
|
+
dbfHeader = (await propertyIterable.next()).value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let iterator;
|
|
46
|
+
|
|
47
|
+
if (propertyIterable) {
|
|
48
|
+
iterator = zipBatchIterators(shapeIterable, propertyIterable);
|
|
49
|
+
} else {
|
|
50
|
+
iterator = shapeIterable;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for await (const item of iterator) {
|
|
54
|
+
let geometries;
|
|
55
|
+
let properties;
|
|
56
|
+
|
|
57
|
+
if (!propertyIterable) {
|
|
58
|
+
geometries = item;
|
|
59
|
+
} else {
|
|
60
|
+
[geometries, properties] = item;
|
|
61
|
+
}
|
|
62
|
+
|
|
96
63
|
const geojsonGeometries = parseGeometries(geometries);
|
|
97
|
-
// parse properties
|
|
98
|
-
let properties = [];
|
|
99
|
-
// @ts-ignore context must be defined
|
|
100
|
-
const dbfResponse = await context.fetch(replaceExtension(context.url, 'dbf'));
|
|
101
|
-
if (dbfResponse.ok) {
|
|
102
|
-
// @ts-ignore context must be defined
|
|
103
|
-
properties = await context.parse(dbfResponse, dbf_loader_1.DBFLoader, { dbf: { encoding: cpg || 'latin1' } });
|
|
104
|
-
}
|
|
105
64
|
let features = joinProperties(geojsonGeometries, properties);
|
|
65
|
+
|
|
106
66
|
if (reproject) {
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
67
|
+
features = reprojectFeatures(features, prj, _targetCrs);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
yield {
|
|
71
|
+
encoding: cpg,
|
|
72
|
+
prj,
|
|
73
|
+
shx,
|
|
74
|
+
header: shapeHeader,
|
|
75
|
+
data: features
|
|
115
76
|
};
|
|
77
|
+
}
|
|
116
78
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
79
|
+
export async function parseShapefile(arrayBuffer, options, context) {
|
|
80
|
+
const {
|
|
81
|
+
reproject = false,
|
|
82
|
+
_targetCrs = 'WGS84'
|
|
83
|
+
} = (options === null || options === void 0 ? void 0 : options.gis) || {};
|
|
84
|
+
const {
|
|
85
|
+
shx,
|
|
86
|
+
cpg,
|
|
87
|
+
prj
|
|
88
|
+
} = await loadShapefileSidecarFiles(options, context);
|
|
89
|
+
const {
|
|
90
|
+
header,
|
|
91
|
+
geometries
|
|
92
|
+
} = await context.parse(arrayBuffer, SHPLoader, options);
|
|
93
|
+
const geojsonGeometries = parseGeometries(geometries);
|
|
94
|
+
let properties = [];
|
|
95
|
+
const dbfResponse = await context.fetch(replaceExtension(context.url, 'dbf'));
|
|
96
|
+
|
|
97
|
+
if (dbfResponse.ok) {
|
|
98
|
+
properties = await context.parse(dbfResponse, DBFLoader, {
|
|
99
|
+
dbf: {
|
|
100
|
+
encoding: cpg || 'latin1'
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let features = joinProperties(geojsonGeometries, properties);
|
|
106
|
+
|
|
107
|
+
if (reproject) {
|
|
108
|
+
features = reprojectFeatures(features, prj, _targetCrs);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
encoding: cpg,
|
|
113
|
+
prj,
|
|
114
|
+
shx,
|
|
115
|
+
header,
|
|
116
|
+
data: features
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
124
120
|
function parseGeometries(geometries) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
121
|
+
const geojsonGeometries = [];
|
|
122
|
+
|
|
123
|
+
for (const geom of geometries) {
|
|
124
|
+
geojsonGeometries.push(binaryToGeometry(geom));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return geojsonGeometries;
|
|
130
128
|
}
|
|
131
|
-
|
|
132
|
-
* Join properties and geometries into features
|
|
133
|
-
*
|
|
134
|
-
* @param geometries [description]
|
|
135
|
-
* @param properties [description]
|
|
136
|
-
* @return [description]
|
|
137
|
-
*/
|
|
129
|
+
|
|
138
130
|
function joinProperties(geometries, properties) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
131
|
+
const features = [];
|
|
132
|
+
|
|
133
|
+
for (let i = 0; i < geometries.length; i++) {
|
|
134
|
+
const geometry = geometries[i];
|
|
135
|
+
const feature = {
|
|
136
|
+
type: 'Feature',
|
|
137
|
+
geometry,
|
|
138
|
+
properties: properties && properties[i] || {}
|
|
139
|
+
};
|
|
140
|
+
features.push(feature);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return features;
|
|
151
144
|
}
|
|
152
|
-
|
|
153
|
-
* Reproject GeoJSON features to output CRS
|
|
154
|
-
*
|
|
155
|
-
* @param features parsed GeoJSON features
|
|
156
|
-
* @param sourceCrs source coordinate reference system
|
|
157
|
-
* @param targetCrs †arget coordinate reference system
|
|
158
|
-
* @return Reprojected Features
|
|
159
|
-
*/
|
|
145
|
+
|
|
160
146
|
function reprojectFeatures(features, sourceCrs, targetCrs) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
147
|
+
if (!sourceCrs && !targetCrs) {
|
|
148
|
+
return features;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const projection = new Proj4Projection({
|
|
152
|
+
from: sourceCrs || 'WGS84',
|
|
153
|
+
to: targetCrs || 'WGS84'
|
|
154
|
+
});
|
|
155
|
+
return transformGeoJsonCoords(features, coord => projection.project(coord));
|
|
166
156
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
157
|
+
|
|
158
|
+
export async function loadShapefileSidecarFiles(options, context) {
|
|
159
|
+
const {
|
|
160
|
+
url,
|
|
161
|
+
fetch
|
|
162
|
+
} = context;
|
|
163
|
+
const shxPromise = fetch(replaceExtension(url, 'shx'));
|
|
164
|
+
const cpgPromise = fetch(replaceExtension(url, 'cpg'));
|
|
165
|
+
const prjPromise = fetch(replaceExtension(url, 'prj'));
|
|
166
|
+
await Promise.all([shxPromise, cpgPromise, prjPromise]);
|
|
167
|
+
let shx;
|
|
168
|
+
let cpg;
|
|
169
|
+
let prj;
|
|
170
|
+
const shxResponse = await shxPromise;
|
|
171
|
+
|
|
172
|
+
if (shxResponse.ok) {
|
|
173
|
+
const arrayBuffer = await shxResponse.arrayBuffer();
|
|
174
|
+
shx = parseShx(arrayBuffer);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const cpgResponse = await cpgPromise;
|
|
178
|
+
|
|
179
|
+
if (cpgResponse.ok) {
|
|
180
|
+
cpg = await cpgResponse.text();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const prjResponse = await prjPromise;
|
|
184
|
+
|
|
185
|
+
if (prjResponse.ok) {
|
|
186
|
+
prj = await prjResponse.text();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
shx,
|
|
191
|
+
cpg,
|
|
192
|
+
prj
|
|
193
|
+
};
|
|
203
194
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
* https://trac.osgeo.org/mapserver/ticket/166
|
|
215
|
-
*/
|
|
216
|
-
function replaceExtension(url, newExtension) {
|
|
217
|
-
const baseName = basename(url);
|
|
218
|
-
const extension = extname(url);
|
|
219
|
-
const isUpperCase = extension === extension.toUpperCase();
|
|
220
|
-
if (isUpperCase) {
|
|
221
|
-
newExtension = newExtension.toUpperCase();
|
|
222
|
-
}
|
|
223
|
-
return `${baseName}.${newExtension}`;
|
|
195
|
+
export function replaceExtension(url, newExtension) {
|
|
196
|
+
const baseName = basename(url);
|
|
197
|
+
const extension = extname(url);
|
|
198
|
+
const isUpperCase = extension === extension.toUpperCase();
|
|
199
|
+
|
|
200
|
+
if (isUpperCase) {
|
|
201
|
+
newExtension = newExtension.toUpperCase();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return "".concat(baseName, ".").concat(newExtension);
|
|
224
205
|
}
|
|
225
|
-
|
|
226
|
-
// NOTE - this gives the entire path minus extension (i.e. NOT same as path.basename)
|
|
227
|
-
/**
|
|
228
|
-
* @param url
|
|
229
|
-
* @returns string
|
|
230
|
-
*/
|
|
206
|
+
|
|
231
207
|
function basename(url) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
208
|
+
const extIndex = url && url.lastIndexOf('.');
|
|
209
|
+
|
|
210
|
+
if (typeof extIndex === 'number') {
|
|
211
|
+
return extIndex >= 0 ? url.substr(0, extIndex) : '';
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return extIndex;
|
|
237
215
|
}
|
|
238
|
-
|
|
239
|
-
* @param url
|
|
240
|
-
* @returns string
|
|
241
|
-
*/
|
|
216
|
+
|
|
242
217
|
function extname(url) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
218
|
+
const extIndex = url && url.lastIndexOf('.');
|
|
219
|
+
|
|
220
|
+
if (typeof extIndex === 'number') {
|
|
221
|
+
return extIndex >= 0 ? url.substr(extIndex + 1) : '';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return extIndex;
|
|
248
225
|
}
|
|
226
|
+
//# sourceMappingURL=parse-shapefile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/parsers/parse-shapefile.ts"],"names":["binaryToGeometry","transformGeoJsonCoords","Proj4Projection","parseShx","zipBatchIterators","SHPLoader","DBFLoader","parseShapefileInBatches","asyncIterator","options","context","reproject","_targetCrs","gis","shx","cpg","prj","loadShapefileSidecarFiles","shapeIterable","parseInBatches","propertyIterable","dbfResponse","fetch","replaceExtension","url","ok","dbf","encoding","shapeHeader","next","value","batchType","dbfHeader","iterator","item","geometries","properties","geojsonGeometries","parseGeometries","features","joinProperties","reprojectFeatures","header","data","parseShapefile","arrayBuffer","parse","geom","push","i","length","geometry","feature","type","sourceCrs","targetCrs","projection","from","to","coord","project","shxPromise","cpgPromise","prjPromise","Promise","all","shxResponse","cpgResponse","text","prjResponse","newExtension","baseName","basename","extension","extname","isUpperCase","toUpperCase","extIndex","lastIndexOf","substr"],"mappings":"AAKA,SAAQA,gBAAR,EAA0BC,sBAA1B,QAAuD,iBAAvD;AACA,SAAQC,eAAR,QAA8B,gBAA9B;AACA,SAAQC,QAAR,QAAuB,aAAvB;AACA,SAAQC,iBAAR,QAAgC,kCAAhC;AACA,SAAQC,SAAR,QAAwB,kBAAxB;AACA,SAAQC,SAAR,QAAwB,kBAAxB;AAkBA,OAAO,gBAAgBC,uBAAhB,CACLC,aADK,EAELC,OAFK,EAGLC,OAHK,EAI2B;AAChC,QAAM;AAACC,IAAAA,SAAS,GAAG,KAAb;AAAoBC,IAAAA,UAAU,GAAG;AAAjC,MAA4C,CAAAH,OAAO,SAAP,IAAAA,OAAO,WAAP,YAAAA,OAAO,CAAEI,GAAT,KAAgB,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AI,CAAZ,GAAgBrD,GAAG,CAACuD,MAAJ,CAAW,CAAX,EAAcF,QAAd,CAAhB,GAA0C,EAAjD;AACD;;AACD,SAAOA,QAAP;AACD;;AAKD,SAASH,OAAT,CAAiBlD,GAAjB,EAAsC;AACpC,QAAMqD,QAAQ,GAAGrD,GAAG,IAAIA,GAAG,CAACsD,WAAJ,CAAgB,GAAhB,CAAxB;;AACA,MAAI,OAAOD,QAAP,KAAoB,QAAxB,EAAkC;AAChC,WAAOA,QAAQ,IAAI,CAAZ,GAAgBrD,GAAG,CAACuD,MAAJ,CAAWF,QAAQ,GAAG,CAAtB,CAAhB,GAA2C,EAAlD;AACD;;AACD,SAAOA,QAAP;AACD","sourcesContent":["// import type {Feature} from '@loaders.gl/gis';\nimport type {SHXOutput} from './parse-shx';\nimport type {SHPHeader} from './parse-shp-header';\nimport type {LoaderContext} from '@loaders.gl/loader-utils';\n\nimport {binaryToGeometry, transformGeoJsonCoords} from '@loaders.gl/gis';\nimport {Proj4Projection} from '@math.gl/proj4';\nimport {parseShx} from './parse-shx';\nimport {zipBatchIterators} from '../streaming/zip-batch-iterators';\nimport {SHPLoader} from '../../shp-loader';\nimport {DBFLoader} from '../../dbf-loader';\n\ntype Feature = any;\ninterface ShapefileOutput {\n encoding?: string;\n prj?: string;\n shx?: SHXOutput;\n header: SHPHeader;\n data: object[];\n}\n/**\n * Parsing of file in batches\n *\n * @param asyncIterator\n * @param options\n * @param context\n */\n// eslint-disable-next-line max-statements, complexity\nexport async function* parseShapefileInBatches(\n asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,\n options?: any,\n context?: LoaderContext\n): AsyncIterable<ShapefileOutput> {\n const {reproject = false, _targetCrs = 'WGS84'} = options?.gis || {};\n const {shx, cpg, prj} = await loadShapefileSidecarFiles(options, context);\n\n // parse geometries\n // @ts-ignore context must be defined\n const shapeIterable: any = await context.parseInBatches(asyncIterator, SHPLoader, options);\n\n // parse properties\n let propertyIterable: any;\n // @ts-ignore context must be defined\n const dbfResponse = await context.fetch(replaceExtension(context?.url || '', 'dbf'));\n if (dbfResponse.ok) {\n // @ts-ignore context must be defined\n propertyIterable = await context.parseInBatches(dbfResponse, DBFLoader, {\n ...options,\n dbf: {encoding: cpg || 'latin1'}\n });\n }\n\n // When `options.metadata` is `true`, there's an extra initial `metadata`\n // object before the iterator starts. zipBatchIterators expects to receive\n // batches of Array objects, and will fail with non-iterable batches, so it's\n // important to skip over the first batch.\n let shapeHeader = (await shapeIterable.next()).value;\n if (shapeHeader && shapeHeader.batchType === 'metadata') {\n shapeHeader = (await shapeIterable.next()).value;\n }\n\n let dbfHeader: {batchType?: string} = {};\n if (propertyIterable) {\n dbfHeader = (await propertyIterable.next()).value;\n if (dbfHeader && dbfHeader.batchType === 'metadata') {\n dbfHeader = (await propertyIterable.next()).value;\n }\n }\n\n let iterator: any;\n if (propertyIterable) {\n iterator = zipBatchIterators(shapeIterable, propertyIterable);\n } else {\n iterator = shapeIterable;\n }\n\n for await (const item of iterator) {\n let geometries: any;\n let properties: any;\n if (!propertyIterable) {\n geometries = item;\n } else {\n [geometries, properties] = item;\n }\n\n const geojsonGeometries = parseGeometries(geometries);\n let features = joinProperties(geojsonGeometries, properties);\n if (reproject) {\n // @ts-ignore\n features = reprojectFeatures(features, prj, _targetCrs);\n }\n yield {\n encoding: cpg,\n prj,\n shx,\n header: shapeHeader,\n data: features\n };\n }\n}\n\n/**\n * Parse shapefile\n *\n * @param arrayBuffer\n * @param options\n * @param context\n * @returns output of shapefile\n */\nexport async function parseShapefile(\n arrayBuffer: ArrayBuffer,\n options?: {[key: string]: any},\n context?: LoaderContext\n): Promise<ShapefileOutput> {\n const {reproject = false, _targetCrs = 'WGS84'} = options?.gis || {};\n const {shx, cpg, prj} = await loadShapefileSidecarFiles(options, context);\n\n // parse geometries\n // @ts-ignore context must be defined\n const {header, geometries} = await context.parse(arrayBuffer, SHPLoader, options); // {shp: shx}\n\n const geojsonGeometries = parseGeometries(geometries);\n\n // parse properties\n let properties = [];\n\n // @ts-ignore context must be defined\n const dbfResponse = await context.fetch(replaceExtension(context.url, 'dbf'));\n if (dbfResponse.ok) {\n // @ts-ignore context must be defined\n properties = await context.parse(dbfResponse, DBFLoader, {dbf: {encoding: cpg || 'latin1'}});\n }\n\n let features = joinProperties(geojsonGeometries, properties);\n if (reproject) {\n features = reprojectFeatures(features, prj, _targetCrs);\n }\n\n return {\n encoding: cpg,\n prj,\n shx,\n header,\n data: features\n };\n}\n\n/**\n * Parse geometries\n *\n * @param geometries\n * @returns geometries as an array\n */\nfunction parseGeometries(geometries: any[]): any[] {\n const geojsonGeometries: any[] = [];\n for (const geom of geometries) {\n geojsonGeometries.push(binaryToGeometry(geom));\n }\n return geojsonGeometries;\n}\n\n/**\n * Join properties and geometries into features\n *\n * @param geometries [description]\n * @param properties [description]\n * @return [description]\n */\nfunction joinProperties(geometries: object[], properties: object[]): Feature[] {\n const features: Feature[] = [];\n for (let i = 0; i < geometries.length; i++) {\n const geometry = geometries[i];\n const feature: Feature = {\n type: 'Feature',\n geometry,\n // properties can be undefined if dbfResponse above was empty\n properties: (properties && properties[i]) || {}\n };\n features.push(feature);\n }\n\n return features;\n}\n\n/**\n * Reproject GeoJSON features to output CRS\n *\n * @param features parsed GeoJSON features\n * @param sourceCrs source coordinate reference system\n * @param targetCrs †arget coordinate reference system\n * @return Reprojected Features\n */\nfunction reprojectFeatures(features: Feature[], sourceCrs?: string, targetCrs?: string): Feature[] {\n if (!sourceCrs && !targetCrs) {\n return features;\n }\n\n const projection = new Proj4Projection({from: sourceCrs || 'WGS84', to: targetCrs || 'WGS84'});\n return transformGeoJsonCoords(features, (coord) => projection.project(coord));\n}\n\n/**\n *\n * @param options\n * @param context\n * @returns Promise\n */\n// eslint-disable-next-line max-statements\nexport async function loadShapefileSidecarFiles(\n options?: object,\n context?: LoaderContext\n): Promise<{\n shx?: SHXOutput;\n cpg?: string;\n prj?: string;\n}> {\n // Attempt a parallel load of the small sidecar files\n // @ts-ignore context must be defined\n const {url, fetch} = context;\n const shxPromise = fetch(replaceExtension(url, 'shx'));\n const cpgPromise = fetch(replaceExtension(url, 'cpg'));\n const prjPromise = fetch(replaceExtension(url, 'prj'));\n await Promise.all([shxPromise, cpgPromise, prjPromise]);\n\n let shx: SHXOutput | undefined;\n let cpg: string | undefined;\n let prj: string | undefined;\n\n const shxResponse = await shxPromise;\n if (shxResponse.ok) {\n const arrayBuffer = await shxResponse.arrayBuffer();\n shx = parseShx(arrayBuffer);\n }\n\n const cpgResponse = await cpgPromise;\n if (cpgResponse.ok) {\n cpg = await cpgResponse.text();\n }\n\n const prjResponse = await prjPromise;\n if (prjResponse.ok) {\n prj = await prjResponse.text();\n }\n\n return {\n shx,\n cpg,\n prj\n };\n}\n\n/**\n * Replace the extension at the end of a path.\n *\n * Matches the case of new extension with the case of the original file extension,\n * to increase the chance of finding files without firing off a request storm looking for various case combinations\n *\n * NOTE: Extensions can be both lower and uppercase\n * per spec, extensions should be lower case, but that doesn't mean they always are. See:\n * calvinmetcalf/shapefile-js#64, mapserver/mapserver#4712\n * https://trac.osgeo.org/mapserver/ticket/166\n */\nexport function replaceExtension(url: string, newExtension: string): string {\n const baseName = basename(url);\n const extension = extname(url);\n const isUpperCase = extension === extension.toUpperCase();\n if (isUpperCase) {\n newExtension = newExtension.toUpperCase();\n }\n return `${baseName}.${newExtension}`;\n}\n\n// NOTE - this gives the entire path minus extension (i.e. NOT same as path.basename)\n/**\n * @param url\n * @returns string\n */\nfunction basename(url: string): string {\n const extIndex = url && url.lastIndexOf('.');\n if (typeof extIndex === 'number') {\n return extIndex >= 0 ? url.substr(0, extIndex) : '';\n }\n return extIndex;\n}\n/**\n * @param url\n * @returns string\n */\nfunction extname(url: string): string {\n const extIndex = url && url.lastIndexOf('.');\n if (typeof extIndex === 'number') {\n return extIndex >= 0 ? url.substr(extIndex + 1) : '';\n }\n return extIndex;\n}\n"],"file":"parse-shapefile.js"}
|