2 * Copyright © 2015 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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_14;
23 import java.util.concurrent.*;
25 import java.lang.Math;
27 import java.net.URLConnection;
29 public class AltosMapLoader extends Thread implements AltosMapStoreListener {
30 AltosMapLoaderListener listener;
32 double latitude, longitude;
41 int tiles_loaded_layer;
42 int tiles_loaded_total;
48 private static final int MAX_LOADING = 200;
50 private Semaphore loading = new Semaphore(MAX_LOADING);
54 int tile_radius(int zoom) {
55 double delta_lon = AltosMapTransform.lon_from_distance(latitude, radius);
57 AltosMapTransform t = new AltosMapTransform(256, 256, zoom + AltosMap.default_zoom, new AltosLatLon(latitude, longitude));
59 AltosPointDouble center = t.point(new AltosLatLon(latitude, longitude));
60 AltosPointDouble edge = t.point(new AltosLatLon(latitude, longitude + delta_lon));
62 int tile_radius = (int) Math.ceil(Math.abs(center.x - edge.x) / AltosMap.px_size);
67 int tiles_per_layer(int zoom) {
68 int tile_radius = tile_radius(zoom);
69 return (tile_radius * 2 + 1) * (tile_radius * 2 + 1);
72 private boolean do_load() {
73 tiles_this_layer = tiles_per_layer(cur_z);
74 tiles_loaded_layer = 0;
75 listener.debug("tiles_this_layer %d (zoom %d)\n", tiles_this_layer, cur_z);
77 int load_radius = tile_radius(cur_z);
78 int zoom = cur_z + AltosMap.default_zoom;
79 int maptype = cur_type;
80 AltosLatLon load_centre = new AltosLatLon(latitude, longitude);
81 AltosMapTransform transform = new AltosMapTransform(256, 256, zoom, load_centre);
83 AltosPointInt upper_left;
84 AltosPointInt lower_right;
86 AltosPointInt centre = AltosMap.floor(transform.point(load_centre));
88 upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
89 centre.y - load_radius * AltosMap.px_size);
90 lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
91 centre.y + load_radius * AltosMap.px_size);
94 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
95 for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
98 } catch (InterruptedException ie) {
101 AltosPointInt point = new AltosPointInt(x, y);
102 AltosLatLon ul = transform.lat_lon(point);
103 AltosLatLon center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
104 AltosMapStore store = AltosMapStore.get(center, zoom, maptype, AltosMap.px_size, scale);
105 listener.debug("load state %s url %s\n", AltosMapTile.status_name(store.status()), store.url);
106 store.add_listener(this);
114 private int next_type(int start) {
116 for (next_type = start;
117 next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
123 private boolean next_load() {
124 int next_type = next_type(cur_type + 1);
126 if (next_type > AltosMap.maptype_terrain) {
127 if (cur_z == max_z) {
132 next_type = next_type(0);
134 cur_type = next_type;
143 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
144 if ((all_types & (1 << t)) != 0)
147 all_types = (1 << AltosMap.maptype_hybrid);
151 cur_type = next_type(0);
154 for (int z = min_z; z <= max_z; z++)
155 tiles_total += tiles_per_layer(z) * ntype;
157 layers_total = (max_z - min_z + 1) * ntype;
159 tiles_loaded_total = 0;
161 listener.debug("total tiles %d layers %d\n", tiles_total, layers_total);
163 listener.loader_start(tiles_total);
167 } while (next_load());
169 listener.loader_done(tiles_total);
172 public synchronized void notify_store(AltosMapStore store, int status) {
173 boolean do_next = false;
174 if (status == AltosMapTile.fetching)
179 store.remove_listener(this);
181 if (layers_loaded >= layers_total)
184 ++tiles_loaded_total;
185 ++tiles_loaded_layer;
187 listener.debug("AltosMapLoader.notify_store status %d total %d of %d layer %d of %d\n",
188 status, tiles_loaded_total, tiles_total, tiles_loaded_layer, tiles_this_layer);
190 if (tiles_loaded_layer == tiles_this_layer) {
192 listener.debug("%d layers loaded\n", layers_loaded);
196 if (tiles_loaded_total == tiles_total)
197 listener.loader_done(tiles_total);
199 listener.loader_notify(tiles_loaded_total,
200 tiles_total, store.file.toString());
203 public void abort() {
207 public AltosMapLoader(AltosMapLoaderListener listener,
208 double latitude, double longitude, int min_z, int max_z, double radius, int all_types, int scale) {
209 listener.debug("lat %f lon %f min_z %d max_z %d radius %f all_types %d\n",
210 latitude, longitude, min_z, max_z, radius, all_types);
211 this.listener = listener;
212 this.latitude = latitude;
213 this.longitude = longitude;
216 this.radius = radius;
218 this.all_types = all_types;
220 this.all_types = 1 << AltosMap.maptype_hybrid;