From b09d0e98541edc7122d992e85547741ffd57f72a Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sat, 26 Nov 2016 19:33:54 +0700 Subject: Implement polyline geofence --- .../org/traccar/geofence/GeofencePolylineTest.java | 54 ++++++++++++++++++++++ .../org/traccar/helper/DistanceCalculatorTest.java | 9 ++++ 2 files changed, 63 insertions(+) create mode 100644 test/org/traccar/geofence/GeofencePolylineTest.java (limited to 'test') diff --git a/test/org/traccar/geofence/GeofencePolylineTest.java b/test/org/traccar/geofence/GeofencePolylineTest.java new file mode 100644 index 000000000..df939d8ba --- /dev/null +++ b/test/org/traccar/geofence/GeofencePolylineTest.java @@ -0,0 +1,54 @@ +package org.traccar.geofence; + +import java.text.ParseException; + +import org.junit.Assert; +import org.junit.Test; + +public class GeofencePolylineTest { + + @Test + public void testPolylineWKT() { + String test = "LINESTRING (55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165)"; + GeofenceGeometry geofenceGeometry = new GeofencePolyline(); + try { + geofenceGeometry.fromWkt(test); + } catch (ParseException e){ + Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); + } + Assert.assertEquals(geofenceGeometry.toWkt(), test); + } + + @Test + public void testContainsPolyline1Interval() { + String test = "LINESTRING (56.83777 60.59833, 56.83766 60.5968)"; + + try { + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 35); + Assert.assertTrue(geofenceGeometry.containsPoint(56.83801, 60.59748)); + + ((GeofencePolyline) geofenceGeometry).setDistance(15); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83801, 60.59748)); + + } catch (ParseException e){ + Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); + } + } + + + @Test + public void testContainsPolyline3Intervals() { + String test = "LINESTRING (56.836 60.6126, 56.8393 60.6114, 56.83887 60.60811, 56.83782 60.5988)"; + + try { + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 15); + Assert.assertTrue(geofenceGeometry.containsPoint(56.83847, 60.60458)); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83764, 60.59725)); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83861, 60.60822)); + + } catch (ParseException e){ + Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); + } + } + +} diff --git a/test/org/traccar/helper/DistanceCalculatorTest.java b/test/org/traccar/helper/DistanceCalculatorTest.java index 7bbb4e3b1..2ce54c583 100644 --- a/test/org/traccar/helper/DistanceCalculatorTest.java +++ b/test/org/traccar/helper/DistanceCalculatorTest.java @@ -10,5 +10,14 @@ public class DistanceCalculatorTest { Assert.assertEquals( DistanceCalculator.distance(0.0, 0.0, 0.05, 0.05), 7863.0, 10.0); } + + @Test + public void testDistanceToInterval() { + Assert.assertEquals(DistanceCalculator.distanceToInterval( + 56.83801, 60.59748, 56.83777, 60.59833, 56.83766, 60.5968), 33.0, 5.0); + + Assert.assertEquals(DistanceCalculator.distanceToInterval( + 56.83753, 60.59508, 56.83777, 60.59833, 56.83766, 60.5968), 105.0, 5.0); + } } -- cgit v1.2.3 From 5ea4572553a84126b7e9f06c822e1a183413a558 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 27 Nov 2016 10:18:13 +0700 Subject: - Rename function - Fix formatting --- src/org/traccar/geofence/GeofencePolyline.java | 2 +- src/org/traccar/helper/DistanceCalculator.java | 2 +- .../org/traccar/geofence/GeofencePolylineTest.java | 25 ++++++++-------------- .../org/traccar/helper/DistanceCalculatorTest.java | 6 +++--- 4 files changed, 14 insertions(+), 21 deletions(-) (limited to 'test') diff --git a/src/org/traccar/geofence/GeofencePolyline.java b/src/org/traccar/geofence/GeofencePolyline.java index e99987207..cadc69da6 100644 --- a/src/org/traccar/geofence/GeofencePolyline.java +++ b/src/org/traccar/geofence/GeofencePolyline.java @@ -38,7 +38,7 @@ public class GeofencePolyline extends GeofenceGeometry { public boolean containsPoint(double latitude, double longitude) { double longitude360 = longitude + Coordinate.DEGREE360; for (int i = 1; i < coordinates.size(); i++) { - if (DistanceCalculator.distanceToInterval( + if (DistanceCalculator.distanceToLine( latitude, longitude360, coordinates.get(i - 1).getLat(), coordinates.get(i - 1).getLon360(), coordinates.get(i).getLat(), coordinates.get(i).getLon360()) <= distance) { return true; diff --git a/src/org/traccar/helper/DistanceCalculator.java b/src/org/traccar/helper/DistanceCalculator.java index 873e84977..88d4ef8a4 100644 --- a/src/org/traccar/helper/DistanceCalculator.java +++ b/src/org/traccar/helper/DistanceCalculator.java @@ -34,7 +34,7 @@ public final class DistanceCalculator { return d * 1000; } - public static double distanceToInterval( + public static double distanceToLine( double pointLat, double pointLon, double lat1, double lon1, double lat2, double lon2) { double d0 = distance(pointLat, pointLon, lat1, lon1); double d1 = distance(lat1, lon1, lat2, lon2); diff --git a/test/org/traccar/geofence/GeofencePolylineTest.java b/test/org/traccar/geofence/GeofencePolylineTest.java index df939d8ba..e281c3dd6 100644 --- a/test/org/traccar/geofence/GeofencePolylineTest.java +++ b/test/org/traccar/geofence/GeofencePolylineTest.java @@ -12,7 +12,7 @@ public class GeofencePolylineTest { String test = "LINESTRING (55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165)"; GeofenceGeometry geofenceGeometry = new GeofencePolyline(); try { - geofenceGeometry.fromWkt(test); + geofenceGeometry.fromWkt(test); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } @@ -22,33 +22,26 @@ public class GeofencePolylineTest { @Test public void testContainsPolyline1Interval() { String test = "LINESTRING (56.83777 60.59833, 56.83766 60.5968)"; - try { - GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 35); - Assert.assertTrue(geofenceGeometry.containsPoint(56.83801, 60.59748)); - - ((GeofencePolyline) geofenceGeometry).setDistance(15); - Assert.assertTrue(!geofenceGeometry.containsPoint(56.83801, 60.59748)); - + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 35); + Assert.assertTrue(geofenceGeometry.containsPoint(56.83801, 60.59748)); + ((GeofencePolyline) geofenceGeometry).setDistance(15); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83801, 60.59748)); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } } - @Test public void testContainsPolyline3Intervals() { String test = "LINESTRING (56.836 60.6126, 56.8393 60.6114, 56.83887 60.60811, 56.83782 60.5988)"; - try { - GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 15); - Assert.assertTrue(geofenceGeometry.containsPoint(56.83847, 60.60458)); - Assert.assertTrue(!geofenceGeometry.containsPoint(56.83764, 60.59725)); - Assert.assertTrue(!geofenceGeometry.containsPoint(56.83861, 60.60822)); - + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 15); + Assert.assertTrue(geofenceGeometry.containsPoint(56.83847, 60.60458)); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83764, 60.59725)); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83861, 60.60822)); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } } - } diff --git a/test/org/traccar/helper/DistanceCalculatorTest.java b/test/org/traccar/helper/DistanceCalculatorTest.java index 2ce54c583..22e41c9a3 100644 --- a/test/org/traccar/helper/DistanceCalculatorTest.java +++ b/test/org/traccar/helper/DistanceCalculatorTest.java @@ -12,11 +12,11 @@ public class DistanceCalculatorTest { } @Test - public void testDistanceToInterval() { - Assert.assertEquals(DistanceCalculator.distanceToInterval( + public void testDistanceToLine() { + Assert.assertEquals(DistanceCalculator.distanceToLine( 56.83801, 60.59748, 56.83777, 60.59833, 56.83766, 60.5968), 33.0, 5.0); - Assert.assertEquals(DistanceCalculator.distanceToInterval( + Assert.assertEquals(DistanceCalculator.distanceToLine( 56.83753, 60.59508, 56.83777, 60.59833, 56.83766, 60.5968), 105.0, 5.0); } -- cgit v1.2.3 From f933147c0cba53df96448a64a2ba58dda4da1659 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 27 Nov 2016 16:01:24 +0700 Subject: - Remove try-catch frome tests - Fixed longitude normalizing for geofences near 180 longitude - Added more tests - Other fixes --- src/org/traccar/geofence/GeofenceGeometry.java | 7 ---- src/org/traccar/geofence/GeofencePolygon.java | 44 +++++++++++++++------ src/org/traccar/geofence/GeofencePolyline.java | 5 +-- test/org/traccar/geofence/GeofenceCircleTest.java | 14 +------ test/org/traccar/geofence/GeofencePolygonTest.java | 36 +++++++++++------ .../org/traccar/geofence/GeofencePolylineTest.java | 46 +++++++++++----------- 6 files changed, 82 insertions(+), 70 deletions(-) (limited to 'test') 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; } } diff --git a/test/org/traccar/geofence/GeofenceCircleTest.java b/test/org/traccar/geofence/GeofenceCircleTest.java index 52c214b53..133ca1db8 100644 --- a/test/org/traccar/geofence/GeofenceCircleTest.java +++ b/test/org/traccar/geofence/GeofenceCircleTest.java @@ -8,29 +8,19 @@ import org.junit.Test; public class GeofenceCircleTest { @Test - public void testCircleWKT() { + public void testCircleWkt() throws ParseException { String test = "CIRCLE (55.75414 37.6204, 100)"; GeofenceGeometry geofenceGeometry = new GeofenceCircle(); - try { geofenceGeometry.fromWkt(test); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } Assert.assertEquals(geofenceGeometry.toWkt(), test); } @Test - public void testContainsCircle() { + public void testContainsCircle() throws ParseException { String test = "CIRCLE (55.75414 37.6204, 100)"; GeofenceGeometry geofenceGeometry = new GeofenceCircle(); - try { geofenceGeometry.fromWkt(test); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } - Assert.assertTrue(geofenceGeometry.containsPoint(55.75477, 37.62025)); - Assert.assertTrue(!geofenceGeometry.containsPoint(55.75545, 37.61921)); } } diff --git a/test/org/traccar/geofence/GeofencePolygonTest.java b/test/org/traccar/geofence/GeofencePolygonTest.java index e3d6aaa29..361e7b70f 100644 --- a/test/org/traccar/geofence/GeofencePolygonTest.java +++ b/test/org/traccar/geofence/GeofencePolygonTest.java @@ -8,31 +8,43 @@ import org.junit.Test; public class GeofencePolygonTest { @Test - public void testPolygonWKT() { + public void testPolygonWkt() throws ParseException { String test = "POLYGON ((55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165))"; GeofenceGeometry geofenceGeometry = new GeofencePolygon(); - try { geofenceGeometry.fromWkt(test); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } Assert.assertEquals(geofenceGeometry.toWkt(), test); } @Test - public void testContainsPolygon() { + public void testContainsPolygon() throws ParseException { String test = "POLYGON ((55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165))"; GeofenceGeometry geofenceGeometry = new GeofencePolygon(); - try { geofenceGeometry.fromWkt(test); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } - Assert.assertTrue(geofenceGeometry.containsPoint(55.75476, 37.61915)); - Assert.assertTrue(!geofenceGeometry.containsPoint(55.75545, 37.61921)); } + + @Test + public void testContainsPolygon180() throws ParseException { + String test = "POLYGON ((66.9494 179.838, 66.9508 -179.8496, 66.8406 -180.0014))"; + GeofenceGeometry geofenceGeometry = new GeofencePolygon(); + geofenceGeometry.fromWkt(test); + Assert.assertTrue(geofenceGeometry.containsPoint(66.9015, -180.0096)); + Assert.assertTrue(geofenceGeometry.containsPoint(66.9015, 179.991)); + Assert.assertTrue(!geofenceGeometry.containsPoint(66.8368, -179.8792)); + + } + + @Test + public void testContainsPolygon0() throws ParseException { + String test = "POLYGON ((51.1966 -0.6207, 51.1897 0.4147, 50.9377 0.5136, 50.8675 -0.6082))"; + GeofenceGeometry geofenceGeometry = new GeofencePolygon(); + geofenceGeometry.fromWkt(test); + Assert.assertTrue(geofenceGeometry.containsPoint(51.0466, -0.0165)); + Assert.assertTrue(geofenceGeometry.containsPoint(51.0466, 0.018)); + Assert.assertTrue(!geofenceGeometry.containsPoint(50.9477, 0.5836)); + + } } diff --git a/test/org/traccar/geofence/GeofencePolylineTest.java b/test/org/traccar/geofence/GeofencePolylineTest.java index e281c3dd6..71aeb4081 100644 --- a/test/org/traccar/geofence/GeofencePolylineTest.java +++ b/test/org/traccar/geofence/GeofencePolylineTest.java @@ -8,40 +8,38 @@ import org.junit.Test; public class GeofencePolylineTest { @Test - public void testPolylineWKT() { + public void testPolylineWkt() throws ParseException { String test = "LINESTRING (55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165)"; GeofenceGeometry geofenceGeometry = new GeofencePolyline(); - try { - geofenceGeometry.fromWkt(test); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } + geofenceGeometry.fromWkt(test); Assert.assertEquals(geofenceGeometry.toWkt(), test); } @Test - public void testContainsPolyline1Interval() { + public void testContainsPolyline1Interval() throws ParseException { String test = "LINESTRING (56.83777 60.59833, 56.83766 60.5968)"; - try { - GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 35); - Assert.assertTrue(geofenceGeometry.containsPoint(56.83801, 60.59748)); - ((GeofencePolyline) geofenceGeometry).setDistance(15); - Assert.assertTrue(!geofenceGeometry.containsPoint(56.83801, 60.59748)); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 35); + Assert.assertTrue(geofenceGeometry.containsPoint(56.83801, 60.59748)); + ((GeofencePolyline) geofenceGeometry).setDistance(15); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83801, 60.59748)); } @Test - public void testContainsPolyline3Intervals() { + public void testContainsPolyline3Intervals() throws ParseException { String test = "LINESTRING (56.836 60.6126, 56.8393 60.6114, 56.83887 60.60811, 56.83782 60.5988)"; - try { - GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 15); - Assert.assertTrue(geofenceGeometry.containsPoint(56.83847, 60.60458)); - Assert.assertTrue(!geofenceGeometry.containsPoint(56.83764, 60.59725)); - Assert.assertTrue(!geofenceGeometry.containsPoint(56.83861, 60.60822)); - } catch (ParseException e){ - Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); - } + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 15); + Assert.assertTrue(geofenceGeometry.containsPoint(56.83847, 60.60458)); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83764, 60.59725)); + Assert.assertTrue(!geofenceGeometry.containsPoint(56.83861, 60.60822)); + + } + + @Test + public void testContainsPolylineNear180() throws ParseException { + String test = "LINESTRING (66.9494 179.838, 66.9508 -179.8496)"; + GeofenceGeometry geofenceGeometry = new GeofencePolyline(test, 25); + Assert.assertTrue(geofenceGeometry.containsPoint(66.95, 180.0)); + Assert.assertTrue(!geofenceGeometry.containsPoint(66.96, 180.0)); + Assert.assertTrue(!geofenceGeometry.containsPoint(66.9509, -179.83)); } } -- cgit v1.2.3