summaryrefslogtreecommitdiff
path: root/altosui/AltosSiteMap.java
blob: 7575c10e62d3b95cfa23ca2c7d938485573d79af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package altosui;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.imageio.ImageIO;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.prefs.*;
import java.lang.Math;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;

public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
	// preferred vertical step in a tile in naut. miles
	// will actually choose a step size between x and 2x, where this
	// is 1.5x
	static final double tile_size_nmi = 0.75;

	static final int px_size = 512;

	static final int MAX_TILE_DELTA = 100;

	private static Point2D.Double translatePoint(Point2D.Double p,
			Point2D.Double d)
	{
		return new Point2D.Double(p.x + d.x, p.y + d.y);
	}

	static class LatLng {
		public double lat, lng;
		public LatLng(double lat, double lng) {
			this.lat = lat;
			this.lng = lng;
		}
	}

	// based on google js
	//  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
	// search for fromLatLngToPoint and fromPointToLatLng
	private static Point2D.Double pt(LatLng latlng, int zoom) {
		double scale_x = 256/360.0 * Math.pow(2, zoom);
		double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
		return pt(latlng, scale_x, scale_y);
	}

	private static Point2D.Double pt(LatLng latlng,
					 double scale_x, double scale_y)
	{
		Point2D.Double res = new Point2D.Double();
		double e;

		res.x = latlng.lng * scale_x;

		e = Math.sin(Math.toRadians(latlng.lat));
		e = Math.max(e,-(1-1.0E-15));
		e = Math.min(e,  1-1.0E-15 );

		res.y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
		return res;
	}

	static private LatLng latlng(Point2D.Double pt,
				     double scale_x, double scale_y)
	{
		double lat, lng;
		double rads;

		lng = pt.x/scale_x;
		rads = 2 * Math.atan(Math.exp(-pt.y/scale_y));
		lat = Math.toDegrees(rads - Math.PI/2);

		return new LatLng(lat,lng);
	}

	int zoom;
	double scale_x, scale_y;

	private Point2D.Double pt(double lat, double lng) {
		return pt(new LatLng(lat, lng), scale_x, scale_y);
	}

	private LatLng latlng(double x, double y) {
		return latlng(new Point2D.Double(x,y), scale_x, scale_y);
	}
	private LatLng latlng(Point2D.Double pt) {
		return latlng(pt, scale_x, scale_y);
	}

	HashMap<Point,AltosSiteMapTile> mapTiles = new HashMap<Point,AltosSiteMapTile>();
	Point2D.Double centre;

	private Point2D.Double tileCoordOffset(Point p) {
		return new Point2D.Double(centre.x - p.x*px_size,
					  centre.y - p.y * px_size);
	}

	private Point tileOffset(Point2D.Double p) {
		return new Point((int)Math.floor((centre.x+p.x)/px_size),
				 (int)Math.floor((centre.y+p.y)/px_size));
	}

	private Point2D.Double getBaseLocation(double lat, double lng) {
		Point2D.Double locn, north_step;

		zoom = 2;
		// stupid loop structure to please Java's control flow analysis
		do {
			zoom++;
			scale_x = 256/360.0 * Math.pow(2, zoom);
			scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
			locn = pt(lat, lng);
			north_step = pt(lat+tile_size_nmi*4/3/60.0, lng);
			if (locn.y - north_step.y > px_size)
				break;
		} while (zoom < 22);
		locn.x = -px_size * Math.floor(locn.x/px_size);
		locn.y = -px_size * Math.floor(locn.y/px_size);
		return locn;
	}

	public void reset() {
		// nothing
	}

	private void bgLoadMap(final AltosSiteMapTile tile,
			       final File pngfile, final String pngurl)
	{
		//System.out.printf("Loading/fetching map %s\n", pngfile);
		Thread thread = new Thread() {
			public void run() {
				ImageIcon res;
				res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl);
				if (res != null) {
					tile.loadMap(res);
				} else {
					System.out.printf("# Failed to fetch file %s\n", pngfile);
					System.out.printf(" wget -O '%s' ''\n", pngfile, pngurl);
				}
			}
		};
		thread.start();
	}

	public static void prefetchMaps(double lat, double lng, int w, int h) {
		AltosSiteMap asm = new AltosSiteMap(true);
		asm.centre = asm.getBaseLocation(lat, lng);

		Point2D.Double p = new Point2D.Double();
		Point2D.Double p2;
		int dx = -w/2, dy = -h/2;
		for (int y = dy; y < h+dy; y++) {
			for (int x = dx; x < w+dx; x++) {
				LatLng map_latlng = asm.latlng(
							    -asm.centre.x + x*px_size + px_size/2,
							    -asm.centre.y + y*px_size + px_size/2);
				File pngfile = asm.MapFile(map_latlng.lat, map_latlng.lng);
				String pngurl = asm.MapURL(map_latlng.lat, map_latlng.lng);
				if (pngfile.exists()) {
					System.out.printf("Already have %s\n", pngfile);
				} else if (AltosSiteMapCache.fetchMap(pngfile, pngurl)) {
					System.out.printf("Fetched map %s\n", pngfile);
				} else {
					System.out.printf("# Failed to fetch file %s\n", pngfile);
					System.out.printf(" wget -O '%s' ''\n", pngfile, pngurl);
				}
			}
		}
	}

	private void initMap(AltosSiteMapTile tile, Point offset) {
		Point2D.Double coord = tileCoordOffset(offset);

		LatLng map_latlng = latlng(px_size/2-coord.x, px_size/2-coord.y);

		File pngfile = MapFile(map_latlng.lat, map_latlng.lng);
		String pngurl = MapURL(map_latlng.lat, map_latlng.lng);
		bgLoadMap(tile, pngfile, pngurl);
	}

	private void initMaps(double lat, double lng) {
		centre = getBaseLocation(lat, lng);

		for (Point k : mapTiles.keySet()) {
			initMap(mapTiles.get(k), k);
		}
	}

	private File MapFile(double lat, double lng) {
		char chlat = lat < 0 ? 'S' : 'N';
		char chlng = lng < 0 ? 'W' : 'E';
		if (lat < 0) lat = -lat;
		if (lng < 0) lng = -lng;
		return new File(AltosPreferences.mapdir(),
				String.format("map-%c%.6f,%c%.6f-%d.png",
					      chlat, lat, chlng, lng, zoom));
	}

	private String MapURL(double lat, double lng) {
		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);
	}

	boolean initialised = false;
	Point2D.Double last_pt = null;
	int last_state = -1;
	public void show(final AltosState state, final int crc_errors) {
		// if insufficient gps data, nothing to update
		if (!state.gps.locked && state.gps.nsat < 4)
			return;

		if (!initialised) {
			if (state.pad_lat != 0 || state.pad_lon != 0) {
				initMaps(state.pad_lat, state.pad_lon);
				initialised = true;
			} else if (state.gps.lat != 0 || state.gps.lon != 0) {
				initMaps(state.gps.lat, state.gps.lon);
				initialised = true;
			} else {
				return;
			}
		}

		final Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
		if (last_pt == pt && last_state == state.state)
			return;

		if (last_pt == null) {
			last_pt = pt;
		}
		boolean in_any = false;
		for (Point offset : mapTiles.keySet()) {
			AltosSiteMapTile tile = mapTiles.get(offset);
			Point2D.Double ref, lref;
			ref = translatePoint(pt, tileCoordOffset(offset));
			lref = translatePoint(last_pt, tileCoordOffset(offset));
			tile.show(state, crc_errors, lref, ref);
			if (0 <= ref.x && ref.x < px_size)
				if (0 <= ref.y && ref.y < px_size)
					in_any = true;
		}

		Point offset = tileOffset(pt);
		if (!in_any) {
			Point2D.Double ref, lref;
			ref = translatePoint(pt, tileCoordOffset(offset));
			lref = translatePoint(last_pt, tileCoordOffset(offset));

			AltosSiteMapTile tile = createTile(offset);
			tile.show(state, crc_errors, lref, ref);
			initMap(tile, offset);
			finishTileLater(tile, offset);
		}

		scrollRocketToVisible(pt);

		if (offset != tileOffset(last_pt)) {
			ensureTilesAround(offset);
		}

		last_pt = pt;
		last_state = state.state;
	}

	private AltosSiteMapTile createTile(Point offset) {
		AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
		mapTiles.put(offset, tile);
		return tile;
	}
	private void finishTileLater(final AltosSiteMapTile tile,
				     final Point offset)
	{
		SwingUtilities.invokeLater( new Runnable() {
			public void run() {
				addTileAt(tile, offset);
			}
		} );
	}

	private void ensureTilesAround(Point base_offset) {
		for (int x = -1; x <= 1; x++) {
			for (int y = -1; y <= 1; y++) {
				Point offset = new Point(base_offset.x + x, base_offset.y + y);
				if (mapTiles.containsKey(offset))
					continue;
				AltosSiteMapTile tile = createTile(offset);
				initMap(tile, offset);
				finishTileLater(tile, offset);
			}
		}
	}

	private Point topleft = new Point(0,0);
	private void scrollRocketToVisible(Point2D.Double pt) {
		Rectangle r = comp.getVisibleRect();
		Point2D.Double copt = translatePoint(pt, tileCoordOffset(topleft));
		int dx = (int)copt.x - r.width/2 - r.x;
		int dy = (int)copt.y - r.height/2 - r.y;
		if (Math.abs(dx) > r.width/4 || Math.abs(dy) > r.height/4) {
			r.x += dx;
			r.y += dy;
			comp.scrollRectToVisible(r);
		}
	}

	private void addTileAt(AltosSiteMapTile tile, Point offset) {
		if (Math.abs(offset.x) >= MAX_TILE_DELTA ||
				Math.abs(offset.y) >= MAX_TILE_DELTA)
		{
			System.out.printf("Rocket too far away from pad (tile %d,%d)\n",
					  offset.x, offset.y);
			return;
		}

		boolean review = false;
		Rectangle r = comp.getVisibleRect();
		if (offset.x < topleft.x) {
			r.x += (topleft.x - offset.x) * px_size;
			topleft.x = offset.x;
			review = true;
		}
		if (offset.y < topleft.y) {
			r.y += (topleft.y - offset.y) * px_size;
			topleft.y = offset.y;
			review = true;
		}
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		// put some space between the map tiles, debugging only
		// c.insets = new Insets(5, 5, 5, 5);

		c.gridx = offset.x + MAX_TILE_DELTA;
		c.gridy = offset.y + MAX_TILE_DELTA;
		layout.setConstraints(tile, c);

		comp.add(tile);
		if (review) {
			comp.scrollRectToVisible(r);
		}
	}

	private AltosSiteMap(boolean knowWhatYouAreDoing) {
		if (!knowWhatYouAreDoing) {
			throw new RuntimeException("Arggh.");
		}
	}

	JComponent comp = new JComponent() { };
	private GridBagLayout layout = new GridBagLayout();

	public AltosSiteMap() {
		GrabNDrag scroller = new GrabNDrag(comp);

		comp.setLayout(layout);

		for (int x = -1; x <= 1; x++) {
			for (int y = -1; y <= 1; y++) {
				Point offset = new Point(x, y);
				AltosSiteMapTile t = createTile(offset);
				addTileAt(t, offset);
			}
		}
		setViewportView(comp);
		setPreferredSize(new Dimension(500,200));
	}
}