-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrink_file.py
33 lines (24 loc) · 1.05 KB
/
shrink_file.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET # Use cElementTree or lxml if too slow
OSM_FILE = "goettingen.osm" # Replace this with your osm file
SAMPLE_FILE = "sample.osm"
def get_element(osm_file, tags=('node', 'way', 'relation')):
"""Yield element if it is the right type of tag
Reference:
http://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python
"""
context = ET.iterparse(osm_file, events=('start', 'end'))
_, root = next(context)
for event, elem in context:
if event == 'end' and elem.tag in tags:
yield elem
root.clear()
with open(SAMPLE_FILE, 'wb') as output:
output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
output.write('<osm version="0.6" generator="Overpass API">\n ')
# Write every 10th top level element
for i, element in enumerate(get_element(OSM_FILE)):
if i % 10 == 0:
output.write(ET.tostring(element, encoding='utf-8'))
output.write('</osm>')