You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.1 KiB

  1. const OverpassLayer = require('overpass-layer')
  2. var httpGet = require('./httpGet')
  3. var loadClash = {}
  4. var cache = {}
  5. function wikidataLoad (id, callback) {
  6. if (id in cache) {
  7. return callback(null, cache[id])
  8. }
  9. if (id in loadClash) {
  10. loadClash[id].push(callback)
  11. return
  12. }
  13. loadClash[id] = []
  14. httpGet('https://www.wikidata.org/wiki/Special:EntityData/' + id + '.json', {}, function (err, result) {
  15. if (err) {
  16. return callback(err, null)
  17. }
  18. result = JSON.parse(result.body)
  19. if (!result.entities || !result.entities[id]) {
  20. console.log('invalid result', result)
  21. cache[id] = false
  22. return callback(err, null)
  23. }
  24. cache[id] = result.entities[id]
  25. callback(null, result.entities[id])
  26. loadClash[id].forEach(function (d) {
  27. d(null, result.entities[id])
  28. })
  29. delete loadClash[id]
  30. })
  31. }
  32. module.exports = {
  33. load: wikidataLoad
  34. }
  35. OverpassLayer.twig.extendFilter('wikidataEntity', function (value, param) {
  36. const ob = global.currentMapFeature
  37. if (value in cache) {
  38. return cache[value]
  39. }
  40. wikidataLoad(value, () => {
  41. if (ob) {
  42. ob.recalc()
  43. }
  44. })
  45. return null
  46. })