如何通过SF查找点属于哪个多边形 [英] How to find which polygon a point belong to via sf

查看:70
本文介绍了如何通过SF查找点属于哪个多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sf对象,其中包含通过.shp文件获得的都市区域的多边形信息(区域).对于给定的经/纬对,我想确定它属于哪个区域.我想我可以利用sf::st_contains(),但是在以正确的格式获取经/纬度时遇到了麻烦.

I have a sf object that contains polygon information (precincts) for a metro area, obtained through a .shp file. For a given lat/lon pair, I want to determine which precinct it belongs to. I'm thinking I can utilize sf::st_contains() but am having trouble getting the lat/lon in the right format.

推荐答案

在lon/lat上使用st_point(),则它可以与其他sf功能一起使用.

Use st_point() on the lon/lat then it can work with other sf functions.

示例:

find_precinct <- function(precincts, point) {
  precincts %>%
    filter(st_contains(geometry, point) == 1) %>%
    `[[`("WARDS_PREC")
}


ggmap::geocode("nicollet mall, st paul") %>%
  rowwise() %>%
  mutate(point = c(lon, lat) %>%
           st_point() %>%
           list(),
         precinct = find_precinct(msvc_precincts, point)
         )

这篇关于如何通过SF查找点属于哪个多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆