source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/BoundaryFixer.java@ 36136

Last change on this file since 36136 was 36103, checked in by taylor.smock, 2 years ago

reltoolbox: Preferentially use the source relation dataset

If the source relation does not have a dataset, use the current edit dataset instead.

This also adds some basic tests.

File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package relcontext.relationfix;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import org.openstreetmap.josm.command.ChangeCommand;
7import org.openstreetmap.josm.command.Command;
8import org.openstreetmap.josm.data.osm.DataSet;
9import org.openstreetmap.josm.data.osm.Node;
10import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.RelationMember;
13import org.openstreetmap.josm.gui.MainApplication;
14import org.openstreetmap.josm.tools.Utils;
15
16/**
17 * Fix multipolygon boundaries
18 * @see <a href="https://wiki.openstreetmap.org/wiki/Relation:boundary">osmwiki:Relation:boundary</a>
19 */
20public class BoundaryFixer extends MultipolygonFixer {
21
22 public BoundaryFixer() {
23 super("boundary", "multipolygon");
24 }
25
26 /**
27 * For boundary relations both "boundary" and "multipolygon" types are applicable, but
28 * it should also have key boundary=administrative to be fully boundary.
29 * @see <a href="https://wiki.openstreetmap.org/wiki/Relation:boundary">osmwiki:Relation:boundary</a>
30 */
31 @Override
32 public boolean isFixerApplicable(Relation rel) {
33 return super.isFixerApplicable(rel) && "administrative".equals(rel.get("boundary"));
34 }
35
36 @Override
37 public boolean isRelationGood(Relation rel) {
38 for (RelationMember m : rel.getMembers()) {
39 if (m.getType().equals(OsmPrimitiveType.RELATION) && !"subarea".equals(m.getRole())) {
40 setWarningMessage(tr("Relation without ''subarea'' role found"));
41 return false;
42 }
43 if (m.getType().equals(OsmPrimitiveType.NODE) && !("label".equals(m.getRole()) || "admin_centre".equals(m.getRole()))) {
44 setWarningMessage(tr("Node without ''label'' or ''admin_centre'' role found"));
45 return false;
46 }
47 if (m.getType().equals(OsmPrimitiveType.WAY) && !("outer".equals(m.getRole()) || "inner".equals(m.getRole()))) {
48 setWarningMessage(tr("Way without ''inner'' or ''outer'' role found"));
49 return false;
50 }
51 }
52 clearWarningMessage();
53 return true;
54 }
55
56 @Override
57 public Command fixRelation(Relation rel) {
58 Relation r = rel;
59 Relation rr = fixMultipolygonRoles(r);
60 boolean fixed = false;
61 if (rr != null) {
62 fixed = true;
63 r = rr;
64 }
65 rr = fixBoundaryRoles(r);
66 if (rr != null) {
67 fixed = true;
68 r = rr;
69 }
70 if (fixed) {
71 final DataSet ds = Utils.firstNonNull(rel.getDataSet(), MainApplication.getLayerManager().getEditDataSet());
72 return new ChangeCommand(ds, rel, r);
73 }
74 return null;
75 }
76
77 private Relation fixBoundaryRoles(Relation source) {
78 Relation r = new Relation(source);
79 boolean fixed = false;
80 for (int i = 0; i < r.getMembersCount(); i++) {
81 RelationMember m = r.getMember(i);
82 String role = null;
83 if (m.isRelation()) {
84 role = "subarea";
85 } else if (m.isNode()) {
86 Node n = (Node) m.getMember();
87 if (!n.isIncomplete()) {
88 if (n.hasKey("place")) {
89 String place = n.get("place");
90 if (place.equals("state") || place.equals("country") ||
91 place.equals("county") || place.equals("region")) {
92 role = "label";
93 } else {
94 role = "admin_centre";
95 }
96 } else {
97 role = "label";
98 }
99 }
100 }
101 if (role != null && !role.equals(m.getRole())) {
102 r.setMember(i, new RelationMember(role, m.getMember()));
103 fixed = true;
104 }
105 }
106 return fixed ? r : null;
107 }
108}
Note: See TracBrowser for help on using the repository browser.