| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.net; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Arrays; |
| 21 |
import java.util.BitSet; |
| 22 |
import java.util.Collection; |
| 23 |
import java.util.Collections; |
| 24 |
import java.util.HashSet; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.LinkedHashMap; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Map; |
| 29 |
import java.util.Map.Entry; |
| 30 |
import java.util.Set; |
| 31 |
import java.util.TreeMap; |
| 32 |
|
| 33 |
import uk.me.parabola.imgfmt.app.Coord; |
| 34 |
import uk.me.parabola.imgfmt.app.CoordNode; |
| 35 |
import uk.me.parabola.log.Logger; |
| 36 |
import uk.me.parabola.util.EnhancedProperties; |
| 37 |
import uk.me.parabola.util.MultiHashMap; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class RoadNetwork { |
| 47 |
private static final Logger log = Logger.getLogger(RoadNetwork.class); |
| 48 |
|
| 49 |
private static final int MAX_RESTRICTIONS_ARCS = 7; |
| 50 |
private final Map<Integer, RouteNode> nodes = new LinkedHashMap<>(); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private final List<RouteNode> boundary = new ArrayList<>(); |
| 55 |
private final List<RoadDef> roadDefs = new ArrayList<>(); |
| 56 |
private List<RouteCenter> centers = new ArrayList<>(); |
| 57 |
private AngleChecker angleChecker = new AngleChecker(); |
| 58 |
private RoundaboutCheck roundaboutCheck = new RoundaboutCheck(); |
| 59 |
|
| 60 |
private boolean reportSimilarArcs; |
| 61 |
private boolean routable; |
| 62 |
private boolean reportRoutingIslands; |
| 63 |
private long maxSumRoadLengths; |
| 64 |
|
| 65 |
private int visitId; |
| 66 |
|
| 67 |
public void config(EnhancedProperties props) { |
| 68 |
reportSimilarArcs = props.getProperty("report-similar-arcs", false); |
| 69 |
int checkRoutingIslandLengths = props.getProperty("check-routing-island-len", -1); |
| 70 |
if (checkRoutingIslandLengths >= 0) { |
| 71 |
Logger.defaultLogger.warn("The --check-routing-island-len option is deprecated. Please use --report-routing-islands and/or max-routing-island-len"); |
| 72 |
maxSumRoadLengths = checkRoutingIslandLengths; |
| 73 |
reportRoutingIslands = log.isInfoEnabled(); |
| 74 |
} |
| 75 |
maxSumRoadLengths = props.getProperty("max-routing-island-len", -1); |
| 76 |
reportRoutingIslands = props.getProperty("report-routing-islands", false); |
| 77 |
routable = props.containsKey("route"); |
| 78 |
angleChecker.config(props); |
| 79 |
roundaboutCheck.config(props); |
| 80 |
} |
| 81 |
|
| 82 |
public void addRoad(RoadDef roadDef, List<Coord> coordList) { |
| 83 |
roadDefs.add(roadDef); |
| 84 |
|
| 85 |
int lastNodePos = -1; |
| 86 |
double roadLength = 0; |
| 87 |
double arcLength = 0; |
| 88 |
int pointsHash = 0; |
| 89 |
|
| 90 |
int npoints = coordList.size(); |
| 91 |
int numCoordNodes = 0; |
| 92 |
int numNumberNodes = 0; |
| 93 |
BitSet nodeFlags = new BitSet(); |
| 94 |
for (int index = 0; index < npoints; index++) { |
| 95 |
Coord co = coordList.get(index); |
| 96 |
pointsHash += co.hashCode(); |
| 97 |
if (index > 0) { |
| 98 |
double segLenth = co.distance(coordList.get(index - 1)); |
| 99 |
arcLength += segLenth; |
| 100 |
roadLength += segLenth; |
| 101 |
} |
| 102 |
|
| 103 |
if (co.getId() > 0) { |
| 104 |
|
| 105 |
nodeFlags.set(numNumberNodes); |
| 106 |
++numCoordNodes; |
| 107 |
|
| 108 |
if (!roadDef.skipAddToNOD()) { |
| 109 |
if (lastNodePos < 0) { |
| 110 |
|
| 111 |
roadDef.setNode(getOrAddNode(co.getId(), co)); |
| 112 |
} else { |
| 113 |
createDirectArcs(roadDef, coordList, lastNodePos, index, roadLength, arcLength, pointsHash); |
| 114 |
} |
| 115 |
|
| 116 |
lastNodePos = index; |
| 117 |
arcLength = 0; |
| 118 |
pointsHash = co.hashCode(); |
| 119 |
} |
| 120 |
} |
| 121 |
if (co.isNumberNode()) |
| 122 |
++numNumberNodes; |
| 123 |
} |
| 124 |
if (roadDef.hasHouseNumbers()){ |
| 125 |
roadDef.setNumNodes(numNumberNodes); |
| 126 |
roadDef.setNod2BitSet(nodeFlags); |
| 127 |
} else { |
| 128 |
|
| 129 |
roadDef.setNumNodes(numCoordNodes); |
| 130 |
} |
| 131 |
roadDef.setLength(roadLength); |
| 132 |
if (coordList.get(0).getId() == 0) { |
| 133 |
roadDef.setStartsWithNode(false); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private void createDirectArcs(RoadDef roadDef, List<Coord> coordList, int prevPos, int currPos, double roadLength, double arcLength, |
| 141 |
int pointsHash) { |
| 142 |
|
| 143 |
Coord prevNode = coordList.get(prevPos); |
| 144 |
Coord currNode = coordList.get(currPos); |
| 145 |
int lastId = prevNode.getId(); |
| 146 |
int currId = currNode.getId(); |
| 147 |
|
| 148 |
if(log.isDebugEnabled()) { |
| 149 |
log.debug("lastId = " + lastId + " curId = " + currId); |
| 150 |
log.debug("from " + prevNode + " to " + currNode); |
| 151 |
log.debug("arclength=" + arcLength + " roadlength=" + roadLength); |
| 152 |
} |
| 153 |
|
| 154 |
RouteNode node1 = getOrAddNode(lastId, prevNode); |
| 155 |
RouteNode node2 = getOrAddNode(currId, currNode); |
| 156 |
if(node1 == node2) |
| 157 |
log.error("Road " + roadDef + " contains consecutive identical nodes at " + currNode.toOSMURL() + " - routing will be broken"); |
| 158 |
else if(arcLength == 0) |
| 159 |
log.warn("Road " + roadDef + " contains zero length arc at " + currNode.toOSMURL()); |
| 160 |
|
| 161 |
double directLength = (prevPos + 1 == currPos) ? arcLength : prevNode.distance(currNode); |
| 162 |
|
| 163 |
|
| 164 |
Coord forwardBearingPoint = coordList.get(prevPos + 1); |
| 165 |
if(prevNode.equals(forwardBearingPoint) || forwardBearingPoint.isAddedNumberNode()) { |
| 166 |
|
| 167 |
|
| 168 |
for (int bi = prevPos + 2; bi <= currPos; ++bi) { |
| 169 |
Coord coTest = coordList.get(bi); |
| 170 |
if (!(coTest.isAddedNumberNode() || prevNode.equals(coTest))) { |
| 171 |
forwardBearingPoint = coTest; |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
double forwardInitialBearing = prevNode.bearingTo(forwardBearingPoint); |
| 177 |
double forwardDirectBearing = (currNode == forwardBearingPoint) ? forwardInitialBearing: prevNode.bearingTo(currNode); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
RouteArc arc = new RouteArc(roadDef, |
| 182 |
node1, node2, |
| 183 |
forwardInitialBearing, |
| 184 |
forwardDirectBearing, |
| 185 |
arcLength, |
| 186 |
arcLength, |
| 187 |
directLength, |
| 188 |
pointsHash); |
| 189 |
arc.setForward(); |
| 190 |
node1.addArc(arc); |
| 191 |
|
| 192 |
|
| 193 |
Coord reverseBearingPoint = coordList.get(currPos - 1); |
| 194 |
if(currNode.equals(reverseBearingPoint) || reverseBearingPoint.isAddedNumberNode()) { |
| 195 |
|
| 196 |
|
| 197 |
for (int bi = currPos - 2; bi >= prevPos; --bi) { |
| 198 |
Coord coTest = coordList.get(bi); |
| 199 |
if (!(coTest.isAddedNumberNode() || currNode.equals(coTest))) { |
| 200 |
reverseBearingPoint = coTest; |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
double reverseInitialBearing = currNode.bearingTo(reverseBearingPoint); |
| 206 |
double reverseDirectBearing = 0; |
| 207 |
if (directLength > 0){ |
| 208 |
|
| 209 |
reverseDirectBearing = forwardDirectBearing + 180; |
| 210 |
} |
| 211 |
RouteArc reverseArc = new RouteArc(roadDef, |
| 212 |
node2, node1, |
| 213 |
reverseInitialBearing, |
| 214 |
reverseDirectBearing, |
| 215 |
arcLength, |
| 216 |
arcLength, |
| 217 |
directLength, |
| 218 |
pointsHash); |
| 219 |
node2.addArc(reverseArc); |
| 220 |
|
| 221 |
|
| 222 |
arc.setReverseArc(reverseArc); |
| 223 |
reverseArc.setReverseArc(arc); |
| 224 |
} |
| 225 |
|
| 226 |
private RouteNode getOrAddNode(int id, Coord coord) { |
| 227 |
return nodes.computeIfAbsent(id, key -> new RouteNode(coord)); |
| 228 |
} |
| 229 |
|
| 230 |
public List<RoadDef> getRoadDefs() { |
| 231 |
return roadDefs; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
private void splitCenters() { |
| 241 |
if (nodes.isEmpty()) |
| 242 |
return; |
| 243 |
assert centers.isEmpty() : "already subdivided into centers"; |
| 244 |
|
| 245 |
List<RouteNode> nodeList = new ArrayList<>(nodes.values()); |
| 246 |
nodes.clear(); |
| 247 |
|
| 248 |
nodeList.forEach(this::performChecks); |
| 249 |
|
| 250 |
|
| 251 |
NOD1Part nod1 = new NOD1Part(); |
| 252 |
nodeList.stream().filter(node -> node.getArcs().isEmpty()).forEach(nod1::addNode); |
| 253 |
centers.addAll(nod1.subdivide()); |
| 254 |
nodeList.removeIf(node -> node.getArcs().isEmpty()); |
| 255 |
|
| 256 |
|
| 257 |
for (int group = 0; group <= 4; group++) { |
| 258 |
nod1 = new NOD1Part(); |
| 259 |
for (RouteNode node : nodeList) { |
| 260 |
if (node.getGroup() == group) { |
| 261 |
nod1.addNode(node); |
| 262 |
} |
| 263 |
} |
| 264 |
centers.addAll(nod1.subdivide()); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
private void performChecks(RouteNode node) { |
| 269 |
if(!node.isBoundary()) { |
| 270 |
roundaboutCheck.check(node); |
| 271 |
if(reportSimilarArcs) |
| 272 |
node.reportSimilarArcs(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
public List<RouteCenter> getCenters() { |
| 277 |
if (routable && centers.isEmpty()){ |
| 278 |
checkRoutingIslands(); |
| 279 |
for (RouteNode n : nodes.values()) { |
| 280 |
if (n.isBoundary()) { |
| 281 |
boundary.add(n); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
angleChecker.check(nodes); |
| 286 |
addArcsToMajorRoads(); |
| 287 |
|
| 288 |
|
| 289 |
for (RoadDef rd : roadDefs) { |
| 290 |
if (rd.skipAddToNOD()) |
| 291 |
continue; |
| 292 |
if (rd.getNode().getArcs().isEmpty()) { |
| 293 |
rd.getNode().setRoadClass(Math.max(rd.getRoadClass(),rd.getNode().getNodeClass())); |
| 294 |
|
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
splitCenters(); |
| 299 |
} |
| 300 |
return centers; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
private void checkRoutingIslands() { |
| 307 |
if (maxSumRoadLengths <= 0 && !reportRoutingIslands) |
| 308 |
return; |
| 309 |
|
| 310 |
long t1 = System.currentTimeMillis(); |
| 311 |
|
| 312 |
|
| 313 |
List<List<RouteNode>> islands = searchIslands(); |
| 314 |
long t2 = System.currentTimeMillis(); |
| 315 |
log.info("Search for routing islands found", islands.size(), "islands in", (t2 - t1), "ms"); |
| 316 |
if (!islands.isEmpty()) { |
| 317 |
analyseIslands(islands); |
| 318 |
} |
| 319 |
if (maxSumRoadLengths > 0) { |
| 320 |
long t3 = System.currentTimeMillis(); |
| 321 |
log.info("routing island removal took", (t3 - t2), "ms"); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
private List<List<RouteNode>> searchIslands() { |
| 327 |
final int myVisitId = ++visitId; |
| 328 |
List<List<RouteNode>> islands = new ArrayList<>(); |
| 329 |
for (RouteNode firstNode : nodes.values()) { |
| 330 |
if (firstNode.getVisitID() != myVisitId) { |
| 331 |
List<RouteNode> island = new ArrayList<>(); |
| 332 |
firstNode.visitNet(myVisitId, island); |
| 333 |
|
| 334 |
if (island.stream().noneMatch(RouteNode::isBoundary)) { |
| 335 |
islands.add(island); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
return islands; |
| 340 |
} |
| 341 |
|
| 342 |
private void analyseIslands (List<List<RouteNode>> islands) { |
| 343 |
|
| 344 |
MultiHashMap<RouteNode, RoadDef> nodeToRoadMap = new MultiHashMap<>(); |
| 345 |
roadDefs.forEach(rd -> { |
| 346 |
if (rd.getNode() != null) { |
| 347 |
nodeToRoadMap.add(rd.getNode(), rd); |
| 348 |
} |
| 349 |
}); |
| 350 |
|
| 351 |
boolean cleanNodes = false; |
| 352 |
for (List<RouteNode> island : islands) { |
| 353 |
|
| 354 |
Set<RoadDef> visitedRoads = new HashSet<>(); |
| 355 |
long sumOfRoadLengths = calcIslandSize(island, nodeToRoadMap, visitedRoads); |
| 356 |
if (reportRoutingIslands) |
| 357 |
log.diagnostic("Routing island " + visitedRoads.iterator().next() + " at " + island.get(0).getCoord() + " with " + island.size() + |
| 358 |
" routing node(s) and total length of " + sumOfRoadLengths + "m"); |
| 359 |
if (sumOfRoadLengths < maxSumRoadLengths) { |
| 360 |
|
| 361 |
island.forEach(RouteNode::discard); |
| 362 |
visitedRoads.forEach(rd -> rd.skipAddToNOD(true)); |
| 363 |
cleanNodes = true; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
if (cleanNodes) { |
| 368 |
|
| 369 |
Iterator<Entry<Integer, RouteNode>> iter = nodes.entrySet().iterator(); |
| 370 |
while (iter.hasNext()) { |
| 371 |
RouteNode n = iter.next().getValue(); |
| 372 |
if (n.isDiscarded()) { |
| 373 |
iter.remove(); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
private static long calcIslandSize(Collection<RouteNode> islandNodes, MultiHashMap<RouteNode, RoadDef> nodeToRoadMap, |
| 387 |
Set<RoadDef> visitedRoads) { |
| 388 |
double sumLen = 0; |
| 389 |
for (RouteNode node : islandNodes) { |
| 390 |
for (RouteArc arc : node.arcsIteration()) { |
| 391 |
if (arc.isDirect() && arc.isForward()) { |
| 392 |
visitedRoads.add(arc.getRoadDef()); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
for (RoadDef rd : nodeToRoadMap.get(node)) { |
| 398 |
visitedRoads.add(rd); |
| 399 |
} |
| 400 |
} |
| 401 |
for (RoadDef rd : visitedRoads) { |
| 402 |
sumLen += rd.getLenInMeter(); |
| 403 |
} |
| 404 |
return Math.round(sumLen); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
private void addArcsToMajorRoads() { |
| 411 |
long t1 = System.currentTimeMillis(); |
| 412 |
|
| 413 |
for (RoadDef rd: roadDefs){ |
| 414 |
if (rd.skipAddToNOD()) |
| 415 |
continue; |
| 416 |
if (rd.getRoadClass() >= 1) |
| 417 |
rd.getNode().addArcsToMajorRoads(rd); |
| 418 |
} |
| 419 |
log.info(" added major road arcs in " + (System.currentTimeMillis() - t1) + " ms"); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public List<RouteNode> getBoundary() { |
| 428 |
return Collections.unmodifiableList(boundary); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public int addRestriction(GeneralRouteRestriction grr) { |
| 445 |
if (grr.getType() == GeneralRouteRestriction.RestrType.TYPE_NO_TROUGH) |
| 446 |
return addNoThroughRoute(grr); |
| 447 |
String sourceDesc = grr.getSourceDesc(); |
| 448 |
|
| 449 |
List<RouteNode> viaNodes = new ArrayList<>(); |
| 450 |
for (CoordNode via : grr.getViaNodes()){ |
| 451 |
RouteNode vn = nodes.get(via.getId()); |
| 452 |
if (vn == null){ |
| 453 |
log.error(sourceDesc, "can't locate 'via' RouteNode with id", via.getId()); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
viaNodes.add(vn); |
| 457 |
} |
| 458 |
|
| 459 |
int firstViaId = grr.getViaNodes().get(0).getId(); |
| 460 |
int lastViaId = grr.getViaNodes().get(grr.getViaNodes().size()-1).getId(); |
| 461 |
RouteNode firstViaNode = nodes.get(firstViaId); |
| 462 |
RouteNode lastViaNode = nodes.get(lastViaId); |
| 463 |
List<List<RouteArc>> viaArcsList = new ArrayList<>(); |
| 464 |
if (grr.getViaNodes().size() != grr.getViaWayIds().size() + 1){ |
| 465 |
log.error(sourceDesc, "internal error: number of via nodes and via ways doesn't fit"); |
| 466 |
return 0; |
| 467 |
} |
| 468 |
for (int i = 1; i < grr.getViaNodes().size(); i++){ |
| 469 |
RouteNode vn = viaNodes.get(i-1); |
| 470 |
Long viaWayId = grr.getViaWayIds().get(i-1); |
| 471 |
List<RouteArc> viaArcs = vn.getDirectArcsTo(viaNodes.get(i), viaWayId); |
| 472 |
if (viaArcs.isEmpty()){ |
| 473 |
log.error(sourceDesc, "can't locate arc from 'via' node at",vn.getCoord().toOSMURL(),"to next 'via' node on way",viaWayId); |
| 474 |
return 0; |
| 475 |
} |
| 476 |
viaArcsList.add(viaArcs); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
int fromId = 0; |
| 481 |
RouteNode fn = null; |
| 482 |
if (grr.getFromNode() != null){ |
| 483 |
fromId = grr.getFromNode().getId(); |
| 484 |
|
| 485 |
fn = nodes.get(fromId); |
| 486 |
if (fn == null ){ |
| 487 |
log.error(sourceDesc, "can't locate 'from' RouteNode with id", fromId); |
| 488 |
return 0; |
| 489 |
} |
| 490 |
} else { |
| 491 |
List<RouteArc> possibleFromArcs = firstViaNode.getDirectArcsOnWay(grr.getFromWayId()); |
| 492 |
for (RouteArc arc : possibleFromArcs){ |
| 493 |
if (fn == null) |
| 494 |
fn = arc.getDest(); |
| 495 |
else if (fn != arc.getDest()){ |
| 496 |
log.warn(sourceDesc, "found different 'from' arcs for way",grr.getFromWayId(),"restriction is ignored"); |
| 497 |
return 0; |
| 498 |
} |
| 499 |
} |
| 500 |
if (fn == null){ |
| 501 |
log.warn(sourceDesc, "can't locate 'from' RouteNode for 'from' way", grr.getFromWayId()); |
| 502 |
return 0; |
| 503 |
} |
| 504 |
fromId = fn.getCoord().getId(); |
| 505 |
} |
| 506 |
List<RouteArc> fromArcs = fn.getDirectArcsTo(firstViaNode, grr.getFromWayId()); |
| 507 |
if (fromArcs.isEmpty()){ |
| 508 |
log.error(sourceDesc, "can't locate arc from 'from' node", fromId, "to 'via' node", firstViaId, "on way", |
| 509 |
grr.getFromWayId()); |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
RouteNode tn = null; |
| 515 |
int toId = 0; |
| 516 |
List<RouteArc> toArcs; |
| 517 |
if (grr.getToNode() != null){ |
| 518 |
|
| 519 |
toId = grr.getToNode().getId(); |
| 520 |
tn = nodes.get(toId); |
| 521 |
if (tn == null ){ |
| 522 |
log.error(sourceDesc, "can't locate 'to' RouteNode with id", toId); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
toArcs = lastViaNode.getDirectArcsTo(tn, grr.getToWayId()); |
| 526 |
} else { |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
List<RouteArc> possibleToArcs = lastViaNode.getDirectArcsOnWay(grr.getToWayId()); |
| 532 |
RouteArc fromArc = fromArcs.get(0); |
| 533 |
|
| 534 |
boolean ignoreAngle = false; |
| 535 |
if (fromArc.getLengthInMeter() <= 0.0001) |
| 536 |
ignoreAngle = true; |
| 537 |
if (grr.getDirIndicator() == '?') |
| 538 |
ignoreAngle = true; |
| 539 |
log.info(sourceDesc, "found", possibleToArcs.size(), "candidates for to-arc"); |
| 540 |
|
| 541 |
|
| 542 |
Map<Integer, List<RouteArc>> angleMap = new TreeMap<>(); |
| 543 |
for (RouteArc arc : possibleToArcs){ |
| 544 |
if (arc.getLengthInMeter() <= 0.0001) |
| 545 |
ignoreAngle = true; |
| 546 |
Integer angle = Math.round(getAngle(fromArc, arc)); |
| 547 |
angleMap.computeIfAbsent(angle, k-> new ArrayList<>()).add(arc); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
Iterator<Entry<Integer, List<RouteArc>>> iter = angleMap.entrySet().iterator(); |
| 552 |
Integer bestAngle = null; |
| 553 |
while (iter.hasNext()){ |
| 554 |
Entry<Integer, List<RouteArc>> entry = iter.next(); |
| 555 |
if (ignoreAngle || matchDirectionInfo(entry.getKey(), grr.getDirIndicator()) ){ |
| 556 |
if (bestAngle == null) |
| 557 |
bestAngle = entry.getKey(); |
| 558 |
else { |
| 559 |
bestAngle = getBetterAngle(bestAngle, entry.getKey(), grr.getDirIndicator()); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
if (bestAngle == null){ |
| 565 |
log.warn(sourceDesc,"the angle of the from and to way don't match the restriction"); |
| 566 |
return 0; |
| 567 |
} |
| 568 |
toArcs = angleMap.get(bestAngle); |
| 569 |
} |
| 570 |
if (toArcs.isEmpty()){ |
| 571 |
log.error(sourceDesc, "can't locate arc from 'via' node", lastViaId, "to 'to' node", toId, "on way", |
| 572 |
grr.getToWayId()); |
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
List<RouteArc> badArcs = new ArrayList<>(); |
| 577 |
|
| 578 |
if (grr.getType() == GeneralRouteRestriction.RestrType.TYPE_NOT){ |
| 579 |
for (RouteArc toArc: toArcs){ |
| 580 |
badArcs.add(toArc); |
| 581 |
} |
| 582 |
} |
| 583 |
else if (grr.getType() == GeneralRouteRestriction.RestrType.TYPE_ONLY){ |
| 584 |
|
| 585 |
for (RouteArc badArc : lastViaNode.arcsIteration()){ |
| 586 |
if (!badArc.isDirect() || toArcs.contains(badArc)) |
| 587 |
continue; |
| 588 |
badArcs.add(badArc); |
| 589 |
} |
| 590 |
if (badArcs.isEmpty()){ |
| 591 |
log.warn(sourceDesc, "restriction ignored because it has no effect"); |
| 592 |
return 0; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
List<List<RouteArc>> arcLists = new ArrayList<>(); |
| 598 |
arcLists.add(fromArcs); |
| 599 |
arcLists.addAll(viaArcsList); |
| 600 |
arcLists.add(badArcs); |
| 601 |
if (arcLists.size() > MAX_RESTRICTIONS_ARCS){ |
| 602 |
log.warn(sourceDesc, "has more than", MAX_RESTRICTIONS_ARCS, "arcs, this is not supported"); |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
for (int i = 0; i < arcLists.size(); i++){ |
| 608 |
List<RouteArc> arcs = arcLists.get(i); |
| 609 |
int countNoEffect = 0; |
| 610 |
int countOneway= 0; |
| 611 |
for (int j = arcs.size() - 1; j >= 0; --j) { |
| 612 |
RouteArc arc = arcs.get(j); |
| 613 |
if (!isUsable(arc.getRoadDef().getAccess(), grr.getExceptionMask())) { |
| 614 |
countNoEffect++; |
| 615 |
arcs.remove(j); |
| 616 |
} else if (arc.getRoadDef().isOneway() && !arc.isForward()) { |
| 617 |
countOneway++; |
| 618 |
arcs.remove(j); |
| 619 |
} |
| 620 |
} |
| 621 |
String arcType = null; |
| 622 |
if (arcs.isEmpty()){ |
| 623 |
if (i == 0) |
| 624 |
arcType = "from way is"; |
| 625 |
else if (i == arcLists.size()-1){ |
| 626 |
if (grr.getType() == GeneralRouteRestriction.RestrType.TYPE_ONLY) |
| 627 |
arcType = "all possible other ways are"; |
| 628 |
else |
| 629 |
arcType = "to way is"; |
| 630 |
} |
| 631 |
else |
| 632 |
arcType = "via way is"; |
| 633 |
String reason; |
| 634 |
if (countNoEffect > 0 && countOneway > 0) |
| 635 |
reason = "wrong direction in oneway or not accessible for restricted vehicles"; |
| 636 |
else if (countNoEffect > 0) |
| 637 |
reason = "not accessible for restricted vehicles"; |
| 638 |
else |
| 639 |
reason = "wrong direction in oneway"; |
| 640 |
log.warn(sourceDesc, "restriction ignored because",arcType,reason); |
| 641 |
return 0; |
| 642 |
} |
| 643 |
} |
| 644 |
if (viaNodes.contains(fn)){ |
| 645 |
log.warn(sourceDesc, "restriction not written because from node appears also as via node"); |
| 646 |
return 0; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
int numCombis = 1; |
| 652 |
int [] indexes = new int[arcLists.size()]; |
| 653 |
for (int i = 0; i < indexes.length; i++){ |
| 654 |
List<RouteArc> arcs = arcLists.get(i); |
| 655 |
numCombis *= arcs.size(); |
| 656 |
} |
| 657 |
List<RouteArc> path = new ArrayList<>(); |
| 658 |
int added = 0; |
| 659 |
for (int i = 0; i < numCombis; i++){ |
| 660 |
for (RouteNode vn : viaNodes){ |
| 661 |
path.clear(); |
| 662 |
boolean viaNodeFound = false; |
| 663 |
byte pathNoAccessMask = 0; |
| 664 |
for (int j = 0; j < indexes.length; j++){ |
| 665 |
RouteArc arc = arcLists.get(j).get(indexes[j]); |
| 666 |
if (!viaNodeFound || arc.getDest() == vn){ |
| 667 |
arc = arc.getReverseArc(); |
| 668 |
} |
| 669 |
if (arc.getSource() == vn) |
| 670 |
viaNodeFound = true; |
| 671 |
if (arc.getDest() == vn){ |
| 672 |
if (added > 0) |
| 673 |
log.error(sourceDesc, "restriction incompletely written because dest in arc is via node"); |
| 674 |
else |
| 675 |
log.warn(sourceDesc, "restriction not written because dest in arc is via node"); |
| 676 |
return added; |
| 677 |
} |
| 678 |
pathNoAccessMask |= ~arc.getRoadDef().getAccess(); |
| 679 |
path.add(arc); |
| 680 |
} |
| 681 |
byte pathAccessMask = (byte)~pathNoAccessMask; |
| 682 |
if (isUsable(pathAccessMask, grr.getExceptionMask())){ |
| 683 |
vn.addRestriction(new RouteRestriction(vn, path, grr.getExceptionMask())); |
| 684 |
++added; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
++indexes[indexes.length-1]; |
| 689 |
for (int j = indexes.length-1; j > 0; --j){ |
| 690 |
if (indexes[j] >= arcLists.get(j).size()){ |
| 691 |
indexes[j] = 0; |
| 692 |
indexes[j-1]++; |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
if (indexes[0] != arcLists.get(0).size()) |
| 699 |
log.error(sourceDesc, "failed to generate all possible paths"); |
| 700 |
log.info(sourceDesc, "added",added,"route restriction(s) to img file"); |
| 701 |
return added; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
private static boolean isUsable(byte roadAccess, byte exceptionMask) { |
| 711 |
return (roadAccess & (byte) ~exceptionMask) != 0; |
| 712 |
} |
| 713 |
|
| 714 |
private int addNoThroughRoute(GeneralRouteRestriction grr) { |
| 715 |
assert grr.getViaNodes() != null; |
| 716 |
assert grr.getViaNodes().size() == 1; |
| 717 |
int viaId = grr.getViaNodes().get(0).getId(); |
| 718 |
RouteNode vn = nodes.get(viaId); |
| 719 |
if (vn == null){ |
| 720 |
log.error(grr.getSourceDesc(), "can't locate 'via' RouteNode with id", viaId); |
| 721 |
return 0; |
| 722 |
} |
| 723 |
int added = 0; |
| 724 |
|
| 725 |
for (RouteArc out : vn.arcsIteration()) { |
| 726 |
if (!out.isDirect()) |
| 727 |
continue; |
| 728 |
for (RouteArc in : vn.arcsIteration()) { |
| 729 |
if (!in.isDirect() || in == out || in.getDest() == out.getDest()) |
| 730 |
continue; |
| 731 |
byte pathAccessMask = (byte) (out.getRoadDef().getAccess() & in.getRoadDef().getAccess()); |
| 732 |
if (isUsable(pathAccessMask, grr.getExceptionMask())) { |
| 733 |
vn.addRestriction(new RouteRestriction(vn, Arrays.asList(in, out), grr.getExceptionMask())); |
| 734 |
added++; |
| 735 |
} else if (log.isDebugEnabled()) { |
| 736 |
log.debug(grr.getSourceDesc(), "ignored no-through-route", in, "to", out); |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
return added; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
private static float getAngle(RouteArc fromArc, RouteArc toArc){ |
| 754 |
|
| 755 |
float headingFrom = fromArc.getFinalHeading(); |
| 756 |
float headingTo = toArc.getInitialHeading(); |
| 757 |
float angle = headingTo - headingFrom; |
| 758 |
while(angle > 180) |
| 759 |
angle -= 360; |
| 760 |
while(angle < -180) |
| 761 |
angle += 360; |
| 762 |
return angle; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
private static Integer getBetterAngle (Integer angle1, Integer angle2, char dirIndicator){ |
| 774 |
switch (dirIndicator) { |
| 775 |
case 'l': |
| 776 |
if (Math.abs(-90 - angle2) < Math.abs(-90 - angle1)) |
| 777 |
return angle2; |
| 778 |
break; |
| 779 |
case 'r': |
| 780 |
if (Math.abs(90 - angle2) < Math.abs(90 - angle1)) |
| 781 |
return angle2; |
| 782 |
break; |
| 783 |
case 'u': |
| 784 |
double d1 = (angle1 < 0) ? -180 - angle1 : 180 - angle1; |
| 785 |
double d2 = (angle2 < 0) ? -180 - angle2 : 180 - angle2; |
| 786 |
if (Math.abs(d2) < Math.abs(d1)) |
| 787 |
return angle2; |
| 788 |
break; |
| 789 |
case 's': |
| 790 |
if (Math.abs(angle2) < Math.abs(angle1)) |
| 791 |
return angle2; |
| 792 |
break; |
| 793 |
default: |
| 794 |
} |
| 795 |
return angle1; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
private static boolean matchDirectionInfo (float angle, char dirIndicator){ |
| 805 |
switch (dirIndicator) { |
| 806 |
case 'l': |
| 807 |
if (angle < -3 && angle > -177) |
| 808 |
return true; |
| 809 |
break; |
| 810 |
case 'r': |
| 811 |
if (angle > 3 && angle < 177) |
| 812 |
return true; |
| 813 |
break; |
| 814 |
case 'u': |
| 815 |
if (angle < -87 || angle > 93) |
| 816 |
return true; |
| 817 |
break; |
| 818 |
case 's': |
| 819 |
if (angle > -87 && angle < 87) |
| 820 |
return true; |
| 821 |
break; |
| 822 |
case '?': |
| 823 |
return true; |
| 824 |
} |
| 825 |
return false; |
| 826 |
} |
| 827 |
|
| 828 |
} |