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