Skip to content

Instantly share code, notes, and snippets.

@Zia-
Created April 26, 2016 12:31
Show Gist options
  • Save Zia-/936b8c22e45b509f59c953a0d091d981 to your computer and use it in GitHub Desktop.
Save Zia-/936b8c22e45b509f59c953a0d091d981 to your computer and use it in GitHub Desktop.
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:
# Other attributes can be added using approach here https://gist.github.com/Zia-/f90cea5870a03dd5d8ac822d9c2a9c28
string = "{\"node\":{\"coordinates\":[%s,%s],\"type\":\"Point\"}}" % (n.getAttribute("lon").encode('ascii','ignore'),n.getAttribute("lat").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