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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_10;
23 import java.lang.Math;
25 import java.net.URLConnection;
27 public class AltosMapLoader extends Thread implements AltosMapTileListener {
28 AltosMapLoaderListener listener;
30 double latitude, longitude;
38 int tiles_loaded_layer;
39 int tiles_loaded_total;
47 int tile_radius(int zoom) {
48 double delta_lon = AltosMapTransform.lon_from_distance(latitude, radius);
50 AltosMapTransform t = new AltosMapTransform(256, 256, zoom + AltosMap.default_zoom, new AltosLatLon(latitude, longitude));
52 AltosPointDouble center = t.point(new AltosLatLon(latitude, longitude));
53 AltosPointDouble edge = t.point(new AltosLatLon(latitude, longitude + delta_lon));
55 int tile_radius = (int) Math.ceil(Math.abs(center.x - edge.x) / AltosMap.px_size);
60 int tiles_per_layer(int zoom) {
61 int tile_radius = tile_radius(zoom);
62 return (tile_radius * 2 + 1) * (tile_radius * 2 + 1);
65 public void do_load() {
66 tiles_this_layer = tiles_per_layer(cur_z);
67 tiles_loaded_layer = 0;
68 listener.debug("tiles_this_layer %d (zoom %d)\n", tiles_this_layer, cur_z);
70 int load_radius = tile_radius(cur_z);
71 int zoom = cur_z + AltosMap.default_zoom;
72 int maptype = cur_type;
73 AltosLatLon load_centre = new AltosLatLon(latitude, longitude);
74 AltosMapTransform transform = new AltosMapTransform(256, 256, zoom, load_centre);
76 map.centre(load_centre);
78 AltosPointInt upper_left;
79 AltosPointInt lower_right;
81 AltosPointInt centre = AltosMap.floor(transform.point(load_centre));
83 upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
84 centre.y - load_radius * AltosMap.px_size);
85 lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
86 centre.y + load_radius * AltosMap.px_size);
89 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
90 for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
91 AltosPointInt point = new AltosPointInt(x, y);
92 AltosLatLon ul = transform.lat_lon(point);
93 AltosLatLon center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
94 AltosMapTile tile = map.map_interface.new_tile(null, ul, center, zoom, maptype, AltosMap.px_size);
95 tile.add_listener(this);
100 public int next_type(int start) {
102 for (next_type = start;
103 next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
109 public void next_load() {
110 int next_type = next_type(cur_type + 1);
112 if (next_type > AltosMap.maptype_terrain) {
113 if (cur_z == max_z) {
118 next_type = next_type(0);
120 cur_type = next_type;
129 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
130 if ((all_types & (1 << t)) != 0)
133 all_types = (1 << AltosMap.maptype_hybrid);
137 cur_type = next_type(0);
140 for (int z = min_z; z <= max_z; z++)
141 tiles_total += tiles_per_layer(z) * ntype;
143 layers_total = (max_z - min_z + 1) * ntype;
145 tiles_loaded_total = 0;
147 listener.debug("total tiles %d layers %d\n", tiles_total, layers_total);
149 listener.loader_start(tiles_total);
153 public synchronized void notify_tile(AltosMapTile tile, int status) {
154 boolean do_next = false;
155 if (status == AltosMapTile.fetching)
158 tile.remove_listener(this);
160 if (layers_loaded >= layers_total)
163 ++tiles_loaded_total;
164 ++tiles_loaded_layer;
166 listener.debug("AltosMapLoader.notify_tile status %d total %d of %d layer %d of %d\n",
167 status, tiles_loaded_total, tiles_total, tiles_loaded_layer, tiles_this_layer);
169 if (tiles_loaded_layer == tiles_this_layer) {
171 listener.debug("%d layers loaded\n", layers_loaded);
175 if (tiles_loaded_total == tiles_total)
176 listener.loader_done(tiles_total);
178 listener.loader_notify(tiles_loaded_total,
179 tiles_total, tile.store.file.toString());
185 public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener,
186 double latitude, double longitude, int min_z, int max_z, double radius, int all_types) {
187 listener.debug("lat %f lon %f min_z %d max_z %d radius %f all_types %d\n",
188 latitude, longitude, min_z, max_z, radius, all_types);
190 this.listener = listener;
191 this.latitude = latitude;
192 this.longitude = longitude;
195 this.radius = radius;
196 this.all_types = all_types;