Changeset 34228 in osm for applications/editors
- Timestamp:
- 2018-05-30T10:01:58+02:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/EdgeSelectionAction.java
r34183 r34228 16 16 import org.openstreetmap.josm.actions.mapmode.MapMode; 17 17 import org.openstreetmap.josm.data.osm.DataSet; 18 import org.openstreetmap.josm.data.osm.Node; 18 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 20 import org.openstreetmap.josm.data.osm.RelationMember; … … 21 22 import org.openstreetmap.josm.gui.MainApplication; 22 23 import org.openstreetmap.josm.gui.dialogs.relation.sort.RelationSorter; 24 import org.openstreetmap.josm.gui.layer.Layer; 25 import org.openstreetmap.josm.plugins.pt_assistant.gui.PTAssistantLayer; 23 26 import org.openstreetmap.josm.plugins.pt_assistant.utils.RouteUtils; 24 27 import org.openstreetmap.josm.tools.ImageProvider; … … 34 37 public class EdgeSelectionAction extends MapMode { 35 38 36 private static final String MAP_MODE_NAME = "Edge Selection"; 37 private static final long serialVersionUID = 2414977774504904238L; 38 39 private transient Set<Way> highlighted; 40 41 private Cursor selectionCursor; 42 private Cursor waySelectCursor; 43 private List<Way> edgeList; 44 45 public EdgeSelectionAction() { 46 super(tr(MAP_MODE_NAME), "edgeSelection", tr(MAP_MODE_NAME), 47 Shortcut.registerShortcut("mapmode:edge_selection", tr("Mode: {0}", tr(MAP_MODE_NAME)), KeyEvent.VK_K, Shortcut.CTRL), 48 ImageProvider.getCursor("normal", "selection")); 49 highlighted = new HashSet<>(); 50 edgeList = new ArrayList<>(); 51 52 selectionCursor = ImageProvider.getCursor("normal", "selection"); 53 waySelectCursor = ImageProvider.getCursor("normal", "select_way"); 54 } 55 56 /* 57 * given a way, it looks at both directions for good candidates to be added 58 * to the edge 59 */ 60 private List<Way> getEdgeFromWay(Way initial, String modeOfTravel) { 61 List<Way> edge = new ArrayList<>(); 62 if (!isWaySuitableForMode(initial, modeOfTravel)) 63 return edge; 64 65 Way curr = initial; 66 while (true) { 67 List<Way> options = curr.firstNode(true).getParentWays(); 68 options.remove(curr); 69 curr = chooseBestWay(options, modeOfTravel); 70 if (curr == null || edge.contains(curr)) 71 break; 72 edge.add(curr); 73 } 74 75 curr = initial; 76 while (true) { 77 List<Way> options = curr.lastNode(true).getParentWays(); 78 options.remove(curr); 79 curr = chooseBestWay(options, modeOfTravel); 80 if (curr == null || edge.contains(curr)) 81 break; 82 edge.add(curr); 83 } 84 85 edge.add(initial); 86 edge = sortEdgeWays(edge); 87 return edge; 88 } 89 90 private List<Way> sortEdgeWays(List<Way> edge) { 91 List<RelationMember> members = 92 edge.stream() 93 .map(w -> new RelationMember("", w)) 94 .collect(Collectors.toList()); 95 List<RelationMember> sorted = new RelationSorter().sortMembers(members); 96 return sorted.stream() 97 .map(RelationMember::getWay) 98 .collect(Collectors.toList()); 99 } 100 101 private Boolean isWaySuitableForMode(Way toCheck, String modeOfTravel) { 102 if ("bus".equals(modeOfTravel)) 103 return RouteUtils.isWaySuitableForBuses(toCheck); 104 105 return RouteUtils.isWaySuitableForPublicTransport(toCheck); 106 } 107 108 /* 109 * 110 */ 111 private Way chooseBestWay(List<Way> ways, String modeOfTravel) { 112 ways.removeIf(w -> !isWaySuitableForMode(w, modeOfTravel)); 113 if (ways.isEmpty()) 114 return null; 115 if (ways.size() == 1) 116 return ways.get(0); 117 118 Way theChoosenOne = null; 119 120 if ("bus".equals(modeOfTravel)) { 121 122 } 123 if ("tram".equals(modeOfTravel)) { 124 125 } 126 127 return theChoosenOne; 128 } 129 130 private String getModeOfTravel() { 131 //find a way to get the currently opened relation editor and get the 132 //from there the current type of route 133 return "bus"; 134 } 135 136 @Override 39 private static final String MAP_MODE_NAME = "Edge Selection"; 40 private static final long serialVersionUID = 2414977774504904238L; 41 42 private transient Set<Way> highlighted; 43 44 private Cursor selectionCursor; 45 private Cursor waySelectCursor; 46 private List<Way> edgeList; 47 private String modeOfTravel = null; 48 49 public EdgeSelectionAction() { 50 super(tr(MAP_MODE_NAME), "edgeSelection", tr(MAP_MODE_NAME), Shortcut.registerShortcut("mapmode:edge_selection", 51 tr("Mode: {0}", tr(MAP_MODE_NAME)), KeyEvent.VK_K, Shortcut.CTRL), 52 ImageProvider.getCursor("normal", "selection")); 53 highlighted = new HashSet<>(); 54 edgeList = new ArrayList<>(); 55 56 selectionCursor = ImageProvider.getCursor("normal", "selection"); 57 waySelectCursor = ImageProvider.getCursor("normal", "select_way"); 58 } 59 60 /* 61 * given a way, it looks at both directions for good candidates to be added to 62 * the edge 63 */ 64 private List<Way> getEdgeFromWay(Way initial, String modeOfTravel) { 65 List<Way> edge = new ArrayList<>(); 66 if (!isWaySuitableForMode(initial, modeOfTravel)) 67 return edge; 68 69 Way curr = initial; 70 while (true) { 71 List<Way> options = curr.firstNode(true).getParentWays(); 72 if (curr.firstNode().hasTag("public_transport", "stop_position")) { 73 break; 74 } 75 options.remove(curr); 76 curr = chooseBestWay(options, modeOfTravel); 77 if (curr == null || edge.contains(curr)) 78 break; 79 edge.add(curr); 80 } 81 82 curr = initial; 83 while (true) { 84 List<Way> options = curr.lastNode(true).getParentWays(); 85 if (curr.lastNode().hasTag("public_transport", "stop_position")) { 86 break; 87 } 88 options.remove(curr); 89 curr = chooseBestWay(options, modeOfTravel); 90 if (curr == null || edge.contains(curr)) 91 break; 92 edge.add(curr); 93 } 94 95 edge.add(initial); 96 edge = sortEdgeWays(edge); 97 return edge; 98 } 99 100 private List<Way> sortEdgeWays(List<Way> edge) { 101 List<RelationMember> members = edge.stream().map(w -> new RelationMember("", w)).collect(Collectors.toList()); 102 List<RelationMember> sorted = new RelationSorter().sortMembers(members); 103 return sorted.stream().map(RelationMember::getWay).collect(Collectors.toList()); 104 } 105 106 private Boolean isWaySuitableForMode(Way way, String modeOfTravel) { 107 if ("bus".equals(modeOfTravel)) 108 return RouteUtils.isWaySuitableForBuses(way); 109 110 if ("bicycle".equals(modeOfTravel)) 111 return RouteUtils.isWaySuitableForBicycle(way); 112 113 if ("foot".equals(modeOfTravel)) { 114 return way.hasTag("highway", "footway") || !(way.hasKey("highway", "motorway") || way.hasKey("foot", "no") 115 || way.hasKey("foot", "use_sidepath")); 116 } 117 118 // if ("hiking".equals(modeOfTravel)) 119 // return RouteUtils.isWaySuitableForBuses(toCheck); 120 // 121 if ("horse".equals(modeOfTravel)) 122 return true; 123 124 if ("light_rail".equals(modeOfTravel)) 125 return way.hasTag("railway", "light_rail"); 126 127 if ("railway".equals(modeOfTravel)) 128 return way.hasKey("railway"); 129 130 if ("subway".equals(modeOfTravel)) 131 return way.hasTag("railway", "subway"); 132 133 if ("train".equals(modeOfTravel)) 134 return way.hasTag("railway", "rail"); 135 136 if ("tram".equals(modeOfTravel)) 137 return way.hasTag("railway", "tram"); 138 139 if ("trolleybus".equals(modeOfTravel)) { 140 return way.hasTag("trolley_wire", "yes"); 141 } 142 143 return RouteUtils.isWaySuitableForPublicTransport(way); 144 } 145 146 /* 147 * 148 */ 149 private Way chooseBestWay(List<Way> ways, String modeOfTravel) { 150 ways.removeIf(w -> !isWaySuitableForMode(w, modeOfTravel)); 151 if (ways.isEmpty()) 152 return null; 153 if (ways.size() == 1) 154 return ways.get(0); 155 156 Way theChoosenOne = null; 157 158 // if ("bus".equals(modeOfTravel)) { 159 // 160 // } 161 // if ("tram".equals(modeOfTravel)) { 162 // 163 // } 164 165 return theChoosenOne; 166 } 167 168 private String getModeOfTravel(Way initial) { 169 // find a way to get the currently opened relation editor and get the 170 // from there the current type of route 171 List<Layer> layers = MainApplication.getLayerManager().getLayers(); 172 for (Layer layer : layers) { 173 if (layer.getName().equals("pt_assistant layer")) { 174 PTAssistantLayer PTL = (PTAssistantLayer) layer; 175 if (PTL.getModeOfTravel() != null) 176 return PTL.getModeOfTravel(); 177 } 178 } 179 return "bus"; 180 } 181 182 @Override 137 183 public void mouseClicked(MouseEvent e) { 138 184 139 DataSet ds = MainApplication.getLayerManager().getEditDataSet(); 140 Way initial = MainApplication.getMap().mapView.getNearestWay(e.getPoint(), OsmPrimitive::isUsable); 141 updateKeyModifiers(e); 142 if(!shift && !ctrl) { 143 /* 144 * remove all previous selection and just add the latest selection 145 */ 146 edgeList.clear(); 147 ds.clearSelection(); 148 if (initial != null) { 149 List<Way> edge = getEdgeFromWay(initial, getModeOfTravel()); 150 for(Way way:edge) { 151 if (!edgeList.contains(way)) 152 edgeList.add(way); 153 } 154 edgeList.addAll(edge); 155 ds.setSelected(edgeList); 156 AutoScaleAction.zoomTo( 157 edge.stream() 158 .map(w -> (OsmPrimitive) w) 159 .collect(Collectors.toList())); 160 } 161 162 } else if (!shift && ctrl) { 163 /* 164 * toggle mode where we can individually select and deselect the edges 165 */ 166 List<Way> edge = getEdgeFromWay(initial, getModeOfTravel()); 185 DataSet ds = MainApplication.getLayerManager().getEditDataSet(); 186 Way initial = MainApplication.getMap().mapView.getNearestWay(e.getPoint(), OsmPrimitive::isUsable); 187 188 updateKeyModifiers(e); 189 190 if (!shift && !ctrl) { 191 /* 192 * remove all previous selection and just add the latest selection 193 */ 194 edgeList.clear(); 195 ds.clearSelection(); 196 if (initial != null) { 197 modeOfTravel = getModeOfTravel(initial); 198 List<Way> edge = getEdgeFromWay(initial, modeOfTravel); 199 for (Way way : edge) { 200 if (!edgeList.contains(way)) 201 edgeList.add(way); 202 } 203 edgeList.addAll(edge); 204 ds.setSelected(edgeList); 205 AutoScaleAction.zoomTo(edge.stream().map(w -> (OsmPrimitive) w).collect(Collectors.toList())); 206 } 207 208 } else if (!shift && ctrl && initial != null) { 209 /* 210 * toggle mode where we can individually select and deselect the edges 211 */ 212 if (edgeList.size() == 0 || modeOfTravel == null) { 213 modeOfTravel = getModeOfTravel(initial); 214 } 215 216 List<Way> edge = getEdgeFromWay(initial, modeOfTravel); 217 List<Way> newEdges = new ArrayList<>(); 167 218 if (edgeList.containsAll(edge)) { 168 System.out.println(edgeList.size()); 169 for(Way way:edge) { 170 if (edgeList.contains(way)) 171 edgeList.remove(way); 172 } 219 for (Way way : edge) { 220 if (edgeList.contains(way)) 221 edgeList.remove(way); 222 } 173 223 } else { 174 for(Way way:edge) { 175 if (!edgeList.contains(way)) 176 edgeList.add(way); 177 } 224 for (Way way : edge) { 225 if (!edgeList.contains(way)) { 226 edgeList.add(way); 227 newEdges.addAll(findNewEdges(way, edge, edgeList)); 228 } 229 } 230 if (newEdges != null) { 231 System.out.println("new"+newEdges.size()); 232 List<Way> waysToBeRemoved = waysToBeRemoved(newEdges); 233 if (waysToBeRemoved != null) { 234 newEdges.removeAll(waysToBeRemoved); 235 } 236 edgeList.addAll(newEdges); 237 } 178 238 } 179 239 ds.clearSelection(); 180 240 ds.setSelected(edgeList); 181 AutoScaleAction.zoomTo( 182 edge.stream() 183 .map(w -> (OsmPrimitive) w) 184 .collect(Collectors.toList()) 185 ); 186 } else if (shift && !ctrl) { 187 /* 188 * add new selection to existing edges 189 */ 190 if (initial != null) { 191 List<Way> edge = getEdgeFromWay(initial, getModeOfTravel()); 192 for(Way way:edge) { 193 if (!edgeList.contains(way)) 194 edgeList.add(way); 195 } 196 edgeList.addAll(edge); 197 ds.setSelected(edgeList); 198 AutoScaleAction.zoomTo( 199 edge.stream() 200 .map(w -> (OsmPrimitive) w) 201 .collect(Collectors.toList())); 202 } 203 } 204 205 } 206 207 @Override 208 public void mouseMoved(MouseEvent e) { 209 super.mouseMoved(e); 210 211 for (Way way : highlighted) { 212 way.setHighlighted(false); 213 } 214 highlighted.clear(); 215 216 Way initial = MainApplication.getMap().mapView.getNearestWay(e.getPoint(), OsmPrimitive::isUsable); 217 if (initial == null) { 218 MainApplication.getMap().mapView.setCursor(selectionCursor); 219 } else { 220 MainApplication.getMap().mapView.setCursor(waySelectCursor); 221 highlighted.addAll(getEdgeFromWay(initial, getModeOfTravel())); 222 } 223 224 for (Way way : highlighted) { 225 way.setHighlighted(true); 226 } 227 } 228 229 @Override 230 public void enterMode() { 231 super.enterMode(); 232 MainApplication.getMap().mapView.addMouseListener(this); 233 MainApplication.getMap().mapView.addMouseMotionListener(this); 234 } 235 236 @Override 237 public void exitMode() { 238 super.exitMode(); 239 MainApplication.getMap().mapView.removeMouseListener(this); 240 MainApplication.getMap().mapView.removeMouseMotionListener(this); 241 } 241 AutoScaleAction.zoomTo(edge.stream().map(w -> (OsmPrimitive) w).collect(Collectors.toList())); 242 } else if (shift && !ctrl && initial != null) { 243 /* 244 * add new selection to existing edges 245 */ 246 if (edgeList.size() == 0 || modeOfTravel == null) { 247 modeOfTravel = getModeOfTravel(initial); 248 } 249 if (initial != null) { 250 List<Way> edge = getEdgeFromWay(initial, modeOfTravel); 251 List<Way> newEdges = new ArrayList<>(); 252 253 for (Way way : edge) { 254 if (!edgeList.contains(way)) { 255 edgeList.add(way); 256 newEdges.addAll(findNewEdges(way, edge, edgeList)); 257 } 258 } 259 260 if (newEdges != null) { 261 System.out.println("new"+newEdges.size()); 262 List<Way> waysToBeRemoved = waysToBeRemoved(newEdges); 263 if (waysToBeRemoved != null) { 264 newEdges.removeAll(waysToBeRemoved); 265 } 266 edgeList.addAll(newEdges); 267 } 268 269 ds.setSelected(edgeList); 270 AutoScaleAction.zoomTo(edge.stream().map(w -> (OsmPrimitive) w).collect(Collectors.toList())); 271 } 272 } 273 274 } 275 276 private List<Way> waysToBeRemoved(List<Way> newEdges) { 277 278 List<Way> waysToBeRemoved = new ArrayList<>(); 279 280 for (int i = 0; i < newEdges.size(); i++) { 281 Node node1 = newEdges.get(i).firstNode(); 282 Node node2 = newEdges.get(i).lastNode(); 283 for (int j = i + 1; j < newEdges.size(); j++) { 284 if (newEdges.get(i).equals(newEdges.get(j))) 285 continue; 286 Node node3 = newEdges.get(j).firstNode(); 287 Node node4 = newEdges.get(j).lastNode(); 288 289 if (node1.equals(node3) && node2.equals(node4)) { 290 if (!waysToBeRemoved.contains(newEdges.get(i))) 291 waysToBeRemoved.add(newEdges.get(i)); 292 if (!waysToBeRemoved.contains(newEdges.get(j))) 293 waysToBeRemoved.add(newEdges.get(j)); 294 295 } else if (node1.equals(node4) && node2.equals(node3)) { 296 if (!waysToBeRemoved.contains(newEdges.get(i))) 297 waysToBeRemoved.add(newEdges.get(i)); 298 if (!waysToBeRemoved.contains(newEdges.get(j))) 299 waysToBeRemoved.add(newEdges.get(j)); 300 } 301 } 302 } 303 System.out.println("remove"+waysToBeRemoved.size()); 304 return waysToBeRemoved; 305 } 306 307 private List<Way> findNewEdges(Way way, List<Way> edge, List<Way> edgeList) { 308 List<Way> newEdges = new ArrayList<>(); 309 310 Node firstNode = way.firstNode(); 311 Node lastNode = way.lastNode(); 312 313 List<Way> parentWayList1 = firstNode.getParentWays(); 314 parentWayList1.removeAll(edgeList); 315 parentWayList1.removeAll(edge); 316 317 List<Way> parentWayList2 = lastNode.getParentWays(); 318 parentWayList2.removeAll(edgeList); 319 parentWayList2.removeAll(edge); 320 321 parentWayList1.addAll(parentWayList2); 322 323 for (Way parentWay : parentWayList1) { 324 if (edge.contains(parentWay) || edgeList.contains(parentWay)) 325 continue; 326 327 Node node1 = parentWay.firstNode(); 328 Node node2 = parentWay.lastNode(); 329 for (Way oldWay : edgeList) { 330 if (!oldWay.equals(way)) { 331 if ((oldWay.containsNode(node1) && !way.containsNode(node1)) 332 || (oldWay.containsNode(node2) && !way.containsNode(node2))) { 333 newEdges.add(parentWay); 334 } 335 336 } 337 } 338 } 339 return newEdges; 340 } 341 342 @Override 343 public void mouseMoved(MouseEvent e) { 344 super.mouseMoved(e); 345 346 for (Way way : highlighted) { 347 way.setHighlighted(false); 348 } 349 highlighted.clear(); 350 351 Way initial = MainApplication.getMap().mapView.getNearestWay(e.getPoint(), OsmPrimitive::isUsable); 352 if (initial == null) { 353 MainApplication.getMap().mapView.setCursor(selectionCursor); 354 } else { 355 MainApplication.getMap().mapView.setCursor(waySelectCursor); 356 highlighted.addAll(getEdgeFromWay(initial, modeOfTravel)); 357 } 358 359 for (Way way : highlighted) { 360 way.setHighlighted(true); 361 } 362 } 363 364 @Override 365 public void enterMode() { 366 super.enterMode(); 367 MainApplication.getMap().mapView.addMouseListener(this); 368 MainApplication.getMap().mapView.addMouseMotionListener(this); 369 } 370 371 @Override 372 public void exitMode() { 373 super.exitMode(); 374 MainApplication.getMap().mapView.removeMouseListener(this); 375 MainApplication.getMap().mapView.removeMouseMotionListener(this); 376 } 242 377 243 378 }
Note:
See TracChangeset
for help on using the changeset viewer.