Skip to content

Commit

Permalink
compact path -> FeatureCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Jul 19, 2024
1 parent e8370e0 commit 19ec0c4
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions exe/backend/src/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,42 +153,36 @@ struct http_server::impl {
location const to,
cost_t const max,
direction const dir) {
auto p = route(w_, l_, get_dijkstra<Profile>(), from, to, max, dir);

auto const p = route(w_, l_, get_dijkstra<Profile>(), from, to, max, dir);
if (!p.has_value()) {
cb(json_response(req, "could not find a valid path",
http::status::not_found));
return;
}

auto to_feature = [&](const path::segment& s) {
return json::object{
{"type", "Feature"},
{
"properties",
{{"level", to_float(s.from_level_)},
{"osm_way_id", s.way_ == way_idx_t::invalid()
? 0U
: to_idx(w_.way_osm_idx_[s.way_])},
{"cost", s.cost_},
{"distance", s.dist_},
{"from_node", s.from_node_properties_},
{"to_node", s.to_node_properties_}},
},
{"geometry", to_line_string(s.polyline_)}};
};

auto feature_collection_meta_data =
json::value{{"duration", p->cost_}, {"distance", p->dist_}};
auto features = utl::all(p->segments_) | utl::transform(to_feature) |
utl::emplace_back_to<json::array>();

auto feature_collection =
json::object{{"type", "FeatureCollection"},
{"metadata", feature_collection_meta_data},
{"features", features}};

cb(json_response(req, json::serialize(feature_collection)));
cb(json_response(
req,
json::serialize(json::object{
{"type", "FeatureCollection"},
{"metadata", {{"duration", p->cost_}, {"distance", p->dist_}}},
{"features", utl::all(p->segments_) |
utl::transform([&](const path::segment& s) {
return json::object{
{"type", "Feature"},
{
"properties",
{{"level", to_float(s.from_level_)},
{"osm_way_id",
s.way_ == way_idx_t::invalid()
? 0U
: to_idx(w_.way_osm_idx_[s.way_])},
{"cost", s.cost_},
{"distance", s.dist_},
{"from_node", s.from_node_properties_},
{"to_node", s.to_node_properties_}},
},
{"geometry", to_line_string(s.polyline_)}};
}) |
utl::emplace_back_to<json::array>()}})));
}

void handle_levels(web_server::http_req_t const& req,
Expand Down

0 comments on commit 19ec0c4

Please sign in to comment.