Repository navigation
Read Via Way Restrictions - #2673
lukasalexanderweber wants to merge 3 commits into
Conversation
7495108 to
c84deb9
Compare
c84deb9 to
847d790
Compare
847d790 to
14567d5
Compare
|
@easbar @karussell is there anything left for me to do to merge this PR? |
|
I'll take a look soon. |
|
does the via-ways restriction works?i found only reader code changed. |
There was a problem hiding this comment.
I did some changes in master today after I read the turn restriction wiki again: #2680. I also added a new branch that adds the ViaType you added here (without MULTI_WAY and UNSUPPORTED) on top of this: fddb7f6.
In your previous branch there were lots of changes to OSMReader etc. so I find it a bit hard to see where we are going here. I'm also not sure how much time on our side will be available to work on #446 #3155 currently. Can you give a rough sketch (no code) of what you think needs to be done to implement #446 #3155 (for the single via-way case) so we can get an estimate how much time will be required to finish this?
| String vehicleTypeRestricted, List<String> vehicleTypesExcept) { | ||
| OSMTurnRestriction.RestrictionType type = OSMTurnRestriction.RestrictionType.get(restrictionType); | ||
| if (type != OSMTurnRestriction.RestrictionType.UNSUPPORTED) { | ||
| ArrayList<Long> viaIDs = new ArrayList<>(); |
There was a problem hiding this comment.
Basically we are using hppc LongArrayList everywhere in GH core. It probably does not matter much in this specific case, but I also do not see a great reason to not use it here.
|
|
||
| for (ReaderRelation.Member member : relation.getMembers()) { | ||
| if (ReaderElement.Type.WAY == member.getType() && "to".equals(member.getRole())) | ||
| // ONE TO Way (a "no_exit" restriction can have ONE OR MORE TO ways) |
There was a problem hiding this comment.
You are describing some of the different cases in these comments, but I am missing the code that handles the 'unexpected' ones. OSM can contain all kinds of strange data. But this wasn't ideal IMO already in master and not really related to this PR. I think we should just do if (from) {...} else if (to) { ... } else if (via) { ... } else { warning }.
| // ONE OR MORE VIA WAYS | ||
| else if (ReaderElement.Type.WAY == member.getType() && "via".equals(member.getRole())) { | ||
| if (viaType == OSMTurnRestriction.ViaType.UNSUPPORTED) { | ||
| viaIDs.add(member.getRef()); | ||
| viaType = OSMTurnRestriction.ViaType.WAY; | ||
| } else if (viaType == OSMTurnRestriction.ViaType.WAY || viaType == OSMTurnRestriction.ViaType.MULTI_WAY) { | ||
| viaIDs.add(member.getRef()); | ||
| viaType = OSMTurnRestriction.ViaType.MULTI_WAY; | ||
| } | ||
| } |
There was a problem hiding this comment.
As far as I understood we should first focus on the case with a single via way, so I would leave this out here. Or do you think the multi-way case isn't much more complicated than the way-case and we should do both at once?
| viaOSMIds.add(4L); | ||
|
|
||
| List<OSMTurnRestriction> osmRel = OSMReader.createTurnRestrictions(rel); | ||
| assertEquals(osmRel.get(0).getViaType(), OSMTurnRestriction.ViaType.MULTI_WAY); |
There was a problem hiding this comment.
JUnit fun fact: The signature of assertEquals is assertEquals(expected, actual) not assertEquals(actual, expected).
There was a problem hiding this comment.
(not anymore. I think they swapped this from 4 to 5)
There was a problem hiding this comment.
No they just swapped the position of the message parameter (from front to back)'
|
Ok I'll give you a short outline about my previous and future work on this topic. At first, I did all the development in one branch/PR. It was asked to split the development in multiple PRs: #2656 (comment) I then decided to create three PRs:
I will now parallel work on
I've got two working approaches on simple via way turn restrictions. The first one is Restriction ID other working examples are
Both approaches have limitations regarding "self-contained" restrictions as but these can be solved with the approach I plan for multiple via ways. Also both approaches manipulate the navigation instructions (when passing zero distance edges). I could also imagine a meeting where we can introduce and I can give you a bit more detail about the development if you are interested. |
Why is this needed and how is it related to this issue?
Ok, this sketch in
Can you explain the difference between the two approaches? Or do you think they are too hard to explain via GitHub? What are the limitations you mentioned? What are 'self-contained' restrictions?
Ok, so you haven't really decided what the final solution will be, also for single via-ways? Shall we postpone the discussion until it is more clear? |
OSMReader and WaySegmentParser are tightly coupled. I needed a long time to figure out whats happening there. I really think that the refactoring would benefit the understanding of the dataflow happening while preparing graphhopper from an osm file. I put much effort in it and my implementation of via ways wouln't work without it. The biggest reason is that I need the ability to add edges to the graph while creating the via way restrictions.
Till now there are two Edge Cases I encountered: Combinations of Via Node and Via Way restrictions for one Node/Way; and the thing I called 'self-contained' restrictions.
as can be seen in the image, these share the same edges and nodes (lets call them overlapping restrictions, self-contained is a really bad term for that xD)
Yes they occour and yes both
In the "self-contained aka overlapping" image, both U-Turns should be prohibited but they are still taken (I know why but there is more to implement for these to work). And then the navigation instruction manipulation I mentioned, but I think for someone who is deeper into that this might be easy to handle.
Since my task at work is to implement multi via ways as well, I would work on with the second apprach if this is fine for you.
Basically I create a detour starting at the start node of the from way. This allows other restrictions to "jump on" the "restriction graph" (see node |
|
@easbar with the information that you know have, what do you think would be the next steps? I could think of:
parallel I will continue working on multi via ways and you can discuss internally if you think a meeting could be a good idea. |
So to implement turn restrictions with via-ways we need to add artificial nodes and/or edges to the graph, like in the sketch in
Ok, so this is rather an advanced example where the c-b-d-e-f restriction has two via-ways and there are overlapping restrictions nearby? What are the directions of the green restrictions? For example is it forbidden to turn from edge 4 to 6, or 6 to 4, or both? And why did you pick such an advanced example? And do you think it general enough to test your approach? I mean do you think that if we have a working solution for this specific example does it mean we can cover all possible restrictions? I'm probably behind here, but the first example I'd like to understand and implement is a via-way restriction that can either be a 'no' or 'only' restriction with ways from=a, via=b, to=c where there can be arbitrary adjacent edges at nodes 0, 1, 2 and 3. At least that would be my definition of the general case we need to deal with when we want to support via-way restrictions (with a single way). Do you already know how to do this? On the conceptual level the next step could then be generalizing this to multiple via-ways and/or overlapping restrictions?
Yes, of course. I am by no means trying to tell you what to work on. If you are willing to share your final result it could also be an option that you just finish it the way you want and once it is complete we at GH can think about if and how to include it in our code base. Of course I also see how it could be advantageous to do this together from the beginning, if there will be enough bandwidth to discuss this as we proceed.
So far I mostly want to understand what needs to be done on a conceptual level. I am much less concerned about actually implementing this once it is clear what we are trying to do. I don't think it is worth to update the present PR, because I already added the ViaType on top of my recent changes in a separate branch here: fddb7f6 and just adding it doesn't really help much for implementing the feature we want. Regarding the OSMReader refactoring, I cannot comment much, because I don't know what needs to be done yet. For example you said:
At which point of the OSM reading (we read the file twice) do you think shall we add the turn restrictions? So far I was assuming we can do this simply after the graph (without turn restrictions) is finished. So I don't really understand why you think you don't have the ability to add edges while creating the via way restrictions. |
|
Would the following work (for the single-via way case, without any overlapping restrictions)? And maybe this is one of the two approaches you meant? And what about the same in the 'only'-case? The new thing here are these artificial edges. For the path post-processing like instructions etc. Does this make sense? |
yes
yes
In Regierungsbezirk Karlsruhe, ~2/3s of the via way restrictions are overlapping (as one junction often has 3-4 restrictions like the examples)
Yes we can do that (I already read "nested restrictions" as a term)
The directions are from left to right. The restrictions are
At first I conceptualized and implemented a simple via way restriction. Since this worked kind of well I wanted to go further and solve the overlapping/multi way cases. I always orientate myself a bit at Project-OSRM/osrm-backend#5907 and try to transfer the approaches to a node based graph, thats where I've got the example.
Probably not, it was just a first orientation how I could approach more complex via way restrictions
I've made a sketch of the three approaches I've tested:
The main strategie is the same at all approaches:
I think that working on this together would be the more sustainable way. I'm not even sure if the approach cannot be generalized anyway. Maybe it turns out that the first of the three approaches is the best. I mostly rejected it because it made too much damage at the navigation instructions. EDIT your sollution solves this case, I'm really happy for such insights.
Yes man, this is so clever. It removes all the struggle of artificial nodes and the navigation instruction issues coming with them. Another sketch:
THIS! I never had problems with the artificial edges only with the nodes
Couln't we simply switch the restrictions to
Yes we'll do it after the graph (without turn restrictions) is finished. From an implementation side, however, we would need to bind the turn restriction creation to the WaySegmentParser since it's got the addEdge function etc. in it (and we cannot simply access it in another class due to its complex builder stuff). Additionally, the WaySegmenthandler hadles 1. the OSM parsing, 2. node handling, 3. way handling, 4. relation handling (by getting those functions from OSMReader!?) and on top of it now the Restriction Handling would also be placed. For me this just wasn't the best way it could be done so I worked out a way to restructure the functionality (I didn't change any function! I just restructured it for clearer reponsibilities and thats what I want to present you in another PR). So even after the new insights about the conception of Turn Restrictions I'm confinced that refactoring should be considered. |
|
Even better, with this approach multi via ways are easy to model (still with the assumption that there are no overlaps). We only need to prevent the "crossing" between artificial and original ways between two via ways. I have a real example with 2 via ways (3082392) and transfered it to a unit test with 3 via ways, both work as expected. The implementation is here |
Thanks, this is very useful and shows how important this feature is and also that overlapping restrictions must be expected. Just for reference here are some screenshots for the first junction:
... and of course we currently violate these restrictions
Ok, so these aren't rare or something. I think it still helps to get simplify first and think about how to do implement the feature when there are no overlaps, but ultimately we should also handle overlapping restrictions.
You mean nested=overlapping?
Ok, I thought so but wasn't sure.
👍 Looking at that discussion it doesn't sound like everything becomes simple just when using an edge-based graph either :)
Thanks, that made it clear.
👍 Ok let's try this.
❤️
Yes, good idea. I mean this: is symmetric, so we can make it so the artificial edge is always the one we do not want in the location index and make sure artificial edges are ignored there. This is easier than excluding all edges for which there is an artificial counter part or something.
Nice, happy to hear!
Yes, that makes sense. But do we really need to restrict b via 2 to c'? But maybe it doesn't even matter. And we should prevent b via 2 to b' and c via 3 to c' I think. Anyway, shall we first focus on single via-ways, then see if there are actual problems for the overlapping cases and then either try to fix the overlaps or generalize to multi via-ways? I mean if you feel like exploring these advanced cases please go ahead, and maybe I'll think about how to integrate this with the current code. I'll have a look at yours of course.
Ok, let me explain the 'idea' behind the OSMReader and related code: The first code we call when we read an OSM file is
Yes, it does initiate the reading of the OSM file. We could move this out and feed a stream of OSM elements to WaySegmentParser instead, but so far I saw no reason to do this. It would be needed if we wanted to build the graph from OSM that we get via http instead of reading a local file, but that would be entirely unrelated to turn restrictions of course. And yes, it also does handle nodes and ways, but there is no way around this, because its purpose is to find the junctions and segments of the road network. I did not mention the relations so far, but to create edges of the routing graph we typically want to know if the corresponding OSM ways are referenced by certain relations like bike networks etc., which is the reason we also read them in WaySegmentParser. The other reason is that we don't want to read the OSM file again after running the WaySegmentParser.
The WaySegmentParser provides callbacks that are used by OSMReader, yes. This is how it can be used for different purposes and at the same time provide an abstraction that does all the segment splitting stuff.
Ok, now we are getting back to the present issue :) I absolutely see how WaySegmentParser and OSMReader can seem very complex (and in parts they are), so adding more things to them can seem like and possibly is the wrong thing to do. I hope I am not missing any details, but so far I think WaySegmentParser is/will be entirely unrelated to turn restrictions. And it should be possible to move the turn restriction handling into a separate class where it also can be tested independently from the other things that are going on during the OSM reading. Basically we should be able to build the entire graph without any turn restrictions, only save a list of turn relations and a mapping between the way IDs and graph edge Ids they use, and then add all the turn restrictions afterwards. For example we can easily add the artificial turn restriction edges after the normal graph building is complete. Especially with the overlapping restrictions this might be needed anyway, because we need to add the restrictions/artificial edges at a point in time where we know about all the restrictions already? I'm not sure yet if it would be possible to simply add them one after the other as if they were independent/non-overlapping. All that being said, I'm not strictly against changing anything about WaySegmentParser and OSMReader and there absolutely is room for improvement. Especially OSMReader is still a somewhat inflexible huge monolith of many different things. The builder in WaySegmentParser is also not ideal, which is something I wanted to fix before, but either forgot about it or ran into some problem, I can have a look again. So feel free to open a PR for your refactorings that we can discuss. Shall we do this first? I can try to re-arrange the code in the way that I think would be appropriate for the new turn restriction handling in the meantime. |
|
As I'm on training the whole next week I just wan't to give a quick response, while not answering everything in detail just yet.
This sounds good, I was just enthusiastic that multi via ways become rather simple :)
Thanks, I've worked out most of what you said, but it probably took longer than it should
I want to give you a short outline on how the dataflow is regarding the implementation of single via way restrictions in https://github.com/lukasalexanderweber/graphhopper/tree/single_via_way_restrictions: OSMReader.java will trigger two parsings of OSMParser.java. The first parse is to preproccess ways (WayPreprocessor.java) and Relations (RelationPreprocessor.java). During Relation Preprocessing, via way turn restriction information are stored (WayRestriction.java in OSMTurnRestrictionData.java). In the second parse, first the nodes are handled (NodeHandler.java). Then the ways are handled (WayHandler.java), storing the ReaderWay.java objects of all via ways (to create artificial edges later on). In the onStart function of the RelationHandler.java, the turn restrictions are build (OSMTurnRestrictionBuilder.java), creating the artificial edges and the according via node restrictions (NodeRestriction.java). Those are then processed if the respective OSMTurnRestriction.java is processed again in the RelationHandler.java. Please don't hestitate to write me on any questions. |
|
Thanks for the update.
Yes, this is really nice.
Ok thanks I'll have a look. |
|
I started to think a lot more about this issue, took a look at https://github.com/lukasalexanderweber/graphhopper/tree/single_via_way_restrictions and ended up trying to combine everything we know so far in another implementation: #2689 The main differences are probably the following:
I'm sure there is still room for lots of improvements and if you also like the new implementation maybe we can transfer some stuff from yours. Also do not hesitate to open the PR for the OSMReader if you think it is an improvement in itself. For this issue I did not need to change much in the OSMReader code though (see #2689 for more details). |
|
Thanks for reaching out and all the work you've put into this! ❤️🔥
Yeah since I needed to add the edges, and the functionality was already there, I tried to make use of OSMReader#addEdge. I definitely agree that this can be decoupled from the reading.
Yeah until then I had assumed that all (via) ways have only two tower nodes (for me those are the edges that really have a 1:1 mapping to the OSM Ways). Example Restrictions with more than two tower nodes are
Yes, this works perfectly fine. I tested with
I'll have a closer look at your implementation, i'm curious to see how you did it :)
I think I will, but probably after the work on restriction handling is finished so that there are not so much merge conflicts. |












#2670 with a new branch.
To be able to process more complex turn restrictions (with one ore more via ways instead of one via node), the functionality to read the restrictions are updated.