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

Last change on this file since 18036 was 18036, checked in by Don-vip, 3 years ago

fix #21120 - fix NPE at WikimediaCommonsLoader.finish

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