diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-05-01 02:33:11 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-05-01 02:33:15 +1200 |
commit | 403d8293e03a549fb479f08db9c83d9fe1232c9b (patch) | |
tree | e219340112f136de993cd2b40a1974010e3d5331 | |
parent | d5db101a0adaea03d0aa031911752d2e4353c9cf (diff) | |
download | trackermap-server-403d8293e03a549fb479f08db9c83d9fe1232c9b.tar.gz trackermap-server-403d8293e03a549fb479f08db9c83d9fe1232c9b.tar.bz2 trackermap-server-403d8293e03a549fb479f08db9c83d9fe1232c9b.zip |
Support geocode xyz reverse geocoder
-rw-r--r-- | src/org/traccar/Context.java | 16 | ||||
-rw-r--r-- | src/org/traccar/geocoder/GeocodeXyzGeocoder.java | 60 | ||||
-rw-r--r-- | test/org/traccar/geocoder/GeocoderTest.java | 33 |
3 files changed, 95 insertions, 14 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 30e6ca713..ee14eb1f7 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -23,6 +23,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Properties; +import com.ning.http.client.AsyncHttpClientConfigDefaults; import org.apache.velocity.app.VelocityEngine; import org.eclipse.jetty.util.URIUtil; import org.traccar.database.CalendarManager; @@ -49,6 +50,7 @@ import org.traccar.geocoder.AddressFormat; import org.traccar.geocoder.BingMapsGeocoder; import org.traccar.geocoder.FactualGeocoder; import org.traccar.geocoder.GeocodeFarmGeocoder; +import org.traccar.geocoder.GeocodeXyzGeocoder; import org.traccar.geocoder.GisgraphyGeocoder; import org.traccar.geocoder.GoogleGeocoder; import org.traccar.geocoder.MapQuestGeocoder; @@ -81,6 +83,9 @@ import org.traccar.web.WebServer; public final class Context { + private static final String USER_AGENT = + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"; + private Context() { } @@ -204,10 +209,15 @@ public final class Context { return velocityEngine; } - private static final AsyncHttpClient ASYNC_HTTP_CLIENT = new AsyncHttpClient(); + private static AsyncHttpClient asyncHttpClient; + + static { + System.setProperty(AsyncHttpClientConfigDefaults.ASYNC_CLIENT + "userAgent", USER_AGENT); + asyncHttpClient = new AsyncHttpClient(); + } public static AsyncHttpClient getAsyncHttpClient() { - return ASYNC_HTTP_CLIENT; + return asyncHttpClient; } private static EventForwarder eventForwarder; @@ -311,6 +321,8 @@ public final class Context { return new FactualGeocoder(url, key, cacheSize, addressFormat); case "geocodefarm": return new GeocodeFarmGeocoder(key, language, cacheSize, addressFormat); + case "geocodexyz": + return new GeocodeXyzGeocoder(key, cacheSize, addressFormat); default: return new GoogleGeocoder(key, language, cacheSize, addressFormat); } 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/test/org/traccar/geocoder/GeocoderTest.java b/test/org/traccar/geocoder/GeocoderTest.java index 8cf6ac524..12d8b4cd6 100644 --- a/test/org/traccar/geocoder/GeocoderTest.java +++ b/test/org/traccar/geocoder/GeocoderTest.java @@ -45,10 +45,8 @@ public class GeocoderTest { public void onFailure(final Throwable e) { } }); - assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", waitAddress()); - assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", - geocoder.getAddress(31.776797, 35.211489, null)); + assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", waitAddress()); } public void testNominatim() throws InterruptedException { @@ -64,10 +62,8 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - assertEquals("35 West 9th Street, NYC, New York, US", waitAddress()); - assertEquals("35 West 9th Street, NYC, New York, US", - geocoder.getAddress(40.7337807, -73.9974401, null)); + assertEquals("35 West 9th Street, NYC, New York, US", waitAddress()); } public void testGisgraphy() throws InterruptedException { @@ -83,9 +79,8 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - assertEquals("Rue du Jardinet, Paris, Île-de-France, FR", waitAddress()); - assertEquals("Rue du Jardinet, Paris, Île-de-France, FR", geocoder.getAddress(48.8530000, 2.3400000, null)); + assertEquals("Rue du Jardinet, Paris, Île-de-France, FR", waitAddress()); } public void testOpenCage() throws InterruptedException { @@ -102,9 +97,8 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - assertEquals("Charleston Road, California, US", waitAddress()); - assertEquals("Charleston Road, California, US", geocoder.getAddress(34.116302, -118.051519, null)); + assertEquals("Charleston Road, California, US", waitAddress()); } public void testGeocodeFarm() throws InterruptedException { @@ -120,10 +114,25 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); + assertEquals("Estrella Avenue, Arcadia, California, United States", waitAddress()); + } + + public void testGeocodeXyz() throws InterruptedException { + Geocoder geocoder = new GeocodeXyzGeocoder(null, 0, new AddressFormat()); + + geocoder.getAddress(34.116302, -118.051519, new Geocoder.ReverseGeocoderCallback() { + @Override + public void onSuccess(String address) { + setAddress(address); + } + + @Override + public void onFailure(Throwable e) { + } + }); - assertEquals("Estrella Avenue, Arcadia, California, United States", - geocoder.getAddress(34.116302, -118.051519, null)); + assertEquals("605 ESTRELLA AVE, ARCADIA, California United States of America, US", waitAddress()); } } |