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 private static Point2D.Double translatePoint(Point2D.Double p,
46 return new Point2D.Double(p.x + d.x, p.y + d.y);
50 public double lat, lng;
51 public LatLng(double lat, double lng) {
58 // http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
59 // search for fromLatLngToPoint and fromPointToLatLng
60 private static Point2D.Double pt(LatLng latlng, int zoom) {
61 double scale_x = 256/360.0 * Math.pow(2, zoom);
62 double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
63 return pt(latlng, scale_x, scale_y);
66 private static Point2D.Double pt(LatLng latlng,
67 double scale_x, double scale_y)
69 Point2D.Double res = new Point2D.Double();
72 res.x = latlng.lng * scale_x;
74 e = Math.sin(Math.toRadians(latlng.lat));
75 e = Math.max(e,-(1-1.0E-15));
76 e = Math.min(e, 1-1.0E-15 );
78 res.y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
82 static private LatLng latlng(Point2D.Double pt,
83 double scale_x, double scale_y)
89 rads = 2 * Math.atan(Math.exp(-pt.y/scale_y));
90 lat = Math.toDegrees(rads - Math.PI/2);
92 return new LatLng(lat,lng);
96 double scale_x, scale_y;
98 private Point2D.Double pt(double lat, double lng) {
99 return pt(new LatLng(lat, lng), scale_x, scale_y);
102 private LatLng latlng(double x, double y) {
103 return latlng(new Point2D.Double(x,y), scale_x, scale_y);
105 private LatLng latlng(Point2D.Double pt) {
106 return latlng(pt, scale_x, scale_y);
109 HashMap<Point,AltosSiteMapTile> mapTiles = new HashMap<Point,AltosSiteMapTile>();
110 Point2D.Double centre;
112 private Point2D.Double tileCoordOffset(Point p) {
113 return new Point2D.Double(centre.x - p.x*px_size,
114 centre.y - p.y * px_size);
117 private Point tileOffset(Point2D.Double p) {
118 return new Point((int)Math.floor((centre.x+p.x)/px_size),
119 (int)Math.floor((centre.y+p.y)/px_size));
122 private Point2D.Double getBaseLocation(double lat, double lng) {
123 Point2D.Double locn, north_step;
126 // stupid loop structure to please Java's control flow analysis
129 scale_x = 256/360.0 * Math.pow(2, zoom);
130 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
132 north_step = pt(lat+tile_size_nmi*4/3/60.0, lng);
133 if (locn.y - north_step.y > px_size)
136 locn.x = -px_size * Math.floor(locn.x/px_size);
137 locn.y = -px_size * Math.floor(locn.y/px_size);
141 public void reset() {
145 private void bgLoadMap(final AltosSiteMapTile tile,
146 final File pngfile, final String pngurl)
148 //System.out.printf("Loading/fetching map %s\n", pngfile);
149 Thread thread = new Thread() {
152 res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl);
156 System.out.printf("# Failed to fetch file %s\n", pngfile);
157 System.out.printf(" wget -O '%s' ''\n", pngfile, pngurl);
164 public static void prefetchMaps(double lat, double lng, int w, int h) {
165 AltosPreferences.init(null);
167 AltosSiteMap asm = new AltosSiteMap(true);
168 asm.centre = asm.getBaseLocation(lat, lng);
170 Point2D.Double p = new Point2D.Double();
172 int dx = -w/2, dy = -h/2;
173 for (int y = dy; y < h+dy; y++) {
174 for (int x = dx; x < w+dx; x++) {
175 LatLng map_latlng = asm.latlng(
176 -asm.centre.x + x*px_size + px_size/2,
177 -asm.centre.y + y*px_size + px_size/2);
178 File pngfile = asm.MapFile(map_latlng.lat, map_latlng.lng);
179 String pngurl = asm.MapURL(map_latlng.lat, map_latlng.lng);
180 if (pngfile.exists()) {
181 System.out.printf("Already have %s\n", pngfile);
182 } else if (AltosSiteMapCache.fetchMap(pngfile, pngurl)) {
183 System.out.printf("Fetched map %s\n", pngfile);
185 System.out.printf("# Failed to fetch file %s\n", pngfile);
186 System.out.printf(" wget -O '%s' ''\n", pngfile, pngurl);
192 private void initMap(AltosSiteMapTile tile, Point offset) {
193 Point2D.Double coord = tileCoordOffset(offset);
195 LatLng map_latlng = latlng(px_size/2-coord.x, px_size/2-coord.y);
197 File pngfile = MapFile(map_latlng.lat, map_latlng.lng);
198 String pngurl = MapURL(map_latlng.lat, map_latlng.lng);
199 bgLoadMap(tile, pngfile, pngurl);
202 private void initMaps(double lat, double lng) {
203 centre = getBaseLocation(lat, lng);
205 for (Point k : mapTiles.keySet()) {
206 initMap(mapTiles.get(k), k);
210 private File MapFile(double lat, double lng) {
211 char chlat = lat < 0 ? 'S' : 'N';
212 char chlng = lng < 0 ? 'E' : 'W';
213 if (lat < 0) lat = -lat;
214 if (lng < 0) lng = -lng;
215 return new File(AltosPreferences.logdir(),
216 String.format("map-%c%.6f,%c%.6f-%d.png",
217 chlat, lat, chlng, lng, zoom));
220 private String MapURL(double lat, double lng) {
221 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);
224 boolean initialised = false;
225 Point2D.Double last_pt = null;
227 public void show(final AltosState state, final int crc_errors) {
228 // if insufficient gps data, nothing to update
229 if (state.gps == null)
231 if (!state.gps.locked) {
232 if (state.pad_lat == 0 && state.pad_lon == 0)
234 if (state.gps.nsat < 4)
239 initMaps(state.pad_lat, state.pad_lon);
243 final Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
244 if (last_pt == pt && last_state == state.state)
247 if (last_pt == null) {
250 boolean in_any = false;
251 for (Point offset : mapTiles.keySet()) {
252 AltosSiteMapTile tile = mapTiles.get(offset);
253 Point2D.Double ref, lref;
254 ref = translatePoint(pt, tileCoordOffset(offset));
255 lref = translatePoint(last_pt, tileCoordOffset(offset));
256 tile.show(state, crc_errors, lref, ref);
257 if (0 <= ref.x && ref.x < px_size)
258 if (0 <= ref.y && ref.y < px_size)
262 final AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
263 final Point offset = tileOffset(pt);
264 mapTiles.put(offset, tile);
266 Point2D.Double ref, lref;
267 ref = translatePoint(pt, tileCoordOffset(offset));
268 lref = translatePoint(last_pt, tileCoordOffset(offset));
269 tile.show(state, crc_errors, lref, ref);
271 initMap(tile, offset);
273 SwingUtilities.invokeLater( new Runnable() {
275 addTileAt(tile, offset);
276 setViewportView(comp);
281 last_state = state.state;
284 private void addTileAt(AltosSiteMapTile tile, Point offset) {
285 GridBagConstraints c = new GridBagConstraints();
286 c.anchor = GridBagConstraints.CENTER;
287 c.fill = GridBagConstraints.BOTH;
289 // put some space between the map tiles, debugging only
290 // c.insets = new Insets(5, 5, 5, 5);
292 c.gridx = offset.x + 100;
293 c.gridy = offset.y + 100;
294 layout.setConstraints(tile, c);
298 private AltosSiteMap(boolean knowWhatYouAreDoing) {
299 if (!knowWhatYouAreDoing) {
300 throw new RuntimeException("Arggh.");
305 private GridBagLayout layout;
307 public AltosSiteMap() {
308 comp = new JComponent() {
309 GrabNDrag scroller = new GrabNDrag(this);
311 addMouseMotionListener(scroller);
312 addMouseListener(scroller);
313 setAutoscrolls(true);
317 layout = new GridBagLayout();
318 comp.setLayout(layout);
320 for (int x = -1; x <= 1; x++) {
321 for (int y = -1; y <= 1; y++) {
322 AltosSiteMapTile t = new AltosSiteMapTile(px_size);
323 Point offset = new Point(x, y);
324 mapTiles.put(offset, t);
325 addTileAt(t, offset);
328 setViewportView(comp);
329 setPreferredSize(new Dimension(500,200));