-
Notifications
You must be signed in to change notification settings - Fork 155
/
shp2json
executable file
·112 lines (101 loc) · 3.72 KB
/
shp2json
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
var fs = require("fs"),
commander = require("commander"),
shapefile = require("../");
commander
.version(require("../package.json").version)
.usage("[options] [file]")
.description("Convert a Shapefile to GeoJSON.")
.option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
.option("-n, --newline-delimited", "output newline-delimited JSON")
.option("-g, --geometry", "output geometries (implies --ignore-properties)")
.option("--ignore-properties", "don’t read shapefile properties (.dbf)")
.option("--encoding <encoding>", "character encoding for shapefile properties (.dbf)")
.option("--crs-name <name>", "specify a named CRS for the output feature collection")
.parse(process.argv);
if (commander.args.length === 0) commander.args[0] = "-";
else if (commander.args.length !== 1) {
console.error();
console.error(" error: multiple input files");
console.error();
process.exit(1);
}
var out = (commander.out === "-" ? process.stdout : fs.createWriteStream(commander.out)).on("error", handleEpipe);
shapefile.open(
commander.args[0] === "-" ? process.stdin : commander.args[0],
commander.geometry || commander.ignoreProperties ? null : undefined,
{encoding: commander.encoding})
.then(commander.newlineDelimited
? (commander.geometry ? writeNewlineDelimitedGeometries : writeNewlineDelimitedFeatures)
: (commander.geometry ? writeGeometryCollection : writeFeatureCollection))
.catch(handleError);
function writeNewlineDelimitedGeometries(source) {
return source.read().then(function repeat(result) {
if (result.done) return;
out.write(JSON.stringify(result.value.geometry));
out.write("\n");
return source.read().then(repeat);
}).then(function() {
if (out !== process.stdout) out.end();
});
}
function writeNewlineDelimitedFeatures(source) {
return source.read().then(function repeat(result) {
if (result.done) return;
out.write(JSON.stringify(result.value));
out.write("\n");
return source.read().then(repeat);
}).then(function() {
if (out !== process.stdout) out.end();
});
}
function writeGeometryCollection(source) {
out.write("{\"type\":\"GeometryCollection\",\"bbox\":");
out.write(JSON.stringify(source.bbox));
out.write(",\"geometries\":[");
return source.read().then(function(result) {
if (result.done) return;
out.write(JSON.stringify(result.value.geometry));
return source.read().then(function repeat(result) {
if (result.done) return;
out.write(",");
out.write(JSON.stringify(result.value.geometry));
return source.read().then(repeat);
});
}).then(function() {
out[out === process.stdout ? "write" : "end"]("]}\n");
});
}
function writeFeatureCollection(source) {
out.write("{\"type\":\"FeatureCollection\",\"bbox\":");
out.write(JSON.stringify(source.bbox));
if (commander.crsName) {
out.write(",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":");
out.write(JSON.stringify(commander.crsName + ""));
out.write("}}");
}
out.write(",\"features\":[");
return source.read().then(function(result) {
if (result.done) return;
out.write(JSON.stringify(result.value));
return source.read().then(function repeat(result) {
if (result.done) return;
out.write(",");
out.write(JSON.stringify(result.value));
return source.read().then(repeat);
});
}).then(function() {
out[out === process.stdout ? "write" : "end"]("]}\n");
});
}
function handleEpipe(error) {
if (error.code === "EPIPE" || error.errno === "EPIPE") {
process.exit(0);
}
}
function handleError(error) {
console.error();
console.error(" error: " + error.message);
console.error();
process.exit(1);
}