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_7;
23 import java.lang.Math;
25 import java.net.URLConnection;
27 public class AltosMapLoader implements AltosMapTileListener, AltosMapStoreListener {
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 listener.debug("Make tile at %d, %d\n", x, y);
92 AltosPointInt point = new AltosPointInt(x, y);
93 AltosLatLon ul = transform.lat_lon(point);
94 AltosLatLon center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
95 AltosMapTile tile = map.map_interface.new_tile(this, ul, center, zoom, maptype, AltosMap.px_size);
96 tile.add_store_listener(this);
97 if (tile.store_status() != AltosMapTile.loading)
98 notify_tile(tile, tile.store_status());
103 public int next_type(int start) {
105 for (next_type = start;
106 next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
112 public void next_load() {
113 int next_type = next_type(cur_type + 1);
115 if (next_type > AltosMap.maptype_terrain) {
116 if (cur_z == max_z) {
121 next_type = next_type(0);
123 cur_type = next_type;
127 private void start_load() {
132 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
133 if ((all_types & (1 << t)) != 0)
136 all_types = (1 << AltosMap.maptype_hybrid);
140 cur_type = next_type(0);
142 for (int z = min_z; z <= max_z; z++)
143 tiles_total += tiles_per_layer(z);
145 layers_total = (max_z - min_z + 1) * ntype;
147 tiles_loaded_total = 0;
149 listener.debug("total tiles %d\n", tiles_total);
151 listener.loader_start(tiles_total);
155 public void load(double latitude, double longitude, int min_z, int max_z, double radius, int all_types) {
156 listener.debug("lat %f lon %f min_z %d max_z %d radius %f all_types %d\n",
157 latitude, longitude, min_z, max_z, radius, all_types);
158 this.latitude = latitude;
159 this.longitude = longitude;
162 this.radius = radius;
163 this.all_types = all_types;
167 public synchronized void notify_store(AltosMapStore store, int status) {
168 boolean do_next = false;
169 if (status == AltosMapTile.loading)
172 if (layers_loaded >= layers_total)
175 ++tiles_loaded_total;
176 ++tiles_loaded_layer;
177 listener.debug("total %d layer %d\n", tiles_loaded_total, tiles_loaded_layer);
179 if (tiles_loaded_layer == tiles_this_layer) {
181 listener.debug("%d layers loaded\n", layers_loaded);
182 if (layers_loaded == layers_total) {
183 listener.loader_done(tiles_total);
189 listener.loader_notify(tiles_loaded_total,
190 tiles_total, store.file.toString());
195 public synchronized void notify_tile(AltosMapTile tile, int status) {
196 notify_store(tile.store, status);
199 public AltosMapCache cache() { return map.cache(); }
201 public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener) {
203 this.listener = listener;