Changeset 16586 in osm
- Timestamp:
- 2009-07-19T19:12:06+02:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/lakewalker
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/lakewalker/build.xml
r14015 r16586 26 26 <attribute name="Plugin-Date" value="${version.entry.commit.date}"/> 27 27 <attribute name="Plugin-Description" value="Helps vectorizing WMS images." /> 28 <attribute name="Plugin-Mainversion" value="1 465"/>28 <attribute name="Plugin-Mainversion" value="1813"/> 29 29 <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/> 30 30 </manifest> -
applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/Lakewalker.java
r15959 r16586 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.io.BufferedReader;6 5 import java.io.File; 7 import java.util. *;8 9 import org.openstreetmap.josm.Main;10 import org.openstreetmap.josm.command.AddCommand; 6 import java.util.ArrayList; 7 import java.util.Collection; 8 import java.util.LinkedList; 9 11 10 import org.openstreetmap.josm.command.Command; 12 import org.openstreetmap.josm.command.SequenceCommand;13 import org.openstreetmap.josm.data.coor.LatLon;14 import org.openstreetmap.josm.data.osm.Node;15 11 import org.openstreetmap.josm.data.osm.Way; 12 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 16 13 17 14 public class Lakewalker { … … 94 91 * @param br_lat 95 92 */ 96 public ArrayList<double[]> trace(double lat, double lon, double tl_lon, double br_lon, double tl_lat, double br_lat) throws LakewalkerException { 97 98 LakewalkerWMS wms = new LakewalkerWMS(this.resolution, this.tilesize, this.wmslayer, this.workingdir); 99 LakewalkerBBox bbox = new LakewalkerBBox(tl_lat,tl_lon,br_lat,br_lon); 100 101 Boolean detect_loop = false; 102 103 ArrayList<double[]> nodelist = new ArrayList<double[]>(); 104 105 int[] xy = geo_to_xy(lat,lon,this.resolution); 106 107 if(!bbox.contains(lat, lon)){ 108 throw new LakewalkerException(tr("The starting location was not within the bbox")); 109 } 110 111 int v; 112 113 setStatus(tr("Looking for shoreline...")); 114 115 while(true){ 116 double[] geo = xy_to_geo(xy[0],xy[1],this.resolution); 117 if(bbox.contains(geo[0],geo[1])==false){ 118 break; 119 } 120 121 v = wms.getPixel(xy[0], xy[1]); 122 if(v > this.threshold){ 123 break; 124 } 125 126 int delta_lat = this.dirslat[getDirectionIndex(this.startdir)]; 127 int delta_lon = this.dirslon[getDirectionIndex(this.startdir)]; 128 129 xy[0] = xy[0]+delta_lon; 130 xy[1] = xy[1]+delta_lat; 131 132 } 133 134 int[] startxy = new int[] {xy[0], xy[1]}; 135 double[] startgeo = xy_to_geo(xy[0],xy[1],this.resolution); 136 137 //System.out.printf("Found shore at lat %.4f lon %.4f\n",lat,lon); 138 139 int last_dir = this.getDirectionIndex(this.startdir); 140 141 for(int i = 0; i < this.maxnode; i++){ 142 143 // Print a counter 144 if(i % 250 == 0){ 145 setStatus(tr("{0} nodes so far...",i)); 146 //System.out.println(i+" nodes so far..."); 147 } 148 149 // Some variables we need 150 int d; 151 int test_x=0; 152 int test_y=0; 153 int new_dir = 0; 154 155 // Loop through all the directions we can go 156 for(d = 1; d <= this.dirslat.length; d++){ 157 158 // Decide which direction we want to look at from this pixel 159 new_dir = (last_dir + d + 4) % 8; 160 161 test_x = xy[0] + this.dirslon[new_dir]; 162 test_y = xy[1] + this.dirslat[new_dir]; 163 164 double[] geo = xy_to_geo(test_x,test_y,this.resolution); 165 166 if(!bbox.contains(geo[0], geo[1])){ 167 System.out.println("Outside bbox"); 168 break; 169 } 170 171 v = wms.getPixel(test_x, test_y); 172 if(v > this.threshold){ 173 break; 174 } 175 176 if(d == this.dirslat.length-1){ 177 System.out.println("Got stuck"); 178 break; 179 } 180 } 181 182 // Remember this direction 183 last_dir = new_dir; 184 185 // Set the pixel we found as current 186 xy[0] = test_x; 187 xy[1] = test_y; 188 189 // Break the loop if we managed to get back to our starting point 190 if(xy[0] == startxy[0] && xy[1] == startxy[1]){ 191 break; 192 } 193 194 // Store this node 195 double[] geo = xy_to_geo(xy[0],xy[1],this.resolution); 196 nodelist.add(geo); 197 //System.out.println("Adding node at "+xy[0]+","+xy[1]+" ("+geo[1]+","+geo[0]+")"); 198 199 // Check if we got stuck in a loop 200 double start_proximity = Math.pow((geo[0] - startgeo[0]),2) + Math.pow((geo[1] - startgeo[1]),2); 201 202 if(detect_loop){ 203 if(start_proximity < Math.pow(start_radius_small,2)){ 204 System.out.println("Detected loop"); 205 break; 206 } 207 }else{ 208 if(start_proximity > Math.pow(start_radius_big,2)){ 209 detect_loop = true; 210 } 211 } 212 } 213 214 return nodelist; 93 public ArrayList<double[]> trace(double lat, double lon, double tl_lon, double br_lon, double tl_lat, double br_lat, ProgressMonitor progressMonitor) throws LakewalkerException { 94 95 progressMonitor.beginTask(null); 96 97 try { 98 99 LakewalkerWMS wms = new LakewalkerWMS(this.resolution, this.tilesize, this.wmslayer, this.workingdir); 100 LakewalkerBBox bbox = new LakewalkerBBox(tl_lat,tl_lon,br_lat,br_lon); 101 102 Boolean detect_loop = false; 103 104 ArrayList<double[]> nodelist = new ArrayList<double[]>(); 105 106 int[] xy = geo_to_xy(lat,lon,this.resolution); 107 108 if(!bbox.contains(lat, lon)){ 109 throw new LakewalkerException(tr("The starting location was not within the bbox")); 110 } 111 112 int v; 113 114 progressMonitor.indeterminateSubTask(tr("Looking for shoreline...")); 115 116 while(true){ 117 double[] geo = xy_to_geo(xy[0],xy[1],this.resolution); 118 if(bbox.contains(geo[0],geo[1])==false){ 119 break; 120 } 121 122 v = wms.getPixel(xy[0], xy[1], progressMonitor.createSubTaskMonitor(0, false)); 123 if(v > this.threshold){ 124 break; 125 } 126 127 int delta_lat = this.dirslat[getDirectionIndex(this.startdir)]; 128 int delta_lon = this.dirslon[getDirectionIndex(this.startdir)]; 129 130 xy[0] = xy[0]+delta_lon; 131 xy[1] = xy[1]+delta_lat; 132 133 } 134 135 int[] startxy = new int[] {xy[0], xy[1]}; 136 double[] startgeo = xy_to_geo(xy[0],xy[1],this.resolution); 137 138 //System.out.printf("Found shore at lat %.4f lon %.4f\n",lat,lon); 139 140 int last_dir = this.getDirectionIndex(this.startdir); 141 142 for(int i = 0; i < this.maxnode; i++){ 143 144 // Print a counter 145 if(i % 250 == 0){ 146 progressMonitor.indeterminateSubTask(tr("{0} nodes so far...",i)); 147 //System.out.println(i+" nodes so far..."); 148 } 149 150 // Some variables we need 151 int d; 152 int test_x=0; 153 int test_y=0; 154 int new_dir = 0; 155 156 // Loop through all the directions we can go 157 for(d = 1; d <= this.dirslat.length; d++){ 158 159 // Decide which direction we want to look at from this pixel 160 new_dir = (last_dir + d + 4) % 8; 161 162 test_x = xy[0] + this.dirslon[new_dir]; 163 test_y = xy[1] + this.dirslat[new_dir]; 164 165 double[] geo = xy_to_geo(test_x,test_y,this.resolution); 166 167 if(!bbox.contains(geo[0], geo[1])){ 168 System.out.println("Outside bbox"); 169 break; 170 } 171 172 v = wms.getPixel(test_x, test_y, progressMonitor.createSubTaskMonitor(0, false)); 173 if(v > this.threshold){ 174 break; 175 } 176 177 if(d == this.dirslat.length-1){ 178 System.out.println("Got stuck"); 179 break; 180 } 181 } 182 183 // Remember this direction 184 last_dir = new_dir; 185 186 // Set the pixel we found as current 187 xy[0] = test_x; 188 xy[1] = test_y; 189 190 // Break the loop if we managed to get back to our starting point 191 if(xy[0] == startxy[0] && xy[1] == startxy[1]){ 192 break; 193 } 194 195 // Store this node 196 double[] geo = xy_to_geo(xy[0],xy[1],this.resolution); 197 nodelist.add(geo); 198 //System.out.println("Adding node at "+xy[0]+","+xy[1]+" ("+geo[1]+","+geo[0]+")"); 199 200 // Check if we got stuck in a loop 201 double start_proximity = Math.pow((geo[0] - startgeo[0]),2) + Math.pow((geo[1] - startgeo[1]),2); 202 203 if(detect_loop){ 204 if(start_proximity < Math.pow(start_radius_small,2)){ 205 System.out.println("Detected loop"); 206 break; 207 } 208 }else{ 209 if(start_proximity > Math.pow(start_radius_big,2)){ 210 detect_loop = true; 211 } 212 } 213 } 214 215 return nodelist; 216 } finally { 217 progressMonitor.finishTask(); 218 } 215 219 } 216 220 … … 431 435 } 432 436 433 protected void setStatus(String s) {434 Main.pleaseWaitDlg.currentAction.setText(s);435 Main.pleaseWaitDlg.repaint();436 }437 438 437 /** 439 438 * Class to do checking of whether the point is within our bbox … … 468 467 } 469 468 } 470 private void printarr(int[] a){471 for(int i = 0; i<a.length; i++){472 System.out.println(i+": "+a[i]);473 }474 }475 469 } 476 470 -
applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerAction.java
r15961 r16586 15 15 import java.util.Comparator; 16 16 import java.util.LinkedList; 17 import java.util.List;18 17 19 18 import javax.swing.JOptionPane; … … 26 25 import org.openstreetmap.josm.data.coor.LatLon; 27 26 import org.openstreetmap.josm.data.osm.Node; 28 import org.openstreetmap.josm.data.osm.OsmPrimitive;29 27 import org.openstreetmap.josm.data.osm.Way; 30 28 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 29 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 31 30 import org.openstreetmap.josm.tools.ImageProvider; 32 31 import org.openstreetmap.josm.tools.Shortcut; … … 152 151 PleaseWaitRunnable lakewalkerTask = new PleaseWaitRunnable(tr("Tracing")){ 153 152 @Override protected void realRun() throws SAXException { 154 setStatus(tr("checking cache..."));153 progressMonitor.subTask(tr("checking cache...")); 155 154 cleanupCache(); 156 processnodelist(pos, topLeft, botRight, waylen,maxnode,threshold,epsilon,resolution,tilesize,startdir,wmslayer,working_dir); 155 processnodelist(pos, topLeft, botRight, waylen, maxnode, threshold, 156 epsilon,resolution,tilesize,startdir,wmslayer, working_dir, 157 progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 157 158 } 158 159 @Override protected void finish() { … … 171 172 } 172 173 173 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){ 174 175 ArrayList<double[]> nodelist = new ArrayList<double[]>(); 176 177 Lakewalker lw = new Lakewalker(waylen,maxnode,threshold,epsilon,resolution,tilesize,startdir,wmslayer,workingdir); 178 try { 179 nodelist = lw.trace(pos.lat(),pos.lon(),topLeft.lon(),botRight.lon(),topLeft.lat(),botRight.lat()); 180 } catch(LakewalkerException e){ 181 System.out.println(e.getError()); 182 } 183 184 System.out.println(nodelist.size()+" nodes generated"); 185 186 /** 187 * Run the nodelist through a vertex reduction algorithm 188 */ 189 190 setStatus(tr("Running vertex reduction...")); 191 192 nodelist = lw.vertexReduce(nodelist, epsilon); 193 194 //System.out.println("After vertex reduction "+nodelist.size()+" nodes remain."); 195 196 /** 197 * And then through douglas-peucker approximation 198 */ 199 200 setStatus(tr("Running Douglas-Peucker approximation...")); 201 202 nodelist = lw.douglasPeucker(nodelist, epsilon, 0); 203 204 //System.out.println("After Douglas-Peucker approximation "+nodelist.size()+" nodes remain."); 205 206 /** 207 * And then through a duplicate node remover 208 */ 209 210 setStatus(tr("Removing duplicate nodes...")); 211 212 nodelist = lw.duplicateNodeRemove(nodelist); 213 214 //System.out.println("After removing duplicate nodes, "+nodelist.size()+" nodes remain."); 215 216 217 // if for some reason (image loading failed, ...) nodelist is empty, no more processing required. 218 if (nodelist.size() == 0) { 219 return; 220 } 221 222 /** 223 * Turn the arraylist into osm nodes 224 */ 225 226 Way way = new Way(); 227 Node n = null; 228 Node fn = null; 229 230 double eastOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_EAST_OFFSET, 0.0); 231 double northOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_NORTH_OFFSET, 0.0); 232 233 int nodesinway = 0; 234 235 for(int i = 0; i< nodelist.size(); i++){ 236 if (cancel) { 237 return; 238 } 239 240 try { 241 LatLon ll = new LatLon(nodelist.get(i)[0]+northOffset, nodelist.get(i)[1]+eastOffset); 242 n = new Node(ll); 243 if(fn==null){ 244 fn = n; 245 } 246 commands.add(new AddCommand(n)); 247 248 } catch (Exception ex) { 249 } 250 251 way.nodes.add(n); 252 253 if(nodesinway > Main.pref.getInteger(LakewalkerPreferences.PREF_MAX_SEG, 500)){ 254 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 255 256 if(!waytype.equals("none")){ 257 way.put("natural",waytype); 258 } 259 260 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 261 commands.add(new AddCommand(way)); 262 263 way = new Way(); 264 265 way.nodes.add(n); 266 267 nodesinway = 0; 268 } 269 nodesinway++; 270 } 271 272 273 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 274 275 if(!waytype.equals("none")){ 276 way.put("natural",waytype); 277 } 278 279 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 280 281 way.nodes.add(fn); 282 283 commands.add(new AddCommand(way)); 284 285 if (!commands.isEmpty()) { 286 Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands)); 287 Main.ds.setSelected(ways); 288 } else { 289 System.out.println("Failed"); 290 } 291 292 commands = new LinkedList<Command>(); 293 ways = new ArrayList<Way>(); 294 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.nodes.add(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.nodes.add(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.nodes.add(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.ds.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 } 295 302 } 296 303 … … 316 323 public void mouseReleased(MouseEvent e) { 317 324 } 318 protected void setStatus(String s) {319 Main.pleaseWaitDlg.currentAction.setText(s);320 Main.pleaseWaitDlg.repaint();321 }322 325 } -
applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerApp.java
r15959 r16586 2 2 3 3 import java.io.File; 4 import java.util.*; 4 import java.util.ArrayList; 5 6 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 5 7 6 8 public class LakewalkerApp { … … 37 39 Lakewalker lw = new Lakewalker(waylen,maxnode,threshold,dp,resolution,tilesize,startdir,wmslayer,working_dir); 38 40 try { 39 nodelist = lw.trace(lat,lon,leftlon,rightlon,toplat,botlat );41 nodelist = lw.trace(lat,lon,leftlon,rightlon,toplat,botlat, NullProgressMonitor.INSTANCE); 40 42 } catch(LakewalkerException e){ 41 43 System.out.println(e.getError()); -
applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerReader.java
r13497 r16586 20 20 import org.openstreetmap.josm.data.osm.Node; 21 21 import org.openstreetmap.josm.data.osm.Way; 22 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 22 23 23 24 public class LakewalkerReader { … … 29 30 * Read the data 30 31 */ 31 public void read(BufferedReader input ) {32 public void read(BufferedReader input, ProgressMonitor progressMonitor) { 32 33 /* 33 34 * Lakewalker will output data it stdout. Each line has a code in … … 42 43 */ 43 44 44 Way way = new Way(); 45 String line; 46 setStatus("Initializing"); 47 double eastOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_EAST_OFFSET, 0.0); 48 double northOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_NORTH_OFFSET, 0.0); 49 char option = ' '; 45 progressMonitor.beginTask(null); 46 try { 47 Way way = new Way(); 48 String line; 49 progressMonitor.indeterminateSubTask(tr("Initializing")); 50 double eastOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_EAST_OFFSET, 0.0); 51 double northOffset = Main.pref.getDouble(LakewalkerPreferences.PREF_NORTH_OFFSET, 0.0); 52 char option = ' '; 50 53 51 52 53 54 54 try { 55 Node n = null; // The current node being created 56 Node tn = null; // The last node of the previous way 57 Node fn = null; // Node to hold the first node in the trace 55 58 56 57 58 59 60 61 62 63 59 while ((line = input.readLine()) != null) { 60 if (cancel) 61 return; 62 System.out.println(line); 63 option = line.charAt(0); 64 switch (option) { 65 case 'n': 66 String[] tokens = line.split(" "); 64 67 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 setStatus(line.substring(2));85 86 87 68 if(tn==null){ 69 try { 70 LatLon ll = new LatLon(Double.parseDouble(tokens[1])+northOffset, 71 Double.parseDouble(tokens[2])+eastOffset); 72 n = new Node(ll); 73 if(fn==null) 74 fn = n; 75 commands.add(new AddCommand(n)); 76 } 77 catch (Exception ex) {} 78 } else { 79 // If there is a last node, and this node has the same coordinates 80 // then we substitute for the previous node 81 n = tn; 82 tn = null; 83 } 84 way.nodes.add(n); 85 break; 86 case 's': 87 progressMonitor.indeterminateSubTask(line.substring(2)); 88 break; 89 case 'x': 90 String waytype = Main.pref.get(LakewalkerPreferences.PREF_WAYTYPE, "water"); 88 91 89 if(!waytype.equals("none")) 90 way.put("natural",waytype); 91 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 92 commands.add(new AddCommand(way)); 93 break; 94 case 't': 95 way = new Way(); 96 tn = n; 97 break; 98 case 'e': 99 String error = line.substring(2); 100 cancel = true; 101 break; 102 } 103 } 104 input.close(); 92 if(!waytype.equals("none")) 93 way.put("natural",waytype); 94 way.put("source", Main.pref.get(LakewalkerPreferences.PREF_SOURCE, "Landsat")); 95 commands.add(new AddCommand(way)); 96 break; 97 case 't': 98 way = new Way(); 99 tn = n; 100 break; 101 case 'e': 102 cancel = true; 103 break; 104 } 105 } 106 input.close(); 105 107 106 // Add the start node to the end of the trace to form a closed shape107 108 109 108 // Add the start node to the end of the trace to form a closed shape 109 way.nodes.add(fn); 110 } 111 catch (Exception ex) { } 110 112 111 if (!commands.isEmpty()) { 112 Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands)); 113 Main.ds.setSelected(ways); 114 } 113 if (!commands.isEmpty()) { 114 Main.main.undoRedo.add(new SequenceCommand(tr("Lakewalker trace"), commands)); 115 Main.ds.setSelected(ways); 116 } 117 } finally { 118 progressMonitor.finishTask(); 119 } 115 120 } 116 121 … … 121 126 cancel = true; 122 127 } 123 protected void setStatus(String s) {124 Main.pleaseWaitDlg.currentAction.setText(s);125 Main.pleaseWaitDlg.repaint();126 }127 128 } -
applications/editors/josm/plugins/lakewalker/src/org/openstreetmap/josm/plugins/lakewalker/LakewalkerWMS.java
r12778 r16586 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import org.openstreetmap.josm.Main;6 import java.awt.Image;7 import javax.imageio.*;8 import java.math.*;9 import java.io.*;10 import java.net.*;11 5 import java.awt.image.BufferedImage; 12 import java.awt.*; 13 import java.awt.image.*; 14 import java.awt.geom.*; 15 import java.util.*; 16 import java.text.*; 6 import java.io.File; 7 import java.io.FileNotFoundException; 8 import java.io.IOException; 9 import java.math.BigDecimal; 10 import java.net.MalformedURLException; 11 import java.net.URL; 12 import java.text.DecimalFormat; 13 import java.text.NumberFormat; 14 import java.util.HashMap; 15 import java.util.Locale; 16 import java.util.Vector; 17 18 import javax.imageio.ImageIO; 19 20 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 17 21 18 22 public class LakewalkerWMS { … … 41 45 } 42 46 43 public BufferedImage getTile(int x, int y ) throws LakewalkerException {44 String status = getStatus();45 setStatus(tr("Downloading image tile..."));46 47 String layer = "global_mosaic_base"; 48 49 int[] bottom_left_xy = new int[2];50 bottom_left_xy[0] = floor(x,this.tilesize);51 bottom_left_xy[1] = floor(y,this.tilesize); 52 53 int[] top_right_xy = new int[2];54 top_right_xy[0] = (int)bottom_left_xy[0] + this.tilesize;55 top_right_xy[1] = (int)bottom_left_xy[1] + this.tilesize; 56 57 double[] topright_geo = xy_to_geo(top_right_xy[0],top_right_xy[1],this.resolution);58 double[] bottomleft_geo = xy_to_geo(bottom_left_xy[0],bottom_left_xy[1],this.resolution); 59 60 String filename = this.wmslayer+"/landsat_"+this.resolution+"_"+this.tilesize+61 "_xy_"+bottom_left_xy[0]+"_"+bottom_left_xy[1]+".png"; 62 63 // The WMS server only understands decimal points using periods, so we need64 // to convert to a locale that uses that to build the proper URL65 NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);66 DecimalFormat df = (DecimalFormat)nf;67 df.applyLocalizedPattern("0.000000"); 68 69 String urlloc = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers="+layer+70 "&styles="+wmslayer+"&srs=EPSG:4326&format=image/png"+71 "&bbox="+df.format(bottomleft_geo[1])+","+df.format(bottomleft_geo[0])+72 ","+df.format(topright_geo[1])+","+df.format(topright_geo[0])+73 "&width="+this.tilesize+"&height="+this.tilesize; 74 75 File file = new File(this.working_dir,filename); 76 77 // Calculate the hashmap key78 String hashkey = Integer.toString(bottom_left_xy[0])+":"+Integer.toString(bottom_left_xy[1]); 79 80 // See if this image is already loaded81 if(this.image != null){82 if(this.imagex != bottom_left_xy[0] || this.imagey != bottom_left_xy[1]){ 83 84 // Check if this image exists in the hashmap85 if(this.imageindex.containsKey(hashkey)){86 // Store which image we have87 this.imagex = bottom_left_xy[0];88 this.imagey = bottom_left_xy[1]; 89 90 // Retrieve from cache91 this.image = this.images.get(this.imageindex.get(hashkey));92 return this.image;93 } else {94 this.image = null;95 }96 } else {97 return this.image;98 99 } 100 101 try {102 System.out.println("Looking for image in disk cache: "+filename); 103 104 // Read from a file105 this.image = ImageIO.read(file); 106 107 this.images.add(this.image);108 this.imageindex.put(hashkey,this.images.size()-1); 109 110 } catch(FileNotFoundException e){111 System.out.println("Could not find cached image, downloading.");112 } catch(IOException e){113 System.out.println(e.getMessage());114 } catch(Exception e){115 System.out.println(e.getMessage());116 } 117 118 if(this.image == null){119 /**120 * Try downloading the image121 */122 try {123 System.out.println("Downloading from "+urlloc); 124 125 // Read from a URL126 URL url = new URL(urlloc);127 this.image = ImageIO.read(url); // this can return null!128 } catch(MalformedURLException e){129 System.out.println(e.getMessage());130 } catch(IOException e){131 System.out.println(e.getMessage());132 } catch(Exception e){133 System.out.println(e.getMessage());134 } 135 136 if (this.image != null) {137 this.images.add(this.image);138 this.imageindex.put(hashkey,this.images.size()-1); 139 140 this.saveimage(file,this.image);141 142 } 143 144 this.imagex = bottom_left_xy[0];145 this.imagey = bottom_left_xy[1]; 146 147 if(this.image == null){148 throw new LakewalkerException(tr("Could not acquire image"));149 } 150 151 setStatus(status);152 153 return this.image;47 public BufferedImage getTile(int x, int y, ProgressMonitor progressMonitor) throws LakewalkerException { 48 progressMonitor.beginTask(tr("Downloading image tile...")); 49 try { 50 String layer = "global_mosaic_base"; 51 52 int[] bottom_left_xy = new int[2]; 53 bottom_left_xy[0] = floor(x,this.tilesize); 54 bottom_left_xy[1] = floor(y,this.tilesize); 55 56 int[] top_right_xy = new int[2]; 57 top_right_xy[0] = bottom_left_xy[0] + this.tilesize; 58 top_right_xy[1] = bottom_left_xy[1] + this.tilesize; 59 60 double[] topright_geo = xy_to_geo(top_right_xy[0],top_right_xy[1],this.resolution); 61 double[] bottomleft_geo = xy_to_geo(bottom_left_xy[0],bottom_left_xy[1],this.resolution); 62 63 String filename = this.wmslayer+"/landsat_"+this.resolution+"_"+this.tilesize+ 64 "_xy_"+bottom_left_xy[0]+"_"+bottom_left_xy[1]+".png"; 65 66 // The WMS server only understands decimal points using periods, so we need 67 // to convert to a locale that uses that to build the proper URL 68 NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); 69 DecimalFormat df = (DecimalFormat)nf; 70 df.applyLocalizedPattern("0.000000"); 71 72 String urlloc = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers="+layer+ 73 "&styles="+wmslayer+"&srs=EPSG:4326&format=image/png"+ 74 "&bbox="+df.format(bottomleft_geo[1])+","+df.format(bottomleft_geo[0])+ 75 ","+df.format(topright_geo[1])+","+df.format(topright_geo[0])+ 76 "&width="+this.tilesize+"&height="+this.tilesize; 77 78 File file = new File(this.working_dir,filename); 79 80 // Calculate the hashmap key 81 String hashkey = Integer.toString(bottom_left_xy[0])+":"+Integer.toString(bottom_left_xy[1]); 82 83 // See if this image is already loaded 84 if(this.image != null){ 85 if(this.imagex != bottom_left_xy[0] || this.imagey != bottom_left_xy[1]){ 86 87 // Check if this image exists in the hashmap 88 if(this.imageindex.containsKey(hashkey)){ 89 // Store which image we have 90 this.imagex = bottom_left_xy[0]; 91 this.imagey = bottom_left_xy[1]; 92 93 // Retrieve from cache 94 this.image = this.images.get(this.imageindex.get(hashkey)); 95 return this.image; 96 } else { 97 this.image = null; 98 } 99 } else { 100 return this.image; 101 } 102 } 103 104 try { 105 System.out.println("Looking for image in disk cache: "+filename); 106 107 // Read from a file 108 this.image = ImageIO.read(file); 109 110 this.images.add(this.image); 111 this.imageindex.put(hashkey,this.images.size()-1); 112 113 } catch(FileNotFoundException e){ 114 System.out.println("Could not find cached image, downloading."); 115 } catch(IOException e){ 116 System.out.println(e.getMessage()); 117 } catch(Exception e){ 118 System.out.println(e.getMessage()); 119 } 120 121 if(this.image == null){ 122 /** 123 * Try downloading the image 124 */ 125 try { 126 System.out.println("Downloading from "+urlloc); 127 128 // Read from a URL 129 URL url = new URL(urlloc); 130 this.image = ImageIO.read(url); // this can return null! 131 } catch(MalformedURLException e){ 132 System.out.println(e.getMessage()); 133 } catch(IOException e){ 134 System.out.println(e.getMessage()); 135 } catch(Exception e){ 136 System.out.println(e.getMessage()); 137 } 138 139 if (this.image != null) { 140 this.images.add(this.image); 141 this.imageindex.put(hashkey,this.images.size()-1); 142 143 this.saveimage(file,this.image); 144 } 145 } 146 147 this.imagex = bottom_left_xy[0]; 148 this.imagey = bottom_left_xy[1]; 149 150 if(this.image == null){ 151 throw new LakewalkerException(tr("Could not acquire image")); 152 } 153 154 return this.image; 155 } finally { 156 progressMonitor.finishTask(); 157 } 154 158 } 155 159 … … 166 170 } 167 171 168 public int getPixel(int x, int y) throws LakewalkerException{ 169 172 public int getPixel(int x, int y, ProgressMonitor progressMonitor) throws LakewalkerException{ 170 173 // Get the previously shown text 171 174 … … 174 177 175 178 try { 176 image = this.getTile(x,y );179 image = this.getTile(x,y, progressMonitor); 177 180 } catch(LakewalkerException e){ 178 181 System.out.println(e.getError()); … … 229 232 return xy; 230 233 } 231 232 private void printarr(int[] a){233 for(int i = 0; i<a.length; i++){234 System.out.println(i+": "+a[i]);235 }236 }237 protected void setStatus(String s) {238 Main.pleaseWaitDlg.currentAction.setText(s);239 Main.pleaseWaitDlg.repaint();240 }241 protected String getStatus(){242 return Main.pleaseWaitDlg.currentAction.getText();243 }244 234 }
Note:
See TracChangeset
for help on using the changeset viewer.