1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
5 | import org.openstreetmap.josm.data.Bounds;
|
---|
6 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
7 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
8 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
9 | import org.openstreetmap.josm.tools.Logging;
|
---|
10 | import org.openstreetmap.josm.tools.Mediawiki;
|
---|
11 | import org.xml.sax.SAXException;
|
---|
12 |
|
---|
13 | import javax.xml.parsers.ParserConfigurationException;
|
---|
14 | import javax.xml.xpath.XPathExpressionException;
|
---|
15 | import java.awt.event.ActionEvent;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import 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 | */
|
---|
25 | public 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 | }
|
---|