aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocoder
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/geocoder')
-rw-r--r--src/org/traccar/geocoder/GeocodeXyzGeocoder.java60
-rw-r--r--src/org/traccar/geocoder/JsonGeocoder.java65
2 files changed, 90 insertions, 35 deletions
diff --git a/src/org/traccar/geocoder/GeocodeXyzGeocoder.java b/src/org/traccar/geocoder/GeocodeXyzGeocoder.java
new file mode 100644
index 000000000..aca360c3d
--- /dev/null
+++ b/src/org/traccar/geocoder/GeocodeXyzGeocoder.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2018 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.geocoder;
+
+import javax.json.JsonObject;
+
+public class GeocodeXyzGeocoder extends JsonGeocoder {
+
+ private static String formatUrl(String key) {
+ String url = "https://geocode.xyz/%f,%f?geoit=JSON";
+ if (key != null) {
+ url += "&key=" + key;
+ }
+ return url;
+ }
+
+ public GeocodeXyzGeocoder(String key, int cacheSize, AddressFormat addressFormat) {
+ super(formatUrl(key), cacheSize, addressFormat);
+ }
+
+ @Override
+ public Address parseAddress(JsonObject json) {
+ Address address = new Address();
+
+ if (json.containsKey("stnumber")) {
+ address.setHouse(json.getString("stnumber"));
+ }
+ if (json.containsKey("staddress")) {
+ address.setStreet(json.getString("staddress"));
+ }
+ if (json.containsKey("city")) {
+ address.setSettlement(json.getString("city"));
+ }
+ if (json.containsKey("region")) {
+ address.setState(json.getString("region"));
+ }
+ if (json.containsKey("prov")) {
+ address.setCountry(json.getString("prov"));
+ }
+ if (json.containsKey("postal")) {
+ address.setPostcode(json.getString("postal"));
+ }
+
+ return address;
+ }
+
+}
diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java
index 82a6ee604..36a3acb76 100644
--- a/src/org/traccar/geocoder/JsonGeocoder.java
+++ b/src/org/traccar/geocoder/JsonGeocoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2018 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.
@@ -15,21 +15,17 @@
*/
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 javax.ws.rs.ClientErrorException;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.InvocationCallback;
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 {
@@ -51,23 +47,23 @@ 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;
+ private String handleResponse(
+ double latitude, double longitude, JsonObject json, ReverseGeocoderCallback callback) {
+
+ Address address = parseAddress(json);
+ 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"));
} else {
- if (callback != null) {
- callback.onFailure(new GeocoderException("Empty address"));
- }
Log.warning("Empty address");
}
}
@@ -88,26 +84,25 @@ public abstract class JsonGeocoder implements Geocoder {
}
}
+ Invocation.Builder request = Context.getClient().target(String.format(url, latitude, longitude)).request();
+
if (callback != null) {
- Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude))
- .execute(new AsyncCompletionHandler() {
+ request.async().get(new InvocationCallback<JsonObject>() {
@Override
- public Object onCompleted(Response response) throws Exception {
- return handleResponse(latitude, longitude, response, callback);
+ public void completed(JsonObject json) {
+ handleResponse(latitude, longitude, json, callback);
}
@Override
- public void onThrowable(Throwable t) {
- callback.onFailure(t);
+ public void failed(Throwable throwable) {
+ callback.onFailure(throwable);
}
});
} 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 handleResponse(latitude, longitude, request.get(JsonObject.class), null);
+ } catch (ClientErrorException e) {
+ Log.warning(e);
}
}
return null;