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