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