[R-sig-Geo] How to calculate the latlng of a point in a certain distance away from another?

Ashton Shortridge ashton at msu.edu
Thu Apr 18 20:28:24 CEST 2013


Dear Endri,

do you simply wish to simulate coordinates for these locations? Because, 
assuming you are working on a planar system and those distances are euclidean, 
each of those distances is a radius for an infinite number of locations on a 
circle around your bus stop. 

Code like this might then do what you want:

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), main='This had better look like a 
circle!')
for (i in 1:1000) {
    pt <- simCoords(c(0,0), 10)
    points(pt[1], pt[2])
}
    
# Test using 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)
    }
}


Hope this helps,

Ashton

On 04/18/13, Endri Raco, wrote:
> Hi group,
> I am stacked in a problem like this.
> I have a bus stop with known coordinates.
> I generate in R language distances from this bus stop. Values of these
> distances are like below
> (in km)
> 
> 
> 0.00000000  0.02725136  1.07634471  1.15963225  1.71421571  2.54945626
> 4.29135102  4.53532958  4.58512710  4.86466833  5.24266630  5.63505465
> 
> I need to convert this distances in coordinates in order to reflect
> points in map which are these distances far from starting bus stop.
> 
> 
> 
> Any ideas about this?
> 
> Please help
> 
> Regards
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-----
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



More information about the R-sig-Geo mailing list