-
Notifications
You must be signed in to change notification settings - Fork 26
/
cli.js
executable file
·149 lines (130 loc) · 4.08 KB
/
cli.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
var geojsonStream = require('geojson-stream'),
es = require('event-stream'),
reproject = require('./'),
proj4 = require('proj4'),
fs = require('fs'),
argv = require('minimist')(process.argv.slice(2)),
useSpatialReference = argv["sr"] || argv["use-spatialreference"],
useEpsgIo = argv["eio"] || argv["use-epsg-io"],
http = require('http'),
https = require('https'),
crss,
fromCrs,
toCrs;
try {
crss = (argv['crs-defs'] ? loadJson(argv['crs-defs']) : {});
} catch (e) {
process.exit(1);
}
for (var k in crss) {
crss[k] = proj4(crss[k]);
}
lookupCrs(argv.from, function(crs) {
fromCrs = crs;
readStream();
});
lookupCrs(argv.to, function(crs) {
toCrs = crs;
readStream();
});
function readStream() {
if ((fromCrs && toCrs) || (!argv.from && !argv.to)) {
var inputStream = ((argv._[0] && fs.createReadStream(argv._[0])) || process.stdin)
var parser = geojsonStream.parse()
.on('header', handleHeader)
var transformStream = es.mapSync(transform)
var outputStream = geojsonStream.stringify()
inputStream.pipe(parser)
parser.pipe(transformStream)
transformStream.pipe(outputStream)
outputStream.pipe(process.stdout)
}
}
function handleHeader(geojson) {
if (geojson && geojson.type !== 'FeatureCollection') {
// If top type isn't FeatureCollection, just transform the whole object in one pass and be done with it;
// if we don't do this, geojson-stream will still output a FeatureCollection header, so the output
// GeoJSON's structure will not match the input's.
process.stdout.write(JSON.stringify(transform(geojson)));
process.exit(0);
}
}
function transform(geojson) {
if (geojson) {
var isGeomCol = geojson.type === 'GeometryCollection' && geojson.geometries;
var isFeature = geojson.type === 'Feature' && geojson.geometry;
var isGeometry = geojson.coordinates;
if (isGeomCol || isFeature || isGeometry) {
if (argv["reverse"]) {
geojson = reproject.reverse(geojson);
}
return reproject.reproject(geojson, fromCrs, toCrs, crss);
}
}
return geojson
}
function loadJson(f) {
var data;
try {
data = fs.readFileSync(f, 'utf8');
} catch (e) {
console.log("Could not open file \"" + f + "\": " + e);
throw e;
}
try {
return JSON.parse(data);
} catch (e) {
console.log("Could not parse JSON from file \"" + f + "\": " + e);
throw e;
}
}
function lookupCrs(crsName, cb) {
if (!crsName) {
cb(null);
return;
}
if (!crss[crsName]) {
var proj;
try {
proj = proj4(crsName);
} catch (e) {
// Ok, go on with proj unset.
}
if (proj) {
return cb(proj);
}
if (useSpatialReference) {
var crsPath = crsName.toLowerCase().replace(':', '/');
getCrs(crsName, "http://www.spatialreference.org/ref/"+ crsPath + "/proj4/", cb);
} else if (useEpsgIo) {
getCrs(crsName, "https://epsg.io/" + crsName.split(":")[1] + ".proj4", cb);
} else {
throw new Error("Could not find definition for CRS \"" + crsName + "\".");
}
} else {
cb(crss[crsName]);
}
}
function getCrs(crsName, url, cb) {
var crsDef = '';
var httpModule;
// OMG.. kill me now please. It's a miracle that anything get done when we have to manage this ourself in 2022...
if (url.startsWith("https://")) {
httpModule = https;
} else {
httpModule = http;
}
httpModule.get(url, function(res) {
if (res.statusCode != 200) {
throw new Error(url + " responded with HTTP " + res.statusCode +
" when looking up \"" + crsName + "\".");
}
res.on('data', function(chunk) {
crsDef += chunk;
}).on('end', function() {
crss[crsName] = proj4(crsDef);
cb(crss[crsName]);
});
});
}