Forked from scascketta/11106@2015-01-20T14:57:00-06:00<---->2015-01-20T15:31:00-06:00.geojson
Last active
August 29, 2015 14:13
-
-
Save luqmaan/833cb3fdfbef1b61cd2d to your computer and use it in GitHub Desktop.
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 | |
import rethinkdb as r | |
import geojson as gj | |
import arrow | |
import sys | |
def make_feature(vehicle, id): | |
point = gj.Point(tuple(vehicle['location']['coordinates'])) | |
time = arrow.get(vehicle['timestamp']).to('America/Chicago') | |
f = { | |
'geometry': point, | |
'properties': {'time': time.isoformat()}, | |
'id': id | |
} | |
return gj.Feature(**f) | |
def make_featcoll(vehicle_id, limit): | |
docs = get_vehicle_data(vehicle_id, limit) | |
features = [] | |
for ind, doc in enumerate(docs): | |
features.append(make_feature(doc, ind)) | |
feat_coll = gj.FeatureCollection(features) | |
start = arrow.get(docs[0]['timestamp']).to('America/Chicago').isoformat() | |
end = arrow.get(docs[-1]['timestamp']).to('America/Chicago').isoformat() | |
fname = '{vid}@{start}<-->{end}.geojson'.format(vid=vehicle_id, start=start, end=end) | |
with open(fname, 'w') as f: | |
gj.dump(feat_coll, f) | |
def get_vehicle_data(vehicle_id, limit): | |
return list(r.table('vehicle_position') \ | |
.filter(r.row['vehicle_id'] == vehicle_id) \ | |
.order_by('timestamp') \ | |
.limit(int(limit)).run()) | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print 'Usage: explore.py <vehicle_id> <limit>' | |
sys.exit(-1) | |
r.connect('localhost', 28015).repl() | |
make_featcoll(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment