}
// For each way, intersect with areas
int nCreatedNodes = 0;
for (OSMWay way : waysById.values()) {
OSMLevel wayLevel = getLevelForWay(way);
// For each segment of the way
for (int i = 0; i < way.getNodeRefs().size() - 1; i++) {
OSMNode nA = nodesById.get(way.getNodeRefs().get(i));
OSMNode nB = nodesById.get(way.getNodeRefs().get(i + 1));
if (nA == null || nB == null) {
continue;
}
Envelope env = new Envelope(nA.lon, nB.lon, nA.lat, nB.lat);
List<RingSegment> ringSegments = spndx.query(env);
if (ringSegments.size() == 0)
continue;
LineString seg = GeometryUtils.makeLineString(nA.lon, nA.lat, nB.lon, nB.lat);
for (RingSegment ringSegment : ringSegments) {
// Skip if both segments share a common node
if (ringSegment.nA.getId() == nA.getId()
|| ringSegment.nA.getId() == nB.getId()
|| ringSegment.nB.getId() == nA.getId()
|| ringSegment.nB.getId() == nB.getId())
continue;
// Check for real intersection
LineString seg2 = GeometryUtils.makeLineString(ringSegment.nA.lon,
ringSegment.nA.lat, ringSegment.nB.lon, ringSegment.nB.lat);
Geometry intersection = seg2.intersection(seg);
Point p = null;
if (intersection.isEmpty()) {
continue;
} else if (intersection instanceof Point) {
p = (Point) intersection;
} else {
/*
* This should never happen (intersection between two lines should be a
* point or a multi-point).
*/
LOG.error("Alien intersection type between {} ({}--{}) and {} ({}--{}): ",
way, nA, nB, ringSegment.area.parent, ringSegment.nA,
ringSegment.nB, intersection);
continue;
}
// Skip if area and way are from "incompatible" levels
OSMLevel areaLevel = getLevelForWay(ringSegment.area.parent);
if (!wayLevel.equals(areaLevel))
continue;
// Create a virtual node and insert it in both the way and the ring
OSMNode virtualNode = createVirtualNode(p.getCoordinate());