R-执行地理空间计算的最佳方法

时间:2018-08-03 20:08:24

标签: r mapping r-raster rgeo-shapefile

我正在一个项目中,我从API中提取犯罪数据,并从本质上计算每个预定义网格单元的犯罪密度。我现在通过将lat和lon放入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

谢谢!

1 个答案:

答案 0 :(得分:0)

# 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)

enter image description here

enter image description here