7112a1c4d22f06e8c7af078ff8b4ef2c93957fe7
[fw/altos] / altoslib / AltosMapLoader.java
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_10;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.lang.Math;
24 import java.net.URL;
25 import java.net.URLConnection;
26
27 public class AltosMapLoader extends Thread implements AltosMapTileListener {
28         AltosMapLoaderListener  listener;
29
30         double  latitude, longitude;
31         int     min_z;
32         int     max_z;
33         int     cur_z;
34         int     all_types;
35         int     cur_type;
36         double  radius;
37
38         int     tiles_loaded_layer;
39         int     tiles_loaded_total;
40         int     tiles_this_layer;
41         int     tiles_total;
42         int     layers_total;
43         int     layers_loaded;
44
45         AltosMap        map;
46
47         int tile_radius(int zoom) {
48                 double  delta_lon = AltosMapTransform.lon_from_distance(latitude, radius);
49
50                 AltosMapTransform t = new AltosMapTransform(256, 256, zoom + AltosMap.default_zoom, new AltosLatLon(latitude, longitude));
51
52                 AltosPointDouble        center = t.point(new AltosLatLon(latitude, longitude));
53                 AltosPointDouble        edge = t.point(new AltosLatLon(latitude, longitude + delta_lon));
54
55                 int tile_radius = (int) Math.ceil(Math.abs(center.x - edge.x) / AltosMap.px_size);
56
57                 return tile_radius;
58         }
59
60         int tiles_per_layer(int zoom) {
61                 int     tile_radius = tile_radius(zoom);
62                 return (tile_radius * 2 + 1) * (tile_radius * 2 + 1);
63         }
64
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);
69
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);
75
76                 map.centre(load_centre);
77
78                 AltosPointInt   upper_left;
79                 AltosPointInt   lower_right;
80
81                 AltosPointInt centre = AltosMap.floor(transform.point(load_centre));
82
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);
87
88
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);
96                         }
97                 }
98         }
99
100         public int next_type(int start) {
101                 int next_type;
102                 for (next_type = start;
103                      next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
104                      next_type++)
105                         ;
106                 return next_type;
107         }
108
109         public void next_load() {
110                 int next_type = next_type(cur_type + 1);
111
112                 if (next_type > AltosMap.maptype_terrain) {
113                         if (cur_z == max_z) {
114                                 return;
115                         } else {
116                                 cur_z++;
117                         }
118                         next_type = next_type(0);
119                 }
120                 cur_type = next_type;
121                 do_load();
122         }
123
124         public void run() {
125
126                 cur_z = min_z;
127                 int ntype = 0;
128
129                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
130                         if ((all_types & (1 << t)) != 0)
131                                 ntype++;
132                 if (ntype == 0) {
133                         all_types = (1 << AltosMap.maptype_hybrid);
134                         ntype = 1;
135                 }
136
137                 cur_type = next_type(0);
138
139                 tiles_total = 0;
140                 for (int z = min_z; z <= max_z; z++)
141                         tiles_total += tiles_per_layer(z) * ntype;
142
143                 layers_total = (max_z - min_z + 1) * ntype;
144                 layers_loaded = 0;
145                 tiles_loaded_total = 0;
146
147                 listener.debug("total tiles %d layers %d\n", tiles_total, layers_total);
148
149                 listener.loader_start(tiles_total);
150                 do_load();
151         }
152
153         public synchronized void notify_tile(AltosMapTile tile, int status) {
154                 boolean do_next = false;
155                 if (status == AltosMapTile.fetching)
156                         return;
157
158                 tile.remove_listener(this);
159
160                 if (layers_loaded >= layers_total)
161                         return;
162
163                 ++tiles_loaded_total;
164                 ++tiles_loaded_layer;
165
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);
168
169                 if (tiles_loaded_layer == tiles_this_layer) {
170                         ++layers_loaded;
171                         listener.debug("%d layers loaded\n", layers_loaded);
172                         do_next = true;
173                 }
174
175                 if (tiles_loaded_total == tiles_total)
176                         listener.loader_done(tiles_total);
177                 else {
178                         listener.loader_notify(tiles_loaded_total,
179                                                tiles_total, tile.store.file.toString());
180                         if (do_next)
181                                 next_load();
182                 }
183         }
184
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);
189                 this.map = map;
190                 this.listener = listener;
191                 this.latitude = latitude;
192                 this.longitude = longitude;
193                 this.min_z = min_z;
194                 this.max_z = max_z;
195                 this.radius = radius;
196                 this.all_types = all_types;
197                 start();
198         }
199 }