2

Given a specific lon and lat point c(lon, lat), and a table data that contains a list of lat and lon, we want to:

  • calculate the distance between this point to all other lat and lon points in the data and
  • return all points from the table that are within 1000 meters from the c(lon, lat)

Tried using distm function but we only succeeded in calculating the distance between c(lon, lat) to one point:

library(spatial)
distm(x, y, fun=distGeo)
distm(data[,3:2], c(40.706914,-74.011322), fun = distHaversine)
smci
  • 32,567
  • 20
  • 113
  • 146
  • You might be looking for the which() function. For example, to find all elements of a numeric vector that are equal to 5, you would write: which(vec == 5). Hard to know exactly what you need without more information though (e.g., which package does the distm() function come from? What is a specific problem to solve with an answer? – Brigadeiro Jan 16 '19 at 17:23
  • @MrLister: this one's more complicated though: it's searching for the nearest in a vector of 2D coords, with arbitrary dist function. (It's not just searching a vector of integer/string/factor for the indices of exact match(es)). – smci Apr 16 '19 at 03:39
  • Is that `spatial::distm()`, `geosphere::distm()` or which package? Please make your code example complete. – smci Apr 16 '19 at 03:50

1 Answers1

0

You can use distHaversine(x,y), where x has the many points (data[,3:2] in your example) and y is the single point.

Reproducible example:

set.seed(123)
y = c(40.706914,-74.011322)
data = data.frame(1:10, rnorm(10, y[1], 0.02), rnorm(10, y[2], 0.02))

which(distHaversine(data[,2:3], y) < 1000)
# [1] 2 4
dww
  • 30,425
  • 5
  • 68
  • 111