Skip to content

[GSoC] Pedestrian area routing: Detect areas during parsing - #6127

Merged
chrstnbwnkl merged 29 commits into
valhalla:masterfrom
pacoalbacetec:pedestrian-routing
Jul 9, 2026
Merged

chrstnbwnkl merged 29 commits into
valhalla:masterfrom
pacoalbacetec:pedestrian-routing

Conversation

@pacoalbacetec

@pacoalbacetec pacoalbacetec commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

Background

First step toward area routing is to detect pedestrian areas (highway=pedestrian and area=yes) during tile building, both as ways and multipolygon relations.

Changes I have made

  • lua/graph.lua: Ways and multipolygon relations of pedestrian areas were discarded, I have added support for them.
  • valhalla/mjolnir/osmway.h: Add an area_ bit to mark pedestrian ways
  • valhalla/mjolnir/osmdata.h: I have built an OSMArea struct and area_relations multimap indexed by relation id so we can see all members of the same relation
  • src/mjolnir/graphbuilder.cc: skip area ways so it won't generate edges for them, which makes all the tests pass
  • src/mjolnir/pbfgraphparser.cc: tag handler for simple ways + relation function for multipolygon areas, which collects outer/inner ways of the areas
  • src/mjolnir/osmdata.cc: serialize/deserialize area_relations to a binary temp file between build stages
  • scripts/valhalla_build_config: add mjolnir.pedestrian_areas boolean option (default false) so existing behavior is unchanged
  • test/gurka/test_area_routing.cc: gurka test verifying that perimeter edges are not generated and that routing correctly goes around the area (at the moment, in the future, is expected to generate edges across the area). And 2 more tests, checking that the config option is or not marking the pedestrian areas ways with the area_bit

Results

Tested on Murcia, Spain extract:

  • All 42 multipolygon pedestrian areas are detected, and I have checked manually the member way counts are correct
  • The areas that are simply mapped as ways are also detected and marked
  • Gurka test passes: area perimeter generates no edges, route goes around correctly
  • With pedestrian_areas: false (default), no behavior change for existing users

Comment thread lua/graph.lua Resolved
Comment thread lua/graph.lua
Comment on lines +2468 to +2471
if (kv["type"] == "multipolygon" and kv["highway"] == "pedestrian" and kv["area"] == "yes") then
return 0, kv
end

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function rels_proc only preserved a few relations and everything else was discarded. I have made pedestrian multipolygon and their tags survive the lua and reach for the pbfgraphparser

Comment on lines +379 to +381
tag_handlers_["pedestrian_area"] = [this]() {
way_.set_area(tag_.second == "true" ? true : false);
};

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the tag handler that marks, in the osmway.h the area_ bit, indicating we are dealing with a pedestrian area. Which only happens, as indicated in the review comment in graph.lua, for ways tagged with both highway=pedestrian and area=yes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since pedestrian_area is a tag chosen by you, and you never set it to anything but "true" in graph.lua, you can just set it to true here to avoid confusion:

Suggested change
tag_handlers_["pedestrian_area"] = [this]() {
way_.set_area(tag_.second == "true" ? true : false);
};
tag_handlers_["pedestrian_area"] = [this]() {
if (pedestrian_areas_) {
way_.set_area(true);
}
};

uint64_t from_way_id = 0;
bool isRestriction = false, isTypeRestriction = false, hasRestriction = false;
bool isRoad = false, isRoute = false, isBicycle = false, isConnectivity = false;
bool isMultipolygon = false, isPedestrian = false, isArea = false;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flags to identify pedestrian multipolygon areas, following a pretty similar pattern as the existing for bike relations

Comment on lines +3864 to +3865
} else if (tag.second == "multipolygon") {
isMultipolygon = true;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extends the type tag handler to also catch mutipolygon

Comment on lines +3992 to +3999
} else if (tag.first == "highway") {
if (tag.second == "pedestrian") {
isPedestrian = true;
}
} else if (tag.first == "area") {
if (tag.second == "yes") {
isArea = true;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tags weren't read anywhere, we need them to identify the areas

Comment thread src/mjolnir/pbfgraphparser.cc Outdated
Comment on lines +4283 to +4295
} else if (isMultipolygon && isPedestrian && isArea) {
for (const auto& member : members) {
OSMArea area;
if (member.role == "outer" && member.member_type == osmium::item_type::way) {
area.is_outer = true;
area.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area));
} else if (member.role == "inner" && member.member_type == osmium::item_type::way) {
area.is_outer = false;
area.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area));
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relations we are looking for are none of the other types and they might be multipolygons, pedestrians and areas. This is where we iterate over the relation members to separate outer and inner ways and fill area_relations

Comment on lines +42 to +47
// OSM area data (stored within OSMData)
struct OSMArea {
uint64_t way_id;
bool is_outer;
};

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct represents a member way of a pedestrian multipolygon area, where we store its id and whether it is an outer or inner ring

Comment thread valhalla/mjolnir/osmdata.h Outdated
using ViaSet = std::unordered_set<uint64_t>;
using AccessRestrictionsMultiMap = std::unordered_multimap<uint64_t, OSMAccessRestriction>;
using BikeMultiMap = std::unordered_multimap<uint64_t, OSMBike>;
using AreaMultiMap = std::unordered_multimap<uint64_t, OSMArea>;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A multimap indexed by relation id, so all member ways of one area can be retrieved together

Comment thread valhalla/mjolnir/osmway.h
Comment on lines +1773 to +1788
/**
* Sets the area flag.
* @param area Is this an area?
*/
void set_area(const bool area) {
area_ = area;
}

/**
* Get the area flag.
* @return Returns area flag.
*/
bool area() const {
return area_;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getter and setter for area_bit

Comment thread valhalla/mjolnir/osmway.h
Comment on lines +2727 to +2728
uint16_t area_ : 1;
uint16_t spare2_ : 1;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save 1 bit to identify pedestrian areas

Comment on lines +184 to +189
// If the way is part of an area, make sure it does not generate edges
if (way.area()) {
current_way_node_index = last_way_node_index + 1;
continue;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skip area ways so we don't generate edges, because we don't want to route along the perimeter of an area. We still store them but keep them out of the graph

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the config option flag check, is not necessary, because the area_bit works as the flag itself

@pacoalbacetec
pacoalbacetec marked this pull request as ready for review June 7, 2026 22:51

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gurka test file for area routing. The only test currently checks that
the perimeter of a pedestrian area does not generate edges (even though
we are collecting its ways), and that routing around the area works as
expected (currently is unimplemented so it goes around) .

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's fine to open a new test file for this.

don't forget that you can make use of #6136. you can test things for real on temp files, gurka graphs aren't very useful at this point.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realise that #6136 was already merged. I'll make use of it then!

Comment on lines +4286 to +4298
} else if (isMultipolygon && isPedestrian && isArea && pedestrian_areas_) {
for (const auto& member : members) {
OSMAreaMember area_member;
if (member.role == "outer" && member.member_type == osmium::item_type::way) {
area_member.is_outer = true;
area_member.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
} else if (member.role == "inner" && member.member_type == osmium::item_type::way) {
area_member.is_outer = false;
area_member.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relations we are looking for are none of the other types and they might be multipolygons, pedestrians and areas. This is where we iterate over the relation members to separate outer and inner ways and fill area_relations. Disabled as default with pedestrian_areas_ config flag

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the new config option to valhalla_build_config with its default
value (false) and help text

Comment thread src/mjolnir/osmdata.cc
Comment on lines +62 to +70
struct AreaRelation {
uint64_t relation_id;
OSMAreaMember area_member;
AreaRelation() : relation_id(0), area_member(OSMAreaMember()) {
}
AreaRelation(uint64_t r, const OSMAreaMember& m) : relation_id(r), area_member(m) {
}
};

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper struct to serialize/deserialize the area relations multimap, follows a similar pattern as the existing BikeRelation struct.

@chrstnbwnkl
chrstnbwnkl self-requested a review July 2, 2026 09:50
chrstnbwnkl
chrstnbwnkl previously approved these changes Jul 2, 2026

@chrstnbwnkl chrstnbwnkl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good so far! I've made some suggestions on simplifying parts of the code a little, but none of those change the logic in any way.

The next step will probably be parsing the ways to get the ones we care about. Think about the parsing order again: if it's possible to parse ways before relations without breaking anything, that could save us a second pass over the ways.

Comment on lines +379 to +381
tag_handlers_["pedestrian_area"] = [this]() {
way_.set_area(tag_.second == "true" ? true : false);
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since pedestrian_area is a tag chosen by you, and you never set it to anything but "true" in graph.lua, you can just set it to true here to avoid confusion:

Suggested change
tag_handlers_["pedestrian_area"] = [this]() {
way_.set_area(tag_.second == "true" ? true : false);
};
tag_handlers_["pedestrian_area"] = [this]() {
if (pedestrian_areas_) {
way_.set_area(true);
}
};

Comment on lines +3995 to +3998
} else if (tag.first == "highway") {
if (tag.second == "pedestrian") {
isPedestrian = true;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (tag.first == "highway") {
if (tag.second == "pedestrian") {
isPedestrian = true;
}
} else if (tag.first == "highway" && tag.second == "pedestrian") {
isPedestrian = true;

We don't currently care about any other highway tag combo, so this can be simplified

Comment on lines +3999 to +4002
} else if (tag.first == "area") {
if (tag.second == "yes") {
isArea = true;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (tag.first == "area") {
if (tag.second == "yes") {
isArea = true;
}
} else if (tag.first == "area" && tag.second == "yes") {
isArea = true;

same here, can be simplified

Comment on lines +4288 to +4297
OSMAreaMember area_member;
if (member.role == "outer" && member.member_type == osmium::item_type::way) {
area_member.is_outer = true;
area_member.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
} else if (member.role == "inner" && member.member_type == osmium::item_type::way) {
area_member.is_outer = false;
area_member.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be simplified a little:

Suggested change
OSMAreaMember area_member;
if (member.role == "outer" && member.member_type == osmium::item_type::way) {
area_member.is_outer = true;
area_member.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
} else if (member.role == "inner" && member.member_type == osmium::item_type::way) {
area_member.is_outer = false;
area_member.way_id = member.member_id;
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
}
if ((member.role == "outer" || member.role == "inner") &&
member.member_type == osmium::item_type::way) {
OSMAreaMember area_member{.way_id = member.member_id, .is_outer = member.role == "outer"};
osmdata_.area_relations.insert(AreaMultiMap::value_type(osmid, area_member));
}

@chrstnbwnkl

Copy link
Copy Markdown
Member

Oops looks like clang-tidy is complaining

@chrstnbwnkl

Copy link
Copy Markdown
Member

Some of the tests operating on real world tiles (Utrecht, Paris, etc.) are failing. I assume this PR includes area ways with highway=pedestrian and turns those into normal edges, routing on the area perimeter. But it's worth checking that that is what's happening 😄

@chrstnbwnkl chrstnbwnkl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's one more test failing; in test/graphbuilder we construct an OSMData instance manually, so you need to brace initialize area_relations just like the other ones:

diff --git a/test/graphbuilder.cc b/test/graphbuilder.cc
index d8da9da17..8e0388b16 100644
--- a/test/graphbuilder.cc
+++ b/test/graphbuilder.cc
@@ -62,6 +62,7 @@ TEST(GraphBuilder, TestConstructEdges) {
                    {}, /* via_set */
                    {}, /* access_restrictions */
                    {}, /* bike_relations */
+                   {}, /* area_relations */
                    {}, /* way_ref */
                    {}, /* way_ref_rev */
                    {}, /* node_names */

@chrstnbwnkl
chrstnbwnkl enabled auto-merge (squash) July 8, 2026 13:33
@chrstnbwnkl
chrstnbwnkl merged commit 0a537b0 into valhalla:master Jul 9, 2026
14 of 16 checks passed
@pacoalbacetec pacoalbacetec changed the title [GSOC] Pedestrian area routing: Detect areas during parsing [GSoC] Pedestrian area routing: Detect areas during parsing Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants