Created
January 18, 2012 12:45
-
-
Save kwylez/1632842 to your computer and use it in GitHub Desktop.
Export SimpleGeo Storage Data to CSV
This file contains 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
#!/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 ([email protected] @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