1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.bbox;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.Point;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Set;
|
---|
19 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
20 |
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.SpringLayout;
|
---|
23 |
|
---|
24 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
25 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
26 | import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
|
---|
27 | import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
|
---|
28 | import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
|
---|
29 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
30 | import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
|
---|
31 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
32 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
33 | import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.data.Bounds;
|
---|
36 | import org.openstreetmap.josm.data.Version;
|
---|
37 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
38 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
39 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
40 | import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
|
---|
41 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
42 | import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
|
---|
43 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
44 |
|
---|
45 | public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser {
|
---|
46 |
|
---|
47 | @FunctionalInterface
|
---|
48 | public interface TileSourceProvider {
|
---|
49 | List<TileSource> getTileSources();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * TMS TileSource provider for the slippymap chooser
|
---|
54 | */
|
---|
55 | public static class TMSTileSourceProvider implements TileSourceProvider {
|
---|
56 | private static final Set<String> existingSlippyMapUrls = new HashSet<>();
|
---|
57 | static {
|
---|
58 | // Urls that already exist in the slippymap chooser and shouldn't be copied from TMS layer list
|
---|
59 | existingSlippyMapUrls.add("https://{switch:a,b,c}.tile.openstreetmap.org/{zoom}/{x}/{y}.png"); // Mapnik
|
---|
60 | existingSlippyMapUrls.add("http://tile.opencyclemap.org/cycle/{zoom}/{x}/{y}.png"); // Cyclemap
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public List<TileSource> getTileSources() {
|
---|
65 | if (!TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get()) return Collections.<TileSource>emptyList();
|
---|
66 | List<TileSource> sources = new ArrayList<>();
|
---|
67 | for (ImageryInfo info : ImageryLayerInfo.instance.getLayers()) {
|
---|
68 | if (existingSlippyMapUrls.contains(info.getUrl())) {
|
---|
69 | continue;
|
---|
70 | }
|
---|
71 | try {
|
---|
72 | TileSource source = TMSLayer.getTileSourceStatic(info);
|
---|
73 | if (source != null) {
|
---|
74 | sources.add(source);
|
---|
75 | }
|
---|
76 | } catch (IllegalArgumentException ex) {
|
---|
77 | if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
|
---|
78 | JOptionPane.showMessageDialog(Main.parent,
|
---|
79 | ex.getMessage(), tr("Warning"),
|
---|
80 | JOptionPane.WARNING_MESSAGE);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return sources;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Plugins that wish to add custom tile sources to slippy map choose should call this method
|
---|
90 | * @param tileSourceProvider new tile source provider
|
---|
91 | */
|
---|
92 | public static void addTileSourceProvider(TileSourceProvider tileSourceProvider) {
|
---|
93 | providers.addIfAbsent(tileSourceProvider);
|
---|
94 | }
|
---|
95 |
|
---|
96 | private static CopyOnWriteArrayList<TileSourceProvider> providers = new CopyOnWriteArrayList<>();
|
---|
97 | static {
|
---|
98 | addTileSourceProvider(() -> Arrays.<TileSource>asList(
|
---|
99 | new OsmTileSource.Mapnik(),
|
---|
100 | new OsmTileSource.CycleMap()));
|
---|
101 | addTileSourceProvider(new TMSTileSourceProvider());
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static final StringProperty PROP_MAPSTYLE = new StringProperty("slippy_map_chooser.mapstyle", "Mapnik");
|
---|
105 | public static final String RESIZE_PROP = SlippyMapBBoxChooser.class.getName() + ".resize";
|
---|
106 |
|
---|
107 | private final transient TileLoader cachedLoader;
|
---|
108 | private final transient OsmTileLoader uncachedLoader;
|
---|
109 |
|
---|
110 | private final SizeButton iSizeButton;
|
---|
111 | private final SourceButton iSourceButton;
|
---|
112 | private transient Bounds bbox;
|
---|
113 |
|
---|
114 | // upper left and lower right corners of the selection rectangle (x/y on ZOOM_MAX)
|
---|
115 | private transient ICoordinate iSelectionRectStart;
|
---|
116 | private transient ICoordinate iSelectionRectEnd;
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Constructs a new {@code SlippyMapBBoxChooser}.
|
---|
120 | */
|
---|
121 | public SlippyMapBBoxChooser() {
|
---|
122 | debug = Main.isDebugEnabled();
|
---|
123 | SpringLayout springLayout = new SpringLayout();
|
---|
124 | setLayout(springLayout);
|
---|
125 |
|
---|
126 | Map<String, String> headers = new HashMap<>();
|
---|
127 | headers.put("User-Agent", Version.getInstance().getFullAgentString());
|
---|
128 |
|
---|
129 | cachedLoader = AbstractCachedTileSourceLayer.getTileLoaderFactory("TMS", TMSCachedTileLoader.class).makeTileLoader(this, headers);
|
---|
130 |
|
---|
131 | uncachedLoader = new OsmTileLoader(this);
|
---|
132 | uncachedLoader.headers.putAll(headers);
|
---|
133 | setZoomContolsVisible(Main.pref.getBoolean("slippy_map_chooser.zoomcontrols", false));
|
---|
134 | setMapMarkerVisible(false);
|
---|
135 | setMinimumSize(new Dimension(350, 350 / 2));
|
---|
136 | // We need to set an initial size - this prevents a wrong zoom selection
|
---|
137 | // for the area before the component has been displayed the first time
|
---|
138 | setBounds(new Rectangle(getMinimumSize()));
|
---|
139 | if (cachedLoader == null) {
|
---|
140 | setFileCacheEnabled(false);
|
---|
141 | } else {
|
---|
142 | setFileCacheEnabled(Main.pref.getBoolean("slippy_map_chooser.file_cache", true));
|
---|
143 | }
|
---|
144 | setMaxTilesInMemory(Main.pref.getInteger("slippy_map_chooser.max_tiles", 1000));
|
---|
145 |
|
---|
146 | List<TileSource> tileSources = getAllTileSources();
|
---|
147 |
|
---|
148 | iSourceButton = new SourceButton(this, tileSources);
|
---|
149 | add(iSourceButton);
|
---|
150 | springLayout.putConstraint(SpringLayout.EAST, iSourceButton, 0, SpringLayout.EAST, this);
|
---|
151 | springLayout.putConstraint(SpringLayout.NORTH, iSourceButton, 30, SpringLayout.NORTH, this);
|
---|
152 |
|
---|
153 | iSizeButton = new SizeButton(this);
|
---|
154 | add(iSizeButton);
|
---|
155 |
|
---|
156 | String mapStyle = PROP_MAPSTYLE.get();
|
---|
157 | boolean foundSource = false;
|
---|
158 | for (TileSource source: tileSources) {
|
---|
159 | if (source.getName().equals(mapStyle)) {
|
---|
160 | this.setTileSource(source);
|
---|
161 | iSourceButton.setCurrentMap(source);
|
---|
162 | foundSource = true;
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | if (!foundSource) {
|
---|
167 | setTileSource(tileSources.get(0));
|
---|
168 | iSourceButton.setCurrentMap(tileSources.get(0));
|
---|
169 | }
|
---|
170 |
|
---|
171 | new SlippyMapControler(this, this);
|
---|
172 | }
|
---|
173 |
|
---|
174 | private List<TileSource> getAllTileSources() {
|
---|
175 | List<TileSource> tileSources = new ArrayList<>();
|
---|
176 | for (TileSourceProvider provider: providers) {
|
---|
177 | tileSources.addAll(provider.getTileSources());
|
---|
178 | }
|
---|
179 | return tileSources;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public boolean handleAttribution(Point p, boolean click) {
|
---|
183 | return attribution.handleAttribution(p, click);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Draw the map.
|
---|
188 | */
|
---|
189 | @Override
|
---|
190 | public void paint(Graphics g) {
|
---|
191 | super.paint(g);
|
---|
192 |
|
---|
193 | // draw selection rectangle
|
---|
194 | if (iSelectionRectStart != null && iSelectionRectEnd != null) {
|
---|
195 | Rectangle box = new Rectangle(getMapPosition(iSelectionRectStart, false));
|
---|
196 | box.add(getMapPosition(iSelectionRectEnd, false));
|
---|
197 |
|
---|
198 | g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
|
---|
199 | g.fillRect(box.x, box.y, box.width, box.height);
|
---|
200 |
|
---|
201 | g.setColor(Color.BLACK);
|
---|
202 | g.drawRect(box.x, box.y, box.width, box.height);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | public final void setFileCacheEnabled(boolean enabled) {
|
---|
207 | if (enabled) {
|
---|
208 | setTileLoader(cachedLoader);
|
---|
209 | } else {
|
---|
210 | setTileLoader(uncachedLoader);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | public final void setMaxTilesInMemory(int tiles) {
|
---|
215 | ((MemoryTileCache) getTileCache()).setCacheSize(tiles);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Callback for the OsmMapControl. (Re-)Sets the start and end point of the selection rectangle.
|
---|
220 | *
|
---|
221 | * @param aStart selection start
|
---|
222 | * @param aEnd selection end
|
---|
223 | */
|
---|
224 | public void setSelection(Point aStart, Point aEnd) {
|
---|
225 | if (aStart == null || aEnd == null || aStart.x == aEnd.x || aStart.y == aEnd.y)
|
---|
226 | return;
|
---|
227 |
|
---|
228 | Point pMax = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
|
---|
229 | Point pMin = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
|
---|
230 |
|
---|
231 | iSelectionRectStart = getPosition(pMin);
|
---|
232 | iSelectionRectEnd = getPosition(pMax);
|
---|
233 |
|
---|
234 | Bounds b = new Bounds(
|
---|
235 | new LatLon(
|
---|
236 | Math.min(iSelectionRectStart.getLat(), iSelectionRectEnd.getLat()),
|
---|
237 | LatLon.toIntervalLon(Math.min(iSelectionRectStart.getLon(), iSelectionRectEnd.getLon()))
|
---|
238 | ),
|
---|
239 | new LatLon(
|
---|
240 | Math.max(iSelectionRectStart.getLat(), iSelectionRectEnd.getLat()),
|
---|
241 | LatLon.toIntervalLon(Math.max(iSelectionRectStart.getLon(), iSelectionRectEnd.getLon())))
|
---|
242 | );
|
---|
243 | Bounds oldValue = this.bbox;
|
---|
244 | this.bbox = b;
|
---|
245 | repaint();
|
---|
246 | firePropertyChange(BBOX_PROP, oldValue, this.bbox);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Performs resizing of the DownloadDialog in order to enlarge or shrink the
|
---|
251 | * map.
|
---|
252 | */
|
---|
253 | public void resizeSlippyMap() {
|
---|
254 | boolean large = iSizeButton.isEnlarged();
|
---|
255 | firePropertyChange(RESIZE_PROP, !large, large);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public void toggleMapSource(TileSource tileSource) {
|
---|
259 | this.tileController.setTileCache(new MemoryTileCache());
|
---|
260 | this.setTileSource(tileSource);
|
---|
261 | PROP_MAPSTYLE.put(tileSource.getName()); // TODO Is name really unique?
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public Bounds getBoundingBox() {
|
---|
266 | return bbox;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Sets the current bounding box in this bbox chooser without
|
---|
271 | * emiting a property change event.
|
---|
272 | *
|
---|
273 | * @param bbox the bounding box. null to reset the bounding box
|
---|
274 | */
|
---|
275 | @Override
|
---|
276 | public void setBoundingBox(Bounds bbox) {
|
---|
277 | if (bbox == null || (bbox.getMinLat() == 0 && bbox.getMinLon() == 0
|
---|
278 | && bbox.getMaxLat() == 0 && bbox.getMaxLon() == 0)) {
|
---|
279 | this.bbox = null;
|
---|
280 | iSelectionRectStart = null;
|
---|
281 | iSelectionRectEnd = null;
|
---|
282 | repaint();
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | this.bbox = bbox;
|
---|
287 | iSelectionRectStart = new Coordinate(bbox.getMinLat(), bbox.getMinLon());
|
---|
288 | iSelectionRectEnd = new Coordinate(bbox.getMaxLat(), bbox.getMaxLon());
|
---|
289 |
|
---|
290 | // calc the screen coordinates for the new selection rectangle
|
---|
291 | MapMarkerDot min = new MapMarkerDot(bbox.getMinLat(), bbox.getMinLon());
|
---|
292 | MapMarkerDot max = new MapMarkerDot(bbox.getMaxLat(), bbox.getMaxLon());
|
---|
293 |
|
---|
294 | List<MapMarker> marker = new ArrayList<>(2);
|
---|
295 | marker.add(min);
|
---|
296 | marker.add(max);
|
---|
297 | setMapMarkerList(marker);
|
---|
298 | setDisplayToFitMapMarkers();
|
---|
299 | zoomOut();
|
---|
300 | repaint();
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Enables or disables painting of the shrink/enlarge button
|
---|
305 | *
|
---|
306 | * @param visible {@code true} to enable painting of the shrink/enlarge button
|
---|
307 | */
|
---|
308 | public void setSizeButtonVisible(boolean visible) {
|
---|
309 | iSizeButton.setVisible(visible);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Refreshes the tile sources
|
---|
314 | * @since 6364
|
---|
315 | */
|
---|
316 | public final void refreshTileSources() {
|
---|
317 | iSourceButton.setSources(getAllTileSources());
|
---|
318 | }
|
---|
319 | }
|
---|