Changeset 26119 in osm for applications/editors/josm/plugins/routing/src
- Timestamp:
- 2011-06-11T04:15:31+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/routing/src/com/innovant/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingGraph.java
r18415 r26119 49 49 * @author Juangui 50 50 * @author Jose Vidal 51 * @author Hassan S 51 52 */ 52 53 public class RoutingGraph { … … 99 100 private RoutingGraphDelegator rgDelegator=null; 100 101 102 103 /** 104 * Graph getter 105 */ 106 public Graph<Node, OsmEdge> getGraph(){ 107 return graph; 108 109 } 110 111 112 private void addEdgeBidirectional( Way way, Node from, Node to){ 113 addEdge(way,from,to); 114 addEdge(way,to,from); 115 } 116 117 private void addEdgeReverseOneway( Way way, Node from, Node to){ 118 addEdge(way,to,from); 119 } 120 121 private void addEdgeNormalOneway( Way way, Node from, Node to){ 122 addEdge(way,from,to); 123 } 124 101 125 /** 102 126 * Speeds … … 131 155 // iterate all ways and segments for all nodes: 132 156 for (Way way : data.getWays()) { 133 if (way != null && !way.isDeleted() && this.isvalidWay(way)) { 134 Node from = null; 135 for (Node to : way.getNodes()) { 136 // Ignore the node if deleted 137 if (!to.isDeleted()) { 138 graph.addVertex(to); 139 if (from != null) { 140 addEdge(way, from, to); 141 if (!isOneWay(way)){ 142 addEdge(way, to, from);} 143 } 144 from = to; 145 } 146 } 147 } 148 } 149 // graph.vertexSet().size(); 157 158 // skip way if not suitable for routing. 159 if (way != null && !way.isDeleted() && this.isvalidWay(way) 160 && way.getNodes().size() > 1) continue; 161 162 // INIT 163 Node from = null; 164 Node to = null; 165 List<Node> nodes = way.getNodes(); 166 int nodes_count = nodes.size(); 167 168 /* 169 * Assume node is A B C D E. The procedure should be 170 * 171 * case 1 - bidirectional ways: 172 * 1) Add vertex A B C D E 173 * 2) Link A<->B, B<->C, C<->D, D<->E as Edges 174 * 175 * case 2 - oneway reverse: 176 * 1) Add vertex A B C D E 177 * 2) Link B->A,C->B,D->C,E->D as Edges. result: A<-B<-C<-D<-E 178 * 179 * case 3 - oneway normal: 180 * 1) Add vertex A B C D E 181 * 2) Link A->B, B->C, C->D, D->E as Edges. result: A->B->C->D->E 182 * 183 * 184 */ 185 186 String oneway_val = way.get("oneway"); /* get (oneway=?) tag for this way. */ 187 188 from = nodes.get(0); /* 1st node A */ 189 graph.addVertex(from); /* add vertex A */ 190 191 for (int i = 1; i < nodes_count; i++) { /* loop from B until E */ 192 193 to = nodes.get(i); /* 2nd node B */ 194 195 if (to != null && !to.isDeleted()) { 196 graph.addVertex(to); /* add vertex B */ 197 198 199 //this is where we link the vertices 200 if (oneway_val == null || oneway_val == "false" || oneway_val == "no" || oneway_val == "0") { 201 //Case 1 (bi-way): oneway=false OR oneway=unset OR oneway=0 OR oneway=no 202 addEdgeBidirectional(way, from, to); 203 204 } else if (oneway_val == "-1") { 205 //Case 2 (oneway reverse): oneway=-1 206 addEdgeReverseOneway(way, from, to); 207 208 } else if (oneway_val == "1" || oneway_val == "yes" || oneway_val == "true") { 209 //Case 3 (oneway normal): oneway=yes OR 1 OR true 210 addEdgeNormalOneway(way, from, to); 211 212 } 213 214 from = to; /* we did A<->B, next loop we will do B<->C, so from=B,to=C for next loop. */ 215 } 216 217 } // end of looping thru nodes 218 } // end of looping thru ways 219 150 220 logger.debug("End Create Graph"); 151 221 logger.debug("Vertex: "+graph.vertexSet().size()); … … 225 295 226 296 /** 227 * Check is One Way.228 *229 * @param way230 * the way.231 * @return <code>true</code> is a one way. <code>false</code> is not a one232 * way.233 */234 private boolean isOneWay(Way way) {235 // FIXME: oneway=-1 is ignored for the moment!236 return way.get("oneway") != null237 || "motorway".equals(way.get("highway"));238 }239 240 /**241 297 * Check if a Way is correct. 242 298 * … … 246 302 */ 247 303 public boolean isvalidWay(Way way) { 248 if (!way.isTagged()) 249 return false; 304 //if (!way.isTagged()) <---not needed me thinks 305 // return false; 250 306 251 307 return way.get("highway") != null || way.get("junction") != null 252 308 || way.get("service") != null; 253 309 254 }255 256 public boolean isvalidNode(Node node) {257 return true;258 310 } 259 311 … … 295 347 DijkstraShortestPath<Node, OsmEdge> routingk = null; 296 348 for (int index = 1; index < nodes.size(); ++index) { 297 routingk = new DijkstraShortestPath<Node, OsmEdge>( rgDelegator, nodes349 routingk = new DijkstraShortestPath<Node, OsmEdge>(g, nodes 298 350 .get(index - 1), nodes.get(index)); 299 351 if (routingk.getPathEdgeList() == null) { -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingLayer.java
r23561 r26119 33 33 import java.awt.Graphics2D; 34 34 import java.awt.Point; 35 import java.awt.RenderingHints; 35 36 import java.awt.Stroke; 36 37 import java.util.ArrayList; 37 38 import java.util.Collection; 38 39 import java.util.List; 40 import java.util.Set; 39 41 40 42 import javax.swing.Action; … … 106 108 this.routingModel = new RoutingModel(dataLayer.data); 107 109 logger.debug("Routing Layer created."); 110 111 112 this.routingModel.routingGraph.createGraph(); /* construct the graph right after we we create the layer */ 113 Main.map.repaint(); /* update MapView */ 108 114 } 109 115 … … 139 145 140 146 Point P = Main.map.mapView.getPoint(n); 141 double dist = p.distance Sq(P);147 double dist = p.distance(P); 142 148 if (dist < snapDistance) { 143 149 if ((nearest == null) || (dist < minDist)) { … … 231 237 // Get routing nodes (start, middle, end) 232 238 List<Node> nodes = routingModel.getSelectedNodes(); 233 if(nodes == null || nodes.size() == 0) {234 logger.debug("no nodes selected");235 return;236 }237 239 238 240 // Get path stroke color from preferences … … 259 261 String widthString = Main.pref.get(PreferencesKeys.KEY_ROUTE_WIDTH.key); 260 262 if (widthString.length() == 0) { 261 widthString = " 8";263 widthString = "2"; /* I think 2 is better */ 262 264 // FIXME add after good width is found: Main.pref.put(KEY_ROUTE_WIDTH, widthString); 263 265 } 264 266 int width = Integer.parseInt(widthString); 265 267 268 269 // draw our graph 270 if (isActiveLayer) { 271 if(routingModel != null) { 272 if(routingModel.routingGraph != null && routingModel.routingGraph.getGraph() != null) { 273 Color color2 = ColorHelper.html2color("#00ff00"); /* just green for now */ 274 Set<OsmEdge> graphEdges = routingModel.routingGraph.getGraph().edgeSet(); 275 OsmEdge firstedge = (OsmEdge) graphEdges.toArray()[0]; 276 Point from = mv.getPoint(firstedge.fromEastNorth()); 277 g.drawRect(from.x-4, from.y+4, from.x+4, from.y-4); 278 for(OsmEdge edge : graphEdges) { 279 drawGraph(g, mv, edge, color2, width); 280 } 281 } 282 } 283 } 284 285 286 if(nodes == null || nodes.size() == 0) return; 287 266 288 // Paint routing path 267 289 List<OsmEdge> routeEdges = routingModel.getRouteEdges(); … … 327 349 328 350 Graphics2D g2d = (Graphics2D)g; 351 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Anti-alias! 329 352 Stroke oldStroke = g2d.getStroke(); 330 353 g2d.setStroke(new BasicStroke(width)); // thickness 331 g.drawLine(from.x, from.y, to.x, to.y); 354 g2d.drawLine(from.x, from.y, to.x, to.y); 332 355 if (showDirection) { 333 356 double t = Math.atan2(to.y-from.y, to.x-from.x) + Math.PI; … … 337 360 g2d.setStroke(oldStroke); 338 361 } 339 362 private void drawGraph(Graphics g, MapView mv, OsmEdge edge, Color col, int width) { 363 g.setColor(col); 364 Point from; 365 Point to; 366 from = mv.getPoint(edge.fromEastNorth()); 367 to = mv.getPoint(edge.toEastNorth()); 368 369 Graphics2D g2d = (Graphics2D)g; 370 Stroke oldStroke = g2d.getStroke(); 371 g2d.setStroke(new BasicStroke(width)); // thickness 372 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); // Anti-alias! 373 g2d.drawLine(from.x, from.y, to.x, to.y); 374 g2d.drawRect(to.x- 4, to.y+4, 4, 4); 375 376 g2d.setStroke(oldStroke); 377 } 378 340 379 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingPlugin.java
r19475 r26119 35 35 import org.apache.log4j.xml.DOMConfigurator; 36 36 import org.openstreetmap.josm.Main; 37 import org.openstreetmap.josm.data.osm.event.DataChangedEvent; 38 import org.openstreetmap.josm.data.osm.DataSet; 39 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; 40 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent.DatasetEventType; 41 import org.openstreetmap.josm.data.osm.event.DataSetListener; 42 import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter; 43 import org.openstreetmap.josm.data.osm.event.DatasetEventManager; 44 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 37 45 import org.openstreetmap.josm.gui.IconToggleButton; 38 46 import org.openstreetmap.josm.gui.MapFrame; … … 60 68 * @version 0.3 61 69 */ 62 public class RoutingPlugin extends Plugin implements LayerChangeListener { 70 public class RoutingPlugin extends Plugin implements LayerChangeListener,DataSetListenerAdapter.Listener { 63 71 /** 64 72 * Logger … … 123 131 */ 124 132 private static RoutingPlugin plugin; 133 134 private DataSetListenerAdapter datasetAdapter; 125 135 126 136 /** … … 129 139 public RoutingPlugin(PluginInformation info) { 130 140 super(info); 141 142 datasetAdapter = new DataSetListenerAdapter(this); 131 143 plugin = this; // Assign reference to the plugin class 132 144 DOMConfigurator.configure("log4j.xml"); … … 141 153 // Register this class as LayerChangeListener 142 154 MapView.addLayerChangeListener(this); 155 DatasetEventManager.getInstance().addDatasetListener(datasetAdapter, FireMode.IN_EDT_CONSOLIDATED); 143 156 logger.debug("Finished loading plugin"); 144 157 } … … 203 216 */ 204 217 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 205 routingDialog.refresh(); 218 219 if (newLayer instanceof RoutingLayer) { /* show Routing toolbar and dialog window */ 220 addRouteNodeButton.setVisible(true); 221 removeRouteNodeButton.setVisible(true); 222 moveRouteNodeButton.setVisible(true); 223 menu.enableRestOfItems(); 224 routingDialog.showDialog(); 225 routingDialog.refresh(); 226 }else{ /* hide Routing toolbar and dialog window */ 227 addRouteNodeButton.setVisible(false); 228 removeRouteNodeButton.setVisible(false); 229 moveRouteNodeButton.setVisible(false); 230 menu.disableRestOfItems(); 231 routingDialog.hideDialog(); 232 } 206 233 } 207 234 … … 254 281 routingDialog.refresh(); 255 282 } 256 283 284 public void processDatasetEvent(AbstractDatasetChangedEvent event){ 285 286 287 } 257 288 /* (non-Javadoc) 258 289 * @see org.openstreetmap.josm.plugins.Plugin#getPreferenceSetting() -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingMenu.java
r25843 r26119 67 67 private JMenuItem reverseMI; 68 68 private JMenuItem clearMI; 69 private JMenuItem regraphMI; 69 70 private JMenu criteriaM; 70 71 private JMenu menu; … … 166 167 }); 167 168 menu.add(clearMI); 169 170 regraphMI = new JMenuItem(tr("Reconstruct Graph")); 171 regraphMI.addActionListener(new ActionListener() { 172 public void actionPerformed(ActionEvent e) { 173 174 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 175 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 176 RoutingModel routingModel = layer.getRoutingModel(); 177 routingModel.routingGraph.resetGraph(); 178 routingModel.routingGraph.createGraph(); 179 } 180 181 } 182 }); 183 menu.add(regraphMI); 184 168 185 169 186 // Initially disabled
Note:
See TracChangeset
for help on using the changeset viewer.