]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SearchOsmPoisFunction.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / function / SearchOsmPoisFunction.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.Coordinate;
13 import tim.prune.data.DataPoint;
14 import tim.prune.data.Distance;
15 import tim.prune.data.Field;
16 import tim.prune.data.Latitude;
17 import tim.prune.data.Longitude;
18 import tim.prune.function.search.GenericDownloaderFunction;
19 import tim.prune.function.search.SearchResult;
20
21 /**
22  * Function to load nearby point information from OSM
23  */
24 public class SearchOsmPoisFunction extends GenericDownloaderFunction
25 {
26         /** Maximum distance from point in m */
27         private static final int MAX_DISTANCE = 250;
28         /** Coordinates to search for */
29         private double _searchLatitude = 0.0, _searchLongitude = 0.0;
30
31
32         /**
33          * Constructor
34          * @param inApp App object
35          */
36         public SearchOsmPoisFunction(App inApp) {
37                 super(inApp);
38         }
39
40         /**
41          * @return name key
42          */
43         public String getNameKey() {
44                 return "function.searchosmpois";
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.osmpois.column.name";
54                 return "dialog.osmpois.column.type";
55         }
56
57
58         /**
59          * Run method to get the nearby points in a separate thread
60          */
61         public void run()
62         {
63                 _statusLabel.setText(I18nManager.getText("confirm.running"));
64                 // Get coordinates from current point (if any) or from centre of screen
65                 DataPoint point = _app.getTrackInfo().getCurrentPoint();
66                 if (point == null)
67                 {
68                         double[] coords  = _app.getViewport().getBounds();
69                         _searchLatitude  = (coords[0] + coords[2]) / 2.0;
70                         _searchLongitude = (coords[1] + coords[3]) / 2.0;
71                 }
72                 else
73                 {
74                         _searchLatitude  = point.getLatitude().getDouble();
75                         _searchLongitude = point.getLongitude().getDouble();
76                 }
77
78                 // Submit search (language not an issue here)
79                 submitSearch(_searchLatitude, _searchLongitude);
80
81                 // Set status label according to error or "none found", leave blank if ok
82                 if (_errorMessage == null && _trackListModel.isEmpty()) {
83                         _errorMessage = I18nManager.getText("dialog.osmpois.nonefound");
84                 }
85                 _statusLabel.setText(_errorMessage == null ? "" : _errorMessage);
86         }
87
88         /**
89          * Submit the search for the given parameters
90          * @param inLat latitude
91          * @param inLon longitude
92          */
93         private void submitSearch(double inLat, double inLon)
94         {
95                 String coords = "around:" + MAX_DISTANCE + "," + inLat + "," + inLon;
96                 String urlString = "http://overpass-api.de/api/interpreter?data=("
97                         + "node(" + coords + ")[\"amenity\"][\"name\"];"
98                         + "node(" + coords + ")[\"railway\"][\"name\"];"
99                         + "node(" + coords + ")[\"highway\"][\"name\"];"
100                         + ");out%20qt;";
101                 //System.out.println(urlString);
102                 // Parse the returned XML with a special handler
103                 SearchOsmPoisXmlHandler xmlHandler = new SearchOsmPoisXmlHandler();
104                 InputStream inStream = null;
105
106                 try
107                 {
108                         URL url = new URL(urlString);
109                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
110                         inStream = url.openStream();
111                         saxParser.parse(inStream, xmlHandler);
112                 }
113                 catch (Exception e) {
114                         _errorMessage = e.getClass().getName() + " - " + e.getMessage();
115                 }
116                 // Close stream and ignore errors
117                 try {
118                         inStream.close();
119                 } catch (Exception e) {}
120
121                 // Calculate distances for each returned point
122                 DataPoint searchPoint = new DataPoint(new Latitude(_searchLatitude, Coordinate.FORMAT_DECIMAL_FORCE_POINT),
123                         new Longitude(_searchLongitude, Coordinate.FORMAT_DECIMAL_FORCE_POINT), null);
124                 for (SearchResult result : xmlHandler.getPointList())
125                 {
126                         Latitude pointLat = new Latitude(result.getLatitude());
127                         Longitude pointLon = new Longitude(result.getLongitude());
128                         DataPoint foundPoint = new DataPoint(pointLat, pointLon, null);
129                         double dist = DataPoint.calculateRadiansBetween(searchPoint, foundPoint);
130                         result.setLength(Distance.convertRadiansToDistance(dist));
131                 }
132
133                 // TODO: maybe limit number of results using MAX_RESULTS
134                 // Add track list to model
135                 ArrayList<SearchResult> pointList = xmlHandler.getPointList();
136                 _trackListModel.addTracks(pointList, true);
137                 _trackListModel.setShowPointTypes(true);
138
139                 // Show error message if any
140                 if (_trackListModel.isEmpty())
141                 {
142                         String error = xmlHandler.getErrorMessage();
143                         if (error != null && !error.equals(""))
144                         {
145                                 _app.showErrorMessageNoLookup(getNameKey(), error);
146                                 _errorMessage = error;
147                         }
148                 }
149         }
150
151         /**
152          * Load the selected point(s)
153          */
154         protected void loadSelected()
155         {
156                 // Find the rows selected in the table and get the corresponding coords
157                 int numSelected = _trackTable.getSelectedRowCount();
158                 if (numSelected < 1) return;
159                 int[] rowNums = _trackTable.getSelectedRows();
160                 for (int i=0; i<numSelected; i++)
161                 {
162                         int rowNum = rowNums[i];
163                         if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
164                         {
165                                 String lat = _trackListModel.getTrack(rowNum).getLatitude();
166                                 String lon = _trackListModel.getTrack(rowNum).getLongitude();
167                                 if (lat != null && lon != null)
168                                 {
169                                         DataPoint point = new DataPoint(new Latitude(lat), new Longitude(lon), null);
170                                         point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
171                                         _app.createPoint(point);
172                                 }
173                         }
174                 }
175                 // Close the dialog
176                 _cancelled = true;
177                 _dialog.dispose();
178         }
179 }