Script to merge and convert a series of OpenStreetMap XAPI results into a single tab-delimited file. Keys are retained as additional fields. OSM id, latitude, and longitude are always exported.
This file contains 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
#!/usr/bin/env python | |
# --------------------------------------------------------------------------- | |
# xapi_parser.py | |
# Created on: 8 May 2012 | |
# Created by: John Reiser <reiser@rowan.edu> | |
# | |
# Script to merge and convert a series of OpenStreetMap XAPI results into a | |
# single tab-delimited file. Keys are retained as additional fields. OSM id, | |
# latitude, and longitude are always exported. | |
# | |
# License: Copyright (C) 2012, John Reiser | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# --------------------------------------------------------------------------- | |
import glob | |
import xml | |
from xml.dom.minidom import parse | |
data = {} | |
keys = [] | |
xmls = glob.glob("*.xml") | |
for f in xmls: | |
dom = xml.dom.minidom.parse(f) | |
for n in dom.getElementsByTagName("node"): | |
nid = n.getAttribute("id") | |
data[nid] = {} | |
data[nid]["lat"] = n.getAttribute("lat") | |
data[nid]["lon"] = n.getAttribute("lon") | |
for tag in n.getElementsByTagName("tag"): | |
if(tag.hasAttribute("k")): | |
k = tag.getAttribute("k") | |
if(k not in keys): | |
keys.append(k) | |
if(tag.hasAttribute("v")): | |
data[nid][k] = tag.getAttribute("v") | |
keys.sort() | |
print "osmid\tlat\tlon\t" + "\t".join(keys) | |
for node, values in data.items(): | |
print "%s\t%s\t%s\t" % (node, values["lat"], values["lon"]), | |
for k in keys: | |
if(k in values): | |
print values[k] + "\t", | |
else: | |
print " \t", | |
print " " | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment