Ignore:
Timestamp:
2016-03-22T22:54:50+01:00 (8 years ago)
Author:
simon04
Message:

JOSM/wikipedia - Zoom to selected Wikipedia article - see #josm12664

Location:
applications/editors/josm/plugins/wikipedia
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r32056 r32114  
    272272    }
    273273
     274    static LatLon getCoordinateForArticle(String wikipediaLang, String article) {
     275        try {
     276            final String url = "https://" + wikipediaLang + ".wikipedia.org/w/api.php" +
     277                    "?action=query" +
     278                    "&prop=coordinates" +
     279                    "&titles=" + Utils.encodeUrl(article) +
     280                    "&format=xml";
     281            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     282                final Document xml = DOCUMENT_BUILDER.parse(in);
     283                final Node node = (Node) X_PATH.compile("//coordinates/co").evaluate(xml, XPathConstants.NODE);
     284                if (node == null) {
     285                    return null;
     286                } else {
     287                    final double lat = Double.parseDouble(node.getAttributes().getNamedItem("lat").getTextContent());
     288                    final double lon = Double.parseDouble(node.getAttributes().getNamedItem("lon").getTextContent());
     289                    return new LatLon(lat, lon);
     290                }
     291            }
     292        } catch (Exception ex) {
     293            throw new RuntimeException(ex);
     294        }
     295    }
     296
    274297    static class WikipediaLangArticle {
    275298
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java

    r32058 r32114  
    2020import javax.swing.JList;
    2121import javax.swing.JOptionPane;
     22import javax.swing.JPopupMenu;
    2223import javax.swing.SwingWorker;
    2324
     
    5354                new SideButton(new PasteWikipediaArticlesAction()),
    5455                new SideButton(new AddWikipediaTagAction()),
    55                 new SideButton(new OpenWikipediaArticleAction()),
    5656                new SideButton(new WikipediaSettingsAction(), false)));
    5757        updateTitle();
     
    100100                }
    101101            });
     102
     103            final JPopupMenu popupMenu = new JPopupMenu();
     104            popupMenu.add(new OpenWikipediaArticleAction());
     105            popupMenu.add(new ZoomToWikipediaArticleAction());
     106            setComponentPopupMenu(popupMenu);
    102107        }
    103108    };
     
    282287    }
    283288
     289    class ZoomToWikipediaArticleAction extends AbstractAction {
     290
     291        ZoomToWikipediaArticleAction() {
     292            super(tr("Zoom to selection"), ImageProvider.get("dialogs/autoscale", "selection"));
     293            putValue(SHORT_DESCRIPTION, tr("Zoom to selection"));
     294        }
     295
     296        @Override
     297        public void actionPerformed(ActionEvent e) {
     298            final WikipediaEntry entry = list.getSelectedValue();
     299            if (entry == null) {
     300                return;
     301            }
     302            final LatLon latLon = WikipediaApp.getCoordinateForArticle(entry.wikipediaLang, entry.wikipediaArticle);
     303            if (latLon == null) {
     304                return;
     305            }
     306            Main.map.mapView.zoomTo(latLon);
     307        }
     308    }
     309
    284310    protected void updateWikipediaArticles() {
    285311        articles.clear();
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r32003 r32114  
    125125
    126126    @Test
     127    public void testGetCoordinates() throws Exception {
     128        assertThat(WikipediaApp.getCoordinateForArticle("de", "Marchreisenspitze"), is(new LatLon(47.1725, 11.30833333)));
     129        assertThat(WikipediaApp.getCoordinateForArticle("en", "Austria"), is(new LatLon(47.33333333, 13.33333333)));
     130        assertThat(WikipediaApp.getCoordinateForArticle("en", "Foobar2000"), nullValue());
     131    }
     132
     133    @Test
    127134    public void testGetBrowserUrl() {
    128135        final WikipediaEntry entry = new WikipediaEntry("Sternheim & Emanuel", "de", "Sternheim & Emanuel");
Note: See TracChangeset for help on using the changeset viewer.