I have a list of some geo points, Now I want to check does these geo points make straight line or have some curve in this. I want to reject geo points those have sharp curves.
-
Will this be on a large scale, where you need to take the curvature of the earth into account? Or is it just within a small area, where it's OK to treat the earth as flat? – Dawood ibn Kareem Aug 04 '17 at 06:19
-
what if draw path on these points? – er.irfankhan11 Aug 04 '17 at 07:11
-
Calculate the bearing between each pair of points, set a threshold on the value, if it is different by too much reject the line. – geocodezip Aug 04 '17 at 11:43
-
@DawoodibnKareem Currently I am considering only limited difference i.e. max of 1 km. – Mandeep Aug 04 '17 at 13:44
-
@geocodezip for turns this logic will fail as there can be sharp turn also – Mandeep Aug 04 '17 at 13:46
-
I thought that was what you wanted to detect, perhaps you could clarify your question. – geocodezip Aug 04 '17 at 13:59
-
@geocodezip Actually I have a scenario of list of location, but due to inefficiency of GPS device, I am receiving sudden jumps in location data, i want to eliminate those points from available data. do you have any idea how we can do this?? – Mandeep Aug 04 '17 at 14:04
1 Answers
OK, since you've said you're looking at fairly small distances, there's no need to take account of the curvature of the earth.
Three points line on a straight line if the gradient of the segment joining the first two is the same as the gradient of the segment joining the last two, or if both gradients are undefined.
Mathematically, this condition is equivalent to saying
(y2 - y1)(x3 - x2) = (y3 - y2)(x2 - x1)
Applying this to latitude and longitude of several points, you need to check this condition for the first, second and third points; then the second, third and fourth; then the third, fourth and fifth; and so on, till you reach the last point.
You'll also need to consider how much tolerance you want in this equality. Your points are probably not going to be exactly aligned, and you might also get some floating point error in the calculation. You'll need to experiment for yourself to work out how much of a difference between the two sides of this equation is acceptable for your purposes.
Depending on how you've named your variables and methods, the code for it might look something like this.
public static boolean areCollinear(Point[] points, double tolerance) {
for (int index = 0; index < points.length - 2; index++) {
double conditionLHS = ( points[index + 1].getLatitude() - points[index].getLatitude()) * points[index + 2].getLongitude() - points[index + 1].getLongitude());
double conditionRHS = ( points[index + 2].getLatitude() - points[index + 1].getLatitude()) * points[index + 1].getLongitude() - points[index].getLongitude());
if (Math.abs(conditionRHS - condition.LHS) > tolerance) {
return false;
}
}
return true;
}
Note that this won't work once you get to distances of more than 100km or so, due to the curvature of the earth. In that case, a more complicated formula will be required.

- 77,785
- 15
- 98
- 110