source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsLoader.java@ 17880

Last change on this file since 17880 was 17880, checked in by simon04, 3 years ago

see #8472 - Show geocoded images from Wikimedia Commons as GeoImageLayer

The icon wikimedia_commons.svg is taken from https://commons.wikimedia.org/wiki/File:Commons-logo.svg ("This image of simple geometry is ineligible for copyright and therefore in the public domain, because it consists entirely of information that is common property and contains no original authorship.")

File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import org.openstreetmap.josm.actions.JosmAction;
5import org.openstreetmap.josm.data.Bounds;
6import org.openstreetmap.josm.gui.MainApplication;
7import org.openstreetmap.josm.gui.PleaseWaitRunnable;
8import org.openstreetmap.josm.io.OsmTransferException;
9import org.openstreetmap.josm.tools.Logging;
10import org.openstreetmap.josm.tools.Mediawiki;
11import org.xml.sax.SAXException;
12
13import javax.xml.parsers.ParserConfigurationException;
14import javax.xml.xpath.XPathExpressionException;
15import java.awt.event.ActionEvent;
16import java.io.IOException;
17import java.util.ArrayList;
18import java.util.List;
19
20import static org.openstreetmap.josm.tools.I18n.tr;
21
22/**
23 * Loads geocoded images from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a> for the given bounding box.
24 */
25public class WikimediaCommonsLoader extends PleaseWaitRunnable {
26 protected String apiUrl = "https://commons.wikimedia.org/w/api.php";
27 protected GeoImageLayer layer;
28 private final Bounds bounds;
29
30 /**
31 * Constructs a new {@code WikimediaCommonsLoader}
32 * @param bounds The bounds to load
33 */
34 public WikimediaCommonsLoader(Bounds bounds) {
35 super(tr("Load images from Wikimedia Commons"));
36 this.bounds = bounds;
37 }
38
39 @Override
40 protected void realRun() throws SAXException, IOException, OsmTransferException {
41 List<ImageEntry> imageEntries = new ArrayList<>();
42 try {
43 new Mediawiki(apiUrl).searchGeoImages(bounds, (title, latLon) -> imageEntries.add(new WikimediaCommonsEntry(title, latLon)));
44 } catch (ParserConfigurationException | XPathExpressionException e) {
45 throw new IllegalStateException(e);
46 }
47 Logging.info("Loaded {0} images from Wikimedia Commons", imageEntries.size());
48 layer = new WikimediaCommonsLayer(imageEntries);
49 }
50
51 @Override
52 protected void finish() {
53 MainApplication.getLayerManager().addLayer(layer);
54 }
55
56 @Override
57 protected void cancel() {
58 // do nothing
59 }
60
61 public static class Action extends JosmAction {
62 public Action() {
63 super(tr("Load images from Wikimedia Commons"), "wikimedia_commons", null, null, false);
64 }
65
66 @Override
67 public void actionPerformed(ActionEvent e) {
68 Bounds bounds = MainApplication.getMap().mapView.getRealBounds();
69 MainApplication.worker.execute(new WikimediaCommonsLoader(bounds));
70 }
71
72 @Override
73 protected void updateEnabledState() {
74 setEnabled(MainApplication.isDisplayingMapView());
75 }
76 }
77
78}
Note: See TracBrowser for help on using the repository browser.