Skip to content

Instantly share code, notes, and snippets.

@baditaflorin
Last active September 6, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baditaflorin/83fa05ca27b61acb1622ccd555f144b6 to your computer and use it in GitHub Desktop.
Save baditaflorin/83fa05ca27b61acb1622ccd555f144b6 to your computer and use it in GitHub Desktop.
import glob
import os
import xml.etree.cElementTree as ET
#TODO Interator over a specifc folder,
#TODO increase value by 100000 for each new file
input_folder = './../../cygnus_output/output/'
output_folder = './../../step_11_ready2merge_output/'
start_number = 1
increase_number_by = -100000
from xml.etree.ElementTree import Element, SubElement
def renumber_func(current_file,start_number,increase_number_by):
tree = ET.ElementTree(file=input_folder + current_file)
root = tree.getroot()
for elem in root:
attrs = elem.attrib
id = int(attrs['id'])
if id < 0: # We want to modify just the negative id`s.
elem.set('id',str(int(id) + int(increase_number_by * start_number))) # The result needs to be passed to the XML as a string
if elem.tag =='way': # This is implemented only for ways. If will FAIL if you have relations.
for sub_elem in elem:
if sub_elem.tag == 'nd': # We ignore other tags such as tag
sub_attrs = sub_elem.attrib
ref = int(sub_attrs['ref'])
if ref < 0: # We want to modify just the negative id`s.
sub_elem.set('ref', str(int(ref) + int(increase_number_by * start_number)))
print (sub_elem.attrib)
tree.write(output_folder + "ready2merge_" + current_file, "UTF-8")
for osm in glob.glob1(input_folder,'osmosis*.osm'): #The script is located in ./scripts/python_script_renumber_cygnus.
# And the output files are located in ./cygnus_output/output/
renumber_func(osm,start_number,increase_number_by)
start_number = start_number + 1
print (osm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment