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.swing.event.MouseInputAdapter;
25 import javax.imageio.ImageIO;
26 import javax.swing.table.*;
30 import java.util.prefs.*;
31 import java.lang.Math;
32 import java.awt.geom.Point2D;
33 import java.awt.geom.Line2D;
35 public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
36 // preferred vertical step in a tile in naut. miles
37 // will actually choose a step size between x and 2x, where this
39 static final double tile_size_nmi = 0.75;
41 static final int px_size = 512;
43 static final int MAX_TILE_DELTA = 100;
45 private static Point2D.Double translatePoint(Point2D.Double p,
48 return new Point2D.Double(p.x + d.x, p.y + d.y);
52 public double lat, lng;
53 public LatLng(double lat, double lng) {
60 // http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
61 // search for fromLatLngToPoint and fromPointToLatLng
62 private static Point2D.Double pt(LatLng latlng, int zoom) {
63 double scale_x = 256/360.0 * Math.pow(2, zoom);
64 double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
65 return pt(latlng, scale_x, scale_y);
68 private static Point2D.Double pt(LatLng latlng,
69 double scale_x, double scale_y)
71 Point2D.Double res = new Point2D.Double();
74 res.x = latlng.lng * scale_x;
76 e = Math.sin(Math.toRadians(latlng.lat));
77 e = Math.max(e,-(1-1.0E-15));
78 e = Math.min(e, 1-1.0E-15 );
80 res.y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
84 static private LatLng latlng(Point2D.Double pt,
85 double scale_x, double scale_y)
91 rads = 2 * Math.atan(Math.exp(-pt.y/scale_y));
92 lat = Math.toDegrees(rads - Math.PI/2);
94 return new LatLng(lat,lng);
98 double scale_x, scale_y;
100 int radius; /* half width/height of tiles to load */
102 private Point2D.Double pt(double lat, double lng) {
103 return pt(new LatLng(lat, lng), scale_x, scale_y);
106 private LatLng latlng(double x, double y) {
107 return latlng(new Point2D.Double(x,y), scale_x, scale_y);
109 private LatLng latlng(Point2D.Double pt) {
110 return latlng(pt, scale_x, scale_y);
113 HashMap<Point,AltosSiteMapTile> mapTiles = new HashMap<Point,AltosSiteMapTile>();
114 Point2D.Double centre;
116 private Point2D.Double tileCoordOffset(Point p) {
117 return new Point2D.Double(centre.x - p.x*px_size,
118 centre.y - p.y * px_size);
121 private Point tileOffset(Point2D.Double p) {
122 return new Point((int)Math.floor((centre.x+p.x)/px_size),
123 (int)Math.floor((centre.y+p.y)/px_size));
126 private Point2D.Double getBaseLocation(double lat, double lng) {
127 Point2D.Double locn, north_step;
130 // stupid loop structure to please Java's control flow analysis
133 scale_x = 256/360.0 * Math.pow(2, zoom);
134 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
136 north_step = pt(lat+tile_size_nmi*4/3/60.0, lng);
137 if (locn.y - north_step.y > px_size)
140 locn.x = -px_size * Math.floor(locn.x/px_size);
141 locn.y = -px_size * Math.floor(locn.y/px_size);
145 public void reset() {
149 public void set_font() {
153 private void loadMap(final AltosSiteMapTile tile,
154 File pngfile, String pngurl)
156 final ImageIcon res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl);
158 SwingUtilities.invokeLater(new Runnable() {
164 System.out.printf("# Failed to fetch file %s\n", pngfile);
165 System.out.printf(" wget -O '%s' '%s'\n", pngfile, pngurl);
172 public int prefetchMap(int x, int y) {
173 LatLng map_latlng = latlng(
174 -centre.x + x*px_size + px_size/2,
175 -centre.y + y*px_size + px_size/2);
176 pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom);
177 pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom);
178 if (pngfile.exists()) {
180 } else if (AltosSiteMapCache.fetchMap(pngfile, pngurl)) {
187 public static void prefetchMaps(double lat, double lng, int w, int h) {
188 AltosSiteMap asm = new AltosSiteMap(true);
189 asm.centre = asm.getBaseLocation(lat, lng);
191 Point2D.Double p = new Point2D.Double();
193 int dx = -w/2, dy = -h/2;
194 for (int y = dy; y < h+dy; y++) {
195 for (int x = dx; x < w+dx; x++) {
196 int r = asm.prefetchMap(x, y);
199 System.out.printf("Already have %s\n", asm.pngfile);
202 System.out.printf("Fetched map %s\n", asm.pngfile);
205 System.out.printf("# Failed to fetch file %s\n", asm.pngfile);
206 System.out.printf(" wget -O '%s' ''\n", asm.pngfile, asm.pngurl);
213 public String initMap(Point offset) {
214 AltosSiteMapTile tile = mapTiles.get(offset);
215 Point2D.Double coord = tileCoordOffset(offset);
217 LatLng map_latlng = latlng(px_size/2-coord.x, px_size/2-coord.y);
219 File pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom);
220 String pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom);
221 loadMap(tile, pngfile, pngurl);
222 return pngfile.toString();
225 public void setBaseLocation(double lat, double lng) {
226 for (Point k : mapTiles.keySet()) {
227 AltosSiteMapTile tile = mapTiles.get(k);
231 centre = getBaseLocation(lat, lng);
232 scrollRocketToVisible(pt(lat,lng));
235 private void initMaps(double lat, double lng) {
236 setBaseLocation(lat, lng);
238 Thread thread = new Thread() {
240 for (Point k : mapTiles.keySet())
247 private static File MapFile(double lat, double lng, int zoom) {
248 char chlat = lat < 0 ? 'S' : 'N';
249 char chlng = lng < 0 ? 'W' : 'E';
250 if (lat < 0) lat = -lat;
251 if (lng < 0) lng = -lng;
252 return new File(AltosPreferences.mapdir(),
253 String.format("map-%c%.6f,%c%.6f-%d.png",
254 chlat, lat, chlng, lng, zoom));
257 private static String MapURL(double lat, double lng, int zoom) {
258 return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=hybrid&format=png32", lat, lng, zoom, px_size, px_size);
261 boolean initialised = false;
262 Point2D.Double last_pt = null;
265 public void show(double lat, double lon) {
267 scrollRocketToVisible(pt(lat, lon));
269 public void show(final AltosState state, final int crc_errors) {
270 // if insufficient gps data, nothing to update
271 if (!state.gps.locked && state.gps.nsat < 4)
275 if (state.pad_lat != 0 || state.pad_lon != 0) {
276 initMaps(state.pad_lat, state.pad_lon);
278 } else if (state.gps.lat != 0 || state.gps.lon != 0) {
279 initMaps(state.gps.lat, state.gps.lon);
286 final Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
287 if (last_pt == pt && last_state == state.state)
290 if (last_pt == null) {
293 boolean in_any = false;
294 for (Point offset : mapTiles.keySet()) {
295 AltosSiteMapTile tile = mapTiles.get(offset);
296 Point2D.Double ref, lref;
297 ref = translatePoint(pt, tileCoordOffset(offset));
298 lref = translatePoint(last_pt, tileCoordOffset(offset));
299 tile.show(state, crc_errors, lref, ref);
300 if (0 <= ref.x && ref.x < px_size)
301 if (0 <= ref.y && ref.y < px_size)
305 Point offset = tileOffset(pt);
307 Point2D.Double ref, lref;
308 ref = translatePoint(pt, tileCoordOffset(offset));
309 lref = translatePoint(last_pt, tileCoordOffset(offset));
311 AltosSiteMapTile tile = createTile(offset);
312 tile.show(state, crc_errors, lref, ref);
314 finishTileLater(tile, offset);
317 scrollRocketToVisible(pt);
319 if (offset != tileOffset(last_pt)) {
320 ensureTilesAround(offset);
324 last_state = state.state;
327 public void draw_circle(double lat, double lon) {
328 final Point2D.Double pt = pt(lat, lon);
330 for (Point offset : mapTiles.keySet()) {
331 AltosSiteMapTile tile = mapTiles.get(offset);
332 Point2D.Double ref = translatePoint(pt, tileCoordOffset(offset));
333 tile.draw_circle(ref);
337 private AltosSiteMapTile createTile(Point offset) {
338 AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
339 mapTiles.put(offset, tile);
342 private void finishTileLater(final AltosSiteMapTile tile,
345 SwingUtilities.invokeLater( new Runnable() {
347 addTileAt(tile, offset);
352 private void ensureTilesAround(Point base_offset) {
353 for (int x = -radius; x <= radius; x++) {
354 for (int y = -radius; y <= radius; y++) {
355 Point offset = new Point(base_offset.x + x, base_offset.y + y);
356 if (mapTiles.containsKey(offset))
358 AltosSiteMapTile tile = createTile(offset);
360 finishTileLater(tile, offset);
365 private Point topleft = new Point(0,0);
366 private void scrollRocketToVisible(Point2D.Double pt) {
367 Rectangle r = comp.getVisibleRect();
368 Point2D.Double copt = translatePoint(pt, tileCoordOffset(topleft));
369 int dx = (int)copt.x - r.width/2 - r.x;
370 int dy = (int)copt.y - r.height/2 - r.y;
371 if (Math.abs(dx) > r.width/4 || Math.abs(dy) > r.height/4) {
374 comp.scrollRectToVisible(r);
378 private void addTileAt(AltosSiteMapTile tile, Point offset) {
379 if (Math.abs(offset.x) >= MAX_TILE_DELTA ||
380 Math.abs(offset.y) >= MAX_TILE_DELTA)
382 System.out.printf("Rocket too far away from pad (tile %d,%d)\n",
387 boolean review = false;
388 Rectangle r = comp.getVisibleRect();
389 if (offset.x < topleft.x) {
390 r.x += (topleft.x - offset.x) * px_size;
391 topleft.x = offset.x;
394 if (offset.y < topleft.y) {
395 r.y += (topleft.y - offset.y) * px_size;
396 topleft.y = offset.y;
399 GridBagConstraints c = new GridBagConstraints();
400 c.anchor = GridBagConstraints.CENTER;
401 c.fill = GridBagConstraints.BOTH;
402 // put some space between the map tiles, debugging only
403 // c.insets = new Insets(5, 5, 5, 5);
405 c.gridx = offset.x + MAX_TILE_DELTA;
406 c.gridy = offset.y + MAX_TILE_DELTA;
407 layout.setConstraints(tile, c);
411 comp.scrollRectToVisible(r);
415 private AltosSiteMap(boolean knowWhatYouAreDoing) {
416 if (!knowWhatYouAreDoing) {
417 throw new RuntimeException("Arggh.");
421 JComponent comp = new JComponent() { };
422 private GridBagLayout layout = new GridBagLayout();
424 public AltosSiteMap(int in_radius) {
427 GrabNDrag scroller = new GrabNDrag(comp);
429 comp.setLayout(layout);
431 for (int x = -radius; x <= radius; x++) {
432 for (int y = -radius; y <= radius; y++) {
433 Point offset = new Point(x, y);
434 AltosSiteMapTile t = createTile(offset);
435 addTileAt(t, offset);
438 setViewportView(comp);
439 setPreferredSize(new Dimension(500,500));
442 public AltosSiteMap() {