aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/geofence/GeofenceGeometry.java7
-rw-r--r--src/org/traccar/geofence/GeofencePolygon.java44
-rw-r--r--src/org/traccar/geofence/GeofencePolyline.java5
3 files changed, 34 insertions, 22 deletions
diff --git a/src/org/traccar/geofence/GeofenceGeometry.java b/src/org/traccar/geofence/GeofenceGeometry.java
index d61e357ce..857ba3414 100644
--- a/src/org/traccar/geofence/GeofenceGeometry.java
+++ b/src/org/traccar/geofence/GeofenceGeometry.java
@@ -27,8 +27,6 @@ public abstract class GeofenceGeometry {
public static class Coordinate {
- public static final double DEGREE360 = 360;
-
private double lat;
private double lon;
@@ -44,11 +42,6 @@ public abstract class GeofenceGeometry {
return lon;
}
- // Need not to confuse algorithm by the abrupt reset of longitude
- public double getLon360() {
- return lon + DEGREE360;
- }
-
public void setLon(double lon) {
this.lon = lon;
}
diff --git a/src/org/traccar/geofence/GeofencePolygon.java b/src/org/traccar/geofence/GeofencePolygon.java
index 48326dc10..2048ba26d 100644
--- a/src/org/traccar/geofence/GeofencePolygon.java
+++ b/src/org/traccar/geofence/GeofencePolygon.java
@@ -32,10 +32,13 @@ public class GeofencePolygon extends GeofenceGeometry {
private double[] constant;
private double[] multiple;
+ private boolean needNormalize = false;
+
private void precalc() {
if (coordinates == null) {
return;
}
+
int polyCorners = coordinates.size();
int i;
int j = polyCorners - 1;
@@ -50,38 +53,55 @@ public class GeofencePolygon extends GeofenceGeometry {
constant = new double[polyCorners];
multiple = new double[polyCorners];
+ boolean hasNegative = false;
+ boolean hasPositive = false;
+ for (i = 0; i < polyCorners; i++) {
+ if (coordinates.get(i).getLon() > 90) {
+ hasPositive = true;
+ } else if (coordinates.get(i).getLon() < -90) {
+ hasNegative = true;
+ }
+ }
+ needNormalize = hasPositive && hasNegative;
for (i = 0; i < polyCorners; j = i++) {
- if (coordinates.get(j).getLon360() == coordinates.get(i).getLon360()) {
+ if (normalizeLon(coordinates.get(j).getLon()) == normalizeLon(coordinates.get(i).getLon())) {
constant[i] = coordinates.get(i).getLat();
multiple[i] = 0;
} else {
constant[i] = coordinates.get(i).getLat()
- - (coordinates.get(i).getLon360() * coordinates.get(j).getLat())
- / (coordinates.get(j).getLon360() - coordinates.get(i).getLon360())
- + (coordinates.get(i).getLon360() * coordinates.get(i).getLat())
- / (coordinates.get(j).getLon360() - coordinates.get(i).getLon360());
+ - (normalizeLon(coordinates.get(i).getLon()) * coordinates.get(j).getLat())
+ / (normalizeLon(coordinates.get(j).getLon()) - normalizeLon(coordinates.get(i).getLon()))
+ + (normalizeLon(coordinates.get(i).getLon()) * coordinates.get(i).getLat())
+ / (normalizeLon(coordinates.get(j).getLon()) - normalizeLon(coordinates.get(i).getLon()));
multiple[i] = (coordinates.get(j).getLat() - coordinates.get(i).getLat())
- / (coordinates.get(j).getLon360() - coordinates.get(i).getLon360());
+ / (normalizeLon(coordinates.get(j).getLon()) - normalizeLon(coordinates.get(i).getLon()));
}
}
}
+ private double normalizeLon(double lon) {
+ if (needNormalize && lon < -90) {
+ return lon + 360;
+ }
+ return lon;
+ }
+
@Override
public boolean containsPoint(double latitude, double longitude) {
int polyCorners = coordinates.size();
int i;
int j = polyCorners - 1;
- double longitude360 = longitude + Coordinate.DEGREE360;
+ double longitudeNorm = normalizeLon(longitude);
boolean oddNodes = false;
for (i = 0; i < polyCorners; j = i++) {
- if (coordinates.get(i).getLon360() < longitude360
- && coordinates.get(j).getLon360() >= longitude360
- || coordinates.get(j).getLon360() < longitude360
- && coordinates.get(i).getLon360() >= longitude360) {
- oddNodes ^= longitude360 * multiple[i] + constant[i] < latitude;
+ if (normalizeLon(coordinates.get(i).getLon()) < longitudeNorm
+ && normalizeLon(coordinates.get(j).getLon()) >= longitudeNorm
+ || normalizeLon(coordinates.get(j).getLon()) < longitudeNorm
+ && normalizeLon(coordinates.get(i).getLon()) >= longitudeNorm) {
+ oddNodes ^= longitudeNorm * multiple[i] + constant[i] < latitude;
}
}
return oddNodes;
diff --git a/src/org/traccar/geofence/GeofencePolyline.java b/src/org/traccar/geofence/GeofencePolyline.java
index cadc69da6..d84f512e3 100644
--- a/src/org/traccar/geofence/GeofencePolyline.java
+++ b/src/org/traccar/geofence/GeofencePolyline.java
@@ -36,11 +36,10 @@ public class GeofencePolyline extends GeofenceGeometry {
@Override
public boolean containsPoint(double latitude, double longitude) {
- double longitude360 = longitude + Coordinate.DEGREE360;
for (int i = 1; i < coordinates.size(); i++) {
if (DistanceCalculator.distanceToLine(
- latitude, longitude360, coordinates.get(i - 1).getLat(), coordinates.get(i - 1).getLon360(),
- coordinates.get(i).getLat(), coordinates.get(i).getLon360()) <= distance) {
+ latitude, longitude, coordinates.get(i - 1).getLat(), coordinates.get(i - 1).getLon(),
+ coordinates.get(i).getLat(), coordinates.get(i).getLon()) <= distance) {
return true;
}
}