Ignore:
Timestamp:
2016-07-08T19:58:07+02:00 (8 years ago)
Author:
simon04
Message:

JOSM/wikipedia: use MediaWiki API to query by coordinates

File:
1 edited

Legend:

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

    r32114 r32606  
    6161    static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang, LatLon min, LatLon max) {
    6262        try {
    63             final String bbox = min.lon() + "," + min.lat() + "," + max.lon() + "," + max.lat();
    6463            // construct url
    65             final String url = "https://tools.wmflabs.org/wp-world/marks.php?"
    66                     + "bbox=" + bbox + "&LANG=" + wikipediaLang;
     64            final String url = "https://" + wikipediaLang + ".wikipedia.org/w/api.php"
     65                    + "?action=query"
     66                    + "&list=geosearch"
     67                    + "&format=xml"
     68                    + "&gslimit=500"
     69                    + "&gsbbox=" + max.lat() + "|" + min.lon() + "|" + min.lat() + "|" + max.lon();
    6770            // parse XML document
    68             final XPathExpression xpathPlacemark = X_PATH.compile("//Placemark");
    69             final XPathExpression xpathName = X_PATH.compile("name/text()");
    70             final XPathExpression xpathCoord = X_PATH.compile("Point/coordinates/text()");
    71             final XPathExpression xpathDescr = X_PATH.compile("description");
     71            final XPathExpression xpathPlacemark = X_PATH.compile("//gs");
     72            final XPathExpression xpathName = X_PATH.compile("@title");
     73            final XPathExpression xpathLat = X_PATH.compile("@lat");
     74            final XPathExpression xpathLon = X_PATH.compile("@lon");
    7275            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
    7376                Document doc = DOCUMENT_BUILDER.parse(in);
     
    7679                List<WikipediaEntry> entries = new ArrayList<>(nodes.getLength());
    7780                for (int i = 0; i < nodes.getLength(); i++) {
    78                     final String[] coord = xpathCoord.evaluate(nodes.item(i)).split(",");
    79                     if (coord.length <= 2) {
    80                         continue;
    81                     }
    82                     final String name = xpathName.evaluate(nodes.item(i));
    83                     final String descr = xpathDescr.evaluate(nodes.item(i));
    84                     entries.add(new WikipediaEntry(name, descr,
    85                             new LatLon(Double.parseDouble(coord[1]), Double.parseDouble(coord[0]))));
     81                    final Node node = nodes.item(i);
     82                    final String name = xpathName.evaluate(node);
     83                    entries.add(new WikipediaEntry(name, wikipediaLang, name, new LatLon((
     84                            (double) xpathLat.evaluate(node, XPathConstants.NUMBER)),
     85                            (double) xpathLon.evaluate(node, XPathConstants.NUMBER))
     86                    ));
    8687                }
    8788                return entries;
     
    369370        private Boolean wiwosmStatus;
    370371
    371         public WikipediaEntry(String name, String description, LatLon coordinate) {
    372             this.name = name;
    373             this.coordinate = coordinate;
    374 
    375             final WikipediaLangArticle wp = WikipediaLangArticle.parseFromUrl(getHrefFromDescription(description));
    376             if (wp == null) {
    377                 Main.warn("Could not extract Wikipedia tag from: " + getHrefFromDescription(description));
    378             }
    379             this.wikipediaLang = wp == null ? null : wp.lang;
    380             this.wikipediaArticle = wp == null ? null : wp.article;
    381         }
    382 
    383372        public WikipediaEntry(String name, String wikipediaLang, String wikipediaArticle) {
     373            this(name, wikipediaLang, wikipediaArticle, null);
     374        }
     375
     376        public WikipediaEntry(String name, String wikipediaLang, String wikipediaArticle, LatLon coordinate) {
    384377            this.name = name;
    385378            this.wikipediaLang = wikipediaLang;
    386379            this.wikipediaArticle = wikipediaArticle;
    387             this.coordinate = null;
    388         }
    389 
    390         protected final String getHrefFromDescription(final String description) {
    391             if (description == null) {
    392                 return null;
    393             }
    394             final Matcher m = Pattern.compile(".*href=\"(.+?)\".*").matcher(description);
    395             if (m.matches()) {
    396                 return m.group(1);
    397             } else {
    398                 Main.warn("Could not parse URL from: " + description);
    399                 return null;
    400             }
     380            this.coordinate = coordinate;
    401381        }
    402382
    403383        protected final Tag createWikipediaTag() {
    404384            return new Tag("wikipedia", wikipediaLang + ":" + wikipediaArticle);
    405         }
    406 
    407         private void updateWiwosmStatus() {
    408             try {
    409                 final String url = "https://tools.wmflabs.org/wiwosm/osmjson/getGeoJSON.php?action=check"
    410                         + "&lang=" + wikipediaLang
    411                         + "&article=" + Utils.encodeUrl(wikipediaArticle);
    412                 try (final Scanner scanner = new Scanner(
    413                         HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContentReader())) {
    414                     wiwosmStatus = scanner.hasNextInt() && scanner.nextInt() == 1;
    415                 }
    416             } catch (IOException ex) {
    417                 throw new RuntimeException(ex);
    418             }
    419385        }
    420386
Note: See TracChangeset for help on using the changeset viewer.