Changeset 28693 in osm for applications
- Timestamp:
- 2012-09-07T16:40:13+02:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/reltoolbox
- Files:
-
- 6 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/reltoolbox/build.xml
r27927 r28693 30 30 <project name="reltoolbox" default="dist" basedir="."> 31 31 <!-- enter the SVN commit message --> 32 <property name="commit.message" value="RelToolbox: fix redifinition warnings"/>32 <property name="commit.message" value="RelToolbox: refactored relation fixing code, added associatedStreet as a fixable relation"/> 33 33 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 34 34 <property name="plugin.main.version" value="5018"/> -
applications/editors/josm/plugins/reltoolbox/src/relcontext/ChosenRelation.java
r26299 r28693 118 118 if( opacity < 0.01 ) 119 119 return; 120 120 121 Composite oldComposite = g.getComposite(); 121 122 Stroke oldStroke = g.getStroke(); 122 Composite oldComposite = g.getComposite();123 g.setStroke(new BasicStroke(9, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 123 124 g.setColor(Color.yellow); 124 g.setStroke(new BasicStroke(9, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));125 125 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f * opacity)); 126 for( OsmPrimitive element : chosenRelation.getMemberPrimitives() ) { 126 127 drawRelations(g, mv, bbox, chosenRelation); 128 129 g.setComposite(oldComposite); 130 g.setStroke(oldStroke); 131 132 } 133 private void drawRelations(Graphics2D g, MapView mv, Bounds bbox, Relation rel) { 134 for( OsmPrimitive element : rel.getMemberPrimitives() ) { 127 135 if( element.getType() == OsmPrimitiveType.NODE ) { 128 136 Node node = (Node)element; … … 142 150 } 143 151 } else if( element.getType() == OsmPrimitiveType.RELATION ) { 144 // todo: draw all relation members (recursion?) 152 Color oldColor = g.getColor(); 153 g.setColor(Color.magenta); 154 drawRelations(g, mv, bbox, (Relation)element); 155 g.setColor(oldColor); 145 156 } 146 157 // todo: closedway, multipolygon - ? 147 158 } 148 g.setStroke(oldStroke);149 g.setComposite(oldComposite);150 159 } 151 160 -
applications/editors/josm/plugins/reltoolbox/src/relcontext/RelContextDialog.java
r28290 r28693 108 108 roleBox.setVisible(false); 109 109 enterRoleAction = new EnterRoleAction(); // just for the shortcut 110 sortAndFixAction = new SortAndFixAction(chosenRelation); 110 111 111 112 // [±][X] relation U [AZ][Down][Edit] 112 113 chosenRelationPanel = new JPanel(new GridBagLayout()); 113 addRemoveMemberAction = new AddRemoveMemberAction(chosenRelation );114 addRemoveMemberAction = new AddRemoveMemberAction(chosenRelation, sortAndFixAction); 114 115 chosenRelationPanel.add(new JButton(addRemoveMemberAction), GBC.std()); 115 116 chosenRelationPanel.add(sizeButton(new JButton(new ClearChosenRelationAction(chosenRelation)), 32, 0), GBC.std()); … … 118 119 chosenRelationPanel.add(chosenRelationComponent, GBC.std().fill().insets(5, 0, 5, 0)); 119 120 chosenRelationPanel.add(roleBox, GBC.std().fill().insets(5, 0, 5, 0)); 120 sortAndFixAction = new SortAndFixAction(chosenRelation);121 121 final JButton sortAndFixButton = (JButton) sizeButton(new JButton(sortAndFixAction), 32, 0); 122 122 chosenRelationPanel.add(sortAndFixButton, GBC.std().fill(GBC.VERTICAL)); -
applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/AddRemoveMemberAction.java
r27927 r28693 1 1 package relcontext.actions; 2 2 3 import java.util.*;4 import org.openstreetmap.josm.data.osm.OsmPrimitive;5 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 6 5 import java.awt.event.ActionEvent; 7 6 import java.awt.event.KeyEvent; 7 import java.util.ArrayList; 8 import java.util.Collection; 9 8 10 import org.openstreetmap.josm.Main; 9 11 import org.openstreetmap.josm.actions.JosmAction; … … 11 13 import org.openstreetmap.josm.command.Command; 12 14 import org.openstreetmap.josm.data.osm.Node; 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 16 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 14 17 import org.openstreetmap.josm.data.osm.Relation; … … 17 20 import org.openstreetmap.josm.tools.ImageProvider; 18 21 import org.openstreetmap.josm.tools.Shortcut; 22 19 23 import relcontext.ChosenRelation; 20 24 import relcontext.ChosenRelationListener; … … 30 34 private static final String ACTION_NAME = "Add/remove member"; 31 35 private ChosenRelation rel; 36 private SortAndFixAction sortAndFix; 32 37 33 public AddRemoveMemberAction( ChosenRelation rel ) {38 public AddRemoveMemberAction( ChosenRelation rel, SortAndFixAction sortAndFix ) { 34 39 super(null, "relcontext/addremove", tr("Add/remove members from the chosen relation"), 35 40 Shortcut.registerShortcut("reltoolbox:addremove", tr("Relation Toolbox: {0}", tr("Add/remove members from the chosen relation")), 36 41 KeyEvent.VK_EQUALS, Shortcut.DIRECT), false); 37 42 this.rel = rel; 43 this.sortAndFix = sortAndFix; 38 44 rel.addChosenRelationListener(this); 39 45 updateEnabledState(); … … 51 57 52 58 // 0. check if relation is broken (temporary) 53 boolean isBroken = !toAdd.isEmpty() && SortAndFixAction.needsFixing(r);59 boolean isBroken = !toAdd.isEmpty() && sortAndFix.needsFixing(r); 54 60 55 61 // 1. remove all present members … … 66 72 67 73 // 3. check for roles again (temporary) 68 Command roleFix = !isBroken && SortAndFixAction.needsFixing(r) ? SortAndFixAction.fixRelation(r) : null;74 Command roleFix = !isBroken && sortAndFix.needsFixing(r) ? sortAndFix.fixRelation(r) : null; 69 75 if( roleFix != null ) 70 76 roleFix.executeCommand(); -
applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/SortAndFixAction.java
r25751 r28693 1 1 package relcontext.actions; 2 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 3 5 import java.awt.event.ActionEvent; 4 import java.util.*; 6 import java.util.ArrayList; 7 import java.util.List; 8 5 9 import javax.swing.AbstractAction; 6 10 import javax.swing.Action; 7 import static org.openstreetmap.josm.tools.I18n.tr; 11 8 12 import org.openstreetmap.josm.Main; 9 import org.openstreetmap.josm.command.ChangeCommand;10 13 import org.openstreetmap.josm.command.Command; 11 import org.openstreetmap.josm.data.osm.*; 14 import org.openstreetmap.josm.data.osm.Relation; 15 import org.openstreetmap.josm.data.osm.RelationMember; 12 16 import org.openstreetmap.josm.tools.ImageProvider; 17 13 18 import relcontext.ChosenRelation; 14 19 import relcontext.ChosenRelationListener; 20 import relcontext.relationfix.AssociatedStreetFixer; 21 import relcontext.relationfix.BoundaryFixer; 22 import relcontext.relationfix.MultipolygonFixer; 23 import relcontext.relationfix.NothingFixer; 24 import relcontext.relationfix.RelationFixer; 15 25 16 26 public class SortAndFixAction extends AbstractAction implements ChosenRelationListener { 17 private ChosenRelation rel; 27 private static final long serialVersionUID = 1L; 28 private ChosenRelation rel; 29 private List<RelationFixer> fixers; 18 30 19 31 public SortAndFixAction( ChosenRelation rel ) { … … 25 37 rel.addChosenRelationListener(this); 26 38 setEnabled(false); 39 40 // construct all available fixers 41 fixers = new ArrayList<RelationFixer>(); 42 fixers.add(new BoundaryFixer()); // should be before multipolygon as takes special case of multipolygon relation - boundary 43 fixers.add(new MultipolygonFixer()); 44 fixers.add(new AssociatedStreetFixer()); 45 27 46 } 28 47 … … 37 56 } 38 57 39 public static boolean needsFixing( Relation rel ) { 40 return !isIncomplete(rel) && (areMultipolygonTagsEmpty(rel) || areBoundaryTagsNotRight(rel)); 58 public boolean needsFixing( Relation rel ) { 59 return !isIncomplete(rel) && !getFixer(rel).isRelationGood(rel); 60 } 61 62 private RelationFixer getFixer( Relation rel ) { 63 for(RelationFixer fixer : fixers) 64 if (fixer.isFixerApplicable(rel)) 65 return fixer; 66 return new NothingFixer(); 41 67 } 42 68 43 public static Command fixRelation( Relation rel ) { 44 Relation r = rel; 45 boolean fixed = false; 46 // todo: sort members 47 // todo: set roles for multipolygon members 48 Relation rr = fixMultipolygonRoles(r); 49 if( rr != null ) { 50 r = rr; 51 fixed = true; 52 } 53 // todo: set roles for boundary members 54 rr= fixBoundaryRoles(r); 55 if( rr != null ) { 56 r = rr; 57 fixed = true; 58 } 59 60 return fixed ? new ChangeCommand(rel, r) : null; 69 public Command fixRelation( Relation rel ) { 70 return getFixer(rel).fixRelation(rel); 61 71 } 62 72 … … 70 80 } 71 81 72 /**73 * Check for ways that have roles different from "outer" and "inner".74 */75 private static boolean areMultipolygonTagsEmpty( Relation r ) {76 if( r == null || r.getMembersCount() == 0 || !ChosenRelation.isMultipolygon(r) )77 return false;78 for( RelationMember m : r.getMembers() ) {79 if( m.getType().equals(OsmPrimitiveType.WAY) && (m.getRole() == null || (!m.getRole().equals("outer") && !m.getRole().equals("inner"))) )80 return true;81 }82 return false;83 }84 85 /**86 * Check for nodes and relations without needed roles.87 */88 private static boolean areBoundaryTagsNotRight( Relation r ) {89 if( r == null || r.getMembersCount() == 0 || !r.hasKey("type") || !r.get("type").equals("boundary") )90 return false;91 for( RelationMember m : r.getMembers() ) {92 if( m.getType().equals(OsmPrimitiveType.RELATION) && (m.getRole() == null || !m.getRole().equals("subarea")) )93 return true;94 else if(m.getType().equals(OsmPrimitiveType.NODE) && (m.getRole() == null || (!m.getRole().equals("label") && !m.getRole().equals("admin_centre"))) )95 return true;96 }97 return false;98 }99 100 /**101 * Basically, created multipolygon from scratch, and if successful, replace roles with new ones.102 */103 private static Relation fixMultipolygonRoles( Relation source ) {104 Collection<Way> ways = new ArrayList<Way>();105 for( OsmPrimitive p : source.getMemberPrimitives() )106 if( p instanceof Way )107 ways.add((Way)p);108 MultipolygonCreate mpc = new MultipolygonCreate();109 String error = mpc.makeFromWays(ways);110 if( error != null )111 return null;112 113 Relation r = new Relation(source);114 boolean fixed = false;115 Set<Way> outerWays = new HashSet<Way>();116 for( MultipolygonCreate.JoinedPolygon poly : mpc.outerWays )117 for( Way w : poly.ways )118 outerWays.add(w);119 Set<Way> innerWays = new HashSet<Way>();120 for( MultipolygonCreate.JoinedPolygon poly : mpc.innerWays )121 for( Way w : poly.ways )122 innerWays.add(w);123 for( int i = 0; i < r.getMembersCount(); i++ ) {124 RelationMember m = r.getMember(i);125 if( m.isWay() ) {126 String role = null;127 if( outerWays.contains((Way)m.getMember()) )128 role = "outer";129 else if( innerWays.contains((Way)m.getMember()) )130 role = "inner";131 if( role != null && !role.equals(m.getRole()) ) {132 r.setMember(i, new RelationMember(role, m.getMember()));133 fixed = true;134 }135 }136 }137 return fixed ? r : null;138 }139 140 private static Relation fixBoundaryRoles( Relation source ) {141 Relation r = new Relation(source);142 boolean fixed = false;143 for( int i = 0; i < r.getMembersCount(); i++ ) {144 RelationMember m = r.getMember(i);145 String role = null;146 if( m.isRelation() )147 role = "subarea";148 else if( m.isNode() ) {149 Node n = (Node)m.getMember();150 if( !n.isIncomplete() ) {151 if( n.hasKey("place") )152 role = "admin_centre";153 else154 role = "label";155 }156 }157 if( role != null && !role.equals(m.getRole()) ) {158 r.setMember(i, new RelationMember(role, m.getMember()));159 fixed = true;160 }161 }162 return fixed ? r : null;163 }164 82 }
Note:
See TracChangeset
for help on using the changeset viewer.