aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/geofence
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-11-19 22:09:54 -0800
committerAnton Tananaev <anton.tananaev@gmail.com>2020-11-19 22:09:54 -0800
commit6d9708e7e8688727fbb6e1db04cf667854addba7 (patch)
tree4c3e90fd2e8f51d3233fd8813d3400a86d5ccd87 /src/main/java/org/traccar/geofence
parent6ea7e5229f8a9269cbcee639a0bd6ede3c7ceb88 (diff)
downloadtrackermap-server-6d9708e7e8688727fbb6e1db04cf667854addba7.tar.gz
trackermap-server-6d9708e7e8688727fbb6e1db04cf667854addba7.tar.bz2
trackermap-server-6d9708e7e8688727fbb6e1db04cf667854addba7.zip
Fix polygon area calculation
Diffstat (limited to 'src/main/java/org/traccar/geofence')
-rw-r--r--src/main/java/org/traccar/geofence/GeofencePolygon.java40
1 files changed, 12 insertions, 28 deletions
diff --git a/src/main/java/org/traccar/geofence/GeofencePolygon.java b/src/main/java/org/traccar/geofence/GeofencePolygon.java
index c03a71225..cd2cbf16a 100644
--- a/src/main/java/org/traccar/geofence/GeofencePolygon.java
+++ b/src/main/java/org/traccar/geofence/GeofencePolygon.java
@@ -15,9 +15,16 @@
*/
package org.traccar.geofence;
+import org.locationtech.spatial4j.context.SpatialContext;
+import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory;
+import org.locationtech.spatial4j.shape.ShapeFactory;
+import org.locationtech.spatial4j.shape.jts.JtsShapeFactory;
+
import java.text.ParseException;
import java.util.ArrayList;
+import static org.locationtech.spatial4j.distance.DistanceUtils.DEG_TO_KM;
+
public class GeofencePolygon extends GeofenceGeometry {
public GeofencePolygon() {
@@ -107,37 +114,14 @@ public class GeofencePolygon extends GeofenceGeometry {
return oddNodes;
}
- private double toRadians(double input) {
- return input / 180.0 * Math.PI;
- }
-
- private double polarTriangleArea(double tan1, double lng1, double tan2, double lng2) {
- double deltaLng = lng1 - lng2;
- double t = tan1 * tan2;
- return 2 * Math.atan2(t * Math.sin(deltaLng), 1 + t * Math.sin(deltaLng));
- }
-
@Override
public double calculateArea() {
- if (coordinates.size() < 3) {
- return 0;
- }
-
- double total = 0;
- Coordinate prev = coordinates.get(coordinates.size() - 1);
- double prevTanLat = Math.tan((Math.PI / 2 - toRadians(prev.getLat())) / 2);
- double prevLng = toRadians(prev.getLon());
-
- for (Coordinate point : coordinates) {
- double tanLat = Math.tan((Math.PI / 2 - toRadians(point.getLat())) / 2);
- double lng = toRadians(point.getLon());
- total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
- prevTanLat = tanLat;
- prevLng = lng;
+ JtsShapeFactory jtsShapeFactory = new JtsSpatialContextFactory().newSpatialContext().getShapeFactory();
+ ShapeFactory.PolygonBuilder polygonBuilder = jtsShapeFactory.polygon();
+ for (Coordinate coordinate : coordinates) {
+ polygonBuilder.pointXY(coordinate.getLon(), coordinate.getLat());
}
-
- double earthRadius = 6371009;
- return total * (earthRadius * earthRadius);
+ return polygonBuilder.build().getArea(SpatialContext.GEO) * DEG_TO_KM * DEG_TO_KM;
}
@Override