Ignore:
Timestamp:
2007-08-13T12:55:42+02:00 (17 years ago)
Author:
brent
Message:

v0.3

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

Legend:

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

    r4071 r4095  
    11A JOSM plugin to interface to Darryl Shpak's Lakewalker module
    22
    3  - On-screen feedback to Lakewalker progress
     3 - On-screen feedback to Lakewalker progress with cancel
    44 - Tag ways appropriately
    55 - limit ways to length
    66 - Access all lakewalker options via preferences and via ctrl-click pop-up dialog
    77 - Manage Landsat image cache from Prefs screen
     8 - Read data in thread
    89 - ...
  • applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerAction.java

    r4071 r4095  
    1212import java.io.File;
    1313import java.io.InputStreamReader;
     14import java.nio.channels.ClosedByInterruptException;
    1415import java.util.ArrayList;
    1516import java.util.Collection;
     
    1819
    1920import javax.swing.JOptionPane;
     21import javax.swing.ProgressMonitor;
    2022
    2123import org.openstreetmap.josm.Main;
     
    2931import org.openstreetmap.josm.data.osm.Segment;
    3032import org.openstreetmap.josm.data.osm.Way;
     33import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3134import org.openstreetmap.josm.tools.ImageProvider;
     35import org.xml.sax.SAXException;
    3236
    3337/**
     
    4246  protected Cursor oldCursor;
    4347  protected List<Node> selectedNodes;
    44 
     48 
    4549  public LakewalkerAction(String name) {
    4650    super(name, "lakewalker-sml", tr("Lake Walker."), KeyEvent.VK_L, KeyEvent.CTRL_MASK
     
    4852    this.name = name;
    4953    setEnabled(true);
     54
    5055  }
    5156
     
    5358
    5459    Main.map.mapView.setCursor(oldCursor);
    55    
     60
    5661    if (Main.map == null) {
    5762      JOptionPane.showMessageDialog(Main.parent, tr("No data loaded."));
    5863      return;
    5964    }
    60    
     65
    6166    selectedNodes = new ArrayList<Node>();
    6267    for (OsmPrimitive osm : Main.ds.getSelected()) {
     
    7681    }
    7782  }
    78  
     83
    7984  protected void lakewalk(Point clickPoint) {
    80    LatLon pos = Main.map.mapView.getLatLon(clickPoint.x, clickPoint.y);
    81    String line;
    82    
    83    File working_dir = new File (Main.pref.getPreferencesDir(), "plugins");
    84    working_dir = new File(working_dir, "Lakewalker");
    85    String target = Main.pref.get(LakewalkerPlugin.PREF_PYTHON) + " lakewalker.py";
    86    LatLon topLeft = Main.map.mapView.getLatLon(0, 0);
    87    LatLon botRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), Main.map.mapView.getHeight());
    88    
    89    target += " --lat=" + pos.lat();
    90    target += " --lon=" + pos.lon();
    91    target += " --left=" + topLeft.lon();
    92    target += " --right=" + botRight.lon();
    93    target += " --top=" + topLeft.lat();
    94    target += " --bottom=" + botRight.lat();
    95    target += " --josm";
    96    
    97    Collection<Command> commands = new LinkedList<Command>();
    98    Way way = new Way();
    99    Node lastNode = null;
    100    Node firstNode = null;
    101    
    102    try
    103    {
    104     Runtime rt = Runtime.getRuntime();
    105     System.out.println("dir: "+working_dir+", target: "+target);
    106     Process p = rt.exec(target, null, working_dir);
    107     System.out.println("Just Run");
    108     BufferedReader input =
    109      
    110       new BufferedReader
    111  
    112       (new InputStreamReader(p.getInputStream()));
    113     BufferedReader err = new BufferedReader(new InputStreamReader (p.getErrorStream())) ;
     85    LatLon pos = Main.map.mapView.getLatLon(clickPoint.x, clickPoint.y);
     86
     87    /*
     88     * Collect options
     89     */
     90    File working_dir = new File(Main.pref.getPreferencesDir(), "plugins");
     91    working_dir = new File(working_dir, "Lakewalker");
     92    String target = Main.pref.get(LakewalkerPreferences.PREF_PYTHON) + " lakewalker.py";
     93    LatLon topLeft = Main.map.mapView.getLatLon(0, 0);
     94    LatLon botRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), Main.map.mapView
     95        .getHeight());
     96
     97    /*
     98     * Build command line
     99     */
     100    target += " --lat=" + pos.lat();
     101    target += " --lon=" + pos.lon();
     102    target += " --left=" + topLeft.lon();
     103    target += " --right=" + botRight.lon();
     104    target += " --top=" + topLeft.lat();
     105    target += " --bottom=" + botRight.lat();
     106    target += " --maxnodes=" + Main.pref.get(LakewalkerPreferences.PREF_MAX_NODES, "50000");
     107    target += " --threshold=" + Main.pref.get(LakewalkerPreferences.PREF_THRESHOLD, "35");
     108    target += " --josm";
     109
     110
    114111   
    115     /*
    116      * Lakewalker will output data it stdout. Each line has a code
    117      * in character 1 indicating the type of data on the line:
    118      *
    119      * m text - Status message
    120      * l name [size] - Access landsat image name. size is returned if it needs to be downloaded.
    121      * e text - Error message
    122      * s nnn - Start node data stream, nnn seperate tracings to follow
    123      * t nnn - Start tracing, nnn nodes to follow
    124      * x [o] - End of Tracing. o indicates do not connect last node to first
    125      * n lat lon [o] - Node. o indicates it is an open node (not connected to the previous node)
    126      * z - End of data stream
    127      */
    128     while ((line = input.readLine()) != null) {
    129       System.out.println(line);
    130       char option = line.charAt(0);
    131       switch(option) {
    132       case 'n':
    133           String[] tokens = line.split(" ");
    134           try {
    135             LatLon ll = new LatLon(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
    136             Node n = new Node(ll);
    137             commands.add(new AddCommand(n));
    138             if (lastNode != null) {
    139               Segment s = new Segment(lastNode, n);
    140               commands.add(new AddCommand(s));
    141               way.segments.add(s);
    142             }
    143             else {
    144               firstNode = n;
    145             }
    146             lastNode = n;
    147           }
    148           catch (Exception ex) {
     112
     113
     114    try {
     115      /*
     116       * Start the Lakewalker
     117       */
     118      Runtime rt = Runtime.getRuntime();
     119      System.out.println("dir: " + working_dir + ", target: " + target);
     120      Process p = rt.exec(target, null, working_dir);
     121      final BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
     122     
     123      /*
     124       * Start a thread to read the output
     125       */
     126      final LakewalkerReader reader = new LakewalkerReader();
     127     
     128      PleaseWaitRunnable lakewalkerTask = new PleaseWaitRunnable(tr("Tracing")){
     129        @Override protected void realRun() throws SAXException {
     130          reader.read(input);
     131        }
     132        @Override protected void finish() {
    149133         
    150           }
    151           break;
     134        }
     135        @Override protected void cancel() {
     136          reader.cancel();
    152137         
    153       case 'x':
    154           Segment s = new Segment(lastNode, firstNode);
    155           commands.add(new AddCommand(s));
    156           way.segments.add(s);
    157           commands.add(new AddCommand(way));
    158           break;
    159       }
    160      
    161      }
    162    
    163     while ((line = err.readLine()) != null) {
    164        System.out.println(line);
    165      }
    166  
    167     input.close();
    168     p.destroy();
    169 
    170    }
    171    catch (Exception ex) {
    172      System.out.println("Exception caught: "+ex.getMessage());
    173    }
    174    
    175    if (!commands.isEmpty()) {
    176      Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands));
    177      Main.ds.setSelected(way);
    178    }
    179   }
    180  
     138        }
     139      };
     140      Main.worker.execute(lakewalkerTask);
     141    }
     142    catch (Exception ex) {
     143      System.out.println("Exception caught: " + ex.getMessage());
     144    }
     145
     146
     147  }
     148
    181149  protected void lakewalk(List nodes) {
    182    
     150
    183151  }
    184152
     
    189157  }
    190158
    191   public void mouseEntered(MouseEvent e) { 
    192   }
    193 
    194   public void mouseExited(MouseEvent e) { 
     159  public void mouseEntered(MouseEvent e) {
     160  }
     161
     162  public void mouseExited(MouseEvent e) {
    195163  }
    196164
     
    200168  public void mouseReleased(MouseEvent e) {
    201169  }
    202  
    203 //  class DuplicateDialog extends JDialog {
    204 //    private static final long serialVersionUID = 1L;
    205 //    protected Box mainPanel;
    206 //    protected IntConfigurer offset;
    207 //    protected boolean cancelled;
    208 //    protected String right;
    209 //    protected String left;
    210 //    protected JComboBox moveCombo;
    211 //
    212 //    public DuplicateDialog(String title) {
    213 //      super();
    214 //      this.setTitle(title);
    215 //      this.setModal(true);
    216 //      initComponents();
    217 //    }
    218 //
    219 //    protected void initComponents() {
    220 //      mainPanel = Box.createVerticalBox();
    221 //      offset = new IntConfigurer("", tr("Offset (metres):  "), new Integer(15));
    222 //      mainPanel.add(offset.getControls());
    223 //      getContentPane().add(mainPanel);
    224 //
    225 //      right = tr("right/down");
    226 //      left = tr("left/up");
    227 //      Box movePanel = Box.createHorizontalBox();
    228 //      movePanel.add(new JLabel(tr("Create new segments to the ")));
    229 //      moveCombo = new JComboBox(new String[] {right, left});
    230 //      movePanel.add(moveCombo);
    231 //      movePanel.add(new JLabel(tr(" of existing segments.")));
    232 //      mainPanel.add(movePanel);
    233 //
    234 //      Box buttonPanel = Box.createHorizontalBox();
    235 //      JButton okButton = new JButton(tr("Ok"));
    236 //      okButton.addActionListener(new ActionListener() {
    237 //        public void actionPerformed(ActionEvent e) {
    238 //          cancelled = false;
    239 //          setVisible(false);
    240 //
    241 //        }
    242 //      });
    243 //      JButton canButton = new JButton(tr("Cancel"));
    244 //      canButton.addActionListener(new ActionListener() {
    245 //        public void actionPerformed(ActionEvent e) {
    246 //          cancelled = true;
    247 //          setVisible(false);
    248 //        }
    249 //      });
    250 //      buttonPanel.add(okButton);
    251 //      buttonPanel.add(canButton);
    252 //      mainPanel.add(buttonPanel);
    253 //
    254 //      pack();
    255 //    }
    256 //
    257 //    protected int getOffset() {
    258 //      int off = offset.getIntValue(15);
    259 //      return right.equals(moveCombo.getSelectedItem()) ? off : -off;
    260 //    }
    261 //
    262 //    protected boolean isCancelled() {
    263 //      return cancelled;
    264 //    }
    265 //
    266 //  }
     170
     171
     172  // class DuplicateDialog extends JDialog {
     173  // private static final long serialVersionUID = 1L;
     174  // protected Box mainPanel;
     175  // protected IntConfigurer offset;
     176  // protected boolean cancelled;
     177  // protected String right;
     178  // protected String left;
     179  // protected JComboBox moveCombo;
     180  //
     181  // public DuplicateDialog(String title) {
     182  // super();
     183  // this.setTitle(title);
     184  // this.setModal(true);
     185  // initComponents();
     186  // }
     187  //
     188  // protected void initComponents() {
     189  // mainPanel = Box.createVerticalBox();
     190  // offset = new IntConfigurer("", tr("Offset (metres): "), new Integer(15));
     191  // mainPanel.add(offset.getControls());
     192  // getContentPane().add(mainPanel);
     193  //
     194  // right = tr("right/down");
     195  // left = tr("left/up");
     196  // Box movePanel = Box.createHorizontalBox();
     197  // movePanel.add(new JLabel(tr("Create new segments to the ")));
     198  // moveCombo = new JComboBox(new String[] {right, left});
     199  // movePanel.add(moveCombo);
     200  // movePanel.add(new JLabel(tr(" of existing segments.")));
     201  // mainPanel.add(movePanel);
     202  //
     203  // Box buttonPanel = Box.createHorizontalBox();
     204  // JButton okButton = new JButton(tr("Ok"));
     205  // okButton.addActionListener(new ActionListener() {
     206  // public void actionPerformed(ActionEvent e) {
     207  // cancelled = false;
     208  // setVisible(false);
     209  //
     210  // }
     211  // });
     212  // JButton canButton = new JButton(tr("Cancel"));
     213  // canButton.addActionListener(new ActionListener() {
     214  // public void actionPerformed(ActionEvent e) {
     215  // cancelled = true;
     216  // setVisible(false);
     217  // }
     218  // });
     219  // buttonPanel.add(okButton);
     220  // buttonPanel.add(canButton);
     221  // mainPanel.add(buttonPanel);
     222  //
     223  // pack();
     224  // }
     225  //
     226  // protected int getOffset() {
     227  // int off = offset.getIntValue(15);
     228  // return right.equals(moveCombo.getSelectedItem()) ? off : -off;
     229  // }
     230  //
     231  // protected boolean isCancelled() {
     232  // return cancelled;
     233  // }
     234  //
     235  // }
    267236}
  • applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerPlugin.java

    r4071 r4095  
    1717public class LakewalkerPlugin extends Plugin {
    1818
    19   public static final String VERSION = "0.2";
    20   public static final String PREF_PYTHON = "lakewalker.python";
     19  public static final String VERSION = "0.3";
    2120 
    2221  protected String name;
     
    4746  public PreferenceSetting getPreferenceSetting()
    4847  {
    49     return new LakewalkerPreferenceSetting();
     48    return new LakewalkerPreferences();
    5049  }
    5150
  • applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerPreferences.java

    r4061 r4095  
    1212import org.openstreetmap.josm.tools.GBC;
    1313import org.openstreetmap.josm.tools.I18n;
    14 public class LakewalkerPreferenceSetting implements PreferenceSetting {
     14public class LakewalkerPreferences implements PreferenceSetting {
    1515
     16  public static final String PREF_PYTHON = "lakewalker.python";
     17  public static final String PREF_MAX_SEG = "lakewalker.max_segs_in_way";
     18  public static final String PREF_MAX_NODES = "lakewalker.max_nodes";
     19  public static final String PREF_THRESHOLD = "lakewalker.threshold";
     20 
    1621  protected JTextField python = new JTextField(10);
     22  protected IntConfigurer maxSegs = new IntConfigurer();
     23  protected IntConfigurer maxNodes = new IntConfigurer();
     24  protected IntConfigurer threshold = new IntConfigurer();
    1725 
    1826  public void addGui(PreferenceDialog gui) {
    19     python.setToolTipText(tr("<html>Path to python executable.</html>"));
     27    python.setToolTipText(tr("Path to python executable."));
     28    maxSegs.setToolTipText(tr("Maximum number of segments per way."));
     29    maxNodes.setToolTipText(tr("Maximum number of nodes to trace."));
     30    maxNodes.setToolTipText(tr("Gray threshold."));
    2031    String description = tr("An interlude to the Lakewalker Python module to trace water bodies on Landsat imagery.<br><br>Version: {0}", LakewalkerPlugin.VERSION);
     32   
    2133    JPanel prefPanel = gui.createPreferenceTab("lakewalker.png", I18n.tr("Lakewalker Plugin Preferences"), description);
    2234    prefPanel.add(new JLabel(tr("Python executable")), GBC.std().insets(10,5,5,0));
    2335    prefPanel.add(python, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
    24    
     36    prefPanel.add(new JLabel(tr("Maximum number of segments per way")), GBC.std().insets(10,5,5,0));
     37    prefPanel.add(maxSegs.getControls(), GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
     38    prefPanel.add(new JLabel(tr("Maximum number of nodes to trace")), GBC.std().insets(10,5,5,0));
     39    prefPanel.add(maxNodes.getControls(), GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
     40    prefPanel.add(new JLabel(tr("Maximum gray value to count as water")), GBC.std().insets(10,5,5,0));
     41    prefPanel.add(threshold.getControls(), GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
     42       
     43    python.setText(Main.pref.get(PREF_PYTHON, "python.exe"));
     44    maxSegs.setValue(Main.pref.get(PREF_MAX_SEG, "250"));
     45    maxNodes.setValue(Main.pref.get(PREF_MAX_NODES, "50000"));
     46    threshold.setValue(Main.pref.get(PREF_THRESHOLD, "35"));
    2547  }
    2648
    2749  public void ok() {
    28     Main.pref.put(LakewalkerPlugin.PREF_PYTHON, python.getText());
    29    
     50    Main.pref.put(PREF_PYTHON, python.getText());
     51    Main.pref.put(PREF_MAX_SEG, maxSegs.getValueString());   
     52    Main.pref.put(PREF_MAX_NODES, maxNodes.getValueString());
     53    Main.pref.put(PREF_THRESHOLD, threshold.getValueString());
    3054  }
    3155 
Note: See TracChangeset for help on using the changeset viewer.