15fd756ef5de6d6eea07ba959db483d405d3ec63
[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 implements AltosMapTileListener, AltosMapStoreListener {
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(this, ul, center, zoom, maptype, AltosMap.px_size);
95                                 tile.add_store_listener(this);
96                                 if (tile.store_status() != AltosMapTile.loading)
97                                         notify_tile(tile, tile.store_status());
98                         }
99                 }
100         }
101
102         public int next_type(int start) {
103                 int next_type;
104                 for (next_type = start;
105                      next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
106                      next_type++)
107                         ;
108                 return next_type;
109         }
110
111         public void next_load() {
112                 int next_type = next_type(cur_type + 1);
113
114                 if (next_type > AltosMap.maptype_terrain) {
115                         if (cur_z == max_z) {
116                                 return;
117                         } else {
118                                 cur_z++;
119                         }
120                         next_type = next_type(0);
121                 }
122                 cur_type = next_type;
123                 do_load();
124         }
125
126         private void start_load() {
127
128                 cur_z = min_z;
129                 int ntype = 0;
130
131                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
132                         if ((all_types & (1 << t)) != 0)
133                                 ntype++;
134                 if (ntype == 0) {
135                         all_types = (1 << AltosMap.maptype_hybrid);
136                         ntype = 1;
137                 }
138
139                 cur_type = next_type(0);
140
141                 for (int z = min_z; z <= max_z; z++)
142                         tiles_total += tiles_per_layer(z);
143
144                 layers_total = (max_z - min_z + 1) * ntype;
145                 layers_loaded = 0;
146                 tiles_loaded_total = 0;
147
148                 listener.debug("total tiles %d\n", tiles_total);
149
150                 listener.loader_start(tiles_total);
151                 do_load();
152         }
153
154         public void load(double latitude, double longitude, int min_z, int max_z, double radius, int all_types) {
155                 listener.debug("lat %f lon %f min_z %d max_z %d radius %f all_types %d\n",
156                                latitude, longitude, min_z, max_z, radius, all_types);
157                 this.latitude = latitude;
158                 this.longitude = longitude;
159                 this.min_z = min_z;
160                 this.max_z = max_z;
161                 this.radius = radius;
162                 this.all_types = all_types;
163                 start_load();
164         }
165
166         public synchronized void notify_store(AltosMapStore store, int status) {
167                 boolean do_next = false;
168                 if (status == AltosMapTile.loading)
169                         return;
170
171                 if (layers_loaded >= layers_total)
172                         return;
173
174                 ++tiles_loaded_total;
175                 ++tiles_loaded_layer;
176
177                 if (tiles_loaded_layer == tiles_this_layer) {
178                         ++layers_loaded;
179                         listener.debug("%d layers loaded\n", layers_loaded);
180                         if (layers_loaded == layers_total) {
181                                 listener.loader_done(tiles_total);
182                                 return;
183                         } else {
184                                 do_next = true;
185                         }
186                 }
187                 listener.loader_notify(tiles_loaded_total,
188                                        tiles_total, store.file.toString());
189                 if (do_next)
190                         next_load();
191         }
192
193         public synchronized void notify_tile(AltosMapTile tile, int status) {
194                 notify_store(tile.store, status);
195         }
196
197         public AltosMapCache cache() { return map.cache(); }
198
199         public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener) {
200                 this.map = map;
201                 this.listener = listener;
202         }
203 }