Changeset 28138 in osm for applications
- Timestamp:
- 2012-03-22T21:18:08+01:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/routing/src/com/innovant/josm
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingGraph.java
r27952 r28138 36 36 import org.jgrapht.alg.BellmanFordShortestPath; 37 37 import org.jgrapht.alg.DijkstraShortestPath; 38 import org.jgrapht.graph.AsUndirectedGraph;39 38 import org.jgrapht.graph.DirectedWeightedMultigraph; 39 import org.openstreetmap.josm.Main; 40 40 import org.openstreetmap.josm.data.osm.DataSet; 41 41 import org.openstreetmap.josm.data.osm.Node; … … 43 43 44 44 import com.innovant.josm.jrt.osm.OsmEdge; 45 import com.innovant.josm.plugin.routing.RoutingLayer; 46 import com.innovant.josm.plugin.routing.RoutingModel; 45 47 46 48 /** … … 53 55 public class RoutingGraph { 54 56 55 56 57 58 privateRoutingProfile routingProfile;59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 privateDataSet data;81 82 83 84 85 86 87 88 89 90 91 92 // public boolean graphState;93 94 95 96 97 // private DirectedWeightedMultigraph<Node, OsmEdge> graph;98 // private WeightedMultigraph<Node, OsmEdge> graph;99 100 101 102 103 104 105 106 107 108 109 110 111 57 /** 58 * Routing Profile 59 */ 60 private final RoutingProfile routingProfile; 61 62 /** 63 * Diferent algorithms to apply to the graph. 64 */ 65 public enum Algorithm { 66 ROUTING_ALG_DIJKSTRA, ROUTING_ALG_BELLMANFORD 67 }; 68 69 /** 70 * Search criteria for the route. 71 */ 72 public enum RouteType {FASTEST,SHORTEST}; 73 74 /** 75 * 76 */ 77 private RouteType routeType; 78 79 /** 80 * Associated Osm DataSet 81 */ 82 private final DataSet data; 83 84 /** 85 * Logger. 86 */ 87 static Logger logger = Logger.getLogger(RoutingGraph.class); 88 89 /** 90 * Graph state 91 * <code>true</code> Graph in memory. 92 * <code>false</code> Graph not created. 93 */ 94 // public boolean graphState; 95 96 /** 97 * OSM Graph. 98 */ 99 // private DirectedWeightedMultigraph<Node, OsmEdge> graph; 100 // private WeightedMultigraph<Node, OsmEdge> graph; 101 private Graph<Node, OsmEdge> graph; 102 private RoutingGraphDelegator rgDelegator=null; 103 104 105 /** 106 * Graph getter 107 */ 108 public Graph<Node, OsmEdge> getGraph(){ 109 return graph; 110 111 } 112 113 112 114 private void addEdgeBidirectional( Way way, Node from, Node to){ 113 114 115 } 116 115 addEdge(way,from,to); 116 addEdge(way,to,from); 117 } 118 117 119 private void addEdgeReverseOneway( Way way, Node from, Node to){ 118 119 } 120 120 addEdge(way,to,from); 121 } 122 121 123 private void addEdgeNormalOneway( Way way, Node from, Node to){ 122 addEdge(way,from,to); 123 } 124 125 /** 126 * Speeds 127 */ 128 private Map<String,Double> waySpeeds; 129 130 /** 131 * Default Constructor. 132 */ 133 public RoutingGraph(DataSet data) { 134 // this.graphState = false; 135 this.graph = null; 136 this.data = data; 137 routeType=RouteType.SHORTEST; 138 routingProfile=new RoutingProfile("default"); 139 routingProfile.setOnewayUse(true); // Don't ignore oneways by default 140 this.setWaySpeeds(routingProfile.getWaySpeeds()); 141 logger.debug("Created RoutingGraph"); 142 } 143 144 /** 145 * Create OSM graph for routing 146 * 147 * @return 148 */ 149 public void createGraph() { 150 151 logger.debug("Creating Graph..."); 152 graph = new DirectedWeightedMultigraph<Node, OsmEdge>(OsmEdge.class); 153 rgDelegator=new RoutingGraphDelegator(graph); 154 rgDelegator.setRouteType(this.routeType); 155 // iterate all ways and segments for all nodes: 156 for (Way way : data.getWays()) { 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 String junction_val = way.get("junction"); /* get (junction=?) tag for this way. */ 188 189 from = nodes.get(0); /* 1st node A */ 190 graph.addVertex(from); /* add vertex A */ 191 192 for (int i = 1; i < nodes_count; i++) { /* loop from B until E */ 193 194 to = nodes.get(i); /* 2nd node B */ 195 196 if (to != null && !to.isDeleted()) { 197 graph.addVertex(to); /* add vertex B */ 198 199 200 //this is where we link the vertices 201 if (oneway_val == null && junction_val == "roundabout") { 202 //Case (roundabout): oneway=implicit yes 203 addEdgeNormalOneway(way, from, to); 204 205 } else if (oneway_val == null || oneway_val == "false" || oneway_val == "no" || oneway_val == "0") { 206 //Case (bi-way): oneway=false OR oneway=unset OR oneway=0 OR oneway=no 207 addEdgeBidirectional(way, from, to); 208 209 } else if (oneway_val == "-1") { 210 //Case (oneway reverse): oneway=-1 211 addEdgeReverseOneway(way, from, to); 212 213 } else if (oneway_val == "1" || oneway_val == "yes" || oneway_val == "true") { 214 //Case (oneway normal): oneway=yes OR 1 OR true 215 addEdgeNormalOneway(way, from, to); 216 217 } 218 219 from = to; /* we did A<->B, next loop we will do B<->C, so from=B,to=C for next loop. */ 220 } 221 222 } // end of looping thru nodes 223 } // end of looping thru ways 224 225 logger.debug("End Create Graph"); 226 logger.debug("Vertex: "+graph.vertexSet().size()); 227 logger.debug("Edges: "+graph.edgeSet().size()); 228 } 229 230 /** 231 * Compute weight and add edge to the graph 232 * @param way 233 * @param from 234 * @param to 235 */ 236 private void addEdge(Way way,Node from, Node to) { 237 double length = from.getCoor().greatCircleDistance(to.getCoor()); 238 239 OsmEdge edge = new OsmEdge(way, from, to); 240 edge.setSpeed(12.1); 241 graph.addEdge(from, to, edge); 242 // weight = getWeight(way); 243 double weight = getWeight(way, length); 244 setWeight(edge, length); 245 logger.debug("edge for way " + way.getId() 246 + "(from node " + from.getId() + " to node " 247 + to.getId() + ") has weight: " + weight); 248 //((GraphDelegator<Node,OsmEdge>) graph).setEdgeWeight(edge, weight); 249 ((DirectedWeightedMultigraph<Node,OsmEdge>)graph).setEdgeWeight(edge, weight); 250 } 251 252 /** 253 * Set the weight for the given segment depending on the highway type 254 * and the length of the segment. The higher the value, the less it is used 255 * in routing. 256 * 257 * @param way 258 * the way. 259 * @return 260 */ 261 private void setWeight(OsmEdge osmedge, double length) { 262 263 osmedge.setLength(length); 264 if (this.waySpeeds.containsKey(osmedge.getWay().get("highway"))) 265 osmedge.setSpeed(this.waySpeeds.get(osmedge.getWay().get("highway"))); 266 267 } 268 269 /** 270 * Returns the weight for the given segment depending on the highway type 271 * and the length of the segment. The higher the value, the less it is used 272 * in routing. 273 * 274 * @param way 275 * the way. 276 * @return 277 */ 278 private double getWeight(Way way, double length) { 279 // Default speed if no setting is found 280 double speed = 1; 281 282 switch (routeType) { 283 case SHORTEST: 284 // Same speed for all types of ways 285 if (this.waySpeeds.containsKey("residential")) 286 speed=this.waySpeeds.get("residential"); 287 break; 288 case FASTEST: 289 // Each type of way may have a different speed 290 if (this.waySpeeds.containsKey(way.get("highway"))) 291 speed=this.waySpeeds.get(way.get("highway")); 292 logger.debug("Speed="+speed); 293 break; 294 default: 295 break; 296 } 297 // Return the time spent to traverse the way 298 return length / speed; 299 } 300 301 /** 302 * Check if a Way is correct. 303 * 304 * @param way 305 * The way. 306 * @return <code>true</code> is valid. <code>false</code> is not valid. 307 */ 308 public boolean isvalidWay(Way way) { 309 //if (!way.isTagged()) <---not needed me thinks 310 // return false; 311 312 return way.get("highway") != null || way.get("junction") != null 313 || way.get("service") != null; 314 315 } 316 317 /** 318 * Apply selected routing algorithm to the graph. 319 * 320 * @param nodes 321 * Nodes used to calculate path. 322 * @param algorithm 323 * Algorithm used to compute the path, 324 * RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or 325 * RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD 326 * @return new path. 327 */ 328 public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) { 329 List<OsmEdge> path = new ArrayList<OsmEdge>(); 330 Graph<Node,OsmEdge> g; 331 double totalWeight = 0; 332 333 if (graph == null) 334 this.createGraph(); 335 logger.debug("apply algorithm between nodes "); 336 337 for (Node node : nodes) { 338 logger.debug(node.getId()); 339 } 340 logger.debug("-----------------------------------"); 341 342 // Assign the graph or an undirected view of the graph to g, 343 // depending on whether oneway tags are used or not 344 if (routingProfile.isOnewayUsed()) 345 g = graph; 346 else 347 g = new AsUndirectedGraph<Node, OsmEdge>((DirectedWeightedMultigraph<Node,OsmEdge>)graph); 348 //TODO: Problemas no tiene encuenta el tema de oneway. 349 switch (algorithm) { 350 case ROUTING_ALG_DIJKSTRA: 351 logger.debug("Using Dijkstra algorithm"); 352 DijkstraShortestPath<Node, OsmEdge> routingk = null; 353 for (int index = 1; index < nodes.size(); ++index) { 354 routingk = new DijkstraShortestPath<Node, OsmEdge>(g, nodes 355 .get(index - 1), nodes.get(index)); 356 if (routingk.getPathEdgeList() == null) { 357 logger.debug("no path found!"); 358 break; 359 } 360 path.addAll(routingk.getPathEdgeList()); 361 totalWeight += routingk.getPathLength(); 362 } 363 break; 364 case ROUTING_ALG_BELLMANFORD: 365 logger.debug("Using Bellman Ford algorithm"); 366 for (int index = 1; index < nodes.size(); ++index) { 367 path = BellmanFordShortestPath.findPathBetween(rgDelegator, nodes 368 .get(index - 1), nodes.get(index)); 369 if (path == null) { 370 logger.debug("no path found!"); 371 return null; 372 } 373 } 374 break; 375 default: 376 logger.debug("Wrong algorithm"); 377 break; 378 } 379 380 logger.debug("shortest path found: " + path + "\nweight: " 381 + totalWeight); 382 return path; 383 } 384 385 /** 386 * Return the number of vertices. 387 * @return the number of vertices. 388 */ 389 public int getVertexCount(){ 390 int value=0; 391 if (graph!=null) value=graph.vertexSet().size(); 392 return value; 393 } 394 395 /** 396 * Return the number of edges. 397 * @return the number of edges. 398 */ 399 public int getEdgeCount(){ 400 int value=0; 401 if (graph!=null) value=graph.edgeSet().size(); 402 return value; 403 } 404 405 /** 406 * @param routeType the routeType to set 407 */ 408 public void setTypeRoute(RouteType routetype) { 409 this.routeType = routetype; 410 this.rgDelegator.setRouteType(routetype); 411 } 412 413 /** 414 * @return the routeType 415 */ 416 public RouteType getTypeRoute() { 417 return routeType; 418 } 419 420 public Map<String, Double> getWaySpeeds() { 421 return waySpeeds; 422 } 423 424 public void setWaySpeeds(Map<String, Double> waySpeeds) { 425 this.waySpeeds = waySpeeds; 426 } 427 428 public void resetGraph() { 429 graph=null; 430 } 431 432 public RoutingProfile getRoutingProfile() { 433 return routingProfile; 434 } 124 addEdge(way,from,to); 125 } 126 127 /** 128 * Speeds 129 */ 130 private Map<String,Double> waySpeeds; 131 132 /** 133 * Default Constructor. 134 */ 135 public RoutingGraph(DataSet data) { 136 // this.graphState = false; 137 this.graph = null; 138 this.data = data; 139 routeType=RouteType.SHORTEST; 140 routingProfile=new RoutingProfile("default"); 141 routingProfile.setOnewayUse(true); // Don't ignore oneways by default 142 this.setWaySpeeds(routingProfile.getWaySpeeds()); 143 logger.debug("Created RoutingGraph"); 144 } 145 146 /** 147 * Create OSM graph for routing 148 * 149 * @return 150 */ 151 public void createGraph() { 152 153 logger.debug("Creating Graph..."); 154 graph = new DirectedWeightedMultigraph<Node, OsmEdge>(OsmEdge.class); 155 rgDelegator=new RoutingGraphDelegator(graph); 156 rgDelegator.setRouteType(this.routeType); 157 // iterate all ways and segments for all nodes: 158 for (Way way : data.getWays()) { 159 160 // skip way if not suitable for routing. 161 if (way == null || way.isDeleted() || !this.isvalidWay(way) 162 || way.getNodes().size() < 1) continue; 163 164 // INIT 165 Node from = null; 166 Node to = null; 167 List<Node> nodes = way.getNodes(); 168 int nodes_count = nodes.size(); 169 170 /* 171 * Assume node is A B C D E. The procedure should be 172 * 173 * case 1 - bidirectional ways: 174 * 1) Add vertex A B C D E 175 * 2) Link A<->B, B<->C, C<->D, D<->E as Edges 176 * 177 * case 2 - oneway reverse: 178 * 1) Add vertex A B C D E 179 * 2) Link B->A,C->B,D->C,E->D as Edges. result: A<-B<-C<-D<-E 180 * 181 * case 3 - oneway normal: 182 * 1) Add vertex A B C D E 183 * 2) Link A->B, B->C, C->D, D->E as Edges. result: A->B->C->D->E 184 * 185 * 186 */ 187 188 String oneway_val = way.get("oneway"); /* get (oneway=?) tag for this way. */ 189 String junction_val = way.get("junction"); /* get (junction=?) tag for this way. */ 190 191 from = nodes.get(0); /* 1st node A */ 192 graph.addVertex(from); /* add vertex A */ 193 194 for (int i = 1; i < nodes_count; i++) { /* loop from B until E */ 195 196 to = nodes.get(i); /* 2nd node B */ 197 198 if (to != null && !to.isDeleted()) { 199 graph.addVertex(to); /* add vertex B */ 200 201 202 //this is where we link the vertices 203 if (!routingProfile.isOnewayUsed()) { 204 //"Ignore oneways" is selected 205 addEdgeBidirectional(way, from, to); 206 207 } else if (oneway_val == null && junction_val == "roundabout") { 208 //Case (roundabout): oneway=implicit yes 209 addEdgeNormalOneway(way, from, to); 210 211 } else if (oneway_val == null || oneway_val == "false" || oneway_val == "no" || oneway_val == "0") { 212 //Case (bi-way): oneway=false OR oneway=unset OR oneway=0 OR oneway=no 213 addEdgeBidirectional(way, from, to); 214 215 } else if (oneway_val == "-1") { 216 //Case (oneway reverse): oneway=-1 217 addEdgeReverseOneway(way, from, to); 218 219 } else if (oneway_val == "1" || oneway_val == "yes" || oneway_val == "true") { 220 //Case (oneway normal): oneway=yes OR 1 OR true 221 addEdgeNormalOneway(way, from, to); 222 223 } 224 225 from = to; /* we did A<->B, next loop we will do B<->C, so from=B,to=C for next loop. */ 226 } 227 228 } // end of looping thru nodes 229 } // end of looping thru ways 230 231 logger.debug("End Create Graph"); 232 logger.debug("Vertex: "+graph.vertexSet().size()); 233 logger.debug("Edges: "+graph.edgeSet().size()); 234 } 235 236 /** 237 * Compute weight and add edge to the graph 238 * @param way 239 * @param from 240 * @param to 241 */ 242 private void addEdge(Way way,Node from, Node to) { 243 double length = from.getCoor().greatCircleDistance(to.getCoor()); 244 245 OsmEdge edge = new OsmEdge(way, from, to); 246 edge.setSpeed(12.1); 247 graph.addEdge(from, to, edge); 248 // weight = getWeight(way); 249 double weight = getWeight(way, length); 250 setWeight(edge, length); 251 logger.debug("edge for way " + way.getId() 252 + "(from node " + from.getId() + " to node " 253 + to.getId() + ") has weight: " + weight); 254 //((GraphDelegator<Node,OsmEdge>) graph).setEdgeWeight(edge, weight); 255 ((DirectedWeightedMultigraph<Node,OsmEdge>)graph).setEdgeWeight(edge, weight); 256 } 257 258 /** 259 * Set the weight for the given segment depending on the highway type 260 * and the length of the segment. The higher the value, the less it is used 261 * in routing. 262 * 263 * @param way 264 * the way. 265 * @return 266 */ 267 private void setWeight(OsmEdge osmedge, double length) { 268 269 osmedge.setLength(length); 270 if (this.waySpeeds.containsKey(osmedge.getWay().get("highway"))) 271 osmedge.setSpeed(this.waySpeeds.get(osmedge.getWay().get("highway"))); 272 273 } 274 275 /** 276 * Returns the weight for the given segment depending on the highway type 277 * and the length of the segment. The higher the value, the less it is used 278 * in routing. 279 * 280 * @param way 281 * the way. 282 * @return 283 */ 284 private double getWeight(Way way, double length) { 285 // Default speed if no setting is found 286 double speed = 1; 287 288 switch (routeType) { 289 case SHORTEST: 290 // Same speed for all types of ways 291 if (this.waySpeeds.containsKey("residential")) 292 speed=this.waySpeeds.get("residential"); 293 break; 294 case FASTEST: 295 // Each type of way may have a different speed 296 if (this.waySpeeds.containsKey(way.get("highway"))) 297 speed=this.waySpeeds.get(way.get("highway")); 298 logger.debug("Speed="+speed); 299 break; 300 default: 301 break; 302 } 303 // Return the time spent to traverse the way 304 return length / speed; 305 } 306 307 /** 308 * Check if a Way is correct. 309 * 310 * @param way 311 * The way. 312 * @return <code>true</code> is valid. <code>false</code> is not valid. 313 */ 314 public boolean isvalidWay(Way way) { 315 //if (!way.isTagged()) <---not needed me thinks 316 // return false; 317 318 return way.get("highway") != null || way.get("junction") != null 319 || way.get("service") != null; 320 321 } 322 323 /** 324 * Apply selected routing algorithm to the graph. 325 * 326 * @param nodes 327 * Nodes used to calculate path. 328 * @param algorithm 329 * Algorithm used to compute the path, 330 * RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or 331 * RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD 332 * @return new path. 333 */ 334 public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) { 335 List<OsmEdge> path = new ArrayList<OsmEdge>(); 336 Graph<Node,OsmEdge> g; 337 double totalWeight = 0; 338 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 339 RoutingModel routingModel = layer.getRoutingModel(); 340 341 if (graph == null || routingModel.getOnewayChanged()) 342 this.createGraph(); 343 logger.debug("apply algorithm between nodes "); 344 345 for (Node node : nodes) { 346 logger.debug(node.getId()); 347 } 348 logger.debug("-----------------------------------"); 349 350 // Assign the graph to g 351 g = graph; 352 353 switch (algorithm) { 354 case ROUTING_ALG_DIJKSTRA: 355 logger.debug("Using Dijkstra algorithm"); 356 DijkstraShortestPath<Node, OsmEdge> routingk = null; 357 for (int index = 1; index < nodes.size(); ++index) { 358 routingk = new DijkstraShortestPath<Node, OsmEdge>(g, nodes 359 .get(index - 1), nodes.get(index)); 360 if (routingk.getPathEdgeList() == null) { 361 logger.debug("no path found!"); 362 break; 363 } 364 path.addAll(routingk.getPathEdgeList()); 365 totalWeight += routingk.getPathLength(); 366 } 367 break; 368 case ROUTING_ALG_BELLMANFORD: 369 logger.debug("Using Bellman Ford algorithm"); 370 for (int index = 1; index < nodes.size(); ++index) { 371 path = BellmanFordShortestPath.findPathBetween(rgDelegator, nodes 372 .get(index - 1), nodes.get(index)); 373 if (path == null) { 374 logger.debug("no path found!"); 375 return null; 376 } 377 } 378 break; 379 default: 380 logger.debug("Wrong algorithm"); 381 break; 382 } 383 384 logger.debug("shortest path found: " + path + "\nweight: " 385 + totalWeight); 386 return path; 387 } 388 389 /** 390 * Return the number of vertices. 391 * @return the number of vertices. 392 */ 393 public int getVertexCount(){ 394 int value=0; 395 if (graph!=null) value=graph.vertexSet().size(); 396 return value; 397 } 398 399 /** 400 * Return the number of edges. 401 * @return the number of edges. 402 */ 403 public int getEdgeCount(){ 404 int value=0; 405 if (graph!=null) value=graph.edgeSet().size(); 406 return value; 407 } 408 409 /** 410 * @param routeType the routeType to set 411 */ 412 public void setTypeRoute(RouteType routetype) { 413 this.routeType = routetype; 414 this.rgDelegator.setRouteType(routetype); 415 } 416 417 /** 418 * @return the routeType 419 */ 420 public RouteType getTypeRoute() { 421 return routeType; 422 } 423 424 public Map<String, Double> getWaySpeeds() { 425 return waySpeeds; 426 } 427 428 public void setWaySpeeds(Map<String, Double> waySpeeds) { 429 this.waySpeeds = waySpeeds; 430 } 431 432 public void resetGraph() { 433 graph=null; 434 } 435 436 public RoutingProfile getRoutingProfile() { 437 return routingProfile; 438 } 435 439 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingModel.java
r15707 r28138 48 48 public class RoutingModel { 49 49 50 51 52 53 50 /** 51 * Logger 52 */ 53 static Logger logger = Logger.getLogger(RoutingModel.class); 54 54 55 56 57 58 55 /** 56 * Graph to calculate route 57 */ 58 public RoutingGraph routingGraph=null; 59 59 60 61 62 63 60 /** 61 * List of nodes that the route has to traverse 62 */ 63 private List<Node> nodes=null; 64 64 65 private List<OsmEdge> path=null; 66 /** 67 * Flag to advise about changes in the selected nodes. 68 */ 69 private boolean changeNodes=false; 70 /** 71 * Default Constructor. 72 */ 73 public RoutingModel(DataSet data) { 74 nodes = new ArrayList<Node>(); 75 System.out.println("gr " + data); 76 routingGraph = new RoutingGraph(data); 77 } 65 private List<OsmEdge> path=null; 78 66 79 /** 80 * Method that returns the selected nodes to calculate route. 81 * @return the selectedNodes 82 */ 83 public List<Node> getSelectedNodes() { 84 return nodes; 85 } 67 /** 68 * Flag to advise about changes in the selected nodes. 69 */ 70 private boolean changeNodes=false; 86 71 87 /** 88 * Adds a node to the route node list. 89 * @param node the node to add. 90 */ 91 public void addNode(Node node) { 92 nodes.add(node); 93 this.changeNodes=true; 94 } 72 /** 73 * Flag to advise about changes in ways. 74 */ 75 private boolean changeOneway=false; 95 76 96 /** 97 * Removes a node from the route node list. 98 * @param index the index of the node to remove. 99 */ 100 public void removeNode(int index) { 101 if (nodes.size()>index) { 102 nodes.remove(index); 103 this.changeNodes=true; 104 } 105 } 77 /** 78 * Default Constructor. 79 */ 80 public RoutingModel(DataSet data) { 81 nodes = new ArrayList<Node>(); 82 System.out.println("gr " + data); 83 routingGraph = new RoutingGraph(data); 84 } 106 85 107 /** 108 * Inserts a node in the route node list. 109 * @param index the index where the node will be inserted 110 * @param node the node to be inserted 111 */ 112 public void insertNode(int index, Node node) { 113 if (nodes.size()>=index) { 114 nodes.add(index, node); 115 this.changeNodes=true; 116 } 117 } 86 /** 87 * Method that returns the selected nodes to calculate route. 88 * @return the selectedNodes 89 */ 90 public List<Node> getSelectedNodes() { 91 return nodes; 92 } 118 93 119 /** 120 * Reverse list of nodes 121 */ 122 public void reverseNodes() { 123 List<Node> aux = new ArrayList<Node>(); 124 for (Node n : nodes) { 125 aux.add(0,n); 126 } 127 nodes = aux; 128 this.changeNodes=true; 129 } 94 /** 95 * Adds a node to the route node list. 96 * @param node the node to add. 97 */ 98 public void addNode(Node node) { 99 nodes.add(node); 100 this.changeNodes=true; 101 } 130 102 131 /** 132 * Get the edges of the route. 133 * @return A list of edges forming the shortest path 134 */ 135 public List<OsmEdge> getRouteEdges() { 136 if (this.changeNodes || path==null) 137 { 138 path=this.routingGraph.applyAlgorithm(nodes, Algorithm.ROUTING_ALG_DIJKSTRA); 139 this.changeNodes=false; 140 } 141 return path; 142 } 103 /** 104 * Removes a node from the route node list. 105 * @param index the index of the node to remove. 106 */ 107 public void removeNode(int index) { 108 if (nodes.size()>index) { 109 nodes.remove(index); 110 this.changeNodes=true; 111 } 112 } 143 113 144 /** 145 * Marks that some node or the node order has changed so the path should be computed again 146 */ 147 public void setNodesChanged() { 148 this.changeNodes = true; 149 } 114 /** 115 * Inserts a node in the route node list. 116 * @param index the index where the node will be inserted 117 * @param node the node to be inserted 118 */ 119 public void insertNode(int index, Node node) { 120 if (nodes.size()>=index) { 121 nodes.add(index, node); 122 this.changeNodes=true; 123 } 124 } 150 125 151 /** 152 * Resets all data. 153 */ 154 public void reset() { 155 nodes.clear(); 156 this.changeNodes=true; 157 } 126 /** 127 * Reverse list of nodes 128 */ 129 public void reverseNodes() { 130 List<Node> aux = new ArrayList<Node>(); 131 for (Node n : nodes) { 132 aux.add(0,n); 133 } 134 nodes = aux; 135 this.changeNodes=true; 136 } 137 138 /** 139 * Get the edges of the route. 140 * @return A list of edges forming the shortest path 141 */ 142 public List<OsmEdge> getRouteEdges() { 143 if (this.changeNodes || path==null) 144 { 145 path=this.routingGraph.applyAlgorithm(nodes, Algorithm.ROUTING_ALG_DIJKSTRA); 146 this.changeNodes=false; 147 this.changeOneway=false; 148 } 149 return path; 150 } 151 152 /** 153 * Marks that some node or the node order has changed so the path should be computed again 154 */ 155 public void setNodesChanged() { 156 this.changeNodes = true; 157 } 158 159 /** 160 * Marks that "Ignore oneway" option has changed so the path should be computed again 161 */ 162 public void setOnewayChanged() { 163 this.changeOneway = true; 164 } 165 166 /** 167 * Marks that "Ignore oneway" option has changed so the path should be computed again 168 */ 169 public boolean getOnewayChanged() { 170 return this.changeOneway; 171 } 172 173 /** 174 * Resets all data. 175 */ 176 public void reset() { 177 nodes.clear(); 178 this.changeNodes=true; 179 } 158 180 159 181 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingPlugin.java
r27894 r28138 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 37 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 38 import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter; 43 39 import org.openstreetmap.josm.data.osm.event.DatasetEventManager; … … 69 65 */ 70 66 public class RoutingPlugin extends Plugin implements LayerChangeListener,DataSetListenerAdapter.Listener { 71 72 73 74 75 76 77 78 79 privateArrayList<RoutingLayer> layers;80 81 82 83 84 privateRoutingDialog routingDialog;85 86 87 88 89 privatePreferenceSetting preferenceSettings;90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 privateRoutingMenu menu;128 129 130 131 132 133 134 privateDataSetListenerAdapter datasetAdapter;135 136 137 138 139 140 141 142 143 144 if (new java.io.File("log4j.xml").exists()) {145 146 147 System.err.println("Routing plugin warning: log4j configuration not found"); 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 menu.enableRestOfItems(); 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 67 /** 68 * Logger 69 */ 70 static Logger logger = Logger.getLogger(RoutingPlugin.class); 71 72 /** 73 * The list of routing layers 74 */ 75 private final ArrayList<RoutingLayer> layers; 76 77 /** 78 * The side dialog where nodes are listed 79 */ 80 private final RoutingDialog routingDialog; 81 82 /** 83 * Preferences Settings Dialog. 84 */ 85 private final PreferenceSetting preferenceSettings; 86 87 /** 88 * MapMode for adding route nodes. 89 * We use this field to enable or disable the mode automatically. 90 */ 91 private AddRouteNodeAction addRouteNodeAction; 92 93 /** 94 * MapMode for removing route nodes. 95 * We use this field to enable or disable the mode automatically. 96 */ 97 private RemoveRouteNodeAction removeRouteNodeAction; 98 99 /** 100 * MapMode for moving route nodes. 101 * We use this field to enable or disable the mode automatically. 102 */ 103 private MoveRouteNodeAction moveRouteNodeAction; 104 105 /** 106 * IconToggleButton for adding route nodes, we use this field to show or hide the button. 107 */ 108 private IconToggleButton addRouteNodeButton; 109 110 /** 111 * IconToggleButton for removing route nodes, we use this field to show or hide the button. 112 */ 113 private IconToggleButton removeRouteNodeButton; 114 115 /** 116 * IconToggleButton for moving route nodes, we use this field to show or hide the button. 117 */ 118 private IconToggleButton moveRouteNodeButton; 119 120 /** 121 * IconToggleButton for moving route nodes, we use this field to show or hide the button. 122 */ 123 private final RoutingMenu menu; 124 125 /** 126 * Reference for the plugin class (as if it were a singleton) 127 */ 128 private static RoutingPlugin plugin; 129 130 private final DataSetListenerAdapter datasetAdapter; 131 132 /** 133 * Default Constructor 134 */ 135 public RoutingPlugin(PluginInformation info) { 136 super(info); 137 138 datasetAdapter = new DataSetListenerAdapter(this); 139 plugin = this; // Assign reference to the plugin class 140 if (new java.io.File("log4j.xml").exists()) { 141 DOMConfigurator.configure("log4j.xml"); 142 } else { 143 System.err.println("Routing plugin warning: log4j configuration not found"); 144 } 145 logger.debug("Loading routing plugin..."); 146 preferenceSettings=new RoutingPreferenceDialog(); 147 // Create side dialog 148 routingDialog = new RoutingDialog(); 149 // Initialize layers list 150 layers = new ArrayList<RoutingLayer>(); 151 // Add menu 152 menu = new RoutingMenu(); 153 // Register this class as LayerChangeListener 154 MapView.addLayerChangeListener(this); 155 DatasetEventManager.getInstance().addDatasetListener(datasetAdapter, FireMode.IN_EDT_CONSOLIDATED); 156 logger.debug("Finished loading plugin"); 157 } 158 159 /** 160 * Provides static access to the plugin instance, to enable access to the plugin methods 161 * @return the instance of the plugin 162 */ 163 public static RoutingPlugin getInstance() { 164 return plugin; 165 } 166 167 /** 168 * Get the routing side dialog 169 * @return The instance of the routing side dialog 170 */ 171 public RoutingDialog getRoutingDialog() { 172 return routingDialog; 173 } 174 175 public void addLayer() { 176 OsmDataLayer osmLayer = Main.map.mapView.getEditLayer(); 177 RoutingLayer layer = new RoutingLayer(tr("Routing") + " [" + osmLayer.getName() + "]", osmLayer); 178 layers.add(layer); 179 Main.main.addLayer(layer); 180 } 181 182 /* 183 * (non-Javadoc) 184 * @see org.openstreetmap.josm.plugins.Plugin#mapFrameInitialized(org.openstreetmap.josm.gui.MapFrame, org.openstreetmap.josm.gui.MapFrame) 185 */ 186 @Override 187 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 188 if(newFrame != null) { 189 // Create plugin map modes 190 addRouteNodeAction = new AddRouteNodeAction(newFrame); 191 removeRouteNodeAction = new RemoveRouteNodeAction(newFrame); 192 moveRouteNodeAction = new MoveRouteNodeAction(newFrame); 193 // Create plugin buttons and add them to the toolbar 194 addRouteNodeButton = new IconToggleButton(addRouteNodeAction); 195 removeRouteNodeButton = new IconToggleButton(removeRouteNodeAction); 196 moveRouteNodeButton = new IconToggleButton(moveRouteNodeAction); 197 addRouteNodeButton.setAutoHideDisabledButton(true); 198 removeRouteNodeButton.setAutoHideDisabledButton(true); 199 moveRouteNodeButton.setAutoHideDisabledButton(true); 200 newFrame.addMapMode(addRouteNodeButton); 201 newFrame.addMapMode(removeRouteNodeButton); 202 newFrame.addMapMode(moveRouteNodeButton); 203 // Enable menu 204 menu.enableStartItem(); 205 newFrame.addToggleDialog(routingDialog); 206 } 207 } 208 209 /* 210 * (non-Javadoc) 211 * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#activeLayerChange(org.openstreetmap.josm.gui.layer.Layer, org.openstreetmap.josm.gui.layer.Layer) 212 */ 213 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 214 if (newLayer instanceof RoutingLayer) { /* show Routing toolbar and dialog window */ 215 menu.enableRestOfItems(); 216 routingDialog.showDialog(); 217 routingDialog.refresh(); 218 }else{ /* hide Routing toolbar and dialog window */ 219 menu.disableRestOfItems(); 220 routingDialog.hideDialog(); 221 } 222 } 223 224 /* 225 * (non-Javadoc) 226 * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerAdded(org.openstreetmap.josm.gui.layer.Layer) 227 */ 228 public void layerAdded(Layer newLayer) { 229 // Add button(s) to the tool bar when the routing layer is added 230 if (newLayer instanceof RoutingLayer) { 231 menu.enableRestOfItems(); 232 // Set layer on top and select layer, also refresh toggleDialog to reflect selection 233 Main.map.mapView.moveLayer(newLayer, 0); 234 logger.debug("Added routing layer."); 235 } 236 } 237 238 /* 239 * (non-Javadoc) 240 * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerRemoved(org.openstreetmap.josm.gui.layer.Layer) 241 */ 242 public void layerRemoved(Layer oldLayer) { 243 if ((oldLayer instanceof RoutingLayer) & (layers.size()==1)) { 244 // Remove button(s) from the tool bar when the last routing layer is removed 245 addRouteNodeButton.setVisible(false); 246 removeRouteNodeButton.setVisible(false); 247 moveRouteNodeButton.setVisible(false); 248 menu.disableRestOfItems(); 249 layers.remove(oldLayer); 250 logger.debug("Removed routing layer."); 251 } else if (oldLayer instanceof OsmDataLayer) { 252 // Remove all associated routing layers 253 // Convert to Array to prevent ConcurrentModificationException when removing layers from ArrayList 254 // FIXME: can't remove associated routing layers without triggering exceptions in some cases 255 RoutingLayer[] layersArray = layers.toArray(new RoutingLayer[0]); 256 for (int i=0;i<layersArray.length;i++) { 257 if (layersArray[i].getDataLayer().equals(oldLayer)) { 258 try { 259 // Remove layer 260 Main.map.mapView.removeLayer(layersArray[i]); 261 } catch (IllegalArgumentException e) { 262 } 263 } 264 } 265 } 266 // Reload RoutingDialog table model 267 routingDialog.refresh(); 268 } 269 270 public void processDatasetEvent(AbstractDatasetChangedEvent event){ 271 272 273 } 274 /* (non-Javadoc) 275 * @see org.openstreetmap.josm.plugins.Plugin#getPreferenceSetting() 276 */ 277 @Override 278 public PreferenceSetting getPreferenceSetting() { 279 return preferenceSettings; 280 } 285 281 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingDialog.java
r27911 r28138 30 30 import static org.openstreetmap.josm.tools.I18n.tr; 31 31 32 import java.awt.BorderLayout;33 32 import java.awt.ComponentOrientation; 34 import java.awt.Font;35 33 import java.awt.event.KeyEvent; 36 34 37 import javax.swing.BorderFactory;38 35 import javax.swing.DefaultListModel; 39 36 import javax.swing.JList; 40 import javax.swing.JPanel;41 37 import javax.swing.JScrollPane; 42 import javax.swing.border.EtchedBorder;43 38 44 39 import org.openstreetmap.josm.Main; … … 57 52 public class RoutingDialog extends ToggleDialog { 58 53 59 private DefaultListModelmodel;60 private JListjList = null;61 54 private final DefaultListModel<String> model; 55 private JList<String> jList = null; 56 private JScrollPane jScrollPane = null; 62 57 63 64 65 66 58 /** 59 * Serial UID 60 */ 61 private static final long serialVersionUID = 8625615652900341987L; 67 62 68 69 70 71 model = new DefaultListModel();72 73 63 public RoutingDialog() { 64 super(tr("Routing"), "routing", tr("Open a list of routing nodes"), 65 Shortcut.registerShortcut("subwindow:routing", tr("Toggle: {0}", tr("Routing")), KeyEvent.VK_R, Shortcut.ALT_CTRL_SHIFT), 150); 66 model = new DefaultListModel<String>(); 67 createLayout(getJScrollPane(), false, null); 68 } 74 69 75 76 77 78 79 80 81 82 83 84 85 86 87 88 70 /** 71 * This method initializes jScrollPane 72 * 73 * @return javax.swing.JScrollPane 74 */ 75 private JScrollPane getJScrollPane() { 76 if (jScrollPane == null) { 77 jScrollPane = new JScrollPane(); 78 jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 79 jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 80 jScrollPane.setViewportView(getJList()); 81 } 82 return jScrollPane; 83 } 89 84 90 91 92 93 94 95 private JListgetJList() {96 97 jList = new JList();98 99 100 101 102 85 /** 86 * This method initializes jList 87 * 88 * @return javax.swing.JList 89 */ 90 private JList<String> getJList() { 91 if (jList == null) { 92 jList = new JList<String>(); 93 jList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 94 jList.setModel(model); 95 } 96 return jList; 97 } 103 98 104 105 106 107 108 109 110 99 /** 100 * Remove item from the list of nodes 101 * @param index 102 */ 103 public void removeNode(int index) { 104 model.remove(index); 105 } 111 106 112 113 114 115 116 117 118 107 /** 108 * Add item to the list of nodes 109 * @param obj 110 */ 111 public void addNode(Node n) { 112 model.addElement(n.getId()+" ["+n.getCoor().toDisplayString()+"]"); 113 } 119 114 120 121 122 123 124 125 126 127 115 /** 116 * Insert item to the list of nodes 117 * @param index 118 * @param obj 119 */ 120 public void insertNode(int index, Node n) { 121 model.insertElementAt(n.getId()+" ["+n.getCoor().toDisplayString()+"]", index); 122 } 128 123 129 130 131 132 133 134 124 /** 125 * Clear list of nodes 126 */ 127 public void clearNodes() { 128 model.clear(); 129 } 135 130 136 137 138 139 140 141 142 143 144 145 131 public void refresh() { 132 clearNodes(); 133 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 134 RoutingLayer routingLayer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 135 RoutingModel routingModel = routingLayer.getRoutingModel(); 136 for (Node n : routingModel.getSelectedNodes()) { 137 addNode(n); 138 } 139 } 140 } 146 141 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingMenu.java
r26119 r28138 59 59 public class RoutingMenu extends JMenu { 60 60 61 /** 62 * Default serial version UID 63 */ 64 private static final long serialVersionUID = 3559922048225708480L; 65 66 private JMenuItem startMI; 67 private JMenuItem reverseMI; 68 private JMenuItem clearMI; 69 private JMenuItem regraphMI; 70 private JMenu criteriaM; 71 private JMenu menu; 72 73 /** 74 * @param s 75 */ 76 public RoutingMenu() { 77 MainMenu mm = Main.main.menu; 78 menu = mm.addMenu(marktr("Routing"), KeyEvent.VK_O, mm.defaultMenuPos, ht("/Plugin/Routing")); 79 80 startMI = new JMenuItem(tr("Add routing layer")); 81 startMI.addActionListener(new ActionListener() { 82 public void actionPerformed(ActionEvent e) { 83 RoutingPlugin.getInstance().addLayer(); 84 } 85 }); 86 menu.add(startMI); 87 88 menu.addSeparator(); 89 ButtonGroup group = new ButtonGroup(); 90 91 criteriaM = new JMenu(tr("Criteria")); 92 93 JRadioButtonMenuItem rshorter = new JRadioButtonMenuItem(tr("Shortest")); 94 rshorter.setSelected(true); 95 rshorter.addItemListener(new ItemListener() { 96 public void itemStateChanged(ItemEvent e) { 97 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 98 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 99 RoutingModel routingModel = layer.getRoutingModel(); 100 if (e.getStateChange()==ItemEvent.SELECTED) { 101 routingModel.routingGraph.setTypeRoute(RouteType.SHORTEST); 102 } else { 103 routingModel.routingGraph.setTypeRoute(RouteType.FASTEST); 104 } 105 // routingModel.routingGraph.resetGraph(); 106 // routingModel.routingGraph.createGraph(); 107 //TODO: Change this way 108 //FIXME: do not change node but recalculate routing. 109 routingModel.setNodesChanged(); 110 Main.map.repaint(); 111 } 112 } 113 114 }); 115 116 JRadioButtonMenuItem rfaster = new JRadioButtonMenuItem(tr("Fastest")); 117 group.add(rshorter); 118 group.add(rfaster); 119 criteriaM.add(rshorter); 120 criteriaM.add(rfaster); 121 122 criteriaM.addSeparator(); 123 JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(tr("Ignore oneways")); 124 cbmi.addItemListener(new ItemListener() { 125 public void itemStateChanged(ItemEvent e) { 126 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 127 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 128 RoutingModel routingModel = layer.getRoutingModel(); 129 if (e.getStateChange()==ItemEvent.SELECTED) 130 routingModel.routingGraph.getRoutingProfile().setOnewayUse(false); 131 else 132 routingModel.routingGraph.getRoutingProfile().setOnewayUse(true); 133 routingModel.setNodesChanged(); 134 Main.map.repaint(); 135 } 136 } 137 }); 138 criteriaM.add(cbmi); 139 menu.add(criteriaM); 140 141 menu.addSeparator(); 142 reverseMI = new JMenuItem(tr("Reverse route")); 143 reverseMI.addActionListener(new ActionListener() { 144 public void actionPerformed(ActionEvent e) { 145 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 146 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 147 RoutingModel routingModel = layer.getRoutingModel(); 148 routingModel.reverseNodes(); 149 Main.map.repaint(); 150 } 151 } 152 }); 153 menu.add(reverseMI); 154 155 clearMI = new JMenuItem(tr("Clear route")); 156 clearMI.addActionListener(new ActionListener() { 157 public void actionPerformed(ActionEvent e) { 158 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 159 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 160 RoutingModel routingModel = layer.getRoutingModel(); 161 // Reset routing nodes and paths 162 routingModel.reset(); 163 RoutingPlugin.getInstance().getRoutingDialog().clearNodes(); 164 Main.map.repaint(); 165 } 166 } 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 185 186 // Initially disabled 187 disableAllItems(); 188 } 189 190 public void disableAllItems() { 191 startMI.setEnabled(false); 192 reverseMI.setEnabled(false); 193 clearMI.setEnabled(false); 194 criteriaM.setEnabled(false); 195 } 196 197 public void enableStartItem() { 198 startMI.setEnabled(true); 199 } 200 201 public void enableRestOfItems() { 202 reverseMI.setEnabled(true); 203 clearMI.setEnabled(true); 204 criteriaM.setEnabled(true); 205 } 206 207 public void disableRestOfItems() { 208 reverseMI.setEnabled(false); 209 clearMI.setEnabled(false); 210 criteriaM.setEnabled(false); 211 } 61 /** 62 * Default serial version UID 63 */ 64 private static final long serialVersionUID = 3559922048225708480L; 65 66 private final JMenuItem startMI; 67 private final JMenuItem reverseMI; 68 private final JMenuItem clearMI; 69 private final JMenuItem regraphMI; 70 private final JMenu criteriaM; 71 private final JMenu menu; 72 73 /** 74 * @param s 75 */ 76 public RoutingMenu() { 77 MainMenu mm = Main.main.menu; 78 menu = mm.addMenu(marktr("Routing"), KeyEvent.VK_O, mm.defaultMenuPos, ht("/Plugin/Routing")); 79 80 startMI = new JMenuItem(tr("Add routing layer")); 81 startMI.addActionListener(new ActionListener() { 82 public void actionPerformed(ActionEvent e) { 83 RoutingPlugin.getInstance().addLayer(); 84 } 85 }); 86 menu.add(startMI); 87 88 menu.addSeparator(); 89 ButtonGroup group = new ButtonGroup(); 90 91 criteriaM = new JMenu(tr("Criteria")); 92 93 JRadioButtonMenuItem rshorter = new JRadioButtonMenuItem(tr("Shortest")); 94 rshorter.setSelected(true); 95 rshorter.addItemListener(new ItemListener() { 96 public void itemStateChanged(ItemEvent e) { 97 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 98 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 99 RoutingModel routingModel = layer.getRoutingModel(); 100 if (e.getStateChange()==ItemEvent.SELECTED) { 101 routingModel.routingGraph.setTypeRoute(RouteType.SHORTEST); 102 } else { 103 routingModel.routingGraph.setTypeRoute(RouteType.FASTEST); 104 } 105 // routingModel.routingGraph.resetGraph(); 106 // routingModel.routingGraph.createGraph(); 107 //TODO: Change this way 108 //FIXME: do not change node but recalculate routing. 109 routingModel.setNodesChanged(); 110 Main.map.repaint(); 111 } 112 } 113 114 }); 115 116 JRadioButtonMenuItem rfaster = new JRadioButtonMenuItem(tr("Fastest")); 117 group.add(rshorter); 118 group.add(rfaster); 119 criteriaM.add(rshorter); 120 criteriaM.add(rfaster); 121 122 criteriaM.addSeparator(); 123 JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(tr("Ignore oneways")); 124 cbmi.addItemListener(new ItemListener() { 125 public void itemStateChanged(ItemEvent e) { 126 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 127 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 128 RoutingModel routingModel = layer.getRoutingModel(); 129 if (e.getStateChange()==ItemEvent.SELECTED) 130 routingModel.routingGraph.getRoutingProfile().setOnewayUse(false); 131 else 132 routingModel.routingGraph.getRoutingProfile().setOnewayUse(true); 133 routingModel.setNodesChanged(); 134 routingModel.setOnewayChanged(); 135 Main.map.repaint(); 136 } 137 } 138 }); 139 criteriaM.add(cbmi); 140 menu.add(criteriaM); 141 142 menu.addSeparator(); 143 reverseMI = new JMenuItem(tr("Reverse route")); 144 reverseMI.addActionListener(new ActionListener() { 145 public void actionPerformed(ActionEvent e) { 146 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 147 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 148 RoutingModel routingModel = layer.getRoutingModel(); 149 routingModel.reverseNodes(); 150 Main.map.repaint(); 151 } 152 } 153 }); 154 menu.add(reverseMI); 155 156 clearMI = new JMenuItem(tr("Clear route")); 157 clearMI.addActionListener(new ActionListener() { 158 public void actionPerformed(ActionEvent e) { 159 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 160 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 161 RoutingModel routingModel = layer.getRoutingModel(); 162 // Reset routing nodes and paths 163 routingModel.reset(); 164 RoutingPlugin.getInstance().getRoutingDialog().clearNodes(); 165 Main.map.repaint(); 166 } 167 } 168 }); 169 menu.add(clearMI); 170 171 regraphMI = new JMenuItem(tr("Reconstruct Graph")); 172 regraphMI.addActionListener(new ActionListener() { 173 public void actionPerformed(ActionEvent e) { 174 175 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 176 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 177 RoutingModel routingModel = layer.getRoutingModel(); 178 routingModel.routingGraph.resetGraph(); 179 routingModel.routingGraph.createGraph(); 180 } 181 182 } 183 }); 184 menu.add(regraphMI); 185 186 187 // Initially disabled 188 disableAllItems(); 189 } 190 191 public void disableAllItems() { 192 startMI.setEnabled(false); 193 reverseMI.setEnabled(false); 194 clearMI.setEnabled(false); 195 criteriaM.setEnabled(false); 196 } 197 198 public void enableStartItem() { 199 startMI.setEnabled(true); 200 } 201 202 public void enableRestOfItems() { 203 reverseMI.setEnabled(true); 204 clearMI.setEnabled(true); 205 criteriaM.setEnabled(true); 206 } 207 208 public void disableRestOfItems() { 209 reverseMI.setEnabled(false); 210 clearMI.setEnabled(false); 211 criteriaM.setEnabled(false); 212 } 212 213 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingPreferenceDialog.java
r27857 r28138 63 63 public class RoutingPreferenceDialog extends DefaultTabPreferenceSetting { 64 64 65 /** 66 * Logger 67 */ 68 static Logger logger = Logger.getLogger(RoutingPreferenceDialog.class); 69 70 private Map<String, String> orig; 71 private DefaultTableModel model; 72 73 /** 74 * Constructor 75 */ 76 public RoutingPreferenceDialog() { 77 super("routing", tr("Routing Plugin Preferences"), tr("Configure routing preferences.")); 78 readPreferences(); 79 } 80 81 public void addGui(final PreferenceTabbedPane gui) { 82 83 JPanel principal = gui.createPreferenceTab(this); 84 85 JPanel p = new JPanel(); 86 p.setLayout(new GridBagLayout()); 87 88 model = new DefaultTableModel(new String[] { tr("Highway type"), 89 tr("Speed (Km/h)") }, 0) { 90 private static final long serialVersionUID = 4253339034781567453L; 91 92 @Override 93 public boolean isCellEditable(int row, int column) { 94 return column != 0; 95 } 96 }; 97 final JTable list = new JTable(model); 98 loadSpeeds(model); 99 100 JScrollPane scroll = new JScrollPane(list); 101 102 p.add(scroll, GBC.eol().fill(GBC.BOTH)); 103 scroll.setPreferredSize(new Dimension(200, 200)); 104 105 JButton add = new JButton(tr("Add")); 106 p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL)); 107 p.add(add, GBC.std().insets(0, 5, 0, 0)); 108 add.addActionListener(new ActionListener() { 109 public void actionPerformed(ActionEvent e) { 110 JPanel p = new JPanel(new GridBagLayout()); 111 p.add(new JLabel(tr("Weight")), GBC.std().insets(0, 0, 5, 0)); 112 JComboBox key = new JComboBox(); 113 for (OsmWayTypes pk : OsmWayTypes.values()) 114 key.addItem(pk.getTag()); 115 JTextField value = new JTextField(10); 116 p.add(key, GBC.eop().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL)); 117 p.add(new JLabel(tr("Value")), GBC.std().insets(0, 0, 5, 0)); 118 p.add(value, GBC.eol().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL)); 119 int answer = JOptionPane.showConfirmDialog(gui, p, 120 tr("Enter weight values"), 121 JOptionPane.OK_CANCEL_OPTION); 122 if (answer == JOptionPane.OK_OPTION) { 123 model 124 .addRow(new String[] { 125 key.getSelectedItem().toString(), 126 value.getText() }); 127 } 128 } 129 }); 130 131 JButton delete = new JButton(tr("Delete")); 132 p.add(delete, GBC.std().insets(0, 5, 0, 0)); 133 delete.addActionListener(new ActionListener() { 134 public void actionPerformed(ActionEvent e) { 135 if (list.getSelectedRow() == -1) 136 JOptionPane.showMessageDialog(gui, 137 tr("Please select the row to delete.")); 138 else { 139 Integer i; 140 while ((i = list.getSelectedRow()) != -1) 141 model.removeRow(i); 142 } 143 } 144 }); 145 146 JButton edit = new JButton(tr("Edit")); 147 p.add(edit, GBC.std().insets(5, 5, 5, 0)); 148 edit.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent e) { 150 edit(gui, list); 151 } 152 }); 153 154 JTabbedPane Opciones = new JTabbedPane(); 155 Opciones.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 156 157 Opciones.addTab("Profile", null, p, null); 158 // Opciones.addTab("Preferences", new JPanel()); 159 160 list.addMouseListener(new MouseAdapter(){ 161 @Override public void mouseClicked(MouseEvent e) { 162 if (e.getClickCount() == 2) 163 edit(gui, list); 164 } 165 }); 166 167 principal.add(Opciones, GBC.eol().fill(GBC.BOTH)); 168 169 } 170 171 public boolean ok() { 172 for (int i = 0; i < model.getRowCount(); ++i) { 173 String value = model.getValueAt(i, 1).toString(); 174 if (value.length() != 0) { 175 String key = model.getValueAt(i, 0).toString(); 176 String origValue = orig.get(key); 177 if (origValue == null || !origValue.equals(value)) 178 Main.pref.put(key, value); 179 orig.remove(key); // processed. 180 } 181 } 182 for (Entry<String, String> e : orig.entrySet()) 183 Main.pref.put(e.getKey(), null); 184 return false; 185 } 186 187 private void edit(final PreferenceTabbedPane gui, final JTable list) { 188 if (list.getSelectedRowCount() != 1) { 189 JOptionPane.showMessageDialog(gui, 190 tr("Please select the row to edit.")); 191 return; 192 } 193 String v = JOptionPane.showInputDialog(tr("New value for {0}", model 194 .getValueAt(list.getSelectedRow(), 0)), model.getValueAt(list 195 .getSelectedRow(), 1)); 196 if (v != null) 197 model.setValueAt(v, list.getSelectedRow(), 1); 198 } 199 200 private void loadSpeeds(DefaultTableModel model) { 201 // Read dialog values from preferences 202 readPreferences(); 203 // Put these values in the model 204 for (String tag : orig.keySet()) { 205 model.addRow(new String[] { tag, orig.get(tag) }); 206 } 207 } 208 209 private void readPreferences() { 210 orig = Main.pref.getAllPrefix("routing.profile.default.speed"); 211 if (orig.size() == 0) { // defaults 212 logger.debug("Loading Default Preferences."); 213 for (OsmWayTypes owt : OsmWayTypes.values()) { 214 Main.pref.putInteger("routing.profile.default.speed." 215 + owt.getTag(), owt.getSpeed()); 216 } 217 orig = Main.pref.getAllPrefix("routing.profile.default.speed"); 218 } 219 else logger.debug("Default preferences already exist."); 220 } 221 222 private String getKeyTag(String tag) { 223 return tag.split(".", 5)[4]; 224 } 225 226 private String getTypeTag(String tag) { 227 return tag.split(".", 5)[3]; 228 } 229 230 private String getNameTag(String tag) { 231 return tag.split(".", 5)[2]; 232 } 65 /** 66 * Logger 67 */ 68 static Logger logger = Logger.getLogger(RoutingPreferenceDialog.class); 69 70 private Map<String, String> orig; 71 private DefaultTableModel model; 72 73 /** 74 * Constructor 75 */ 76 public RoutingPreferenceDialog() { 77 super("routing", tr("Routing Plugin Preferences"), tr("Configure routing preferences.")); 78 readPreferences(); 79 } 80 81 public void addGui(final PreferenceTabbedPane gui) { 82 83 JPanel principal = gui.createPreferenceTab(this); 84 85 JPanel p = new JPanel(); 86 p.setLayout(new GridBagLayout()); 87 88 model = new DefaultTableModel(new String[] { tr("Highway type"), 89 tr("Speed (Km/h)") }, 0) { 90 private static final long serialVersionUID = 4253339034781567453L; 91 92 @Override 93 public boolean isCellEditable(int row, int column) { 94 return column != 0; 95 } 96 }; 97 final JTable list = new JTable(model); 98 loadSpeeds(model); 99 100 JScrollPane scroll = new JScrollPane(list); 101 102 p.add(scroll, GBC.eol().fill(GBC.BOTH)); 103 scroll.setPreferredSize(new Dimension(200, 200)); 104 105 JButton add = new JButton(tr("Add")); 106 p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL)); 107 p.add(add, GBC.std().insets(0, 5, 0, 0)); 108 add.addActionListener(new ActionListener() { 109 public void actionPerformed(ActionEvent e) { 110 JPanel p = new JPanel(new GridBagLayout()); 111 p.add(new JLabel(tr("Weight")), GBC.std().insets(0, 0, 5, 0)); 112 JComboBox<String> key = new JComboBox<String>(); 113 for (OsmWayTypes pk : OsmWayTypes.values()) 114 key.addItem(pk.getTag()); 115 JTextField value = new JTextField(10); 116 p.add(key, GBC.eop().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL)); 117 p.add(new JLabel(tr("Value")), GBC.std().insets(0, 0, 5, 0)); 118 p.add(value, GBC.eol().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL)); 119 int answer = JOptionPane.showConfirmDialog(gui, p, 120 tr("Enter weight values"), 121 JOptionPane.OK_CANCEL_OPTION); 122 if (answer == JOptionPane.OK_OPTION) { 123 model 124 .addRow(new String[] { 125 key.getSelectedItem().toString(), 126 value.getText() }); 127 } 128 } 129 }); 130 131 JButton delete = new JButton(tr("Delete")); 132 p.add(delete, GBC.std().insets(0, 5, 0, 0)); 133 delete.addActionListener(new ActionListener() { 134 public void actionPerformed(ActionEvent e) { 135 if (list.getSelectedRow() == -1) 136 JOptionPane.showMessageDialog(gui, 137 tr("Please select the row to delete.")); 138 else { 139 Integer i; 140 while ((i = list.getSelectedRow()) != -1) 141 model.removeRow(i); 142 } 143 } 144 }); 145 146 JButton edit = new JButton(tr("Edit")); 147 p.add(edit, GBC.std().insets(5, 5, 5, 0)); 148 edit.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent e) { 150 edit(gui, list); 151 } 152 }); 153 154 JTabbedPane Opciones = new JTabbedPane(); 155 Opciones.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 156 157 Opciones.addTab("Profile", null, p, null); 158 // Opciones.addTab("Preferences", new JPanel()); 159 160 list.addMouseListener(new MouseAdapter(){ 161 @Override public void mouseClicked(MouseEvent e) { 162 if (e.getClickCount() == 2) 163 edit(gui, list); 164 } 165 }); 166 167 principal.add(Opciones, GBC.eol().fill(GBC.BOTH)); 168 169 } 170 171 public boolean ok() { 172 for (int i = 0; i < model.getRowCount(); ++i) { 173 String value = model.getValueAt(i, 1).toString(); 174 if (value.length() != 0) { 175 String key = model.getValueAt(i, 0).toString(); 176 String origValue = orig.get(key); 177 if (origValue == null || !origValue.equals(value)) 178 Main.pref.put(key, value); 179 orig.remove(key); // processed. 180 } 181 } 182 for (Entry<String, String> e : orig.entrySet()) 183 Main.pref.put(e.getKey(), null); 184 return false; 185 } 186 187 private void edit(final PreferenceTabbedPane gui, final JTable list) { 188 if (list.getSelectedRowCount() != 1) { 189 JOptionPane.showMessageDialog(gui, 190 tr("Please select the row to edit.")); 191 return; 192 } 193 String v = JOptionPane.showInputDialog(tr("New value for {0}", model 194 .getValueAt(list.getSelectedRow(), 0)), model.getValueAt(list 195 .getSelectedRow(), 1)); 196 if (v != null) 197 model.setValueAt(v, list.getSelectedRow(), 1); 198 } 199 200 private void loadSpeeds(DefaultTableModel model) { 201 // Read dialog values from preferences 202 readPreferences(); 203 // Put these values in the model 204 for (String tag : orig.keySet()) { 205 model.addRow(new String[] { tag, orig.get(tag) }); 206 } 207 } 208 209 private void readPreferences() { 210 orig = Main.pref.getAllPrefix("routing.profile.default.speed"); 211 if (orig.size() == 0) { // defaults 212 logger.debug("Loading Default Preferences."); 213 for (OsmWayTypes owt : OsmWayTypes.values()) { 214 Main.pref.putInteger("routing.profile.default.speed." 215 + owt.getTag(), owt.getSpeed()); 216 } 217 orig = Main.pref.getAllPrefix("routing.profile.default.speed"); 218 } 219 else logger.debug("Default preferences already exist."); 220 } 221 /* 222 private String getKeyTag(String tag) { 223 return tag.split(".", 5)[4]; 224 } 225 226 private String getTypeTag(String tag) { 227 return tag.split(".", 5)[3]; 228 } 229 230 private String getNameTag(String tag) { 231 return tag.split(".", 5)[2]; 232 } 233 */ 233 234 }
Note:
See TracChangeset
for help on using the changeset viewer.