From 404044342a9fc9891366fa4378b4b1330cbca6c3 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 8 Nov 2017 15:43:13 +0500 Subject: - Implement synchronous geocoding - Implement retry geocoding for trips/stops reports - Implement API for revers geocoding --- src/org/traccar/BasePipelineFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org/traccar/BasePipelineFactory.java') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 771ab8acb..27b9678a3 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.getGeocoder() != null) { + if (Context.getConfig().getBoolean("geocoder.enable")) { geocoderHandler = new GeocoderHandler( Context.getGeocoder(), Context.getConfig().getBoolean("geocoder.processInvalidPositions")); -- cgit v1.2.3 From 96d89e2e352f5cba70a346b1f71bfb3bd399556b Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 10 Nov 2017 09:56:30 +0500 Subject: - Combine "getAddress" functions - Change switches logic --- src/org/traccar/BasePipelineFactory.java | 2 +- src/org/traccar/Context.java | 2 +- src/org/traccar/api/resource/ServerResource.java | 4 +- src/org/traccar/geocoder/Geocoder.java | 4 +- src/org/traccar/geocoder/JsonGeocoder.java | 95 +++++++++++------------- src/org/traccar/reports/ReportUtils.java | 15 ++-- test/org/traccar/geocoder/GeocoderTest.java | 26 ++++--- 7 files changed, 74 insertions(+), 74 deletions(-) (limited to 'src/org/traccar/BasePipelineFactory.java') 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); diff --git a/test/org/traccar/geocoder/GeocoderTest.java b/test/org/traccar/geocoder/GeocoderTest.java index 84e2fb2d6..7c6208048 100644 --- a/test/org/traccar/geocoder/GeocoderTest.java +++ b/test/org/traccar/geocoder/GeocoderTest.java @@ -1,5 +1,7 @@ package org.traccar.geocoder; +import java.util.Locale; + import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -9,6 +11,7 @@ public class GeocoderTest { @Ignore @Test public void test() throws InterruptedException { + Locale.setDefault(Locale.US); testGoogle(); } @@ -38,12 +41,13 @@ public class GeocoderTest { } @Override - public void onFailure(Throwable e) { + public void onFailure(final Throwable e) { } }); Assert.assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", waitAddress()); - Assert.assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", geocoder.getAddress(31.776797, 35.211489)); + Assert.assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", + geocoder.getAddress(31.776797, 35.211489, null)); } public void testNominatim() throws InterruptedException { @@ -59,9 +63,10 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - Assert.assertEquals("35 West 9th Street, NYC, New York, US", waitAddress()); + Assert.assertEquals("35 West 9th Street, NYC, New York, US", waitAddress()); - Assert.assertEquals("35 West 9th Street, NYC, New York, US", geocoder.getAddress(40.7337807, -73.9974401)); + Assert.assertEquals("35 West 9th Street, NYC, New York, US", + geocoder.getAddress(40.7337807, -73.9974401, null)); } public void testGisgraphy() throws InterruptedException { @@ -77,9 +82,9 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - Assert.assertEquals("Rue du Jardinet, Paris, FR", waitAddress()); + Assert.assertEquals("Rue du Jardinet, Paris, FR", waitAddress()); - Assert.assertEquals("Rue du Jardinet, Paris, FR", geocoder.getAddress(48.8530000, 2.3400000)); + Assert.assertEquals("Rue du Jardinet, Paris, FR", geocoder.getAddress(48.8530000, 2.3400000, null)); } public void testOpenCage() throws InterruptedException { @@ -96,9 +101,9 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - Assert.assertEquals("Charleston Road, California, US", waitAddress()); + Assert.assertEquals("Charleston Road, California, US", waitAddress()); - Assert.assertEquals("Charleston Road, California, US", geocoder.getAddress(34.116302, -118.051519)); + Assert.assertEquals("Charleston Road, California, US", geocoder.getAddress(34.116302, -118.051519, null)); } public void testGeocodeFarm() throws InterruptedException { @@ -114,9 +119,10 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - Assert.assertEquals("Estrella Avenue, Arcadia, California, United States", waitAddress()); + Assert.assertEquals("Estrella Avenue, Arcadia, California, United States", waitAddress()); - Assert.assertEquals("Estrella Avenue, Arcadia, California, United States", geocoder.getAddress(34.116302, -118.051519)); + Assert.assertEquals("Estrella Avenue, Arcadia, California, United States", + geocoder.getAddress(34.116302, -118.051519, null)); } } -- cgit v1.2.3