]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/GetWikipediaFunction.java
Version 18.1, September 2015
[GpsPrune.git] / tim / prune / function / GetWikipediaFunction.java
index be33a95b7ba3266944726b498d5bddf2e84ef8d8..804ba714bbf1db0acc86fa36e1e65514c4498b27 100644 (file)
@@ -1,6 +1,8 @@
 package tim.prune.function;
 
+import java.io.BufferedReader;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.ArrayList;
 
@@ -10,14 +12,16 @@ import javax.xml.parsers.SAXParserFactory;
 import tim.prune.App;
 import tim.prune.I18nManager;
 import tim.prune.data.DataPoint;
+import tim.prune.data.Distance;
 import tim.prune.data.Field;
 import tim.prune.data.Latitude;
 import tim.prune.data.Longitude;
+import tim.prune.data.UnitSetLibrary;
 import tim.prune.function.search.GenericDownloaderFunction;
 import tim.prune.function.search.SearchResult;
 
 /**
- * Function to load nearby point information from Wikipedia
+ * Function to load nearby point information from Wikipedia (and Wikimedia)
  * according to the currently viewed area
  */
 public class GetWikipediaFunction extends GenericDownloaderFunction
@@ -57,7 +61,7 @@ public class GetWikipediaFunction extends GenericDownloaderFunction
 
 
        /**
-        * Run method to call geonames in separate thread
+        * Run method to get the nearby points in a separate thread
         */
        public void run()
        {
@@ -77,7 +81,10 @@ public class GetWikipediaFunction extends GenericDownloaderFunction
                        lon = point.getLongitude().getDouble();
                }
 
-               // Firstly try the local language
+               // Before we ask geonames online, let's get wikimedia galleries first
+               searchWikimediaGalleries(lat, lon);
+
+               // For geonames, firstly try the local language
                String lang = I18nManager.getText("wikipedia.lang");
                submitSearch(lat, lon, lang);
                // If we didn't get anything, try a secondary language
@@ -129,7 +136,7 @@ public class GetWikipediaFunction extends GenericDownloaderFunction
                } catch (Exception e) {}
                // Add track list to model
                ArrayList<SearchResult> trackList = xmlHandler.getTrackList();
-               _trackListModel.addTracks(trackList);
+               _trackListModel.addTracks(trackList, true);
 
                // Show error message if any
                if (_trackListModel.isEmpty())
@@ -171,4 +178,61 @@ public class GetWikipediaFunction extends GenericDownloaderFunction
                _cancelled = true;
                _dialog.dispose();
        }
+
+       /**
+        * Search the local wikimedia index to see if there are any galleries nearby
+        * @param inLat latitude
+        * @param inLon longitude
+        */
+       private void searchWikimediaGalleries(double inLat, double inLon)
+       {
+               BufferedReader reader = null;
+               try
+               {
+                       InputStream in = GetWikipediaFunction.class.getResourceAsStream("/tim/prune/function/search/wikimedia_galleries.txt");
+                       reader = new BufferedReader(new InputStreamReader(in));
+                       if (reader != null)
+                       {
+                               ArrayList<SearchResult> trackList = new ArrayList<SearchResult>();
+                               DataPoint herePoint = new DataPoint(new Latitude(inLat, Latitude.FORMAT_DEG), new Longitude(inLon, Longitude.FORMAT_DEG), null);
+                               // Loop through the file line by line, looking for nearby points
+                               String line = null;
+                               while ((line = reader.readLine()) != null)
+                               {
+                                       String[] lineComps = line.split("\t");
+                                       if (lineComps.length == 4)
+                                       {
+                                               DataPoint p = new DataPoint(new Latitude(lineComps[2]), new Longitude(lineComps[3]), null);
+                                               double distFromHere = Distance.convertRadiansToDistance(
+                                                       DataPoint.calculateRadiansBetween(p, herePoint), UnitSetLibrary.UNITS_KILOMETRES);
+                                               if (distFromHere < MAX_DISTANCE)
+                                               {
+                                                       SearchResult gallery = new SearchResult();
+                                                       gallery.setTrackName(I18nManager.getText("dialog.wikipedia.gallery") + ": " + lineComps[0]);
+                                                       gallery.setDescription(lineComps[1]);
+                                                       gallery.setLatitude(lineComps[2]);
+                                                       gallery.setLongitude(lineComps[3]);
+                                                       gallery.setWebUrl("https://commons.wikimedia.org/wiki/" + lineComps[0]);
+                                                       gallery.setLength(distFromHere * 1000.0); // convert from km to m
+                                                       trackList.add(gallery);
+                                               }
+                                       }
+                               }
+                               _trackListModel.addTracks(trackList, true);
+                       }
+               }
+               catch (java.io.IOException e) {
+                       System.err.println("Exception trying to read wikimedia file : " + e.getMessage());
+               }
+               catch (NullPointerException e) {
+                       System.err.println("Couldn't find wikimedia file : " + e.getMessage());
+               }
+               finally
+               {
+                       try {
+                               reader.close();
+                       }
+                       catch (Exception e) {} // ignore
+               }
+       }
 }