当我在shapefile多边形上的Points(Lat,Lon)上运行Over()函数时,不断获取NA

时间:2020-03-07 23:52:08

标签: r shapefile sf sp rgdal

我不确定为什么每次在shapefile中的多边形上运行纬度和经度点的Over函数时,为什么我总是得到NA。请注意,这是我第一次进行空间分析,但是我已经完成了研究并复制了东西,但没有成功。我需要将多边形之外的一些点设为NA,以便可以专注于实际数据。

我阅读了这些资料,因为它们与我的事业有关,但我无法解决我的问题:
sp::over() for point in polygon analysis
https://gis.stackexchange.com/questions/133625/checking-if-points-fall-within-polygon-shapefile
https://gis.stackexchange.com/questions/278723/r-error-in-checking-point-inside-polygon

这是我的代码块

library(sp)
library(rgdal)
library(readr)

gainsville_df <- read_csv("311_Service_Requests__myGNV_.csv")
gnv <- readOGR("~\\Downloads\\GIS_cgbound", layer = "cgbound")

gnv_latlon <- spTransform(gnv, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

gnv_raw <- data.frame(Longitude= gainsville_df$Longitude, Latitude= gainsville_df$Latitude)

coordinates(gnv_raw) <- ~Longitude + Latitude
proj4string(gnv_raw) <- proj4string(gnv)
over(gnv_raw, as(gnv,"SpatialLinesDataFrame"))

#Yeilds:
#  FID_cgboun Id Perimeter Area Acres Hectares Shape_Leng
#1         NA NA        NA   NA    NA       NA         NA

# Desired Output:
# Whereas I should have seen which gainesville Latitudes and Longitude are within the shpaefile
# polygon so I can drop the outliers, that have the NA. According to this, none of my LatLon points 
# are inside the polygon.

数据文件在这里:
形状文件:https://github.com/THsTestingGround/SO_readOGR_quest/tree/master/GIS_cgbound
读取csv文件:https://github.com/THsTestingGround/SO_readOGR_quest/blob/master/311_Service_Requests__myGNV_.csv

如果有人可以帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

我意识到您的点数据是一个SF对象,因为您将POINT (-82.34323174 29.67058748)作为字符。因此,我首先重建了您的数据。我也在这里分配了一个投影。

library(tidyverse)
library(sf)
library(RCurl)

url <- getURL("https://raw.githubusercontent.com/THsTestingGround/SO_readOGR_quest/master/311_Service_Requests__myGNV_.csv")

mydf <- read_csv(url) %>% 
        mutate(Location = gsub(x = Location, pattern = "POINT \\(|\\)", replacement = "")) %>% 
        separate(col = "Location", into = c("lon", "lat"), sep = " ") %>% 
        st_as_sf(coords = c(3,4)) %>% 
        st_set_crs(4326)

我使用sf包导入了shapefile,因为您的数据(在本示例中为mydf)是一个sf对象。导入数据时,我意识到我有LINESTRING,而不是多边形。我相信这就是over()无法正常工作的原因。在这里,我创建了多边形。具体来说,我将所有七个多边形连接在一起。

mypoly <- st_read("cgbound.shp") %>% 
          st_transform(crs = 4326) %>% 
          st_polygonize() %>% 
          st_union()

让我们检查一下数据点和多边形的样子。您肯定有数据点不在多边形之内。

ggplot() +
geom_sf(data = mypoly) +
geom_point(data = mydf, aes(x = Longitude, y = Latitude))

enter image description here

您说,“我需要将多边形之外的一些点设为NA。”因此,我决定使用mydfst_intersects()中创建一个新列。如果数据点停留在多边形中,则在新列check中将看到TRUE。否则,您将看到FALSE。

mutate(mydf,
      check = as.vector(st_intersects(x = mydf, y = mypoly, sparse = FALSE))) -> result

最后,检查如何检查数据点。

ggplot() +
geom_sf(data = mypoly) +
geom_point(data = result, aes(x = Longitude, y = Latitude, color = check))

enter image description here

如果您想通过这种SF方法使用over()混合,则可以执行以下操作。

mutate(mydf,
       check = over(as(mydf, "Spatial"), as(mypoly, "Spatial")))

您想做的最后一件事是对数据进行子集化

filter(result, check == TRUE)

最简单的方式

我向您展示了这种SF方法的工作方式。但是以下实际上是您所需要的。 st_filter()提取停留在mypoly中的数据点。在这种情况下,将删除位于外部的数据点。如果您不必为这些点创建NA,则更加容易。

st_filter(x = mydf, y = mypoly, predicate = st_intersects) -> result2

ggplot() +
geom_sf(data = mypoly) +
geom_sf(data = result2)

enter image description here