2 * Copyright © 2014 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_13;
25 public class AltosMapStore {
28 LinkedList<AltosMapStoreListener> listeners = new LinkedList<AltosMapStoreListener>();
32 private static File map_file(AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
33 double lat = center.lat;
34 double lon = center.lon;
35 char chlat = lat < 0 ? 'S' : 'N';
36 char chlon = lon < 0 ? 'W' : 'E';
38 if (lat < 0) lat = -lat;
39 if (lon < 0) lon = -lon;
40 String maptype_string = String.format("%s-", AltosMap.maptype_names[maptype]);
42 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
43 format_string = "jpg";
45 format_string = "png";
46 return new File(AltosPreferences.mapdir(),
47 String.format("map-%c%.6f,%c%.6f-%s%d%s.%s",
48 chlat, lat, chlon, lon, maptype_string, zoom, scale == 1 ? "" : String.format("-%d", scale), format_string));
51 public static String google_maps_api_key = null;
53 private static String google_map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale, String format_string) {
54 return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&scale=%d&sensor=false&maptype=%s&format=%s&key=%s",
55 center.lat, center.lon, zoom, px_size, px_size, scale,
56 AltosMap.maptype_names[maptype], format_string, google_maps_api_key);
59 private static String altos_map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale, String format_string) {
60 return String.format("https://maps.altusmetrum.org/altos-map?center=%.6f,%.6f&zoom=%d",
61 center.lat, center.lon, zoom);
64 private static String map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
67 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
68 format_string = "jpg";
70 format_string = "png32";
72 for (int s = 1; s < scale; s <<= 1)
77 if (google_maps_api_key != null)
78 return google_map_url(center, zoom, maptype, px_size, scale, format_string);
80 return altos_map_url(center, zoom, maptype, px_size, scale, format_string);
83 public synchronized int status() {
87 public synchronized void add_listener(AltosMapStoreListener listener) {
88 if (!listeners.contains(listener))
89 listeners.add(listener);
90 listener.notify_store(this, status);
93 public synchronized void remove_listener(AltosMapStoreListener listener) {
94 listeners.remove(listener);
97 private synchronized void notify_listeners(int status) {
99 for (AltosMapStoreListener listener : listeners)
100 listener.notify_store(this, status);
103 static Object forbidden_lock = new Object();
104 static long forbidden_time;
105 static boolean forbidden_set;
106 public static int forbidden_response;
108 private int fetch_url() {
113 } catch (java.net.MalformedURLException e) {
114 return AltosMapTile.bad_request;
118 URLConnection uc = null;
120 uc = u.openConnection();
121 String type = uc.getContentType();
122 int contentLength = uc.getContentLength();
123 if (uc instanceof HttpURLConnection) {
124 int response = ((HttpURLConnection) uc).getResponseCode();
126 case HttpURLConnection.HTTP_FORBIDDEN:
127 case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
128 case HttpURLConnection.HTTP_UNAUTHORIZED:
129 synchronized (forbidden_lock) {
130 forbidden_time = System.nanoTime();
131 forbidden_set = true;
132 forbidden_response = response;
133 return AltosMapTile.forbidden;
137 InputStream in = new BufferedInputStream(uc.getInputStream());
140 data = new byte[contentLength];
141 while (offset < contentLength) {
142 bytesRead = in.read(data, offset, data.length - offset);
149 if (offset != contentLength)
150 return AltosMapTile.failed;
152 } catch (IOException e) {
153 return AltosMapTile.failed;
157 FileOutputStream out = new FileOutputStream(file);
161 } catch (FileNotFoundException e) {
162 return AltosMapTile.bad_request;
163 } catch (IOException e) {
166 return AltosMapTile.bad_request;
168 return AltosMapTile.fetched;
171 static Object fetch_lock = new Object();
173 static final long forbidden_interval = 60l * 1000l * 1000l * 1000l;
174 static final long google_maps_ratelimit_ms = 1200;
176 static Object fetcher_lock = new Object();
178 static LinkedList<AltosMapStore> waiting = new LinkedList<AltosMapStore>();
179 static LinkedList<AltosMapStore> running = new LinkedList<AltosMapStore>();
181 static final int concurrent_fetchers = 128;
183 static void start_fetchers() {
184 while (!waiting.isEmpty() && running.size() < concurrent_fetchers) {
185 AltosMapStore s = waiting.remove();
187 Thread lt = s.make_fetcher_thread();
192 void finish_fetcher() {
193 synchronized(fetcher_lock) {
194 running.remove(this);
200 synchronized(fetcher_lock) {
206 class fetcher implements Runnable {
211 notify_listeners(AltosMapTile.fetched);
215 synchronized(forbidden_lock) {
216 if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval) {
217 notify_listeners(AltosMapTile.forbidden);
224 new_status = fetch_url();
226 notify_listeners(new_status);
233 private Thread make_fetcher_thread() {
234 return new Thread(new fetcher());
237 private void fetch() {
241 private AltosMapStore (String url, File file) {
246 status = AltosMapTile.fetched;
248 status = AltosMapTile.fetching;
253 public int hashCode() {
254 return url.hashCode();
257 public boolean equals(Object o) {
261 if (!(o instanceof AltosMapStore))
264 AltosMapStore other = (AltosMapStore) o;
265 return url.equals(other.url);
268 static HashMap<String,AltosMapStore> stores = new HashMap<String,AltosMapStore>();
270 public static AltosMapStore get(AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
271 String url = map_url(center, zoom, maptype, px_size, scale);
274 synchronized(stores) {
275 if (stores.containsKey(url)) {
276 store = stores.get(url);
278 store = new AltosMapStore(url, map_file(center, zoom, maptype, px_size, scale));
279 stores.put(url, store);