Last active
April 12, 2016 14:18
-
-
Save Zia-/f90cea5870a03dd5d8ac822d9c2a9c28 to your computer and use it in GitHub Desktop.
Convert OSM nodes XML data into JSON format
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
import csv | |
from xml.dom import minidom | |
from osgeo import ogr | |
import os, sys | |
abs_file_path = "<path_to_planet_osm_file>/planet.osm"; | |
xmldoc = minidom.parse(abs_file_path) | |
json_list = list() | |
node = xmldoc.getElementsByTagName("node") | |
for n in node: | |
string = "{\"node\": {\"id\": \"%s\",\"lat\": \"%s\",\"lon\": \"%s\",\"version\": \"%s\",\"timestamp\": \"%s\",\"changeset\": \"%s\",\"uid\": \"%s\",\"user\": \"%s\"}}" % (n.getAttribute("id").encode('ascii','ignore'),n.getAttribute("lat").encode('ascii','ignore'),n.getAttribute("lon").encode('ascii','ignore'),n.getAttribute("version").encode('ascii','ignore'),n.getAttribute("timestamp").encode('ascii','ignore'),n.getAttribute("changeset").encode('ascii','ignore'),n.getAttribute("uid").encode('ascii','ignore'),n.getAttribute("user").encode('ascii','ignore')) | |
dic = {} | |
dic['json_key'] = string | |
json_list.append(dic) | |
# If you want to put all json objects separated by commans | |
'''abs_file_path = "<path_to_planet_osm_file>/planet.json"; | |
i = 1; | |
with open(abs_file_path, 'wb') as f: | |
for feature in json_list: | |
data_input = feature['json_key'] | |
f.write(data_input) | |
if (i < len(json_list)): | |
i += 1; | |
f.write(", ")''' | |
# All json objects not separated by commas, used in mongodb import | |
abs_file_path = "<path_to_planet_osm_file>/planet.json"; | |
with open(abs_file_path, 'wb') as f: | |
for feature in json_list: | |
data_input = feature['json_key'] | |
f.write(data_input) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment