better error behaviour if no map
[fw/altos] / ao-tools / altosui / AltosSiteMap.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 AltosSiteMap extends JComponent implements AltosFlightDisplay {
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     private boolean setLocation(double new_lat, double new_lng) {
44         int new_zoom = 15;
45         lat = new_lat;
46         lng = new_lng;
47         zoom = new_zoom;
48         scale_x = 256/360.0 * Math.pow(2, zoom);
49         scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
50         coord_pt = pt(lat, lng, new Point2D.Double(0,0));
51         coord_pt.x = 320-coord_pt.x;
52         coord_pt.y = 320-coord_pt.y;
53         last_pt = null;
54
55         try {
56             File pngfile = new File(AltosPreferences.logdir(), 
57                                     FileCoord(lat, lng, zoom));
58             System.out.printf("Trying file %s\n", pngfile);
59             BufferedImage myPicture = ImageIO.read(pngfile);
60             picLabel.setIcon(new ImageIcon( myPicture ));
61             g2d = myPicture.createGraphics();
62         } catch (Exception e) { 
63             // throw new RuntimeException(e);
64             return false;
65         }
66         return true;
67     }
68
69     private static double limit(double v, double lo, double hi) {
70         if (v < lo)
71             return lo;
72         if (hi < v)
73             return hi;
74         return v;
75     }
76
77     private static String FileCoord(double lat, double lng, int zoom) {
78         char chlat = lat < 0 ? 'S' : 'N';
79         char chlng = lng < 0 ? 'E' : 'W';
80         if (lat < 0) lat = -lat;
81         if (lng < 0) lng = -lng;
82         return String.format("map-%c%.3f,%c%.3f-%d.png",
83                 chlat, lat, chlng, lng, zoom);
84     }
85
86
87     // based on google js
88     //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
89     // search for fromLatLngToPoint and fromPointToLatLng
90     private Point2D.Double pt(double lat, double lng) {
91         return pt(lat, lng, coord_pt);
92     }
93
94     private Point2D.Double pt(double lat, double lng, Point2D.Double centre) {
95         Point2D.Double res = new Point2D.Double();
96         double e;
97
98         res.x = centre.x + lng*scale_x;
99         e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
100         res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
101         return res;
102     }
103
104
105     public void reset() {
106         // ?
107     }
108
109     static Color stateColors[] = {
110         Color.WHITE,  // startup
111         Color.WHITE,  // idle
112         Color.WHITE,  // pad
113         Color.RED,    // boost
114         Color.PINK,   // fast
115         Color.YELLOW, // coast
116         Color.CYAN,   // drogue
117         Color.BLUE,   // main
118         Color.BLACK   // landed
119     };
120
121     boolean nomaps = false;
122     public void show(AltosState state, int crc_errors) {
123         if (nomaps)
124             return;
125         if (!state.gps_ready && state.pad_lat == 0 && state.pad_lon == 0)
126             return;
127         double plat = (int)(state.pad_lat*200)/200.0;
128         double plon = (int)(state.pad_lon*200)/200.0;
129
130         if (last_pt == null) {
131             if (!setLocation(plat, plon)) {
132                 nomaps = true;
133                 return;
134             }
135         }
136
137         Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
138         if (last_pt != null && pt != last_pt) {
139             if (0 <= state.state && state.state < stateColors.length) {
140                 g2d.setColor(stateColors[state.state]);
141             }
142             g2d.draw(new Line2D.Double(last_pt, pt));
143         }
144         last_pt = pt;
145         repaint();
146     }
147    
148     JLabel picLabel = new JLabel();
149
150     public AltosSiteMap() {
151         GridBagLayout layout = new GridBagLayout();
152         setLayout(layout);
153
154         GridBagConstraints c = new GridBagConstraints();
155
156         c.gridx = 0; c.gridy = 0;
157         c.weightx = 1; c.weighty = 1;
158         c.anchor = GridBagConstraints.CENTER;
159         c.fill = GridBagConstraints.BOTH;
160         picLabel = new JLabel();
161         JScrollPane scrollPane = new JScrollPane(picLabel);
162         layout.setConstraints(scrollPane, c);
163         add(scrollPane);
164
165     }
166 }
167