-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.js
executable file
·108 lines (91 loc) · 2.63 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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const svgify = require('geojson-svgify')
const projections = require('projections')
const bbox = require('@turf/bbox').default
const h = require('virtual-hyperscript-svg')
const toString = require('virtual-dom-stringify')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v']
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
geojson-to-svg [-p mercator]
Options:
--projection -p Which projection to use. Default: mercator
Refer to npmjs.com/projections for a list.
--properties -k comma separated list of properties to add
to the svg as data attributes.
Examples:
cat example.geo.json | geojson-to-svg -p lambert -k GEN > example.svg
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`geojson-to-svg v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
console.error(err)
process.exit(1)
}
const projection = argv.projection || argv.p || 'mercator'
if (!(projection in projections)) {
showError('Unknown projection. These are supported:\n' + Object.keys(projections).join(', '))
}
const project = ([lon, lat]) => {
const {x, y} = projections[projection]({lat, lon}) // todo
return [x, y]
}
const properties = argv['properties'] || argv.k || ''
const propertiesArray = properties.split(',')
// todo
const styles = h('style', {}, `
.shape {
stroke: none;
fill: black;
}
`)
let input = ''
process.stdin
.on('error', showError)
.on('data', (d) => {
input += d.toString('utf8')
})
.once('end', () => {
try {
const geojson = JSON.parse(input)
const [west, south, east, north] = bbox(geojson)
const polylines = svgify(geojson, {
projection: project,
computeProps(feature) {
const {properties} = feature
const propKeys = Object.keys(properties)
const dataAttributes = propKeys.reduce((acc, key) => {
const value = properties[key]
if (!propertiesArray.includes(key)) return acc;
const dataKey = `data-${key.toLowerCase()}`
acc[dataKey] = value
return acc
}, {})
dataAttributes.className = 'shape';
return dataAttributes;
}
})
const [left, top] = project([west, north])
const [right, bottom] = project([east, south])
const width = right - left
const height = bottom - top
const svg = h('svg', {
width: (Math.abs(width) * 100000) + '',
height: (Math.abs(height) * 100000) + '',
viewBox: [left, top, width, height].join(',')
}, [styles, polylines])
process.stdout.write(toString(svg))
} catch (err) {
return showError(err)
}
})