From 3b4e6da65158a434905dc652e46c69d2c38cea7f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 27 May 2015 23:12:34 -0700 Subject: [PATCH] altoslib: Add map loading helper class 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 --- altoslib/AltosMapLoader.java | 143 +++++++++++++++++++++++++++ altoslib/AltosMapLoaderListener.java | 26 +++++ altoslib/Makefile.am | 2 + 3 files changed, 171 insertions(+) create mode 100644 altoslib/AltosMapLoader.java create mode 100644 altoslib/AltosMapLoaderListener.java diff --git a/altoslib/AltosMapLoader.java b/altoslib/AltosMapLoader.java new file mode 100644 index 00000000..5db20cf8 --- /dev/null +++ b/altoslib/AltosMapLoader.java @@ -0,0 +1,143 @@ +/* + * Copyright © 2015 Keith Packard + * + * 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 index 00000000..79f8b9df --- /dev/null +++ b/altoslib/AltosMapLoaderListener.java @@ -0,0 +1,26 @@ +/* + * Copyright © 2015 Keith Packard + * + * 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); +} diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index de13c1ec..a6b178fa 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -154,6 +154,8 @@ altoslib_JAVA = \ AltosLaunchSite.java \ AltosLaunchSiteListener.java \ AltosLaunchSites.java \ + AltosMapLoaderListener.java \ + AltosMapLoader.java \ AltosVersion.java JAR=altoslib_$(ALTOSLIB_VERSION).jar -- 2.30.2