altoslib: Add map loading helper class
authorKeith Packard <keithp@keithp.com>
Thu, 28 May 2015 06:12:34 +0000 (23:12 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 28 May 2015 06:12:34 +0000 (23:12 -0700)
This adds the AltosMapLoader class, which iterates over a sequence of
zoom levels and formats to get local copies of a desired launch site.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosMapLoader.java [new file with mode: 0644]
altoslib/AltosMapLoaderListener.java [new file with mode: 0644]
altoslib/Makefile.am

diff --git a/altoslib/AltosMapLoader.java b/altoslib/AltosMapLoader.java
new file mode 100644 (file)
index 0000000..5db20cf
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * 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 org.altusmetrum.altoslib_7;
+
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.lang.Math;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class AltosMapLoader implements AltosMapTileListener {
+       AltosMapLoaderListener  listener;
+
+       double  latitude, longitude;
+       int     min_z;
+       int     max_z;
+       int     cur_z;
+       int     all_types;
+       int     cur_type;
+       int     radius;
+
+       int     tiles_per_layer;
+       int     tiles_loaded;
+       int     layers_total;
+       int     layers_loaded;
+
+       AltosMap        map;
+       AltosMapCache   cache;
+
+       public void do_load() {
+               map.set_zoom(cur_z + AltosMap.default_zoom);
+               map.set_maptype(cur_type);
+               map.set_load_params(latitude, longitude, radius, this);
+       }
+
+       public int next_type(int start) {
+               int next_type;
+               for (next_type = start;
+                    next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
+                    next_type++)
+                       ;
+               return next_type;
+       }
+
+       public void next_load() {
+               int next_type = next_type(cur_type + 1);
+
+               if (next_type > AltosMap.maptype_terrain) {
+                       if (cur_z == max_z) {
+                               return;
+                       } else {
+                               cur_z++;
+                       }
+                       next_type = next_type(0);
+               }
+               cur_type = next_type;
+               do_load();
+       }
+
+       private void start_load() {
+
+               cur_z = min_z;
+               int ntype = 0;
+
+               for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
+                       if ((all_types & (1 << t)) != 0)
+                               ntype++;
+               if (ntype == 0) {
+                       all_types = (1 << AltosMap.maptype_hybrid);
+                       ntype = 1;
+               }
+
+               cur_type = next_type(0);
+               tiles_per_layer = (radius * 2 + 1) * (radius * 2 + 1);
+               layers_total = (max_z - min_z + 1) * ntype;
+               layers_loaded = 0;
+               tiles_loaded = 0;
+
+               listener.loader_start(layers_total * tiles_per_layer);
+               do_load();
+       }
+
+       public void load(double latitude, double longitude, int min_z, int max_z, int radius, int all_types) {
+               this.latitude = latitude;
+               this.longitude = longitude;
+               this.min_z = min_z;
+               this.max_z = max_z;
+               this.radius = radius;
+               this.all_types = all_types;
+               start_load();
+       }
+
+       public synchronized void notify_tile(AltosMapTile tile, int status) {
+               boolean do_next = false;
+               if (status == AltosMapTile.loading)
+                       return;
+
+               if (layers_loaded >= layers_total)
+                       return;
+
+               ++tiles_loaded;
+
+               if (tiles_loaded == tiles_per_layer) {
+                       tiles_loaded = 0;
+                       ++layers_loaded;
+                       if (layers_loaded == layers_total) {
+                               listener.loader_done(layers_total * tiles_per_layer);
+                               return;
+                       } else {
+                               do_next = true;
+                       }
+               }
+               listener.loader_notify(layers_loaded * tiles_per_layer + tiles_loaded,
+                                      layers_total * tiles_per_layer, tile.store.file.toString());
+               if (do_next)
+                       next_load();
+       }
+
+       public AltosMapCache cache() { return cache; }
+
+       public AltosMapLoader(AltosMap map, AltosMapCache cache,
+                             AltosMapLoaderListener listener) {
+               this.map = map;
+               this.cache = cache;
+               this.listener = listener;
+       }
+}
diff --git a/altoslib/AltosMapLoaderListener.java b/altoslib/AltosMapLoaderListener.java
new file mode 100644 (file)
index 0000000..79f8b9d
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * 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 org.altusmetrum.altoslib_7;
+
+public interface AltosMapLoaderListener {
+       public abstract void loader_start(int max);
+
+       public abstract void loader_notify(int cur, int max, String name);
+
+       public abstract void loader_done(int max);
+}
index de13c1ec0853f2a4d8d798bf86f3f9a513c18a4b..a6b178fa167fcbee9efcd110222432c5c7abf8ab 100644 (file)
@@ -154,6 +154,8 @@ altoslib_JAVA = \
        AltosLaunchSite.java \
        AltosLaunchSiteListener.java \
        AltosLaunchSites.java \
        AltosLaunchSite.java \
        AltosLaunchSiteListener.java \
        AltosLaunchSites.java \
+       AltosMapLoaderListener.java \
+       AltosMapLoader.java \
        AltosVersion.java
 
 JAR=altoslib_$(ALTOSLIB_VERSION).jar
        AltosVersion.java
 
 JAR=altoslib_$(ALTOSLIB_VERSION).jar