Skip to content

Instantly share code, notes, and snippets.

@ukor
Last active March 30, 2020 22:50
Show Gist options
  • Select an option

  • Save ukor/d0d6742dd8382dc8be71cc8cc00b73a3 to your computer and use it in GitHub Desktop.

Select an option

Save ukor/d0d6742dd8382dc8be71cc8cc00b73a3 to your computer and use it in GitHub Desktop.
Parse geojson downloaded from https://wambachers-osm.website/boundaries/
from os import listdir, rename
from os.path import isfile, join
import simplejson as json
import ijson
def read_file(file_path):
print(file_path)
with open(file_path, "rb") as _file:
obj = ijson.items(_file, "features.item")
features = (o for o in obj if o["type"] == "Feature")
for feature in features:
# write to a new file
file_name = feature['properties']['name']
_geoJson = {
"type": "FeatureCollection",
"features": [feature]
}
write_file(file_name, _geoJson)
print(f"Wrote => {file_name}")
write_count = 0
def write_file(file_name, obj):
_f_name = file_name.lower().replace("/", "_").replace(" ", "_")
print(f"Writing geoJSON to => {_f_name}")
_file_path = join("./mapTestBackend/geojson", _f_name)+".json"
with open(_file_path, "w") as json_file:
json.dump(obj, json_file, use_decimal=True, indent=2,
separators=(",", ":"), encoding='utf-8')
write_count += 1
_dir_path = "./uk/"
_files = []
for _file in listdir(_dir_path):
full_path = join(_dir_path, _file)
if isfile(full_path):
new_name = full_path.strip().replace(" ", "_")
rename(full_path, new_name)
# _files.append(new_name)
read_file(new_name)
print(f"Wrote {write_count} files")
simplejson=3.17.0
ijson=2.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment