]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/GetWikipediaFunction.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / function / GetWikipediaFunction.java
1 package tim.prune.function;
2
3 import java.io.InputStream;
4 import java.net.URL;
5 import java.util.ArrayList;
6
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
9
10 import tim.prune.App;
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;
18
19 /**
20  * Function to load nearby point information from Wikipedia
21  * according to the currently viewed area
22  */
23 public class GetWikipediaFunction extends GenericDownloaderFunction
24 {
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";
31
32
33         /**
34          * Constructor
35          * @param inApp App object
36          */
37         public GetWikipediaFunction(App inApp) {
38                 super(inApp);
39         }
40
41         /**
42          * @return name key
43          */
44         public String getNameKey() {
45                 return "function.getwikipedia";
46         }
47
48         /**
49          * @param inColNum index of column, 0 or 1
50          * @return key for this column
51          */
52         protected String getColumnKey(int inColNum)
53         {
54                 if (inColNum == 0) return "dialog.wikipedia.column.name";
55                 return "dialog.wikipedia.column.distance";
56         }
57
58
59         /**
60          * Run method to call geonames in separate thread
61          */
62         public void run()
63         {
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();
68                 if (point == null)
69                 {
70                         double[] coords = _app.getViewport().getBounds();
71                         lat = (coords[0] + coords[2]) / 2.0;
72                         lon = (coords[1] + coords[3]) / 2.0;
73                 }
74                 else
75                 {
76                         lat = point.getLatitude().getDouble();
77                         lon = point.getLongitude().getDouble();
78                 }
79
80                 // Firstly try the local language
81                 String lang = I18nManager.getText("wikipedia.lang");
82                 submitSearch(lat, lon, lang);
83                 // If we didn't get anything, try a secondary language
84                 if (_trackListModel.isEmpty() && _errorMessage == null && lang.equals("als")) {
85                         submitSearch(lat, lon, "de");
86                 }
87                 // If still nothing then try english
88                 if (_trackListModel.isEmpty() && _errorMessage == null && !lang.equals("en")) {
89                         submitSearch(lat, lon, "en");
90                 }
91
92                 // Set status label according to error or "none found", leave blank if ok
93                 if (_errorMessage == null && _trackListModel.isEmpty()) {
94                         _errorMessage = I18nManager.getText("dialog.wikipedia.nonefound");
95                 }
96                 _statusLabel.setText(_errorMessage == null ? "" : _errorMessage);
97         }
98
99         /**
100          * Submit the search for the given parameters
101          * @param inLat latitude
102          * @param inLon longitude
103          * @param inLang language code to use, such as en or de
104          */
105         private void submitSearch(double inLat, double inLon, String inLang)
106         {
107                 // Example http://api.geonames.org/findNearbyWikipedia?lat=47&lng=9
108                 String urlString = "http://api.geonames.org/findNearbyWikipedia?lat=" +
109                         inLat + "&lng=" + inLon + "&maxRows=" + MAX_RESULTS
110                         + "&radius=" + MAX_DISTANCE + "&lang=" + inLang
111                         + "&username=" + GEONAMES_USERNAME;
112                 // Parse the returned XML with a special handler
113                 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
114                 InputStream inStream = null;
115
116                 try
117                 {
118                         URL url = new URL(urlString);
119                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
120                         inStream = url.openStream();
121                         saxParser.parse(inStream, xmlHandler);
122                 }
123                 catch (Exception e) {
124                         _errorMessage = e.getClass().getName() + " - " + e.getMessage();
125                 }
126                 // Close stream and ignore errors
127                 try {
128                         inStream.close();
129                 } catch (Exception e) {}
130                 // Add track list to model
131                 ArrayList<GpsiesTrack> trackList = xmlHandler.getTrackList();
132                 _trackListModel.addTracks(trackList);
133
134                 // Show error message if any
135                 if (_trackListModel.isEmpty())
136                 {
137                         String error = xmlHandler.getErrorMessage();
138                         if (error != null && !error.equals(""))
139                         {
140                                 _app.showErrorMessageNoLookup(getNameKey(), error);
141                                 _errorMessage = error;
142                         }
143                 }
144         }
145
146         /**
147          * Load the selected point(s)
148          */
149         protected void loadSelected()
150         {
151                 // Find the rows selected in the table and get the corresponding coords
152                 int numSelected = _trackTable.getSelectedRowCount();
153                 if (numSelected < 1) return;
154                 int[] rowNums = _trackTable.getSelectedRows();
155                 for (int i=0; i<numSelected; i++)
156                 {
157                         int rowNum = rowNums[i];
158                         if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
159                         {
160                                 String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
161                                 String[] latlon = coords.split(",");
162                                 if (latlon.length == 2)
163                                 {
164                                         DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
165                                         point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
166                                         _app.createPoint(point);
167                                 }
168                         }
169                 }
170                 // Close the dialog
171                 _cancelled = true;
172                 _dialog.dispose();
173         }
174 }