Changeset 32606 in osm
- Timestamp:
- 2016-07-08T19:58:07+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32114 r32606 61 61 static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang, LatLon min, LatLon max) { 62 62 try { 63 final String bbox = min.lon() + "," + min.lat() + "," + max.lon() + "," + max.lat();64 63 // 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(); 67 70 // parse XML document 68 final XPathExpression xpathPlacemark = X_PATH.compile("// Placemark");69 final XPathExpression xpathName = X_PATH.compile(" name/text()");70 final XPathExpression xpath Coord= X_PATH.compile("Point/coordinates/text()");71 final XPathExpression xpath Descr= 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"); 72 75 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 73 76 Document doc = DOCUMENT_BUILDER.parse(in); … … 76 79 List<WikipediaEntry> entries = new ArrayList<>(nodes.getLength()); 77 80 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 )); 86 87 } 87 88 return entries; … … 369 370 private Boolean wiwosmStatus; 370 371 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 383 372 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) { 384 377 this.name = name; 385 378 this.wikipediaLang = wikipediaLang; 386 379 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; 401 381 } 402 382 403 383 protected final Tag createWikipediaTag() { 404 384 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=" + wikipediaLang411 + "&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 }419 385 } 420 386
Note:
See TracChangeset
for help on using the changeset viewer.