aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geofence/GeofencePolygon.java
diff options
context:
space:
mode:
authorPhilipp Prangenberg <philipp.prangenberg@derkurier.de>2016-12-05 12:03:08 +0100
committerPhilipp Prangenberg <philipp.prangenberg@derkurier.de>2016-12-05 12:03:08 +0100
commita21f436a58133f7da0cae06366d729665f3b8f9c (patch)
tree72ff1743d96f79e4a9d85b0d48715e5f9aa67cf9 /src/org/traccar/geofence/GeofencePolygon.java
parent960bf899414d89221e92138fdb98777c3f4f73ec (diff)
parent40607036c5aa6385a7ae3f3a283bf107238a5944 (diff)
downloadtrackermap-server-a21f436a58133f7da0cae06366d729665f3b8f9c.tar.gz
trackermap-server-a21f436a58133f7da0cae06366d729665f3b8f9c.tar.bz2
trackermap-server-a21f436a58133f7da0cae06366d729665f3b8f9c.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'src/org/traccar/geofence/GeofencePolygon.java')
-rw-r--r--src/org/traccar/geofence/GeofencePolygon.java75
1 files changed, 33 insertions, 42 deletions
diff --git a/src/org/traccar/geofence/GeofencePolygon.java b/src/org/traccar/geofence/GeofencePolygon.java
index 33d91c0ff..2048ba26d 100644
--- a/src/org/traccar/geofence/GeofencePolygon.java
+++ b/src/org/traccar/geofence/GeofencePolygon.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Anton Tananaev (anton.tananaev@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.
@@ -27,44 +27,18 @@ public class GeofencePolygon extends GeofenceGeometry {
fromWkt(wkt);
}
- private static class Coordinate {
-
- public static final double DEGREE360 = 360;
-
- private double lat;
- private double lon;
-
- public double getLat() {
- return lat;
- }
-
- public void setLat(double lat) {
- this.lat = lat;
- }
-
- public double getLon() {
- 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;
- }
- }
-
private ArrayList<Coordinate> coordinates;
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;
@@ -79,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;