Changeset 18825 in osm for applications/editors/josm/plugins/lakewalker/src
- Timestamp:
- 2009-11-27T21:14:28+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerAction.java
r17375 r18825 39 39 class LakewalkerAction extends JosmAction implements MouseListener { 40 40 41 private static final long serialVersionUID = 1L; 42 protected String name; 43 protected Cursor oldCursor; 44 protected Thread executeThread; 45 protected boolean cancel; 46 47 protected Collection<Command> commands = new LinkedList<Command>(); 48 protected Collection<Way> ways = new ArrayList<Way>(); 49 50 public LakewalkerAction(String name) { 51 super(name, "lakewalker-sml", tr("Lake Walker."), 52 Shortcut.registerShortcut("tools:lakewalker", tr("Tool: {0}", tr("Lake Walker")), 53 KeyEvent.VK_L, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 54 this.name = name; 55 setEnabled(true); 56 } 57 58 public void actionPerformed(ActionEvent e) { 59 if(Main.map == null || Main.map.mapView == null) 60 return; 61 62 Main.map.mapView.setCursor(oldCursor); 63 64 if (Main.map == null) { 65 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded.")); 66 return; 67 } 68 69 oldCursor = Main.map.mapView.getCursor(); 70 Main.map.mapView.setCursor(ImageProvider.getCursor("crosshair", "lakewalker-sml")); 71 Main.map.mapView.addMouseListener(this); 72 } 73 74 /** 75 * check for presence of cache folder and trim cache to specified size. 76 * size/age limit is on a per folder basis. 77 */ 78 private void cleanupCache() { 79 final long maxCacheAge = System.currentTimeMillis()-Main.pref.getInteger(LakewalkerPreferences.PREF_MAXCACHEAGE, 100)*24*60*60*1000L; 80 final long maxCacheSize = Main.pref.getInteger(LakewalkerPreferences.PREF_MAXCACHESIZE, 300)*1024*1024; 81 82 for (String wmsFolder : LakewalkerPreferences.WMSLAYERS) { 83 String wmsCacheDirName = Main.pref.getPreferencesDir()+"plugins/Lakewalker/"+wmsFolder; 84 File wmsCacheDir = new File(wmsCacheDirName); 85 86 if (wmsCacheDir.exists() && wmsCacheDir.isDirectory()) { 87 File wmsCache[] = wmsCacheDir.listFiles(); 88 89 // sort files by date (most recent first) 90 Arrays.sort(wmsCache, new Comparator<File>() { 91 public int compare(File f1, File f2) { 92 return (int)(f2.lastModified()-f1.lastModified()); 93 } 94 }); 95 96 // delete aged or oversized, keep newest. Once size/age limit was reached delete all older files 97 long folderSize = 0; 98 boolean quickdelete = false; 99 for (File cacheEntry : wmsCache) { 100 if (!cacheEntry.isFile()) continue; 101 if (!quickdelete) { 102 folderSize += cacheEntry.length(); 103 if (folderSize > maxCacheSize) { 104 quickdelete = true; 105 } else if (cacheEntry.lastModified() < maxCacheAge) { 106 quickdelete = true; 107 } 108 } 109 110 if (quickdelete) { 111 cacheEntry.delete(); 112 } 113 } 114 115 } else { 116 // create cache directory 117 if (!wmsCacheDir.mkdirs()) { 118 JOptionPane.showMessageDialog(Main.parent, tr("Error creating cache directory: {0}", wmsCacheDirName)); 119 } 120 } 121 } 122 } 123 124 protected void lakewalk(Point clickPoint){ 41 private static final long serialVersionUID = 1L; 42 protected String name; 43 protected Cursor oldCursor; 44 protected Thread executeThread; 45 protected boolean cancel; 46 protected boolean active = false; 47 48 protected Collection<Command> commands = new LinkedList<Command>(); 49 protected Collection<Way> ways = new ArrayList<Way>(); 50 51 public LakewalkerAction(String name) { 52 super(name, "lakewalker-sml", tr("Lake Walker."), 53 Shortcut.registerShortcut("tools:lakewalker", tr("Tool: {0}", tr("Lake Walker")), 54 KeyEvent.VK_L, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 55 this.name = name; 56 setEnabled(true); 57 } 58 59 public void actionPerformed(ActionEvent e) { 60 if(Main.map == null || Main.map.mapView == null || active) 61 return; 62 63 active = true; 64 oldCursor = Main.map.mapView.getCursor(); 65 Main.map.mapView.setCursor(ImageProvider.getCursor("crosshair", "lakewalker-sml")); 66 Main.map.mapView.addMouseListener(this); 67 } 68 125 69 /** 126 * Positional data 127 */ 128 final LatLon pos = Main.map.mapView.getLatLon(clickPoint.x, clickPoint.y); 129 final LatLon topLeft = Main.map.mapView.getLatLon(0, 0); 130 final LatLon botRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), Main.map.mapView 131 .getHeight()); 132 133 /** 134 * Cache/working directory location 135 */ 136 final File working_dir = new File(Main.pref.getPreferencesDir(), "plugins/Lakewalker"); 137 138 /* 139 * Collect options 140 */ 141 final int waylen = Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_SEG, 500); 142 final int maxnode = Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_NODES, 50000); 143 final int threshold = Main.pref.getInteger(LakewalkerPreferences.PREF_THRESHOLD_VALUE, 90); 144 final double epsilon = Main.pref.getDouble(LakewalkerPreferences.PREF_EPSILON, 0.0003); 145 final int resolution = Main.pref.getInteger(LakewalkerPreferences.PREF_LANDSAT_RES, 4000); 146 final int tilesize = Main.pref.getInteger(LakewalkerPreferences.PREF_LANDSAT_SIZE, 2000); 147 final String startdir = Main.pref.get(LakewalkerPreferences.PREF_START_DIR, "east"); 148 final String wmslayer = Main.pref.get(LakewalkerPreferences.PREF_WMS, "IR1"); 149 150 try { 151 PleaseWaitRunnable lakewalkerTask = new PleaseWaitRunnable(tr("Tracing")){ 152 @Override protected void realRun() throws SAXException { 153 progressMonitor.subTask(tr("checking cache...")); 154 cleanupCache(); 155 processnodelist(pos, topLeft, botRight, waylen, maxnode, threshold, 156 epsilon,resolution,tilesize,startdir,wmslayer, working_dir, 157 progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 158 } 159 @Override protected void finish() { 160 161 } 162 @Override protected void cancel() { 163 cancel(); 164 } 165 }; 166 Thread executeThread = new Thread(lakewalkerTask); 167 executeThread.start(); 168 } 169 catch (Exception ex) { 170 System.out.println("Exception caught: " + ex.getMessage()); 171 } 172 } 173 174 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, ProgressMonitor progressMonitor){ 175 progressMonitor.beginTask(null, 3); 176 try { 177 ArrayList<double[]> nodelist = new ArrayList<double[]>(); 178 179 Lakewalker lw = new Lakewalker(waylen,maxnode,threshold,epsilon,resolution,tilesize,startdir,wmslayer,workingdir); 180 try { 181 nodelist = lw.trace(pos.lat(),pos.lon(),topLeft.lon(),botRight.lon(),topLeft.lat(),botRight.lat(), 182 progressMonitor.createSubTaskMonitor(1, false)); 183 } catch(LakewalkerException e){ 184 System.out.println(e.getError()); 185 } 186 187 System.out.println(nodelist.size()+" nodes generated"); 188 189 /** 190 * Run the nodelist through a vertex reduction algorithm 191 */ 192 193 progressMonitor.subTask(tr("Running vertex reduction...")); 194 195 nodelist = lw.vertexReduce(nodelist, epsilon); 196 197 //System.out.println("After vertex reduction "+nodelist.size()+" nodes remain."); 198 199 /** 200 * And then through douglas-peucker approximation 201 */ 202 203 progressMonitor.worked(1); 204 progressMonitor.subTask(tr("Running Douglas-Peucker approximation...")); 205 206 nodelist = lw.douglasPeucker(nodelist, epsilon, 0); 207 208 //System.out.println("After Douglas-Peucker approximation "+nodelist.size()+" nodes remain."); 209 210 /** 211 * And then through a duplicate node remover 212 */ 213 214 progressMonitor.worked(1); 215 progressMonitor.subTask(tr("Removing duplicate nodes...")); 216 217 nodelist = lw.duplicateNodeRemove(nodelist); 218 219 //System.out.println("After removing duplicate nodes, "+nodelist.size()+" nodes remain."); 220 221 222 // if for some reason (image loading failed, ...) nodelist is empty, no more processing required. 223 if (nodelist.size() == 0) { 224 return; 225 } 226 227 /** 228 * Turn the arraylist into osm nodes 229 */ 230 231 Way way = new Way(); 232 Node n = null; 233 Node fn = null; 234 235 double eastOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_EAST_OFFSET, 0.0); 236 double northOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_NORTH_OFFSET, 0.0); 237 238 int nodesinway = 0; 239 240 for(int i = 0; i< nodelist.size(); i++){ 241 if (cancel) { 242 return; 243 } 244 245 try { 246 LatLon ll = new LatLon(nodelist.get(i)[0]+northOffset, nodelist.get(i)[1]+eastOffset); 247 n = new Node(ll); 248 if(fn==null){ 249 fn = n; 250 } 251 commands.add(new AddCommand(n)); 252 253 } catch (Exception ex) { 254 } 255 256 way.addNode(n); 257 258 if(nodesinway > Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_SEG, 500)){ 259 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 260 261 if(!waytype.equals("none")){ 262 way.put("natural",waytype); 263 } 264 265 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 266 commands.add(new AddCommand(way)); 267 268 way = new Way(); 269 270 way.addNode(n); 271 272 nodesinway = 0; 273 } 274 nodesinway++; 275 } 276 277 278 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 279 280 if(!waytype.equals("none")){ 281 way.put("natural",waytype); 282 } 283 284 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 285 286 way.addNode(fn); 287 288 commands.add(new AddCommand(way)); 289 290 if (!commands.isEmpty()) { 291 Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands)); 292 Main.main.getCurrentDataSet().setSelected(ways); 293 } else { 294 System.out.println("Failed"); 295 } 296 297 commands = new LinkedList<Command>(); 298 ways = new ArrayList<Way>(); 299 } finally { 300 progressMonitor.finishTask(); 301 } 302 } 303 304 public void cancel() { 305 cancel = true; 306 } 307 308 public void mouseClicked(MouseEvent e) { 309 Main.map.mapView.removeMouseListener(this); 310 Main.map.mapView.setCursor(oldCursor); 311 lakewalk(e.getPoint()); 312 } 313 314 public void mouseEntered(MouseEvent e) { 315 } 316 317 public void mouseExited(MouseEvent e) { 318 } 319 320 public void mousePressed(MouseEvent e) { 321 } 322 323 public void mouseReleased(MouseEvent e) { 324 } 70 * check for presence of cache folder and trim cache to specified size. 71 * size/age limit is on a per folder basis. 72 */ 73 private void cleanupCache() { 74 final long maxCacheAge = System.currentTimeMillis()-Main.pref.getInteger(LakewalkerPreferences.PREF_MAXCACHEAGE, 100)*24*60*60*1000L; 75 final long maxCacheSize = Main.pref.getInteger(LakewalkerPreferences.PREF_MAXCACHESIZE, 300)*1024*1024; 76 77 for (String wmsFolder : LakewalkerPreferences.WMSLAYERS) { 78 String wmsCacheDirName = Main.pref.getPreferencesDir()+"plugins/Lakewalker/"+wmsFolder; 79 File wmsCacheDir = new File(wmsCacheDirName); 80 81 if (wmsCacheDir.exists() && wmsCacheDir.isDirectory()) { 82 File wmsCache[] = wmsCacheDir.listFiles(); 83 84 // sort files by date (most recent first) 85 Arrays.sort(wmsCache, new Comparator<File>() { 86 public int compare(File f1, File f2) { 87 return (int)(f2.lastModified()-f1.lastModified()); 88 } 89 }); 90 91 // delete aged or oversized, keep newest. Once size/age limit was reached delete all older files 92 long folderSize = 0; 93 boolean quickdelete = false; 94 for (File cacheEntry : wmsCache) { 95 if (!cacheEntry.isFile()) continue; 96 if (!quickdelete) { 97 folderSize += cacheEntry.length(); 98 if (folderSize > maxCacheSize) { 99 quickdelete = true; 100 } else if (cacheEntry.lastModified() < maxCacheAge) { 101 quickdelete = true; 102 } 103 } 104 105 if (quickdelete) { 106 cacheEntry.delete(); 107 } 108 } 109 110 } else { 111 // create cache directory 112 if (!wmsCacheDir.mkdirs()) { 113 JOptionPane.showMessageDialog(Main.parent, tr("Error creating cache directory: {0}", wmsCacheDirName)); 114 } 115 } 116 } 117 } 118 119 protected void lakewalk(Point clickPoint){ 120 /** 121 * Positional data 122 */ 123 final LatLon pos = Main.map.mapView.getLatLon(clickPoint.x, clickPoint.y); 124 final LatLon topLeft = Main.map.mapView.getLatLon(0, 0); 125 final LatLon botRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 126 Main.map.mapView.getHeight()); 127 128 /** 129 * Cache/working directory location 130 */ 131 final File working_dir = new File(Main.pref.getPreferencesDir(), "plugins/Lakewalker"); 132 133 /* 134 * Collect options 135 */ 136 final int waylen = Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_SEG, 500); 137 final int maxnode = Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_NODES, 50000); 138 final int threshold = Main.pref.getInteger(LakewalkerPreferences.PREF_THRESHOLD_VALUE, 90); 139 final double epsilon = Main.pref.getDouble(LakewalkerPreferences.PREF_EPSILON, 0.0003); 140 final int resolution = Main.pref.getInteger(LakewalkerPreferences.PREF_LANDSAT_RES, 4000); 141 final int tilesize = Main.pref.getInteger(LakewalkerPreferences.PREF_LANDSAT_SIZE, 2000); 142 final String startdir = Main.pref.get(LakewalkerPreferences.PREF_START_DIR, "east"); 143 final String wmslayer = Main.pref.get(LakewalkerPreferences.PREF_WMS, "IR1"); 144 145 try { 146 PleaseWaitRunnable lakewalkerTask = new PleaseWaitRunnable(tr("Tracing")){ 147 @Override protected void realRun() throws SAXException { 148 progressMonitor.subTask(tr("checking cache...")); 149 cleanupCache(); 150 processnodelist(pos, topLeft, botRight, waylen, maxnode, threshold, 151 epsilon,resolution,tilesize,startdir,wmslayer, working_dir, 152 progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 153 } 154 @Override protected void finish() { 155 } 156 @Override protected void cancel() { 157 cancel(); 158 } 159 }; 160 Thread executeThread = new Thread(lakewalkerTask); 161 executeThread.start(); 162 } 163 catch (Exception ex) { 164 System.out.println("Exception caught: " + ex.getMessage()); 165 } 166 } 167 168 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, ProgressMonitor progressMonitor){ 169 progressMonitor.beginTask(null, 3); 170 try { 171 ArrayList<double[]> nodelist = new ArrayList<double[]>(); 172 173 Lakewalker lw = new Lakewalker(waylen,maxnode,threshold,epsilon,resolution,tilesize,startdir,wmslayer,workingdir); 174 try { 175 nodelist = lw.trace(pos.lat(),pos.lon(),topLeft.lon(),botRight.lon(),topLeft.lat(),botRight.lat(), 176 progressMonitor.createSubTaskMonitor(1, false)); 177 } catch(LakewalkerException e){ 178 System.out.println(e.getError()); 179 } 180 181 System.out.println(nodelist.size()+" nodes generated"); 182 183 /** 184 * Run the nodelist through a vertex reduction algorithm 185 */ 186 187 progressMonitor.subTask(tr("Running vertex reduction...")); 188 189 nodelist = lw.vertexReduce(nodelist, epsilon); 190 191 //System.out.println("After vertex reduction "+nodelist.size()+" nodes remain."); 192 193 /** 194 * And then through douglas-peucker approximation 195 */ 196 197 progressMonitor.worked(1); 198 progressMonitor.subTask(tr("Running Douglas-Peucker approximation...")); 199 200 nodelist = lw.douglasPeucker(nodelist, epsilon, 0); 201 202 //System.out.println("After Douglas-Peucker approximation "+nodelist.size()+" nodes remain."); 203 204 /** 205 * And then through a duplicate node remover 206 */ 207 208 progressMonitor.worked(1); 209 progressMonitor.subTask(tr("Removing duplicate nodes...")); 210 211 nodelist = lw.duplicateNodeRemove(nodelist); 212 213 //System.out.println("After removing duplicate nodes, "+nodelist.size()+" nodes remain."); 214 215 216 // if for some reason (image loading failed, ...) nodelist is empty, no more processing required. 217 if (nodelist.size() == 0) { 218 return; 219 } 220 221 /** 222 * Turn the arraylist into osm nodes 223 */ 224 225 Way way = new Way(); 226 Node n = null; 227 Node fn = null; 228 229 double eastOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_EAST_OFFSET, 0.0); 230 double northOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_NORTH_OFFSET, 0.0); 231 232 int nodesinway = 0; 233 234 for(int i = 0; i< nodelist.size(); i++){ 235 if (cancel) { 236 return; 237 } 238 239 try { 240 LatLon ll = new LatLon(nodelist.get(i)[0]+northOffset, nodelist.get(i)[1]+eastOffset); 241 n = new Node(ll); 242 if(fn==null){ 243 fn = n; 244 } 245 commands.add(new AddCommand(n)); 246 247 } catch (Exception ex) { 248 } 249 250 way.addNode(n); 251 252 if(nodesinway > Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_SEG, 500)){ 253 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 254 255 if(!waytype.equals("none")){ 256 way.put("natural",waytype); 257 } 258 259 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 260 commands.add(new AddCommand(way)); 261 262 way = new Way(); 263 264 way.addNode(n); 265 266 nodesinway = 0; 267 } 268 nodesinway++; 269 } 270 271 272 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 273 274 if(!waytype.equals("none")){ 275 way.put("natural",waytype); 276 } 277 278 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 279 280 way.addNode(fn); 281 282 commands.add(new AddCommand(way)); 283 284 if (!commands.isEmpty()) { 285 Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands)); 286 Main.main.getCurrentDataSet().setSelected(ways); 287 } else { 288 System.out.println("Failed"); 289 } 290 291 commands = new LinkedList<Command>(); 292 ways = new ArrayList<Way>(); 293 } finally { 294 progressMonitor.finishTask(); 295 } 296 } 297 298 public void cancel() { 299 cancel = true; 300 } 301 302 public void mouseClicked(MouseEvent e) { 303 if(active) 304 { 305 active = false; 306 Main.map.mapView.removeMouseListener(this); 307 Main.map.mapView.setCursor(oldCursor); 308 lakewalk(e.getPoint()); 309 } 310 } 311 312 public void mouseEntered(MouseEvent e) { 313 } 314 315 public void mouseExited(MouseEvent e) { 316 } 317 318 public void mousePressed(MouseEvent e) { 319 } 320 321 public void mouseReleased(MouseEvent e) { 322 } 325 323 }
Note:
See TracChangeset
for help on using the changeset viewer.