1 package tim.prune.function.gpsies;
3 import java.io.IOException;
4 import java.io.InputStream;
6 import java.net.URLConnection;
7 import java.util.ArrayList;
9 import javax.xml.parsers.SAXParser;
10 import javax.xml.parsers.SAXParserFactory;
13 import tim.prune.GpsPrune;
14 import tim.prune.I18nManager;
15 import tim.prune.function.search.GenericDownloaderFunction;
16 import tim.prune.function.search.SearchResult;
17 import tim.prune.load.xml.XmlFileLoader;
18 import tim.prune.load.xml.ZipFileLoader;
21 * Function to load track information from Gpsies.com
22 * according to the currently viewed area
24 public class GetGpsiesFunction extends GenericDownloaderFunction
26 /** Number of results per page */
27 private static final int RESULTS_PER_PAGE = 20;
28 /** Maximum number of results to get */
29 private static final int MAX_RESULTS = 60;
30 /** New API key (specific to this program) */
31 private static final String GPSIES_API_KEY = "oumgvvbckiwpvsnb";
36 * @param inApp App object
38 public GetGpsiesFunction(App inApp) {
45 public String getNameKey() {
46 return "function.getgpsies";
50 * @param inColNum index of column, 0 or 1
51 * @return key for this column
53 protected String getColumnKey(int inColNum)
55 if (inColNum == 0) return "dialog.gpsies.column.name";
56 return "dialog.gpsies.column.length";
61 * Run method to call gpsies.com in separate thread
65 _statusLabel.setText(I18nManager.getText("confirm.running"));
66 // Act on callback to update list and send another request if necessary
67 double[] coords = _app.getViewport().getBounds();
70 ArrayList<SearchResult> trackList = null;
72 String descMessage = "";
73 InputStream inStream = null;
74 // Loop for each page of the results
77 // Example http://ws.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&resultPage=1&key=oumgvvbckiwpvsnb
78 String urlString = "http://ws.gpsies.com/api.do?BBOX=" +
79 coords[1] + "," + coords[0] + "," + coords[3] + "," + coords[2] +
80 "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage +
81 "&key=" + GPSIES_API_KEY;
82 // Parse the returned XML with a special handler
83 GpsiesXmlHandler xmlHandler = new GpsiesXmlHandler();
86 url = new URL(urlString);
87 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
88 URLConnection conn = url.openConnection();
89 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
90 inStream = conn.getInputStream();
91 saxParser.parse(inStream, xmlHandler);
94 descMessage = e.getClass().getName() + " - " + e.getMessage();
96 // Close stream and ignore errors
99 } catch (Exception e) {}
100 // Add track list to model
101 trackList = xmlHandler.getTrackList();
102 _trackListModel.addTracks(trackList);
104 // Compare number of results with results per page and call again if necessary
107 while (trackList != null && trackList.size() == RESULTS_PER_PAGE
108 && _trackListModel.getRowCount() < MAX_RESULTS && !_cancelled);
109 // Set status label according to error or "none found", leave blank if ok
110 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
111 descMessage = I18nManager.getText("dialog.gpsies.nonefound");
113 _statusLabel.setText(descMessage);
117 * Load the selected track(s)
119 protected void loadSelected()
121 // Find the row(s) selected in the table and get the corresponding track
122 int numSelected = _trackTable.getSelectedRowCount();
123 if (numSelected < 1) return;
124 int[] rowNums = _trackTable.getSelectedRows();
125 for (int i=0; i<numSelected; i++)
127 int rowNum = rowNums[i];
128 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
130 String url = _trackListModel.getTrack(rowNum).getDownloadLink();
131 XmlFileLoader xmlLoader = new XmlFileLoader(_app);
132 ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
133 if (i>0) _app.autoAppendNextFile();
136 loader.openStream(new URL(url).openStream());
138 catch (IOException ioe) {
139 System.err.println("IO Exception : " + ioe.getMessage());