]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/OsmMapSource.java
Version 10, May 2010
[GpsPrune.git] / tim / prune / gui / map / OsmMapSource.java
1 package tim.prune.gui.map;
2
3 import tim.prune.I18nManager;
4
5 /**
6  * Class to provide a map source for all OSM-like sources
7  * (eg mapnik, opencyclemap, openpistemap etc).
8  * These can be single-layer or double-layer sources with png tiles
9  */
10 public class OsmMapSource extends MapSource
11 {
12         /** Name for this source */
13         private String _name = null;
14         /** Base urls */
15         private String[] _baseUrls = null;
16         /** Site names */
17         private String[] _siteNames = null;
18         /** Maximum zoom level */
19         private int _maxZoom = 0;
20
21         /**
22          * Constructor giving single name and url
23          * @param inName source name
24          * @param inUrl base url
25          */
26         public OsmMapSource(String inName, String inUrl)
27         {
28                 this(inName, inUrl, null, 18);
29         }
30
31         /**
32          * Constructor giving name, urls and maximum zoom
33          * @param inName source name
34          * @param inUrl1 base layer url
35          * @param inUrl2 upper layer url
36          * @param inMaxZoom maximum zoom level
37          */
38         public OsmMapSource(String inName, String inUrl1, String inUrl2, int inMaxZoom)
39         {
40                 _name = inName;
41                 if (_name == null || _name.trim().equals("")) {_name = I18nManager.getText("mapsource.unknown");}
42                 _baseUrls = new String[2];
43                 _baseUrls[0] = fixBaseUrl(inUrl1);
44                 _baseUrls[1] = fixBaseUrl(inUrl2);
45                 _siteNames = new String[2];
46                 _siteNames[0] = fixSiteName(_baseUrls[0]);
47                 _siteNames[1] = fixSiteName(_baseUrls[1]);
48                 // Swap layers if second layer given without first
49                 if (_baseUrls[0] == null && _baseUrls[1] != null)
50                 {
51                         _baseUrls[0] = _baseUrls[1];
52                         _siteNames[0] = _siteNames[1];
53                         _baseUrls[1] = _siteNames[1] = null;
54                 }
55                 _maxZoom = inMaxZoom;
56         }
57
58         /**
59          * Construct a new map source from its config string
60          * @param inConfigString string from Config, separated by semicolons
61          * @return new map source, or null if not parseable
62          */
63         public static OsmMapSource fromConfig(String inConfigString)
64         {
65                 OsmMapSource source = null;
66                 if (inConfigString.startsWith("o:"))
67                 {
68                         String[] items = inConfigString.substring(2).split(";");
69                         try {
70                                 if (items.length == 3) {
71                                         source = new OsmMapSource(items[0], items[1], null, Integer.parseInt(items[2]));
72                                 }
73                                 else if (items.length == 4) {
74                                         source = new OsmMapSource(items[0], items[1], items[2], Integer.parseInt(items[3]));
75                                 }
76                         } catch (NumberFormatException nfe) {}
77                 }
78                 return source;
79         }
80
81         /**
82          * @return name
83          */
84         public String getName() {
85                 return _name;
86         }
87
88         /** Number of layers */
89         public int getNumLayers() {
90                 return _baseUrls[1] == null?1:2;
91         }
92
93         /** Base url for this source */
94         public String getBaseUrl(int inLayerNum) {
95                 return _baseUrls[inLayerNum];
96         }
97
98         /** site name without protocol or www. */
99         public String getSiteName(int inLayerNum) {
100                 return _siteNames[inLayerNum];
101         }
102
103         /**
104          * Make the URL to get the specified tile
105          */
106         public String makeURL(int inLayerNum, int inZoom, int inX, int inY)
107         {
108                 return _baseUrls[inLayerNum] + inZoom + "/" + inX + "/" + inY + getFileExtension(inLayerNum);
109         }
110
111         /** file extension is always png */
112         public final String getFileExtension(int inLayerNum) {
113                 return ".png";
114         }
115
116         /**
117          * @return maximum zoom level
118          */
119         public final int getMaxZoomLevel()
120         {
121                 return _maxZoom;
122         }
123
124
125         /**
126          * @return semicolon-separated list of all fields
127          */
128         public String getConfigString()
129         {
130                 return "o:" +  getName() + ";" + getSiteStrings() + getMaxZoomLevel();
131         }
132 }