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