Ticket #7178: add_inside_search_operator_factory.patch
| File add_inside_search_operator_factory.patch, 8.4 KB (added by , 14 years ago) |
|---|
-
utilsplugin2/src/utilsplugin2/selection/NodeWayUtils.java
457 457 // System.out.printf("Intersected intercount %d %s\n",interCount, point.toString()); 458 458 if (interCount%2 == 1) return 1; else return 0; 459 459 } 460 461 public static Collection<OsmPrimitive> selectAllInside(Collection<OsmPrimitive> selected, DataSet dataset) { 462 Set<Way> selectedWays = OsmPrimitive.getFilteredSet(selected, Way.class); 463 Set<Relation> selectedRels = OsmPrimitive.getFilteredSet(selected, Relation.class); 460 464 465 for (Relation r: selectedRels) { 466 if (!r.isMultipolygon()) selectedRels.remove(r); 467 } 468 469 Set<Way> newWays = new HashSet<Way>(); 470 Set<Node> newNodes = new HashSet<Node>(); 471 // select ways attached to already selected ways 472 if (!selectedWays.isEmpty()) { 473 for (Way w: selectedWays) { 474 addAllInsideWay(dataset,w,newWays,newNodes); 475 } 476 } 477 if (!selectedRels.isEmpty()) { 478 for (Relation r: selectedRels) { 479 addAllInsideMultipolygon(dataset,r,newWays,newNodes); 480 } 481 } 482 483 Set<OsmPrimitive> insideSelection = new HashSet<OsmPrimitive>(); 484 if (!newWays.isEmpty() || !newNodes.isEmpty()) { 485 insideSelection.addAll(newWays); 486 insideSelection.addAll(newNodes); 487 } 488 return insideSelection; 489 } 490 491 461 492 } -
utilsplugin2/src/utilsplugin2/selection/SelectAllInsideAction.java
14 14 15 15 import org.openstreetmap.josm.Main; 16 16 import org.openstreetmap.josm.actions.JosmAction; 17 import org.openstreetmap.josm.data.osm.Node; 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 import org.openstreetmap.josm.data.osm.Relation; 20 import org.openstreetmap.josm.data.osm.Way; 17 import org.openstreetmap.josm.data.osm.*; 21 18 import org.openstreetmap.josm.tools.Shortcut; 22 19 23 20 /** … … 31 28 KeyEvent.VK_I, Shortcut.GROUP_EDIT ,KeyEvent.ALT_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK), true); 32 29 putValue("help", ht("/Action/SelectAllInside")); 33 30 } 34 31 32 @Override 35 33 public void actionPerformed(ActionEvent e) { 36 34 long t=System.currentTimeMillis(); 37 Set<Way> selectedWays = OsmPrimitive.getFilteredSet(getCurrentDataSet().getSelected(), Way.class); 38 Set<Relation> selectedRels = OsmPrimitive.getFilteredSet(getCurrentDataSet().getSelected(), Relation.class); 39 40 for (Relation r: selectedRels) { 41 if (!r.isMultipolygon()) selectedRels.remove(r); 42 } 43 44 Set<Way> newWays = new HashSet<Way>(); 45 Set<Node> newNodes = new HashSet<Node>(); 46 // select ways attached to already selected ways 47 if (!selectedWays.isEmpty()) { 48 for (Way w: selectedWays) { 49 NodeWayUtils.addAllInsideWay(getCurrentDataSet(),w,newWays,newNodes); 50 } 51 } 52 if (!selectedRels.isEmpty()) { 53 for (Relation r: selectedRels) { 54 NodeWayUtils.addAllInsideMultipolygon(getCurrentDataSet(),r,newWays,newNodes); 55 } 56 } 57 if (!newWays.isEmpty() || !newNodes.isEmpty()) { 58 getCurrentDataSet().addSelected(newWays); 59 getCurrentDataSet().addSelected(newNodes); 35 Collection<OsmPrimitive> insideSelected = NodeWayUtils.selectAllInside(getCurrentDataSet().getSelected(), getCurrentDataSet()); 36 37 if (!insideSelected.isEmpty()) { 38 getCurrentDataSet().addSelected(insideSelected); 60 39 } else{ 61 40 JOptionPane.showMessageDialog(Main.parent, 62 41 tr("Nothing found. Please select some closed ways or multipolygons to find all primitives inside them!"), -
utilsplugin2/src/utilsplugin2/UtilsPlugin2.java
1 1 // License: GPL v2 or later. See LICENSE file for details. 2 2 package utilsplugin2; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 4 6 import utilsplugin2.customurl.ChooseURLAction; 5 7 import utilsplugin2.customurl.OpenPageAction; 6 8 import utilsplugin2.customurl.UtilsPluginPreferences; 7 9 8 10 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 9 11 import java.awt.event.KeyEvent; 12 import java.util.Collection; 13 import java.util.HashSet; 14 import java.util.Set; 10 15 import javax.swing.JMenu; 11 16 import javax.swing.JMenuItem; 12 17 import utilsplugin2.selection.*; 13 18 import utilsplugin2.dumbutils.*; 14 19 15 20 import org.openstreetmap.josm.Main; 21 import org.openstreetmap.josm.actions.search.PushbackTokenizer; 22 import org.openstreetmap.josm.actions.search.SearchCompiler; 23 import org.openstreetmap.josm.actions.search.SearchCompiler.*; 24 import org.openstreetmap.josm.data.osm.OsmPrimitive; 25 import org.openstreetmap.josm.data.osm.Relation; 26 import org.openstreetmap.josm.data.osm.Way; 16 27 import org.openstreetmap.josm.gui.MainMenu; 17 28 import org.openstreetmap.josm.gui.MapFrame; 18 29 import org.openstreetmap.josm.plugins.Plugin; … … 87 98 88 99 selectURL = MainMenu.add(toolsMenu, new ChooseURLAction()); 89 100 90 101 // register search operators 102 SearchCompiler.addMatchFactory(new UtilsMatchFactory()); 91 103 } 92 104 93 105 @Override … … 124 136 public PreferenceSetting getPreferenceSetting() { 125 137 return new UtilsPluginPreferences(); 126 138 } 139 140 public class UtilsMatchFactory implements MatchFactory { 127 141 142 @Override 143 public Match getSimpleMatch(String keyword, PushbackTokenizer tokenizer) throws ParseError { 144 return null; 145 } 146 147 @Override 148 public UnaryMatch getUnaryMatch(String keyword, Match matchOperand, PushbackTokenizer tokenizer) throws ParseError { 149 if ("inside".equals(keyword)) 150 return new InsideMatch(matchOperand); 151 return null; 152 } 153 154 @Override 155 public BinaryMatch getBinaryMatch(String keyword, Match lhs, Match rhs, PushbackTokenizer tokenizer) throws ParseError { 156 return null; 157 } 158 159 @Override 160 public Collection<String> getKeywords() { 161 throw new UnsupportedOperationException("Not supported yet."); 162 } 163 164 @Override 165 public boolean isUnaryMatch(String keyword) { 166 if ("inside".equals(keyword)) 167 return true; 168 return false; 169 } 170 171 @Override 172 public boolean isBinaryMatch(String keyword) { 173 throw new UnsupportedOperationException("Not supported yet."); 174 } 175 176 } 177 178 /** 179 * Matches all objects contained within the match expression. 180 */ 181 public static class InsideMatch extends UnaryMatch { 182 private Collection<OsmPrimitive> inside = null; 183 184 public InsideMatch(Match match) { 185 super(match); 186 init(); 187 188 } 189 190 /** 191 * Find all objects inside areas which match the expression 192 */ 193 private void init() { 194 Collection<OsmPrimitive> matchedAreas = new HashSet<OsmPrimitive>(); 195 196 // find all ways that match the expression 197 Collection<Way> ways = Main.main.getCurrentDataSet().getWays(); 198 for (Way way : ways) { 199 if (match.match(way)) 200 matchedAreas.add(way); 201 } 202 203 // find all relations that match the expression 204 Collection<Relation> rels = Main.main.getCurrentDataSet().getRelations(); 205 for (Relation rel : rels) { 206 if (match.match(rel)) 207 matchedAreas.add(rel); 208 } 209 210 inside = NodeWayUtils.selectAllInside(matchedAreas, Main.main.getCurrentDataSet()); 211 } 212 213 @Override 214 public boolean match(OsmPrimitive osm) { 215 return inside.contains(osm); 216 } 217 } 128 218 }
