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