Ignore:
Timestamp:
2011-06-11T04:15:31+02:00 (13 years ago)
Author:
hassans
Message:

Construct graph immediately. Added rendering for graph route. Proper graph construction for all value of oneway=?. Hiding of toolbar and dialog when route layer is deactivated. Tweaked snapping and route line width.

Location:
applications/editors/josm/plugins/routing
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/routing/build.xml

    r25199 r26119  
    3535        <property name="plugin.build.dir"       value="build"/>
    3636        <property name="plugin.jar"             value="${plugin.dist.dir}/${ant.project.name}.jar"/>
    37         <property name="jgrapht"                value="lib/jgrapht-jdk1.5.jar"/>
    38         <property name="log4j"                  value="lib/log4j-1.2.15.jar"/>
     37        <property name="libdir"                 location="lib"/>
     38        <property name="jgrapht"                value="${libdir}/jgrapht-jdk1.5.jar"/>
     39        <property name="log4j"                  value="${libdir}/log4j-1.2.15.jar"/>
    3940        <property name="ant.build.javac.target" value="1.5"/>
    4041        <!-- Some initializations for several other targets -->
    4142        <target name="init">
     43                <mkdir dir="${plugin.dist.dir}"/>
    4244                <mkdir dir="${plugin.build.dir}"/>
    4345        </target>
     
    6971                <jar destfile="${plugin.jar}" basedir="${plugin.build.dir}">
    7072                        <manifest>
    71                                 <attribute name="Author" value="Jose Vidal &lt;vidalfree@gmail.com&gt;, Juangui Jordán &lt;juangui@gmail.com&gt;"/>
     73                                <attribute name="Author" value="Jose Vidal &lt;vidalfree@gmail.com&gt;, Juangui Jordán &lt;juangui@gmail.com&gt;, Hassan S &lt;hassan.sabirin@gmail.com&gt;"/>
    7274                                <attribute name="Plugin-Class" value="com.innovant.josm.plugin.routing.RoutingPlugin"/>
    7375                                <attribute name="Plugin-Date" value="${version.entry.commit.date}"/>
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingGraph.java

    r18415 r26119  
    4949 * @author Juangui
    5050 * @author Jose Vidal
     51 * @author Hassan S
    5152 */
    5253public class RoutingGraph {
     
    99100    private RoutingGraphDelegator rgDelegator=null;
    100101
     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   
    101125    /**
    102126     * Speeds
     
    131155        // iterate all ways and segments for all nodes:
    132156        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                 
    150220        logger.debug("End Create Graph");
    151221        logger.debug("Vertex: "+graph.vertexSet().size());
     
    225295
    226296    /**
    227      * Check is One Way.
    228      *
    229      * @param way
    230      *            the way.
    231      * @return <code>true</code> is a one way. <code>false</code> is not a one
    232      *         way.
    233      */
    234     private boolean isOneWay(Way way) {
    235         // FIXME: oneway=-1 is ignored for the moment!
    236         return way.get("oneway") != null
    237                 || "motorway".equals(way.get("highway"));
    238     }
    239 
    240     /**
    241297     * Check if a Way is correct.
    242298     *
     
    246302     */
    247303    public boolean isvalidWay(Way way) {
    248         if (!way.isTagged())
    249             return false;
     304        //if (!way.isTagged())            <---not needed me thinks
     305        //    return false;
    250306
    251307        return way.get("highway") != null || way.get("junction") != null
    252308                || way.get("service") != null;
    253309
    254     }
    255 
    256     public boolean isvalidNode(Node node) {
    257         return true;
    258310    }
    259311
     
    295347            DijkstraShortestPath<Node, OsmEdge> routingk = null;
    296348            for (int index = 1; index < nodes.size(); ++index) {
    297                 routingk = new DijkstraShortestPath<Node, OsmEdge>(rgDelegator, nodes
     349                routingk = new DijkstraShortestPath<Node, OsmEdge>(g, nodes
    298350                        .get(index - 1), nodes.get(index));
    299351                if (routingk.getPathEdgeList() == null) {
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingLayer.java

    r23561 r26119  
    3333import java.awt.Graphics2D;
    3434import java.awt.Point;
     35import java.awt.RenderingHints;
    3536import java.awt.Stroke;
    3637import java.util.ArrayList;
    3738import java.util.Collection;
    3839import java.util.List;
     40import java.util.Set;
    3941
    4042import javax.swing.Action;
     
    106108        this.routingModel = new RoutingModel(dataLayer.data);
    107109        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 */
    108114    }
    109115
     
    139145
    140146                Point P = Main.map.mapView.getPoint(n);
    141                 double dist = p.distanceSq(P);
     147                double dist = p.distance(P);
    142148                if (dist < snapDistance) {
    143149                    if ((nearest == null) || (dist < minDist)) {
     
    231237        // Get routing nodes (start, middle, end)
    232238        List<Node> nodes = routingModel.getSelectedNodes();
    233         if(nodes == null || nodes.size() == 0) {
    234             logger.debug("no nodes selected");
    235             return;
    236         }
    237239
    238240        // Get path stroke color from preferences
     
    259261        String widthString = Main.pref.get(PreferencesKeys.KEY_ROUTE_WIDTH.key);
    260262        if (widthString.length() == 0) {
    261             widthString = "8";
     263            widthString = "2";                                          /* I think 2 is better  */
    262264            // FIXME add after good width is found: Main.pref.put(KEY_ROUTE_WIDTH, widthString);
    263265        }
    264266        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       
    266288        // Paint routing path
    267289        List<OsmEdge> routeEdges = routingModel.getRouteEdges();
     
    327349
    328350            Graphics2D g2d = (Graphics2D)g;
     351            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Anti-alias!
    329352            Stroke oldStroke = g2d.getStroke();
    330353            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);
    332355            if (showDirection) {
    333356                double t = Math.atan2(to.y-from.y, to.x-from.x) + Math.PI;
     
    337360            g2d.setStroke(oldStroke);
    338361    }
    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               
    340379}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingPlugin.java

    r19475 r26119  
    3535import org.apache.log4j.xml.DOMConfigurator;
    3636import org.openstreetmap.josm.Main;
     37import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     38import org.openstreetmap.josm.data.osm.DataSet;
     39import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     40import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent.DatasetEventType;
     41import org.openstreetmap.josm.data.osm.event.DataSetListener;
     42import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
     43import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     44import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    3745import org.openstreetmap.josm.gui.IconToggleButton;
    3846import org.openstreetmap.josm.gui.MapFrame;
     
    6068 * @version 0.3
    6169 */
    62 public class RoutingPlugin extends Plugin implements LayerChangeListener {
     70public class RoutingPlugin extends Plugin implements LayerChangeListener,DataSetListenerAdapter.Listener {
    6371    /**
    6472     * Logger
     
    123131     */
    124132    private static RoutingPlugin plugin;
     133   
     134    private DataSetListenerAdapter datasetAdapter;
    125135
    126136    /**
     
    129139    public RoutingPlugin(PluginInformation info) {
    130140        super(info);
     141       
     142        datasetAdapter = new DataSetListenerAdapter(this);
    131143        plugin = this; // Assign reference to the plugin class
    132144        DOMConfigurator.configure("log4j.xml");
     
    141153        // Register this class as LayerChangeListener
    142154        MapView.addLayerChangeListener(this);
     155        DatasetEventManager.getInstance().addDatasetListener(datasetAdapter, FireMode.IN_EDT_CONSOLIDATED);
    143156        logger.debug("Finished loading plugin");
    144157    }
     
    203216     */
    204217    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                }
    206233    }
    207234
     
    254281        routingDialog.refresh();
    255282    }
    256 
     283   
     284    public void processDatasetEvent(AbstractDatasetChangedEvent event){
     285       
     286       
     287    }
    257288    /* (non-Javadoc)
    258289     * @see org.openstreetmap.josm.plugins.Plugin#getPreferenceSetting()
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingMenu.java

    r25843 r26119  
    6767    private JMenuItem reverseMI;
    6868    private JMenuItem clearMI;
     69    private JMenuItem regraphMI;
    6970    private JMenu criteriaM;
    7071    private JMenu menu;
     
    166167        });
    167168        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       
    168185
    169186        // Initially disabled
Note: See TracChangeset for help on using the changeset viewer.