first cut at turnon scripts for EasyTimer v2
[fw/altos] / map-server / altos-mapd / AltosMapd.java
index 923c713e6d2880e37eb29aecf7faf6ca9f262e7b..3d1d5d7317f3e938732efda8ed7f29caed76a681 100644 (file)
@@ -20,7 +20,7 @@ import java.text.*;
 import java.util.*;
 import java.util.concurrent.*;
 
-import org.altusmetrum.altoslib_13.*;
+import org.altusmetrum.altoslib_14.*;
 
 public class AltosMapd implements AltosLaunchSiteListener {
 
@@ -32,6 +32,8 @@ public class AltosMapd implements AltosLaunchSiteListener {
 
        public final static int scale = 1;
 
+       public static int max_zoom = 17;
+
        public static double valid_radius = 17000;      /* 17km */
 
        public String map_dir = null;
@@ -40,7 +42,8 @@ public class AltosMapd implements AltosLaunchSiteListener {
 
        public void usage() {
                System.out.printf("usage: altos-mapd [--mapdir <map-directory] [--launch-sites <launch-sites-file>]\n" +
-                                 "                  [--radius <valid-radius-m> [--port <port>] [--key <key-file>]\n");
+                                 "                  [--radius <valid-radius-m> [--port <port>] [--key <key-file>]\n" +
+                                 "                  [--max-zoom <max-zoom-level>\n");
                System.exit(1);
        }
 
@@ -57,7 +60,27 @@ public class AltosMapd implements AltosLaunchSiteListener {
                }
        }
 
-       public static boolean check_lat_lon(double lat, double lon) {
+       private static boolean west_of(double a, double b) {
+               double  diff = b - a;
+
+               while (diff >= 360.0)
+                       diff -= 360.0;
+               while (diff <= -360.0)
+                       diff += 360.0;
+
+               return diff >= 0;
+       }
+
+       public static boolean check_lat_lon(double lat, double lon, int zoom) {
+
+               if (zoom > max_zoom)
+                       return false;
+
+               AltosMapTransform       transform = new AltosMapTransform(px_size, px_size, zoom, new AltosLatLon(lat, lon));
+
+               AltosLatLon             upper_left = transform.screen_lat_lon(new AltosPointInt(0, 0));
+               AltosLatLon             lower_right = transform.screen_lat_lon(new AltosPointInt(px_size, px_size));
+
                synchronized (launch_sites_ready) {
                        if (launch_sites == null) {
                                try {
@@ -73,8 +96,34 @@ public class AltosMapd implements AltosLaunchSiteListener {
                }
 
                for (AltosLaunchSite site : launch_sites) {
+
+                       /* Figure out which point in the tile to
+                        * measure to the site That's one of the edges
+                        * or the site location depend on where the
+                        * site is in relation to the tile
+                        */
+
+                       double          check_lon;
+
+                       if (west_of(site.longitude, upper_left.lon))
+                               check_lon = upper_left.lon;
+                       else if (west_of(lower_right.lon, site.longitude))
+                               check_lon = lower_right.lon;
+                       else
+                               check_lon = site.longitude;
+
+                       double          check_lat;
+
+                       if (site.latitude < lower_right.lat)
+                               check_lat = lower_right.lat;
+                       else if (upper_left.lat < site.latitude)
+                               check_lat = upper_left.lat;
+                       else
+                               check_lat = site.latitude;
+
                        AltosGreatCircle gc = new AltosGreatCircle(site.latitude, site.longitude,
-                                                                  lat, lon);
+                                                                  check_lat, check_lon);
+
                        if (gc.distance <= valid_radius)
                                return true;
                }
@@ -114,6 +163,13 @@ public class AltosMapd implements AltosLaunchSiteListener {
                        } else if (args[i].equals("--key") && i < args.length-1) {
                                key_file = args[i+1];
                                skip = 2;
+                       } else if (args[i].equals("--max-zoom") && i < args.length-1) {
+                               try {
+                                       max_zoom = AltosParse.parse_int(args[i+1]);
+                               } catch (ParseException pe) {
+                                       usage();
+                               }
+                               skip = 2;
                        } else {
                                usage();
                        }
@@ -163,7 +219,6 @@ public class AltosMapd implements AltosLaunchSiteListener {
                                        System.out.printf("accept failed\n");
                                        continue;
                                }
-                               System.out.printf("got client\n");
                                new AltosMapdClient(client);
                        } catch (Exception e) {
                                System.out.printf("Exception %s\n", e.toString());