forked from perliedman/ocad2geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
105 lines (93 loc) · 2.99 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
const fs = require('fs')
const mkdirp = require('mkdirp')
const { toWgs84 } = require('reproject')
const geojsonvt = require('geojson-vt')
const vtpbf = require('vt-pbf')
const argv = require('minimist')(process.argv.slice(2));
const { readOcad, ocadToGeoJson, ocadToMapboxGlStyle } = require('./')
const filePath = argv._[0]
var outStream
readOcad(filePath)
.then(ocadFile => {
if (argv.v) {
writeInfo(filePath, ocadFile, process.stderr)
}
let mode = 'geojson'
if (argv.p) {
mode = 'params'
} else if (argv.s) {
mode = 'symbols'
const n = Number(argv.s)
const t = Math.trunc(n)
symNum = (t + (n - t) / 100) * 1000
} else if (argv['vector-tiles']) {
mode = 'vectortiles'
}
if (mode !== 'vectortiles') {
outStream = argv.o ? fs.createWriteStream(argv.o) : process.stdout
} else {
mkdirp.sync(argv.o)
}
switch (mode) {
case 'geojson':
outStream.write(JSON.stringify(ocadToGeoJson(ocadFile), null, 2))
break
case 'params':
outStream.write(JSON.stringify(ocadFile.parameterStrings, null, 2))
break
case 'symbols':
const symbol = ocadFile.symbols.find(s => s.symNum === symNum)
if (!symbol) {
console.error(`No such symbol ${argv.s} (${symNum})`)
process.exit(1)
}
delete symbol.buffer
if (!argv.iconBits) {
delete symbol.iconBits
}
outStream.write(JSON.stringify(symbol, null, 2))
break
case 'vectortiles':
const geoJson = toWgs84(ocadToGeoJson(ocadFile), '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
const tileIndex = geojsonvt(geoJson, {
maxZoom: 14,
indexMaxZoom: 14,
indexMaxPoints: 0
})
tileIndex.tileCoords.forEach(tc => {
mkdirp.sync(`${argv.o}/${tc.z}/${tc.x}`)
const tile = tileIndex.getTile(tc.z, tc.x, tc.y)
const pbf = vtpbf.fromGeojsonVt({ ocad: tile })
const tilePath = `${argv.o}/${tc.z}/${tc.x}/${tc.y}.pbf`
fs.writeFileSync(tilePath, pbf)
console.log(tilePath)
})
fs.writeFileSync(`${argv.o}/layers.json`, JSON.stringify(ocadToMapboxGlStyle(ocadFile, {
source: 'map',
sourceLayer: 'ocad'
}), null, 2))
break
default:
console.error(`Unknown mode ${mode}.`)
process.exit(1)
}
})
const writeInfo = (path, map, stream) => {
const header = map.header
let infos = [
`File: ${path}`,
`OCAD version: ${header.version}.${header.subVersion}.${header.subSubVersion}`,
`File version: ${header.currentFileVersion}`,
`Total number objects: ${map.objects.length}`,
]
if (map.parameterStrings[1039]) {
const scalePar = map.parameterStrings[1039][0]
infos = infos.concat([
`Scale: 1:${scalePar.m}`,
`Northing: ${scalePar.y}`,
`Easting: ${scalePar.x}`
])
}
stream.write(infos.join('\n'))
stream.write('\n')
}