Create a gist now

Instantly share code, notes, and snippets.

Export SimpleGeo Storage Data to CSV
#!/usr/bin/env python
# Written by Cory D. Wiles (http://twitter.com/kwylez) and Bob Waycott (http://twitter.com/bobwaycott)
# Inspired by original work of Javier de la Torre (jatorre@vizzuality.com @jatorre)
# Ruby script: https://gist.github.com/1610866
import csv
import time
from simplegeo import Client
OAUTH_TOKEN = 'XXXXXXX'
OAUTH_SECRET = 'XXXXXXX'
DEBUG = False
TIMEFORMAT = "%Y-%m-%d %H:%M:%S"
def get_layers():
""" Returns simplegeo layers """
client = Client(OAUTH_TOKEN, OAUTH_SECRET)
return client.storage.get_layers()['layers']
def get_record_features_by_name(name):
""" Returns layers from simplegeo records """
client = Client(OAUTH_TOKEN, OAUTH_SECRET)
sg_records = client.storage.get_nearby(name, 0, 0, bbox='-90, -180, 90, 180', limit=500)
return sg_records['features']
def write_csv():
""" Iterate over the records and save a CSV """
for layer in get_layers():
header = False
output_name = layer['name'].replace('_', '.')
layer_file = open(output_name, 'wb')
writer = csv.writer(layer_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for record in get_record_features_by_name(layer['name']):
if DEBUG:
print '**********************Feature %s' % record
# Let's get writing
if not header:
# Write out header rows, excepting the 'layer' key
header_row = ['id', 'lat', 'lon', 'created'] + ['%s' % k for k in record['properties'].iterkeys() if k != 'layer']
writer.writerow(header_row)
header = True
else:
created_on = time.strftime(TIMEFORMAT, time.localtime(record['created']))
lat = record['geometry']['coordinates'][0]
lon = record['geometry']['coordinates'][1]
# Write out CSV row, excepting the 'layer' key
writer.writerow([record['id'], lat, lon, created_on] + ['%s' % v for k, v in record['properties'].iteritems() if k != 'layer'])
layer_file.close()
if __name__ == '__main__':
write_csv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment