AltosSiteMap: automatic fetching of map data
[fw/altos] / ao-tools / altosui / AltosSiteMapTile.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.lang.Math;
31 import java.awt.geom.Point2D;
32 import java.awt.geom.Line2D;
33
34 public class AltosSiteMapTile extends JLayeredPane {
35     int zoom;
36     double scale_x, scale_y;
37     Point2D.Double coord_pt;
38     Point2D.Double last_pt;
39
40     AltosSiteMapLabel mapLabel;
41     JLabel draw;
42     Graphics2D g2d;
43
44     int off_x;
45     int off_y;
46
47     static final int px_size = 512;
48
49     private void loadMap() {
50         Point2D.Double map_latlng = latlng(px_size/2, px_size/2);
51         mapLabel.loadMap(map_latlng.x, map_latlng.y, zoom, px_size);
52     }
53
54     private boolean setLocation(double lat, double lng) {
55         Point2D.Double north_step;
56         double step_nm = 0.5;
57         for (zoom = 3; zoom < 22; zoom++) {
58             coord_pt = pt(lat, lng, new Point2D.Double(0,0), zoom);
59             north_step = pt(lat+step_nm/60.0, lng, 
60                     new Point2D.Double(0,0), zoom);
61             if (coord_pt.y - north_step.y > px_size/2)
62                 break;
63         }
64         coord_pt.x = -px_size * Math.floor(coord_pt.x/px_size + off_x);
65         coord_pt.y = -px_size * Math.floor(coord_pt.y/px_size + off_y);
66
67         scale_x = 256/360.0 * Math.pow(2, zoom);
68         scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
69
70         last_pt = null;
71
72         return true;
73     }
74
75     private static double limit(double v, double lo, double hi) {
76         if (v < lo)
77             return lo;
78         if (hi < v)
79             return hi;
80         return v;
81     }
82
83     // based on google js
84     //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
85     // search for fromLatLngToPoint and fromPointToLatLng
86     private Point2D.Double pt(double lat, double lng) {
87         return pt(lat, lng, coord_pt, scale_x, scale_y);
88     }
89
90     private static Point2D.Double pt(double lat, double lng, 
91             Point2D.Double centre, int zoom)
92     {
93         double scale_x = 256/360.0 * Math.pow(2, zoom);
94         double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
95         return pt(lat, lng, centre, scale_x, scale_y);
96     }
97
98     private static Point2D.Double pt(double lat, double lng, 
99             Point2D.Double centre, double scale_x, double scale_y)
100     {
101         Point2D.Double res = new Point2D.Double();
102         double e;
103
104         res.x = centre.x + lng*scale_x;
105         e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
106         res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
107         return res;
108     }
109
110     private Point2D.Double latlng(double x, double y) {
111         return latlng(new Point2D.Double(x,y), coord_pt);
112     }
113     private Point2D.Double latlng(Point2D.Double pt) {
114         return latlng(pt, coord_pt);
115     }
116     private Point2D.Double latlng(Point2D.Double pt, Point2D.Double centre) {
117         double lat, lng;
118         double rads;
119
120         lng = (pt.x - centre.x)/scale_x;
121         rads = 2 * Math.atan(Math.exp((pt.y-centre.y)/-scale_y));
122         lat = Math.toDegrees(rads - Math.PI/2);
123                                                                     
124         return new Point2D.Double(lat,lng);
125     }
126
127     static Color stateColors[] = {
128         Color.WHITE,  // startup
129         Color.WHITE,  // idle
130         Color.WHITE,  // pad
131         Color.RED,    // boost
132         Color.PINK,   // fast
133         Color.YELLOW, // coast
134         Color.CYAN,   // drogue
135         Color.BLUE,   // main
136         Color.BLACK   // landed
137     };
138
139     boolean drawn_landed_circle = false;
140     public void show(AltosState state, int crc_errors) {
141         if (!state.gps_ready) {
142             if (state.pad_lat == 0 && state.pad_lon == 0)
143                 return;
144             if (state.ngps < 3)
145                 return;
146         }
147
148         if (last_pt == null) {
149             setLocation(state.pad_lat, state.pad_lon);
150             loadMap();
151             last_pt = pt(state.pad_lat, state.pad_lon);
152         }
153
154         Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
155         if (pt != last_pt) {
156             if (0 <= state.state && state.state < stateColors.length) {
157                 g2d.setColor(stateColors[state.state]);
158             }
159             g2d.draw(new Line2D.Double(last_pt, pt));
160         }
161
162         if (0 <= pt.x && pt.x < px_size) {
163             if (0 <= pt.y && pt.y < px_size) {
164                 int dx = 500, dy = 250;
165                 if (state.state > 2) {
166                     dx = Math.min(200, 20 + (int) Math.abs(last_pt.x - pt.x));
167                     dy = Math.min(100, 10 + (int) Math.abs(last_pt.y - pt.y));
168                 }
169                 Rectangle r = new Rectangle((int)pt.x-dx, (int)pt.y-dy, 
170                                             dx*2, dy*2);
171                 scrollRectToVisible(r);
172             }
173         }
174
175         if (state.state == 8 && !drawn_landed_circle) {
176             drawn_landed_circle = true;
177             g2d.setColor(Color.RED);
178             g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
179             g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
180             g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
181         }
182
183         last_pt = pt;
184         repaint();
185     }
186
187     public static Graphics2D fillLabel(JLabel l, Color c) {
188         BufferedImage img = new BufferedImage(px_size, px_size,
189                 BufferedImage.TYPE_INT_ARGB);
190         Graphics2D g = img.createGraphics();
191         g.setColor(c);
192         g.fillRect(0, 0, px_size, px_size);
193         l.setIcon(new ImageIcon(img));
194         return g;
195     }
196
197     public AltosSiteMapTile(int x_tile_offset, int y_tile_offset) {
198         setPreferredSize(new Dimension(px_size, px_size));
199
200         mapLabel = new AltosSiteMapLabel();
201         fillLabel(mapLabel, Color.GRAY);
202         mapLabel.setOpaque(true);
203         mapLabel.setBounds(0, 0, px_size, px_size);
204         add(mapLabel, new Integer(0));
205
206         draw = new JLabel();
207         g2d = fillLabel(draw, new Color(127, 127, 127, 0));
208         draw.setBounds(0, 0, px_size, px_size);
209         draw.setOpaque(false);
210
211         add(draw, new Integer(1));
212
213         off_x = x_tile_offset;
214         off_y = y_tile_offset;
215     }
216 }
217