This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
geojsonio.js
executable file
·76 lines (70 loc) · 2.33 KB
/
geojsonio.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
#!/usr/bin/env node
var concat = require('concat-stream'),
opener = require('opener'),
tty = require('tty'),
path = require('path'),
fs = require('fs'),
validator = require('@mapbox/geojsonhint'),
GitHubApi = require('github'),
github = new GitHubApi({
version: '3.0.0',
protocol: 'https'
});
argv = require('minimist')(process.argv.slice(2));
MAX_URL_LEN = 150e3,
BIG_LEN = 5000000;
if (argv.help || argv.h || !(argv._[0] || !tty.isatty(0))) {
return help();
}
((argv._[0] && fs.createReadStream(argv._[0])) || process.stdin).pipe(concat(openData));
function openData(body) {
if (body.length > BIG_LEN) {
console.error('This file is very large, and will likely display slowly on geojson.io');
}
if (body.length <= MAX_URL_LEN) {
var messages = validator.hint(JSON.parse(body.toString()));
var errors = messages.filter(function (message) {
return !message.hasOwnProperty('level') || message.level !== 'message';
})
if (errors.length == 0) {
messages.forEach(function (message) {
console.log(message.message);
});
displayResource('#data=data:application/json,' + encodeURIComponent(
JSON.stringify(JSON.parse(body.toString()))));
} else {
console.log("This is not valid GeoJSON. Errors:\n");
errors.forEach(function (error) {
console.log(error.message);
});
}
} else {
github.gists.create({
description: '',
public: true,
files: {
'map.geojson': {
content: JSON.stringify(JSON.parse(body.toString()))
}
}
}, function (err, res) {
if (err) {
console.error('Unable to create Gist: ' + JSON.stringify(err));
} else {
displayResource('#id=gist:/' + res.id);
}
});
}
}
function displayResource(path) {
try {
(argv.print ? console.log : opener)(
(argv.domain || 'http://geojson.io/') + path);
} catch(e) {
console.error('Valid GeoJSON file required as input.');
help();
}
}
function help() {
fs.createReadStream(path.join(__dirname, 'README.md')).pipe(process.stdout);
}