diff options
author | Shinryuken <watertext@hotmail.it> | 2017-11-19 11:22:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-19 11:22:31 +0100 |
commit | a5b273f8f8afd1a67613517c6487642601070456 (patch) | |
tree | babc63f5d50977fc91b3946b1da2e86ec74c51a5 /src/org/traccar/geocoder | |
parent | c7928ef4a722cb40e9911c8e76e308fc48c85567 (diff) | |
parent | fb9cbb2b74dc0060bd0dc5d0b3bfeb958ed6f3b5 (diff) | |
download | trackermap-server-a5b273f8f8afd1a67613517c6487642601070456.tar.gz trackermap-server-a5b273f8f8afd1a67613517c6487642601070456.tar.bz2 trackermap-server-a5b273f8f8afd1a67613517c6487642601070456.zip |
Merge branch 'master' into payload-as-form-param
Diffstat (limited to 'src/org/traccar/geocoder')
-rw-r--r-- | src/org/traccar/geocoder/BingMapsGeocoder.java | 5 | ||||
-rw-r--r-- | src/org/traccar/geocoder/FactualGeocoder.java | 5 | ||||
-rw-r--r-- | src/org/traccar/geocoder/GeocodeFarmGeocoder.java | 6 | ||||
-rw-r--r-- | src/org/traccar/geocoder/Geocoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/geocoder/GisgraphyGeocoder.java | 10 | ||||
-rw-r--r-- | src/org/traccar/geocoder/GoogleGeocoder.java | 6 | ||||
-rw-r--r-- | src/org/traccar/geocoder/JsonGeocoder.java | 85 | ||||
-rw-r--r-- | src/org/traccar/geocoder/MapQuestGeocoder.java | 5 | ||||
-rw-r--r-- | src/org/traccar/geocoder/NominatimGeocoder.java | 6 | ||||
-rw-r--r-- | src/org/traccar/geocoder/OpenCageGeocoder.java | 6 |
10 files changed, 85 insertions, 53 deletions
diff --git a/src/org/traccar/geocoder/BingMapsGeocoder.java b/src/org/traccar/geocoder/BingMapsGeocoder.java index a9b36219a..fbfb5394a 100644 --- a/src/org/traccar/geocoder/BingMapsGeocoder.java +++ b/src/org/traccar/geocoder/BingMapsGeocoder.java @@ -1,5 +1,6 @@ /* * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * Copyright 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +21,8 @@ import javax.json.JsonObject; public class BingMapsGeocoder extends JsonGeocoder { - public BingMapsGeocoder(String url, String key, int cacheSize) { - super(url + "/Locations/%f,%f?key=" + key + "&include=ciso2", cacheSize); + public BingMapsGeocoder(String url, String key, int cacheSize, AddressFormat addressFormat) { + super(url + "/Locations/%f,%f?key=" + key + "&include=ciso2", cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/FactualGeocoder.java b/src/org/traccar/geocoder/FactualGeocoder.java index 0c76e4625..c7a68c293 100644 --- a/src/org/traccar/geocoder/FactualGeocoder.java +++ b/src/org/traccar/geocoder/FactualGeocoder.java @@ -1,5 +1,6 @@ /* * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * Copyright 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +20,8 @@ import javax.json.JsonObject; public class FactualGeocoder extends JsonGeocoder { - public FactualGeocoder(String url, String key, int cacheSize) { - super(url + "?latitude=%f&longitude=%f&KEY=" + key, cacheSize); + public FactualGeocoder(String url, String key, int cacheSize, AddressFormat addressFormat) { + super(url + "?latitude=%f&longitude=%f&KEY=" + key, cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/GeocodeFarmGeocoder.java b/src/org/traccar/geocoder/GeocodeFarmGeocoder.java index 73ff85aa4..4fb956af9 100644 --- a/src/org/traccar/geocoder/GeocodeFarmGeocoder.java +++ b/src/org/traccar/geocoder/GeocodeFarmGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,8 +30,8 @@ public class GeocodeFarmGeocoder extends JsonGeocoder { } return url; } - public GeocodeFarmGeocoder(String key, String language, int cacheSize) { - super(formatUrl(key, language), cacheSize); + public GeocodeFarmGeocoder(String key, String language, int cacheSize, AddressFormat addressFormat) { + super(formatUrl(key, language), cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/Geocoder.java b/src/org/traccar/geocoder/Geocoder.java index 3ce3fb67f..587a27520 100644 --- a/src/org/traccar/geocoder/Geocoder.java +++ b/src/org/traccar/geocoder/Geocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2013 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,6 @@ public interface Geocoder { } - void getAddress(AddressFormat format, double latitude, double longitude, ReverseGeocoderCallback callback); + String getAddress(double latitude, double longitude, ReverseGeocoderCallback callback); } diff --git a/src/org/traccar/geocoder/GisgraphyGeocoder.java b/src/org/traccar/geocoder/GisgraphyGeocoder.java index 1432166e9..a0c831966 100644 --- a/src/org/traccar/geocoder/GisgraphyGeocoder.java +++ b/src/org/traccar/geocoder/GisgraphyGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,12 +19,12 @@ import javax.json.JsonObject; public class GisgraphyGeocoder extends JsonGeocoder { - public GisgraphyGeocoder() { - this("http://services.gisgraphy.com/reversegeocoding/search", 0); + public GisgraphyGeocoder(AddressFormat addressFormat) { + this("http://services.gisgraphy.com/reversegeocoding/search", 0, addressFormat); } - public GisgraphyGeocoder(String url, int cacheSize) { - super(url + "?format=json&lat=%f&lng=%f&from=1&to=1", cacheSize); + public GisgraphyGeocoder(String url, int cacheSize, AddressFormat addressFormat) { + super(url + "?format=json&lat=%f&lng=%f&from=1&to=1", cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/GoogleGeocoder.java b/src/org/traccar/geocoder/GoogleGeocoder.java index b38870c8f..235ea9ea9 100644 --- a/src/org/traccar/geocoder/GoogleGeocoder.java +++ b/src/org/traccar/geocoder/GoogleGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,8 +32,8 @@ public class GoogleGeocoder extends JsonGeocoder { return url; } - public GoogleGeocoder(String key, String language, int cacheSize) { - super(formatUrl(key, language), cacheSize); + public GoogleGeocoder(String key, String language, int cacheSize, AddressFormat addressFormat) { + super(formatUrl(key, language), cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java index 6d1380729..82a6ee604 100644 --- a/src/org/traccar/geocoder/JsonGeocoder.java +++ b/src/org/traccar/geocoder/JsonGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,23 +18,29 @@ package org.traccar.geocoder; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; import org.traccar.Context; +import org.traccar.helper.Log; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; + +import java.io.IOException; import java.util.AbstractMap; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ExecutionException; public abstract class JsonGeocoder implements Geocoder { private final String url; + private final AddressFormat addressFormat; private Map<Map.Entry<Double, Double>, String> cache; - public JsonGeocoder(String url, final int cacheSize) { + public JsonGeocoder(String url, final int cacheSize, AddressFormat addressFormat) { this.url = url; + this.addressFormat = addressFormat; if (cacheSize > 0) { this.cache = Collections.synchronizedMap(new LinkedHashMap<Map.Entry<Double, Double>, String>() { @Override @@ -45,43 +51,66 @@ public abstract class JsonGeocoder implements Geocoder { } } + private String handleResponse(double latitude, double longitude, Response response, + ReverseGeocoderCallback callback) throws IOException { + try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { + Address address = parseAddress(reader.readObject()); + if (address != null) { + String formattedAddress = addressFormat.format(address); + if (cache != null) { + cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); + } + if (callback != null) { + callback.onSuccess(formattedAddress); + } + return formattedAddress; + } else { + if (callback != null) { + callback.onFailure(new GeocoderException("Empty address")); + } + Log.warning("Empty address"); + } + } + return null; + } + @Override - public void getAddress( - final AddressFormat format, final double latitude, - final double longitude, final ReverseGeocoderCallback callback) { + public String getAddress( + final double latitude, final double longitude, final ReverseGeocoderCallback callback) { if (cache != null) { String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude)); if (cachedAddress != null) { - callback.onSuccess(cachedAddress); - return; + if (callback != null) { + callback.onSuccess(cachedAddress); + } + return cachedAddress; } } - Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude)) - .execute(new AsyncCompletionHandler() { - @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - Address address = parseAddress(reader.readObject()); - if (address != null) { - String formattedAddress = format.format(address); - if (cache != null) { - cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); - } - callback.onSuccess(formattedAddress); - } else { - callback.onFailure(new GeocoderException("Empty address")); - } + if (callback != null) { + Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude)) + .execute(new AsyncCompletionHandler() { + @Override + public Object onCompleted(Response response) throws Exception { + return handleResponse(latitude, longitude, response, callback); } - return null; - } - @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); + @Override + public void onThrowable(Throwable t) { + callback.onFailure(t); + } + }); + } else { + try { + Response response = Context.getAsyncHttpClient() + .prepareGet(String.format(url, latitude, longitude)).execute().get(); + return handleResponse(latitude, longitude, response, null); + } catch (InterruptedException | ExecutionException | IOException error) { + Log.warning("Geocoding failed", error); } - }); + } + return null; } public abstract Address parseAddress(JsonObject json); diff --git a/src/org/traccar/geocoder/MapQuestGeocoder.java b/src/org/traccar/geocoder/MapQuestGeocoder.java index 7d7217e91..4029e3f07 100644 --- a/src/org/traccar/geocoder/MapQuestGeocoder.java +++ b/src/org/traccar/geocoder/MapQuestGeocoder.java @@ -1,5 +1,6 @@ /* * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * Copyright 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +21,8 @@ import javax.json.JsonObject; public class MapQuestGeocoder extends JsonGeocoder { - public MapQuestGeocoder(String url, String key, int cacheSize) { - super(url + "?key=" + key + "&location=%f,%f", cacheSize); + public MapQuestGeocoder(String url, String key, int cacheSize, AddressFormat addressFormat) { + super(url + "?key=" + key + "&location=%f,%f", cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/NominatimGeocoder.java b/src/org/traccar/geocoder/NominatimGeocoder.java index 051727e93..75d2e9f99 100644 --- a/src/org/traccar/geocoder/NominatimGeocoder.java +++ b/src/org/traccar/geocoder/NominatimGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 - 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2014 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,8 +33,8 @@ public class NominatimGeocoder extends JsonGeocoder { return url; } - public NominatimGeocoder(String url, String key, String language, int cacheSize) { - super(formatUrl(url, key, language), cacheSize); + public NominatimGeocoder(String url, String key, String language, int cacheSize, AddressFormat addressFormat) { + super(formatUrl(url, key, language), cacheSize, addressFormat); } @Override diff --git a/src/org/traccar/geocoder/OpenCageGeocoder.java b/src/org/traccar/geocoder/OpenCageGeocoder.java index 9fa56a4a3..894397ee3 100644 --- a/src/org/traccar/geocoder/OpenCageGeocoder.java +++ b/src/org/traccar/geocoder/OpenCageGeocoder.java @@ -1,6 +1,6 @@ /* * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ import javax.json.JsonObject; public class OpenCageGeocoder extends JsonGeocoder { - public OpenCageGeocoder(String url, String key, int cacheSize) { - super(url + "/json?q=%f,%f&key=" + key, cacheSize); + public OpenCageGeocoder(String url, String key, int cacheSize, AddressFormat addressFormat) { + super(url + "/json?q=%f,%f&key=" + key, cacheSize, addressFormat); } @Override |