Arrays.asList("outer", "inner", "forward", "backward", "north", "south", "east", "west"));
// First, change the original way
Way changedWay = new Way(way);
changedWay.setNodes(chunkIt.next());
commandList.add(new ChangeCommand(way, changedWay));
if (!newSelection.contains(way)) {
newSelection.add(way);
}
List<Way> newWays = new ArrayList<>();
// Second, create new ways
while (chunkIt.hasNext()) {
Way wayToAdd = new Way();
wayToAdd.setKeys(way.getKeys());
newWays.add(wayToAdd);
wayToAdd.setNodes(chunkIt.next());
commandList.add(new AddCommand(layer,wayToAdd));
newSelection.add(wayToAdd);
}
boolean warnmerole = false;
boolean warnme = false;
// now copy all relations to new way also
for (Relation r : OsmPrimitive.getFilteredList(way.getReferrers(), Relation.class)) {
if (!r.isUsable()) {
continue;
}
Relation c = null;
String type = r.get("type");
if (type == null) {
type = "";
}
int i_c = 0, i_r = 0;
List<RelationMember> relationMembers = r.getMembers();
for (RelationMember rm: relationMembers) {
if (rm.isWay() && rm.getMember() == way) {
boolean insert = true;
if ("restriction".equals(type))
{
/* this code assumes the restriction is correct. No real error checking done */
String role = rm.getRole();
if("from".equals(role) || "to".equals(role))
{
OsmPrimitive via = null;
for (RelationMember rmv : r.getMembers()) {
if("via".equals(rmv.getRole())){
via = rmv.getMember();
}
}
List<Node> nodes = new ArrayList<>();
if(via != null) {
if(via instanceof Node) {
nodes.add((Node)via);
} else if(via instanceof Way) {
nodes.add(((Way)via).lastNode());
nodes.add(((Way)via).firstNode());
}
}
Way res = null;
for(Node n : nodes) {
if(changedWay.isFirstLastNode(n)) {
res = way;
}
}
if(res == null)
{
for (Way wayToAdd : newWays) {
for(Node n : nodes) {
if(wayToAdd.isFirstLastNode(n)) {
res = wayToAdd;
}
}
}
if(res != null)
{
if (c == null) {
c = new Relation(r);
}
c.addMember(new RelationMember(role, res));
c.removeMembersFor(way);
insert = false;
}
} else {
insert = false;
}
}
else if(!"via".equals(role)) {
warnme = true;
}
}
else if (!("route".equals(type)) && !("multipolygon".equals(type))) {
warnme = true;
}
if (c == null) {
c = new Relation(r);
}
if(insert)
{
if (rm.hasRole() && !nowarnroles.contains(rm.getRole())) {
warnmerole = true;
}
Boolean backwards = null;
int k = 1;
while (i_r - k >= 0 || i_r + k < relationMembers.size()) {
if ((i_r - k >= 0) && relationMembers.get(i_r - k).isWay()){
Way w = relationMembers.get(i_r - k).getWay();
if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
backwards = false;
} else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
backwards = true;
}
break;
}
if ((i_r + k < relationMembers.size()) && relationMembers.get(i_r + k).isWay()){
Way w = relationMembers.get(i_r + k).getWay();
if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
backwards = true;
} else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
backwards = false;
}
break;
}
k++;
}
int j = i_c;
for (Way wayToAdd : newWays) {
RelationMember em = new RelationMember(rm.getRole(), wayToAdd);
j++;
if ((backwards != null) && backwards) {
c.addMember(i_c, em);
} else {
c.addMember(j, em);
}
}
i_c = j;
}
}
i_c++; i_r++;
}
if (c != null) {
commandList.add(new ChangeCommand(layer,r, c));
}
}
if (warnmerole) {
new Notification(
tr("A role based relation membership was copied to all new ways.<br>You should verify this and correct it when necessary."))