Changeset 10684 in osm for applications
- Timestamp:
- 2008-09-14T14:45:47+02:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/wmsplugin/src/wmsplugin
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Grabber.java
r10683 r10684 1 1 package wmsplugin; 2 2 3 public interface Grabber extends Runnable { 4 3 import org.openstreetmap.josm.data.Bounds; 4 import org.openstreetmap.josm.data.projection.Projection; 5 import org.openstreetmap.josm.gui.MapView; 6 import org.openstreetmap.josm.Main; 7 import java.awt.image.BufferedImage; 8 import java.awt.Graphics; 9 import java.awt.Color; 10 import java.awt.Font; 11 import javax.swing.JOptionPane; 12 13 abstract public class Grabber implements Runnable { 14 protected Bounds b; 15 protected Projection proj; 16 protected double pixelPerDegree; 17 protected MapView mv; 18 protected WMSLayer layer; 19 protected GeorefImage image; 20 21 Grabber(Bounds b, Projection proj, 22 double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { 23 this.b = b; 24 this.proj = proj; 25 this.pixelPerDegree = pixelPerDegree; 26 this.image = image; 27 this.mv = mv; 28 this.layer = layer; 29 } 30 31 abstract void fetch() throws Exception; // the image fetch code 32 33 int width(){ 34 return (int) ((b.max.lon() - b.min.lon()) * pixelPerDegree); 35 } 36 int height(){ 37 return (int) ((b.max.lat() - b.min.lat()) * pixelPerDegree); 38 } 39 40 protected void grabError(Exception e){ // report error when grabing image 41 e.printStackTrace(); 42 43 BufferedImage img = new BufferedImage(width(), height(), BufferedImage.TYPE_INT_ARGB); 44 Graphics g = img.getGraphics(); 45 g.setColor(Color.RED); 46 g.fillRect(0, 0, width(), height()); 47 Font font = g.getFont(); 48 Font tempFont = font.deriveFont(Font.PLAIN).deriveFont(36.0f); 49 g.setFont(tempFont); 50 g.setColor(Color.BLACK); 51 g.drawString(e.getClass().getSimpleName() + " occured", 10, height()/2); 52 image.image = img; 53 g.setFont(font); 54 } 55 56 protected void attempt(){ // try to fetch the image 57 int maxTries = 5; // n tries for every image 58 for (int i = 1; i <= maxTries; i++) { 59 try { 60 fetch(); 61 break; // break out of the retry loop 62 } catch (Exception e) { 63 try { // sleep some time and then ask the server again 64 Thread.sleep(random(1000, 2000)); 65 } catch (InterruptedException e1) {} 66 67 if(i == maxTries) grabError(e); 68 } 69 } 70 } 71 72 public static int random(int min, int max) { 73 return (int)(Math.random() * ((max+1)-min) ) + min; 74 } 5 75 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSGrabber.java
r10683 r10684 1 1 package wmsplugin; 2 2 3 import static org.openstreetmap.josm.tools.I18n.tr;4 5 3 import java.awt.image.BufferedImage; 4 import java.io.BufferedReader; 6 5 import java.io.IOException; 7 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.net.HttpURLConnection; 8 9 import java.net.MalformedURLException; 9 10 import java.net.URL; 11 import java.net.URLConnection; 10 12 import java.text.DecimalFormat; 11 13 import java.text.DecimalFormatSymbols; 12 14 import java.text.NumberFormat; 13 15 import java.util.Locale; 14 import java.util.ArrayList;15 16 16 17 import javax.imageio.ImageIO; 17 import javax.swing.JOptionPane;18 18 19 19 import org.openstreetmap.josm.Main; … … 24 24 25 25 26 public class WMSGrabber implements Grabber {26 public class WMSGrabber extends Grabber { 27 27 protected String baseURL; 28 28 29 protected Bounds b; 30 protected Projection proj; 31 protected double pixelPerDegree; 32 protected GeorefImage image; 33 protected MapView mv; 34 protected WMSLayer layer; 35 36 WMSGrabber(String _baseURL, Bounds _b, Projection _proj, 37 double _pixelPerDegree, GeorefImage _image, MapView _mv, WMSLayer _layer) { 38 this.baseURL = _baseURL; 39 b = _b; 40 proj = _proj; 41 pixelPerDegree = _pixelPerDegree; 42 image = _image; 43 mv = _mv; 44 layer = _layer; 29 WMSGrabber(String baseURL, Bounds b, Projection proj, 30 double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { 31 super(b, proj, pixelPerDegree, image, mv, layer); 32 this.baseURL = baseURL; 45 33 } 46 34 47 35 public void run() { 36 attempt(); 37 mv.repaint(); 38 } 39 40 void fetch() throws Exception{ 41 URL url = null; 42 try { 43 url = getURL( 44 b.min.lon(), b.min.lat(), 45 b.max.lon(), b.max.lat(), 46 width(), height()); 48 47 49 i nt w = (int) ((b.max.lon() - b.min.lon()) * pixelPerDegree);50 i nt h = (int) ((b.max.lat() - b.min.lat()) * pixelPerDegree);48 image.min = proj.latlon2eastNorth(b.min); 49 image.max = proj.latlon2eastNorth(b.max); 51 50 52 try { 53 URL url = getURL( 54 b.min.lon(), b.min.lat(), 55 b.max.lon(), b.max.lat(), 56 w, h); 57 58 image.min = proj.latlon2eastNorth(b.min); 59 image.max = proj.latlon2eastNorth(b.max); 60 51 if(image.isVisible(mv)) //don't download, if the image isn't visible already 61 52 image.image = grab(url); 62 image.downloadingStarted = false; 63 64 mv.repaint(); 65 } 66 catch (MalformedURLException e) { 67 if(layer.messageNum-- > 0) 68 JOptionPane.showMessageDialog(Main.parent,tr("WMSPlugin: Illegal url.\n{0}",e.getMessage())); 69 } 70 catch (IOException e) { 71 if(layer.messageNum-- > 0) 72 JOptionPane.showMessageDialog(Main.parent,tr("WMSPlugin: IO exception.\n{0}",e.getMessage())); 73 } 53 image.downloadingStarted = false; 54 } catch(Exception e) { 55 throw new Exception(e.getMessage() + "\nImage couldn't be fetched: " + (url != null ? url.toString() : "")); 56 } 74 57 } 75 58 … … 90 73 91 74 protected BufferedImage grab(URL url) throws IOException { 92 InputStream is = new ProgressInputStream( 93 url.openConnection(), null); 94 if(!image.isVisible(mv)) 95 return null; 75 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 76 77 String contentType = conn.getHeaderField("Content-Type"); 78 if( conn.getResponseCode() != 200 79 || contentType != null && !contentType.startsWith("image") ) { 80 throw new IOException(readException(conn)); 81 } 82 83 InputStream is = new ProgressInputStream(conn, null); 96 84 BufferedImage img = ImageIO.read(is); 97 85 is.close(); 98 86 return img; 99 87 } 88 89 protected String readException(URLConnection conn) throws IOException { 90 StringBuilder exception = new StringBuilder(); 91 InputStream in = conn.getInputStream(); 92 BufferedReader br = new BufferedReader(new InputStreamReader(in)); 93 94 String line = null; 95 while( (line = br.readLine()) != null) { 96 exception.append(line); 97 exception.append('\n'); 98 } 99 return exception.toString(); 100 } 100 101 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java
r10683 r10684 6 6 import java.awt.Component; 7 7 import java.awt.Graphics; 8 import java.awt.Point;9 8 import java.awt.Toolkit; 10 9 import java.awt.event.ActionEvent; … … 12 11 import java.io.FileInputStream; 13 12 import java.io.FileOutputStream; 14 import java.io.IOException;15 13 import java.io.ObjectInputStream; 16 14 import java.io.ObjectOutputStream; 17 import java.util.ArrayList;18 15 19 16 import javax.swing.AbstractAction; … … 29 26 import org.openstreetmap.josm.Main; 30 27 import org.openstreetmap.josm.actions.ExtensionFileFilter; 31 import org.openstreetmap.josm.actions.SaveActionBase;32 28 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 33 import org.openstreetmap.josm.data.projection.Projection;34 29 import org.openstreetmap.josm.data.Bounds; 35 30 import org.openstreetmap.josm.data.coor.LatLon; 36 31 import org.openstreetmap.josm.gui.MapView; 37 import java.util.ArrayList;38 32 import java.util.concurrent.ExecutorService; 39 33 import java.util.concurrent.Executors; -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/YAHOOGrabber.java
r10683 r10684 4 4 5 5 import java.awt.image.BufferedImage; 6 import java.awt.Image; 7 import java.net.URL; 8 import java.util.ArrayList; 6 9 import java.io.IOException; 7 import java.io.InputStream;8 import java.io.InputStreamReader;9 import java.io.BufferedReader;10 import java.net.MalformedURLException;11 import java.net.URL;12 import java.text.DecimalFormat;13 import java.text.DecimalFormatSymbols;14 import java.text.NumberFormat;15 import java.util.Locale;16 import java.util.ArrayList;17 10 18 import java.awt.*;19 import java.awt.image.*;20 import java.io.*;21 import java.net.*;22 import java.util.ArrayList;23 import java.util.List;24 11 import java.util.StringTokenizer; 25 12 26 13 import javax.imageio.ImageIO; 27 import javax.swing.JOptionPane;28 14 29 15 import org.openstreetmap.josm.Main; 30 16 import org.openstreetmap.josm.data.Bounds; 31 17 import org.openstreetmap.josm.data.projection.Projection; 32 import org.openstreetmap.josm.io.ProgressInputStream;33 18 import org.openstreetmap.josm.gui.MapView; 34 19 35 20 36 public class YAHOOGrabber implements Grabber{ 37 protected String baseURL; 21 public class YAHOOGrabber extends WMSGrabber{ 38 22 protected String browserCmd; 39 23 40 protected Bounds b; 41 protected Projection proj; 42 protected double pixelPerDegree; 43 protected GeorefImage image; 44 protected MapView mv; 45 protected WMSLayer layer; 46 protected int width, height; 47 48 YAHOOGrabber(String _baseURL, Bounds _b, Projection _proj, 49 double _pixelPerDegree, GeorefImage _image, MapView _mv, WMSLayer _layer) { 50 this.baseURL = "file://" + Main.pref.getPreferencesDir() + "plugins/wmsplugin/ymap.html?request=getmap&format=image/jpeg"; 51 this.browserCmd = _baseURL.replaceFirst("yahoo://", ""); 52 this.b = _b; 53 this.proj = _proj; 54 this.pixelPerDegree = _pixelPerDegree; 55 this.image = _image; 56 this.mv = _mv; 57 this.layer = _layer; 24 YAHOOGrabber(String baseURL, Bounds b, Projection proj, 25 double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { 26 super("file://" + Main.pref.getPreferencesDir() + "plugins/wmsplugin/ymap.html?request=getmap&format=image/jpeg", b, proj, pixelPerDegree, image, mv, layer); 27 this.browserCmd = baseURL.replaceFirst("yahoo://", ""); 58 28 } 59 29 60 public void run() { 61 Image img; 62 63 width = (int) ((b.max.lon() - b.min.lon()) * pixelPerDegree); 64 height = (int) ((b.max.lat() - b.min.lat()) * pixelPerDegree); 65 66 try { 67 URL url = getURL( 68 b.min.lon(), b.min.lat(), 69 b.max.lon(), b.max.lat(), 70 width, height); 71 72 image.min = proj.latlon2eastNorth(b.min); 73 image.max = proj.latlon2eastNorth(b.max); 74 if(!image.isVisible(mv)){ //don't download, if the image isn't visible already 75 image.downloadingStarted = false; 76 return; 77 } 78 Process browser = browse(url.toString());; 79 image.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 80 img = ImageIO.read(browser.getInputStream()).getScaledInstance(width, height, Image.SCALE_FAST); 81 image.image.getGraphics().drawImage(img, 0 , 0, null); 82 83 image.downloadingStarted = false; 84 85 mv.repaint(); 86 } 87 catch (MalformedURLException e) { 88 if(layer.messageNum-- > 0) 89 JOptionPane.showMessageDialog(Main.parent,tr("WMSPlugin (YAHOOGrabber): Illegal url.\n{0}",e.getMessage())); 90 } 91 catch (IOException e) { 92 if(layer.messageNum-- > 0) 93 JOptionPane.showMessageDialog(Main.parent,tr("WMSPlugin (YAHOOGrabber): IO exception.\n{0}",e.getMessage())); 94 } 95 catch (NullPointerException e) { 96 if(layer.messageNum-- > 0) 97 JOptionPane.showMessageDialog(Main.parent,tr("WMSPlugin (YAHOOGrabber): Null pointer exception.\n{0}",e.getMessage())); 98 } 99 } 100 101 102 protected Process browse(String url) throws IOException { 30 protected BufferedImage grab(URL url) throws IOException { 103 31 ArrayList<String> cmdParams = new ArrayList<String>(); 104 32 105 StringTokenizer st = new StringTokenizer(tr(browserCmd, url ));33 StringTokenizer st = new StringTokenizer(tr(browserCmd, url.toString())); 106 34 while( st.hasMoreTokens() ) 107 35 cmdParams.add(st.nextToken()); … … 110 38 ProcessBuilder builder = new ProcessBuilder( cmdParams); 111 39 40 41 Process browser; 112 42 try { 113 returnbuilder.start();43 browser = builder.start(); 114 44 } 115 45 catch(IOException ioe) { 116 throw new IOException( tr("Could not start browser. Please check that the executable path is correct."));46 throw new IOException( "Could not start browser. Please check that the executable path is correct.\n" + ioe.getMessage() ); 117 47 } 48 49 BufferedImage i = new BufferedImage(width(), height(), BufferedImage.TYPE_INT_RGB); 50 Image img = ImageIO.read(browser.getInputStream()).getScaledInstance(width(), height(), Image.SCALE_FAST); 51 i.getGraphics().drawImage(img, 0 , 0, null); 52 return i; 118 53 } 119 120 protected static final NumberFormat121 latLonFormat = new DecimalFormat("###0.0000000",122 new DecimalFormatSymbols(Locale.US));123 124 protected URL getURL(double w, double s,double e,double n,125 int wi, int ht) throws MalformedURLException {126 String str = baseURL + "&bbox="127 + latLonFormat.format(w) + ","128 + latLonFormat.format(s) + ","129 + latLonFormat.format(e) + ","130 + latLonFormat.format(n)131 + "&width=" + wi + "&height=" + ht;132 return new URL(str.replace(" ", "%20"));133 }134 135 54 }
Note:
See TracChangeset
for help on using the changeset viewer.