我正在做一个项目,我从API中提取犯罪数据,并基本上计算每个预定义网格单元的犯罪密度。现在,我通过将经度和经度放入data.frame中,然后计算点中心半径内的点数来实现这一点。这在计算上是昂贵的,因为在预定义的网格中有数千个点和数千个犯罪点。
我想知道是否有更好的方法来计算犯罪密度;我听说栅格可能很有价值?
以下是一些示例数据:
# Create a predefined grid of coordinates
predef.grid <- data.frame(lat = seq(from = 2.0, to = 4.0, by = 0.1),lon = seq(from = 19.0, to = 21.0, by = 0.1))
predef.grid <- expand.grid(predef.grid)
# Create random sample of crime incidents
crime.incidents <- data.frame(lat = rnorm(10, 4),lon = rnorm(10,20))
crime.incidents <- expand.grid(mydata)
# Need to count number of crimes within radius of every point in predef.grid谢谢!
发布于 2018-08-04 07:48:10
# Need to count number of crimes within radius of every point in
library(raster)
library(sp)
# predfined raster
predef.grid <- raster(xmn=2, # xmin
ymn=4, # ymin
xmx=19, # xmax
ymx=21, # ymax
res=1, # spatial resolution
vals = 1) # cell value
plot(predef.grid)
# Create random sample of crime incidents
# points should be a Spatial object of some form, point, etc.
crime.incidents <- spsample(x = as(extent(predef.grid), 'SpatialPolygons'),
n = 100,
type = 'random')
# plot points over grid
points(crime.incidents, pch = 20)
# count points per cell
density <- rasterize(crime.incidents, predef.grid, fun='count')
# plot the density
plot(density)


https://stackoverflow.com/questions/51679481
复制相似问题