Skip to content

Instantly share code, notes, and snippets.

@mittenchops
Last active December 22, 2015 16:29
Show Gist options
  • Save mittenchops/6499844 to your computer and use it in GitHub Desktop.
Save mittenchops/6499844 to your computer and use it in GitHub Desktop.
Load GeoJSON, posted here to for SO and mongoDB mailing list references
import json, pymongo, shapefile, sys, ijson
from ijson import common
from ijson.backends import yajl
def floaten(event):
if event[1] == 'number':
return (event[0], event[1], float(event[2]))
else:
return event
# This does not actually work for some reason.
def loader(geojsonfile, collection):
with open(geojsonfile) as jf:
events = imap(floaten, yajl.parse(jf))
i = 0
for d in common.items(events, 'features'):
print('Loading record {}'.format(str(i)))
db[collection].insert(d)
time.sleep(1)
i = i + 1
print("Upload of shapefile: {} into collection: {} successful".format(geojsonfile, collection))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment