R 从光栅单元内的shapefile中计算点

R 从光栅单元内的shapefile中计算点,r,raster,R,Raster,在某种程度上,是否可以使用R从光栅文件的每个单元格中的形状文件中计算点数 其思想是检索一个数据框,该数据框包含光栅中每个单元格的一行和该单元格内点数的一列,同时保留光栅单元格的原始值。这回答了标题“如何从每个单元格内的形状文件中计算点数”中的问题 library(rgdal)#此包用于读取和操作shapefile 库(光栅)#此光栅包 shp库(光栅) #光栅和点数据示例 r欢迎来到StackOverflow。本网站通常的格式是向我们展示您尝试了什么以及它是如何失败的,而不是简单地要求从头开始编

在某种程度上,是否可以使用R从光栅文件的每个单元格中的形状文件中计算点数


其思想是检索一个数据框,该数据框包含光栅中每个单元格的一行和该单元格内点数的一列,同时保留光栅单元格的原始值。

这回答了标题“如何从每个单元格内的形状文件中计算点数”中的问题

library(rgdal)#此包用于读取和操作shapefile
库(光栅)#此光栅包
shp
库(光栅)
#光栅和点数据示例

r欢迎来到StackOverflow。本网站通常的格式是向我们展示您尝试了什么以及它是如何失败的,而不是简单地要求从头开始编写代码。请花些时间阅读帮助页,尤其是和。你还应该了解。
library(rgdal)           # this package to read and manipulate shapefiles
library(raster)          # this package for rasters
shp <- readOGR("my_shape_file.shp")        # read in your points from the shape file
ras <- raster("my_raster_file")            # read in your raster
shp <- spTransform(shp, projection(ras))   # make sure that the two spatial objects share the same coordinate system
cells <- cellFromXY(ras,shp)     # find which cells the points are in
table(cells)                     # and make a table of number of occurences in each cell
library(raster)
# example raster and points data
r <- raster(ncols=10, nrows=5)
n <- 100
x <- runif(n) * 360 - 180
y <- runif(n) * 180 - 90
xy <- cbind(x, y)

rp <- rasterize(xy, r, fun=function(x,...)length(x))
as.data.frame(rp)