aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocoder
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-11-08 15:43:13 +0500
committerAbyss777 <abyss@fox5.ru>2017-11-08 15:43:13 +0500
commit404044342a9fc9891366fa4378b4b1330cbca6c3 (patch)
tree98a2bfc2b6998e006f874d30ee9976c68345fee1 /src/org/traccar/geocoder
parent92f00d0da0dafd08ccf2a623406963e61fcde276 (diff)
downloadtraccar-server-404044342a9fc9891366fa4378b4b1330cbca6c3.tar.gz
traccar-server-404044342a9fc9891366fa4378b4b1330cbca6c3.tar.bz2
traccar-server-404044342a9fc9891366fa4378b4b1330cbca6c3.zip
- Implement synchronous geocoding
- Implement retry geocoding for trips/stops reports - Implement API for revers geocoding
Diffstat (limited to 'src/org/traccar/geocoder')
-rw-r--r--src/org/traccar/geocoder/BingMapsGeocoder.java5
-rw-r--r--src/org/traccar/geocoder/FactualGeocoder.java5
-rw-r--r--src/org/traccar/geocoder/GeocodeFarmGeocoder.java6
-rw-r--r--src/org/traccar/geocoder/Geocoder.java6
-rw-r--r--src/org/traccar/geocoder/GisgraphyGeocoder.java10
-rw-r--r--src/org/traccar/geocoder/GoogleGeocoder.java6
-rw-r--r--src/org/traccar/geocoder/JsonGeocoder.java48
-rw-r--r--src/org/traccar/geocoder/MapQuestGeocoder.java5
-rw-r--r--src/org/traccar/geocoder/NominatimGeocoder.java6
-rw-r--r--src/org/traccar/geocoder/OpenCageGeocoder.java6
10 files changed, 72 insertions, 31 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..c3fa394c7 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,8 @@ public interface Geocoder {
}
- void getAddress(AddressFormat format, double latitude, double longitude, ReverseGeocoderCallback callback);
+ void getAddress(double latitude, double longitude, ReverseGeocoderCallback callback);
+
+ String getAddress(double latitude, double longitude);
}
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..2ae1fe5aa 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,30 @@ 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.Locale;
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
@@ -47,8 +54,7 @@ public abstract class JsonGeocoder implements Geocoder {
@Override
public void getAddress(
- final AddressFormat format, final double latitude,
- final double longitude, final ReverseGeocoderCallback callback) {
+ final double latitude, final double longitude, final ReverseGeocoderCallback callback) {
if (cache != null) {
String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude));
@@ -58,14 +64,14 @@ public abstract class JsonGeocoder implements Geocoder {
}
}
- Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude))
+ 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 = format.format(address);
+ String formattedAddress = addressFormat.format(address);
if (cache != null) {
cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress);
}
@@ -84,6 +90,36 @@ public abstract class JsonGeocoder implements Geocoder {
});
}
+ @Override
+ public String getAddress(final double latitude, final double longitude) {
+ if (cache != null) {
+ String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude));
+ if (cachedAddress != null) {
+ 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");
+ }
+ }
+ } 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