From 679c2852e3c4987091e0b84b7e7d80a9f72964a0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 31 Dec 2016 11:41:56 +1300 Subject: Add exception classes and more renames --- src/org/traccar/BasePipelineFactory.java | 20 ++--- src/org/traccar/Context.java | 46 +++++------ src/org/traccar/GeocoderHandler.java | 80 +++++++++++++++++++ src/org/traccar/GeolocationHandler.java | 76 ++++++++++++++++++ src/org/traccar/LocationProviderHandler.java | 76 ------------------ src/org/traccar/ReverseGeocoderHandler.java | 80 ------------------- src/org/traccar/geocoder/BingMapsGeocoder.java | 59 ++++++++++++++ .../traccar/geocoder/BingMapsReverseGeocoder.java | 59 -------------- src/org/traccar/geocoder/FactualGeocoder.java | 57 ++++++++++++++ .../traccar/geocoder/FactualReverseGeocoder.java | 57 -------------- src/org/traccar/geocoder/GeocodeFarmGeocoder.java | 61 +++++++++++++++ .../geocoder/GeocodeFarmReverseGeocoder.java | 61 --------------- src/org/traccar/geocoder/Geocoder.java | 30 ++++++++ src/org/traccar/geocoder/GeocoderException.java | 24 ++++++ src/org/traccar/geocoder/GisgraphyGeocoder.java | 52 +++++++++++++ .../traccar/geocoder/GisgraphyReverseGeocoder.java | 52 ------------- src/org/traccar/geocoder/GoogleGeocoder.java | 86 +++++++++++++++++++++ .../traccar/geocoder/GoogleReverseGeocoder.java | 86 --------------------- src/org/traccar/geocoder/JsonGeocoder.java | 89 ++++++++++++++++++++++ src/org/traccar/geocoder/JsonReverseGeocoder.java | 89 ---------------------- src/org/traccar/geocoder/MapQuestGeocoder.java | 62 +++++++++++++++ .../traccar/geocoder/MapQuestReverseGeocoder.java | 62 --------------- src/org/traccar/geocoder/NominatimGeocoder.java | 81 ++++++++++++++++++++ .../traccar/geocoder/NominatimReverseGeocoder.java | 81 -------------------- src/org/traccar/geocoder/OpenCageGeocoder.java | 73 ++++++++++++++++++ .../traccar/geocoder/OpenCageReverseGeocoder.java | 73 ------------------ src/org/traccar/geocoder/ReverseGeocoder.java | 30 -------- .../traccar/geolocation/GeolocationException.java | 24 ++++++ .../geolocation/OpenCellIdGeolocationProvider.java | 4 +- .../geolocation/UniversalGeolocationProvider.java | 3 +- 30 files changed, 891 insertions(+), 842 deletions(-) create mode 100644 src/org/traccar/GeocoderHandler.java create mode 100644 src/org/traccar/GeolocationHandler.java delete mode 100644 src/org/traccar/LocationProviderHandler.java delete mode 100644 src/org/traccar/ReverseGeocoderHandler.java create mode 100644 src/org/traccar/geocoder/BingMapsGeocoder.java delete mode 100644 src/org/traccar/geocoder/BingMapsReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/FactualGeocoder.java delete mode 100644 src/org/traccar/geocoder/FactualReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/GeocodeFarmGeocoder.java delete mode 100644 src/org/traccar/geocoder/GeocodeFarmReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/Geocoder.java create mode 100644 src/org/traccar/geocoder/GeocoderException.java create mode 100644 src/org/traccar/geocoder/GisgraphyGeocoder.java delete mode 100644 src/org/traccar/geocoder/GisgraphyReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/GoogleGeocoder.java delete mode 100644 src/org/traccar/geocoder/GoogleReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/JsonGeocoder.java delete mode 100644 src/org/traccar/geocoder/JsonReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/MapQuestGeocoder.java delete mode 100644 src/org/traccar/geocoder/MapQuestReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/NominatimGeocoder.java delete mode 100644 src/org/traccar/geocoder/NominatimReverseGeocoder.java create mode 100644 src/org/traccar/geocoder/OpenCageGeocoder.java delete mode 100644 src/org/traccar/geocoder/OpenCageReverseGeocoder.java delete mode 100644 src/org/traccar/geocoder/ReverseGeocoder.java create mode 100644 src/org/traccar/geolocation/GeolocationException.java (limited to 'src') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 94a6d85aa..d949c9c9c 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -48,8 +48,8 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private FilterHandler filterHandler; private CoordinatesHandler coordinatesHandler; private DistanceHandler distanceHandler; - private ReverseGeocoderHandler reverseGeocoderHandler; - private LocationProviderHandler locationProviderHandler; + private GeocoderHandler geocoderHandler; + private GeolocationHandler geolocationHandler; private HemisphereHandler hemisphereHandler; private CopyAttributesHandler copyAttributesHandler; @@ -125,13 +125,13 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { coordinatesHandler = new CoordinatesHandler(); } - if (Context.getReverseGeocoder() != null) { - reverseGeocoderHandler = new ReverseGeocoderHandler( - Context.getReverseGeocoder(), Context.getConfig().getBoolean("geocoder.processInvalidPositions")); + if (Context.getGeocoder() != null) { + geocoderHandler = new GeocoderHandler( + Context.getGeocoder(), Context.getConfig().getBoolean("geocoder.processInvalidPositions")); } if (Context.getGeolocationProvider() != null) { - locationProviderHandler = new LocationProviderHandler( + geolocationHandler = new GeolocationHandler( Context.getGeolocationProvider(), Context.getConfig().getBoolean("location.processInvalidPositions")); } @@ -189,11 +189,11 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (hemisphereHandler != null) { pipeline.addLast("hemisphere", hemisphereHandler); } - if (reverseGeocoderHandler != null) { - pipeline.addLast("geocoder", reverseGeocoderHandler); + if (geocoderHandler != null) { + pipeline.addLast("geocoder", geocoderHandler); } - if (locationProviderHandler != null) { - pipeline.addLast("location", locationProviderHandler); + if (geolocationHandler != null) { + pipeline.addLast("location", geolocationHandler); } pipeline.addLast("remoteAddress", new RemoteAddressHandler()); diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 72d5dd9f8..46294edfa 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -34,15 +34,15 @@ import org.traccar.database.NotificationManager; import org.traccar.database.PermissionsManager; import org.traccar.database.GeofenceManager; import org.traccar.database.StatisticsManager; -import org.traccar.geocoder.BingMapsReverseGeocoder; -import org.traccar.geocoder.FactualReverseGeocoder; -import org.traccar.geocoder.GeocodeFarmReverseGeocoder; -import org.traccar.geocoder.GisgraphyReverseGeocoder; -import org.traccar.geocoder.GoogleReverseGeocoder; -import org.traccar.geocoder.MapQuestReverseGeocoder; -import org.traccar.geocoder.NominatimReverseGeocoder; -import org.traccar.geocoder.OpenCageReverseGeocoder; -import org.traccar.geocoder.ReverseGeocoder; +import org.traccar.geocoder.BingMapsGeocoder; +import org.traccar.geocoder.FactualGeocoder; +import org.traccar.geocoder.GeocodeFarmGeocoder; +import org.traccar.geocoder.GisgraphyGeocoder; +import org.traccar.geocoder.GoogleGeocoder; +import org.traccar.geocoder.MapQuestGeocoder; +import org.traccar.geocoder.NominatimGeocoder; +import org.traccar.geocoder.OpenCageGeocoder; +import org.traccar.geocoder.Geocoder; import org.traccar.helper.Log; import org.traccar.geolocation.GoogleGeolocationProvider; import org.traccar.geolocation.GeolocationProvider; @@ -104,10 +104,10 @@ public final class Context { return permissionsManager; } - private static ReverseGeocoder reverseGeocoder; + private static Geocoder geocoder; - public static ReverseGeocoder getReverseGeocoder() { - return reverseGeocoder; + public static Geocoder getGeocoder() { + return geocoder; } private static GeolocationProvider geolocationProvider; @@ -213,37 +213,37 @@ public final class Context { switch (type) { case "nominatim": if (key != null) { - reverseGeocoder = new NominatimReverseGeocoder(url, key, cacheSize); + geocoder = new NominatimGeocoder(url, key, cacheSize); } else { - reverseGeocoder = new NominatimReverseGeocoder(url, cacheSize); + geocoder = new NominatimGeocoder(url, cacheSize); } break; case "gisgraphy": - reverseGeocoder = new GisgraphyReverseGeocoder(url, cacheSize); + geocoder = new GisgraphyGeocoder(url, cacheSize); break; case "mapquest": - reverseGeocoder = new MapQuestReverseGeocoder(url, key, cacheSize); + geocoder = new MapQuestGeocoder(url, key, cacheSize); break; case "opencage": - reverseGeocoder = new OpenCageReverseGeocoder(url, key, cacheSize); + geocoder = new OpenCageGeocoder(url, key, cacheSize); break; case "bingmaps": - reverseGeocoder = new BingMapsReverseGeocoder(url, key, cacheSize); + geocoder = new BingMapsGeocoder(url, key, cacheSize); break; case "factual": - reverseGeocoder = new FactualReverseGeocoder(url, key, cacheSize); + geocoder = new FactualGeocoder(url, key, cacheSize); break; case "geocodefarm": if (key != null) { - reverseGeocoder = new GeocodeFarmReverseGeocoder(key, cacheSize); + geocoder = new GeocodeFarmGeocoder(key, cacheSize); } else { - reverseGeocoder = new GeocodeFarmReverseGeocoder(cacheSize); + geocoder = new GeocodeFarmGeocoder(cacheSize); } default: if (key != null) { - reverseGeocoder = new GoogleReverseGeocoder(key, cacheSize); + geocoder = new GoogleGeocoder(key, cacheSize); } else { - reverseGeocoder = new GoogleReverseGeocoder(cacheSize); + geocoder = new GoogleGeocoder(cacheSize); } break; } diff --git a/src/org/traccar/GeocoderHandler.java b/src/org/traccar/GeocoderHandler.java new file mode 100644 index 000000000..8933cadac --- /dev/null +++ b/src/org/traccar/GeocoderHandler.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012 - 2016 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; + +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelUpstreamHandler; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.MessageEvent; +import org.traccar.geocoder.AddressFormat; +import org.traccar.geocoder.Geocoder; +import org.traccar.helper.Log; +import org.traccar.model.Position; + +public class GeocoderHandler implements ChannelUpstreamHandler { + + private final Geocoder geocoder; + private final boolean processInvalidPositions; + private final AddressFormat addressFormat; + + public GeocoderHandler(Geocoder geocoder, boolean processInvalidPositions) { + this.geocoder = geocoder; + this.processInvalidPositions = processInvalidPositions; + + String formatString = Context.getConfig().getString("geocoder.format"); + if (formatString != null) { + addressFormat = new AddressFormat(formatString); + } else { + addressFormat = new AddressFormat(); + } + } + + @Override + public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { + if (!(evt instanceof MessageEvent)) { + ctx.sendUpstream(evt); + return; + } + + final MessageEvent event = (MessageEvent) evt; + Object message = event.getMessage(); + if (message instanceof Position) { + final Position position = (Position) message; + if (processInvalidPositions || position.getValid()) { + geocoder.getAddress(addressFormat, position.getLatitude(), position.getLongitude(), + new Geocoder.ReverseGeocoderCallback() { + @Override + public void onSuccess(String address) { + position.setAddress(address); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + + @Override + public void onFailure(Throwable e) { + Log.warning("Geocoding failed", e); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + }); + } else { + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + } else { + Channels.fireMessageReceived(ctx, message, event.getRemoteAddress()); + } + } + +} diff --git a/src/org/traccar/GeolocationHandler.java b/src/org/traccar/GeolocationHandler.java new file mode 100644 index 000000000..1e9bd6772 --- /dev/null +++ b/src/org/traccar/GeolocationHandler.java @@ -0,0 +1,76 @@ +/* + * Copyright 2015 - 2016 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; + +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelUpstreamHandler; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.MessageEvent; +import org.traccar.helper.Log; +import org.traccar.geolocation.GeolocationProvider; +import org.traccar.model.Position; + +public class GeolocationHandler implements ChannelUpstreamHandler { + + private final GeolocationProvider geolocationProvider; + private final boolean processInvalidPositions; + + public GeolocationHandler(GeolocationProvider geolocationProvider, boolean processInvalidPositions) { + this.geolocationProvider = geolocationProvider; + this.processInvalidPositions = processInvalidPositions; + } + + @Override + public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { + if (!(evt instanceof MessageEvent)) { + ctx.sendUpstream(evt); + return; + } + + final MessageEvent event = (MessageEvent) evt; + Object message = event.getMessage(); + if (message instanceof Position) { + final Position position = (Position) message; + if ((position.getOutdated() || processInvalidPositions && !position.getValid()) + && position.getNetwork() != null) { + geolocationProvider.getLocation(position.getNetwork(), new GeolocationProvider.LocationProviderCallback() { + @Override + public void onSuccess(double latitude, double longitude, double accuracy) { + position.set(Position.KEY_APPROXIMATE, true); + position.setValid(true); + position.setFixTime(position.getDeviceTime()); + position.setLatitude(latitude); + position.setLongitude(longitude); + position.setAccuracy(accuracy); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + + @Override + public void onFailure(Throwable e) { + Log.warning(e); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + }); + } else { + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + } else { + Channels.fireMessageReceived(ctx, message, event.getRemoteAddress()); + } + } + +} diff --git a/src/org/traccar/LocationProviderHandler.java b/src/org/traccar/LocationProviderHandler.java deleted file mode 100644 index bf37a8b88..000000000 --- a/src/org/traccar/LocationProviderHandler.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2015 - 2016 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; - -import org.jboss.netty.channel.ChannelEvent; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelUpstreamHandler; -import org.jboss.netty.channel.Channels; -import org.jboss.netty.channel.MessageEvent; -import org.traccar.helper.Log; -import org.traccar.geolocation.GeolocationProvider; -import org.traccar.model.Position; - -public class LocationProviderHandler implements ChannelUpstreamHandler { - - private final GeolocationProvider geolocationProvider; - private final boolean processInvalidPositions; - - public LocationProviderHandler(GeolocationProvider geolocationProvider, boolean processInvalidPositions) { - this.geolocationProvider = geolocationProvider; - this.processInvalidPositions = processInvalidPositions; - } - - @Override - public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { - if (!(evt instanceof MessageEvent)) { - ctx.sendUpstream(evt); - return; - } - - final MessageEvent event = (MessageEvent) evt; - Object message = event.getMessage(); - if (message instanceof Position) { - final Position position = (Position) message; - if ((position.getOutdated() || processInvalidPositions && !position.getValid()) - && position.getNetwork() != null) { - geolocationProvider.getLocation(position.getNetwork(), new GeolocationProvider.LocationProviderCallback() { - @Override - public void onSuccess(double latitude, double longitude, double accuracy) { - position.set(Position.KEY_APPROXIMATE, true); - position.setValid(true); - position.setFixTime(position.getDeviceTime()); - position.setLatitude(latitude); - position.setLongitude(longitude); - position.setAccuracy(accuracy); - Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); - } - - @Override - public void onFailure(Throwable e) { - Log.warning("Geolocation failed", e); - Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); - } - }); - } else { - Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); - } - } else { - Channels.fireMessageReceived(ctx, message, event.getRemoteAddress()); - } - } - -} diff --git a/src/org/traccar/ReverseGeocoderHandler.java b/src/org/traccar/ReverseGeocoderHandler.java deleted file mode 100644 index 5813c1136..000000000 --- a/src/org/traccar/ReverseGeocoderHandler.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2012 - 2016 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; - -import org.jboss.netty.channel.ChannelEvent; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelUpstreamHandler; -import org.jboss.netty.channel.Channels; -import org.jboss.netty.channel.MessageEvent; -import org.traccar.geocoder.AddressFormat; -import org.traccar.geocoder.ReverseGeocoder; -import org.traccar.helper.Log; -import org.traccar.model.Position; - -public class ReverseGeocoderHandler implements ChannelUpstreamHandler { - - private final ReverseGeocoder geocoder; - private final boolean processInvalidPositions; - private final AddressFormat addressFormat; - - public ReverseGeocoderHandler(ReverseGeocoder geocoder, boolean processInvalidPositions) { - this.geocoder = geocoder; - this.processInvalidPositions = processInvalidPositions; - - String formatString = Context.getConfig().getString("geocoder.format"); - if (formatString != null) { - addressFormat = new AddressFormat(formatString); - } else { - addressFormat = new AddressFormat(); - } - } - - @Override - public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { - if (!(evt instanceof MessageEvent)) { - ctx.sendUpstream(evt); - return; - } - - final MessageEvent event = (MessageEvent) evt; - Object message = event.getMessage(); - if (message instanceof Position) { - final Position position = (Position) message; - if (processInvalidPositions || position.getValid()) { - geocoder.getAddress(addressFormat, position.getLatitude(), position.getLongitude(), - new ReverseGeocoder.ReverseGeocoderCallback() { - @Override - public void onSuccess(String address) { - position.setAddress(address); - Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); - } - - @Override - public void onFailure(Throwable e) { - Log.warning("Geocoding failed", e); - Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); - } - }); - } else { - Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); - } - } else { - Channels.fireMessageReceived(ctx, message, event.getRemoteAddress()); - } - } - -} diff --git a/src/org/traccar/geocoder/BingMapsGeocoder.java b/src/org/traccar/geocoder/BingMapsGeocoder.java new file mode 100644 index 000000000..a9b36219a --- /dev/null +++ b/src/org/traccar/geocoder/BingMapsGeocoder.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * + * 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.JsonArray; +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); + } + + @Override + public Address parseAddress(JsonObject json) { + JsonArray result = json.getJsonArray("resourceSets"); + if (result != null) { + JsonObject location = + result.getJsonObject(0).getJsonArray("resources").getJsonObject(0).getJsonObject("address"); + if (location != null) { + Address address = new Address(); + if (location.containsKey("addressLine")) { + address.setStreet(location.getString("addressLine")); + } + if (location.containsKey("locality")) { + address.setSettlement(location.getString("locality")); + } + if (location.containsKey("adminDistrict2")) { + address.setDistrict(location.getString("adminDistrict2")); + } + if (location.containsKey("adminDistrict")) { + address.setState(location.getString("adminDistrict")); + } + if (location.containsKey("countryRegionIso2")) { + address.setCountry(location.getString("countryRegionIso2").toUpperCase()); + } + if (location.containsKey("postalCode")) { + address.setPostcode(location.getString("postalCode")); + } + return address; + } + } + return null; + } + +} diff --git a/src/org/traccar/geocoder/BingMapsReverseGeocoder.java b/src/org/traccar/geocoder/BingMapsReverseGeocoder.java deleted file mode 100644 index 46354d06a..000000000 --- a/src/org/traccar/geocoder/BingMapsReverseGeocoder.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) - * - * 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.JsonArray; -import javax.json.JsonObject; - -public class BingMapsReverseGeocoder extends JsonReverseGeocoder { - - public BingMapsReverseGeocoder(String url, String key, int cacheSize) { - super(url + "/Locations/%f,%f?key=" + key + "&include=ciso2", cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - JsonArray result = json.getJsonArray("resourceSets"); - if (result != null) { - JsonObject location = - result.getJsonObject(0).getJsonArray("resources").getJsonObject(0).getJsonObject("address"); - if (location != null) { - Address address = new Address(); - if (location.containsKey("addressLine")) { - address.setStreet(location.getString("addressLine")); - } - if (location.containsKey("locality")) { - address.setSettlement(location.getString("locality")); - } - if (location.containsKey("adminDistrict2")) { - address.setDistrict(location.getString("adminDistrict2")); - } - if (location.containsKey("adminDistrict")) { - address.setState(location.getString("adminDistrict")); - } - if (location.containsKey("countryRegionIso2")) { - address.setCountry(location.getString("countryRegionIso2").toUpperCase()); - } - if (location.containsKey("postalCode")) { - address.setPostcode(location.getString("postalCode")); - } - return address; - } - } - return null; - } - -} diff --git a/src/org/traccar/geocoder/FactualGeocoder.java b/src/org/traccar/geocoder/FactualGeocoder.java new file mode 100644 index 000000000..0c76e4625 --- /dev/null +++ b/src/org/traccar/geocoder/FactualGeocoder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * + * 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 FactualGeocoder extends JsonGeocoder { + + public FactualGeocoder(String url, String key, int cacheSize) { + super(url + "?latitude=%f&longitude=%f&KEY=" + key, cacheSize); + } + + @Override + public Address parseAddress(JsonObject json) { + JsonObject result = json.getJsonObject("response").getJsonObject("data"); + if (result != null) { + Address address = new Address(); + if (result.getJsonObject("street_number") != null) { + address.setHouse(result.getJsonObject("street_number").getString("name")); + } + if (result.getJsonObject("street_name") != null) { + address.setStreet(result.getJsonObject("street_name").getString("name")); + } + if (result.getJsonObject("locality") != null) { + address.setSettlement(result.getJsonObject("locality").getString("name")); + } + if (result.getJsonObject("county") != null) { + address.setDistrict(result.getJsonObject("county").getString("name")); + } + if (result.getJsonObject("region") != null) { + address.setState(result.getJsonObject("region").getString("name")); + } + if (result.getJsonObject("country") != null) { + address.setCountry(result.getJsonObject("country").getString("name")); + } + if (result.getJsonObject("postcode") != null) { + address.setPostcode(result.getJsonObject("postcode").getString("name")); + } + return address; + } + return null; + } + +} diff --git a/src/org/traccar/geocoder/FactualReverseGeocoder.java b/src/org/traccar/geocoder/FactualReverseGeocoder.java deleted file mode 100644 index f1137893e..000000000 --- a/src/org/traccar/geocoder/FactualReverseGeocoder.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) - * - * 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 FactualReverseGeocoder extends JsonReverseGeocoder { - - public FactualReverseGeocoder(String url, String key, int cacheSize) { - super(url + "?latitude=%f&longitude=%f&KEY=" + key, cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - JsonObject result = json.getJsonObject("response").getJsonObject("data"); - if (result != null) { - Address address = new Address(); - if (result.getJsonObject("street_number") != null) { - address.setHouse(result.getJsonObject("street_number").getString("name")); - } - if (result.getJsonObject("street_name") != null) { - address.setStreet(result.getJsonObject("street_name").getString("name")); - } - if (result.getJsonObject("locality") != null) { - address.setSettlement(result.getJsonObject("locality").getString("name")); - } - if (result.getJsonObject("county") != null) { - address.setDistrict(result.getJsonObject("county").getString("name")); - } - if (result.getJsonObject("region") != null) { - address.setState(result.getJsonObject("region").getString("name")); - } - if (result.getJsonObject("country") != null) { - address.setCountry(result.getJsonObject("country").getString("name")); - } - if (result.getJsonObject("postcode") != null) { - address.setPostcode(result.getJsonObject("postcode").getString("name")); - } - return address; - } - return null; - } - -} diff --git a/src/org/traccar/geocoder/GeocodeFarmGeocoder.java b/src/org/traccar/geocoder/GeocodeFarmGeocoder.java new file mode 100644 index 000000000..585095606 --- /dev/null +++ b/src/org/traccar/geocoder/GeocodeFarmGeocoder.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016 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 GeocodeFarmGeocoder extends JsonGeocoder { + + private static final String URL = "https://www.geocode.farm/v3/json/reverse/"; + + public GeocodeFarmGeocoder(int cacheSize) { + super(URL + "?lat=%f&lon=%f&country=us&lang=en&count=1", cacheSize); + } + + public GeocodeFarmGeocoder(String key, int cacheSize) { + super(URL + "?lat=%f&lon=%f&country=us&lang=en&count=1&key=" + key, cacheSize); + } + + @Override + public Address parseAddress(JsonObject json) { + Address address = new Address(); + + JsonObject result = json + .getJsonObject("geocoding_results") + .getJsonArray("RESULTS") + .getJsonObject(0) + .getJsonObject("ADDRESS"); + + if (result.containsKey("street_number")) { + address.setStreet(result.getString("street_number")); + } + if (result.containsKey("street_name")) { + address.setStreet(result.getString("street_name")); + } + if (result.containsKey("locality")) { + address.setSettlement(result.getString("locality")); + } + if (result.containsKey("admin_1")) { + address.setState(result.getString("admin_1")); + } + if (result.containsKey("country")) { + address.setCountry(result.getString("country")); + } + + return address; + } + +} diff --git a/src/org/traccar/geocoder/GeocodeFarmReverseGeocoder.java b/src/org/traccar/geocoder/GeocodeFarmReverseGeocoder.java deleted file mode 100644 index ca69a9026..000000000 --- a/src/org/traccar/geocoder/GeocodeFarmReverseGeocoder.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016 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 GeocodeFarmReverseGeocoder extends JsonReverseGeocoder { - - private static final String URL = "https://www.geocode.farm/v3/json/reverse/"; - - public GeocodeFarmReverseGeocoder(int cacheSize) { - super(URL + "?lat=%f&lon=%f&country=us&lang=en&count=1", cacheSize); - } - - public GeocodeFarmReverseGeocoder(String key, int cacheSize) { - super(URL + "?lat=%f&lon=%f&country=us&lang=en&count=1&key=" + key, cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - Address address = new Address(); - - JsonObject result = json - .getJsonObject("geocoding_results") - .getJsonArray("RESULTS") - .getJsonObject(0) - .getJsonObject("ADDRESS"); - - if (result.containsKey("street_number")) { - address.setStreet(result.getString("street_number")); - } - if (result.containsKey("street_name")) { - address.setStreet(result.getString("street_name")); - } - if (result.containsKey("locality")) { - address.setSettlement(result.getString("locality")); - } - if (result.containsKey("admin_1")) { - address.setState(result.getString("admin_1")); - } - if (result.containsKey("country")) { - address.setCountry(result.getString("country")); - } - - return address; - } - -} diff --git a/src/org/traccar/geocoder/Geocoder.java b/src/org/traccar/geocoder/Geocoder.java new file mode 100644 index 000000000..3ce3fb67f --- /dev/null +++ b/src/org/traccar/geocoder/Geocoder.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012 - 2013 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; + +public interface Geocoder { + + interface ReverseGeocoderCallback { + + void onSuccess(String address); + + void onFailure(Throwable e); + + } + + void getAddress(AddressFormat format, double latitude, double longitude, ReverseGeocoderCallback callback); + +} diff --git a/src/org/traccar/geocoder/GeocoderException.java b/src/org/traccar/geocoder/GeocoderException.java new file mode 100644 index 000000000..608916641 --- /dev/null +++ b/src/org/traccar/geocoder/GeocoderException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2016 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; + +public class GeocoderException extends RuntimeException { + + public GeocoderException(String message) { + super(message); + } + +} diff --git a/src/org/traccar/geocoder/GisgraphyGeocoder.java b/src/org/traccar/geocoder/GisgraphyGeocoder.java new file mode 100644 index 000000000..1432166e9 --- /dev/null +++ b/src/org/traccar/geocoder/GisgraphyGeocoder.java @@ -0,0 +1,52 @@ +/* + * Copyright 2015 - 2016 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 GisgraphyGeocoder extends JsonGeocoder { + + public GisgraphyGeocoder() { + this("http://services.gisgraphy.com/reversegeocoding/search", 0); + } + + public GisgraphyGeocoder(String url, int cacheSize) { + super(url + "?format=json&lat=%f&lng=%f&from=1&to=1", cacheSize); + } + + @Override + public Address parseAddress(JsonObject json) { + Address address = new Address(); + + JsonObject result = json.getJsonArray("result").getJsonObject(0); + + if (result.containsKey("streetName")) { + address.setStreet(result.getString("streetName")); + } + if (result.containsKey("city")) { + address.setSettlement(result.getString("city")); + } + if (result.containsKey("state")) { + address.setState(result.getString("state")); + } + if (result.containsKey("countryCode")) { + address.setCountry(result.getString("countryCode")); + } + + return address; + } + +} diff --git a/src/org/traccar/geocoder/GisgraphyReverseGeocoder.java b/src/org/traccar/geocoder/GisgraphyReverseGeocoder.java deleted file mode 100644 index b656120d9..000000000 --- a/src/org/traccar/geocoder/GisgraphyReverseGeocoder.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2015 - 2016 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 GisgraphyReverseGeocoder extends JsonReverseGeocoder { - - public GisgraphyReverseGeocoder() { - this("http://services.gisgraphy.com/reversegeocoding/search", 0); - } - - public GisgraphyReverseGeocoder(String url, int cacheSize) { - super(url + "?format=json&lat=%f&lng=%f&from=1&to=1", cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - Address address = new Address(); - - JsonObject result = json.getJsonArray("result").getJsonObject(0); - - if (result.containsKey("streetName")) { - address.setStreet(result.getString("streetName")); - } - if (result.containsKey("city")) { - address.setSettlement(result.getString("city")); - } - if (result.containsKey("state")) { - address.setState(result.getString("state")); - } - if (result.containsKey("countryCode")) { - address.setCountry(result.getString("countryCode")); - } - - return address; - } - -} diff --git a/src/org/traccar/geocoder/GoogleGeocoder.java b/src/org/traccar/geocoder/GoogleGeocoder.java new file mode 100644 index 000000000..0506e701a --- /dev/null +++ b/src/org/traccar/geocoder/GoogleGeocoder.java @@ -0,0 +1,86 @@ +/* + * Copyright 2012 - 2015 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.JsonArray; +import javax.json.JsonObject; +import javax.json.JsonString; + +public class GoogleGeocoder extends JsonGeocoder { + + public GoogleGeocoder() { + this(0); + } + + public GoogleGeocoder(int cacheSize) { + super("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f", cacheSize); + } + + public GoogleGeocoder(String key, int cacheSize) { + super("https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=" + key, cacheSize); + } + + @Override + public Address parseAddress(JsonObject json) { + JsonArray results = json.getJsonArray("results"); + + if (!results.isEmpty()) { + Address address = new Address(); + + JsonObject result = (JsonObject) results.get(0); + JsonArray components = result.getJsonArray("address_components"); + + for (JsonObject component : components.getValuesAs(JsonObject.class)) { + + String value = component.getString("short_name"); + + typesLoop: for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) { + + switch (type.getString()) { + case "street_number": + address.setHouse(value); + break typesLoop; + case "route": + address.setStreet(value); + break typesLoop; + case "locality": + address.setSettlement(value); + break typesLoop; + case "administrative_area_level_2": + address.setDistrict(value); + break typesLoop; + case "administrative_area_level_1": + address.setState(value); + break typesLoop; + case "country": + address.setCountry(value); + break typesLoop; + case "postal_code": + address.setPostcode(value); + break typesLoop; + default: + break; + } + } + } + + return address; + } + + return null; + } + +} diff --git a/src/org/traccar/geocoder/GoogleReverseGeocoder.java b/src/org/traccar/geocoder/GoogleReverseGeocoder.java deleted file mode 100644 index f644079d3..000000000 --- a/src/org/traccar/geocoder/GoogleReverseGeocoder.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2012 - 2015 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.JsonArray; -import javax.json.JsonObject; -import javax.json.JsonString; - -public class GoogleReverseGeocoder extends JsonReverseGeocoder { - - public GoogleReverseGeocoder() { - this(0); - } - - public GoogleReverseGeocoder(int cacheSize) { - super("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f", cacheSize); - } - - public GoogleReverseGeocoder(String key, int cacheSize) { - super("https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=" + key, cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - JsonArray results = json.getJsonArray("results"); - - if (!results.isEmpty()) { - Address address = new Address(); - - JsonObject result = (JsonObject) results.get(0); - JsonArray components = result.getJsonArray("address_components"); - - for (JsonObject component : components.getValuesAs(JsonObject.class)) { - - String value = component.getString("short_name"); - - typesLoop: for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) { - - switch (type.getString()) { - case "street_number": - address.setHouse(value); - break typesLoop; - case "route": - address.setStreet(value); - break typesLoop; - case "locality": - address.setSettlement(value); - break typesLoop; - case "administrative_area_level_2": - address.setDistrict(value); - break typesLoop; - case "administrative_area_level_1": - address.setState(value); - break typesLoop; - case "country": - address.setCountry(value); - break typesLoop; - case "postal_code": - address.setPostcode(value); - break typesLoop; - default: - break; - } - } - } - - return address; - } - - return null; - } - -} diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java new file mode 100644 index 000000000..6d1380729 --- /dev/null +++ b/src/org/traccar/geocoder/JsonGeocoder.java @@ -0,0 +1,89 @@ +/* + * Copyright 2015 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 com.ning.http.client.AsyncCompletionHandler; +import com.ning.http.client.Response; +import org.traccar.Context; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonReader; +import java.util.AbstractMap; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +public abstract class JsonGeocoder implements Geocoder { + + private final String url; + + private Map, String> cache; + + public JsonGeocoder(String url, final int cacheSize) { + this.url = url; + if (cacheSize > 0) { + this.cache = Collections.synchronizedMap(new LinkedHashMap, String>() { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > cacheSize; + } + }); + } + } + + @Override + public void getAddress( + final AddressFormat format, 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(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); + if (cache != null) { + cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); + } + callback.onSuccess(formattedAddress); + } else { + callback.onFailure(new GeocoderException("Empty address")); + } + } + return null; + } + + @Override + public void onThrowable(Throwable t) { + callback.onFailure(t); + } + }); + } + + public abstract Address parseAddress(JsonObject json); + +} diff --git a/src/org/traccar/geocoder/JsonReverseGeocoder.java b/src/org/traccar/geocoder/JsonReverseGeocoder.java deleted file mode 100644 index 808b7c83f..000000000 --- a/src/org/traccar/geocoder/JsonReverseGeocoder.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2015 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 com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; -import org.traccar.Context; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonReader; -import java.util.AbstractMap; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; - -public abstract class JsonReverseGeocoder implements ReverseGeocoder { - - private final String url; - - private Map, String> cache; - - public JsonReverseGeocoder(String url, final int cacheSize) { - this.url = url; - if (cacheSize > 0) { - this.cache = Collections.synchronizedMap(new LinkedHashMap, String>() { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > cacheSize; - } - }); - } - } - - @Override - public void getAddress( - final AddressFormat format, 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(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); - if (cache != null) { - cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); - } - callback.onSuccess(formattedAddress); - } else { - callback.onFailure(new IllegalArgumentException("Empty address")); - } - } - return null; - } - - @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); - } - }); - } - - public abstract Address parseAddress(JsonObject json); - -} diff --git a/src/org/traccar/geocoder/MapQuestGeocoder.java b/src/org/traccar/geocoder/MapQuestGeocoder.java new file mode 100644 index 000000000..7d7217e91 --- /dev/null +++ b/src/org/traccar/geocoder/MapQuestGeocoder.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * + * 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.JsonArray; +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); + } + + @Override + public Address parseAddress(JsonObject json) { + JsonArray result = json.getJsonArray("results"); + if (result != null) { + JsonArray locations = result.getJsonObject(0).getJsonArray("locations"); + if (locations != null) { + JsonObject location = locations.getJsonObject(0); + + Address address = new Address(); + + if (location.containsKey("street")) { + address.setStreet(location.getString("street")); + } + if (location.containsKey("adminArea5")) { + address.setSettlement(location.getString("adminArea5")); + } + if (location.containsKey("adminArea4")) { + address.setDistrict(location.getString("adminArea4")); + } + if (location.containsKey("adminArea3")) { + address.setState(location.getString("adminArea3")); + } + if (location.containsKey("adminArea1")) { + address.setCountry(location.getString("adminArea1").toUpperCase()); + } + if (location.containsKey("postalCode")) { + address.setPostcode(location.getString("postalCode")); + } + + return address; + } + } + return null; + } + +} diff --git a/src/org/traccar/geocoder/MapQuestReverseGeocoder.java b/src/org/traccar/geocoder/MapQuestReverseGeocoder.java deleted file mode 100644 index 9c0a0e891..000000000 --- a/src/org/traccar/geocoder/MapQuestReverseGeocoder.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) - * - * 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.JsonArray; -import javax.json.JsonObject; - -public class MapQuestReverseGeocoder extends JsonReverseGeocoder { - - public MapQuestReverseGeocoder(String url, String key, int cacheSize) { - super(url + "?key=" + key + "&location=%f,%f", cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - JsonArray result = json.getJsonArray("results"); - if (result != null) { - JsonArray locations = result.getJsonObject(0).getJsonArray("locations"); - if (locations != null) { - JsonObject location = locations.getJsonObject(0); - - Address address = new Address(); - - if (location.containsKey("street")) { - address.setStreet(location.getString("street")); - } - if (location.containsKey("adminArea5")) { - address.setSettlement(location.getString("adminArea5")); - } - if (location.containsKey("adminArea4")) { - address.setDistrict(location.getString("adminArea4")); - } - if (location.containsKey("adminArea3")) { - address.setState(location.getString("adminArea3")); - } - if (location.containsKey("adminArea1")) { - address.setCountry(location.getString("adminArea1").toUpperCase()); - } - if (location.containsKey("postalCode")) { - address.setPostcode(location.getString("postalCode")); - } - - return address; - } - } - return null; - } - -} diff --git a/src/org/traccar/geocoder/NominatimGeocoder.java b/src/org/traccar/geocoder/NominatimGeocoder.java new file mode 100644 index 000000000..b0ee39c6a --- /dev/null +++ b/src/org/traccar/geocoder/NominatimGeocoder.java @@ -0,0 +1,81 @@ +/* + * Copyright 2014 - 2015 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 NominatimGeocoder extends JsonGeocoder { + + public NominatimGeocoder() { + this("http://nominatim.openstreetmap.org/reverse", 0); + } + + public NominatimGeocoder(String url, int cacheSize) { + super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1", cacheSize); + } + + public NominatimGeocoder(String url, String key, int cacheSize) { + super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1&key=" + key, cacheSize); + } + + @Override + public Address parseAddress(JsonObject json) { + JsonObject result = json.getJsonObject("address"); + + if (result != null) { + Address address = new Address(); + + if (result.containsKey("house_number")) { + address.setHouse(result.getString("house_number")); + } + if (result.containsKey("road")) { + address.setStreet(result.getString("road")); + } + if (result.containsKey("suburb")) { + address.setSuburb(result.getString("suburb")); + } + + if (result.containsKey("village")) { + address.setSettlement(result.getString("village")); + } else if (result.containsKey("town")) { + address.setSettlement(result.getString("town")); + } else if (result.containsKey("city")) { + address.setSettlement(result.getString("city")); + } + + if (result.containsKey("state_district")) { + address.setDistrict(result.getString("state_district")); + } else if (result.containsKey("region")) { + address.setDistrict(result.getString("region")); + } + + if (result.containsKey("state")) { + address.setState(result.getString("state")); + } + if (result.containsKey("country_code")) { + address.setCountry(result.getString("country_code").toUpperCase()); + } + if (result.containsKey("postcode")) { + address.setPostcode(result.getString("postcode")); + } + + return address; + } + + return null; + } + +} diff --git a/src/org/traccar/geocoder/NominatimReverseGeocoder.java b/src/org/traccar/geocoder/NominatimReverseGeocoder.java deleted file mode 100644 index faeaf247f..000000000 --- a/src/org/traccar/geocoder/NominatimReverseGeocoder.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2014 - 2015 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 NominatimReverseGeocoder extends JsonReverseGeocoder { - - public NominatimReverseGeocoder() { - this("http://nominatim.openstreetmap.org/reverse", 0); - } - - public NominatimReverseGeocoder(String url, int cacheSize) { - super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1", cacheSize); - } - - public NominatimReverseGeocoder(String url, String key, int cacheSize) { - super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1&key=" + key, cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - JsonObject result = json.getJsonObject("address"); - - if (result != null) { - Address address = new Address(); - - if (result.containsKey("house_number")) { - address.setHouse(result.getString("house_number")); - } - if (result.containsKey("road")) { - address.setStreet(result.getString("road")); - } - if (result.containsKey("suburb")) { - address.setSuburb(result.getString("suburb")); - } - - if (result.containsKey("village")) { - address.setSettlement(result.getString("village")); - } else if (result.containsKey("town")) { - address.setSettlement(result.getString("town")); - } else if (result.containsKey("city")) { - address.setSettlement(result.getString("city")); - } - - if (result.containsKey("state_district")) { - address.setDistrict(result.getString("state_district")); - } else if (result.containsKey("region")) { - address.setDistrict(result.getString("region")); - } - - if (result.containsKey("state")) { - address.setState(result.getString("state")); - } - if (result.containsKey("country_code")) { - address.setCountry(result.getString("country_code").toUpperCase()); - } - if (result.containsKey("postcode")) { - address.setPostcode(result.getString("postcode")); - } - - return address; - } - - return null; - } - -} diff --git a/src/org/traccar/geocoder/OpenCageGeocoder.java b/src/org/traccar/geocoder/OpenCageGeocoder.java new file mode 100644 index 000000000..9fa56a4a3 --- /dev/null +++ b/src/org/traccar/geocoder/OpenCageGeocoder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) + * Copyright 2016 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.JsonArray; +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); + } + + @Override + public Address parseAddress(JsonObject json) { + JsonArray result = json.getJsonArray("results"); + if (result != null) { + JsonObject location = result.getJsonObject(0).getJsonObject("components"); + if (location != null) { + Address address = new Address(); + + if (location.containsKey("building")) { + address.setHouse(location.getString("building")); + } + if (location.containsKey("house_number")) { + address.setHouse(location.getString("house_number")); + } + if (location.containsKey("road")) { + address.setStreet(location.getString("road")); + } + if (location.containsKey("suburb")) { + address.setSuburb(location.getString("suburb")); + } + if (location.containsKey("city")) { + address.setSettlement(location.getString("city")); + } + if (location.containsKey("city_district")) { + address.setSettlement(location.getString("city_district")); + } + if (location.containsKey("county")) { + address.setDistrict(location.getString("county")); + } + if (location.containsKey("state")) { + address.setState(location.getString("state")); + } + if (location.containsKey("country_code")) { + address.setCountry(location.getString("country_code").toUpperCase()); + } + if (location.containsKey("postcode")) { + address.setPostcode(location.getString("postcode")); + } + + return address; + } + } + return null; + } + +} diff --git a/src/org/traccar/geocoder/OpenCageReverseGeocoder.java b/src/org/traccar/geocoder/OpenCageReverseGeocoder.java deleted file mode 100644 index a913a9288..000000000 --- a/src/org/traccar/geocoder/OpenCageReverseGeocoder.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2014 - 2015 Stefaan Van Dooren (stefaan.vandooren@gmail.com) - * Copyright 2016 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.JsonArray; -import javax.json.JsonObject; - -public class OpenCageReverseGeocoder extends JsonReverseGeocoder { - - public OpenCageReverseGeocoder(String url, String key, int cacheSize) { - super(url + "/json?q=%f,%f&key=" + key, cacheSize); - } - - @Override - public Address parseAddress(JsonObject json) { - JsonArray result = json.getJsonArray("results"); - if (result != null) { - JsonObject location = result.getJsonObject(0).getJsonObject("components"); - if (location != null) { - Address address = new Address(); - - if (location.containsKey("building")) { - address.setHouse(location.getString("building")); - } - if (location.containsKey("house_number")) { - address.setHouse(location.getString("house_number")); - } - if (location.containsKey("road")) { - address.setStreet(location.getString("road")); - } - if (location.containsKey("suburb")) { - address.setSuburb(location.getString("suburb")); - } - if (location.containsKey("city")) { - address.setSettlement(location.getString("city")); - } - if (location.containsKey("city_district")) { - address.setSettlement(location.getString("city_district")); - } - if (location.containsKey("county")) { - address.setDistrict(location.getString("county")); - } - if (location.containsKey("state")) { - address.setState(location.getString("state")); - } - if (location.containsKey("country_code")) { - address.setCountry(location.getString("country_code").toUpperCase()); - } - if (location.containsKey("postcode")) { - address.setPostcode(location.getString("postcode")); - } - - return address; - } - } - return null; - } - -} diff --git a/src/org/traccar/geocoder/ReverseGeocoder.java b/src/org/traccar/geocoder/ReverseGeocoder.java deleted file mode 100644 index 2f6d6d82b..000000000 --- a/src/org/traccar/geocoder/ReverseGeocoder.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2012 - 2013 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; - -public interface ReverseGeocoder { - - interface ReverseGeocoderCallback { - - void onSuccess(String address); - - void onFailure(Throwable e); - - } - - void getAddress(AddressFormat format, double latitude, double longitude, ReverseGeocoderCallback callback); - -} diff --git a/src/org/traccar/geolocation/GeolocationException.java b/src/org/traccar/geolocation/GeolocationException.java new file mode 100644 index 000000000..5847cc807 --- /dev/null +++ b/src/org/traccar/geolocation/GeolocationException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2016 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.geolocation; + +public class GeolocationException extends RuntimeException { + + public GeolocationException(String message) { + super(message); + } + +} diff --git a/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java b/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java index 7d129e3e9..d6e45b550 100644 --- a/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java +++ b/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java @@ -55,7 +55,7 @@ public class OpenCellIdGeolocationProvider implements GeolocationProvider { json.getJsonNumber("lat").doubleValue(), json.getJsonNumber("lon").doubleValue(), 0); } else { - callback.onFailure(new IllegalArgumentException("Coordinates are missing")); + callback.onFailure(new GeolocationException("Coordinates are missing")); } } return null; @@ -68,7 +68,7 @@ public class OpenCellIdGeolocationProvider implements GeolocationProvider { }); } else { - callback.onFailure(new IllegalArgumentException("No network information")); + callback.onFailure(new GeolocationException("No network information")); } } diff --git a/src/org/traccar/geolocation/UniversalGeolocationProvider.java b/src/org/traccar/geolocation/UniversalGeolocationProvider.java index 320d0774b..48152f7df 100644 --- a/src/org/traccar/geolocation/UniversalGeolocationProvider.java +++ b/src/org/traccar/geolocation/UniversalGeolocationProvider.java @@ -43,7 +43,8 @@ public class UniversalGeolocationProvider implements GeolocationProvider { try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { JsonObject json = reader.readObject(); if (json.containsKey("error")) { - callback.onFailure(new RuntimeException(json.getJsonObject("error").getString("message"))); + callback.onFailure( + new GeolocationException(json.getJsonObject("error").getString("message"))); } else { JsonObject location = json.getJsonObject("location"); callback.onSuccess( -- cgit v1.2.3