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.
{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [-97.687675, 30.379732]}, "type": "Feature", "id": 0, "properties": {"time": "2015-01-20T14:42:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.690529, 30.374672]}, "type": "Feature", "id": 1, "properties": {"time": "2015-01-20T14:43:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.69268, 30.370932]}, "type": "Feature", "id": 2, "properties": {"time": "2015-01-20T14:44:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.695625, 30.365734]}, "type": "Feature", "id": 3, "properties": {"time": "2015-01-20T14:45:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.6968, 30.363672]}, "type": "Feature", "id": 4, "properties": {"time": "2015-01-20T14:47:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.700691, 30.357128]}, "type": "Feature", "id": 5, "properties": {"time": "2015-01-20T14:48:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.705757, 30.353251]}, "type": "Feature", "id": 6, "properties": {"time": "2015-01-20T14:49:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.711746, 30.349152]}, "type": "Feature", "id": 7, "properties": {"time": "2015-01-20T14:50:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.715164, 30.343845]}, "type": "Feature", "id": 8, "properties": {"time": "2015-01-20T14:51:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.719025, 30.33774]}, "type": "Feature", "id": 9, "properties": {"time": "2015-01-20T14:52:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.720932, 30.33466]}, "type": "Feature", "id": 10, "properties": {"time": "2015-01-20T14:53:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.722511, 30.332186]}, "type": "Feature", "id": 11, "properties": {"time": "2015-01-20T14:54:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.725868, 30.326822]}, "type": "Feature", "id": 12, "properties": {"time": "2015-01-20T14:55:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.726532, 30.325668]}, "type": "Feature", "id": 13, "properties": {"time": "2015-01-20T14:56:00-06:00"}}, {"geometry": {"type": "Point", "coordinates": [-97.731667, 30.317554]}, "type": "Feature", "id": 14, "properties": {"time": "2015-01-20T14:57:00-06:00"}}]}
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