sampler.py 929 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Use cElementTree or lxml if too slow
  4. import xml.etree.ElementTree as ET
  5. OSM_FILE = "san-francisco_california.osm"
  6. SAMPLE_FILE = "sample.osm"
  7. def get_element(osm_file, tags=('node', 'way', 'relation')):
  8. """Yield element if it is the right type of tag.
  9. """
  10. context = ET.iterparse(osm_file, events=('start', 'end'))
  11. _, root = next(context)
  12. for event, elem in context:
  13. if event == 'end' and elem.tag in tags:
  14. yield elem
  15. root.clear()
  16. with open(SAMPLE_FILE, 'wb') as output:
  17. output.write(bytes('<?xml version="1.0" encoding="UTF-8"?>\n', 'UTF-8'))
  18. output.write(bytes('<osm>\n ', 'UTF-8'))
  19. # Write every 130th top level element
  20. for i, element in enumerate(get_element(OSM_FILE)):
  21. if i % 130 == 0:
  22. output.write(ET.tostring(element, encoding='utf-8'))
  23. output.write(bytes('</osm>', 'UTF-8'))