Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luqmaan/833cb3fdfbef1b61cd2d to your computer and use it in GitHub Desktop.
Save luqmaan/833cb3fdfbef1b61cd2d to your computer and use it in GitHub Desktop.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/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