-
Notifications
You must be signed in to change notification settings - Fork 1
/
geotag.js
84 lines (76 loc) · 2.49 KB
/
geotag.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
const fs = require('fs')
const osmtogeojson = require('osmtogeojson')
const AnkiExport = require('anki-apkg-export').default
const { mapBounds } = require('./lib/overpass.js')
const render = require('./lib/render.js')
const { transform, inside, invertYAxe } = require('./lib/transform.js')
const { generate, save } = require('./lib/photos.js')
const options = require('./config/options.js')
const prompt = 'Where was this photo taken?'
const args = require('minimist')(process.argv.slice(2))
let outputArg = args.o
if (!outputArg) {
if (!fs.existsSync('output')) {
fs.mkdirSync('output')
}
outputArg = 'output/geotag.apkg'
}
let useCache = true
let cache = []
async function mapAround(lat, lon) {
const r = options.mapContextRadius
const bounds = {
minlat: lat - r,
maxlat: lat + r,
minlon: lon - r,
maxlon: lon + r
}
let map
if (useCache) {
// Avoid hitting the API repeatedly for nearby locations
let hit = false
for (const c of cache) {
if (inside(c.bounds, bounds)) {
hit = true
map = transform(osmtogeojson(c.map), bounds)
break
}
}
if (!hit) {
const wideBounds = {
minlat: lat - 2*r,
maxlat: lat + 2*r,
minlon: lon - 2*r,
maxlon: lon + 2*r
}
const osm = await mapBounds(wideBounds, true)
cache.push({
bounds: wideBounds,
map: osm
})
map = transform(osmtogeojson(osm), bounds)
}
} else {
const osm = await mapBounds(bounds, true)
map = transform(osmtogeojson(osm), bounds)
}
invertYAxe(map)
const image = await render(map, options.mapSize, options.mapSize, options.mapDetail, true)
return image
}
async function geotagCard(exif, image, deck, count) {
if (!exif || !('latitude' in exif) || !('longitude' in exif)) {
return 'Unable to find geotag'
}
const frontName = count + '-front.png'
const backName = count + '-back.png'
const buf = await mapAround(exif.latitude, exif.longitude)
deck.addMedia(frontName, image)
deck.addMedia(backName, buf)
deck.addCard(`${prompt}<br><img src="${frontName}" />`, `<img src="${backName}" />`)
}
console.log('Initializing deck...')
const apkg = new AnkiExport(options.deckName)
generate(geotagCard, apkg, args._).then(count => {
save(deck, outputArg, count)
})