1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.io.UnsupportedEncodingException;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
19 | import org.openstreetmap.josm.data.Bounds;
|
---|
20 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
21 |
|
---|
22 | public class ImageryLayerInfo {
|
---|
23 |
|
---|
24 | public static final ImageryLayerInfo instance = new ImageryLayerInfo();
|
---|
25 | ArrayList<ImageryInfo> layers = new ArrayList<ImageryInfo>();
|
---|
26 | static ArrayList<ImageryInfo> defaultLayers = new ArrayList<ImageryInfo>();
|
---|
27 |
|
---|
28 | private final static String[] DEFAULT_LAYER_SITES = {
|
---|
29 | "http://josm.openstreetmap.de/maps"
|
---|
30 | };
|
---|
31 |
|
---|
32 | private ImageryLayerInfo() {
|
---|
33 | }
|
---|
34 |
|
---|
35 | public ImageryLayerInfo(ImageryLayerInfo info) {
|
---|
36 | layers.addAll(info.layers);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void load() {
|
---|
40 | layers.clear();
|
---|
41 | for(Collection<String> c : Main.pref.getArray("imagery.layers",
|
---|
42 | Collections.<Collection<String>>emptySet())) {
|
---|
43 | ImageryInfo i = new ImageryInfo(c);
|
---|
44 | String url = i.getUrl();
|
---|
45 | if(url != null) {
|
---|
46 | /* FIXME: Remove the attribution copy stuff end of 2011 */
|
---|
47 | if(!i.hasAttribution()) {
|
---|
48 | for(ImageryInfo d : defaultLayers) {
|
---|
49 | if(url.equals(d.getUrl())) {
|
---|
50 | i.copyAttribution(d);
|
---|
51 | i.setBounds(d.getBounds());
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | add(i);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | Collections.sort(layers);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void loadDefaults(boolean clearCache) {
|
---|
63 | defaultLayers.clear();
|
---|
64 | Collection<String> defaults = Main.pref.getCollection(
|
---|
65 | "imagery.layers.default", Collections.<String>emptySet());
|
---|
66 | ArrayList<String> defaultsSave = new ArrayList<String>();
|
---|
67 | for(String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES)))
|
---|
68 | {
|
---|
69 | try
|
---|
70 | {
|
---|
71 | if (clearCache) {
|
---|
72 | MirroredInputStream.cleanup(source);
|
---|
73 | }
|
---|
74 | MirroredInputStream s = new MirroredInputStream(source, -1);
|
---|
75 | try {
|
---|
76 | InputStreamReader r;
|
---|
77 | try
|
---|
78 | {
|
---|
79 | r = new InputStreamReader(s, "UTF-8");
|
---|
80 | }
|
---|
81 | catch (UnsupportedEncodingException e)
|
---|
82 | {
|
---|
83 | r = new InputStreamReader(s);
|
---|
84 | }
|
---|
85 | BufferedReader reader = new BufferedReader(r);
|
---|
86 | String line;
|
---|
87 | while((line = reader.readLine()) != null)
|
---|
88 | {
|
---|
89 | String val[] = line.split(";");
|
---|
90 | if(!line.startsWith("#") && val.length >= 3) {
|
---|
91 | boolean force = "true".equals(val[0]);
|
---|
92 | String name = tr(val[1]);
|
---|
93 | String url = val[2];
|
---|
94 | String eulaAcceptanceRequired = null;
|
---|
95 |
|
---|
96 | if (val.length >= 4 && !val[3].isEmpty()) {
|
---|
97 | // 4th parameter optional for license agreement (EULA)
|
---|
98 | eulaAcceptanceRequired = val[3];
|
---|
99 | }
|
---|
100 |
|
---|
101 | ImageryInfo info = new ImageryInfo(name, url, eulaAcceptanceRequired);
|
---|
102 |
|
---|
103 | if (val.length >= 5 && !val[4].isEmpty()) {
|
---|
104 | // 5th parameter optional for bounds
|
---|
105 | try {
|
---|
106 | info.setBounds(new Bounds(val[4], ","));
|
---|
107 | } catch (IllegalArgumentException e) {
|
---|
108 | Main.warn(e.toString());
|
---|
109 | }
|
---|
110 | }
|
---|
111 | if (val.length >= 6 && !val[5].isEmpty()) {
|
---|
112 | info.setAttributionText(val[5]);
|
---|
113 | }
|
---|
114 | if (val.length >= 7 && !val[6].isEmpty()) {
|
---|
115 | info.setAttributionLinkURL(val[6]);
|
---|
116 | }
|
---|
117 | if (val.length >= 8 && !val[7].isEmpty()) {
|
---|
118 | info.setTermsOfUseURL(val[7]);
|
---|
119 | }
|
---|
120 | if (val.length >= 9 && !val[8].isEmpty()) {
|
---|
121 | info.setAttributionImage(val[8]);
|
---|
122 | }
|
---|
123 |
|
---|
124 | defaultLayers.add(info);
|
---|
125 |
|
---|
126 | if (force) {
|
---|
127 | defaultsSave.add(url);
|
---|
128 | if (!defaults.contains(url)) {
|
---|
129 | for (ImageryInfo i : layers) {
|
---|
130 | if ((i.getImageryType() == ImageryType.WMS && url.equals(i.getUrl()))
|
---|
131 | || url.equals(i.getFullUrl())) {
|
---|
132 | force = false;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | if (force) {
|
---|
136 | add(new ImageryInfo(name, url));
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | } finally {
|
---|
143 | s.close();
|
---|
144 | }
|
---|
145 | }
|
---|
146 | catch (IOException e)
|
---|
147 | {
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | Collections.sort(defaultLayers);
|
---|
152 | Main.pref.putCollection("imagery.layers.default", defaultsSave.size() > 0
|
---|
153 | ? defaultsSave : defaults);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void add(ImageryInfo info) {
|
---|
157 | layers.add(info);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void remove(ImageryInfo info) {
|
---|
161 | layers.remove(info);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void save() {
|
---|
165 | LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
|
---|
166 | for (ImageryInfo info : layers) {
|
---|
167 | coll.add(info.getInfoArray());
|
---|
168 | }
|
---|
169 | Main.pref.putArray("imagery.layers", coll);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public List<ImageryInfo> getLayers() {
|
---|
173 | return Collections.unmodifiableList(layers);
|
---|
174 | }
|
---|
175 |
|
---|
176 | public List<ImageryInfo> getDefaultLayers() {
|
---|
177 | return Collections.unmodifiableList(defaultLayers);
|
---|
178 | }
|
---|
179 |
|
---|
180 | public static void addLayer(ImageryInfo info) {
|
---|
181 | instance.add(info);
|
---|
182 | instance.save();
|
---|
183 | Main.main.menu.imageryMenu.refreshImageryMenu();
|
---|
184 | }
|
---|
185 | }
|
---|