Changeset 6910 in osm


Ignore:
Timestamp:
2008-02-18T09:06:14+01:00 (16 years ago)
Author:
jrreid
Message:

Updates to lakewalker to add native java port of the python script (consider it an alpha release). Older style of calling the python script still supported (via an additional menu item)

Location:
applications/editors/josm/plugins/lakewalker
Files:
5 added
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/lakewalker/README

    r4095 r6910  
    1 A JOSM plugin to interface to Darryl Shpak's Lakewalker module
     1README FOR Lakewalker
     2=======================
    23
     4The Lakewalker plugin can be used to generate traces of lakes, rivers or shoreline from
     5LANDSAT imagery (or theoretically any imagery that is provided on a WMS server).
     6
     7Currently the plugin features 2 modes of operation: Native and Classic. The classic mode
     8requires the Python lakewalker.py script to be downloaded into the lakewalker directory
     9within the JOSM plugin directory.
     10
     11Common Features:
    312 - On-screen feedback to Lakewalker progress with cancel
    413 - Tag ways appropriately
    514 - limit ways to length
    6  - Access all lakewalker options via preferences and via ctrl-click pop-up dialog
    715 - Manage Landsat image cache from Prefs screen
     16
     17Native Features:
     18 - Doesn't require Python
     19
     20Classic Features:
     21 - Access all lakewalker options via preferences
    822 - Read data in thread
    923 - ...
  • applications/editors/josm/plugins/lakewalker/build.xml

    r6789 r6910  
    4949        <attribute name="Plugin-Class" value="org.openstreetmap.josm.plugins.lakewalker.LakewalkerPlugin" />
    5050        <attribute name="Plugin-Description" value="Interface to Lakewalker module" />
    51         <attribute name="Plugin-Version" value="0.1"/>
    52         <attribute name="Author" value="Brent Easton &lt;b.easton@uws.edu.au>"/>
     51        <attribute name="Plugin-Version" value="0.2"/>
     52        <attribute name="Author" value="Brent Easton &lt;b.easton@uws.edu.au>, Jason Reid &lt;jrreid@ucalgary.ca>"/>
     53                <attribute name="Main-Class" value="org.openstreetmap.josm.plugins.lakewalker.LakewalkerApp"/>
    5354      </manifest>
    5455    </jar>
  • applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerAction.java

    r6874 r6910  
    1313import java.io.InputStreamReader;
    1414import java.util.ArrayList;
     15import java.util.Collection;
     16import java.util.LinkedList;
    1517import java.util.List;
     18import java.util.Vector;
    1619
    1720import javax.swing.JOptionPane;
     
    2427import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2528import org.openstreetmap.josm.tools.ImageProvider;
     29import org.openstreetmap.josm.data.coor.LatLon;
     30import org.openstreetmap.josm.data.osm.Node;
     31import org.openstreetmap.josm.data.osm.Way;
     32import org.openstreetmap.josm.command.AddCommand;
     33import org.openstreetmap.josm.command.Command;
     34import org.openstreetmap.josm.command.SequenceCommand;
     35
    2636import org.xml.sax.SAXException;
    2737
     
    3848  protected List<Node> selectedNodes;
    3949  protected Thread executeThread;
     50  protected boolean cancel;
     51 
     52  protected Collection<Command> commands = new LinkedList<Command>();
     53  protected Collection<Way> ways = new ArrayList<Way>();
    4054 
    4155  public LakewalkerAction(String name) {
     
    4559    setEnabled(true);
    4660  }
    47 
     61 
    4862  public void actionPerformed(ActionEvent e) {
    4963
     
    7387  }
    7488
    75   protected void lakewalk(Point clickPoint) {
    76     LatLon pos = Main.map.mapView.getLatLon(clickPoint.x, clickPoint.y);
    77 
    78     /*
    79      * Collect options
    80      */
    81     File working_dir = new File(Main.pref.getPreferencesDir(), "plugins");
    82     working_dir = new File(working_dir, "Lakewalker");
    83     String target = Main.pref.get(LakewalkerPreferences.PREF_PYTHON) + " lakewalker.py";
    84     LatLon topLeft = Main.map.mapView.getLatLon(0, 0);
    85     LatLon botRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), Main.map.mapView
    86         .getHeight());
    87 
    88     /*
    89      * Build command line
    90      */
    91     target += " --lat=" + pos.lat();
    92     target += " --lon=" + pos.lon();
    93     target += " --left=" + topLeft.lon();
    94     target += " --right=" + botRight.lon();
    95     target += " --top=" + topLeft.lat();
    96     target += " --bottom=" + botRight.lat();
    97     target += " --waylength=" + Main.pref.get(LakewalkerPreferences.PREF_MAX_SEG, "500");
    98     target += " --maxnodes=" + Main.pref.get(LakewalkerPreferences.PREF_MAX_NODES, "50000");
    99     target += " --threshold=" + Main.pref.get(LakewalkerPreferences.PREF_THRESHOLD, "35");
    100     target += " --dp-epsilon=" + Main.pref.get(LakewalkerPreferences.PREF_EPSILON, "0.0003");
    101     target += " --landsat-res=" + Main.pref.get(LakewalkerPreferences.PREF_LANDSAT_RES, "4000");
    102     target += " --tilesize=" + Main.pref.get(LakewalkerPreferences.PREF_LANDSAT_SIZE, "2000");
    103     target += " --startdir=" + Main.pref.get(LakewalkerPreferences.PREF_START_DIR, "east");
    104     target += " --wms=" + Main.pref.get(LakewalkerPreferences.PREF_WMS, "IR1");   
    105     target += " --josm";
     89  protected void lakewalk(Point clickPoint){
     90        /**
     91         * Positional data
     92         */
     93        final LatLon pos = Main.map.mapView.getLatLon(clickPoint.x, clickPoint.y);
     94        final LatLon topLeft = Main.map.mapView.getLatLon(0, 0);
     95        final LatLon botRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), Main.map.mapView
     96             .getHeight());         
     97
     98        /**
     99         * Cache/working directory location
     100         */
     101        final File working_dir = new File(Main.pref.getPreferencesDir(), "plugins/Lakewalker");
     102       
     103        /*
     104         * Collect options
     105         */
     106        final int waylen = Integer.parseInt(Main.pref.get(LakewalkerPreferences.PREF_MAX_SEG, "500"));
     107        final int maxnode = Integer.parseInt(Main.pref.get(LakewalkerPreferences.PREF_MAX_NODES, "50000"));
     108        final int threshold = Integer.parseInt(Main.pref.get(LakewalkerPreferences.PREF_THRESHOLD, "35"));
     109        final double epsilon = Double.parseDouble(Main.pref.get(LakewalkerPreferences.PREF_EPSILON, "0.0003"));
     110        final int resolution = Integer.parseInt(Main.pref.get(LakewalkerPreferences.PREF_LANDSAT_RES, "4000"));
     111        final int tilesize = Integer.parseInt(Main.pref.get(LakewalkerPreferences.PREF_LANDSAT_SIZE, "2000"));
     112        final String startdir = Main.pref.get(LakewalkerPreferences.PREF_START_DIR, "east");
     113        final String wmslayer = Main.pref.get(LakewalkerPreferences.PREF_WMS, "IR1");     
     114           
     115        try {
     116        PleaseWaitRunnable lakewalkerTask = new PleaseWaitRunnable(tr("Tracing")){
     117          @Override protected void realRun() throws SAXException {
     118                  processnodelist(pos, topLeft, botRight, waylen,maxnode,threshold,epsilon,resolution,tilesize,startdir,wmslayer,working_dir);
     119          }
     120          @Override protected void finish() {
     121           
     122          }
     123          @Override protected void cancel() {
     124            cancel();
     125          }
     126        };
     127        Thread executeThread = new Thread(lakewalkerTask);
     128        executeThread.start();
     129      }
     130      catch (Exception ex) {
     131        System.out.println("Exception caught: " + ex.getMessage());
     132      }     
     133  }
     134 
     135  private void processnodelist(LatLon pos, LatLon topLeft, LatLon botRight, int waylen, int maxnode, int threshold, double epsilon, int resolution, int tilesize, String startdir, String wmslayer, File workingdir){
     136         
     137        ArrayList<double[]> nodelist = new ArrayList<double[]>();
     138         
     139        Lakewalker lw = new Lakewalker(waylen,maxnode,threshold,epsilon,resolution,tilesize,startdir,wmslayer,workingdir);
     140        try {
     141                nodelist = lw.trace(pos.lat(),pos.lon(),topLeft.lon(),botRight.lon(),topLeft.lat(),botRight.lat());
     142        } catch(LakewalkerException e){
     143                System.out.println(e.getError());
     144        }
     145       
     146        System.out.println(nodelist.size()+" nodes generated");
     147       
     148        /**
     149         * Run the nodelist through a vertex reduction algorithm
     150         */
     151       
     152        nodelist = lw.vertex_reduce(nodelist, epsilon);
     153       
     154        System.out.println("After vertex reduction "+nodelist.size()+" nodes remain.");
     155       
     156        /**
     157         * And then through douglas-peucker reduction
     158         */
     159       
     160        nodelist = lw.douglas_peucker(nodelist, epsilon);
     161       
     162        System.out.println("After Douglas-Peucker approximation "+nodelist.size()+" nodes remain.");
     163         
     164        /**
     165         * Turn the arraylist into osm nodes
     166         */
     167       
     168        Way way = new Way();
     169        Node n = null;
     170        Node tn = null;
     171        Node fn = null;
     172       
     173        double eastOffset = 0.0;
     174        double northOffset = 0.0;
     175        try {
     176          eastOffset = Double.parseDouble(Main.pref.get(LakewalkerPreferences.PREF_EAST_OFFSET, "0.0"));
     177          northOffset = Double.parseDouble(Main.pref.get(LakewalkerPreferences.PREF_NORTH_OFFSET, "0.0"));
     178        }
     179        catch (Exception e) {
     180         
     181        }
     182        char option = ' ';
     183       
     184        int nodesinway = 0;
     185       
     186        for(int i = 0; i< nodelist.size(); i++){
     187                if (cancel) {
     188                        return;
     189            }
     190                       
     191                try {           
     192                  LatLon ll = new LatLon(nodelist.get(i)[0]+northOffset, nodelist.get(i)[1]+eastOffset);
     193                  n = new Node(ll);
     194                  if(fn==null){
     195                    fn = n;
     196                  }
     197                  commands.add(new AddCommand(n));
     198                 
     199                } catch (Exception ex) {                 
     200                }           
     201             
     202                way.nodes.add(n);
     203               
     204                if(nodesinway > Integer.parseInt(Main.pref.get(LakewalkerPreferences.PREF_MAX_SEG, "500"))){
     205                        String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water");
     206               
     207                if(!waytype.equals("none")){
     208                  way.put("natural",waytype);
     209                }
     210               
     211                way.put("created_by", "Dshpak_landsat_lakes");
     212                commands.add(new AddCommand(way));
     213               
     214                way = new Way();
     215
     216                way.nodes.add(n);
     217               
     218                nodesinway = 0;
     219                }
     220                nodesinway++;
     221        }
     222       
     223        commands.add(new AddCommand(way));
     224        String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water");
    106225   
    107 
    108     try {
    109       /*
    110        * Start the Lakewalker
    111        */
    112       Runtime rt = Runtime.getRuntime();
    113       System.out.println("dir: " + working_dir + ", target: " + target);
    114       final Process p = rt.exec(target, null, working_dir);
    115       final BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    116      
    117       /*
    118        * Start a thread to read the output
    119        */
    120       final LakewalkerReader reader = new LakewalkerReader();
    121      
    122       PleaseWaitRunnable lakewalkerTask = new PleaseWaitRunnable(tr("Tracing")){
    123         @Override protected void realRun() throws SAXException {
    124           reader.read(input);
    125         }
    126         @Override protected void finish() {
    127          
    128         }
    129         @Override protected void cancel() {
    130           reader.cancel();
    131           executeThread.interrupt();
    132           p.destroy();
    133         }
    134       };
    135       Thread executeThread = new Thread(lakewalkerTask);
    136       executeThread.start();
    137     }
    138     catch (Exception ex) {
    139       System.out.println("Exception caught: " + ex.getMessage());
    140     }
    141 
    142 
     226    if(!waytype.equals("none")){
     227          way.put("natural",waytype);
     228    }
     229   
     230    way.put("created_by", "Dshpak_landsat_lakes");
     231   
     232        way.nodes.add(fn);
     233       
     234       
     235        if (!commands.isEmpty()) {
     236        Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands));
     237        Main.ds.setSelected(ways);
     238    } else {
     239          System.out.println("Failed");
     240    }
     241  }
     242 
     243  public void cancel() {
     244          cancel = true;
    143245  }
    144246
     
    164266  public void mouseReleased(MouseEvent e) {
    165267  }
    166 
    167 
    168   // class DuplicateDialog extends JDialog {
    169   // private static final long serialVersionUID = 1L;
    170   // protected Box mainPanel;
    171   // protected IntConfigurer offset;
    172   // protected boolean cancelled;
    173   // protected String right;
    174   // protected String left;
    175   // protected JComboBox moveCombo;
    176   //
    177   // public DuplicateDialog(String title) {
    178   // super();
    179   // this.setTitle(title);
    180   // this.setModal(true);
    181   // initComponents();
    182   // }
    183   //
    184   // protected void initComponents() {
    185   // mainPanel = Box.createVerticalBox();
    186   // offset = new IntConfigurer("", tr("Offset (metres): "), new Integer(15));
    187   // mainPanel.add(offset.getControls());
    188   // getContentPane().add(mainPanel);
    189   //
    190   // right = tr("right/down");
    191   // left = tr("left/up");
    192   // Box movePanel = Box.createHorizontalBox();
    193   // movePanel.add(new JLabel(tr("Create new segments to the ")));
    194   // moveCombo = new JComboBox(new String[] {right, left});
    195   // movePanel.add(moveCombo);
    196   // movePanel.add(new JLabel(tr(" of existing segments.")));
    197   // mainPanel.add(movePanel);
    198   //
    199   // Box buttonPanel = Box.createHorizontalBox();
    200   // JButton okButton = new JButton(tr("Ok"));
    201   // okButton.addActionListener(new ActionListener() {
    202   // public void actionPerformed(ActionEvent e) {
    203   // cancelled = false;
    204   // setVisible(false);
    205   //
    206   // }
    207   // });
    208   // JButton canButton = new JButton(tr("Cancel"));
    209   // canButton.addActionListener(new ActionListener() {
    210   // public void actionPerformed(ActionEvent e) {
    211   // cancelled = true;
    212   // setVisible(false);
    213   // }
    214   // });
    215   // buttonPanel.add(okButton);
    216   // buttonPanel.add(canButton);
    217   // mainPanel.add(buttonPanel);
    218   //
    219   // pack();
    220   // }
    221   //
    222   // protected int getOffset() {
    223   // int off = offset.getIntValue(15);
    224   // return right.equals(moveCombo.getSelectedItem()) ? off : -off;
    225   // }
    226   //
    227   // protected boolean isCancelled() {
    228   // return cancelled;
    229   // }
    230   //
    231   // }
     268 
    232269}
  • applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerPlugin.java

    r5979 r6910  
    2020 
    2121  protected String name;
     22  protected String name2;
    2223
    2324  public LakewalkerPlugin() {
    2425    name = tr("Lake Walker");
     26    name2 = tr("Lake Walker (Old)");
    2527    JMenu toolsMenu = null;
    2628    for (int i = 0; i < Main.main.menu.getMenuCount() && toolsMenu == null; i++) {
     
    4042      toolsMenu.addSeparator();
    4143      toolsMenu.add(new JMenuItem(new LakewalkerAction(name)));
     44      toolsMenu.add(new JMenuItem(new LakewalkerActionOld(name2)));
    4245    }
    4346   
Note: See TracChangeset for help on using the changeset viewer.