[英]colour points based on location from SpatialPolygonsDataFrame
I have a Large SpatialPolygonsDataFrame downloaded from here: 我从这里下载了一个大SpatialPolygonsDataFrame:
http://omap.africanmarineatlas.org/BIOSPHERE/pages/3_terrestrial%20vegetation.htm http://omap.africanmarineatlas.org/BIOSPHERE/pages/3_terrestrial%20vegetation.htm
I used the following to create the SpatialPolygonsDataFrame: 我使用以下代码创建SpatialPolygonsDataFrame:
white_veg <- readOGR(dsn="data",
layer="Whites vegetation")
I have a separate dataframe that looks like this: 我有一个单独的数据框,看起来像这样:
id <- c(1,2,3,4,5,6,7,8)
lat <- c(13.8, 13.552, 13.381, -15.440, -15.860, 13.967, -27.750, -27.750)
lon <- c(2.250, 2.687, 2.865, 23.250, 23.340, 30.527, 21.420, 21.420)
x <- c(566, 537, 554, 879, 811, 268, 216, 216)
y <- c(8, 10.32, 3.83, 64.8, 53.7, 4, 5.8, 2.2)
locations <- data.frame(id, lat, lon, x, y)
I want to create a new column in locations
that classifies each row according to which vegetation type polygon from white_veg
the lat
+ lon
coordinates are inside, eg the factors in white_veg@data$DESCRIPTIO
. 我想创建一个新列locations
,根据分类各行其植被类型多边形从white_veg
的lat
+ lon
坐标都在里面,例如,在因素white_veg@data$DESCRIPTIO
。
I have read a question on gcontains()
from the rgeos
package, but this only returns a logical vector of length 1, I'm not sure what that indicates: 我已经从rgeos
包中读取了有关gcontains()
的问题 ,但这仅返回长度为1的逻辑向量,我不确定这表明什么:
locations_coords <- data.frame(locations$lat, locations$lon)
locations_spoints <- SpatialPoints(locations_coords,proj4string=CRS(proj4string(white_veg)))
gContains(white_veg, locations_spoints)
I also looked at this on the GIS SE , using over
from the sp
package, but the answers weren't thorough enough for me to understand. 我还使用sp
包中的over
在GIS SE上进行over
,但是答案还不够彻底,不足以让我理解。
I ended up using over
from the sp
package like this: 我最终像这样从sp
包中使用over
:
locations_veg_class <- locations %>%
mutate(veg_class = over(locations_spoints, white_veg)$DESCRIPTIO)
where locations_spoints
is the SpatialPoints object I created in the question. 其中locations_spoints
是我在问题中创建的SpatialPoints对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.