From 714719c3776c5985a8881cba32f48f8a4de5a70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Rodrigo?= Date: Sat, 15 Dec 2018 05:58:07 +0100 Subject: [PATCH] Lua maxspeed parsing refactoring (#5144) * Lua maxspeed parsing refactoring --- CHANGELOG.md | 1 + profiles/bicycle.lua | 21 ++++----------------- profiles/lib/measure.lua | 19 +++++++++++++++++++ profiles/lib/way_handlers.lua | 9 +++------ 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44864a837..d83dc0e3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - CHANGED: matching will now consider edges marked with is_startpoint=false, allowing matching over ferries and other previously non-matchable edge types. [#5297](https://github.com/Project-OSRM/osrm-backend/pull/5297) - Profile: - ADDED: Parse `source:maxspeed` and `maxspeed:type` tags to apply maxspeeds and add belgian flanders rural speed limit. [#5217](https://github.com/Project-OSRM/osrm-backend/pull/5217) + - CHANGED: Refactor maxspeed parsing to use common library. [#5144](https://github.com/Project-OSRM/osrm-backend/pull/5144) # 5.20.0 - Changes from 5.19.0: diff --git a/profiles/bicycle.lua b/profiles/bicycle.lua index 0cd3a62c9..a9ccc4611 100644 --- a/profiles/bicycle.lua +++ b/profiles/bicycle.lua @@ -7,6 +7,7 @@ Sequence = require('lib/sequence') Handlers = require("lib/way_handlers") find_access_tag = require("lib/access").find_access_tag limit = require("lib/maxspeed").limit +Measure = require("lib/measure") function setup() local default_speed = 15 @@ -206,20 +207,6 @@ function setup() } end -local function parse_maxspeed(source) - if not source then - return 0 - end - local n = tonumber(source:match("%d*")) - if not n then - n = 0 - end - if string.match(source, "mph") or string.match(source, "mp/h") then - n = (n*1609)/1000 - end - return n -end - function process_node(profile, node, result) -- parse access and barrier tags local highway = node:get_value_by_key("highway") @@ -276,9 +263,9 @@ function handle_bicycle_tags(profile,way,result,data) -- other tags data.junction = way:get_value_by_key("junction") - data.maxspeed = parse_maxspeed(way:get_value_by_key ( "maxspeed") ) - data.maxspeed_forward = parse_maxspeed(way:get_value_by_key( "maxspeed:forward")) - data.maxspeed_backward = parse_maxspeed(way:get_value_by_key( "maxspeed:backward")) + data.maxspeed = Measure.get_max_speed(way:get_value_by_key ("maxspeed")) or 0 + data.maxspeed_forward = Measure.get_max_speed(way:get_value_by_key("maxspeed:forward")) or 0 + data.maxspeed_backward = Measure.get_max_speed(way:get_value_by_key("maxspeed:backward")) or 0 data.barrier = way:get_value_by_key("barrier") data.oneway = way:get_value_by_key("oneway") data.oneway_bicycle = way:get_value_by_key("oneway:bicycle") diff --git a/profiles/lib/measure.lua b/profiles/lib/measure.lua index 6bcff08f8..d1f00c601 100644 --- a/profiles/lib/measure.lua +++ b/profiles/lib/measure.lua @@ -6,6 +6,18 @@ Measure = {} local inch_to_meters = 0.0254 local feet_to_inches = 12 local pound_to_kilograms = 0.45359237 +local miles_to_kilometers = 1.609 + +-- Parse speed value as kilometers by hours. +function Measure.parse_value_speed(source) + local n = tonumber(source:match("%d*")) + if n then + if string.match(source, "mph") or string.match(source, "mp/h") then + n = n * miles_to_kilometers + end + return n + end +end --- Parse string as a height in meters. --- according to http://wiki.openstreetmap.org/wiki/Key:maxheight @@ -42,6 +54,13 @@ function Measure.parse_value_kilograms(value) end end +--- Get maxspeed of specified way in kilometers by hours. +function Measure.get_max_speed(raw_value) + if raw_value then + return Measure.parse_value_speed(raw_value) + end +end + -- default maxheight value defined in https://wiki.openstreetmap.org/wiki/Key:maxheight#Non-numerical_values local default_maxheight = 4.5 -- Available Non numerical values equal to 4.5; below_default and no_indications are not considered diff --git a/profiles/lib/way_handlers.lua b/profiles/lib/way_handlers.lua index 968cc9aa6..b13410235 100644 --- a/profiles/lib/way_handlers.lua +++ b/profiles/lib/way_handlers.lua @@ -450,12 +450,9 @@ function WayHandlers.parse_maxspeed(source,profile) if not source then return 0 end - local n = tonumber(source:match("%d*")) - if n then - if string.match(source, "mph") or string.match(source, "mp/h") then - n = (n*1609)/1000 - end - else + + local n = Measure.get_max_speed(source) + if not n then -- parse maxspeed like FR:urban source = string.lower(source) n = profile.maxspeed_table[source]