aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-09-14 21:54:19 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-09-14 21:54:19 -0700
commitf4e7350ee5e15c0d463a4fcf83db877acc181f13 (patch)
tree4a397328c4110aedcfcf298d57993a269a87b759 /src/main/java/org
parente454daf2bdef1ec13ab30fad36bbb330f1ddd6d0 (diff)
downloadtrackermap-server-f4e7350ee5e15c0d463a4fcf83db877acc181f13.tar.gz
trackermap-server-f4e7350ee5e15c0d463a4fcf83db877acc181f13.tar.bz2
trackermap-server-f4e7350ee5e15c0d463a4fcf83db877acc181f13.zip
Add speed limit provider
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/traccar/speedlimit/OverpassSpeedLimitProvider.java74
-rw-r--r--src/main/java/org/traccar/speedlimit/SpeedLimitException.java24
-rw-r--r--src/main/java/org/traccar/speedlimit/SpeedLimitProvider.java30
3 files changed, 128 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/speedlimit/OverpassSpeedLimitProvider.java b/src/main/java/org/traccar/speedlimit/OverpassSpeedLimitProvider.java
new file mode 100644
index 000000000..429a47c76
--- /dev/null
+++ b/src/main/java/org/traccar/speedlimit/OverpassSpeedLimitProvider.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2020 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.speedlimit;
+
+import org.traccar.Context;
+import org.traccar.helper.UnitsConverter;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.ws.rs.client.AsyncInvoker;
+import javax.ws.rs.client.InvocationCallback;
+
+public class OverpassSpeedLimitProvider implements SpeedLimitProvider {
+
+ private final String url;
+
+ public OverpassSpeedLimitProvider(String url) {
+ this.url = url + "?data=[out:json];way[maxspeed](around:100.0,%f,%f);out%%20tags;";
+ }
+
+ private Double parseSpeed(String value) {
+ if (value.endsWith(" mph")) {
+ return UnitsConverter.knotsFromMph(Double.parseDouble(value.substring(0, value.length() - 4)));
+ } else if (value.endsWith(" knots")) {
+ return Double.parseDouble(value.substring(0, value.length() - 6));
+ } else if (value.matches("\\d+")) {
+ return UnitsConverter.knotsFromKph(Double.parseDouble(value));
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public void getSpeedLimit(double latitude, double longitude, SpeedLimitProviderCallback callback) {
+ String formattedUrl = String.format(url, latitude, longitude);
+ AsyncInvoker invoker = Context.getClient().target(formattedUrl).request().async();
+ invoker.get(new InvocationCallback<JsonObject>() {
+ @Override
+ public void completed(JsonObject json) {
+ JsonArray elements = json.getJsonArray("elements");
+ if (!elements.isEmpty()) {
+ Double maxSpeed = parseSpeed(
+ elements.getJsonObject(0).getJsonObject("tags").getString("maxspeed"));
+ if (maxSpeed != null) {
+ callback.onSuccess(maxSpeed);
+ } else {
+ callback.onFailure(new SpeedLimitException("Parsing failed"));
+ }
+ } else {
+ callback.onFailure(new SpeedLimitException("Not found"));
+ }
+ }
+
+ @Override
+ public void failed(Throwable throwable) {
+ callback.onFailure(throwable);
+ }
+ });
+ }
+
+}
diff --git a/src/main/java/org/traccar/speedlimit/SpeedLimitException.java b/src/main/java/org/traccar/speedlimit/SpeedLimitException.java
new file mode 100644
index 000000000..453c952d3
--- /dev/null
+++ b/src/main/java/org/traccar/speedlimit/SpeedLimitException.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2020 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.speedlimit;
+
+public class SpeedLimitException extends RuntimeException {
+
+ public SpeedLimitException(String message) {
+ super(message);
+ }
+
+}
diff --git a/src/main/java/org/traccar/speedlimit/SpeedLimitProvider.java b/src/main/java/org/traccar/speedlimit/SpeedLimitProvider.java
new file mode 100644
index 000000000..f6579f87f
--- /dev/null
+++ b/src/main/java/org/traccar/speedlimit/SpeedLimitProvider.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2020 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.speedlimit;
+
+public interface SpeedLimitProvider {
+
+ interface SpeedLimitProviderCallback {
+
+ void onSuccess(double speedLimit);
+
+ void onFailure(Throwable e);
+
+ }
+
+ void getSpeedLimit(double latitude, double longitude, SpeedLimitProviderCallback callback);
+
+}