]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/GetGpsiesFunction.java
51085cf14742a8ae57b6b4bf75930439ee1fb8b0
[GpsPrune.git] / tim / prune / function / gpsies / GetGpsiesFunction.java
1 package tim.prune.function.gpsies;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.net.URLConnection;
7 import java.util.ArrayList;
8
9 import javax.xml.parsers.SAXParser;
10 import javax.xml.parsers.SAXParserFactory;
11
12 import tim.prune.App;
13 import tim.prune.GpsPrune;
14 import tim.prune.I18nManager;
15 import tim.prune.load.xml.XmlFileLoader;
16 import tim.prune.load.xml.ZipFileLoader;
17
18 /**
19  * Function to load track information from Gpsies.com
20  * according to the currently viewed area
21  */
22 public class GetGpsiesFunction extends GenericDownloaderFunction
23 {
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";
30
31
32         /**
33          * Constructor
34          * @param inApp App object
35          */
36         public GetGpsiesFunction(App inApp) {
37                 super(inApp);
38         }
39
40         /**
41          * @return name key
42          */
43         public String getNameKey() {
44                 return "function.getgpsies";
45         }
46
47         /**
48          * @param inColNum index of column, 0 or 1
49          * @return key for this column
50          */
51         protected String getColumnKey(int inColNum)
52         {
53                 if (inColNum == 0) return "dialog.gpsies.column.name";
54                 return "dialog.gpsies.column.length";
55         }
56
57
58         /**
59          * Run method to call gpsies.com in separate thread
60          */
61         public void run()
62         {
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();
66                 // Example http://www.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&trackTypes=jogging&filetype=kml&device=Run.GPS
67                 int currPage = 1;
68
69                 ArrayList<GpsiesTrack> trackList = null;
70                 URL url = null;
71                 String descMessage = "";
72                 InputStream inStream = null;
73                 // Loop for each page of the results
74                 do
75                 {
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();
82                         try
83                         {
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);
90                         }
91                         catch (Exception e) {
92                                 descMessage = e.getClass().getName() + " - " + e.getMessage();
93                         }
94                         // Close stream and ignore errors
95                         try {
96                                 inStream.close();
97                         } catch (Exception e) {}
98                         // Add track list to model
99                         trackList = xmlHandler.getTrackList();
100                         _trackListModel.addTracks(trackList);
101
102                         // Compare number of results with results per page and call again if necessary
103                         currPage++;
104                 }
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");
110                 }
111                 _statusLabel.setText(descMessage);
112         }
113
114         /**
115          * Load the selected track or point
116          */
117         protected void loadSelected()
118         {
119                 // Find the row selected in the table and get the corresponding track
120                 int rowNum = _trackTable.getSelectedRow();
121                 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
122                 {
123                         String url = _trackListModel.getTrack(rowNum).getDownloadLink();
124                         XmlFileLoader xmlLoader = new XmlFileLoader(_app);
125                         ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
126                         try
127                         {
128                                 loader.openStream(new URL(url).openStream());
129                         }
130                         catch (IOException ioe) {
131                                 System.err.println("IO Exception : " + ioe.getMessage());
132                         }
133                 }
134                 // Close the dialog
135                 _cancelled = true;
136                 _dialog.dispose();
137         }
138 }