Repository navigation
Clean up FerrySpeedCalculator, use exact edge distance #2528
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b38bf99
Use actual road_distance instead of estimated_distance, remove distan…
easbar b28bdf7
Remove handling of non-existent distance (never happens)
easbar 31d2800
Remove loop check, update
easbar d720d47
add comment
easbar 9bf4c61
update
easbar 0c55f1c
Pass minSpeed instead of speedFactor to ferry calculator
easbar cdeeadd
road_distance -> edge_distance
easbar 91f82f7
Remove beeline_distance tag again
easbar d7667ab
Merge branch 'master' into ferry_cleanup
easbar 198e594
Log distance for ferries with unrealistic duration
easbar c48d476
Merge branch 'master' into ferry_cleanup
easbar 82c16db
Derive speed from duration for ways, not edges
easbar b40bb31
Update comment
easbar f9bce32
Update comment again
easbar aad1685
Print only one warning per ferry way that is missing duration tag
easbar 2c669be
Only print warning for *long* ferries without duration tag
easbar 276676e
fix m->km
easbar ff52aa1
Ignore duration tag when it yields unrealistic low speed
easbar 34255c4
Log more unrealistic low speeds
easbar 8f2a24b
Log warning about missing duration tags less often (only for very lon…
easbar 761867a
minor
easbar f2128ac
Merge branch 'master' into ferry_cleanup
easbar b1d96c5
some more cleanup
easbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 28 additions & 57 deletions
85
core/src/main/java/com/graphhopper/routing/util/FerrySpeedCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,72 +1,43 @@ | ||
| package com.graphhopper.routing.util; | ||
|
|
||
| import com.graphhopper.reader.ReaderWay; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class FerrySpeedCalculator { | ||
| private final double unknownSpeed, minSpeed, maxSpeed; | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(FerrySpeedCalculator.class); | ||
| private final double speedFactor; | ||
| private final double unknownSpeed, longSpeed, shortSpeed, maxSpeed; | ||
|
|
||
| public FerrySpeedCalculator(double speedFactor, double maxSpeed, double longSpeed, double shortSpeed, double unknownSpeed) { | ||
| this.speedFactor = speedFactor; | ||
| this.unknownSpeed = unknownSpeed; | ||
| this.longSpeed = longSpeed; | ||
| this.shortSpeed = shortSpeed; | ||
| public FerrySpeedCalculator(double minSpeed, double maxSpeed, double unknownSpeed) { | ||
| this.minSpeed = minSpeed; | ||
| this.maxSpeed = maxSpeed; | ||
| this.unknownSpeed = unknownSpeed; | ||
| } | ||
|
|
||
| /** | ||
| * Special handling for ferry ways. | ||
| */ | ||
| public double getSpeed(ReaderWay way) { | ||
| // During the reader process we have converted the duration value into an artificial tag called "duration:seconds". | ||
| Long duration = way.getTag("duration:seconds", 0L); | ||
| // seconds to hours | ||
| double durationInHours = duration / 60d / 60d; | ||
| // Check if our graphhopper specific artificially created estimated_distance way tag is present | ||
| Number estimatedLength = way.getTag("estimated_distance", null); | ||
| if (durationInHours > 0) { | ||
| if (estimatedLength != null) { | ||
| double estimatedLengthInKm = estimatedLength.doubleValue() / 1000; | ||
| // If duration AND distance is available we can calculate the speed more precisely | ||
| // and set both speed to the same value. Factor 1.4 slower because of waiting time! | ||
| double calculatedTripSpeed = estimatedLengthInKm / durationInHours / 1.4; | ||
| // Plausibility check especially for the case of wrongly used PxM format with the intention to | ||
| // specify the duration in minutes, but actually using months | ||
| if (calculatedTripSpeed > 0.01d) { | ||
| if (calculatedTripSpeed > maxSpeed) { | ||
| return maxSpeed; | ||
| } | ||
| // If the speed is lower than the speed we can store, we have to set it to the minSpeed, but > 0 | ||
| if (Math.round(calculatedTripSpeed) < speedFactor / 2) { | ||
| return speedFactor / 2; | ||
| } | ||
| // todo: We currently face two problems related to ferry speeds: | ||
| // 1) We cannot account for waiting times for short ferries (when we do the ferry speed is slower than the slowest we can store) | ||
| // 2) When the ferry speed is larger than the maximum speed of the encoder (like 15km/h for foot) the | ||
| // ferry speed will be faster than what we can store. | ||
|
|
||
| return Math.round(calculatedTripSpeed); | ||
| } else { | ||
| long lastId = way.getNodes().isEmpty() ? -1 : way.getNodes().get(way.getNodes().size() - 1); | ||
| long firstId = way.getNodes().isEmpty() ? -1 : way.getNodes().get(0); | ||
| if (firstId != lastId) | ||
| LOGGER.warn("Unrealistic long duration ignored in way with way ID=" + way.getId() + " : Duration tag value=" | ||
| + way.getTag("duration") + " (=" + Math.round(duration / 60d) + " minutes)"); | ||
| durationInHours = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (durationInHours == 0) { | ||
| if (estimatedLength != null && estimatedLength.doubleValue() <= 300) | ||
| return speedFactor / 2; | ||
| // unknown speed -> put penalty on ferry transport | ||
| return unknownSpeed; | ||
| } else if (durationInHours > 1) { | ||
| // lengthy ferries should be faster than short trip ferry | ||
| return longSpeed; | ||
| // OSMReader adds the artificial 'speed_from_duration', 'duration:seconds' and 'way_distance' tags that we can | ||
| // use to set the ferry speed. Otherwise we need to use fallback values. | ||
| double speedInKmPerHour = way.getTag("speed_from_duration", Double.NaN); | ||
| if (!Double.isNaN(speedInKmPerHour)) { | ||
| // we reduce the speed to account for waiting time (we increase the duration by 40%) | ||
| double speedWithWaitingTime = speedInKmPerHour / 1.4; | ||
| return Math.round(Math.max(minSpeed, Math.min(speedWithWaitingTime, maxSpeed))); | ||
| } else { | ||
| return shortSpeed; | ||
| // we have no speed value to work with because there was no valid duration tag. | ||
| // we have to take a guess based on the distance. | ||
| double wayDistance = way.getTag("way_distance", Double.NaN); | ||
| if (Double.isNaN(wayDistance)) | ||
| // we don't know the distance of this way either. probably a broken way at the border of the map. | ||
| return unknownSpeed; | ||
| else if (wayDistance < 300) | ||
| // use the slowest possible speed for very short ferries | ||
| return minSpeed; | ||
| else { | ||
| // todo: distinguish speed based on the distance of the ferry, see #2532 | ||
| return unknownSpeed; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
estimatedLength == nullcan never happen.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand this now: Before #2448, #2457, cb38af6,
estimatedLength == nullcould happen when the first/last OSM node of a ferry way was missing:graphhopper/core/src/main/java/com/graphhopper/reader/osm/OSMReader.java
Lines 206 to 212 in f95397b
And the reason we have to consider the distance of the entire way is that 1) the duration tag refers to the duration of the entire way and 2) ferry ways sometimes intersect and are thus split into several edges for which we cannot directly use the duration tag.