String wayName = null;
OsmRelation wayRelation = null;
for (int i = 0; i < partToMerge.size(); i++) {
Relationship chunk = partToMerge.get(i);
//Create Coordinate objects for the nodes. Sequence matters!
Node startNode = chunk.getStartNode();
Node endNode = chunk.getEndNode();
Double startLat = (Double) startNode.getProperty(OsmEntityAttributeKey.NODE_LAT.name());
Double startLon = (Double) startNode.getProperty(OsmEntityAttributeKey.NODE_LON.name());
Double endLat = (Double) endNode.getProperty(OsmEntityAttributeKey.NODE_LAT.name());
Double endLon = (Double) endNode.getProperty(OsmEntityAttributeKey.NODE_LON.name());
coordinates.add(new Coordinate(startLon, startLat));
coordinates.add(new Coordinate(endLon, endLat));
//merge
if (partToMerge.size() == 1) {
//TODO nothing to merge
continue;
}
if (i == 0) {
wayRelation = chunk.isType(OsmRelation.BIDIRECTIONAL) ? OsmRelation.BIDIRECTIONAL : OsmRelation.ONE_WAY;
wayName = chunk.hasProperty(OsmEntityAttributeKey.WAY_NAME.name()) ?
(String) chunk.getProperty(OsmEntityAttributeKey.WAY_NAME.name()) : null;
//Start relationship -> only start node survives
graphDb.index().forRelationships("ways").remove(chunk);
chunk.delete();
} else {
graphDb.index().forRelationships("ways").remove(chunk);
chunk.delete();
graphDb.index().forNodes("nodes").remove(startNode);
startNode.delete();
}
}
Coordinate[] coords = getCoordinates(coordinates);
if (partToMerge.size() == 1) {
Relationship relationship = partToMerge.get(0);
double length = getDistanceInMeters(coords);
relationship.setProperty(OsmEntityAttributeKey.WAY_DISTANCE.name(), length);
} else if (partToMerge.size() > 1) {
//Create new relationship
Node startNode = partToMerge.get(0).getStartNode();
Node endNode = partToMerge.get(partToMerge.size() - 1).getEndNode();
Relationship newRelationship = startNode.createRelationshipTo(endNode, wayRelation);
//Set relationship properties
newRelationship.setProperty(OsmEntityAttributeKey.WAY_ID.name(), wayId);
if (wayName != null) {
newRelationship.setProperty(OsmEntityAttributeKey.WAY_NAME.name(), wayName);
}
//Set geometry and distance
LineString lineString = geometryFactory.createLineString(coords);
double length = getDistanceInMeters(coords);
String textGeometry = lineString.toText();
newRelationship.setProperty(OsmEntityAttributeKey.WAY_DISTANCE.name(), length);
newRelationship.setProperty(OsmEntityAttributeKey.WAY_GEOMETRY.name(), textGeometry);
//Add to index
graphDb.index().forRelationships("ways").add(newRelationship,
OsmEntityAttributeKey.WAY_ID.name(),
wayId);