
GIS with R
#Create a kernel density raster for one year of crime data
library(raster)
#Creating Traffic Crime for 2016 and 2017
TrafCrime2016 = TrafCrime[TrafCrime$date < as.Date('2017-01-01'),]
TrafCrime2017 = TrafCrime[TrafCrime$date >= as.Date('2017-01-01'),]
pixelsize = 100
box = round(extent(neighborhoods) / pixelsize) * pixelsize
template = raster(box, crs = project,
nrows = (box@ymax - box@ymin) / pixelsize,
ncols = (box@xmax - box@xmin) / pixelsize)
TrafCrime2017$PRESENT = 1
raster2017 = rasterize(TrafCrime2017 , template, field = 'PRESENT', fun = sum)
TrafCrime2016$PRESENT = 1
raster2016 = rasterize(TrafCrime2016 , template, field = 'PRESENT', fun = sum)
#Create Kernel Density Map of traffic crimes in the city of portland during 2017
kernel = focalWeight(raster2017, d = 264, type = 'Gauss')
heat2017 = focal(raster2017, kernel, fun = sum, na.rm=T)
dev.new()
plot(heat2017,main = "Kernel Density Map of Traffic Crimes in the City of Portland in 2017")
plot(neighborhoods, border='#00000040', add=T)
#Create Kernel Density Map of traffic crimes in the city of portland during 2016
kernel = focalWeight(raster2016, d = 264, type = 'Gauss')
heat2016 = focal(raster2016, kernel, fun = sum, na.rm=T)
dev.new()
plot(heat2016,main = "Kernel Density Map of Traffic Crimes in the City of Portland in 2016")
plot(neighborhoods, border='#00000040', add=T)