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:
Thanx Ashton, Experimenting on a starting point from Google maps p<-c(41.32217,19.802721)
simCoords(p,500)[1] 125.7413 512.6246.
I suppose these are in radians and need to be converted? using
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(41.32217,19.802721))>
plot(41.32217,19.802721, 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])+ }
I still dont get any results. What I am doing wrong? Please help me understand. Regards On Thu, Apr 18, 2013 at 8:28 PM, Ashton Shortridge <ashton at msu.edu>
wrote:
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
----- 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) } }