]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/SearchWikipediaNames.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / SearchWikipediaNames.java
index 4377df81d5a39bbdd65bc897d0bf575104b2011e..0458943036ed8007ffecb0838bf66c504aff4e6d 100644 (file)
@@ -1,7 +1,9 @@
 package tim.prune.function;
 
 import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.util.ArrayList;
 
 import javax.swing.JOptionPane;
@@ -26,6 +28,8 @@ public class SearchWikipediaNames extends GenericDownloaderFunction
        private String _searchTerm = null;
        /** Maximum number of results to get */
        private static final int MAX_RESULTS = 20;
+       /** Username to use for geonames queries */
+       private static final String GEONAMES_USERNAME = "gpsprune";
 
        /**
         * Constructor
@@ -88,9 +92,16 @@ public class SearchWikipediaNames extends GenericDownloaderFunction
                else {
                        lang = "en";
                }
+               // Replace awkward characters with character equivalents
+               String searchTerm;
+               try {
+                       searchTerm = URLEncoder.encode(_searchTerm, "UTF-8");
+               } catch (UnsupportedEncodingException e1) {
+                       searchTerm = _searchTerm;
+               }
                // Example http://ws.geonames.org/wikipediaSearch?q=london&maxRows=10
-               String urlString = "http://ws.geonames.org/wikipediaSearch?title=" + _searchTerm + "&maxRows=" + MAX_RESULTS
-                       + "&lang=" + lang;
+               String urlString = "http://api.geonames.org/wikipediaSearch?title=" + searchTerm
+                       + "&maxRows=" + MAX_RESULTS + "&lang=" + lang + "&username=" + GEONAMES_USERNAME;
                // Parse the returned XML with a special handler
                GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
                try
@@ -119,22 +130,29 @@ public class SearchWikipediaNames extends GenericDownloaderFunction
                _statusLabel.setText(descMessage);
        }
 
+
        /**
-        * Load the selected track or point
+        * Load the selected point(s)
         */
        protected void loadSelected()
        {
-               // Find the row selected in the table and get the corresponding track
-               int rowNum = _trackTable.getSelectedRow();
-               if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
+               // Find the rows selected in the table and get the corresponding coords
+               int numSelected = _trackTable.getSelectedRowCount();
+               if (numSelected < 1) return;
+               int[] rowNums = _trackTable.getSelectedRows();
+               for (int i=0; i<numSelected; i++)
                {
-                       String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
-                       String[] latlon = coords.split(",");
-                       if (latlon.length == 2)
+                       int rowNum = rowNums[i];
+                       if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
                        {
-                               DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
-                               point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
-                               _app.createPoint(point);
+                               String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
+                               String[] latlon = coords.split(",");
+                               if (latlon.length == 2)
+                               {
+                                       DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
+                                       point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
+                                       _app.createPoint(point);
+                               }
                        }
                }
                // Close the dialog