]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/GetWikipediaFunction.java
acef09c6a15d3ba2a396df9b6733c781f97b856b
[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
30
31         /**
32          * Constructor
33          * @param inApp App object
34          */
35         public GetWikipediaFunction(App inApp) {
36                 super(inApp);
37         }
38
39         /**
40          * @return name key
41          */
42         public String getNameKey() {
43                 return "function.getwikipedia";
44         }
45
46         /**
47          * @param inColNum index of column, 0 or 1
48          * @return key for this column
49          */
50         protected String getColumnKey(int inColNum)
51         {
52                 if (inColNum == 0) return "dialog.wikipedia.column.name";
53                 return "dialog.wikipedia.column.distance";
54         }
55
56
57         /**
58          * Run method to call geonames in separate thread
59          */
60         public void run()
61         {
62                 _statusLabel.setText(I18nManager.getText("confirm.running"));
63                 // Get coordinates from current point (if any) or from centre of screen
64                 double lat = 0.0, lon = 0.0;
65                 DataPoint point = _app.getTrackInfo().getCurrentPoint();
66                 if (point == null)
67                 {
68                         double[] coords = _app.getViewport().getBounds();
69                         lat = (coords[0] + coords[2]) / 2.0;
70                         lon = (coords[1] + coords[3]) / 2.0;
71                 }
72                 else {
73                         lat = point.getLatitude().getDouble();
74                         lon = point.getLongitude().getDouble();
75                 }
76
77                 String descMessage = "";
78                 InputStream inStream = null;
79
80                 // Example http://ws.geonames.org/findNearbyWikipedia?lat=47&lng=9
81                 String urlString = "http://ws.geonames.org/findNearbyWikipedia?lat=" +
82                         lat + "&lng=" + lon + "&maxRows=" + MAX_RESULTS
83                         + "&radius=" + MAX_DISTANCE + "&lang=" + I18nManager.getText("wikipedia.lang");
84                 // Parse the returned XML with a special handler
85                 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
86                 try
87                 {
88                         URL url = new URL(urlString);
89                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
90                         inStream = url.openStream();
91                         saxParser.parse(inStream, xmlHandler);
92                 }
93                 catch (Exception e) {
94                         descMessage = e.getClass().getName() + " - " + e.getMessage();
95                 }
96                 // Close stream and ignore errors
97                 try {
98                         inStream.close();
99                 } catch (Exception e) {}
100                 // Add track list to model
101                 ArrayList<GpsiesTrack> trackList = xmlHandler.getTrackList();
102                 _trackListModel.addTracks(trackList);
103
104                 // Set status label according to error or "none found", leave blank if ok
105                 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
106                         descMessage = I18nManager.getText("dialog.gpsies.nonefound");
107                 }
108                 _statusLabel.setText(descMessage);
109         }
110
111         /**
112          * Load the selected track or point
113          */
114         protected void loadSelected()
115         {
116                 // Find the row selected in the table and get the corresponding track
117                 int rowNum = _trackTable.getSelectedRow();
118                 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
119                 {
120                         String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
121                         String[] latlon = coords.split(",");
122                         if (latlon.length == 2)
123                         {
124                                 DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
125                                 point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
126                                 _app.createPoint(point);
127                         }
128                 }
129                 // Close the dialog
130                 _cancelled = true;
131                 _dialog.dispose();
132         }
133 }