Route elevation profile (climb, descent, max grade) from the terrain tiles, and reach (isochrones) in the route panel
This commit is contained in:
1 parent
b80575828a
commit
cca985d2b9
10 files changed
+274
-26
No files matched your search
+45
-17
@@ -143,24 +143,11 @@ module Maptool
|
||||
# (km), time (s) and the turn-by-turn instructions.
|
||||
def self.route(valhalla_url, locations, vehicle)
|
||||
raise RouteError, "at least two points are needed" if locations.size < 2
|
||||
raise Unavailable, "routing is not set up" if valhalla_url.to_s.empty?
|
||||
|
||||
car = vehicle[:weight] <= CAR_MAX_WEIGHT
|
||||
costing = car ? "auto" : "truck"
|
||||
options = car ? vehicle.slice(:height, :width) : vehicle
|
||||
body = { locations: locations.map { |lon, lat| { lon: Float(lon), lat: Float(lat) } },
|
||||
costing: costing, costing_options: { costing => options }, units: "kilometers" }
|
||||
uri = URI("#{valhalla_url}/route")
|
||||
response = begin
|
||||
Net::HTTP.start(uri.host, uri.port, open_timeout: 2, read_timeout: 60) do |http|
|
||||
http.post(uri.path, JSON.generate(body), "Content-Type" => "application/json")
|
||||
end
|
||||
rescue SystemCallError, Net::OpenTimeout, SocketError
|
||||
raise Unavailable, "the routing service is not running"
|
||||
end
|
||||
data = JSON.parse(response.body)
|
||||
raise RouteError, data["error"] || "no route found" unless response.is_a?(Net::HTTPSuccess)
|
||||
|
||||
costing, options = costing(vehicle)
|
||||
data = valhalla(valhalla_url, "/route",
|
||||
locations: locations.map { |lon, lat| { lon: Float(lon), lat: Float(lat) } },
|
||||
costing: costing, costing_options: { costing => options }, units: "kilometers")
|
||||
legs = data["trip"]["legs"]
|
||||
{ type: "Feature",
|
||||
geometry: { type: "LineString", coordinates: legs.flat_map { |leg| decode_polyline(leg["shape"]) } },
|
||||
@@ -171,6 +158,47 @@ module Maptool
|
||||
raise RouteError, "invalid coordinates"
|
||||
end
|
||||
|
||||
ISOCHRONE_MAX_MINUTES = 240 # Valhalla's own default limit
|
||||
|
||||
# How far the vehicle gets from [lon, lat] in each of `minutes`: a
|
||||
# GeoJSON FeatureCollection of polygons, largest first.
|
||||
def self.isochrone(valhalla_url, center, minutes, vehicle)
|
||||
lon, lat = center
|
||||
minutes = Array(minutes).map { |m| Integer(m) }.uniq.sort
|
||||
raise RouteError, "1 to 4 times between 1 and #{ISOCHRONE_MAX_MINUTES} minutes" unless (1..4).cover?(minutes.size) && minutes.all? { |m| (1..ISOCHRONE_MAX_MINUTES).cover?(m) }
|
||||
|
||||
costing, options = costing(vehicle)
|
||||
valhalla(valhalla_url, "/isochrone",
|
||||
locations: [{ lon: Float(lon), lat: Float(lat) }], costing: costing, costing_options: { costing => options },
|
||||
contours: minutes.map { |m| { time: m } }, polygons: true)
|
||||
rescue ArgumentError, TypeError
|
||||
raise RouteError, "invalid center or times"
|
||||
end
|
||||
|
||||
# Up to CAR_MAX_WEIGHT "auto" with height/width, above it "truck".
|
||||
def self.costing(vehicle)
|
||||
vehicle[:weight].to_f <= CAR_MAX_WEIGHT ? ["auto", vehicle.slice(:height, :width)] : ["truck", vehicle]
|
||||
end
|
||||
private_class_method :costing
|
||||
|
||||
def self.valhalla(valhalla_url, path, body)
|
||||
raise Unavailable, "routing is not set up" if valhalla_url.to_s.empty?
|
||||
|
||||
uri = URI("#{valhalla_url}#{path}")
|
||||
response = begin
|
||||
Net::HTTP.start(uri.host, uri.port, open_timeout: 2, read_timeout: 60) do |http|
|
||||
http.post(uri.path, JSON.generate(body), "Content-Type" => "application/json")
|
||||
end
|
||||
rescue SystemCallError, Net::OpenTimeout, SocketError
|
||||
raise Unavailable, "the routing service is not running"
|
||||
end
|
||||
data = JSON.parse(response.body)
|
||||
raise RouteError, data["error"] || "no result" unless response.is_a?(Net::HTTPSuccess)
|
||||
|
||||
data
|
||||
end
|
||||
private_class_method :valhalla
|
||||
|
||||
# Valhalla's encoded polyline (precision 6) → [[lon, lat], ...].
|
||||
def self.decode_polyline(encoded)
|
||||
values = []
|
||||
|
||||
Reference in new issue
Block a user