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.load.xml.XmlFileLoader;
16 import tim.prune.load.xml.ZipFileLoader;
19 * Function to load track information from Gpsies.com
20 * according to the currently viewed area
22 public class GetGpsiesFunction extends GenericDownloaderFunction
24 /** Number of results per page */
25 private static final int RESULTS_PER_PAGE = 20;
26 /** Maximum number of results to get */
27 private static final int MAX_RESULTS = 60;
28 /** New API key (specific to this program) */
29 private static final String GPSIES_API_KEY = "oumgvvbckiwpvsnb";
34 * @param inApp App object
36 public GetGpsiesFunction(App inApp) {
43 public String getNameKey() {
44 return "function.getgpsies";
48 * @param inColNum index of column, 0 or 1
49 * @return key for this column
51 protected String getColumnKey(int inColNum)
53 if (inColNum == 0) return "dialog.gpsies.column.name";
54 return "dialog.gpsies.column.length";
59 * Run method to call gpsies.com in separate thread
63 _statusLabel.setText(I18nManager.getText("confirm.running"));
64 // Act on callback to update list and send another request if necessary
65 double[] coords = _app.getViewport().getBounds();
68 ArrayList<GpsiesTrack> trackList = null;
70 String descMessage = "";
71 InputStream inStream = null;
72 // Loop for each page of the results
75 // Example http://ws.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&resultPage=1&key=oumgvvbckiwpvsnb
76 String urlString = "http://ws.gpsies.com/api.do?BBOX=" +
77 coords[1] + "," + coords[0] + "," + coords[3] + "," + coords[2] +
78 "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage +
79 "&key=" + GPSIES_API_KEY;
80 // Parse the returned XML with a special handler
81 GpsiesXmlHandler xmlHandler = new GpsiesXmlHandler();
84 url = new URL(urlString);
85 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
86 URLConnection conn = url.openConnection();
87 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
88 inStream = conn.getInputStream();
89 saxParser.parse(inStream, xmlHandler);
92 descMessage = e.getClass().getName() + " - " + e.getMessage();
94 // Close stream and ignore errors
97 } catch (Exception e) {}
98 // Add track list to model
99 trackList = xmlHandler.getTrackList();
100 _trackListModel.addTracks(trackList);
102 // Compare number of results with results per page and call again if necessary
105 while (trackList != null && trackList.size() == RESULTS_PER_PAGE
106 && _trackListModel.getRowCount() < MAX_RESULTS && !_cancelled);
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);
115 * Load the selected track(s)
117 protected void loadSelected()
119 // Find the row(s) selected in the table and get the corresponding track
120 int numSelected = _trackTable.getSelectedRowCount();
121 if (numSelected < 1) return;
122 int[] rowNums = _trackTable.getSelectedRows();
123 for (int i=0; i<numSelected; i++)
125 int rowNum = rowNums[i];
126 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
128 String url = _trackListModel.getTrack(rowNum).getDownloadLink();
129 XmlFileLoader xmlLoader = new XmlFileLoader(_app);
130 ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
131 if (i>0) _app.autoAppendNextFile();
134 loader.openStream(new URL(url).openStream());
136 catch (IOException ioe) {
137 System.err.println("IO Exception : " + ioe.getMessage());