6f33aabd152d0e696183274a7cf7222907167fba
[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 void setLocation(double new_lat, double new_lng, int new_zoom) {
44         lat = new_lat;
45         lng = new_lng;
46         zoom = new_zoom;
47         scale_x = 256/360.0 * Math.pow(2, zoom);
48         scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
49         coord_pt = pt(lat, lng, new Point2D.Double(0,0));
50         coord_pt.x = 320-coord_pt.x;
51         coord_pt.y = 320-coord_pt.y;
52         last_pt = null;
53     }
54
55     private static double limit(double v, double lo, double hi) {
56         if (v < lo)
57             return lo;
58         if (hi < v)
59             return hi;
60         return v;
61     }
62
63     // based on google js
64     //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
65     // search for fromLatLngToPoint and fromPointToLatLng
66     private Point2D.Double pt(double lat, double lng) {
67         return pt(lat, lng, coord_pt);
68     }
69
70     private Point2D.Double pt(double lat, double lng, Point2D.Double centre) {
71         Point2D.Double res = new Point2D.Double();
72         double e;
73
74         res.x = centre.x + lng*scale_x;
75         e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
76         res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
77         return res;
78     }
79
80
81     public void reset() {
82         // ?
83     }
84
85     static Color stateColors[] = {
86         Color.WHITE,  // startup
87         Color.WHITE,  // idle
88         Color.WHITE,  // pad
89         Color.RED,    // boost
90         Color.PINK,   // fast
91         Color.YELLOW, // coast
92         Color.CYAN,   // drogue
93         Color.BLUE,   // main
94         Color.BLACK   // landed
95     };
96
97     public void show(AltosState state, int crc_errors) {
98         if (!state.gps_ready)
99             return;
100         Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
101         if (last_pt != null && pt != last_pt) {
102             if (0 <= state.state && state.state < stateColors.length) {
103                 g2d.setColor(stateColors[state.state]);
104             }
105             g2d.draw(new Line2D.Double(last_pt, pt));
106         }
107         last_pt = pt;
108         repaint();
109     }
110     
111     public AltosSiteMap() {
112         GridBagLayout layout = new GridBagLayout();
113         setLayout(layout);
114
115         GridBagConstraints c = new GridBagConstraints();
116
117         setLocation(-27.850, 152.960, 15);
118         String pngfile = "/home/aj/qrs-S27.850,W152.960-15.png";
119
120         c.gridx = 0; c.gridy = 0;
121         c.weightx = 1; c.weighty = 1;
122         c.anchor = GridBagConstraints.CENTER;
123         c.fill = GridBagConstraints.BOTH;
124
125         try {
126             BufferedImage myPicture = ImageIO.read(new File(pngfile));
127             g2d = myPicture.createGraphics();
128             JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
129             JScrollPane scrollPane = new JScrollPane(picLabel);
130             layout.setConstraints(scrollPane, c);
131             add(scrollPane);
132         } catch (Exception e) { 
133             throw new RuntimeException(e);
134         };
135     }
136 }
137