df57aa7dec6cc6f50c611b14a61160f113d8a49c
[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 JLabel {
35     int zoom;
36     double scale_x, scale_y;
37     Point2D.Double coord_pt;
38     Point2D.Double last_pt;
39
40     Graphics2D g2d;
41
42     int off_x;
43     int off_y;
44
45     int px_size = 512;
46
47     private boolean setLocation(double lat, double lng) {
48         Point2D.Double north_step;
49         double step_nm = 0.5;
50         for (zoom = 3; zoom < 22; zoom++) {
51             coord_pt = pt(lat, lng, new Point2D.Double(0,0), zoom);
52             north_step = pt(lat+step_nm/60.0, lng, 
53                     new Point2D.Double(0,0), zoom);
54             if (coord_pt.y - north_step.y > px_size/2)
55                 break;
56         }
57         coord_pt.x = -px_size * Math.floor(coord_pt.x/px_size + off_x);
58         coord_pt.y = -px_size * Math.floor(coord_pt.y/px_size + off_y);
59
60         scale_x = 256/360.0 * Math.pow(2, zoom);
61         scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
62
63         last_pt = null;
64
65         Point2D.Double map_latlng;
66         map_latlng = latlng(new Point2D.Double(px_size/2, px_size/2));
67
68         File pngfile = new File(AltosPreferences.logdir(), 
69                                 FileCoord(map_latlng, zoom));
70         try {
71             BufferedImage myPicture;
72             myPicture = ImageIO.read(pngfile);
73             setIcon(new ImageIcon( myPicture ));
74             System.out.printf("# Found file %s\n", pngfile);
75             g2d = myPicture.createGraphics();
76         } catch (Exception e) { 
77             // throw new RuntimeException(e);
78             System.out.printf("# Failed to find file %s\n", pngfile);
79             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);
80         }
81         return true;
82     }
83
84     private static double limit(double v, double lo, double hi) {
85         if (v < lo)
86             return lo;
87         if (hi < v)
88             return hi;
89         return v;
90     }
91
92     private static String FileCoord(Point2D.Double latlng, int zoom) {
93         double lat, lng;
94         lat = latlng.x;
95         lng = latlng.y;
96         return FileCoord(lat, lng, zoom);
97     }
98     private static String FileCoord(double lat, double lng, int zoom) {
99         char chlat = lat < 0 ? 'S' : 'N';
100         char chlng = lng < 0 ? 'E' : 'W';
101         if (lat < 0) lat = -lat;
102         if (lng < 0) lng = -lng;
103         return String.format("map-%c%.6f,%c%.6f-%d.png",
104                 chlat, lat, chlng, lng, zoom);
105     }
106
107
108     // based on google js
109     //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
110     // search for fromLatLngToPoint and fromPointToLatLng
111     private Point2D.Double pt(double lat, double lng) {
112         return pt(lat, lng, coord_pt, scale_x, scale_y);
113     }
114
115     private static Point2D.Double pt(double lat, double lng, 
116             Point2D.Double centre, int zoom)
117     {
118         double scale_x = 256/360.0 * Math.pow(2, zoom);
119         double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
120         return pt(lat, lng, centre, scale_x, scale_y);
121     }
122
123     private static Point2D.Double pt(double lat, double lng, 
124             Point2D.Double centre, double scale_x, double scale_y)
125     {
126         Point2D.Double res = new Point2D.Double();
127         double e;
128
129         res.x = centre.x + lng*scale_x;
130         e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
131         res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
132         return res;
133     }
134
135     private Point2D.Double latlng(Point2D.Double pt) {
136         return latlng(pt, coord_pt);
137     }
138     private Point2D.Double latlng(Point2D.Double pt, Point2D.Double centre) {
139         double lat, lng;
140         double rads;
141
142         lng = (pt.x - centre.x)/scale_x;
143         rads = 2 * Math.atan(Math.exp((pt.y-centre.y)/-scale_y));
144         lat = Math.toDegrees(rads - Math.PI/2);
145                                                                     
146         return new Point2D.Double(lat,lng);
147     }
148
149     static Color stateColors[] = {
150         Color.WHITE,  // startup
151         Color.WHITE,  // idle
152         Color.WHITE,  // pad
153         Color.RED,    // boost
154         Color.PINK,   // fast
155         Color.YELLOW, // coast
156         Color.CYAN,   // drogue
157         Color.BLUE,   // main
158         Color.BLACK   // landed
159     };
160
161     boolean drawn_landed_circle = false;
162     boolean nomaps = false;
163     public void show(AltosState state, int crc_errors) {
164         if (nomaps)
165             return;
166         if (!state.gps_ready && state.pad_lat == 0 && state.pad_lon == 0)
167             return;
168         double plat = state.pad_lat;
169         double plon = state.pad_lon;
170
171         if (last_pt == null) {
172             if (!setLocation(plat, plon)) {
173                 nomaps = true;
174                 return;
175             }
176         }
177
178         Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
179         if (last_pt != null && 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 (last_pt != null && 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 AltosSiteMapTile(int x_tile_offset, int y_tile_offset) {
212         BufferedImage myPicture = new BufferedImage(px_size, px_size, 
213                 BufferedImage.TYPE_INT_RGB);
214         setIcon(new ImageIcon( myPicture ));
215         g2d = myPicture.createGraphics();
216         g2d.setColor(Color.GRAY);
217         g2d.fillRect(0, 0, px_size, px_size);
218
219         off_x = x_tile_offset;
220         off_y = y_tile_offset;
221     }
222 }
223