Ignore:
Timestamp:
2007-07-29T22:33:42+02:00 (19 years ago)
Author:
christofd
Message:

use multigraph to allow multiple ways for a segment.
added weights for streets
looks like if it works now :-)

Location:
applications/editors/josm/plugins/navigator/src/at/dallermassl/josm/plugin/navigator
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/navigator/src/at/dallermassl/josm/plugin/navigator/NavigatorLayer.java

    r3828 r3865  
    147147            endIcon.paintIcon(mv, g, screen.x, screen.y - endIcon.getIconHeight());
    148148        }
    149 
    150         Main.pref.hasKey(KEY_ROUTE_COLOR);
    151         String selectString = Main.pref.get(KEY_ROUTE_SELECT);
    152         if(selectString.length() == 0) {
    153             selectString = "true";
    154             Main.pref.put(KEY_ROUTE_SELECT, selectString);
    155         }
    156        
    157         if(Boolean.parseBoolean(selectString)) {
    158             List<Segment> path = navigatorNodeModel.getSegmentPath();
    159             if(path != null) {
    160                 synchronized(path) {
    161                     Main.ds.setSelected(path);
    162 //                  Main.map.mapView.repaint();
    163                 }
    164             }
    165         }
    166149       
    167150        String colorString = Main.pref.get(KEY_ROUTE_COLOR);
     
    243226    public void navigate() {
    244227        navigatorNodeModel.calculateShortesPath();
     228        Main.pref.hasKey(KEY_ROUTE_COLOR);
     229        String selectString = Main.pref.get(KEY_ROUTE_SELECT);
     230        if(selectString.length() == 0) {
     231            selectString = "true";
     232            Main.pref.put(KEY_ROUTE_SELECT, selectString);
     233        }
     234       
     235        if(Boolean.parseBoolean(selectString)) {
     236            List<Segment> path = navigatorNodeModel.getSegmentPath();
     237            if(path != null) {
     238                synchronized(path) {
     239                    Main.ds.setSelected(path);
     240                }
     241            }
     242        }
    245243        Main.map.repaint();       
    246244    }
  • applications/editors/josm/plugins/navigator/src/at/dallermassl/josm/plugin/navigator/NavigatorModeAction.java

    r3829 r3865  
    2626   
    2727    public NavigatorModeAction(MapFrame mapFrame, NavigatorModel navigatorModel, NavigatorLayer navigationLayer) {
    28         super(tr("Navigator"), "navigation", tr("Set start/end for autorouting"), KeyEvent.VK_F, mapFrame, ImageProvider.getCursor("crosshair", "selection"));
     28        super(tr("Navigator"), "navigation", tr("Set start/end for autorouting. Middle Mouse button to reset."), KeyEvent.VK_F, mapFrame, ImageProvider.getCursor("crosshair", "selection"));
    2929        this.navigatorModel = navigatorModel;
    3030        this.navigatorLayer = navigationLayer;
  • applications/editors/josm/plugins/navigator/src/at/dallermassl/josm/plugin/navigator/NavigatorModel.java

    r3830 r3865  
    66import java.util.ArrayList;
    77import java.util.Collection;
     8import java.util.HashMap;
    89import java.util.LinkedList;
    910import java.util.List;
     11import java.util.Map;
    1012
    1113import org.jgrapht.Graph;
     
    2426 */
    2527public class NavigatorModel {
    26     private Graph graph;
     28    private Graph<Node, SegmentEdge> graph;
    2729    private List<Node> nodes;
    28     private int selectionChangedCalls;
    29     List<Segment> segmentPath;
    30     List<SegmentEdge> edgePath;
     30    private List<Segment> segmentPath;
     31    private List<SegmentEdge> edgePath;
     32    private Map<String, Double> highwayWeight;
    3133
    3234    public NavigatorModel() {
    3335        nodes = new ArrayList<Node>();
     36        highwayWeight = new HashMap<String, Double>();
     37    }
     38   
     39    /**
     40     * Set the weight for the given highway type. The higher the weight is,
     41     * the more it is preferred in routing.
     42     * @param type the type of the highway.
     43     * @param weigth the weight.
     44     */
     45    public void setHighwayTypeWeight(String type, double weigth) {
     46        highwayWeight.put(type, weigth);
    3447    }
    3548   
     
    4558        if (graph == null) {
    4659            OsmGraphCreator graphCreator = new OsmGraphCreator();
     60            for(String type : highwayWeight.keySet()) {
     61                graphCreator.setHighwayTypeWeight(type, highwayWeight.get(type));
     62            }
    4763            // graph = graphCreator.createGraph();
    4864            graph = graphCreator.createSegmentGraph();
     
    8096        edgePath.addAll(fullPath);
    8197       
    82         System.out.println("shortest path found: " + fullPath + " weight: " + fullWeight);
     98        System.out.println("shortest path found: " + fullPath + "\nweight: " + fullWeight);
    8399        System.out.println(getPathDescription());
    84100//        double weight2 = 0;
     
    121137        Way oldWay = null;
    122138        Way way = null;
    123         for(SegmentEdge edge : edgePath) {
     139       
     140        SegmentEdge edge;
     141        for(int segIndex = 0; segIndex < edgePath.size(); ++segIndex) {
     142            edge = edgePath.get(segIndex);
    124143            way = edge.getWay();
     144            if(way == null) {
     145                System.out.println("way is null!");
     146            }
    125147            length += edge.getLengthInM();
    126             if(oldWay != null && !oldWay.equals(way)) {
    127                 description = new PathDescription(oldWay, length);
     148            // if way changes or it is the last way add the description:
     149            if(oldWay != null && !way.equals(oldWay)) {
     150                description = new PathDescription(oldWay, length); // add finished way
    128151                pathDescriptions.add(description);
    129                 length = 0;
     152                length = 0;               
     153            }
     154            if(segIndex == edgePath.size() - 1) {
     155                description = new PathDescription(way, length);
     156                pathDescriptions.add(description);               
    130157            }
    131158            oldWay = way;
    132159        }
    133         if(way != null) {
    134             description = new PathDescription(way, length);
    135             pathDescriptions.add(description);
    136         }
     160       
     161       
     162//        for(SegmentEdge edge : edgePath) {
     163//            way = edge.getWay();
     164//            length += edge.getLengthInM();
     165//            if(oldWay != null && !oldWay.equals(way)) {
     166//                description = new PathDescription(oldWay, length);
     167//                pathDescriptions.add(description);
     168//                length = 0;
     169//            }
     170//            oldWay = way;
     171//        }
     172//        if(way != null) {
     173//            description = new PathDescription(way, length);
     174//            pathDescriptions.add(description);
     175//        }
    137176        return pathDescriptions;
    138177    }
  • applications/editors/josm/plugins/navigator/src/at/dallermassl/josm/plugin/navigator/NavigatorPlugin.java

    r3831 r3865  
    88import java.awt.event.ActionEvent;
    99import java.awt.event.ActionListener;
    10 import java.util.ArrayList;
    11 import java.util.List;
     10import java.util.Map;
    1211
    13 import javax.swing.AbstractButton;
    1412import javax.swing.JMenu;
    1513import javax.swing.JMenuBar;
    1614import javax.swing.JMenuItem;
    1715
    18 import org.jgrapht.Graph;
    19 import org.jgrapht.alg.DijkstraShortestPath;
    2016import org.openstreetmap.josm.Main;
    21 import org.openstreetmap.josm.data.osm.Node;
    22 import org.openstreetmap.josm.data.osm.Segment;
    2317import org.openstreetmap.josm.gui.IconToggleButton;
    2418import org.openstreetmap.josm.gui.MapFrame;
    25 import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
    2619import org.openstreetmap.josm.plugins.Plugin;
    2720
     
    3326 */
    3427public class NavigatorPlugin extends Plugin {
     28    private static final String KEY_HIGHWAY_WEIGHT_PREFIX = "navigator.weight.";
    3529    private NavigatorLayer navigatorLayer;
    3630    private NavigatorModel navigatorModel;
     
    4135    public NavigatorPlugin() {
    4236        super();
    43        
     37        checkWeights();
    4438        navigatorModel = new NavigatorModel();
     39        setHighwayTypeWeights();
    4540        navigatorLayer = new NavigatorLayer(tr("Navigation"));
    4641        navigatorLayer.setNavigatorNodeModel(navigatorModel);
     
    5449            public void actionPerformed(ActionEvent e) {
    5550                navigatorModel.resetGraph();
     51                setHighwayTypeWeights();
    5652            }
    5753        });
    5854        navigatorMenu.add(resetMenuItem);
    5955        menu.add(navigatorMenu);
     56    }
     57   
     58    /**
     59     * Reads the weight values for the different highway types from the preferences.
     60     */
     61    private void setHighwayTypeWeights() {
     62        Map<String, String> weightMap = Main.pref.getAllPrefix(KEY_HIGHWAY_WEIGHT_PREFIX);
     63        String type;
     64        double weight;
     65        String value;
     66        for(String typeKey : weightMap.keySet()) {
     67            type = typeKey.substring(KEY_HIGHWAY_WEIGHT_PREFIX.length());
     68            weight = Double.parseDouble(weightMap.get(typeKey));
     69            navigatorModel.setHighwayTypeWeight(type, weight);
     70        }
     71    }
     72   
     73    /**
     74     * Checks if there are any highway weights set in the preferences. If not, default
     75     * values are used.
     76     */
     77    private void checkWeights() {
     78        setDefaultWeight("motorway", 100.0);
     79        setDefaultWeight("primary", 80.0);
     80        setDefaultWeight("secondary", 70.0);
     81        setDefaultWeight("tertiary", 60.0);
     82        setDefaultWeight("unclassified", 60.0);
     83        setDefaultWeight("residential", 40.0);
     84        setDefaultWeight("pedestrian", 0.0);
     85        setDefaultWeight("cycleway", 0.0);
     86        setDefaultWeight("footway", 0.0);
     87    }
     88   
     89    private void setDefaultWeight(String type, double value) {
     90        if(!Main.pref.hasKey(KEY_HIGHWAY_WEIGHT_PREFIX + type)) {
     91            Main.pref.put(KEY_HIGHWAY_WEIGHT_PREFIX + type, String.valueOf(value));
     92        }
    6093    }
    6194   
  • applications/editors/josm/plugins/navigator/src/at/dallermassl/josm/plugin/navigator/OsmGraphCreator.java

    r3832 r3865  
    3333
    3434    private Map<String, Double> highwayWeight;
    35     private static final double DEFAULT_WEIGHT = Double.MAX_VALUE;
     35    private static final double DEFAULT_WEIGHT = 0.0;
    3636
    3737    public OsmGraphCreator() {
    3838        highwayWeight = new HashMap<String, Double>();
    39         highwayWeight.put("motorway", 130.0);
    40         highwayWeight.put("primary", 100.0);
    41         highwayWeight.put("secondary", 70.0);
    42         highwayWeight.put("unclassified", 50.0);
    43         highwayWeight.put("residential", 40.0);
    44         highwayWeight.put("footway", 1.0);
     39    }
     40   
     41    /**
     42     * Set the weight for the given highway type. The higher the weight is,
     43     * the more it is preferred in routing.
     44     * @param type the type of the highway.
     45     * @param weigth the weight.
     46     */
     47    public void setHighwayTypeWeight(String type, double weigth) {
     48        highwayWeight.put(type, weigth);
     49        System.out.println("set " + type + " to " + weigth);
    4550    }
    4651
    4752    public Graph<Node, SegmentEdge> createSegmentGraph() {
    48         SimpleDirectedWeightedGraph<Node, SegmentEdge> graph = new SimpleDirectedWeightedGraph<Node, SegmentEdge>(SegmentEdge.class);
     53        DirectedWeightedMultigraph<Node, SegmentEdge> graph = new DirectedWeightedMultigraph<Node, SegmentEdge>(SegmentEdge.class);
     54        // SimpleDirectedWeightedGraph<Node, SegmentEdge> graph = new SimpleDirectedWeightedGraph<Node, SegmentEdge>(SegmentEdge.class);
    4955        // SimpleGraph<Node, SegmentEdge> graph = new SimpleGraph<Node, SegmentEdge>(SegmentEdge.class);
    5056        SegmentEdge edge;
     
    8187    /**
    8288     * Returns the weight for the given segment depending on the highway type and the length of the
    83      * segment.
    84      *
     89     * segment. The higher the value, the less it is used in routing.
    8590     * @param way
    8691     * @param segment
     
    9095        String type = way.get("highway");
    9196        if (type == null) {
    92             return 0.0d;
     97            return Double.MAX_VALUE;
    9398        }
    9499        Double weightValue = highwayWeight.get(type);
     
    99104            weight = weightValue.doubleValue();
    100105        }
    101         double distance = Math.sqrt(segment.from.coor.distance(segment.to.coor)) * 111000; // deg
    102                                                                                             // to m
    103                                                                                             // (at
    104                                                                                             // equator
    105                                                                                             // :-)
    106         return distance; // weight;
     106        // deg to m (at equator :-):
     107        double distance = Math.sqrt(segment.from.coor.distance(segment.to.coor)) * 111000;
     108        if(weight == 0.0) {
     109            weight = 1E-20;
     110        }
     111        return distance / weight;
    107112    }
    108113
Note: See TracChangeset for help on using the changeset viewer.