简体   繁体   English

基于SpatialPolygonsDataFrame中位置的颜色点

[英]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_veglat + 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.

相关问题 R geo:从点到SpatialPolygonsDataFrame - R geo: from points to SpatialPolygonsDataFrame R从空间多边形数据帧中提取点向量 - R extract vector of points from spatialpolygonsdataframe 如何在R中将具有基于位置的数据的列添加到SpatialPolygonsDataFrame中? - How to add a column with location-based data to a SpatialPolygonsDataFrame in R? 从 SpatialPolygonsDataFrame 创建曲面细分? - Create Tesselation from SpatialPolygonsDataFrame? ggplot2; 将来自不同数据集的点和栅格/矩阵数据添加到空间多边形数据框的图上 - ggplot2; Adding points from a different dataset and a raster/matrix data to a plot of a spatialpolygonsdataframe R - SpatialPolygons列表中的SpatialPolygonsDataFrame - R - SpatialPolygonsDataFrame from a list of SpatialPolygons AddPolygons()需要SpatialPolygonsDataFrame的坐标 - AddPolygons() requires coordinates from SpatialPolygonsDataFrame 将列表中的所有SpatialPolygonsDataFrame对象聚合到一个SpatialPolygonsDataFrame中 - Aggregating all SpatialPolygonsDataFrame objects from list into one SpatialPolygonsDataFrame R - 使用shapefile / SpatialPolygonsDataFrame的数据来着色/填充ggplots - R - using data of shapefiles/SpatialPolygonsDataFrame to colour/fill ggplots R:根据地图上的点的颜色为其值着色并添加图例 - R: Colour points on a map based on their value and add legend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM