I have coordinates in decimal degrees e.g: point A:Lat,Lng(5.317101667,-2.023303333) and point B:lat,lng(5.317093333,-2.023318333). The altitude in Meters at Point A is 11m and the altitude and Point B is 33m, How can i accurately calculate the distance between these points? Note I know about Haversine, howeve,r these points are close to each other and I want an accurate way to calculate the distance between these two, would finding the distance between the points with d=sqrt(x2-x1)^2+(y2-y1)^2+(Z2-Z1)^2, be accurate? if so what units would the answer be in? I am looking to get the answer in metres.
How to Calculate Distance Between Decimal Degree Coordinates
coordinatesdistance
Related Solutions
What you're looking into is measuring a distance on google maps. Unfortunatly, this is not a question to which an easy answer can be given, but you might consider one of these ways :
- The least accurate way : the haversin formula ,
- Ellipsoidal Earth projected to a plane formula (since Google Maps is based on a Mercator projection and a WGS84 Datum, this formula may yield better results if you succeed in putting it into an algorithm),
- The cleanest of ways, which will yield the best results, is to reproject your map into a local coordinate system (CS) and then mesure the distance on it ;
Let me now focus on this last bullet : google maps uses a coordinate system called "Pseudo Mercator" or "Web Mercator" which have the code EPSG:3857, I live in Morocco, so the coordinate system I should use is EPSG:32630, depending on where you live, you should pick a convenient coordinate system for accurate results. Once you pick your coordinate system, your should look for a the mathematical tranformation between EPSG:3857 and EPSG:XXXXX (your local CS) or leave it to a third party library to carry such transformation (depends on which tools you're using).
Edit : just forgot to mention that if you want a third party library that can handle tranformations, look up Proj4 or PROJ4NET for C# programming.
The code from the previous question does in fact solve this problem once you have explored the issue a bit.
Basically your line doesn't have enough points so it "cuts" the corner across the more localised projection that is used to allow JTS to proceed as if the Earth is flat. This needs one additional line of code:
route = (LineString) Densifier.densify(route, .01);
The brown line is your original 3 point line and the blue line is a densified line (that has points added so it projects properly), shown in a local projection (EPSG:2470). As you can see the distance to the brown line to the point is much shorter than it should be.
So here is some suitably modified code:
public double getDistance(Point point, LineString line) {
double dist = -1.0;
try {
String code = "AUTO:42001," + point.getX() + "," + point.getY();
CoordinateReferenceSystem auto = CRS.decode(code);
// auto = CRS.decode("epsg:2470");
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
Geometry g3 = JTS.transform(line, transform);
Geometry g4 = JTS.transform(point, transform);
Coordinate[] c = DistanceOp.nearestPoints(g4, g3);
Coordinate c1 = new Coordinate();
//System.out.println(c[1].distance(g4.getCoordinate()));
JTS.transform(c[1], c1, transform.inverse());
//System.out.println(geometryFactory.createPoint(c1));
dist = JTS.orthodromicDistance(point.getCoordinate(), c1, DefaultGeographicCRS.WGS84);
} catch (Exception e) {
e.printStackTrace();
}
return dist;
}
And incase you are wondering about the difference between the "flat distance" (c[1].distance(g4.getCoordinate())
) is 760.731km and the "curved distance" (JTS.orthodromicDistance(point.getCoordinate(), c1, DefaultGeographicCRS.WGS84);
) is 760.980km. For reference the distance tool in QGIS gives 760.919 km.
Best Answer
Haversine is used for the great circle distance (shortest distance following the sphere curvature). So it does not take the height into account.
For straight line distance, I would first convert your lat/long/radius+alt triplets to XYZ in a 3D cartesian system (see here). Then you can apply your equation.
Because your points are very close from each other, you could also change you coordinates to a projected coordinate system, then you can use your equation for 3D distance with all units in meters. For an exact solution, you should select an custom azimuthal equidistant projection centered on one of your points.
Note : because you are near the equator, you could also use an approximation of the size of a degree (0.5% error if you assume that they are the same in lat and long) in order to compute your distances in meter. 2*pi*R=360degree, so the mean value of a degree along the meridian = 111132 m