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

Last change on this file since 5319 was 5319, checked in by frederik, 18 years ago

added very simple capability of loading and saving wms layers to WMSPlugin; requires at least JOSM v456 to work

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