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

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

reltoolbox: Clean up a bunch of lint warnings

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