Skip to content

Instantly share code, notes, and snippets.

@efwe
Last active January 8, 2016 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efwe/dfc73a236cfc94ef3313 to your computer and use it in GitHub Desktop.
Save efwe/dfc73a236cfc94ef3313 to your computer and use it in GitHub Desktop.
def routes_to
lat = params[:lat].to_f
lon = params[:lon].to_f
features = []
TrackPoint.collection.aggregate([{'$geoNear' => {near: [lon, lat], spherical: true, num: 500, maxDistance: 5/6371, distanceMultiplier: 6371, distanceField: 'dist.calculated'}}, {'$group' => {'_id' => '$route_id'}}]).each { |route_id|
route = Route.find(route_id[:_id])
gjson_line = {
:type => "Feature",
:geometry => {
:type => "LineString",
:coordinates => route.track_points.map { |p| p.location }
}
}
features << gjson_line
}
feature_collection = {
:type => "FeatureCollection",
:features => features
}
render :json => Yajl::Encoder.encode(feature_collection), :content_type => 'application/json'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment