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