-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
175 lines (156 loc) · 5.11 KB
/
map.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const fs = require('fs')
const path = require('path')
const DOMParser = require('xmldom').DOMParser
const osmtogeojson = require('osmtogeojson')
const AnkiExport = require('anki-apkg-export').default
const render = require('./lib/render.js')
const { transform, scale, invertYAxe, inside } = require('./lib/transform.js')
const { match } = require('./config/filter.js')
const options = require('./config/options.js')
const parseArgs = require('minimist')
const featureTypes = ['roads', 'places']
const opts = {
boolean: featureTypes,
default: {
roads: true,
places: true
}
}
const args = parseArgs(process.argv.slice(2), opts)
const includedFeatureTypes = featureTypes.filter(t => args[t])
let outputArg = args.o
if (!outputArg) {
if (!fs.existsSync('output')) {
fs.mkdirSync('output')
}
outputArg = 'output/map.apkg'
}
function boundsAround(point) {
const r = options.mapContextRadius
return {
minlon: point[0] - r,
maxlon: point[0] + r,
minlat: point[1] - r,
maxlat: point[1] + r
}
}
const roadWords = ['Way', 'Street', 'Road', 'Drive', 'Boulevard', 'Lane']
function featureType(f) {
if (f.type == 'LineString') {
for (const roadWord of roadWords) {
if (f.properties.name.includes(roadWord)) {
return 'roads'
}
}
}
return 'places'
}
function valid(f) {
if (!f.originalReprpoint || !f.properties.name) {
return false
}
// Anything that has just a number for a name probably isn't important
if (!isNaN(Number(f.properties.name))) {
return false
}
if (f.properties.name.toLowerCase().startsWith('nameless')) {
return false
}
if (f.type == 'LineString' && featureType(f) != 'roads') {
return false
}
return !match(f.properties, 'invalid')
}
function addCard(deck, map, back, i) {
const fname = `map${i}.png`
return new Promise((resolve, reject) => {
render(map, options.mapSize, options.mapSize, options.mapDetail).then(buf => {
console.log(`${i}: ${back}`)
deck.addMedia(fname, buf)
deck.addCard(`<img src="${fname}" />`, back)
resolve()
}).catch(reject)
})
}
async function generate(source, deck, i) {
const file = fs.readFileSync(path.resolve(source), { encoding: 'utf-8' })
const parser = new DOMParser()
const xmlDoc = parser.parseFromString(file, "text/xml")
const boundEl = xmlDoc.getElementsByTagName('bounds')[0]
const bounds = {
minlat: boundEl.getAttribute('minlat'),
maxlat: boundEl.getAttribute('maxlat'),
minlon: boundEl.getAttribute('minlon'),
maxlon: boundEl.getAttribute('maxlon')
}
const minSize = 2 * options.mapContextRadius
const latRange = bounds.maxlat - bounds.minlat
const lonRange = bounds.maxlon - bounds.minlon
if (latRange < minSize || lonRange < minSize) {
console.log(`Map file ${source} is too small for context radius ${options.mapContextRadius}. Use a bigger map or reduce mapContextRadius in config/options.js.`)
return i
}
const original = osmtogeojson(xmlDoc)
const map = transform(original, bounds)
for (const f of map.features) {
if (!valid(f)) {
continue
}
if (!includedFeatureTypes.includes(featureType(f))) {
continue
}
const zoomed = boundsAround(f.originalReprpoint)
if (!inside(bounds, zoomed)) {
continue
}
// Center the map around this feature
const realName = f.properties.name
const sameNameFeatures = []
for (f2 of map.features) {
if (f2.originalCoordinates) {
f2.coordinates = scale(f2.originalCoordinates, zoomed, map.granularity)
}
if (f2.originalReprpoint) {
f2.reprpoint = scale(f2.originalReprpoint, zoomed, map.granularity)
}
if (f2.properties.name == realName) {
f2.properties.name = ''
sameNameFeatures.push(f2)
}
}
invertYAxe(map)
// Obscure the feature
f.properties.name = '???'
f.properties.highlight = true
// Create the card
try {
i++
await addCard(deck, map, realName, i)
} catch (err) {
i--
}
// Reset
f.properties.name = realName
for (const f2 of sameNameFeatures) {
f2.properties.name = realName
}
f.properties.highlight = false
}
return i
}
async function makeDeck(sources, name, destination) {
console.log('Initializing deck...')
let apkg = new AnkiExport(name)
let count = 0
for (const source of sources) {
count = await generate(source, apkg, count)
}
if (count == 0) {
console.log('No cards generated.')
} else {
const zip = await apkg.save()
fs.writeFileSync(path.resolve(destination), zip, 'binary');
console.log(`Package has been generated: ${destination} (${count} cards)`);
}
}
makeDeck(args._, options.deckName, outputArg)