Last active
April 21, 2016 12:49
-
-
Save Zia-/062d16ff32c33c83922c691275a15021 to your computer and use it in GitHub Desktop.
You are given a list of osm ways' ID and you want to collect only them, along with corresponding nodes, to generate new osm file. Run this python. Will generate two files, final_osmfile.osm is the one you want at the end. You can generate selected_ways.txt by simply clicking the required ways in JOSM and pressing ctrl+c and ctrl+v.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.dom import minidom | |
from osgeo import ogr | |
import os, re | |
proj_dir = os.getcwd() | |
rel_file_path = "initial_osmfile.osm"; | |
abs_file_path = os.path.join(proj_dir, rel_file_path) | |
xmldoc = minidom.parse(abs_file_path) | |
node = xmldoc.getElementsByTagName("node") | |
way = xmldoc.getElementsByTagName("way") | |
rel_path_mid = "middle_file.osm" | |
with open(os.path.join(proj_dir, "selected_ways.txt")) as selected_ways: | |
content = selected_ways.readlines(); | |
node_str = ''; | |
way_str = ''; | |
with open(os.path.join(proj_dir, rel_path_mid), 'w') as f: | |
f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") | |
f.write("<osm version=\"0.6\" generator=\"Overpass API\">") | |
f.write("<meta osm_base=\"2016-04-21T07:05:03Z\"/>") | |
for n in node: | |
node_str = node_str + (n.toprettyxml(indent = ' ')) | |
cont = []; | |
for c in range(0,len(content)): | |
wordList = re.sub("[^\w]", " ", content[c]).split() | |
cont.append(wordList[1]); | |
for w in way: | |
if w.getAttribute("id") in cont: | |
way_str = way_str + (w.toprettyxml(indent = ' ')) | |
node_str = node_str.encode('ascii', 'ignore').decode('ascii') | |
way_str = way_str.encode('ascii', 'ignore').decode('ascii') | |
f.write(node_str + way_str) | |
f.write("</osm>") | |
# Again ......................................................... | |
xmldoc = minidom.parse(os.path.join(proj_dir, rel_path_mid)) | |
node = xmldoc.getElementsByTagName("node") | |
way = xmldoc.getElementsByTagName("way") | |
rel_path_end = "final_osmfile.osm" | |
node_str = ''; | |
way_str = ''; | |
with open(os.path.join(proj_dir, rel_path_end), 'w') as f: | |
f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") | |
f.write("<osm version=\"0.6\" generator=\"Overpass API\">") | |
f.write("<meta osm_base=\"2016-04-21T07:05:03Z\"/>") | |
for w in way: | |
way_str = way_str + (w.toprettyxml(indent = ' ')) | |
''' collect all nodes here first ''' | |
nd = w.getElementsByTagName("nd") | |
for nn in nd: | |
ref = nn.getAttribute("ref") | |
for n in node: | |
if (ref == n.getAttribute("id")): | |
node_str = node_str + (n.toprettyxml(indent = ' ')) | |
node_str = node_str.encode('ascii', 'ignore').decode('ascii') | |
way_str = way_str.encode('ascii', 'ignore').decode('ascii') | |
f.write(node_str + way_str) | |
f.write("</osm>") | |
# str_osm = ''; | |
# for w in way: | |
# cont = []; | |
# for c in range(0,len(content)): | |
# wordList = re.sub("[^\w]", " ", content[c]).split() | |
# cont.append(wordList[1]); | |
# if w.getAttribute("id") not in cont: | |
# # print "not"; | |
# parentNode = w.parentNode | |
# parentNode.insertBefore(xmldoc.createComment(w.toxml()), w) | |
# parentNode.removeChild(w) | |
# f = open(abs_file_path, "w") | |
# f.write(xmldoc.toxml()) | |
# f.close() | |
# way_str = ''; | |
# for c in range(0,len(content)): | |
# wordList = re.sub("[^\w]", " ", content[c]).split() | |
# for w in way: | |
# id1 = w.getAttribute("id") | |
# if (id1 == wordList[1]): | |
# # print "found"; | |
# way_str = way_str + (w.toprettyxml(indent = ' ')) | |
# # print (w.toprettyxml(indent = ' ')) | |
# # print id1 | |
# # print wordList[1] | |
# | |
# print way_str | |
# # | |
# doc = minidom.Document() | |
# foo = doc.createElement("foo") | |
# doc.appendChild(foo) | |
# print(foo.__class__) | |
# # xml.dom.minidom.Element | |
# | |
# print(foo.toprettyxml(indent = ' ')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
way 145430100 | |
way 26426584 | |
way 313727372 | |
way 48733326 | |
way 231051208 | |
way 337012187 | |
way 337012182 | |
way 227612570 | |
way 218081463 | |
way 230931460 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment