mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-01 00:30:46 +00:00
Finishing Geocoder-Cache
This commit is contained in:
1 parent
02a293360e
commit
a62e9c98c0
5 files changed
+175
-62
No files matched your search
@@ -18,7 +18,7 @@ public interface ReverseGeocoder {
|
||||
* Returns the nearest {@link Placemark} towards the given {@link Position}.
|
||||
*
|
||||
* @param position The position where to search
|
||||
* @return The nearest {@link Placemark} towards the given {@link Position}
|
||||
* @return The nearest {@link Placemark} towards the given {@link Position} or null if there is none within 300km
|
||||
* @throws IOException
|
||||
* @throws ParseException
|
||||
*/
|
||||
@@ -29,7 +29,7 @@ public interface ReverseGeocoder {
|
||||
*
|
||||
* @param position The position where to search
|
||||
* @param radius The search radius
|
||||
* @return A list of {@link Placemark Placemarks} near the given {@link Position}
|
||||
* @return A list of {@link Placemark Placemarks} near the given {@link Position} or null if there are none within 300km
|
||||
* @throws IOException
|
||||
* @throws ParseException
|
||||
*/
|
||||
|
||||
+133
-58
@@ -3,6 +3,7 @@ package com.sap.sailing.geocoding.impl;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
@@ -27,9 +28,17 @@ import com.sap.sailing.geocoding.ReverseGeocoder;
|
||||
public class ReverseGeocoderImpl implements ReverseGeocoder {
|
||||
|
||||
private final String NEARBY_PLACE_SERVICE = "http://api.geonames.org/findNearbyPlaceNameJSON?";
|
||||
private final double POSITION_CACHE_DISTANCE_LIMIT = 10.0;
|
||||
/**
|
||||
* Maximal distance in degree for the cache.<br />
|
||||
* The first number is the distance in kilometers and the second number is a needed calculation factor and mustn't
|
||||
* be changed! To change the distance just change the first number!
|
||||
*/
|
||||
private final double POSITION_CACHE_DISTANCE_LIMIT = 5.0 * 0.00899928005759539236861051115911;
|
||||
private final int XKM_RADIUS = 5;
|
||||
private final int ROWS_PER_XKM_RADIUS = 20;
|
||||
private final int ROWS_PER_XKM_RADIUS = 15;
|
||||
|
||||
private final int MAX_ROW_NUMBER = 1000;
|
||||
private final int MAX_RADIUS = 300;
|
||||
|
||||
private QuadTree<Triple<Position, Double, List<Placemark>>> cache = new QuadTree<Triple<Position,Double,List<Placemark>>>();;
|
||||
|
||||
@@ -41,22 +50,14 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
|
||||
if (cachedPlacemarks != null) {
|
||||
p = cachedPlacemarks.getC().get(0);
|
||||
} else {
|
||||
StringBuilder url = new StringBuilder(NEARBY_PLACE_SERVICE);
|
||||
url.append("&lat=" + Double.toString(position.getLatDeg()));
|
||||
url.append("&lng=" + Double.toString(position.getLngDeg()));
|
||||
url.append("&username=" + GEONAMES_USER);
|
||||
JSONArray geonames = callNearestService(position);
|
||||
if (!geonames.isEmpty()) {
|
||||
p = JSONToPlacemark((JSONObject) geonames.get(0));
|
||||
|
||||
URL request = new URL(url.toString());
|
||||
URLConnection connection = request.openConnection();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject obj = (JSONObject) parser.parse(in);
|
||||
JSONArray geonames = (JSONArray) obj.get("geonames");
|
||||
p = JSONToPlacemark((JSONObject) geonames.get(0));
|
||||
|
||||
List<Placemark> placemarks = new ArrayList<Placemark>();
|
||||
placemarks.add(p);
|
||||
cachePlacemarks(position, 0.0, placemarks);
|
||||
List<Placemark> placemarks = new ArrayList<Placemark>();
|
||||
placemarks.add(p);
|
||||
cachePlacemarks(position, 0.0, placemarks);
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
@@ -66,40 +67,47 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
|
||||
public List<Placemark> getPlacemarksNear(Position position, double radius) throws IOException, ParseException {
|
||||
List<Placemark> placemarks = null;
|
||||
Triple<Position, Double, List<Placemark>> cachedPlacemarks = checkCache(position);
|
||||
int radiusInt = (int) radius;
|
||||
|
||||
//Calculating the search radius and the maximum number of returning Placemarks
|
||||
double limitedRadius = Math.min(radius, MAX_RADIUS);
|
||||
int radiusInt = (int) limitedRadius;
|
||||
int xKmRadius = radiusInt / XKM_RADIUS;
|
||||
int maxRows = (int) (ROWS_PER_XKM_RADIUS * Math.pow(2, xKmRadius));
|
||||
maxRows = Math.min(maxRows, MAX_ROW_NUMBER);
|
||||
|
||||
if (cachedPlacemarks != null && cachedPlacemarks.getB() >= radius) {
|
||||
placemarks = cachedPlacemarks.getC().subList(0, maxRows);
|
||||
if (cachedPlacemarks != null && cachedPlacemarks.getB() >= limitedRadius) {
|
||||
if (cachedPlacemarks.getC().size() > maxRows) {
|
||||
placemarks = cachedPlacemarks.getC().subList(0, maxRows);
|
||||
} else {
|
||||
placemarks = new ArrayList<Placemark>(cachedPlacemarks.getC());
|
||||
}
|
||||
} else {
|
||||
placemarks = new ArrayList<Placemark>();
|
||||
StringBuilder url = new StringBuilder(NEARBY_PLACE_SERVICE);
|
||||
url.append("&lat=" + Double.toString(position.getLatDeg()));
|
||||
url.append("&lng=" + Double.toString(position.getLngDeg()));
|
||||
url.append("&radius=" + Double.toString(radius));
|
||||
url.append("&maxRows=" + Integer.toString(maxRows));
|
||||
url.append("&username=" + GEONAMES_USER);
|
||||
|
||||
URL request = new URL(url.toString());
|
||||
URLConnection connection = request.openConnection();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject obj = (JSONObject) parser.parse(in);
|
||||
JSONArray geonames = (JSONArray) obj.get("geonames");
|
||||
//Recalculating the radius and resetting the search position to keep the cache correct
|
||||
Position searchPosition = null;
|
||||
if (cachedPlacemarks != null) {
|
||||
searchPosition = cachedPlacemarks.getA();
|
||||
double distance = position.getDistance(searchPosition).getKilometers();
|
||||
limitedRadius = Math.min(MAX_RADIUS, limitedRadius + distance);
|
||||
} else {
|
||||
searchPosition = position;
|
||||
}
|
||||
|
||||
JSONArray geonames = callNearbyService(searchPosition, limitedRadius, maxRows);
|
||||
Iterator<Object> iterator = geonames.iterator();
|
||||
placemarks = !geonames.isEmpty() ? new ArrayList<Placemark>() : null;
|
||||
while (iterator.hasNext()) {
|
||||
JSONObject object = (JSONObject) iterator.next();
|
||||
placemarks.add(JSONToPlacemark(object));
|
||||
Placemark place = JSONToPlacemark(object);
|
||||
if (place != null) {
|
||||
placemarks.add(JSONToPlacemark(object));
|
||||
}
|
||||
}
|
||||
|
||||
// If there are no cached placemarks for the requested Position just cache them, otherwise update the cache
|
||||
if (cachedPlacemarks == null) {
|
||||
cachePlacemarks(position, radius, placemarks);
|
||||
} else {
|
||||
if (maxRows < placemarks.size() - 1) {
|
||||
updateCachedPlacemarks(position, radius, placemarks.subList(maxRows, placemarks.size() - 1));
|
||||
}
|
||||
if (cachedPlacemarks == null && placemarks != null) {
|
||||
cachePlacemarks(searchPosition, limitedRadius, placemarks);
|
||||
} else if (placemarks != null) {
|
||||
updateCachedPlacemarks(searchPosition, limitedRadius, placemarks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,23 +118,33 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
|
||||
public Placemark getPlacemarkLast(Position position, double radius, Comparator<Placemark> comp) throws IOException,
|
||||
ParseException {
|
||||
List<Placemark> placemarks = getPlacemarksNearSorted(position, radius, comp);
|
||||
return placemarks.get(placemarks.size() - 1);
|
||||
return placemarks == null ? null : placemarks.get(placemarks.size() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Placemark getPlacemarkFirst(Position position, double radius, Comparator<Placemark> comp)
|
||||
throws IOException, ParseException {
|
||||
return getPlacemarksNearSorted(position, radius, comp).get(0);
|
||||
List<Placemark> placemarks = getPlacemarksNearSorted(position, radius, comp);
|
||||
return placemarks == null ? null : placemarks.get(0);
|
||||
}
|
||||
|
||||
private List<Placemark> getPlacemarksNearSorted(Position position, double radius, Comparator<Placemark> comp)
|
||||
throws IOException, ParseException {
|
||||
List<Placemark> placemarks = getPlacemarksNear(position, radius);
|
||||
Collections.sort(placemarks, comp);
|
||||
|
||||
if (placemarks != null) {
|
||||
Collections.sort(placemarks, comp);
|
||||
}
|
||||
return placemarks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Placemark} for a compatible JSONObject.
|
||||
*
|
||||
* @param json
|
||||
* The object to be converted
|
||||
* @return A {@link Placemark} or <code>null</code>, if the object doesn't contain a name, a postion or the
|
||||
* if the population is 0
|
||||
*/
|
||||
private Placemark JSONToPlacemark(JSONObject json) {
|
||||
String name = (String) json.get("toponymName");
|
||||
String countryCode = (String) json.get("countryCode");
|
||||
@@ -150,29 +168,86 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
|
||||
|
||||
long population = (Long) json.get("population");
|
||||
|
||||
if (name != null && lngDeg != null && latDeg != null) {
|
||||
if (name != null && lngDeg != null && latDeg != null && population != 0) {
|
||||
return new PlacemarkImpl(name, countryCode, p, population);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void cachePlacemarks(Position p, Double radius, List<Placemark> placemarks) {
|
||||
Collections.sort(placemarks, new Placemark.ByDistance(p));
|
||||
cache.put(p, new Triple<Position, Double, List<Placemark>>(p, radius, placemarks));
|
||||
/**
|
||||
* Caches the <code>position</code>, the <code>radius</code> and the <code>placemarks</code> at the Position
|
||||
* <code>p</code> in the cache.
|
||||
*
|
||||
* @param position
|
||||
* The position in the cache and the point of the search
|
||||
* @param radius
|
||||
* The radius of the search
|
||||
* @param placemarks
|
||||
* The results of the search
|
||||
*/
|
||||
private void cachePlacemarks(Position position, Double radius, List<Placemark> placemarks) {
|
||||
Collections.sort(placemarks, new Placemark.ByDistance(position));
|
||||
cache.put(position, new Triple<Position, Double, List<Placemark>>(position, radius, placemarks));
|
||||
}
|
||||
|
||||
private void updateCachedPlacemarks(Position p, Double newRadius, List<Placemark> newPlacemarks) {
|
||||
Triple<Position, Double, List<Placemark>> cachedPlacemarks = checkCache(p);
|
||||
if (cachedPlacemarks != null) {
|
||||
cachedPlacemarks.setB(newRadius);
|
||||
cachedPlacemarks.getC().addAll(newPlacemarks);
|
||||
Collections.sort(cachedPlacemarks.getC(), new Placemark.ByDistance(cachedPlacemarks.getA()));
|
||||
}
|
||||
/**
|
||||
* Replaces the data at <code>cachedPoint</code> with the <code>newRadius</code> and <code>newPlacemarks</code>.
|
||||
*
|
||||
* @param cachedPoint
|
||||
* The position of the data which should be replaced. Has to be a Position which was cached before.<br />
|
||||
* The paramater is as best a value from a Triple in the cache, like <code>cachedData.getA()</code>.
|
||||
* @param newRadius
|
||||
* The new radius of the cached search results
|
||||
* @param newPlacemarks
|
||||
* The new search results
|
||||
*/
|
||||
private void updateCachedPlacemarks(Position cachedPoint, Double newRadius, List<Placemark> newPlacemarks) {
|
||||
cache.replace(cachedPoint, new Triple<Position, Double, List<Placemark>>(cachedPoint, newRadius, newPlacemarks));
|
||||
}
|
||||
|
||||
private Triple<Position, Double, List<Placemark>> checkCache(Position p) {
|
||||
return cache.get(p, POSITION_CACHE_DISTANCE_LIMIT);
|
||||
/**
|
||||
* @param position
|
||||
* The position to be checked by the cache
|
||||
* @return The cached placemarks (with meta-data) sorted by distance towards <code>position</code> or
|
||||
* <code>null</code>, if there's nothing cached around <code>postion</code> within
|
||||
* {@link ReverseGeocoderImpl#POSITION_CACHE_DISTANCE_LIMIT the distance limit}
|
||||
*/
|
||||
private Triple<Position, Double, List<Placemark>> checkCache(Position position) {
|
||||
return cache.get(position, POSITION_CACHE_DISTANCE_LIMIT);
|
||||
}
|
||||
|
||||
private JSONArray callNearestService(Position position) throws MalformedURLException, IOException, ParseException {
|
||||
StringBuilder url = new StringBuilder(NEARBY_PLACE_SERVICE);
|
||||
url.append("&lat=" + Double.toString(position.getLatDeg()));
|
||||
url.append("&lng=" + Double.toString(position.getLngDeg()));
|
||||
url.append("&username=" + GEONAMES_USER);
|
||||
|
||||
URL request = new URL(url.toString());
|
||||
URLConnection connection = request.openConnection();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject obj = (JSONObject) parser.parse(in);
|
||||
JSONArray geonames = (JSONArray) obj.get("geonames");
|
||||
return geonames;
|
||||
}
|
||||
|
||||
private JSONArray callNearbyService(Position position, double radius, int maxRows) throws MalformedURLException,
|
||||
IOException, ParseException {
|
||||
StringBuilder url = new StringBuilder(NEARBY_PLACE_SERVICE);
|
||||
url.append("&lat=" + Double.toString(position.getLatDeg()));
|
||||
url.append("&lng=" + Double.toString(position.getLngDeg()));
|
||||
url.append("&radius=" + Double.toString(radius));
|
||||
url.append("&maxRows=" + Integer.toString(maxRows));
|
||||
url.append("&username=" + GEONAMES_USER);
|
||||
|
||||
URL request = new URL(url.toString());
|
||||
URLConnection connection = request.openConnection();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject obj = (JSONObject) parser.parse(in);
|
||||
JSONArray geonames = (JSONArray) obj.get("geonames");
|
||||
return geonames;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user