一组点与具有sf in R的多边形之间的距离

一组点与具有sf in R的多边形之间的距离,r,spatial,sf,R,Spatial,Sf,我在地图上有一个点的数据框和一个被描述为点的多边形的感兴趣区域。我想计算每个点到多边形之间的距离,理想情况下使用sf包 library("tidyverse") library("sf") # area of interest area <- "POLYGON ((121863.900623145 486546.136633659, 121830.369032584 486624.24942906, 121742.202408334 486680.476675484, 121626

我在地图上有一个点的数据框和一个被描述为点的多边形的感兴趣区域。我想计算每个点到多边形之间的距离,理想情况下使用
sf

library("tidyverse")
library("sf")

# area of interest
area <- 
  "POLYGON ((121863.900623145 486546.136633659, 121830.369032584 486624.24942906, 121742.202408334 486680.476675484, 121626.493982203 486692.384434804, 121415.359596921 486693.816446951, 121116.219703244 486773.748535465, 120965.69439283 486674.642759986, 121168.798757601 486495.217550029, 121542.879304342 486414.780364836, 121870.487595417 486512.71203006, 121863.900623145 486546.136633659))"

# convert to sf and project on a projected coord system
area <- st_as_sfc(area, crs = 7415L)

# points with long/lat coords
pnts <- 
  data.frame(
    id = 1:3, 
    long = c(4.85558, 4.89904, 4.91073),
    lat = c(52.39707, 52.36612, 52.36255)
    )

# convert to sf with the same crs
pnts_sf <- st_as_sf(pnts, crs = 7415L, coords = c("long", "lat"))

# check if crs are equal
all.equal(st_crs(pnts_sf),st_crs(area))
2.在变异呼叫中-所有错误答案

pnts_sf %>% 
  mutate(
    distance = st_distance(area, by_element = TRUE),
    distance2 = st_distance(area, by_element = FALSE),
    distance3 = st_distance(geometry, area, by_element = TRUE)
  )
然而,这种方法似乎有效,并给出了正确的距离

3.
map
over long/lat-工作正常

pnts_geoms <- 
  map2(
    pnts$long, 
    pnts$lat, 
    ~ st_sfc(st_point(c(.x, .y)) , crs = 4326L) 
  ) %>% 
  map(st_transform, crs = 7415L)

map_dbl(pnts_geoms, st_distance, y = area)

结果证明,整个混乱是由我的一个小小的愚蠢疏忽造成的。以下是分类:

  • 数据帧来自与
    区域
    多边形不同的源(!)
  • 在监督下,我一直试图将他们设置为
    CRS7415
    ,这是一个合法但不正确的举动,最终导致了错误的答案
  • 正确的方法是将其转换为
    crs
    中的
    sf
    对象,将其转换为
    区域
    对象所在的对象,然后继续计算距离
总而言之:

# this part was wrong, crs was supposed to be the one they were
# originally coded in
pnts_sf <- st_as_sf(pnts, crs = 4326L, coords = c("long", "lat"))

# then apply the transfromation to another crs
pnts_sf <- st_transform(crs = 7415L)

st_distance(pnts_sf, area)

--------------------------
Units: m
          [,1]
[1,] 3998.5701
[2,]    0.0000
[3,]  751.8097
#这部分是错误的,crs应该是他们的
#最初编码为

pnts\u sf您需要在
st\u distance()调用中通过\u element=TRUE设置
?同样,对于可重复性,为+1example@Phil啊,我想出来了——这是一个非常非常新手的错误!我通过另一个使用epsg 4326
crs的来源获得了点的
long
/
lat
。这部分我没有想到。因此,在创建数据帧之后,到
sf
对象的转换应该是
pnts\u sf,这是非常有用的。你愿意把你的解决方案写下来作为一个答案,这样人们就可以看到它被解决了吗?是的,你是对的,我会把它写在一个答案里。谢谢你的帮助,你的评论让我发现了错误。当我按照我看到的代码进行操作时,我没有看到点2在多边形内。是否可能缺少一个步骤?
> packageVersion("dplyr")
[1] ‘0.7.3’
> packageVersion("sf")
[1] ‘0.5.5’
# this part was wrong, crs was supposed to be the one they were
# originally coded in
pnts_sf <- st_as_sf(pnts, crs = 4326L, coords = c("long", "lat"))

# then apply the transfromation to another crs
pnts_sf <- st_transform(crs = 7415L)

st_distance(pnts_sf, area)

--------------------------
Units: m
          [,1]
[1,] 3998.5701
[2,]    0.0000
[3,]  751.8097