Skip to content
Prev 18054 / 29559 Next

How to calculate the latlng of a point in a certain distance away from another?

Hi Endri,

I assumed your distances were in a planar coordinate system. "Distances" in 
decimal degrees don't really make sense, as decimal degrees are a spherical 
system and distances need to be calculated and handled differently.

The results you got with:
simCoords(p,500)
[1] 125.7413 512.6246

got you a location 500 units away (on a cartesian plane) from that initial 
coordinate, but of course it's not a valid location on the earth's surface. 
You could of course simulate a location "1 degree away" and get a coordinate 
that wouldn't look unreasonable, but it wouldn't really be correct.

The second code you included looks pretty jumbled in email. I've attached the 
original script rather than pasting the code inline this time. Maybe that will 
work better. It works fine on my R install, pasted to the command line. Vanilla 
R, no extensions needed.

Ashton
On 04/18/13, Endri Raco, wrote:
wrote:
2.54945626
5.63505465
-----
Ashton Shortridge
Associate Professor			ashton at msu.edu
Dept of Geography			http://www.msu.edu/~ashton
235 Geography Building		ph (517) 432-3561
Michigan State University		fx (517) 432-1671
-------------- next part --------------
# simCoords.R
# Based on a response to the R spatial mailing list, asking
# for help generating coordinates from a vector of distances from an origin.
# written by A. Shortridge, 4/2013

simCoords <- function(origin, dist) {
    # Origin is a 2-element vector (x,y) of the starting coordinate
    # dist is the distance of the offset from this origin to simulate
    
    # simulate an angle in radians
    alpha <- runif(1,-pi,pi)
    
    # Calculate x and y offsets
    yoff <- dist * sin(alpha)
    xoff <- dist * cos(alpha)
    
    # Add offsets to origin, return a vector coordinate
    return (c(origin[1] + xoff, origin[2] + yoff))
}

# Simple function test
simCoords(c(0,0), 10)

# Graphical test of the function
plot(0,0, xlim=c(-15,15), ylim=c(-15,15))
for (i in 1:1000) {
    pt <- simCoords(c(0,0), 10)
    points(pt[1], pt[2])
}

# Test of your distances
distances <- c(0.00000000,  0.02725136,  1.07634471, 1.15963225,  1.71421571,  2.54945626, 4.29135102,  4.53532958,  4.58512710,  4.86466833,  5.24266630,  5.63505465)

exampleCoords <- lapply(distances, simCoords, origin=c(0,0))
plot(0,0, xlim=c(-10,10), ylim=c(-10,10), pch=2, col='blue', cex=2, main='Points near a bus stop')
for (p in 1:length(exampleCoords)) {
    points(exampleCoords[p][[1]][1],exampleCoords[p][[1]][2])
}

# A final test: plotting many, many simulations should look like a map of nested circles
plot(0,0, xlim=c(-10,10), ylim=c(-10,10), pch=2, col='blue', cex=2, main='Points near a bus stop, nsim=200\nThis should look like a bunch of nested circles!')
for (i in 1:200) {
    exampleCoords <- lapply(distances, simCoords, origin=c(0,0))
    for (p in 1:length(exampleCoords)) {
        points(exampleCoords[p][[1]][1],exampleCoords[p][[1]][2], cex=0.4)
    }
}