import_facilities.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # coding=utf-8
  2. import os
  3. import sys
  4. from xml.etree import ElementTree
  5. from OpenUmeaRecreationalFacilities import *
  6. """ Because ckanclient won't work with python 3 yet, download this manually from:
  7. $ wget http://openumea.se/dataset/recreational-facilities
  8. API documentation for ckanclient is otherwise available at development site:
  9. https://github.com/okfn/ckanclient
  10. Make sure the filename matches the variable below and is in your current folder.
  11. """
  12. FILE = 'Facilities.xml'
  13. def print_err(*args):
  14. print(*args, file=sys.stderr)
  15. pass
  16. print_err("parsing XML")
  17. full_tree = ElementTree.parse(FILE)
  18. xml_root = full_tree.getroot()
  19. indexdir = '{0}/facilities/'.format(os.getcwd())
  20. indexxml = indexdir + '/facilities.xml'
  21. os.makedirs(indexdir, exist_ok=True)
  22. markerout = open(indexdir + '/markers.tsv', mode='w')
  23. markercols = '{0}\t{1}\t{2}\t{3}\t{4}\n'.format('id', 'lat', 'lon', 'title', 'description')
  24. markerout.write(markercols)
  25. print_err("traversing XML")
  26. for element in list(xml_root):
  27. try:
  28. if int(element.attrib['ID']) <= 0: # use .get('ID', 0) instead?
  29. continue
  30. #raise ValueError("Invalid Facility ID: {0}".format(element.attrib['ID']))
  31. current = Facility(element)
  32. if current.Coordinate == None:
  33. continue
  34. filedir = indexdir + '{0}/'.format(current.ID)
  35. xmlfile = filedir + 'object.xml'
  36. objfile = filedir + 'facility.tsv'
  37. geofile = filedir + 'coords.txt'
  38. imgdir = filedir + 'images'
  39. os.makedirs(imgdir, exist_ok=True)
  40. with open(xmlfile, mode='w', encoding='utf-8') as fileout:
  41. fileout.write(str(current))
  42. with open(objfile, mode='w', encoding='utf-8') as fileout:
  43. fileout.write(markercols)
  44. fileout.write(current.cols())
  45. with open(geofile, mode='w', encoding='utf-8') as fileout:
  46. fileout.write(str(current.Coordinate))
  47. markerout.write("{0}\n".format(current.cols()))
  48. except NameError as detail:
  49. print_err("Bad location entry: ", detail)
  50. except IOError as detail:
  51. print_err("Could not print data", detail)
  52. except ValueError as detail:
  53. print_err("Bad data value: ", detail)
  54. xml_root.remove(element)
  55. except:
  56. print_err("Unexpected error:", sys.exc_info()[0])
  57. raise
  58. markerout.close()
  59. full_tree.write(indexxml, encoding='utf-8', xml_declaration=True)