2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
21 import java.awt.image.*;
22 import java.awt.event.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
29 import java.util.prefs.*;
30 import java.lang.Math;
31 import java.awt.geom.Point2D;
32 import java.awt.geom.Line2D;
34 public class AltosSiteMapTile extends JLabel {
36 double scale_x, scale_y;
37 Point2D.Double coord_pt;
38 Point2D.Double last_pt;
47 private boolean setLocation(double lat, double lng) {
48 Point2D.Double north_step;
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)
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);
60 scale_x = 256/360.0 * Math.pow(2, zoom);
61 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
65 Point2D.Double map_latlng;
66 map_latlng = latlng(new Point2D.Double(px_size/2, px_size/2));
68 BufferedImage myPicture;
69 File pngfile = new File(AltosPreferences.logdir(),
70 FileCoord(map_latlng, zoom));
72 myPicture = ImageIO.read(pngfile);
73 System.out.printf("# Found file %s\n", pngfile);
74 } catch (Exception e) {
75 // throw new RuntimeException(e);
76 System.out.printf("# Failed to find file %s\n", pngfile);
77 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);
78 myPicture = new BufferedImage(px_size, px_size,
79 BufferedImage.TYPE_INT_RGB);
81 setIcon(new ImageIcon( myPicture ));
82 g2d = myPicture.createGraphics();
86 private static double limit(double v, double lo, double hi) {
94 private static String FileCoord(Point2D.Double latlng, int zoom) {
98 return FileCoord(lat, lng, zoom);
100 private static String FileCoord(double lat, double lng, int zoom) {
101 char chlat = lat < 0 ? 'S' : 'N';
102 char chlng = lng < 0 ? 'E' : 'W';
103 if (lat < 0) lat = -lat;
104 if (lng < 0) lng = -lng;
105 return String.format("map-%c%.6f,%c%.6f-%d.png",
106 chlat, lat, chlng, lng, zoom);
110 // based on google js
111 // http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
112 // search for fromLatLngToPoint and fromPointToLatLng
113 private Point2D.Double pt(double lat, double lng) {
114 return pt(lat, lng, coord_pt, scale_x, scale_y);
117 private static Point2D.Double pt(double lat, double lng,
118 Point2D.Double centre, int zoom)
120 double scale_x = 256/360.0 * Math.pow(2, zoom);
121 double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
122 return pt(lat, lng, centre, scale_x, scale_y);
125 private static Point2D.Double pt(double lat, double lng,
126 Point2D.Double centre, double scale_x, double scale_y)
128 Point2D.Double res = new Point2D.Double();
131 res.x = centre.x + lng*scale_x;
132 e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
133 res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
137 private Point2D.Double latlng(Point2D.Double pt) {
138 return latlng(pt, coord_pt);
140 private Point2D.Double latlng(Point2D.Double pt, Point2D.Double centre) {
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);
148 return new Point2D.Double(lat,lng);
151 static Color stateColors[] = {
152 Color.WHITE, // startup
157 Color.YELLOW, // coast
158 Color.CYAN, // drogue
160 Color.BLACK // landed
163 boolean drawn_landed_circle = false;
164 boolean nomaps = false;
165 public void show(AltosState state, int crc_errors) {
168 if (!state.gps_ready && state.pad_lat == 0 && state.pad_lon == 0)
170 double plat = (int)(state.pad_lat*200)/200.0;
171 double plon = (int)(state.pad_lon*200)/200.0;
173 if (last_pt == null) {
174 if (!setLocation(plat, plon)) {
180 Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
181 if (last_pt != null && pt != last_pt) {
182 if (0 <= state.state && state.state < stateColors.length) {
183 g2d.setColor(stateColors[state.state]);
185 g2d.draw(new Line2D.Double(last_pt, pt));
188 if (state.state == 8 && !drawn_landed_circle) {
189 drawn_landed_circle = true;
190 g2d.setColor(Color.RED);
191 g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
192 g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
193 g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
200 public AltosSiteMapTile(int x_tile_offset, int y_tile_offset) {
201 off_x = x_tile_offset;
202 off_y = y_tile_offset;