Skip to content

Commit

Permalink
For #100 - finished calculation of line-geofence
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed Mar 29, 2015
1 parent 55f4ed3 commit 8db1e6d
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/main/java/org/traccar/web/server/model/DataServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,8 @@ public List<Position> getLatestPositions() {
switch (geoFence.getType()) {
case POLYGON:
Path2D shape = new Path2D.Double();
List<GeoFence.LonLat> points = geoFence.points();
for (int i = 0; i < points.size(); i++) {
GeoFence.LonLat point = points.get(i);
if (i == 0) {
for (GeoFence.LonLat point : geoFence.points()) {
if (shape.getCurrentPoint() == null) {
shape.moveTo(point.lon, point.lat);
} else {
shape.lineTo(point.lon, point.lat);
Expand All @@ -497,7 +495,34 @@ public List<Position> getLatestPositions() {
add = getDistance(position.getLongitude(), position.getLatitude(), center.lon, center.lat) <= geoFence.getRadius() / 1000;
break;
case LINE:
// TODO
GeoFence.LonLat prevPoint = null;
for (GeoFence.LonLat point : geoFence.points()) {
if (prevPoint != null) {
// from http://stackoverflow.com/questions/1459368/snap-point-to-a-line
double apx = position.getLongitude() - prevPoint.lon;
double apy = position.getLatitude() - prevPoint.lat;
double abx = point.lon - prevPoint.lon;
double aby = point.lat - prevPoint.lat;

double ab2 = abx * abx + aby * aby;
double ap_ab = apx * abx + apy * aby;
double t = ap_ab / ab2;
if (t < 0) {
t = 0;
} else if (t > 1) {
t = 1;
}

double destLon = prevPoint.lon + abx * t;
double destLat = prevPoint.lat + aby * t;

if (getDistance(destLon, destLat, position.getLongitude(), position.getLatitude()) <= geoFence.getRadius() / 2000) {
add = true;
break;
}
}
prevPoint = point;
}
break;
}

Expand Down

0 comments on commit 8db1e6d

Please sign in to comment.