Update java library versions
[fw/altos] / altoslib / AltosMapTile.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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_9;
19
20 import java.io.*;
21 import java.util.*;
22
23 public abstract class AltosMapTile implements AltosFontListener {
24         AltosMapTileListener    listener;
25         public AltosLatLon      upper_left, center;
26         public int              px_size;
27         int             zoom;
28         int             maptype;
29         int             scale;
30         public AltosMapStore    store;
31         public AltosMapCache    cache;
32         public int      status;
33
34         static public final int success = 0;
35         static public final int loading = 1;
36         static public final int failed = 2;
37         static public final int bad_request = 3;
38         static public final int forbidden = 4;
39
40         private File map_file() {
41                 double lat = center.lat;
42                 double lon = center.lon;
43                 char chlat = lat < 0 ? 'S' : 'N';
44                 char chlon = lon < 0 ? 'W' : 'E';
45
46                 if (lat < 0) lat = -lat;
47                 if (lon < 0) lon = -lon;
48                 String maptype_string = String.format("%s-", AltosMap.maptype_names[maptype]);
49                 String format_string;
50                 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
51                         format_string = "jpg";
52                 else
53                         format_string = "png";
54                 return new File(AltosPreferences.mapdir(),
55                                 String.format("map-%c%.6f,%c%.6f-%s%d%s.%s",
56                                               chlat, lat, chlon, lon, maptype_string, zoom, scale == 1 ? "" : String.format("-%d", scale), format_string));
57         }
58
59         private String map_url() {
60                 String format_string;
61                 int z = zoom;
62
63                 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
64                         format_string = "jpg";
65                 else
66                         format_string = "png32";
67
68                 for (int s = 1; s < scale; s <<= 1)
69                         z--;
70
71                 if (AltosVersion.has_google_maps_api_key())
72                         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",
73                                              center.lat, center.lon, z, px_size/scale, px_size/scale, scale, AltosMap.maptype_names[maptype], format_string, AltosVersion.google_maps_api_key);
74                 else
75                         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",
76                                              center.lat, center.lon, z, px_size/scale, px_size/scale, AltosMap.maptype_names[maptype], format_string);
77         }
78
79         public void font_size_changed(int font_size) {
80         }
81
82         public void set_status(int status) {
83                 this.status = status;
84                 listener.notify_tile(this, status);
85         }
86
87         public void notify_image(AltosImage image) {
88                 listener.notify_tile(this, status);
89         }
90
91         public int store_status() {
92                 return store.status();
93         }
94
95         public void add_store_listener(AltosMapStoreListener listener) {
96                 store.add_listener(listener);
97         }
98
99         public void remove_store_listener(AltosMapStoreListener listener) {
100                 store.remove_listener(listener);
101         }
102
103         public abstract void paint(AltosMapTransform t);
104
105         public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
106                 this.listener = listener;
107                 this.upper_left = upper_left;
108                 this.cache = listener.cache();
109
110                 while (center.lon < -180.0)
111                         center.lon += 360.0;
112                 while (center.lon > 180.0)
113                         center.lon -= 360.0;
114
115                 this.center = center;
116                 this.zoom = zoom;
117                 this.maptype = maptype;
118                 this.px_size = px_size;
119                 this.scale = scale;
120
121                 status = AltosMapTile.loading;
122                 store = AltosMapStore.get(map_url(), map_file());
123         }
124
125         public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
126                 this(listener, upper_left, center, zoom, maptype, px_size, 1);
127         }
128 }