Adding Distance to a Gps Coordinate

Adding distance to a GPS coordinate

  • P0(lat0,lon0) : initial position (unit : degrees)
  • dx,dy : random offsets from your initial position in meters

You can use an approximation to compute the position of the randomized position:

 lat = lat0 + (180/pi)*(dy/6378137)
lon = lon0 + (180/pi)*(dx/6378137)/cos(lat0)

This is quite precise as long as the random distance offset is below 10-100 km

Edit: of course in Java Math.cos() expects radians so do use Math.cos(Math.PI/180.0*lat0) if lat0 is in degrees as assumed above.

Add exact distance to GPS coordinates

If you have a center (x,y) and you move on the x axis by 30 meters and on the y axis by another 30 meters your distance from the center won't be 30.

It will be Math.sqrt( Math.pow(x, 2) + Math.pow(y, 2) );.

Specifically, there are an infinite number of points that are 30 meters distant from the center (or your initial coordinates).

If you want to move in only one direction, then you can simply add/subtract 30 meters in either of your axis.

As you already did:

double x = longitude + (180/Math.PI)*(distance/6378137)/cos(lat0);

or

double y = latitude + (180/Math.PI)*(distance/6378137);

but not both...


You are still better off by using angles in your calculations, which will turn handy when you move on both axis.

By knowing which direction you are headed to, for example 50° from the x axis,

double a = Math.toRadians(50);  // degrees

double x = longitude + (180/Math.PI) * (30 / 6378137)/cos(lat0) * Math.cos(a);
double y = latitude + (180/Math.PI) * (30 / 6378137) * Math.sin(a);

Coming back to your question, if you want to move on the x axis and the y axis by the same distance and end up exactly 30 meters away from the center, then your angle will be double a = Math.toRadians(45); (if you head North-East) *

In fact you will obtain for both (30 / 6378137) * Math.cos(a) and (30 / 6378137) * Math.sin(a) a result of x1 = y1 = 3.325924707417923e-06.

If you then apply Pythagoras

double finalDistance = Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2)) * 6378137; 

you will find finalDistance to be 30 meters from your initial coordinates.


*
The correct calculation would be Math.toRadians(45 * (2 * n - 1)); | where n = [1, 2, 3, 4]

Create coordinate based on distance and direction

You can use formula from this excellent site (section Destination point given distance and bearing from start point)

var φ2 = Math.asin( Math.sin(φ1)*Math.cos(d/R) +
Math.cos(φ1)*Math.sin(d/R)*Math.cos(brng) );
var λ2 = λ1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(φ1),
Math.cos(d/R)-Math.sin(φ1)*Math.sin(φ2));

where φ is latitude, λ is longitude, θ is the bearing (clockwise from north), δ is the angular distance d/R; d being the distance travelled, R the earth’s radius

Calculate distance between 2 GPS coordinates

Calculate the distance between two coordinates by latitude and longitude, including a Javascript implementation.

West and South locations are negative.
Remember minutes and seconds are out of 60 so S31 30' is -31.50 degrees.

Don't forget to convert degrees to radians. Many languages have this function. Or its a simple calculation: radians = degrees * PI / 180.

function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}

function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {
var earthRadiusKm = 6371;

var dLat = degreesToRadians(lat2-lat1);
var dLon = degreesToRadians(lon2-lon1);

lat1 = degreesToRadians(lat1);
lat2 = degreesToRadians(lat2);

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return earthRadiusKm * c;
}

Here are some examples of usage:

distanceInKmBetweenEarthCoordinates(0,0,0,0)  // Distance between same 
// points should be 0
0

distanceInKmBetweenEarthCoordinates(51.5, 0, 38.8, -77.1) // From London
// to Arlington
5918.185064088764

Calculating position from another GPS position + distance

Once we know the meridional distance in km, and knowing this is 360° of latitude, we can calculate the offset due to moving north or south by x kilometres.

For every degree we move north or south we move (40007.86 / 360) = 111.13 km.

Also we'll include some error checking for locations near the poles..

And I'll add a more general formula for getting a new location given an offset north and east. (Negative for south and west as is the usual convention), this will only be accurate for small displacements.

function getNewLatitude(latitude, distanceKm) {    const meridionalRadiuskm = 40007.86;    latitude = (latitude + distanceKm / (meridionalRadiuskm / 360));    if (latitude > 90) return 180 - latitude;    if (latitude < -90) return -(180 + latitude);    return latitude;}
console.log(getNewLatitude(50, 100));console.log(getNewLatitude(50, -100));
// This function may also be useful, you can use this to get a new location base on a north/south / east/west offset in km. Note that accuracy will start to reduce as the offset increases. function getNewLocation(lat, lon, offsetNorthKm, offsetEastKm) { // The approximate distance in kilometres of one degree at 0,0. const ONE_DEGREE_KM = 111.32;
const deltaLatitude = offsetNorthKm / ONE_DEGREE_KM; const deltaLongitude = offsetEastKm / (ONE_DEGREE_KM * Math.cos(Math.PI * lat / 180));
let result = { lat: lat + deltaLatitude, lon: lon + deltaLongitude } return result;}

Minimum distance between two approximate GPS coordinates

Assuming the two regions are "close enough" so one can neglect the spherical nature of the problem, along with ...

Assuming that those "confidence regions" are somehow important to the user of the result, along with ...

The fact that a single number as a result would erase the uncertainty of the information (or the measurement errors), I would recommend to not expect a number but an interval to be an adequate result.

Let p1, p2 be two "close enough" centers of regions R1, R2.

Let u1, u2 be the uncertainty of the position in the same distance unit as p1, p2 are measured, as the radius of those circles.

Center distance:

dc p1 p2 = |p2-p1|

BorderDistance Minimum:

bdmin p1 p2 u1 u2 = (dc p1 p2) - u1 - u2

BorderDistance Maximum:

bdmax p1 p2 u1 u2 = (dc p1 p2) + u1 + u2

Then, the distance between those regions is the interval:

[bdmin p1 p2 u1 u2, bdmax p1 p2 u1 u2]

let sq x = x*x
let distance p1 p2 : float =
let x1,y1 = p1
let x2,y2 = p2
sqrt(sq (x2-x1) + sq (y2-y1))

let rdistance p1 u1 p2 u2 =
( (distance p1 p2) - u1 - u2
, (distance p1 p2) + u1 + u2
)

let rdistance3 p1 u1 p2 u2 =
let mi,ma = rdistance p1 u1 p2 u2
(mi,distance p1 p2,ma)

let P1 = (0.0,0.0)
let P2 = (10.0,10.0)
let U1 = 2.0
let U2 = 5.0

printfn "as interval: %A" (rdistance P1 U1 P2 U2)
printfn "as interval with center: %A" (rdistance3 P1 U1 P2 U2)

as interval: (7.142135624, 21.14213562)

as interval with center: (7.142135624, 14.14213562, 21.14213562)

The latter version is nice as it allows users to continue as they please, having all 3 values and also are able to get a feeling for the accuracy.

Discussion:

If the true data looks like the one on the picture, it does not pay off to take spherical geometry formulae for the computation. The reason being, that the size of the circles is magnitudes larger than the error yielded from euklidean geometry.

If, on the other hand, the true data would be at significantly large distances, it would probably not matter if the center points or the edges of the circles were taken for the computation. As then the radius of the circles would be tiny compared to the distance. Then, though, spherical geometry is needed.

Last not least, if this is only one step in a longer series of computations, it pays off to keep accuracy information.

See for example the wikipedia article on interval arithmetic.

If you see U1, U2 as statistical parameters, such as the n% confidence region (think something like standard deviation), then you can try to find a statistical model and reason about it.

Warming up, if we assumed both P1 and P2 were measured points from the same statistical distribution, which they are obviously not. Then, the variance of both points would be the same. Which is obviously not the case. Then, given a series of P1,P2 pairs, you could estimate the underlying distribution and use something like a t-test to test the hypothesis P1 = P2.

Now, what you probably have as your U1, U2 in GPS laymans terms is called "dilution of precision" (DOP, some use 2, actually HDOP,VDOP), which is a single number, aggregating the uncertainty of the GPS fix computation. It is a function of many parameters, as a GPS receiver can actually notice:

  • Number of visible and used satellites used for the fix
  • Estimate of time accuracy
  • Accuracy information stated by the satellites
  • ...

Let's say, the GPS receiver only sees 3 satellites. What it does is "measure" the distance to each satellite which is at a location known to the GPS receiver (the satellites send their positions). So from each of the satellites, the receiver can yield a sphere of its own position, with the radius of the sphere being the distance, the center being the location of the satellite.

Intersecting the Spheres computed for each used satellite, one can receive a volume in which the GPS receiver is located. In the absence of any measurement errors etc. it would actually the be exact location of the receiver...in case of selective availability being turned off. SA is an artificial error satellites can add to their information, which decreases the accuracy, civilian GPS receivers can obtain. I think it has been turned off now for a while...

Since the GPS receiver does not have an atomic clock, but the GPS satellites do have those, the estimation task of the receiver is not only to estimate its 3 coordinates, but also the state of its own cheap clock. This is why a GPS fix with only 3 satellites is also called a 2D fix (as the system of equations is still underdetermined). 4 and more satellites yield a 3D fix.

Beyond that basic theory of how it works, there are factors specific to the location of the GPS receiver. Apart from the number of satellites a receiver can use in a given location, there can be RF frequency reflections etc., which can render the "distance computed by time delay" for one or more satellites errorneous. If, say the GPS receiver sees many more than 4 satellites, it will be able to construe, that some measurements are inconsistent compared to the remaining measurements.

All those aspects shown above are then computed into a single floating point number, named "Dilution of precision".

So, clearly it is not easy to take basic statistical tests for the hypothesis P1 <> P2 and one would have to dig deeper than is possible here in this format.



Related Topics



Leave a reply



Submit