1 | package wmsplugin;
|
---|
2 |
|
---|
3 | import uk.me.jstott.jcoord.OSRef;
|
---|
4 | import uk.me.jstott.jcoord.LatLng;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.MalformedURLException;
|
---|
8 | import java.net.URL;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
14 |
|
---|
15 | import java.awt.Graphics2D;
|
---|
16 | import java.awt.BasicStroke;
|
---|
17 | import java.awt.Point;
|
---|
18 | import java.awt.Graphics;
|
---|
19 | import java.awt.Color;
|
---|
20 |
|
---|
21 | public class OSGBImage extends WMSImage
|
---|
22 | {
|
---|
23 | public OSGBImage(String constURL)
|
---|
24 | {
|
---|
25 | super(constURL);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public void grab(NavigatableComponent nc,double minlat,double minlon,
|
---|
29 | double maxlat,double maxlon) throws IOException
|
---|
30 | {
|
---|
31 | // To deal with the fact that grid refs and lat/lon don't align
|
---|
32 | LatLng ll1 = new LatLng(minlat,minlon),
|
---|
33 | ll2 = new LatLng(maxlat,maxlon),
|
---|
34 | ll3 = new LatLng(maxlat,minlon),
|
---|
35 | ll4 = new LatLng(minlat,maxlon);
|
---|
36 |
|
---|
37 | ll1.toOSGB36();
|
---|
38 | ll2.toOSGB36();
|
---|
39 | ll3.toOSGB36();
|
---|
40 | ll4.toOSGB36();
|
---|
41 |
|
---|
42 | OSRef bottomLeftGR = ll1.toOSRef(),
|
---|
43 | topRightGR = ll2.toOSRef(),
|
---|
44 | topLeftGR = ll3.toOSRef(),
|
---|
45 | bottomRightGR = ll4.toOSRef();
|
---|
46 |
|
---|
47 | double w = Math.min(bottomLeftGR.getEasting(),
|
---|
48 | topLeftGR.getEasting()),
|
---|
49 | s = Math.min(bottomLeftGR.getNorthing(),
|
---|
50 | bottomRightGR.getNorthing()),
|
---|
51 | e = Math.max(bottomRightGR.getEasting(),
|
---|
52 | topRightGR.getEasting()),
|
---|
53 | n = Math.max(topLeftGR.getNorthing(),
|
---|
54 | topRightGR.getNorthing());
|
---|
55 |
|
---|
56 | // Adjust topLeft and bottomRight due to messing around with
|
---|
57 | // projections
|
---|
58 | LatLng tl2 = new OSRef(w,n).toLatLng();
|
---|
59 | LatLng br2 = new OSRef(e,s).toLatLng();
|
---|
60 | tl2.toWGS84();
|
---|
61 | br2.toWGS84();
|
---|
62 |
|
---|
63 | topLeft = Main.proj.latlon2eastNorth
|
---|
64 | (new LatLon(tl2.getLat(),tl2.getLng() ));
|
---|
65 | bottomRight = Main.proj.latlon2eastNorth
|
---|
66 | (new LatLon(br2.getLat(),br2.getLng() ));
|
---|
67 |
|
---|
68 | grabbedScale = nc.getScale(); // enPerPixel
|
---|
69 |
|
---|
70 | int widthPx = (int)((bottomRight.east()-topLeft.east())/grabbedScale),
|
---|
71 | heightPx = (int)
|
---|
72 | ((topLeft.north()-bottomRight.north()) / grabbedScale);
|
---|
73 |
|
---|
74 | try
|
---|
75 | {
|
---|
76 | URL url = doGetURL(w,s,e,n,widthPx,heightPx);
|
---|
77 | doGrab(url);
|
---|
78 | }
|
---|
79 | catch(MalformedURLException ex)
|
---|
80 | {
|
---|
81 | System.out.println("Illegal url. Error="+ex);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void paint(Graphics g,NavigatableComponent nc)
|
---|
86 | {
|
---|
87 | if(theImage!=null)
|
---|
88 | {
|
---|
89 | super.paint(g,nc);
|
---|
90 | Graphics2D g2d = (Graphics2D)g;
|
---|
91 | g2d.setStroke(new BasicStroke(2));
|
---|
92 |
|
---|
93 | // Display markers at the OSGB intersections.
|
---|
94 | // The code is very convoluted - projections really are fun
|
---|
95 | // things to deal with :-)
|
---|
96 | // Oh well, at least I can let someone else do the maths :-)
|
---|
97 |
|
---|
98 | double zoomInFactor = grabbedScale / nc.getScale();
|
---|
99 |
|
---|
100 | EastNorth topLeftDisplaced =
|
---|
101 | new EastNorth(topLeft.east()+dEast, topLeft.north()+dNorth);
|
---|
102 | EastNorth bottomRightDisplaced =
|
---|
103 | new EastNorth(bottomRight.east()+dEast,
|
---|
104 | bottomRight.north()+dNorth);
|
---|
105 |
|
---|
106 | LatLon ll5 = Main.proj.eastNorth2latlon(topLeftDisplaced),
|
---|
107 | ll6 = Main.proj.eastNorth2latlon(bottomRightDisplaced);
|
---|
108 |
|
---|
109 | LatLng ll7 = new LatLng(ll5.lat(),ll5.lon());
|
---|
110 | LatLng ll8 = new LatLng(ll6.lat(),ll6.lon());
|
---|
111 | ll7.toOSGB36();
|
---|
112 | ll8.toOSGB36();
|
---|
113 |
|
---|
114 | LatLng curLatLng;
|
---|
115 | EastNorth curEN;
|
---|
116 |
|
---|
117 |
|
---|
118 | OSRef osgb1 = ll7.toOSRef(),
|
---|
119 | osgb2 = ll8.toOSRef();
|
---|
120 |
|
---|
121 | for(int easting=(int)(osgb1.getEasting()/1000) + 1;
|
---|
122 | easting<=(int)(osgb2.getEasting()/1000);
|
---|
123 | easting++)
|
---|
124 | {
|
---|
125 | for (int northing=(int)(osgb1.getNorthing()/1000) ;
|
---|
126 | northing>(int)(osgb2.getNorthing()/1000);
|
---|
127 | northing--)
|
---|
128 | {
|
---|
129 | // Now we have to convert the OSGB eastings and northings
|
---|
130 | // *back* to EastNorth units so we can draw the
|
---|
131 | // intersections....
|
---|
132 | // Not to mention converting between JOSM LatLon and
|
---|
133 | // JCoord LatLng....
|
---|
134 |
|
---|
135 |
|
---|
136 | curLatLng = new OSRef(easting*1000,northing*1000).
|
---|
137 | toLatLng();
|
---|
138 | curLatLng.toWGS84();
|
---|
139 | curEN = Main.proj.latlon2eastNorth
|
---|
140 | (new LatLon(curLatLng.getLat(),
|
---|
141 | curLatLng.getLng() ) );
|
---|
142 |
|
---|
143 | // draw a cross at the intersection
|
---|
144 | Point p = Main.map.mapView.getPoint(curEN);
|
---|
145 | g.setColor(Color.BLUE);
|
---|
146 | g.drawLine(p.x-5,p.y,p.x+5,p.y);
|
---|
147 | g.drawLine(p.x,p.y-5,p.x,p.y+5);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | g2d.setStroke(new BasicStroke(1));
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|