AltosSiteMapTile: seperate map and drawing layers
[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     JLabel 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         File pngfile = new File(AltosPreferences.logdir(), 
52                                 FileCoord(map_latlng, zoom));
53         try {
54             mapLabel.setIcon(new ImageIcon(ImageIO.read(pngfile)));
55         } catch (Exception e) { 
56             // throw new RuntimeException(e);
57             System.out.printf("# Failed to find file %s\n", pngfile);
58             System.out.printf(" wget -O '%s' 'http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=hybrid&format=png32'\n", pngfile, map_latlng.x, map_latlng.y, zoom, px_size, px_size);
59         }
60     }
61
62     private boolean setLocation(double lat, double lng) {
63         Point2D.Double north_step;
64         double step_nm = 0.5;
65         for (zoom = 3; zoom < 22; zoom++) {
66             coord_pt = pt(lat, lng, new Point2D.Double(0,0), zoom);
67             north_step = pt(lat+step_nm/60.0, lng, 
68                     new Point2D.Double(0,0), zoom);
69             if (coord_pt.y - north_step.y > px_size/2)
70                 break;
71         }
72         coord_pt.x = -px_size * Math.floor(coord_pt.x/px_size + off_x);
73         coord_pt.y = -px_size * Math.floor(coord_pt.y/px_size + off_y);
74
75         scale_x = 256/360.0 * Math.pow(2, zoom);
76         scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
77
78         last_pt = null;
79
80         return true;
81     }
82
83     private static double limit(double v, double lo, double hi) {
84         if (v < lo)
85             return lo;
86         if (hi < v)
87             return hi;
88         return v;
89     }
90
91     private static String FileCoord(Point2D.Double latlng, int zoom) {
92         double lat, lng;
93         lat = latlng.x;
94         lng = latlng.y;
95         return FileCoord(lat, lng, zoom);
96     }
97     private static String FileCoord(double lat, double lng, int zoom) {
98         char chlat = lat < 0 ? 'S' : 'N';
99         char chlng = lng < 0 ? 'E' : 'W';
100         if (lat < 0) lat = -lat;
101         if (lng < 0) lng = -lng;
102         return String.format("map-%c%.6f,%c%.6f-%d.png",
103                 chlat, lat, chlng, lng, zoom);
104     }
105
106
107     // based on google js
108     //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
109     // search for fromLatLngToPoint and fromPointToLatLng
110     private Point2D.Double pt(double lat, double lng) {
111         return pt(lat, lng, coord_pt, scale_x, scale_y);
112     }
113
114     private static Point2D.Double pt(double lat, double lng, 
115             Point2D.Double centre, int zoom)
116     {
117         double scale_x = 256/360.0 * Math.pow(2, zoom);
118         double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
119         return pt(lat, lng, centre, scale_x, scale_y);
120     }
121
122     private static Point2D.Double pt(double lat, double lng, 
123             Point2D.Double centre, double scale_x, double scale_y)
124     {
125         Point2D.Double res = new Point2D.Double();
126         double e;
127
128         res.x = centre.x + lng*scale_x;
129         e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
130         res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
131         return res;
132     }
133
134     private Point2D.Double latlng(double x, double y) {
135         return latlng(new Point2D.Double(x,y), coord_pt);
136     }
137     private Point2D.Double latlng(Point2D.Double pt) {
138         return latlng(pt, coord_pt);
139     }
140     private Point2D.Double latlng(Point2D.Double pt, Point2D.Double centre) {
141         double lat, lng;
142         double rads;
143
144         lng = (pt.x - centre.x)/scale_x;
145         rads = 2 * Math.atan(Math.exp((pt.y-centre.y)/-scale_y));
146         lat = Math.toDegrees(rads - Math.PI/2);
147                                                                     
148         return new Point2D.Double(lat,lng);
149     }
150
151     static Color stateColors[] = {
152         Color.WHITE,  // startup
153         Color.WHITE,  // idle
154         Color.WHITE,  // pad
155         Color.RED,    // boost
156         Color.PINK,   // fast
157         Color.YELLOW, // coast
158         Color.CYAN,   // drogue
159         Color.BLUE,   // main
160         Color.BLACK   // landed
161     };
162
163     boolean drawn_landed_circle = false;
164     public void show(AltosState state, int crc_errors) {
165         if (!state.gps_ready) {
166             if (state.pad_lat == 0 && state.pad_lon == 0)
167                 return;
168             if (state.ngps < 3)
169                 return;
170         }
171
172         if (last_pt == null) {
173             setLocation(state.pad_lat, state.pad_lon);
174             loadMap();
175             last_pt = pt;
176         }
177
178         Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
179         if (pt != last_pt) {
180             if (0 <= state.state && state.state < stateColors.length) {
181                 g2d.setColor(stateColors[state.state]);
182             }
183             g2d.draw(new Line2D.Double(last_pt, pt));
184         }
185
186         if (0 <= pt.x && pt.x < px_size) {
187             if (0 <= pt.y && pt.y < px_size) {
188                 int dx = 500, dy = 250;
189                 if (state.state > 2) {
190                     dx = Math.min(200, 20 + (int) Math.abs(last_pt.x - pt.x));
191                     dy = Math.min(100, 10 + (int) Math.abs(last_pt.y - pt.y));
192                 }
193                 Rectangle r = new Rectangle((int)pt.x-dx, (int)pt.y-dy, 
194                                             dx*2, dy*2);
195                 scrollRectToVisible(r);
196             }
197         }
198
199         if (state.state == 8 && !drawn_landed_circle) {
200             drawn_landed_circle = true;
201             g2d.setColor(Color.RED);
202             g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
203             g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
204             g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
205         }
206
207         last_pt = pt;
208         repaint();
209     }
210
211     public static Graphics2D fillLabel(JLabel l, Color c) {
212         BufferedImage img = new BufferedImage(px_size, px_size,
213                 BufferedImage.TYPE_INT_ARGB);
214         Graphics2D g = img.createGraphics();
215         g.setColor(c);
216         g.fillRect(0, 0, px_size, px_size);
217         l.setIcon(new ImageIcon(img));
218         return g;
219     }
220
221     public AltosSiteMapTile(int x_tile_offset, int y_tile_offset) {
222         setPreferredSize(new Dimension(px_size, px_size));
223
224         mapLabel = new JLabel();
225         fillLabel(mapLabel, Color.GRAY);
226         mapLabel.setOpaque(true);
227         mapLabel.setBounds(0, 0, px_size, px_size);
228         add(mapLabel, new Integer(0));
229
230         draw = new JLabel();
231         g2d = fillLabel(draw, new Color(127, 127, 127, 0));
232         draw.setBounds(0, 0, px_size, px_size);
233         draw.setOpaque(false);
234
235         add(draw, new Integer(1));
236
237         off_x = x_tile_offset;
238         off_y = y_tile_offset;
239     }
240 }
241