Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wheelchair routing, elevator fix #4

Merged
merged 4 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions exe/backend/src/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "osr/routing/profiles/bike.h"
#include "osr/routing/profiles/car.h"
#include "osr/routing/profiles/foot.h"
#include "osr/routing/profiles/wheelchair.h"
#include "osr/routing/route.h"

using namespace net;
Expand Down Expand Up @@ -111,11 +110,11 @@ struct http_server::impl {
auto p = std::optional<path>{};
switch (profile) {
case search_profile::kFoot:
p = route(w_, l_, get_dijkstra<foot>(), from, to, max,
p = route(w_, l_, get_dijkstra<foot<false>>(), from, to, max,
direction::kForward);
break;
case search_profile::kWheelchair:
p = route(w_, l_, get_dijkstra<wheelchair>(), from, to, max,
p = route(w_, l_, get_dijkstra<foot<true>>(), from, to, max,
direction::kForward);
break;
case search_profile::kBike:
Expand All @@ -140,7 +139,7 @@ struct http_server::impl {
return json::value{
{"type", "Feature"},
{"properties",
{{"level", to_float(s.level_)},
{{"level", to_float(s.from_level_)},
{"way",
s.way_ == way_idx_t::invalid()
? 0U
Expand All @@ -162,7 +161,11 @@ struct http_server::impl {
{waypoints[3].as_double(), waypoints[2].as_double()});
auto levels = hash_set<level_t>{};
l_.find(min, max, [&](way_idx_t const x) {
levels.emplace(w_.way_properties_[x].get_level());
auto const p = w_.way_properties_[x];
levels.emplace(p.from_level());
if (p.from_level() != p.to_level()) {
levels.emplace(p.to_level());
}
});
auto levels_sorted =
utl::to_vec(levels, [](level_t const l) { return to_float(l); });
Expand All @@ -186,14 +189,34 @@ struct http_server::impl {

auto gj = geojson_writer{.w_ = w_};
l_.find(min, max, [&](way_idx_t const w) {
if (level == level_t::invalid() ||
w_.way_properties_[w].get_level() == level) {
if (level == level_t::invalid()) {
gj.write_way(w);
return;
}

auto const way_prop = w_.way_properties_[w];
if (way_prop.is_elevator()) {
auto const n = w_.way_nodes_[w][0];
auto const np = w_.node_properties_[n];
if (np.is_multi_level()) {
auto has_level = false;
for_each_set_bit(
foot<true>::get_elevator_multi_levels(w_, n),
[&](auto&& bit) { has_level |= (level == level_t{bit}); });
if (has_level) {
gj.write_way(w);
return;
}
}
}

if (way_prop.from_level() == level || way_prop.to_level() == level) {
gj.write_way(w);
return;
}
});

auto const s = get_dijkstra<car>();
cb(json_response(req, gj.finish(s)));
cb(json_response(req, gj.finish(get_dijkstra<foot<true>>())));
}

void handle_static(web_server::http_req_t&& req,
Expand Down
54 changes: 31 additions & 23 deletions include/osr/extract/tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "osmium/osm/object.hpp"

#include "osr/types.h"
#include "osr/ways.h"

namespace osr {

Expand All @@ -30,7 +29,10 @@ struct tags {
case cista::hash("oneway:bicycle"):
not_oneway_bike_ = t.value() == "no"sv;
break;
case cista::hash("motor_vehicle"): motor_vehicle_ = t.value(); break;
case cista::hash("motor_vehicle"):
motor_vehicle_ = t.value();
is_destination_ |= motor_vehicle_ == "destination";
break;
case cista::hash("foot"): foot_ = t.value(); break;
case cista::hash("bicycle"): bicycle_ = t.value(); break;
case cista::hash("highway"):
Expand All @@ -41,20 +43,24 @@ struct tags {
break;
case cista::hash("level"): {
auto s = utl::cstr{t.value()};
auto from_lvl = 0.0F, to_lvl = 0.0F;
utl::parse_arg(s, from_lvl);
if (s) {
++s;
utl::parse_arg(s, to_lvl);
while (s) {
auto l = 0.0F;
utl::parse_arg(s, l);
auto const lvl = to_level(std::clamp(l, kMinLevel, kMaxLevel));
level_bits_ |= (1U << to_idx(lvl));
if (s) {
++s;
}
}
level_ = to_level(std::clamp(from_lvl, kMinLevel, kMaxLevel));
level_to_ = to_level(std::clamp(to_lvl, kMinLevel, kMaxLevel));
break;
}
case cista::hash("entrance"): is_entrance_ = true; break;
case cista::hash("sidewalk"): sidewalk_ = t.value(); break;
case cista::hash("cycleway"): cycleway_ = t.value(); break;
case cista::hash("motorcar"): motorcar_ = t.value(); break;
case cista::hash("motorcar"):
motorcar_ = t.value();
is_destination_ |= motorcar_ == "destination";
break;
case cista::hash("barrier"): barrier_ = t.value(); break;
case cista::hash("public_transport"):
is_platform_ |=
Expand All @@ -64,9 +70,11 @@ struct tags {
switch (cista::hash(std::string_view{t.value()})) {
case cista::hash("private"):
case cista::hash("delivery"):
case cista::hash("destination"): [[fallthrough]];
case cista::hash("no"): vehicle_ = override::kBlacklist; break;

case cista::hash("destination"):
is_destination_ = true;
[[fallthrough]];
case cista::hash("permissive"): [[fallthrough]];
case cista::hash("yes"): vehicle_ = override::kWhitelist; break;
}
Expand All @@ -78,17 +86,17 @@ struct tags {
case cista::hash("forestry"):
case cista::hash("emergency"):
case cista::hash("psv"):
case cista::hash("customers"):
case cista::hash("private"): [[fallthrough]];
case cista::hash("delivery"): access_ = override::kBlacklist; break;

case cista::hash("designated"):
case cista::hash("dismount"):
case cista::hash("customers"):
case cista::hash("permissive"): [[fallthrough]];
case cista::hash("yes"): access_ = override::kWhitelist; break;
}
break;
case cista::hash("max_speed"): max_speed_ = t.value(); break;
case cista::hash("maxspeed"): max_speed_ = t.value(); break;
}
}
}
Expand Down Expand Up @@ -128,6 +136,7 @@ struct tags {
std::string_view max_speed_;

// https://wiki.openstreetmap.org/wiki/Key:vehicle
bool is_destination_{false};
override vehicle_{override::kNone};

// https://wiki.openstreetmap.org/wiki/Key:access
Expand All @@ -146,8 +155,7 @@ struct tags {
bool is_entrance_{false};

// https://wiki.openstreetmap.org/wiki/Key:level
level_t level_{to_level(0.0F)};
level_t level_to_{to_level(0.0F)}; // for elevators
level_bits_t level_bits_{0U};
};

template <typename T>
Expand All @@ -167,9 +175,8 @@ struct foot_profile {

switch (cista::hash(t.foot_)) {
case cista::hash("no"):
case cista::hash("private"):
case cista::hash("use_sidepath"): [[fallthrough]];
case cista::hash("destination"): return override::kBlacklist;
case cista::hash("private"): [[fallthrough]];
case cista::hash("use_sidepath"): return override::kBlacklist;

case cista::hash("yes"):
case cista::hash("permissive"): [[fallthrough]];
Expand All @@ -189,6 +196,9 @@ struct foot_profile {

static bool default_access(tags const& t, osm_obj_type const type) {
if (type == osm_obj_type::kWay) {
if (t.is_elevator_) {
return true;
}
switch (cista::hash(t.highway_)) {
case cista::hash("primary"):
case cista::hash("primary_link"):
Expand Down Expand Up @@ -228,9 +238,8 @@ struct bike_profile {
switch (cista::hash(t.bicycle_)) {
case cista::hash("no"):
case cista::hash("private"):
case cista::hash("optional_sidepath"):
case cista::hash("use_sidepath"): [[fallthrough]];
case cista::hash("destination"): return override::kBlacklist;
case cista::hash("optional_sidepath"): [[fallthrough]];
case cista::hash("use_sidepath"): return override::kBlacklist;

case cista::hash("yes"):
case cista::hash("permissive"): [[fallthrough]];
Expand Down Expand Up @@ -300,8 +309,7 @@ struct car_profile {
case cista::hash("agricultural;forestry"):
case cista::hash("permit"):
case cista::hash("customers"):
case cista::hash("delivery"):
case cista::hash("destination"): [[fallthrough]];
case cista::hash("delivery"): [[fallthrough]];
case cista::hash("no"): return override::kBlacklist;

case cista::hash("designated"):
Expand Down
53 changes: 35 additions & 18 deletions include/osr/geojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "utl/pipes/transform.h"
#include "utl/pipes/vec.h"

#include "osr/routing/profiles/foot.h"
#include "osr/ways.h"

namespace osr {
Expand Down Expand Up @@ -44,15 +45,19 @@ struct geojson_writer {
{"properties",
{{"type", "edge"},
{"osm_way_id", to_idx(w_.way_osm_idx_[i])},
{"internal_id", to_idx(i)},
{"distance", dist},
{"car", p.is_car_accessible()},
{"bike", p.is_bike_accessible()},
{"foot", p.is_foot_accessible()},
{"is_destination", p.is_destination()},
{"oneway_car", p.is_oneway_car()},
{"oneway_bike", p.is_oneway_bike()},
{"max_speed", p.max_speed_km_per_h()},
{"level", to_float(level_t{p.level_})},
{"is_elevator", p.is_elevator()}}},
{"level", to_float(level_t{p.from_level()})},
{"to_level", to_float(level_t{p.to_level()})},
{"is_elevator", p.is_elevator()},
{"is_steps", p.is_steps()}}},
{"geometry", to_line_string(std::initializer_list<geo::latlng>{
w_.get_node_pos(from), w_.get_node_pos(to)})}});
}
Expand All @@ -62,14 +67,18 @@ struct geojson_writer {
{"properties",
{{"type", "geometry"},
{"osm_way_id", to_idx(w_.way_osm_idx_[i])},
{"access_car", p.is_car_accessible()},
{"access_bike", p.is_bike_accessible()},
{"access_foot", p.is_foot_accessible()},
{"internal_id", to_idx(i)},
{"car", p.is_car_accessible()},
{"bike", p.is_bike_accessible()},
{"foot", p.is_foot_accessible()},
{"is_destination", p.is_destination()},
{"oneway_car", p.is_oneway_car()},
{"oneway_bike", p.is_oneway_bike()},
{"max_speed", p.max_speed_km_per_h()},
{"level", to_float(level_t{p.level_})},
{"is_elevator", p.is_elevator()}}},
{"from_level", to_float(level_t{p.from_level()})},
{"to_level", to_float(level_t{p.to_level()})},
{"is_elevator", p.is_elevator()},
{"is_steps", p.is_steps()}}},
{"geometry", to_line_string(w_.way_polylines_[i])}});

nodes_.insert(begin(nodes), end(nodes));
Expand All @@ -81,23 +90,31 @@ struct geojson_writer {
auto const p = w_.node_properties_[n];

auto ss = std::stringstream{};
Dijkstra::profile_t::resolve_all(w_, n, [&](auto const n) {
auto const cost = s.get_cost(n);
if (cost != kInfeasible) {
ss << "{";
n.print(ss, w_);
ss << ", " << cost << "}\n";
}
});
Dijkstra::profile_t::resolve_all(w_, n, level_t::invalid(),
[&](auto const n) {
auto const cost = s.get_cost(n);
if (cost != kInfeasible) {
ss << "{";
n.print(ss, w_);
ss << ", " << cost << "}\n";
}
});

auto levels = std::stringstream{};
foot<true>::for_each_elevator_level(
w_, n, [&](auto&& l) { levels << to_float(level_t{l}) << " "; });

auto properties = boost::json::object{
{"osm_node_id", to_idx(w_.node_to_osm_[n])},
{"internal_id", to_idx(n)},
{"car", p.is_car_accessible()},
{"bike", p.is_bike_accessible()},
{"foot", p.is_walk_accessible()},
{"is_restricted", w_.node_is_restricted_[n]},
{"is_entrance", static_cast<bool>(p.is_entrance_)},
{"is_elevator", static_cast<bool>(p.is_elevator_)},
{"is_entrance", p.is_entrance()},
{"is_elevator", p.is_elevator()},
{"multi_level", p.is_multi_level()},
{"levels", levels.str()},
{"ways", fmt::format("{}", w_.node_ways_[n] |
std::views::transform([&](auto&& w) {
return w_.way_osm_idx_[w];
Expand All @@ -110,7 +127,7 @@ struct geojson_writer {
w_.way_osm_idx_[w_.node_ways_[n][r.from_]],
w_.way_osm_idx_[w_.node_ways_[n][r.to_]]};
}))},
{"labels", ss.str()}};
{"label", ss.str().empty() ? "unreachable" : ss.str()}};
features_.emplace_back(boost::json::value{
{"type", "Feature"},
{"properties", properties},
Expand Down
25 changes: 16 additions & 9 deletions include/osr/lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ struct location {
struct node_candidate {
bool valid() const { return node_ != node_idx_t::invalid(); }

level_t lvl_{level_t::invalid()};
direction way_dir_{direction::kForward};
node_idx_t node_{node_idx_t::invalid()};
double dist_to_node_{0.0};
cost_t cost_{0U};
cost_t offroad_cost_{0U};
std::vector<geo::latlng> path_{};
};

Expand Down Expand Up @@ -95,14 +98,13 @@ struct lookup {
find(query.pos_, [&](way_idx_t const way) {
auto const p = ways_.way_properties_[way];
auto d = distance_to_way(query.pos_, ways_.way_polylines_[way]);
if (d.dist_to_way_ < Profile::kMaxMatchDistance &&
(query.lvl_ == level_t::invalid() || p.get_level() == query.lvl_)) {
if (d.dist_to_way_ < Profile::kMaxMatchDistance) {
auto& wc = way_candidates.emplace_back(std::move(d));
wc.way_ = way;
wc.left_ =
find_next_node<Profile>(wc, query, direction::kBackward, reverse);
wc.right_ =
find_next_node<Profile>(wc, query, direction::kForward, reverse);
wc.left_ = find_next_node<Profile>(wc, query, direction::kBackward,
query.lvl_, reverse);
wc.right_ = find_next_node<Profile>(wc, query, direction::kForward,
query.lvl_, reverse);
}
});
utl::sort(way_candidates);
Expand All @@ -113,6 +115,7 @@ struct lookup {
node_candidate find_next_node(way_candidate const& wc,
location const& query,
direction const dir,
level_t const lvl,
bool const reverse) const {
auto const way_prop = ways_.way_properties_[wc.way_];
auto const edge_dir = reverse ? opposite(dir) : dir;
Expand All @@ -121,9 +124,13 @@ struct lookup {
return node_candidate{};
}

auto const off_road_length = geo::distance(query.pos_, wc.best_);
auto c = node_candidate{.dist_to_node_ = off_road_length,
.cost_ = Profile::way_cost(way_prop, edge_dir, 0U),
auto const offroad_cost =
Profile::way_cost(way_prop, edge_dir, wc.dist_to_way_);
auto c = node_candidate{.lvl_ = lvl,
.way_dir_ = dir,
.dist_to_node_ = wc.dist_to_way_,
.cost_ = offroad_cost,
.offroad_cost_ = offroad_cost,
.path_ = {query.pos_, wc.best_}};
auto const polyline = ways_.way_polylines_[wc.way_];
auto const osm_nodes = ways_.way_osm_nodes_[wc.way_];
Expand Down
Loading