diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 2 | ||||
-rw-r--r-- | src/org/traccar/Context.java | 2 | ||||
-rw-r--r-- | src/org/traccar/api/resource/ServerResource.java | 4 | ||||
-rw-r--r-- | src/org/traccar/geocoder/Geocoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/geocoder/JsonGeocoder.java | 95 | ||||
-rw-r--r-- | src/org/traccar/reports/ReportUtils.java | 15 |
6 files changed, 58 insertions, 64 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 27b9678a3..aa188d209 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -141,7 +141,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { filterHandler = new FilterHandler(); } - if (Context.getConfig().getBoolean("geocoder.enable")) { + if (Context.getGeocoder() != null && !Context.getConfig().getBoolean("geocoder.ignorePositions")) { geocoderHandler = new GeocoderHandler( Context.getGeocoder(), Context.getConfig().getBoolean("geocoder.processInvalidPositions")); diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 0671f1d29..237da40c8 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -333,7 +333,7 @@ public final class Context { identityManager = deviceManager; - if (config.getBoolean("geocoder.enable") || config.getBoolean("report.retryGeocoding")) { + if (config.getBoolean("geocoder.enable")) { geocoder = initGeocoder(); } diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java index b803c3380..c0914995b 100644 --- a/src/org/traccar/api/resource/ServerResource.java +++ b/src/org/traccar/api/resource/ServerResource.java @@ -48,10 +48,10 @@ public class ServerResource extends BaseResource { return Response.ok(entity).build(); } - @Path("geocoding") + @Path("geocode") @GET public String geocode(@QueryParam("latitude") double latitude, @QueryParam("longitude") double longitude) { - return Context.getGeocoder().getAddress(latitude, longitude); + return Context.getGeocoder().getAddress(latitude, longitude, null); } } diff --git a/src/org/traccar/geocoder/Geocoder.java b/src/org/traccar/geocoder/Geocoder.java index c3fa394c7..587a27520 100644 --- a/src/org/traccar/geocoder/Geocoder.java +++ b/src/org/traccar/geocoder/Geocoder.java @@ -25,8 +25,6 @@ public interface Geocoder { } - void getAddress(double latitude, double longitude, ReverseGeocoderCallback callback); - - String getAddress(double latitude, double longitude); + String getAddress(double latitude, double longitude, ReverseGeocoderCallback callback); } diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java index 2ae1fe5aa..82a6ee604 100644 --- a/src/org/traccar/geocoder/JsonGeocoder.java +++ b/src/org/traccar/geocoder/JsonGeocoder.java @@ -28,7 +28,6 @@ import java.io.IOException; import java.util.AbstractMap; import java.util.Collections; import java.util.LinkedHashMap; -import java.util.Locale; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -52,70 +51,64 @@ public abstract class JsonGeocoder implements Geocoder { } } - @Override - public void 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; - } - } - - Context.getAsyncHttpClient().prepareGet(String.format(Locale.US, 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 = addressFormat.format(address); - if (cache != null) { - cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); - } - callback.onSuccess(formattedAddress); - } else { - callback.onFailure(new GeocoderException("Empty address")); - } + 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); } - return null; - } - - @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); + 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 String getAddress(final double latitude, final double longitude) { + 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) { + if (callback != null) { + callback.onSuccess(cachedAddress); + } return cachedAddress; } } - try { - Response response = Context.getAsyncHttpClient() - .prepareGet(String.format(Locale.US, url, latitude, longitude)).execute().get(); - 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); - } - return formattedAddress; - } else { - Log.warning("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); } + + @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); } - } catch (InterruptedException | ExecutionException | IOException error) { - Log.warning("Geocoding failed", error); } return null; } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index f0d6d40c2..e04f2f90c 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -191,8 +191,9 @@ public final class ReportUtils { trip.setStartLon(startTrip.getLongitude()); trip.setStartTime(startTrip.getFixTime()); String startAddress = startTrip.getAddress(); - if (startAddress == null && Context.getConfig().getBoolean("report.retryGeocoding")) { - startAddress = Context.getGeocoder().getAddress(startTrip.getLatitude(), startTrip.getLongitude()); + if (startAddress == null && Context.getGeocoder() != null + && Context.getConfig().getBoolean("geocoder.onRequest")) { + startAddress = Context.getGeocoder().getAddress(startTrip.getLatitude(), startTrip.getLongitude(), null); } trip.setStartAddress(startAddress); @@ -201,8 +202,9 @@ public final class ReportUtils { trip.setEndLon(endTrip.getLongitude()); trip.setEndTime(endTrip.getFixTime()); String endAddress = endTrip.getAddress(); - if (endAddress == null && Context.getConfig().getBoolean("report.retryGeocoding")) { - endAddress = Context.getGeocoder().getAddress(startTrip.getLatitude(), startTrip.getLongitude()); + if (endAddress == null && Context.getGeocoder() != null + && Context.getConfig().getBoolean("geocoder.onRequest")) { + endAddress = Context.getGeocoder().getAddress(endTrip.getLatitude(), endTrip.getLongitude(), null); } trip.setEndAddress(endAddress); @@ -233,8 +235,9 @@ public final class ReportUtils { stop.setLongitude(startStop.getLongitude()); stop.setStartTime(startStop.getFixTime()); String address = startStop.getAddress(); - if (address == null && Context.getConfig().getBoolean("report.retryGeocoding")) { - address = Context.getGeocoder().getAddress(stop.getLatitude(), stop.getLongitude()); + if (address == null && Context.getGeocoder() != null + && Context.getConfig().getBoolean("geocoder.onRequest")) { + address = Context.getGeocoder().getAddress(stop.getLatitude(), stop.getLongitude(), null); } stop.setAddress(address); |