diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-06-20 22:37:50 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-20 22:37:50 +1200 |
commit | 98ba0e282a64ecddb7bb8f1b2075b5bab0ef3eac (patch) | |
tree | 825c03638ad5f3fb73016035c35c97523edc8784 /src/org/traccar/DistanceHandler.java | |
parent | 6f569b6d62b110c2679772c4a56122db6a614ed9 (diff) | |
parent | 9129b6f4eb6ab58a83107e47c888bb4a3bbf0f90 (diff) | |
download | trackermap-server-98ba0e282a64ecddb7bb8f1b2075b5bab0ef3eac.tar.gz trackermap-server-98ba0e282a64ecddb7bb8f1b2075b5bab0ef3eac.tar.bz2 trackermap-server-98ba0e282a64ecddb7bb8f1b2075b5bab0ef3eac.zip |
Merge pull request #3269 from Abyss777/distance_optimization
Distance calculation optimization
Diffstat (limited to 'src/org/traccar/DistanceHandler.java')
-rw-r--r-- | src/org/traccar/DistanceHandler.java | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 0ab2d9048..295bc3b29 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -1,6 +1,6 @@ /* * Copyright 2015 Amila Silva - * 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. @@ -24,6 +24,16 @@ import java.math.RoundingMode; public class DistanceHandler extends BaseDataHandler { + private final boolean filter; + private final int coordinatesMinError; + private final int coordinatesMaxError; + + public DistanceHandler(boolean filter, int coordinatesMinError, int coordinatesMaxError) { + this.filter = filter; + this.coordinatesMinError = coordinatesMinError; + this.coordinatesMaxError = coordinatesMaxError; + } + private Position getLastPosition(long deviceId) { if (Context.getIdentityManager() != null) { return Context.getIdentityManager().getLastPosition(deviceId); @@ -35,25 +45,32 @@ public class DistanceHandler extends BaseDataHandler { protected Position handlePosition(Position position) { double distance = 0.0; + if (position.getAttributes().containsKey(Position.KEY_DISTANCE)) { + distance = position.getDouble(Position.KEY_DISTANCE); + } double totalDistance = 0.0; Position last = getLastPosition(position.getDeviceId()); if (last != null) { totalDistance = last.getDouble(Position.KEY_TOTAL_DISTANCE); - if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { distance = DistanceCalculator.distance( position.getLatitude(), position.getLongitude(), last.getLatitude(), last.getLongitude()); - distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); - } else { - distance = position.getDouble(Position.KEY_DISTANCE); + } + if (filter && last.getValid() && last.getLatitude() != 0 && last.getLongitude() != 0) { + boolean satisfiesMin = coordinatesMinError == 0 || distance > coordinatesMinError; + boolean satisfiesMax = coordinatesMaxError == 0 + || distance < coordinatesMaxError || position.getValid(); + if (!satisfiesMin || !satisfiesMax) { + position.setLatitude(last.getLatitude()); + position.setLongitude(last.getLongitude()); + distance = 0; + } } } - if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { - position.set(Position.KEY_DISTANCE, distance); - } + position.set(Position.KEY_DISTANCE, distance); totalDistance = BigDecimal.valueOf(totalDistance + distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); |