forked from mbostock/shapefile
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (67 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import path from "path-source";
import array from "array-source";
import stream from "stream-source";
import dbf from "./dbf/index";
import shapefile from "./shapefile/index";
import shp from "./shp/index";
export function open(shp, dbf, options) {
if (typeof dbf === "string") {
if (!/\.dbf$/.test(dbf)) dbf += ".dbf";
dbf = path(dbf, options);
} else if (dbf instanceof ArrayBuffer || dbf instanceof Uint8Array) {
dbf = array(dbf);
} else if (dbf != null) {
dbf = stream(dbf);
}
if (typeof shp === "string") {
if (!/\.shp$/.test(shp)) shp += ".shp";
if (dbf === undefined) dbf = path(shp.substring(0, shp.length - 4) + ".dbf", options).catch(function() {});
shp = path(shp, options);
} else if (shp instanceof ArrayBuffer || shp instanceof Uint8Array) {
shp = array(shp);
} else {
shp = stream(shp);
}
return Promise.all([shp, dbf]).then(function(sources) {
var shp = sources[0], dbf = sources[1], encoding = "windows-1252";
if (options && options.encoding != null) encoding = options.encoding;
return shapefile(shp, dbf, dbf && new TextDecoder(encoding));
});
}
export function openShp(source, options) {
if (typeof source === "string") {
if (!/\.shp$/.test(source)) source += ".shp";
source = path(source, options);
} else if (source instanceof ArrayBuffer || source instanceof Uint8Array) {
source = array(source);
} else {
source = stream(source);
}
return Promise.resolve(source).then(shp);
}
export function openDbf(source, options) {
var encoding = "windows-1252";
if (options && options.encoding != null) encoding = options.encoding;
encoding = new TextDecoder(encoding);
if (typeof source === "string") {
if (!/\.dbf$/.test(source)) source += ".dbf";
source = path(source, options);
} else if (source instanceof ArrayBuffer || source instanceof Uint8Array) {
source = array(source);
} else {
source = stream(source);
}
return Promise.resolve(source).then(function(source) {
return dbf(source, encoding);
});
}
export function read(shp, dbf, options) {
return open(shp, dbf, options).then(function(source) {
var features = [], collection = {type: "FeatureCollection", features: features, bbox: source.bbox};
return source.read().then(function read(result) {
if (result.done) return collection;
features.push(result.value);
return source.read().then(read);
});
});
}