openstreetmap.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ## OpenStreetMap (Map)
  2. #
  3. # @website https://openstreetmap.org/
  4. # @provide-api yes (http://wiki.openstreetmap.org/wiki/Nominatim)
  5. #
  6. # @using-api yes
  7. # @results JSON
  8. # @stable yes
  9. # @parse url, title
  10. from json import loads
  11. # engine dependent config
  12. categories = ['map']
  13. paging = False
  14. # search-url
  15. url = 'https://nominatim.openstreetmap.org/search/{query}?format=json&polygon_geojson=1'
  16. result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
  17. # do search-request
  18. def request(query, params):
  19. params['url'] = url.format(query=query)
  20. return params
  21. # get response from search-request
  22. def response(resp):
  23. results = []
  24. json = loads(resp.text)
  25. # parse results
  26. for r in json:
  27. title = r['display_name']
  28. osm_type = r.get('osm_type', r.get('type'))
  29. url = result_base_url.format(osm_type=osm_type,
  30. osm_id=r['osm_id'])
  31. geojson = r.get('geojson')
  32. # if no geojson is found and osm_type is a node, add geojson Point
  33. if not geojson and\
  34. osm_type == 'node':
  35. geojson = {u'type':u'Point',
  36. u'coordinates':[r['lon'],r['lat']]}
  37. # append result
  38. results.append({'template': 'map.html',
  39. 'title': title,
  40. 'content': '',
  41. 'longitude': r['lon'],
  42. 'latitude': r['lat'],
  43. 'boundingbox': r['boundingbox'],
  44. 'geojson': geojson,
  45. 'url': url})
  46. # return results
  47. return results