Skip to content

Instantly share code, notes, and snippets.

@mager
Created June 23, 2010 20:26
Show Gist options
  • Select an option

  • Save mager/450494 to your computer and use it in GitHub Desktop.

Select an option

Save mager/450494 to your computer and use it in GitHub Desktop.
import simplegeo
import datetime
import time
from lxml import etree
from simplegeo import Client, Record, APIError
MY_OAUTH_KEY = 'my-oauth-key'
MY_OAUTH_SECRET = 'my-oauth-secret'
MY_LAYER = 'gov.usgs.earthquake'
def parse_stupid_timestamp(timestamp):
year, month, daytime = timestamp.split('/')
day, time = daytime.split('_')
hour, minute, second = time.split(':')
return datetime.datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
records = []
def main():
client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET)
f = open('earthquake_data.xml')
xml = f.read()
root = etree.fromstring(xml)
events = {}
for child in [node for node in root.getchildren() if node.tag == 'event']:
event = {}
event['timestamp'] = parse_stupid_timestamp(child.attrib['time-stamp'])
for param in [node for node in child.getchildren() if node.tag == 'param']:
event[param.attrib['name']] = param.attrib['value']
events[child.attrib['id']] = event
for id, event in events.iteritems():
# print '%s: %s, %s @ %s' % (id, event['latitude'], event['longitude'], event['timestamp'])
timestamp = int(time.mktime(event['timestamp'].timetuple()))
record = Record(
layer=MY_LAYER,
id=id,
lat=event['latitude'],
lon=event['longitude'],
created=timestamp,
magnitude=event['magnitude'],
depth=event['depth'],
numphases=event['num-phases'],
azimuthalgap=event['azimuthal-gap'],
magnitudetype=['magnitude-type'],
magnitudetypeext=['magnitude-type-ext'],
standardmagerror=['stand-mag-error']
)
records.append(record)
for chunk in chunker(records, 90):
client.add_records(MY_LAYER, chunk)
print "%s records added" % len(chunk)
def chunker(seq, size):
return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment