From ebd8c84940388f4816404ccacee5390ef8df6ed7 Mon Sep 17 00:00:00 2001 From: Emil Tin Date: Sun, 5 May 2013 09:11:11 +0200 Subject: [PATCH] easy way to set both directions from lua, maxspeed helper --- Extractor/ExtractorStructs.h | 21 ++++++- Extractor/ScriptingEnvironment.cpp | 2 + features/bicycle/mode.feature | 67 +++++++++++++-------- profiles/bicycle.lua | 93 ++++++++++++------------------ profiles/lib/maxspeed.lua | 17 ++++++ 5 files changed, 120 insertions(+), 80 deletions(-) create mode 100644 profiles/lib/maxspeed.lua diff --git a/Extractor/ExtractorStructs.h b/Extractor/ExtractorStructs.h index cf3bb1c6f..6f2b9a47e 100644 --- a/Extractor/ExtractorStructs.h +++ b/Extractor/ExtractorStructs.h @@ -81,7 +81,26 @@ struct ExtractionWay { } return ExtractionWay::bidirectional; } - + + inline void set_mode(const TravelMode m) { forward.mode = m; backward.mode = m; } + inline const TravelMode get_mode() { + if( forward.mode == backward.mode ) { + return forward.mode; + } else { + return -1; + } + } + + inline void set_speed(const double s) { forward.speed = s; backward.speed = s; } + inline const double get_speed() { + if( forward.speed == backward.speed ) { + return forward.speed; + } else { + return -1; + } + } + + unsigned id; unsigned nameID; std::string name; diff --git a/Extractor/ScriptingEnvironment.cpp b/Extractor/ScriptingEnvironment.cpp index 4452d3c48..509c5f096 100644 --- a/Extractor/ScriptingEnvironment.cpp +++ b/Extractor/ScriptingEnvironment.cpp @@ -76,6 +76,8 @@ ScriptingEnvironment::ScriptingEnvironment(const char * fileName) { .def_readwrite("tags", &ExtractionWay::keyVals) .def_readwrite("forward", &ExtractionWay::forward) .def_readwrite("backward", &ExtractionWay::backward) + .property("mode", &ExtractionWay::get_mode, &ExtractionWay::set_mode) + .property("speed", &ExtractionWay::get_speed, &ExtractionWay::set_speed) ]; luabind::module(myLuaState) [ diff --git a/features/bicycle/mode.feature b/features/bicycle/mode.feature index cf9afeeee..19b35c5c2 100644 --- a/features/bicycle/mode.feature +++ b/features/bicycle/mode.feature @@ -3,8 +3,9 @@ Feature: Bike - Mode flag # bicycle modes: # 1 bike -# 2 ferry -# 3 push +# 2 pushing +# 3 ferry +# 4 train Background: Given the profile "bicycle" @@ -22,12 +23,32 @@ Feature: Bike - Mode flag When I route I should get | from | to | route | turns | modes | - | a | d | ab,bc,cd | head,right,left,destination | 1,2,1 | - | d | a | cd,bc,ab | head,right,left,destination | 1,2,1 | - | c | a | bc,ab | head,left,destination | 2,1 | - | d | b | cd,bc | head,right,destination | 1,2 | - | a | c | ab,bc | head,right,destination | 1,2 | - | b | d | bc,cd | head,left,destination | 2,1 | + | a | d | ab,bc,cd | head,right,left,destination | 1,3,1 | + | d | a | cd,bc,ab | head,right,left,destination | 1,3,1 | + | c | a | bc,ab | head,left,destination | 3,1 | + | d | b | cd,bc | head,right,destination | 1,3 | + | a | c | ab,bc | head,right,destination | 1,3 | + | b | d | bc,cd | head,left,destination | 3,1 | + + Scenario: Bike - Mode when using a train + Given the node map + | a | b | | + | | c | d | + + And the ways + | nodes | highway | railway | bicycle | + | ab | primary | | | + | bc | | train | yes | + | cd | primary | | | + + When I route I should get + | from | to | route | turns | modes | + | a | d | ab,bc,cd | head,right,left,destination | 1,4,1 | + | d | a | cd,bc,ab | head,right,left,destination | 1,4,1 | + | c | a | bc,ab | head,left,destination | 4,1 | + | d | b | cd,bc | head,right,destination | 1,4 | + | a | c | ab,bc | head,right,destination | 1,4 | + | b | d | bc,cd | head,left,destination | 4,1 | Scenario: Bike - Mode when pushing bike against oneways Given the node map @@ -43,9 +64,9 @@ Feature: Bike - Mode flag When I route I should get | from | to | route | turns | modes | | a | d | ab,bc,cd | head,right,left,destination | 1,1,1 | - | d | a | cd,bc,ab | head,right,left,destination | 1,3,1 | - | c | a | bc,ab | head,left,destination | 3,1 | - | d | b | cd,bc | head,right,destination | 1,3 | + | d | a | cd,bc,ab | head,right,left,destination | 1,2,1 | + | c | a | bc,ab | head,left,destination | 2,1 | + | d | b | cd,bc | head,right,destination | 1,2 | | a | c | ab,bc | head,right,destination | 1,1 | | b | d | bc,cd | head,left,destination | 1,1 | @@ -62,12 +83,12 @@ Feature: Bike - Mode flag When I route I should get | from | to | route | turns | modes | - | a | d | ab,bc,cd | head,right,left,destination | 1,3,1 | - | d | a | cd,bc,ab | head,right,left,destination | 1,3,1 | - | c | a | bc,ab | head,left,destination | 3,1 | - | d | b | cd,bc | head,right,destination | 1,3 | - | a | c | ab,bc | head,right,destination | 1,3 | - | b | d | bc,cd | head,left,destination | 3,1 | + | a | d | ab,bc,cd | head,right,left,destination | 1,2,1 | + | d | a | cd,bc,ab | head,right,left,destination | 1,2,1 | + | c | a | bc,ab | head,left,destination | 2,1 | + | d | b | cd,bc | head,right,destination | 1,2 | + | a | c | ab,bc | head,right,destination | 1,2 | + | b | d | bc,cd | head,left,destination | 2,1 | Scenario: Bike - Mode when pushing on pedestrain areas Given the node map @@ -82,9 +103,9 @@ Feature: Bike - Mode flag When I route I should get | from | to | route | modes | - | a | f | ab,bcd,df | 1,3,1 | - | f | a | df,bcd,ab | 1,3,1 | - | d | a | bcd,ab | 3,1 | - | f | b | df,bcd | 1,3 | - | a | d | ab,bcd | 1,3 | - | b | f | bcd,df | 3,1 | + | a | f | ab,bcd,df | 1,2,1 | + | f | a | df,bcd,ab | 1,2,1 | + | d | a | bcd,ab | 2,1 | + | f | b | df,bcd | 1,2 | + | a | d | ab,bcd | 1,2 | + | b | f | bcd,df | 2,1 | diff --git a/profiles/bicycle.lua b/profiles/bicycle.lua index 250ded0fd..41dc036e0 100644 --- a/profiles/bicycle.lua +++ b/profiles/bicycle.lua @@ -1,4 +1,5 @@ require("lib/access") +require("lib/maxspeed") -- Begin of globals barrier_whitelist = { [""] = true, ["cycle_barrier"] = true, ["bollard"] = true, ["entrance"] = true, ["cattle_grid"] = true, ["border_control"] = true, ["toll_booth"] = true, ["sally_port"] = true, ["gate"] = true, ["no"] = true} @@ -95,6 +96,13 @@ turn_penalty = 60 turn_bias = 1.4 -- End of globals +--modes +mode_normal = 1 +mode_pushing = 2 +mode_ferry = 3 +mode_train = 4 + + function get_exceptions(vector) for i,v in ipairs(restriction_exception_tags) do vector:Add(v) @@ -191,76 +199,62 @@ function way_function (way) way.name = "{highway:"..highway.."}" -- if no name exists, use way type -- this encoding scheme is excepted to be a temporary solution end - - way.forward.mode = 1 - way.backward.mode = 1 + + way.mode = mode_normal -- speed if route_speeds[route] then -- ferries (doesn't cover routes tagged using relations) + way.mode = mode_ferry way.ignore_in_grid = true if durationIsValid(duration) then way.duration = math.max( 1, parseDuration(duration) ) else - way.forward.speed = route_speeds[route] - way.backward.speed = route_speeds[route] + way.speed = route_speeds[route] end - way.forward.mode = 2 - way.backward.mode = 2 elseif platform_speeds[railway] then -- railway platforms (old tagging scheme) - way.forward.speed = platform_speeds[railway] - way.backward.speed = platform_speeds[railway] + way.speed = platform_speeds[railway] elseif platform_speeds[public_transport] then -- public_transport platforms (new tagging platform) - way.forward.speed = platform_speeds[public_transport] - way.backward.speed = platform_speeds[public_transport] + way.speed = platform_speeds[public_transport] elseif railway_speeds[railway] then -- railways if access and access_tag_whitelist[access] then - way.forward.speed = railway_speeds[railway] - way.backward.speed = railway_speeds[railway] + way.mode = mode_train + way.speed = railway_speeds[railway] end elseif amenity_speeds[amenity] then -- parking areas - way.forward.speed = amenity_speeds[amenity] - way.backward.speed = amenity_speeds[amenity] + way.speed = amenity_speeds[amenity] elseif bicycle_speeds[highway] then -- regular ways - way.forward.speed = bicycle_speeds[highway] - way.backward.speed = bicycle_speeds[highway] + way.speed = bicycle_speeds[highway] elseif access_tag_whitelist[access] then -- unknown way, but valid access tag - way.forward.speed = default_speed - way.backward.speed = default_speed + way.speed = default_speed else -- biking not allowed, maybe we can push our bike? -- essentially requires pedestrian profiling, for example foot=no mean we can't push a bike if foot ~= 'no' then if pedestrian_speeds[highway] then -- pedestrian-only ways and areas - way.forward.speed = pedestrian_speeds[highway] - way.backward.speed = pedestrian_speeds[highway] - way.forward.mode = 3 - way.backward.mode = 3 + way.speed = pedestrian_speeds[highway] + way.mode = mode_pushing elseif man_made and man_made_speeds[man_made] then -- man made structures - way.forward.speed = man_made_speeds[man_made] - way.backward.speed = man_made_speeds[man_made] - way.forward.mode = 3 - way.backward.mode = 3 + way.speed = man_made_speeds[man_made] + way.mode = mode_pushing elseif foot == 'yes' then - way.forward.speed = walking_speed - way.backward.speed = walking_speed - way.forward.mode = 3 - way.backward.mode = 3 + way.speed = walking_speed + way.mode = mode_pushing elseif foot_forward == 'yes' then way.forward.speed = walking_speed - way.forward.mode = 3 + way.forward.mode = mode_pushing way.backward.mode = 0 elseif foot_backward == 'yes' then way.backward.speed = walking_speed - way.backward.mode = 3 + way.backward.mode = mode_pushing way.forward.mode = 0 end end @@ -283,18 +277,18 @@ function way_function (way) elseif string.find(cycleway, "opposite") == 1 then if impliedOneway then way.forward.mode = 0 - way.backward.mode = 1 + way.backward.mode = mode_normal end elseif cycleway_tags[cycleway_left] and cycleway_tags[cycleway_right] then -- prevent implied elseif cycleway_tags[cycleway_left] then if impliedOneway then way.forward.mode = 0 - way.backward.mode = 1 + way.backward.mode = mode_normal end elseif cycleway_tags[cycleway_right] then if impliedOneway then - way.forward.mode = 1 + way.forward.mode = mode_normal way.backward.mode = 0 end elseif oneway == "-1" then @@ -310,10 +304,10 @@ function way_function (way) if junction ~= "roundabout" then if way.backward.mode == 0 then way.backward.speed = walking_speed - way.backward.mode = 3 + way.backward.mode = mode_pushing elseif way.forward.mode == 0 then way.forward.speed = walking_speed - way.forward.mode = 3 + way.forward.mode = mode_pushing end end end @@ -332,27 +326,14 @@ function way_function (way) end -- surfaces - if surface then - surface_speed = surface_speeds[surface] - if surface_speed then - way.forward.speed = math.min(way.forward.speed, surface_speed) - way.backward.speed = math.min(way.backward.speed, surface_speed) - end + if surface_speeds[surface] then + way.forward.speed = math.min(way.forward.speed, surface_speeds[surface]) + way.backward.speed = math.min(way.backward.speed, surface_speeds[surface]) end -- maxspeed - if maxspeed_forward and maxspeed_forward>0 then - way.forward.speed = math.min(way.forward.speed, maxspeed_forward) - elseif maxspeed and maxspeed>0 then - way.forward.speed = math.min(way.forward.speed, maxspeed) - end - - if maxspeed_backward and maxspeed_backward>0 then - way.backward.speed = math.min(way.backward.speed, maxspeed_backward) - elseif maxspeed and maxspeed>0 then - way.backward.speed = math.min(way.backward.speed, maxspeed) - end - + MaxSpeed.limit( way, maxspeed, maxspeed_forward, maxspeed_backward ) + return true end diff --git a/profiles/lib/maxspeed.lua b/profiles/lib/maxspeed.lua new file mode 100644 index 000000000..4600c0f0d --- /dev/null +++ b/profiles/lib/maxspeed.lua @@ -0,0 +1,17 @@ +local math = math + +module "MaxSpeed" + +function limit(way,max,maxf,maxb) + if maxf and maxf>0 then + way.forward.speed = math.min(way.forward.speed, maxf) + elseif max and max>0 then + way.forward.speed = math.min(way.forward.speed, max) + end + + if maxb and maxb>0 then + way.backward.speed = math.min(way.backward.speed, maxb) + elseif max and max>0 then + way.backward.speed = math.min(way.backward.speed, max) + end +end