2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
21 import java.util.concurrent.*;
23 import org.altusmetrum.altoslib_13.*;
25 public class AltosMapd implements AltosLaunchSiteListener {
27 public static int port = 16717;
29 public final static int maptype = AltosMap.maptype_hybrid;
31 public final static int px_size = 512;
33 public final static int scale = 1;
35 public static int max_zoom = 17;
37 public static double valid_radius = 17000; /* 17km */
39 public String map_dir = null;
40 public String launch_sites_file = null;
41 public String key_file = null;
44 System.out.printf("usage: altos-mapd [--mapdir <map-directory] [--launch-sites <launch-sites-file>]\n" +
45 " [--radius <valid-radius-m> [--port <port>] [--key <key-file>]\n" +
46 " [--max-zoom <max-zoom-level>\n");
50 private static Semaphore launch_sites_ready;
52 private static List<AltosLaunchSite> launch_sites;
54 public void notify_launch_sites(List<AltosLaunchSite> sites) {
55 synchronized (launch_sites_ready) {
58 launch_sites_ready.release();
63 private static boolean west_of(double a, double b) {
68 while (diff <= -360.0)
74 public static boolean check_lat_lon(double lat, double lon, int zoom) {
79 AltosMapTransform transform = new AltosMapTransform(px_size, px_size, zoom, new AltosLatLon(lat, lon));
81 AltosLatLon upper_left = transform.screen_lat_lon(new AltosPointInt(0, 0));
82 AltosLatLon lower_right = transform.screen_lat_lon(new AltosPointInt(px_size, px_size));
84 synchronized (launch_sites_ready) {
85 if (launch_sites == null) {
87 launch_sites_ready.acquire();
88 } catch (InterruptedException ie) {
93 if (launch_sites == null) {
94 System.out.printf("No launch site data available, refusing all requests\n");
98 for (AltosLaunchSite site : launch_sites) {
100 /* Figure out which point in the tile to
101 * measure to the site That's one of the edges
102 * or the site location depend on where the
103 * site is in relation to the tile
108 if (west_of(site.longitude, upper_left.lon))
109 check_lon = upper_left.lon;
110 else if (west_of(lower_right.lon, site.longitude))
111 check_lon = lower_right.lon;
113 check_lon = site.longitude;
117 if (site.latitude < lower_right.lat)
118 check_lat = lower_right.lat;
119 else if (upper_left.lat < site.latitude)
120 check_lat = upper_left.lat;
122 check_lat = site.latitude;
124 AltosGreatCircle gc = new AltosGreatCircle(site.latitude, site.longitude,
125 check_lat, check_lon);
127 if (gc.distance <= valid_radius)
134 AltosMapdServer server;
136 public void process(String[] args) {
138 AltosPreferences.init(new AltosMapdPreferences());
141 for (int i = 0; i < args.length; i += skip) {
143 if (args[i].equals("--mapdir") && i < args.length-1) {
146 } else if (args[i].equals("--launch-sites") && i < args.length-1) {
147 launch_sites_file = args[i+1];
149 } else if (args[i].equals("--radius") && i < args.length-1) {
151 valid_radius = AltosParse.parse_double_locale(args[i+1]);
152 } catch (ParseException pe) {
156 } else if (args[i].equals("--port") && i < args.length-1) {
158 port = AltosParse.parse_int(args[i+1]);
159 } catch (ParseException pe) {
163 } else if (args[i].equals("--key") && i < args.length-1) {
164 key_file = args[i+1];
166 } else if (args[i].equals("--max-zoom") && i < args.length-1) {
168 max_zoom = AltosParse.parse_int(args[i+1]);
169 } catch (ParseException pe) {
181 if (key_file != null) {
183 BufferedReader key_reader = new BufferedReader(new FileReader(key_file));
185 String line = key_reader.readLine();
186 if (line == null || line.length() != 39) {
187 System.out.printf("%s: invalid contents %d \"%s\"\n",
188 key_file, line.length(), line);
192 AltosMapStore.google_maps_api_key = line;
193 } catch (Exception e) {
194 System.out.printf("%s: %s\n", key_file, e.toString());
199 AltosPreferences.mapdir = new File(map_dir);
201 if (launch_sites_file != null)
202 AltosLaunchSites.launch_sites_url = "file://" + launch_sites_file;
204 launch_sites_ready = new Semaphore(0);
206 new AltosLaunchSites(this);
209 server = new AltosMapdServer(port);
210 } catch (IOException ie) {
211 System.out.printf("Cannot bind to port %d: %s\n", port, ie.toString());
217 Socket client = server.accept();
218 if (client == null) {
219 System.out.printf("accept failed\n");
222 new AltosMapdClient(client);
223 } catch (Exception e) {
224 System.out.printf("Exception %s\n", e.toString());
229 public void AltosMapd() {
232 public static void main(final String[] args) {
233 new AltosMapd().process(args);