| 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.DataSet;
|
|---|
| 9 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 10 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 13 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 14 | import 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 | */
|
|---|
| 20 | public 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 | }
|
|---|