| 1 |
package uk.me.parabola.imgfmt.app.net; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.nio.file.Files; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.List; |
| 8 |
import uk.me.parabola.imgfmt.app.Coord; |
| 9 |
import uk.me.parabola.log.Logger; |
| 10 |
import uk.me.parabola.util.EnhancedProperties; |
| 11 |
|
| 12 |
public class RoundaboutCheck { |
| 13 |
private final Logger log = Logger.getLogger(RoundaboutCheck.class); |
| 14 |
private boolean checkRoundaboutLoops = false; |
| 15 |
private boolean checkRoundaboutDirections = false; |
| 16 |
private boolean checkRoundaboutOverlaps = false; |
| 17 |
private boolean checkRoundaboutJunctions = false; |
| 18 |
private boolean checkRoundaboutFlares = false; |
| 19 |
private boolean checkRoundabouts = false; |
| 20 |
private int maxFlareLength = 200; |
| 21 |
private int maxFlareLengthMultiple = 3; |
| 22 |
private int flareSeparationOverride = 5; |
| 23 |
private int maxFlareToSeparationMultiple = 7; |
| 24 |
private int maxFlareAngle = 90; |
| 25 |
private int maxFlareBearing = 90; |
| 26 |
private int maxEntryAngle = 145; |
| 27 |
private boolean discardBothFlaresExtend = true; |
| 28 |
private boolean discardMultipleFlares = true; |
| 29 |
private boolean discardDifferentFlareNames = true; |
| 30 |
private boolean discardDifferentAccesses = true; |
| 31 |
|
| 32 |
private RouteNode nodeToCheck; |
| 33 |
private Coord coord; |
| 34 |
|
| 35 |
private List<RouteNode> roundaboutNodes = new ArrayList<>(); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private static void printOptionHelpMsg(String option) { |
| 42 |
if(!"help".equals(option)) |
| 43 |
System.err.println("Unknown report-roundabout-issues option '" + option + "'"); |
| 44 |
|
| 45 |
System.err.println("Report roundabout issues options are:"); |
| 46 |
System.err.println(" all apply all the checks"); |
| 47 |
System.err.println(" loop check that each roundabouts is formed from a single loop with no"); |
| 48 |
System.err.println(" forks or gaps"); |
| 49 |
System.err.println(" direction check the direction of travel around the roundabout"); |
| 50 |
System.err.println(" overlaps check that highways do not overlap the roundabout"); |
| 51 |
System.err.println(" junctions check that no more than one connecting highway joins at each node"); |
| 52 |
System.err.println(" flares check that roundabout flare roads are one-way, are in the right"); |
| 53 |
System.err.println(" direction, and don't extend beyond the flare"); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public void config(EnhancedProperties props) { |
| 60 |
if (props.getProperty("check-roundabouts", false)) { |
| 61 |
checkRoundaboutLoops = RouteNode.isWarningLogged(); |
| 62 |
checkRoundaboutDirections = checkRoundaboutLoops; |
| 63 |
checkRoundaboutJunctions = checkRoundaboutLoops; |
| 64 |
log.error("The check-roundabouts option is deprecated - use report-roundabout-issues."); |
| 65 |
} |
| 66 |
if (props.getProperty("check-roundabout-flares", false)) { |
| 67 |
checkRoundaboutFlares = RouteNode.isWarningLogged(); |
| 68 |
log.error("The check-roundabout-flares option is deprecated - use report-roundabout-issues."); |
| 69 |
} |
| 70 |
String mflr = props.getProperty("max-flare-length-ratio"); |
| 71 |
if (mflr != null) { |
| 72 |
maxFlareToSeparationMultiple = props.getProperty("max-flare-length-ratio", maxFlareToSeparationMultiple); |
| 73 |
log.error("The max-flare-length-ratio option is deprecated - use roundabout-flare-rules-config."); |
| 74 |
} |
| 75 |
String checkRoundaboutsOption = props.getProperty("report-roundabout-issues"); |
| 76 |
if (checkRoundaboutsOption != null) { |
| 77 |
if ("".equals(checkRoundaboutsOption)) { |
| 78 |
checkRoundaboutLoops = true; |
| 79 |
checkRoundaboutDirections = true; |
| 80 |
checkRoundaboutOverlaps = true; |
| 81 |
checkRoundaboutJunctions = true; |
| 82 |
checkRoundaboutFlares = true; |
| 83 |
} |
| 84 |
for (String option : checkRoundaboutsOption.split(",")) { |
| 85 |
if ("all".equals(option)) { |
| 86 |
checkRoundaboutLoops = true; |
| 87 |
checkRoundaboutDirections = true; |
| 88 |
checkRoundaboutOverlaps = true; |
| 89 |
checkRoundaboutJunctions = true; |
| 90 |
checkRoundaboutFlares = true; |
| 91 |
} else if ("loop".equals(option)) { |
| 92 |
checkRoundaboutLoops = true; |
| 93 |
} else if ("direction".equals(option)) { |
| 94 |
checkRoundaboutDirections = true; |
| 95 |
} else if ("overlaps".equals(option)) { |
| 96 |
checkRoundaboutOverlaps = true; |
| 97 |
} else if ("junctions".equals(option)) { |
| 98 |
checkRoundaboutJunctions = true; |
| 99 |
} else if ("flares".equals(option)) { |
| 100 |
checkRoundaboutFlares = true; |
| 101 |
} else if (option.isEmpty()) { |
| 102 |
|
| 103 |
} else { |
| 104 |
printOptionHelpMsg(option); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
checkRoundabouts = checkRoundaboutLoops || checkRoundaboutOverlaps || checkRoundaboutJunctions || checkRoundaboutFlares; |
| 109 |
String configFilename = props.getProperty("roundabout-flare-rules-config", ""); |
| 110 |
if (!configFilename.isEmpty()) { |
| 111 |
File file = new File(configFilename); |
| 112 |
try { |
| 113 |
List<String> rules = Files.readAllLines(file.toPath()); |
| 114 |
for (int i = 0; i < rules.size(); i++) { |
| 115 |
String rule = rules.get(i); |
| 116 |
int hashPos = rule.indexOf('#'); |
| 117 |
if (hashPos >= 0) |
| 118 |
rule = rule.substring(0, hashPos); |
| 119 |
rule = rule.replaceAll("\\s+", ""); |
| 120 |
if (!rule.isEmpty()) { |
| 121 |
String[] ruleParts = rule.split(":"); |
| 122 |
if (ruleParts.length == 2) { |
| 123 |
try { |
| 124 |
switch (ruleParts[0]) { |
| 125 |
case "max-flare-length": |
| 126 |
maxFlareLength = Integer.decode(ruleParts[1]); |
| 127 |
break; |
| 128 |
|
| 129 |
case "max-flare-length-multiple": |
| 130 |
maxFlareLengthMultiple = Integer.decode(ruleParts[1]); |
| 131 |
break; |
| 132 |
|
| 133 |
case "flare-separation-override": |
| 134 |
flareSeparationOverride = Integer.decode(ruleParts[1]); |
| 135 |
break; |
| 136 |
|
| 137 |
case "max-flare-to-separation-multiple": |
| 138 |
maxFlareToSeparationMultiple = Integer.decode(ruleParts[1]); |
| 139 |
break; |
| 140 |
|
| 141 |
case "max-flare-angle": |
| 142 |
maxFlareAngle = Integer.decode(ruleParts[1]); |
| 143 |
break; |
| 144 |
|
| 145 |
case "max-flare-bearing": |
| 146 |
maxFlareBearing = Integer.decode(ruleParts[1]); |
| 147 |
break; |
| 148 |
|
| 149 |
case "max-entry-angle": |
| 150 |
maxEntryAngle = Integer.decode(ruleParts[1]); |
| 151 |
break; |
| 152 |
|
| 153 |
case "discard-both-flares-extend": |
| 154 |
discardBothFlaresExtend = Boolean.getBoolean(ruleParts[1]); |
| 155 |
break; |
| 156 |
|
| 157 |
case "discard-multiple-flares": |
| 158 |
discardMultipleFlares = Boolean.getBoolean(ruleParts[1]); |
| 159 |
break; |
| 160 |
|
| 161 |
case "discard-different-flare-names": |
| 162 |
discardDifferentFlareNames = Boolean.getBoolean(ruleParts[1]); |
| 163 |
break; |
| 164 |
|
| 165 |
case "discard-different-accesses": |
| 166 |
discardDifferentAccesses = Boolean.getBoolean(ruleParts[1]); |
| 167 |
break; |
| 168 |
|
| 169 |
default: |
| 170 |
log.error("Unrecognised roundabout flare rule", ruleParts[0], "in", configFilename); |
| 171 |
} |
| 172 |
} |
| 173 |
catch (Exception ex) { |
| 174 |
log.error("Invalid value", ruleParts[1], "for roundabout flare rule", ruleParts[0], "in", configFilename); |
| 175 |
} |
| 176 |
} |
| 177 |
else { |
| 178 |
log.error("Invalid roundabout flare rule", rule, "in", configFilename); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} catch (IOException ex) { |
| 183 |
log.error("Error reading roundabout flare rules file", configFilename); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
public void check(RouteNode routeNode) { |
| 189 |
if (checkRoundabouts) { |
| 190 |
nodeToCheck = routeNode; |
| 191 |
coord = nodeToCheck.getCoord(); |
| 192 |
roundaboutNodes.clear(); |
| 193 |
List<RouteArc> roundaboutArcs = new ArrayList<>(); |
| 194 |
List<RouteArc> nonRoundaboutArcs = new ArrayList<>(); |
| 195 |
for(RouteArc ra : nodeToCheck.getArcs()) { |
| 196 |
if (ra.isDirect()) { |
| 197 |
|
| 198 |
RoadDef rd = ra.getRoadDef(); |
| 199 |
if (!rd.isSynthesised()) { |
| 200 |
if (rd.isRoundabout()) |
| 201 |
roundaboutArcs.add(ra); |
| 202 |
else |
| 203 |
nonRoundaboutArcs.add(ra); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
if (!roundaboutArcs.isEmpty()) { |
| 208 |
|
| 209 |
if(log.isDebugEnabled()) |
| 210 |
log.debug("Roundabout node", coord.toOSMURL()); |
| 211 |
roundaboutNodes.add(nodeToCheck); |
| 212 |
boolean roundaboutComplete = true; |
| 213 |
for (RouteArc ra : roundaboutArcs) { |
| 214 |
if (ra.isForward()) { |
| 215 |
for (RouteArc ra1 : nonRoundaboutArcs) { |
| 216 |
if ((ra1.getDirectHeading() == ra.getDirectHeading()) && (ra1.getInitialHeading() == ra.getInitialHeading()) && (ra1.getFinalHeading() == ra.getFinalHeading()) && (ra1.getLengthInMeter() == ra.getLengthInMeter())) { |
| 217 |
|
| 218 |
nonRoundaboutArcs.remove(ra1); |
| 219 |
if(checkRoundaboutOverlaps && !ra.getRoadDef().messagePreviouslyIssued("overlaps roundabout")) |
| 220 |
log.diagnostic("Highway " + ra1.getRoadDef() + " overlaps roundabout " + ra.getRoadDef() + " at " + coord.toOSMURL()); |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
RouteNode rn = ra.getDest(); |
| 225 |
while (!roundaboutNodes.contains(rn)) { |
| 226 |
roundaboutNodes.add(rn); |
| 227 |
if(log.isDebugEnabled()) |
| 228 |
log.debug("Roundabout node", rn.getCoord().toOSMURL()); |
| 229 |
RouteNode nrn = null; |
| 230 |
for (RouteArc nra : rn.getArcs()) { |
| 231 |
if (nra.isDirect() && nra.isForward()) { |
| 232 |
RoadDef nrd = nra.getRoadDef(); |
| 233 |
if (nrd.isRoundabout() && !nrd.isSynthesised()) |
| 234 |
nrn = nra.getDest(); |
| 235 |
} |
| 236 |
} |
| 237 |
rn = nrn; |
| 238 |
if (rn == null) { |
| 239 |
roundaboutComplete = false; |
| 240 |
log.info("Incomplete roundabout", coord.toOSMURL()); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
Coord roundaboutCentre = getRoundaboutCentre(); |
| 247 |
|
| 248 |
if (nonRoundaboutArcs.size() > 1) { |
| 249 |
int countNonRoundaboutRoads = 0; |
| 250 |
int countNonRoundaboutOtherHighways = 0; |
| 251 |
int countHighwaysInsideRoundabout = 0; |
| 252 |
for (RouteArc ra : nonRoundaboutArcs) { |
| 253 |
RoadDef rd = ra.getRoadDef(); |
| 254 |
|
| 255 |
byte access = rd.getAccess(); |
| 256 |
if (access != 0 && (access != AccessTagsAndBits.FOOT)) { |
| 257 |
if (roundaboutComplete && goesInsideRoundabout(ra, nodeToCheck, roundaboutCentre)) { |
| 258 |
countHighwaysInsideRoundabout++; |
| 259 |
|
| 260 |
for (RouteArc ra2 : nonRoundaboutArcs) |
| 261 |
ra2.getRoadDef().doFlareCheck(false); |
| 262 |
} |
| 263 |
else { |
| 264 |
if ((access & AccessTagsAndBits.CAR) != 0) |
| 265 |
countNonRoundaboutRoads++; |
| 266 |
else if ((access & (AccessTagsAndBits.BIKE | AccessTagsAndBits.BUS | AccessTagsAndBits.TAXI | AccessTagsAndBits.TRUCK)) != 0) |
| 267 |
countNonRoundaboutOtherHighways++; |
| 268 |
} |
| 269 |
} |
| 270 |
else |
| 271 |
rd.doFlareCheck(false); |
| 272 |
} |
| 273 |
|
| 274 |
RouteArc roundaboutArc = roundaboutArcs.get(0); |
| 275 |
if (checkRoundaboutLoops) { |
| 276 |
if (nodeToCheck.getArcs().size() > 1 && roundaboutArcs.size() == 1) |
| 277 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + (roundaboutArc.isForward() ? " starts at " : " ends at ") + coord.toOSMURL()); |
| 278 |
if(roundaboutArcs.size() > 2) { |
| 279 |
for(RouteArc fa : roundaboutArcs) { |
| 280 |
if(fa.isForward()) { |
| 281 |
RoadDef rd = fa.getRoadDef(); |
| 282 |
for(RouteArc fb : roundaboutArcs) { |
| 283 |
if(fb != fa) { |
| 284 |
if(fa.getPointsHash() == fb.getPointsHash() && |
| 285 |
((fb.isForward() && fb.getDest() == fa.getDest()) || |
| 286 |
(!fb.isForward() && fb.getSource() == fa.getDest()))) { |
| 287 |
if(!rd.messagePreviouslyIssued("roundabout forks/overlaps")) { |
| 288 |
log.diagnostic("Roundabout " + rd + " overlaps " + fb.getRoadDef() + " at " + coord.toOSMURL()); |
| 289 |
} |
| 290 |
} |
| 291 |
else if (fb.isForward() |
| 292 |
&& !rd.messagePreviouslyIssued("roundabout forks/overlaps")) { |
| 293 |
log.diagnostic("Roundabout " + rd + " forks at " + coord.toOSMURL()); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if (checkRoundaboutJunctions) { |
| 303 |
if (countNonRoundaboutRoads > 1) { |
| 304 |
if (!nodeToCheck.getRestrictions().isEmpty()) |
| 305 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " with restriction is connected to more than one road at " + coord.toOSMURL()); |
| 306 |
else |
| 307 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " is connected to more than one road at " + coord.toOSMURL()); |
| 308 |
} else if (countNonRoundaboutRoads == 1) { |
| 309 |
if (countNonRoundaboutOtherHighways > 0) { |
| 310 |
if (countHighwaysInsideRoundabout > 0) |
| 311 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " is connected to a road, " + countNonRoundaboutOtherHighways + " other highway(s) and " + countHighwaysInsideRoundabout + " highways inside the roundabout at " + coord.toOSMURL()); |
| 312 |
else |
| 313 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " is connected to a road and " + countNonRoundaboutOtherHighways + " other highway(s) at " + coord.toOSMURL()); |
| 314 |
} |
| 315 |
else if (countHighwaysInsideRoundabout > 0) |
| 316 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " is connected to a road and " + countHighwaysInsideRoundabout + " highway(s) inside the roundabout at " + coord.toOSMURL()); |
| 317 |
} else if (countNonRoundaboutOtherHighways > 0) { |
| 318 |
if (countHighwaysInsideRoundabout > 0) |
| 319 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " is connected to " + countNonRoundaboutOtherHighways+ " highway(s) and " + countHighwaysInsideRoundabout + " inside the roundabout at " + coord.toOSMURL()); |
| 320 |
else if (countNonRoundaboutOtherHighways > 1) |
| 321 |
log.diagnostic("Roundabout " + roundaboutArc.getRoadDef() + " is connected to " + countNonRoundaboutOtherHighways + " highways at " + coord.toOSMURL()); |
| 322 |
} else if (countHighwaysInsideRoundabout > 1) |
| 323 |
log.diagnostic("Roundabout " +roundaboutArc.getRoadDef() + " is connected to " + countHighwaysInsideRoundabout + " highways inside the roundabout at " + coord.toOSMURL()); |
| 324 |
} |
| 325 |
} |
| 326 |
if (roundaboutComplete && checkRoundaboutFlares) |
| 327 |
checkRoundaboutFlares(roundaboutCentre); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public void checkRoundaboutFlares(Coord centre) { |
| 339 |
for(RouteArc r : nodeToCheck.getArcs()) { |
| 340 |
|
| 341 |
if(!r.isForward() || !r.isDirect() || !r.getRoadDef().isRoundabout() || r.getRoadDef().isSynthesised()) |
| 342 |
continue; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
RouteNode nextExitNode = null; |
| 347 |
RouteNode nextButOneExitNode = null; |
| 348 |
RouteNode precedingExitNode = null; |
| 349 |
for (RouteNode rn : roundaboutNodes) { |
| 350 |
if (rn == nodeToCheck) |
| 351 |
continue; |
| 352 |
|
| 353 |
for (RouteArc ra : rn.getArcs()) { |
| 354 |
if (!ra.isDirect()) |
| 355 |
continue; |
| 356 |
|
| 357 |
RoadDef rd = ra.getRoadDef(); |
| 358 |
if (!rd.isSynthesised() && !rd.isRoundabout()) { |
| 359 |
byte access = rd.getAccess(); |
| 360 |
if (access != 0 && (access != AccessTagsAndBits.FOOT)) { |
| 361 |
if (goesInsideRoundabout(ra, rn, centre)) { |
| 362 |
for (RouteArc ra2 : rn.getArcs()) |
| 363 |
ra2.getRoadDef().doFlareCheck(false); |
| 364 |
} |
| 365 |
if (nextExitNode == null) |
| 366 |
nextExitNode = rn; |
| 367 |
else { |
| 368 |
if (nextButOneExitNode == null) |
| 369 |
nextButOneExitNode = rn; |
| 370 |
precedingExitNode = rn; |
| 371 |
} |
| 372 |
} |
| 373 |
else |
| 374 |
rd.doFlareCheck(false); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if(nextExitNode == null) |
| 380 |
continue; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
for(RouteArc fa : nodeToCheck.getArcs()) { |
| 386 |
if(!fa.isDirect()) |
| 387 |
continue; |
| 388 |
|
| 389 |
RoadDef rda = fa.getRoadDef(); |
| 390 |
if (rda.isSynthesised() || !rda.doFlareCheck()) |
| 391 |
continue; |
| 392 |
|
| 393 |
for(RouteArc fb : nextExitNode.getArcs()) { |
| 394 |
if(!fb.isDirect() || (fa == fb)) |
| 395 |
continue; |
| 396 |
|
| 397 |
RoadDef rdb = fb.getRoadDef(); |
| 398 |
if(rdb.isSynthesised() || !rdb.doFlareCheck()) |
| 399 |
continue; |
| 400 |
|
| 401 |
if(fa.getDest() == fb.getDest()) { |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if(roundaboutSegmentLength(nodeToCheck, nextExitNode) >= |
| 412 |
roundaboutSegmentLength(nextExitNode, nodeToCheck)) |
| 413 |
continue; |
| 414 |
|
| 415 |
|
| 416 |
boolean hasRoad = false; |
| 417 |
for (RouteArc ra2 : fa.getDest().getArcs()) { |
| 418 |
if (ra2.isDirect() && (ra2 != fa) && (ra2 != fb)) { |
| 419 |
RoadDef rd2 = ra2.getRoadDef(); |
| 420 |
byte access = rd2.getAccess(); |
| 421 |
if (access != 0 && (access != AccessTagsAndBits.FOOT)) { |
| 422 |
hasRoad = true; |
| 423 |
break; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
if (!hasRoad) { |
| 428 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "no ongoing road"); |
| 429 |
continue; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
if (maxFlareLength > 0) { |
| 434 |
float faLength = fa.getLengthInMeter(); |
| 435 |
float fbLength = fb.getLengthInMeter(); |
| 436 |
if (faLength > maxFlareLength || fbLength > maxFlareLength) { |
| 437 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where the flares are", faLength, "and", fbLength, "m"); |
| 438 |
continue; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
if (maxFlareLengthMultiple > 0) { |
| 444 |
float faLength = fa.getLengthInMeter(); |
| 445 |
float fbLength = fb.getLengthInMeter(); |
| 446 |
if (faLength > maxFlareLengthMultiple * fbLength || fbLength > maxFlareLengthMultiple * faLength) { |
| 447 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where one flare is", String.format("%.1f", Math.max(faLength, fbLength)/Math.min(faLength, fbLength)), "times longer than the other"); |
| 448 |
continue; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
if (maxFlareBearing > 0) { |
| 454 |
double flareAngle = fa.getReverseArc().getInitialHeading() - fa.getDest().getCoord().bearingTo(centre); |
| 455 |
if (flareAngle < -180) |
| 456 |
flareAngle += 360; |
| 457 |
if (flareAngle > 180) |
| 458 |
flareAngle -= 360; |
| 459 |
if (Math.abs(flareAngle) > maxFlareBearing) { |
| 460 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where angle to centre is", String.format("%.1f", flareAngle)); |
| 461 |
continue; |
| 462 |
} |
| 463 |
flareAngle = fb.getReverseArc().getInitialHeading() - fb.getDest().getCoord().bearingTo(centre); |
| 464 |
if (flareAngle < -180) |
| 465 |
flareAngle += 360; |
| 466 |
if (flareAngle > 180) |
| 467 |
flareAngle -= 360; |
| 468 |
if (Math.abs(flareAngle) > maxFlareBearing) { |
| 469 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where angle to centre is", String.format("%.1f", flareAngle)); |
| 470 |
continue; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
if (maxFlareAngle > 0) { |
| 476 |
double flareAngle = fa.getReverseArc().getInitialHeading() - fb.getReverseArc().getInitialHeading(); |
| 477 |
if (flareAngle < -180) |
| 478 |
flareAngle += 360; |
| 479 |
if (flareAngle > 180) |
| 480 |
flareAngle -= 360; |
| 481 |
if (Math.abs(flareAngle) > maxFlareAngle) { |
| 482 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where subtended angle is", String.format("%.1f", flareAngle)); |
| 483 |
continue; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
boolean faExtended = false; |
| 489 |
boolean fbExtended = false; |
| 490 |
boolean thirdArcToRoundabout = false; |
| 491 |
for (RouteArc ra2 : fa.getDest().getArcs()) { |
| 492 |
if (ra2.isDirect()) { |
| 493 |
if (ra2.getDest() != nodeToCheck && ra2.getSource() != nodeToCheck && ra2.getDest() != nextExitNode && ra2.getSource() != nextExitNode) |
| 494 |
{ |
| 495 |
RoadDef rd2 = ra2.getRoadDef(); |
| 496 |
if (rd2 == rda) |
| 497 |
faExtended = true; |
| 498 |
if (rd2 == rdb) |
| 499 |
fbExtended = true; |
| 500 |
if (!thirdArcToRoundabout) { |
| 501 |
byte access = rd2.getAccess(); |
| 502 |
if (access != 0 && (access != AccessTagsAndBits.FOOT)) { |
| 503 |
for (RouteNode rn : roundaboutNodes) { |
| 504 |
if (rn == ra2.getSource() || rn == ra2.getDest()) { |
| 505 |
thirdArcToRoundabout = true; |
| 506 |
break; |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
if (faExtended && fbExtended && discardBothFlaresExtend) { |
| 515 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where flares both extend at", fa.getDest().getCoord().toOSMURL()); |
| 516 |
continue; |
| 517 |
} |
| 518 |
if (discardMultipleFlares && thirdArcToRoundabout) { |
| 519 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where more than two roads join roundabout from", fa.getDest().getCoord().toOSMURL()); |
| 520 |
continue; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
String namea = rda.getName(); |
| 525 |
String nameb = rdb.getName(); |
| 526 |
if (discardDifferentFlareNames) { |
| 527 |
if (namea != null && nameb != null && !namea.equals(nameb) && !namea.contains(nameb) && !nameb.contains(namea)) { |
| 528 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where flares have different names", namea, "and", nameb); |
| 529 |
continue; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
byte accessa = rda.getAccess(); |
| 535 |
byte accessb = rdb.getAccess(); |
| 536 |
if (discardDifferentAccesses) { |
| 537 |
if (accessa != accessb) { |
| 538 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where flares have different accesses"); |
| 539 |
continue; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
if ((centre != null) && (maxEntryAngle > 0)) { |
| 545 |
double bearingFrom = centre.bearingTo(coord); |
| 546 |
double bearingTo = centre.bearingTo(nextExitNode.getCoord()); |
| 547 |
double angle = bearingTo - bearingFrom; |
| 548 |
if (angle < 0) |
| 549 |
angle = -angle; |
| 550 |
if (angle > 180) |
| 551 |
angle = 360 - angle; |
| 552 |
if (angle > maxEntryAngle) { |
| 553 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, coord.toOSMURL(), nextExitNode.getCoord().toOSMURL(), "where angle", angle, "from centre is too large"); |
| 554 |
continue; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
if(maxFlareToSeparationMultiple > 0) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
int roundaboutSegmentLength = roundaboutSegmentLength(nodeToCheck, nextExitNode); |
| 565 |
int maxLength = (flareSeparationOverride == 0 ? roundaboutSegmentLength : Math.max(flareSeparationOverride, roundaboutSegmentLength)) * maxFlareToSeparationMultiple; |
| 566 |
if(maxLength > 0 && |
| 567 |
fa.getLength() > maxLength && |
| 568 |
fb.getLength() > maxLength) { |
| 569 |
log.info("Discarding possible roundabout flare", rda, "with", rdb, "where roads are", fa.getLength(), "and", fb.getLength(), "m long with a separation of", roundaboutSegmentLength, "m"); |
| 570 |
continue; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
boolean processed = false; |
| 576 |
RoadDef extendedRoad = null; |
| 577 |
for(RouteArc a : fa.getDest().getArcs()) { |
| 578 |
if(a.isDirect() && a.getDest() != nodeToCheck && a.getDest() != nextExitNode) { |
| 579 |
RoadDef ard = a.getRoadDef(); |
| 580 |
if((ard == rda) || (ard == rdb)) { |
| 581 |
|
| 582 |
|
| 583 |
if (nextButOneExitNode != null) { |
| 584 |
|
| 585 |
for(RouteArc fc : nextButOneExitNode.getArcs()) { |
| 586 |
if(!fc.isDirect()) |
| 587 |
continue; |
| 588 |
|
| 589 |
RoadDef rdc = fc.getRoadDef(); |
| 590 |
if (rdc.isSynthesised()) |
| 591 |
continue; |
| 592 |
|
| 593 |
byte access = rdc.getAccess(); |
| 594 |
if (access != 0 && (access != AccessTagsAndBits.FOOT)) { |
| 595 |
if(a.getDest() == fc.getDest()) { |
| 596 |
|
| 597 |
processed = true; |
| 598 |
log.info("Discarding possible roundabout flare", rda, "at", fa.getDest().getCoord().toOSMURL(), "multiple flares found"); |
| 599 |
break; |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
if (!processed && (precedingExitNode != null)) { |
| 605 |
|
| 606 |
for(RouteArc fc : precedingExitNode.getArcs()) { |
| 607 |
if(!fc.isDirect()) |
| 608 |
continue; |
| 609 |
|
| 610 |
RoadDef rdc = fc.getRoadDef(); |
| 611 |
if (rdc.isSynthesised()) |
| 612 |
continue; |
| 613 |
|
| 614 |
byte access = rdc.getAccess(); |
| 615 |
if (access != 0 && (access != AccessTagsAndBits.FOOT)) { |
| 616 |
if(a.getDest() == fc.getDest()) { |
| 617 |
|
| 618 |
processed = true; |
| 619 |
log.info("Discarding possible roundabout flare", rda, "at", fa.getDest().getCoord().toOSMURL(), "multiple flares found"); |
| 620 |
break; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
if (processed) |
| 626 |
break; |
| 627 |
extendedRoad = ard; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
if (processed) |
| 632 |
continue; |
| 633 |
|
| 634 |
|
| 635 |
if(!rda.isOneway()) |
| 636 |
log.diagnostic("Outgoing roundabout flare road " + rda + " is not oneway " + fa.getSource().getCoord().toOSMURL()); |
| 637 |
else if(!rdb.isOneway()) |
| 638 |
log.diagnostic("Incoming roundabout flare road " + rdb + " is not oneway " + fb.getDest().getCoord().toOSMURL()); |
| 639 |
else if(!fa.isForward()) |
| 640 |
log.diagnostic("Outgoing roundabout flare road " + rda + " points in wrong direction " + fa.getSource().getCoord().toOSMURL()); |
| 641 |
else if(fb.isForward()) |
| 642 |
log.diagnostic("Incoming roundabout flare road " + rdb + " points in wrong direction " + fb.getSource().getCoord().toOSMURL()); |
| 643 |
else if (extendedRoad != null) { |
| 644 |
if(extendedRoad == rda) |
| 645 |
log.diagnostic("Outgoing roundabout flare road " + rda + " does not finish at flare " + fa.getDest().getCoord().toOSMURL()); |
| 646 |
else if(extendedRoad == rdb) |
| 647 |
log.diagnostic("Incoming roundabout flare road " + rdb + " does not start at flare " + fb.getDest().getCoord().toOSMURL()); |
| 648 |
} |
| 649 |
else if (namea != null && nameb != null && !namea.equals(nameb)) |
| 650 |
log.diagnostic("Roundabout flare roads " + rda + " and " + rdb + " have different names " + namea + " and " + nameb); |
| 651 |
else if (namea != null && nameb == null) |
| 652 |
log.diagnostic("Roundabout flare road " + rda + " has name " + namea + " but " + rdb + " is unnamed"); |
| 653 |
else if (namea == null && nameb != null) |
| 654 |
log.diagnostic("Roundabout flare road " + rdb + " has name " + nameb + " but " + rda + " is unnamed"); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
private static int roundaboutSegmentLength(final RouteNode n1, final RouteNode n2) { |
| 663 |
List<RouteNode> seen = new ArrayList<>(); |
| 664 |
int len = 0; |
| 665 |
RouteNode n = n1; |
| 666 |
boolean checkMoreLinks = true; |
| 667 |
while(checkMoreLinks && !seen.contains(n)) { |
| 668 |
checkMoreLinks = false; |
| 669 |
seen.add(n); |
| 670 |
for(RouteArc a : n.getArcs()) { |
| 671 |
if(a.isForward() && |
| 672 |
a.getRoadDef().isRoundabout() && |
| 673 |
!a.getRoadDef().isSynthesised()) { |
| 674 |
len += a.getLength(); |
| 675 |
n = a.getDest(); |
| 676 |
if(n == n2) |
| 677 |
return len; |
| 678 |
checkMoreLinks = true; |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
return Integer.MAX_VALUE; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
private boolean goesInsideRoundabout(RouteArc ra, RouteNode rn, Coord roundaboutCentre) { |
| 694 |
|
| 695 |
RouteNode nextNode = ra.getSource() == rn ? ra.getDest() : ra.getSource(); |
| 696 |
Coord nextCoord = nextNode.getCoord(); |
| 697 |
boolean rejoinsRoundabout = false; |
| 698 |
for (RouteNode roundaboutNode : roundaboutNodes) { |
| 699 |
if (roundaboutNode == nextNode) { |
| 700 |
|
| 701 |
rejoinsRoundabout = true; |
| 702 |
nextCoord = rn.getCoord().offset(ra.getSource() == rn ? ra.getInitialHeading() : ra.getReverseArc().getInitialHeading(), rn.getCoord().distance(nextCoord) / 2); |
| 703 |
break; |
| 704 |
} |
| 705 |
} |
| 706 |
double distanceToCentre = roundaboutCentre.distance(rn.getCoord()); |
| 707 |
double nextDistanceToCentre = roundaboutCentre.distance(nextCoord); |
| 708 |
RoadDef rd = ra.getRoadDef(); |
| 709 |
if (Math.abs(nextDistanceToCentre - distanceToCentre) < 5) { |
| 710 |
if (rejoinsRoundabout) { |
| 711 |
Coord nextCoord2 = nextCoord.offset(rn.getCoord().bearingTo(nextCoord), nextCoord.distance(rn.getCoord()) / 2); |
| 712 |
double nextDistanceToCentre2 = roundaboutCentre.distance(nextCoord2); |
| 713 |
if (Math.abs(nextDistanceToCentre2 - distanceToCentre) > 2) |
| 714 |
return nextDistanceToCentre2 < distanceToCentre; |
| 715 |
} |
| 716 |
else { |
| 717 |
|
| 718 |
for (RouteArc nra : nextNode.getArcs()) { |
| 719 |
if ((nra != ra) && (nra.getRoadDef() == rd)) { |
| 720 |
RouteNode nextNode2 = nra.getSource() == nextNode ? nra.getDest() : nra.getSource(); |
| 721 |
if (nextNode2 == rn) |
| 722 |
continue; |
| 723 |
|
| 724 |
Coord nextCoord2 = nextNode2.getCoord(); |
| 725 |
for (RouteNode roundaboutNode : roundaboutNodes) { |
| 726 |
if (roundaboutNode == nextNode2) { |
| 727 |
|
| 728 |
nextCoord2 = nextCoord.offset(nra.getSource() == nextNode ? nra.getInitialHeading() : nra.getReverseArc().getInitialHeading(), nextCoord.distance(nextCoord2) / 2); |
| 729 |
break; |
| 730 |
} |
| 731 |
} |
| 732 |
if (nextCoord.distance(nextCoord2) > distanceToCentre) |
| 733 |
nextCoord2 = nextCoord.offset(nextCoord.bearingTo(nextCoord2), distanceToCentre / 2); |
| 734 |
double nextDistanceToCentre2 = roundaboutCentre.distance(nextCoord2); |
| 735 |
if (Math.abs(nextDistanceToCentre2 - distanceToCentre) > 2) |
| 736 |
return nextDistanceToCentre2 < distanceToCentre; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
Coord nextCoord3 = nextCoord.offset(rn.getCoord().bearingTo(nextCoord), distanceToCentre / 2); |
| 741 |
double nextDistanceToCentre3 = roundaboutCentre.distance(nextCoord3); |
| 742 |
if (Math.abs(nextDistanceToCentre3 - distanceToCentre) > 2) |
| 743 |
return nextDistanceToCentre3 < distanceToCentre; |
| 744 |
} |
| 745 |
} |
| 746 |
if ((Math.abs(nextDistanceToCentre - distanceToCentre) < 2) && !rd.messagePreviouslyIssued("inside roundabout")) |
| 747 |
log.info("Way",rd,"unable to accurately determine whether",nextCoord.toOSMURL()," is inside roundabout centred at", roundaboutCentre.toOSMURL()); |
| 748 |
|
| 749 |
return nextDistanceToCentre < distanceToCentre; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
private class CentreFinder { |
| 756 |
double delta; |
| 757 |
RouteNode nearestNodeToCentre; |
| 758 |
RouteNode furthestNodeFromCentre; |
| 759 |
} |
| 760 |
|
| 761 |
private Coord getRoundaboutCentre() { |
| 762 |
int numPoints = roundaboutNodes.size(); |
| 763 |
double centreLat = 0; |
| 764 |
double centreLon = 0; |
| 765 |
for (RouteNode rn : roundaboutNodes) { |
| 766 |
centreLat += rn.getCoord().getHighPrecLat(); |
| 767 |
centreLon += rn.getCoord().getHighPrecLon(); |
| 768 |
} |
| 769 |
|
| 770 |
Coord roundaboutCentre = Coord.makeHighPrecCoord((int)Math.round(centreLat / numPoints), (int)Math.round(centreLon / numPoints)); |
| 771 |
if(log.isDebugEnabled()) |
| 772 |
log.debug("Roundabout centre of gravity", roundaboutCentre.toOSMURL()); |
| 773 |
if (roundaboutNodes.size() > 2) { |
| 774 |
CentreFinder cf = getDeltaDistanceFromCentre(roundaboutCentre); |
| 775 |
while (cf.delta > 1.0) { |
| 776 |
double bearing = roundaboutCentre.bearingTo(cf.furthestNodeFromCentre.getCoord()); |
| 777 |
Coord betterCentre = roundaboutCentre.offset(bearing, cf.delta / 3); |
| 778 |
CentreFinder bettercf = getDeltaDistanceFromCentre(betterCentre); |
| 779 |
if(log.isDebugEnabled()) |
| 780 |
log.debug("Trying centre", betterCentre.toOSMURL()); |
| 781 |
if (bettercf.delta < cf.delta) { |
| 782 |
cf = bettercf; |
| 783 |
roundaboutCentre = betterCentre; |
| 784 |
} |
| 785 |
else { |
| 786 |
bearing = cf.nearestNodeToCentre.getCoord().bearingTo(roundaboutCentre); |
| 787 |
betterCentre = roundaboutCentre.offset(bearing, cf.delta / 3); |
| 788 |
if(log.isDebugEnabled()) |
| 789 |
log.debug("Trying centre", betterCentre.toOSMURL()); |
| 790 |
bettercf = getDeltaDistanceFromCentre(betterCentre); |
| 791 |
if (bettercf.delta < cf.delta) { |
| 792 |
cf = bettercf; |
| 793 |
roundaboutCentre = betterCentre; |
| 794 |
} |
| 795 |
else |
| 796 |
break; |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
if(log.isDebugEnabled()) |
| 801 |
log.debug("Roundabout centre", roundaboutCentre.toOSMURL()); |
| 802 |
return roundaboutCentre; |
| 803 |
} |
| 804 |
|
| 805 |
private CentreFinder getDeltaDistanceFromCentre(Coord centreCoord) { |
| 806 |
CentreFinder cf = new CentreFinder(); |
| 807 |
double minDistanceToCentre = 9999999; |
| 808 |
double maxDistanceToCentre = 0; |
| 809 |
for (RouteNode rn : roundaboutNodes) { |
| 810 |
double distanceToCentre = centreCoord.distance(rn.getCoord()); |
| 811 |
if (minDistanceToCentre > distanceToCentre) { |
| 812 |
minDistanceToCentre = distanceToCentre; |
| 813 |
cf.nearestNodeToCentre = rn; |
| 814 |
} |
| 815 |
if (maxDistanceToCentre < distanceToCentre) { |
| 816 |
maxDistanceToCentre = distanceToCentre; |
| 817 |
cf.furthestNodeFromCentre = rn; |
| 818 |
} |
| 819 |
} |
| 820 |
cf.delta = maxDistanceToCentre - minDistanceToCentre; |
| 821 |
return cf; |
| 822 |
} |
| 823 |
|
| 824 |
} |