Wrote this function to generate some raster data for a lab exercise on map algebra for a GIS class I taught. Rasterized data are common for the development of land use regression models, which require all input data to be continuous surfaces. E.g. when developing a predictive surface for No2 concentrations, arterial road density within a given buffer length is likely an important predictor. Here I develop a function to automate the process of creating such a surface in R using the sf package.
The function "rasterize_lines" has 4 inputs:
- Study area polygon
- Polylines to be rasterized
- Spatial resolution of the output raster
- Buffer length from which to calculate line densities from each raster cell centroid.
The function "rasterize_lines" takes the following steps to rasterize the polylines:
- Create empty raster surface of specified grid cell size and study area extent
- Generate buffers of specified length from the centroid of the empty raster surface
- Split the polylines by the centroid buffers layer
- Sum the length of polylines within each buffer in the centroid buffers layer
- Assign the length within each buffer to the associated raster
- Output raster where each grid cell has the value of the length of the polylines within the specified buffer length of the cell centroid
Here we have a map of the City of Vancouver's separated bike lanes as input data:
After running "rasterize_lines" the data have been converted to raster grid with a 100m spatial resolution. Each individual grid cell is assigned the the sum of the length of separated bike lanes within 300m of its centroid.