Skip to content

PathDetails issue when placing the waypoint exactly on a PathDetail change - #1138

Closed
boldtrn wants to merge 15 commits into
graphhopper:masterfrom
boldtrn:pathdetails_roundtrip_issue
Closed

boldtrn wants to merge 15 commits into
graphhopper:masterfrom
boldtrn:pathdetails_roundtrip_issue

Conversation

@boldtrn

@boldtrn boldtrn commented Aug 17, 2017 •

Copy link
Copy Markdown
Member

I found the issue here. If we place a waypoint at exactly the position where the PathDetails change, the PathDetails are not merged. The index we passed to the PathDetail generation was too big by 1 index. So this was not necessarily a Roundtrip Issue.

@boldtrn boldtrn added the bug label Aug 17, 2017
@boldtrn boldtrn changed the title PathDetails issue with Roundtrips PathDetails issue when placing the waypoint exactly on a PathDetail change Aug 18, 2017
@boldtrn

boldtrn commented Aug 18, 2017 •

Copy link
Copy Markdown
Member Author

The trick here was again the +1 for via points. The issue is described in #1139. Since our merge method corrected most issues at via points by merging the last and first PathDetail of adjacent Paths, if they have the same value. Therefore, this issue only pops up if the last PathDetail of one Path has a different value than the first PathDetail of the next Path.

The test has a via point that is placed exactly between two edges, with different speed values.

if (!pathDetails.isEmpty() && !otherDetails.isEmpty()) {
PathDetail lastDetail = pathDetails.get(pathDetails.size() - 1);
// Add +1 for the via point
lastDetail.setLast(lastDetail.getLast()+1);

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.

Hmmh, this feels wrong and we should probably skip adding the via points opposed to my recent comment in #1139?

if (pathIndex + 1 < paths.size()) {
ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
newInstr.setViaCount(pathIndex + 1);
// Similar to the FinishInstruction, ViaInstructions have lenght=0

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could also enforce this during the Finish Instruction creation here

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 do not fully understand it. The ViaInstruction and FinishInstruction both should have one point, why is setting to an empty list necessary? And why do we still need this viaPosition/viaCount workaround?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As I understood it, they should have no points. If they have no points, we create intervals like [9,9]. If there is one point, we create an interval [9,10]. If we leave the point, we have to create a workaround for the simplification as well as for the serialization.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I just updated the PR to show an alternative approach. Now the Instructions contain one point, but we ignore these points during the simplification and serialization. I think it looks a bit cleaner like this.

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.

If there is one point, we create an interval [9,10]

But that is two points?

If we leave the point, we have to create a workaround for the simplification as well as for the serialization.

Why? Simplification should be disabled for <3 points.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

But that is two points?

No. The second point ref of the interval is equal to the first point of the next interval. Intervals are generated by [ START_REF, START_REF + POIN_COUNT ] for all Instructions,
expect the FinshInstruction where we artificially set the point count to 0 in the SimpleRouteSerializer. This PR enforces this behavior for all ViaInstructions and FinishInstructions.

Due to that they don't have their own point in the points array. That's why we have to create the workaround with lenght=0 in the PathSimplification.

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.

Hmmh. Let me summarize how it should be: every instruction has a first point and a last one, obviously the last point is the first point of the next instruction and the json would be interval:[first_ref,last_ref] and length=last_ref-first_ref. For Finish and ViaInstruction the result is length=0 (due to first==last) and pointlist.size==1 which makes still sense to me.

Everything which contradicts to this should be fixed e.g. the current ViaInstruction interval etc. and/or make the Instructions like the JSON output (and also PathDetails).

this.add(points, 0, points.size());
}

public void add(PointList points, int from, int to) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added this method to have a better performance compared to first copy the PointList and then add it.

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 good. Another idea would be removeLast via size--

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are right, might be the better solution. I did it like this, because at first I wanted to remove the first point of the PointList, which turned out to be the worse approach :). Once it was implemented doing the other war around worked easy. Should we switch to the removeLast approach?

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'm fine with the current state

@boldtrn

boldtrn commented Aug 24, 2017

Copy link
Copy Markdown
Member Author

I updated this PR to remove the duplicated point in the PointList

@karussell karussell 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.

Thanks!

this.add(points, 0, points.size());
}

public void add(PointList points, int from, int to) {

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 good. Another idea would be removeLast via size--

if (pathIndex + 1 < paths.size()) {
ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
newInstr.setViaCount(pathIndex + 1);
// Similar to the FinishInstruction, ViaInstructions have lenght=0

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 do not fully understand it. The ViaInstruction and FinishInstruction both should have one point, why is setting to an empty list necessary? And why do we still need this viaPosition/viaCount workaround?

if(instruction instanceof ViaInstruction)
size = 0;
else
size = instruction.getPoints().size();

@karussell karussell Aug 25, 2017 •

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.

Here I would expect 'getPoints.size-1' for both cases

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For the ViaInstruction it shouldn't matter as it should be only one point anyway. For the other case it should be the size IMHO. Could you explain why it should be size-1?

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.

Consistency. All path details currently work this way: you specify first and last and the length is last-first. Now imagine an instruction 0-1-2-3-4, where e.g. at 3 there is the via point, then the first instruction includes point 3, i.e. it is interval:[0,3] and therefor the length is 3-0=3, although the pointlist.size is 4.

@@ -99,8 +99,6 @@ public void doWork(PathWrapper altRsp, List<Path> paths, Translation tr) {
if (pathIndex + 1 < paths.size()) {
ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
newInstr.setViaCount(pathIndex + 1);

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.

can we remove setViaCount as getViaCount is unused?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

can we remove setViaCount as getViaCount is unused?

The viaCount is used in the getTurnDescription mehtod of the ViaInstruction.

@karussell karussell Aug 26, 2017 •

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.

Ah, ok. Makes sense. Is it possible to move this to the constructor and remove the getViaCount getter?

@boldtrn

boldtrn commented Sep 5, 2017

Copy link
Copy Markdown
Member Author

I just updated this PR. The latest push is only WIP, and it's not even building. I just commited to show you the current approach I am taking. Essentially I tried to remove everything point related and move it up to the InstructionList, so the InstructionList contains the whole PointList for a Path and can get Points for a single Instruction. The Instruction only stores references. This breaks the current API, but I think it's a nice improvement :).

Let me know what you think or if we should follow a different Path.

@karussell karussell 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.

Yes, I think this is more consistent in a few places and already align with the JSON API - thanks!

public FinishInstruction(PointAccess pointAccess, int node) {
this(pointAccess.getLatitude(node), pointAccess.getLongitude(node),
pointAccess.is3D() ? pointAccess.getElevation(node) : Double.NaN);
public FinishInstruction(int pointRef) {

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.

Is pointRef == last == first for the FinishInstruction?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes

this.name = name;
this.points = pl;
this.annotation = ia;
this.first = first;

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.

Can we make first final?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, as we change the first reference during the simplification. I would have liked that as well.

return AC.calcAzimuth(lat, lon, nextLat, nextLon);
public int getLast() {
if (last < first)
throw new IllegalStateException("last cannot be smaller than first");

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.

checkOne can be used or is unused?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍

this.points = points;
}

public PointList getPoints(Instruction instruction) {

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.

Maybe a different name or some javadocs are important to see the difference between this method and getPoints()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes certainly, it's still pretty raw and I am trying to get everything working, as it might involve bigger changes at some points in the code (not 100% sure yet).

fullWeight += path.getWeight();
if (enableInstructions) {
InstructionList il = path.calcInstructions(tr);
InstructionList il = path.calcInstructions(tr, origPoints);

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.

A better name now for origPoints might be origPointCount (?) as I initially thought it is the full PointList that must be injected into every InstructionList ...

if (o instanceof Instruction) {
first = ((Instruction) o).getFirst();
last = ((Instruction) o).getLast();
}

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.

here an if&else if might make sense with and else that throws an IllegalStateException as the list is of an unknown type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I actually want the Instruction and PathDetail implement the same interface, that all provide getLast, getFirst, getLength, etc. so we could solve most of this without any instanceof.

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.

For now I would have no problem with instanceof.

return viaPosition;
}

public void setViaCount(int count) {

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 can be removed now (?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We still need the viaPosition, the getter is just not used.

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.

Ups, I see :) ... we still could pass it via the constructor and make it final (?)

if (instruction.getLength() == 0)
// Copy does not include the last point
return this.points.copy(instruction.getFirst(), instruction.getLast() + 1);
return this.points.copy(instruction.getFirst(), instruction.getLast());

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.

We should always include the last point, even if we break backward compatibility.

}

/**
* Appends the insturctionList to this InstructionList, from instruction index 0 to toIndex

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.

minor typo: instructionList

foundInstruction = instructionIndex;
break;
}
}

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.

Is this logic change of introducing two for-loops necessary or to make it easier?

@boldtrn

boldtrn commented Sep 7, 2017

Copy link
Copy Markdown
Member Author

Yes, I think this is more consistent in a few places and already align with the JSON API - thanks!

Thanks for the feedback, I updated the PR. I got everything working except for the gtfs and web module so far. The web module depends on the gtfs module, so I couldn't build it without fixing the gtfs module.

@michaz, maybe you can give me a hint on how to fix the issues in the TripFromLabel class? Especially tricky for me was the method InstructionList getInstructions(Translation tr, List<Trip.Leg> legs).

Why is there a special handling for the last leg here? Because of the FinishInstruction?

Why do we use instructions.size()-2 here? I would have assumed that it would be instructions.size()-1, meaning the last instruction, but -2 means the instruction before the last instruction. Is this because the last instruction in this case would be a stop at a station, but we stay in the same train, so we just extend the points of the instruction before instead of creating a new instruction?

What happens here with the replace? Are the points of the arrivalInstruction the same as the points of the instruction that is replaced?

@michaz

michaz commented Sep 7, 2017

Copy link
Copy Markdown
Member

Why is there a special handling for the last leg here? Because of the FinishInstruction?

Yes.

Is this because the last instruction in this case would be a stop at a station, but we stay in the same train, so we just extend the points of the instruction before instead of creating a new instruction?

Yes.

What happens here with the replace? Are the points of the arrivalInstruction the same as the points of the instruction that is replaced?

No. When we are "inSameVehicleAsPrevious", the previous trip already gave an arrival instruction, but we don't want it, so we overwrite it. We only really arrive now.

Those are two trips which, according to the schedule, are technically two trips with two trip numbers, but the user does not know this and doesn't care, so we patch the instructions so that they look like one trip.

@boldtrn

boldtrn commented Sep 8, 2017

Copy link
Copy Markdown
Member Author

Thanks @michaz.

Is this because the last instruction in this case would be a stop at a station, but we stay in the same train, so we just extend the points of the instruction before instead of creating a new instruction?
Yes.

Why is the station instruction needed then? Is this working with the current code? Right now the serialization and deserialization (and probably a lot more code as well) assumes that instructions follow each other and are not mixed.

No. When we are "inSameVehicleAsPrevious", the previous trip already gave an arrival instruction, but we don't want it, so we overwrite it. We only really arrive now.

Would this be the same example as the train switches it's name example as above?

@michaz

michaz commented Sep 10, 2017 •

Copy link
Copy Markdown
Member

Is this working with the current code?

The test is: GraphHopperGtfsIT.testBlockTrips, and last time I checked, it looked fine in the JS GUI. (No automatic tests for that, though.) Maybe have a look? What is currently supposed to happen in the isSameVehicleAsPrevious-case is just this:

For these Legs:

Leg 1 A -> B
Leg 2 B -> C (inSameVehicleAsPrevious == true)

create not these instructions

Enter at A
Get off at B
Enter at B
Get off at C

but these

Enter at A
Get off at C

... with those instructions covering all the points of the ride. In the normal order. What you see in the code in the loop is (if I remember correctly) processing Leg 1 the normal way (without looking ahead), and when Leg 2 comes, change things to the expected result.

Missing luxury feature (please go for it if you have time!): Insert a "Stay in vehicle -- vehicle changes its headsign to C" instruction, located at B.

edges(path).map(edgeLabel -> edgeLabel.edgeIteratorState).collect(Collectors.toList()),
lineStringFromEdges(path),
edges(path).mapToDouble(edgeLabel -> edgeLabel.distance).sum(),
instructions.stream().collect(Collectors.toCollection(() -> new InstructionList(tr))),

@boldtrn boldtrn Sep 11, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Was there a reason for this, couldn't understand why we are not passing the instructions as created @michaz?

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.

You're right, that was left over from something. I'm merging this line (for a start :-) ) to master, thanks.

@boldtrn

boldtrn commented Sep 11, 2017

Copy link
Copy Markdown
Member Author

Thanks for the clarification @michaz, I understood it slightly different.

but these
Enter at A
Get off at C

This should be fine, and thanks for pointing me towards the test.

I finally got the GTFS tests to succeed. Can you live with the changes @michaz? Unfortunately, I wasn't able to do this with a lower impact, as I needed to change more than I would have expected, but maybe there would be a better way to do this?

The PR is still WIP BTW :).

@michaz

michaz commented Sep 25, 2017 •

Copy link
Copy Markdown
Member

I finally got the GTFS tests to succeed. Can you live with the changes @michaz? Unfortunately, I wasn't able to do this with a lower impact, as I needed to change more than I would have expected, but maybe there would be a better way to do this?

Looks good to me. I mean, yes, it's more code, but on the other hand, Instruction now contains what it claims to contain, namely indices, not points.

The super-cleanest thing of all would probably to have a builder which allows creating instructions with something like the previous syntax, but the new result. But not necessary, from my point.

@boldtrn

boldtrn commented Feb 6, 2018

Copy link
Copy Markdown
Member Author

This PR has been stale for quite a while now. The biggest problem when converting Instructions to contain only indices in contrast to a PointList, it becomes more expensive to work with the points of an instruction. The code became more complicated and there was a measurable slowdown. This might be improvable with updating the InstructionList code, but for now it's probably not worth the effort. In the future it would be still nice to see this feature merged into master.

@boldtrn boldtrn closed this Feb 6, 2018
@michaz

michaz commented Feb 12, 2018 •

Copy link
Copy Markdown
Member

Hmm, I thought it was ready to merge, wasn't it? I don't even remember, it was so long ago. But look at how much writing and thinking was put in by all of us.

I don't even remember what the problem was which it was solving, but if it was solving something substantial, like allowing a corner case which was previously not working in some surprising way, please re-consider merging it.

@boldtrn

boldtrn commented Feb 12, 2018

Copy link
Copy Markdown
Member Author

I don't even remember what the problem was which it was solving, but if it was solving something substantial, like allowing a corner case which was previously not working in some surprising way, please re-consider merging it.

Yes parts of this PR (the bug part) has been merged in a separate PR #1240.

The new feature part of this PR was to remove the PointList from the Instructions and only store start and end indexes, similar to the PathDetails. In the long term this could allow to remove the difference between PathDetails and Instructions and to declare instructions just a PathDetail. Overall I was also hoping to remove some of the overhead generated by splitting the geometry into the instructions and afterwards merging them again. The problem with this is that it didn't feel good in the code, it made many things more complicated, for which I created some helper methods to hide the more complicated parts. Overall it is harder to work with indexes than with PointLists as you need to update the global PointList.

The PR was "working" in a sense that the response looked correct. It was a bit slower than our current approach. Because the InstructionList used many rather "expensive" calls that required us to extract a part of the global PointList. I think this could have been improved and it might become even a bit faster than our current approach.

IMHO there was still some work to do to get this to a mergeable state and I am not sure if the required work justifies the benefit? If you want to I can reopen this PR, update it to the current master and do some more tests?

@michaz

michaz commented Feb 13, 2018 •

Copy link
Copy Markdown
Member

Ohhhhkay, thanks for explaining. That seems fine then. I was only looking at the headline, and that looked worrying to me. Like, when I use a seemingly-general data structure in a certain totally-allowed-looking way, it won't work. Because I'm notoriously the person who wants to use it in exactly that way. ;-)

@michaz

michaz commented Feb 13, 2018

Copy link
Copy Markdown
Member

(Or do we still have that?)

@boldtrn

boldtrn commented Feb 13, 2018

Copy link
Copy Markdown
Member Author

Like, when I use a seemingly-general data structure in a certain totally-allowed-looking way, it won't work.

Do you mean if we still see issues with PathDetails? AFAIK they work fine and I am not aware of any bugs related to them that would prevent anyone from using them or implementing own PathDetails.

@michaz

michaz commented Feb 13, 2018

Copy link
Copy Markdown
Member

Okay cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants