1 package tim.prune.function;
3 import java.io.InputStream;
5 import java.util.ArrayList;
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
11 import tim.prune.I18nManager;
12 import tim.prune.data.DataPoint;
13 import tim.prune.data.Field;
14 import tim.prune.data.Latitude;
15 import tim.prune.data.Longitude;
16 import tim.prune.function.gpsies.GenericDownloaderFunction;
17 import tim.prune.function.gpsies.GpsiesTrack;
20 * Function to load nearby point information from Wikipedia
21 * according to the currently viewed area
23 public class GetWikipediaFunction extends GenericDownloaderFunction
25 /** Maximum number of results to get */
26 private static final int MAX_RESULTS = 20;
27 /** Maximum distance from point in km */
28 private static final int MAX_DISTANCE = 15;
29 /** Username to use for geonames queries */
30 private static final String GEONAMES_USERNAME = "gpsprune";
35 * @param inApp App object
37 public GetWikipediaFunction(App inApp) {
44 public String getNameKey() {
45 return "function.getwikipedia";
49 * @param inColNum index of column, 0 or 1
50 * @return key for this column
52 protected String getColumnKey(int inColNum)
54 if (inColNum == 0) return "dialog.wikipedia.column.name";
55 return "dialog.wikipedia.column.distance";
60 * Run method to call geonames in separate thread
64 _statusLabel.setText(I18nManager.getText("confirm.running"));
65 // Get coordinates from current point (if any) or from centre of screen
66 double lat = 0.0, lon = 0.0;
67 DataPoint point = _app.getTrackInfo().getCurrentPoint();
70 double[] coords = _app.getViewport().getBounds();
71 lat = (coords[0] + coords[2]) / 2.0;
72 lon = (coords[1] + coords[3]) / 2.0;
75 lat = point.getLatitude().getDouble();
76 lon = point.getLongitude().getDouble();
79 String descMessage = "";
80 InputStream inStream = null;
82 // Example http://api.geonames.org/findNearbyWikipedia?lat=47&lng=9
83 String urlString = "http://api.geonames.org/findNearbyWikipedia?lat=" +
84 lat + "&lng=" + lon + "&maxRows=" + MAX_RESULTS
85 + "&radius=" + MAX_DISTANCE + "&lang=" + I18nManager.getText("wikipedia.lang")
86 + "&username=" + GEONAMES_USERNAME;
87 // Parse the returned XML with a special handler
88 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
91 URL url = new URL(urlString);
92 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
93 inStream = url.openStream();
94 saxParser.parse(inStream, xmlHandler);
97 descMessage = e.getClass().getName() + " - " + e.getMessage();
99 // Close stream and ignore errors
102 } catch (Exception e) {}
103 // Add track list to model
104 ArrayList<GpsiesTrack> trackList = xmlHandler.getTrackList();
105 _trackListModel.addTracks(trackList);
107 // Set status label according to error or "none found", leave blank if ok
108 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
109 descMessage = I18nManager.getText("dialog.gpsies.nonefound");
111 _statusLabel.setText(descMessage);
112 // Show error message if any
113 if (trackList == null || trackList.size() == 0) {
114 String error = xmlHandler.getErrorMessage();
115 if (error != null && !error.equals("")) {
116 _app.showErrorMessageNoLookup(getNameKey(), error);
123 * Load the selected track or point
125 protected void loadSelected()
127 // Find the row selected in the table and get the corresponding track
128 int rowNum = _trackTable.getSelectedRow();
129 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
131 String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
132 String[] latlon = coords.split(",");
133 if (latlon.length == 2)
135 DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
136 point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
137 _app.createPoint(point);