Changeset 15078 in josm for trunk/src


Ignore:
Timestamp:
2019-05-15T22:38:20+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #17718 - Make it easier to add specially treated relations to SplitWayCommand (patch by taylor.smock)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/command/SplitWayCommand.java

    r14654 r15078  
    99import java.util.Collection;
    1010import java.util.Collections;
     11import java.util.HashMap;
    1112import java.util.HashSet;
    1213import java.util.Iterator;
    1314import java.util.LinkedList;
    1415import java.util.List;
     16import java.util.Map;
    1517import java.util.Objects;
    1618import java.util.Optional;
     
    5254    private final Way originalWay;
    5355    private final List<Way> newWays;
     56    /** Map&lt;Restriction type, type to treat it as&gt; */
     57    private static final Map<String, String> relationSpecialTypes = new HashMap<>();
     58    static {
     59        relationSpecialTypes.put("restriction", "restriction");
     60        relationSpecialTypes.put("destination_sign", "restriction");
     61    }
    5462
    5563    /**
     
    311319                if (rm.isWay() && rm.getMember() == way) {
    312320                    boolean insert = true;
    313                     if ("restriction".equals(type) || "destination_sign".equals(type)) {
    314                         /* this code assumes the restriction is correct. No real error checking done */
    315                         String role = rm.getRole();
    316                         if ("from".equals(role) || "to".equals(role)) {
    317                             OsmPrimitive via = findVia(r, type);
    318                             List<Node> nodes = new ArrayList<>();
    319                             if (via != null) {
    320                                 if (via instanceof Node) {
    321                                     nodes.add((Node) via);
    322                                 } else if (via instanceof Way) {
    323                                     nodes.add(((Way) via).lastNode());
    324                                     nodes.add(((Way) via).firstNode());
    325                                 }
    326                             }
    327                             Way res = null;
    328                             for (Node n : nodes) {
    329                                 if (changedWay.isFirstLastNode(n)) {
    330                                     res = way;
    331                                 }
    332                             }
    333                             if (res == null) {
    334                                 for (Way wayToAdd : newWays) {
    335                                     for (Node n : nodes) {
    336                                         if (wayToAdd.isFirstLastNode(n)) {
    337                                             res = wayToAdd;
    338                                         }
    339                                     }
    340                                 }
    341                                 if (res != null) {
    342                                     if (c == null) {
    343                                         c = new Relation(r);
    344                                     }
    345                                     c.addMember(new RelationMember(role, res));
    346                                     c.removeMembersFor(way);
    347                                     insert = false;
    348                                 }
    349                             } else {
    350                                 insert = false;
    351                             }
    352                         } else if (!"via".equals(role)) {
    353                             warnme = true;
    354                         }
     321                    if (relationSpecialTypes.containsKey(type) && "restriction".equals(relationSpecialTypes.get(type))) {
     322                        Map<String, Boolean> rValue = treatAsRestriction(r, rm, c, newWays, way, changedWay);
     323                        warnme = rValue.containsKey("warnme") ? rValue.get("warnme") : warnme;
     324                        insert = rValue.containsKey("insert") ? rValue.get("insert") : insert;
    355325                    } else if (!("route".equals(type)) && !("multipolygon".equals(type))) {
    356326                        warnme = true;
     
    440410    }
    441411
     412    private static Map<String, Boolean> treatAsRestriction(Relation r,
     413            RelationMember rm, Relation c, Collection<Way> newWays, Way way,
     414            Way changedWay) {
     415        HashMap<String, Boolean> rMap = new HashMap<>();
     416        /* this code assumes the restriction is correct. No real error checking done */
     417        String role = rm.getRole();
     418        String type = Optional.ofNullable(r.get("type")).orElse("");
     419        if ("from".equals(role) || "to".equals(role)) {
     420            OsmPrimitive via = findVia(r, type);
     421            List<Node> nodes = new ArrayList<>();
     422            if (via != null) {
     423                if (via instanceof Node) {
     424                    nodes.add((Node) via);
     425                } else if (via instanceof Way) {
     426                    nodes.add(((Way) via).lastNode());
     427                    nodes.add(((Way) via).firstNode());
     428                }
     429            }
     430            Way res = null;
     431            for (Node n : nodes) {
     432                if (changedWay.isFirstLastNode(n)) {
     433                    res = way;
     434                }
     435            }
     436            if (res == null) {
     437                for (Way wayToAdd : newWays) {
     438                    for (Node n : nodes) {
     439                        if (wayToAdd.isFirstLastNode(n)) {
     440                            res = wayToAdd;
     441                        }
     442                    }
     443                }
     444                if (res != null) {
     445                    if (c == null) {
     446                        c = new Relation(r);
     447                    }
     448                    c.addMember(new RelationMember(role, res));
     449                    c.removeMembersFor(way);
     450                    rMap.put("insert", false);
     451                }
     452            } else {
     453                rMap.put("insert", false);
     454            }
     455        } else if (!"via".equals(role)) {
     456            rMap.put("warnme", true);
     457        }
     458        return rMap;
     459    }
     460
    442461    static OsmPrimitive findVia(Relation r, String type) {
    443462        if (type != null) {
     
    478497        return chunks != null ? splitWay(way, chunks, selection) : null;
    479498    }
     499
     500    /**
     501     * Add relations that are treated in a specific way.
     502     * @param relationType The value in the {@code type} key
     503     * @param treatAs The type of relation to treat the {@code relationType} as.
     504     * Currently only supports relations that can be handled like "restriction"
     505     * relations.
     506     * @return the previous value associated with relationType, or null if there was no mapping
     507     * @since 15078
     508     */
     509    public static String addSpecialRelationType(String relationType, String treatAs) {
     510        return relationSpecialTypes.put(relationType, treatAs);
     511    }
     512
     513    /**
     514     * Get the types of relations that are treated differently
     515     * @return {@code Map<Relation Type, Type of Relation it is to be treated as>}
     516     * @since 15078
     517     */
     518    public static Map<String, String> getSpecialRelationTypes() {
     519        return relationSpecialTypes;
     520    }
    480521}
Note: See TracChangeset for help on using the changeset viewer.