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/cgi-bin/altos-map?lat=%.6f&lon=%.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 private int fetch_url() {
108 } catch (java.net.MalformedURLException e) {
109 return AltosMapTile.bad_request;
113 URLConnection uc = null;
115 int status = AltosMapTile.failed;
118 while (tries < 10 && status != AltosMapTile.fetched) {
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 return AltosMapTile.forbidden;
132 InputStream in = new BufferedInputStream(uc.getInputStream());
135 data = new byte[contentLength];
136 while (offset < contentLength) {
137 bytesRead = in.read(data, offset, data.length - offset);
144 if (offset == contentLength)
145 status = AltosMapTile.fetched;
147 status = AltosMapTile.failed;
149 } catch (IOException e) {
150 status = AltosMapTile.failed;
153 if (status != AltosMapTile.fetched) {
156 } catch (InterruptedException ie) {
159 System.out.printf("Fetch failed, retrying %d\n", tries);
163 if (status != AltosMapTile.fetched)
167 FileOutputStream out = new FileOutputStream(file);
172 } catch (FileNotFoundException e) {
173 return AltosMapTile.bad_request;
174 } catch (IOException e) {
177 return AltosMapTile.bad_request;
179 return AltosMapTile.fetched;
182 static Object fetch_lock = new Object();
184 static Object fetcher_lock = new Object();
186 static LinkedList<AltosMapStore> waiting = new LinkedList<AltosMapStore>();
187 static LinkedList<AltosMapStore> running = new LinkedList<AltosMapStore>();
189 static int concurrent_fetchers() {
190 if (google_maps_api_key == null)
195 static void start_fetchers() {
196 while (!waiting.isEmpty() && running.size() < concurrent_fetchers()) {
197 AltosMapStore s = waiting.remove();
199 Thread lt = s.make_fetcher_thread();
204 void finish_fetcher() {
205 synchronized(fetcher_lock) {
206 running.remove(this);
212 synchronized(fetcher_lock) {
218 class fetcher implements Runnable {
223 notify_listeners(AltosMapTile.fetched);
229 new_status = fetch_url();
231 notify_listeners(new_status);
238 private Thread make_fetcher_thread() {
239 return new Thread(new fetcher());
242 private void fetch() {
246 private AltosMapStore (String url, File file) {
251 status = AltosMapTile.fetched;
253 status = AltosMapTile.fetching;
258 public int hashCode() {
259 return url.hashCode();
262 public boolean equals(Object o) {
266 if (!(o instanceof AltosMapStore))
269 AltosMapStore other = (AltosMapStore) o;
270 return url.equals(other.url);
273 static HashMap<String,AltosMapStore> stores = new HashMap<String,AltosMapStore>();
275 public static AltosMapStore get(AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
276 String url = map_url(center, zoom, maptype, px_size, scale);
279 synchronized(stores) {
280 if (stores.containsKey(url)) {
281 store = stores.get(url);
283 store = new AltosMapStore(url, map_file(center, zoom, maptype, px_size, scale));
284 stores.put(url, store);