source: osm/applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSImage.java@ 5245

Last change on this file since 5245 was 5245, checked in by gabriel, 18 years ago

wmsplugin: Escape spaces in URLs.

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1package wmsplugin;
2
3import java.awt.Graphics;
4import java.awt.Image;
5import java.awt.Point;
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.MalformedURLException;
9import java.net.URL;
10
11import javax.imageio.ImageIO;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.coor.EastNorth;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.gui.NavigatableComponent;
17import org.openstreetmap.josm.io.ProgressInputStream;
18
19public class WMSImage
20{
21 String constURL;
22 protected Image theImage;
23 protected double grabbedScale;
24 protected EastNorth topLeft, bottomRight;
25 double dEast, dNorth;
26
27 public WMSImage(String constURL)
28 {
29 this.constURL = constURL;
30 }
31
32 public void grab(NavigatableComponent nc) throws IOException
33 {
34
35 EastNorth topLeft = nc.getEastNorth(0,0);
36 grabbedScale = nc.getScale(); // scale is enPerPixel
37
38 this.topLeft = topLeft;
39
40 try
41 {
42 URL url = getURL(nc);
43 doGrab(url);
44 }
45 catch(MalformedURLException e)
46 {
47 System.out.println("Illegal url. Error="+e);
48 }
49 }
50
51 public void grab(NavigatableComponent nc,double minlat,double minlon,
52 double maxlat,double maxlon) throws IOException
53 {
54 LatLon p = new LatLon(minlat,minlon),
55 p2 = new LatLon(maxlat,maxlon);
56
57 grabbedScale = nc.getScale(); // enPerPixel
58
59 topLeft = Main.proj.latlon2eastNorth(new LatLon(maxlat,minlon));
60 bottomRight = Main.proj.latlon2eastNorth(new LatLon(minlat,maxlon));
61
62 int widthPx = (int)((bottomRight.east()-topLeft.east())/grabbedScale),
63 heightPx = (int)
64 ((topLeft.north()-bottomRight.north()) / grabbedScale);
65
66 try
67 {
68 URL url = doGetURL(p.lon(),p.lat(),
69 p2.lon(),p2.lat(),widthPx,heightPx);
70 doGrab(url);
71 }
72 catch(MalformedURLException e)
73 {
74 System.out.println("Illegal url. Error="+e);
75 }
76 }
77
78 private URL getURL(NavigatableComponent nc) throws MalformedURLException
79 {
80 double widthEN = nc.getWidth()*grabbedScale,
81 heightEN = nc.getHeight()*grabbedScale;
82 LatLon p = Main.proj.eastNorth2latlon(new EastNorth
83 (topLeft.east(), topLeft.north()-heightEN));
84 LatLon p2 = Main.proj.eastNorth2latlon(new EastNorth
85 (topLeft.east()+widthEN, topLeft.north()));
86 return doGetURL(p.lon(),p.lat(),p2.lon(),p2.lat(),
87 (int)(widthEN/grabbedScale),
88 (int)(heightEN/grabbedScale) );
89 }
90
91 protected URL doGetURL(double w,double s,double e,double n, int wi,
92 int ht) throws MalformedURLException
93 {
94 String str = constURL + "&bbox=" + w +"," + s + ","+
95 e+","+n + "&width=" + wi + "&height=" + ht;
96 return new URL(str.replace(" ", "%20"));
97 }
98
99 protected void doGrab (URL url) throws IOException
100 {
101 InputStream is = new ProgressInputStream(
102 url.openConnection(), Main.pleaseWaitDlg);
103 theImage = ImageIO.read(is) ;
104 is.close();
105 Main.map.repaint();
106 }
107
108 public void displace (double dEast, double dNorth)
109 {
110 this.dEast += dEast;
111 this.dNorth += dNorth;
112 }
113
114 public boolean contains(EastNorth eastNorth)
115 {
116 double e1 = topLeft.east()+dEast,
117 e2 = bottomRight.east()+dEast,
118 n1 = bottomRight.north()+dNorth,
119 n2 = topLeft.north()+dNorth;
120
121 boolean b = eastNorth.east()>=e1 && eastNorth.east()<=e2 &&
122 eastNorth.north()>=n1 && eastNorth.north()<=n2;
123 return b;
124 }
125
126 public void paint(Graphics g,NavigatableComponent nc)
127 {
128 if(theImage!=null)
129 {
130 double zoomInFactor = grabbedScale / nc.getScale();
131
132 // Find the image x and y of the supplied bottom left
133 // This will be the difference in EastNorth units, divided by the
134 // grabbed scale in EastNorth/pixel.
135
136 int w = theImage.getWidth(null), h=theImage.getHeight(null);
137 EastNorth topLeftDisplaced =
138 new EastNorth(topLeft.east()+dEast, topLeft.north()+dNorth);
139 Point displacement = Main.map.mapView.getPoint(topLeftDisplaced);
140 g.drawImage(theImage,displacement.x,displacement.y,
141 (int)(displacement.x+w*zoomInFactor),
142 (int)(displacement.y+h*zoomInFactor),
143 0,0,w,h,null);
144 }
145 }
146
147}
Note: See TracBrowser for help on using the repository browser.