R - Fitting a grid over a City Map and inputting data into grid squares

女生的网名这么多〃 提交于 2019-12-04 10:21:38

Additionally, here's an sf and tidyverse-based solution:

With sf, you can make a grid of squares with the st_make_grid() function. Here I'll make a 2km grid over San Jose's bounding box, then intersect it with the boundary of San Jose. Note that I'm projecting to UTM zone 10N so I can specify the grid size in meters.

library(tigris)
library(tidyverse)
library(sf)
options(tigris_class = "sf", tigris_use_cache = TRUE)
set.seed(1234)

sj <- places("CA", cb = TRUE) %>%
  filter(NAME == "San Jose") %>%
  st_transform(26910)

g <- sj %>%
  st_make_grid(cellsize = 2000) %>%
  st_intersection(sj) %>%
  st_cast("MULTIPOLYGON") %>%
  st_sf() %>%
  mutate(id = row_number())

Next, we can generate some random crime data with st_sample() and plot it to see what we are working with.

thefts <- st_sample(sj, size = 500) %>%
  st_sf()

assaults <- st_sample(sj, size = 200) %>%
  st_sf()

plot(g$geometry)
plot(thefts, add = TRUE, col = "red")

Crime data can then be joined to the grid spatially with st_join(). We can plot to check our results.

theft_grid <- g %>%
  st_join(thefts) %>%
  group_by(id) %>%
  summarize(num_thefts = n())

plot(theft_grid["num_thefts"])

We can then do the same with the assaults data, then join the two datasets together to get the desired result. If you had a lot of crime datasets, these could be modified to work within some variation of purrr::map().

assault_grid <- g %>%
  st_join(assaults) %>%
  group_by(id) %>%
  summarize(num_assaults = n()) 

st_geometry(assault_grid) <- NULL

crime_data <- left_join(theft_grid, assault_grid, by = "id")

crime_data

Simple feature collection with 190 features and 3 fields
geometry type:  GEOMETRY
dimension:      XY
bbox:           xmin: 584412 ymin: 4109499 xmax: 625213.2 ymax: 4147443
epsg (SRID):    26910
proj4string:    +proj=utm +zone=10 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# A tibble: 190 x 4
      id num_thefts num_assaults                                                     geometry
   <int>      <int>        <int>                                               <GEOMETRY [m]>
 1     1          2            1 POLYGON ((607150.3 4111499, 608412 4111499, 608412 4109738,…
 2     2          4            1 POLYGON ((608412 4109738, 608412 4111499, 609237.8 4111499,…
 3     3          3            1 POLYGON ((608412 4113454, 608412 4111499, 607150.3 4111499,…
 4     4          2            2 POLYGON ((609237.8 4111499, 608412 4111499, 608412 4113454,…
 5     5          1            1 MULTIPOLYGON (((610412 4112522, 610412 4112804, 610597 4112…
 6     6          1            1 POLYGON ((616205.4 4113499, 616412 4113499, 616412 4113309,…
 7     7          1            1 MULTIPOLYGON (((617467.1 4113499, 618107.9 4113499, 617697.…
 8     8          2            1 POLYGON ((605206.8 4115499, 606412 4115499, 606412 4114617,…
 9     9          5            1 POLYGON ((606412 4114617, 606412 4115499, 608078.2 4115499,…
10    10          1            1 POLYGON ((609242.7 4115499, 610412 4115499, 610412 4113499,…
# ... with 180 more rows

With a Spatial* object, as your data

library(tigris)
ca_cities = tigris::places(state = "CA") #using tigris package to get shape file of all CA cities
sj = ca_cities[ca_cities$NAME == "San Jose",] #specifying to San Jose
sjutm = sp::spTransform(sj, CRS("+proj=utm +zone=10 +datum=WGS84"))

You can make a grid of polygons like this

library(raster)
r <- raster(sjutm, ncol=25, nrow=25)
rp <- as(r, 'SpatialPolygons')

Show it

plot(sjutm, col='red')
lines(rp, col='blue')

To count the number of cases per grid cell (using some random points here) you do not want to use the polygons but rather the RasterLayer

set.seed(0)
x <- runif(500, xmin(r), xmax(r))
y <- runif(500, ymin(r), ymax(r))
xy1 <- cbind(x, y)
x <- runif(500, xmin(r), xmax(r))
y <- runif(500, ymin(r), ymax(r))
xy2 <- cbind(x, y)

d1 <- rasterize(xy1, r, fun="count", background=0)
d2 <- rasterize(xy2, r, fun="count", background=0)

plot(d1)
plot(sjutm, add=TRUE)

Followed by

s <- stack(d1, d2)
names(s) = c("assault", "theft")
s <- mask(s, sjutm)
plot(s, addfun=function()lines(sjutm))

To get the table you are after

p <- rasterToPoints(s)
cell <- cellFromXY(s, p[,1:2])
res <- data.frame(grid_id=cell, p[,3:4])
head(res)
#  grid_id assault theft
#1       1       1     1
#2       2       0     1
#3       3       0     3
#4       5       1     1
#5       6       1     0
#6      26       0     0

You can also create a SpatialPolygonsDataFrame from the results

pp <- as(s, 'SpatialPolygonsDataFrame')
pp
#class       : SpatialPolygonsDataFrame 
#features    : 190 
#extent      : 584411.5, 623584.9, 4109499, 4147443  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=utm +zone=10 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#variables   : 2
#names       : assault, theft 
#min values  :       0,     0 
#max values  :       4,     5 

If your goal is only the visual, and not necessarily all the grid-aggregation code and data you can generate an interactive map and grid in library(mapdeck) (noting you'll need a Mapbox access token)

The first step to generate the data is borrowed from @kwalkertcu 's answer

library(tigris)
library(sf)
options(tigris_class = "sf", tigris_use_cache = TRUE)
set.seed(1234)

sj <- places("CA", cb = TRUE) %>%
  filter(NAME == "San Jose") %>%
  st_transform(26910)

thefts <- st_sample(sj, size = 500) %>%
  st_sf() %>%
  st_transform(crs = 4326)

## some random weight data
thefts$weight <- sample(1:100, size = nrow(thefts), replace = T)

Then, given a sf object with a weight column you can plot it using add_screengrid()

library(mapdeck)

set_token("MAPBOX_TOKEN")

mapdeck(
  style = mapdeck_style("dark")
  , location = c(-121.8, 37.3)
  , zoom = 6
) %>%
  add_screengrid(
    data = thefts
    , cell_size = 15
    , weight = "weight"
  )

Notes:

  • I'm using the github version of mapdeck where the API has changed slightly, but the CRAN version should yield the same result.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!