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;
+import java.util.concurrent.*;
+import org.altusmetrum.AltosLib.*;
public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
// preferred vertical step in a tile in naut. miles
// 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)
int zoom;
double scale_x, scale_y;
+ int radius; /* half width/height of tiles to load */
+
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>();
+ ConcurrentHashMap<Point,AltosSiteMapTile> mapTiles = new ConcurrentHashMap<Point,AltosSiteMapTile>();
Point2D.Double centre;
private Point2D.Double tileCoordOffset(Point p) {
// nothing
}
- private void bgLoadMap(final AltosSiteMapTile tile,
- final File pngfile, final String pngurl)
+ public void set_font() {
+ // nothing
+ }
+
+ private void loadMap(final AltosSiteMapTile tile,
+ File pngfile, 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();
+ final ImageIcon res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl);
+ if (res != null) {
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ tile.loadMap(res);
+ }
+ });
+ } else {
+ System.out.printf("# Failed to fetch file %s\n", pngfile);
+ System.out.printf(" wget -O '%s' '%s'\n", pngfile, pngurl);
+ }
}
- public static void prefetchMaps(double lat, double lng, int w, int h) {
- AltosPreferences.init(null);
+ File pngfile;
+ String pngurl;
+
+ public int prefetchMap(int x, int y) {
+ LatLng map_latlng = latlng(
+ -centre.x + x*px_size + px_size/2,
+ -centre.y + y*px_size + px_size/2);
+ pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom);
+ pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom);
+ if (pngfile.exists()) {
+ return 1;
+ } else if (AltosSiteMapCache.fetchMap(pngfile, pngurl)) {
+ return 0;
+ } else {
+ return -1;
+ }
+ }
+ 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;
+ //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);
+ int r = asm.prefetchMap(x, y);
+ switch (r) {
+ case 1:
+ System.out.printf("Already have %s\n", asm.pngfile);
+ break;
+ case 0:
+ System.out.printf("Fetched map %s\n", asm.pngfile);
+ break;
+ case -1:
+ System.out.printf("# Failed to fetch file %s\n", asm.pngfile);
+ System.out.printf(" wget -O '%s' ''\n", asm.pngfile, asm.pngurl);
+ break;
}
}
}
}
- private void initMap(AltosSiteMapTile tile, Point offset) {
+ public String initMap(Point offset) {
+ AltosSiteMapTile tile = mapTiles.get(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);
+ File pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom);
+ String pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom);
+ loadMap(tile, pngfile, pngurl);
+ return pngfile.toString();
}
- private void initMaps(double lat, double lng) {
- centre = getBaseLocation(lat, lng);
-
+ public void setBaseLocation(double lat, double lng) {
for (Point k : mapTiles.keySet()) {
- initMap(mapTiles.get(k), k);
+ AltosSiteMapTile tile = mapTiles.get(k);
+ tile.clearMap();
}
+
+ centre = getBaseLocation(lat, lng);
+ scrollRocketToVisible(pt(lat,lng));
}
- private File MapFile(double lat, double lng) {
+ private void initMaps(double lat, double lng) {
+ setBaseLocation(lat, lng);
+
+ Thread thread = new Thread() {
+ public void run() {
+ for (Point k : mapTiles.keySet())
+ initMap(k);
+ }
+ };
+ thread.start();
+ }
+
+ private static File MapFile(double lat, double lng, int zoom) {
char chlat = lat < 0 ? 'S' : 'N';
- char chlng = lng < 0 ? 'E' : 'W';
+ char chlng = lng < 0 ? 'W' : 'E';
if (lat < 0) lat = -lat;
if (lng < 0) lng = -lng;
- return new File(AltosPreferences.logdir(),
+ return new File(AltosUIPreferences.mapdir(),
String.format("map-%c%.6f,%c%.6f-%d.png",
chlat, lat, chlng, lng, zoom));
}
- private String MapURL(double lat, double lng) {
+ private static String MapURL(double lat, double lng, int zoom) {
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(double lat, double lon) {
+ initMaps(lat, lon);
+ scrollRocketToVisible(pt(lat, lon));
+ }
public void show(final AltosState state, final int crc_errors) {
// if insufficient gps data, nothing to update
- if (state.gps == null)
- return;
if (!state.gps.locked && state.gps.nsat < 4)
return;
AltosSiteMapTile tile = createTile(offset);
tile.show(state, crc_errors, lref, ref);
- initMap(tile, offset);
+ initMap(offset);
finishTileLater(tile, offset);
}
last_state = state.state;
}
+ public void draw_circle(double lat, double lon) {
+ final Point2D.Double pt = pt(lat, lon);
+
+ for (Point offset : mapTiles.keySet()) {
+ AltosSiteMapTile tile = mapTiles.get(offset);
+ Point2D.Double ref = translatePoint(pt, tileCoordOffset(offset));
+ tile.draw_circle(ref);
+ }
+ }
+
private AltosSiteMapTile createTile(Point offset) {
AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
mapTiles.put(offset, tile);
}
private void ensureTilesAround(Point base_offset) {
- for (int x = -1; x <= 1; x++) {
- for (int y = -1; y <= 1; y++) {
+ for (int x = -radius; x <= radius; x++) {
+ for (int y = -radius; y <= radius; 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);
+ initMap(offset);
finishTileLater(tile, offset);
}
}
JComponent comp = new JComponent() { };
private GridBagLayout layout = new GridBagLayout();
- public AltosSiteMap() {
+ public AltosSiteMap(int in_radius) {
+ radius = in_radius;
+
GrabNDrag scroller = new GrabNDrag(comp);
comp.setLayout(layout);
- for (int x = -1; x <= 1; x++) {
- for (int y = -1; y <= 1; y++) {
+ for (int x = -radius; x <= radius; x++) {
+ for (int y = -radius; y <= radius; y++) {
Point offset = new Point(x, y);
AltosSiteMapTile t = createTile(offset);
addTileAt(t, offset);
}
}
setViewportView(comp);
- setPreferredSize(new Dimension(500,200));
+ setPreferredSize(new Dimension(500,500));
+ }
+
+ public AltosSiteMap() {
+ this(1);
}
}