【问题标题】:In R, find the polygon containing a point - Shapefile [duplicate]在R中,找到包含一个点的多边形 - Shapefile [重复]
【发布时间】:2015-08-10 04:54:21
【问题描述】:

我有一个 shapefile,我想找到包含点列表的多边形。例如,

rio <- readShapeSpatial("setores_rio.shp")  
bairrorio.fort<- fortify(rio , region = "neighborhood")

head(bairrorio.fort)
   long       lat order  hole piece          group           id
1 -43.17769 -22.91814     1 FALSE     1 330455705001.1 330455705001
2 -43.17771 -22.91814     2 FALSE     1 330455705001.1 330455705001
3 -43.17771 -22.91808     3 FALSE     1 330455705001.1 330455705001
4 -43.17793 -22.91811     4 FALSE     1 330455705001.1 330455705001
5 -43.17811 -22.91768     5 FALSE     1 330455705001.1 330455705001
6 -43.17802 -22.91766     6 FALSE     1 330455705001.1 330455705001

假设 p = c(long, lat) 是一个具有 lat long 定位的点。我想找到包含点 p 的 id(neighborhood)(参见 bairrorio.fort)。

【问题讨论】:

  • bairrorio.fort[bairrorio.fort$long==long &amp; bairrorio.fort$lat==lat,"id"] 会给你想要的
  • 查看 sp 包中的over()
  • 您确定点 p= c(long, lat) 可以是任意点吗?它不起作用...我认为您的建议可能仅适用于 bairrorio.fort 中的点,但是,当我使用 p = c(-43.17769 -22.91814) 运行时(参见 bairrorio.fort 的第一行)结果是 > bairrorio.fort[bairrorio.fort$long == -43.17769 & bairrorio.fort$lat == -22.91814, "id"] 字符(0)
  • 不要使用readShapeSpatial 来读取shapefile。使用来自rgdal 包的readOGR

标签: r shapefile


【解决方案1】:

您可以使用来自 sp: 的point.in.polygon

library(sp)
library(magrittr)

bairrorio.fort %>% 
  split(.$id) %>% 
  sapply(function(x) point.in.polygon(p[1], p[2], x$long, x$lat) > 0) %>%  
  names(.)[.]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多