| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.io.IOException;
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 |
|
|---|
| 11 | import javax.xml.parsers.ParserConfigurationException;
|
|---|
| 12 | import javax.xml.xpath.XPathExpressionException;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 15 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 16 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 18 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
|---|
| 21 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
|---|
| 22 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 23 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 24 | import org.openstreetmap.josm.tools.Mediawiki;
|
|---|
| 25 | import org.xml.sax.SAXException;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Loads geocoded images from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a> for the given bounding box.
|
|---|
| 29 | */
|
|---|
| 30 | public class WikimediaCommonsLoader extends PleaseWaitRunnable {
|
|---|
| 31 | protected String apiUrl = "https://commons.wikimedia.org/w/api.php";
|
|---|
| 32 | protected GeoImageLayer layer;
|
|---|
| 33 | private final Bounds bounds;
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Constructs a new {@code WikimediaCommonsLoader}
|
|---|
| 37 | * @param bounds The bounds to load
|
|---|
| 38 | */
|
|---|
| 39 | public WikimediaCommonsLoader(Bounds bounds) {
|
|---|
| 40 | super(tr("Load images from Wikimedia Commons"));
|
|---|
| 41 | this.bounds = bounds;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | @Override
|
|---|
| 45 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
|---|
| 46 | List<ImageEntry> imageEntries = new ArrayList<>();
|
|---|
| 47 | try {
|
|---|
| 48 | new Mediawiki(apiUrl).searchGeoImages(bounds, (title, latLon) -> imageEntries.add(new WikimediaCommonsEntry(title, latLon)));
|
|---|
| 49 | } catch (ParserConfigurationException | XPathExpressionException e) {
|
|---|
| 50 | throw new IllegalStateException(e);
|
|---|
| 51 | }
|
|---|
| 52 | Logging.info("Loaded {0} images from Wikimedia Commons", imageEntries.size());
|
|---|
| 53 | layer = new WikimediaCommonsLayer(imageEntries);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | @Override
|
|---|
| 57 | protected void finish() {
|
|---|
| 58 | if (layer != null) {
|
|---|
| 59 | MainApplication.getLayerManager().addLayer(layer);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | @Override
|
|---|
| 64 | protected void cancel() {
|
|---|
| 65 | // do nothing
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Load images from Wikimedia Commons
|
|---|
| 70 | * @since 18021
|
|---|
| 71 | */
|
|---|
| 72 | public static class WikimediaCommonsLoadImagesAction extends JosmAction implements LayerChangeListener {
|
|---|
| 73 | /**
|
|---|
| 74 | * Constructs a new {@code WikimediaCommonsLoadImagesAction}
|
|---|
| 75 | */
|
|---|
| 76 | public WikimediaCommonsLoadImagesAction() {
|
|---|
| 77 | super(tr("Load images from Wikimedia Commons"), "wikimedia_commons", null, null, false, false);
|
|---|
| 78 | MainApplication.getLayerManager().addLayerChangeListener(this);
|
|---|
| 79 | initEnabledState();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | @Override
|
|---|
| 83 | public void actionPerformed(ActionEvent e) {
|
|---|
| 84 | Bounds bounds = MainApplication.getMap().mapView.getRealBounds();
|
|---|
| 85 | MainApplication.worker.execute(new WikimediaCommonsLoader(bounds));
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | protected void updateEnabledState() {
|
|---|
| 90 | setEnabled(MainApplication.isDisplayingMapView());
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | @Override
|
|---|
| 94 | public void layerAdded(LayerAddEvent e) {
|
|---|
| 95 | updateEnabledState();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | @Override
|
|---|
| 99 | public void layerRemoving(LayerRemoveEvent e) {
|
|---|
| 100 | if (e.isLastLayer())
|
|---|
| 101 | setEnabled(false);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | @Override
|
|---|
| 105 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
|---|
| 106 | // not used
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | @Override
|
|---|
| 110 | public void destroy() {
|
|---|
| 111 | MainApplication.getLayerManager().removeLayerChangeListener(this);
|
|---|
| 112 | super.destroy();
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|