我一直在尝试正确解决这个问题,实际上它正在工作,但距离是错误的:
- 1-我有一个积分集合
- 2-我有一个点,我需要在其中找到所有 200m 远的点
- 3- 通常,它应该返回 6 点,但它只返回 4
library(sf)
library(geosphere)
#My points
x<-c(2.3009132,2.2999853,2.2995872,2.2985374,2.2991502,2.2984043,2.3054471,2.3009132,2.3048155,2.3014964)
y<-c(48.8511847,48.8505062,48.8502346,48.8495305,48.8499405,48.8494376,48.8542721,48.8511847,48.853842,48.8515819)
y<-c(48.8511847,48.8505062,48.8502346,48.8495305,48.8499405,48.8494376,48.8542721,48.8511847,48.853842,48.8515819)
df<-data.frame(x=x,y=y)
#Transforming to SF object
sdf<-st_transform(st_as_sf(df, coords = c("x", "y"),
crs = 4326, agr = "constant"),3857)
#My point to which I need to calculte
pnt<- st_transform(
st_sfc(st_point(x = c(2.3009132, 48.8511847)), crs = 4326), 3857)
#A buffer of 200m arround my point
buffer <- st_buffer(pnt,200)
#getting points within the buffer
intr <- st_intersects(sdf, buffer, sparse=F)
#transforming back to lon/lat
sdf <- st_transform(sdf,4326)
#getting the selected points
sdf<-sdf[which(unlist(intr)),]
#Only 4 points were found
> sdf
Simple feature collection with 4 features and 0 fields
geometry type: POINT
dimension: XY
bbox: xmin: 256033.2 ymin: 6249533 xmax: 256201.4 ymax: 6249715
epsg (SRID): 3857
proj4string: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
geometry
1 POINT (256136.5 6249648)
2 POINT (256033.2 6249533)
3 POINT (256136.5 6249648)
4 POINT (256201.4 6249715)
#To verify I have calculated the distance to my point
t_sdf<-df%>% mutate(d = by(df, 1:nrow(df), function(row) {
+ distHaversine(c(row$x, row$y), c(2.3009132, 48.8511847), r = 6378137)
+ }))
#6 points are less than 200m to my point
> t_sdf %>% arrange(d)
x y d
1 2.300913 48.85118 0.00000
2 2.300913 48.85118 0.00000
3 2.301496 48.85158 61.48172
4 2.299985 48.85051 101.61024
5 2.299587 48.85023 143.59844
6 2.299150 48.84994 189.36954
.....