1 package tim.prune.function.gpsies;
3 import java.io.IOException;
4 import java.io.InputStream;
6 import java.util.ArrayList;
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
12 import tim.prune.I18nManager;
13 import tim.prune.load.xml.XmlFileLoader;
14 import tim.prune.load.xml.ZipFileLoader;
17 * Function to load track information from Gpsies.com
18 * according to the currently viewed area
20 public class GetGpsiesFunction extends GenericDownloaderFunction
22 /** Number of results per page */
23 private static final int RESULTS_PER_PAGE = 20;
24 /** Maximum number of results to get */
25 private static final int MAX_RESULTS = 60;
30 * @param inApp App object
32 public GetGpsiesFunction(App inApp) {
39 public String getNameKey() {
40 return "function.getgpsies";
44 * @param inColNum index of column, 0 or 1
45 * @return key for this column
47 protected String getColumnKey(int inColNum)
49 if (inColNum == 0) return "dialog.gpsies.column.name";
50 return "dialog.gpsies.column.length";
55 * Run method to call gpsies.com in separate thread
59 _statusLabel.setText(I18nManager.getText("confirm.running"));
60 // Act on callback to update list and send another request if necessary
61 double[] coords = _app.getViewport().getBounds();
62 // Example http://www.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&trackTypes=jogging&filetype=kml&device=Run.GPS
65 ArrayList<GpsiesTrack> trackList = null;
67 String descMessage = "";
68 InputStream inStream = null;
69 // Loop for each page of the results
72 String urlString = "http://www.gpsies.com/api.do?BBOX=" +
73 coords[1] + "," + coords[0] + "," + coords[3] + "," + coords[2] +
74 "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage;
75 // Parse the returned XML with a special handler
76 GpsiesXmlHandler xmlHandler = new GpsiesXmlHandler();
79 url = new URL(urlString);
80 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
81 inStream = url.openStream();
82 saxParser.parse(inStream, xmlHandler);
85 descMessage = e.getClass().getName() + " - " + e.getMessage();
87 // Close stream and ignore errors
90 } catch (Exception e) {}
91 // Add track list to model
92 trackList = xmlHandler.getTrackList();
93 _trackListModel.addTracks(trackList);
95 // Compare number of results with results per page and call again if necessary
98 while (trackList != null && trackList.size() == RESULTS_PER_PAGE
99 && _trackListModel.getRowCount() < MAX_RESULTS && !_cancelled);
100 // Set status label according to error or "none found", leave blank if ok
101 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
102 descMessage = I18nManager.getText("dialog.gpsies.nonefound");
104 _statusLabel.setText(descMessage);
108 * Load the selected track or point
110 protected void loadSelected()
112 // Find the row selected in the table and get the corresponding track
113 int rowNum = _trackTable.getSelectedRow();
114 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
116 String url = _trackListModel.getTrack(rowNum).getDownloadLink();
117 XmlFileLoader xmlLoader = new XmlFileLoader(_app);
118 ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
121 loader.openStream(new URL(url).openStream());
123 catch (IOException ioe) {
124 System.err.println("IO Exception : " + ioe.getMessage());