X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2FGetWikipediaFunction.java;h=3ffdd06817d87fe984b6063c7e7a63aa3e1ab4d9;hb=a6197ddcaac11c0b943183da7d46169742d024af;hp=acef09c6a15d3ba2a396df9b6733c781f97b856b;hpb=f35b6d628f68e3b5ef19965ad8988d0dd1eb8efa;p=GpsPrune.git diff --git a/tim/prune/function/GetWikipediaFunction.java b/tim/prune/function/GetWikipediaFunction.java index acef09c..3ffdd06 100644 --- a/tim/prune/function/GetWikipediaFunction.java +++ b/tim/prune/function/GetWikipediaFunction.java @@ -26,6 +26,8 @@ public class GetWikipediaFunction extends GenericDownloaderFunction private static final int MAX_RESULTS = 20; /** Maximum distance from point in km */ private static final int MAX_DISTANCE = 15; + /** Username to use for geonames queries */ + private static final String GEONAMES_USERNAME = "gpsprune"; /** @@ -69,20 +71,48 @@ public class GetWikipediaFunction extends GenericDownloaderFunction lat = (coords[0] + coords[2]) / 2.0; lon = (coords[1] + coords[3]) / 2.0; } - else { + else + { lat = point.getLatitude().getDouble(); lon = point.getLongitude().getDouble(); } - String descMessage = ""; - InputStream inStream = null; + // Firstly try the local language + String lang = I18nManager.getText("wikipedia.lang"); + submitSearch(lat, lon, lang); + // If we didn't get anything, try a secondary language + if (_trackListModel.isEmpty() && _errorMessage == null && lang.equals("als")) { + submitSearch(lat, lon, "de"); + } + // If still nothing then try english + if (_trackListModel.isEmpty() && _errorMessage == null && !lang.equals("en")) { + submitSearch(lat, lon, "en"); + } - // Example http://ws.geonames.org/findNearbyWikipedia?lat=47&lng=9 - String urlString = "http://ws.geonames.org/findNearbyWikipedia?lat=" + - lat + "&lng=" + lon + "&maxRows=" + MAX_RESULTS - + "&radius=" + MAX_DISTANCE + "&lang=" + I18nManager.getText("wikipedia.lang"); + // Set status label according to error or "none found", leave blank if ok + if (_errorMessage == null && _trackListModel.isEmpty()) { + _errorMessage = I18nManager.getText("dialog.wikipedia.nonefound"); + } + _statusLabel.setText(_errorMessage == null ? "" : _errorMessage); + } + + /** + * Submit the search for the given parameters + * @param inLat latitude + * @param inLon longitude + * @param inLang language code to use, such as en or de + */ + private void submitSearch(double inLat, double inLon, String inLang) + { + // Example http://api.geonames.org/findNearbyWikipedia?lat=47&lng=9 + String urlString = "http://api.geonames.org/findNearbyWikipedia?lat=" + + inLat + "&lng=" + inLon + "&maxRows=" + MAX_RESULTS + + "&radius=" + MAX_DISTANCE + "&lang=" + inLang + + "&username=" + GEONAMES_USERNAME; // Parse the returned XML with a special handler GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler(); + InputStream inStream = null; + try { URL url = new URL(urlString); @@ -91,7 +121,7 @@ public class GetWikipediaFunction extends GenericDownloaderFunction saxParser.parse(inStream, xmlHandler); } catch (Exception e) { - descMessage = e.getClass().getName() + " - " + e.getMessage(); + _errorMessage = e.getClass().getName() + " - " + e.getMessage(); } // Close stream and ignore errors try { @@ -101,29 +131,40 @@ public class GetWikipediaFunction extends GenericDownloaderFunction ArrayList trackList = xmlHandler.getTrackList(); _trackListModel.addTracks(trackList); - // Set status label according to error or "none found", leave blank if ok - if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) { - descMessage = I18nManager.getText("dialog.gpsies.nonefound"); + // Show error message if any + if (_trackListModel.isEmpty()) + { + String error = xmlHandler.getErrorMessage(); + if (error != null && !error.equals("")) + { + _app.showErrorMessageNoLookup(getNameKey(), error); + _errorMessage = error; + } } - _statusLabel.setText(descMessage); } /** - * Load the selected track or point + * Load the selected point(s) */ protected void loadSelected() { - // Find the row selected in the table and get the corresponding track - int rowNum = _trackTable.getSelectedRow(); - if (rowNum >= 0 && rowNum < _trackListModel.getRowCount()) + // Find the rows selected in the table and get the corresponding coords + int numSelected = _trackTable.getSelectedRowCount(); + if (numSelected < 1) return; + int[] rowNums = _trackTable.getSelectedRows(); + for (int i=0; i= 0 && rowNum < _trackListModel.getRowCount()) { - DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null); - point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false); - _app.createPoint(point); + String coords = _trackListModel.getTrack(rowNum).getDownloadLink(); + String[] latlon = coords.split(","); + if (latlon.length == 2) + { + DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null); + point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false); + _app.createPoint(point); + } } } // Close the dialog