Skip to content

Instantly share code, notes, and snippets.

@mhweber
Created February 5, 2016 16:06
Show Gist options
  • Save mhweber/bd4303b8cd5b7fb98068 to your computer and use it in GitHub Desktop.
Save mhweber/bd4303b8cd5b7fb98068 to your computer and use it in GitHub Desktop.
Simple method to rasterize a shapefile using the raster package in R
library(raster)
## grab US counties shapefile in Oregon
shp <- shapefile("C:/Users/mweber/Temp/OR_counties.shp")
## no dropping of projection metadata
r <- raster(shp,nrows=100,ncols=150)
r <- rasterize(shp,r,fun="first")
plot(shp,axes=TRUE,border="grey")
plot(r,add=TRUE)
projection(r)
@mhweber
Copy link
Author

mhweber commented Jun 17, 2021

I would suggest st_rasterize in the stars package now rather than rasterize in raster package - https://r-spatial.github.io/stars/reference/st_rasterize.html. I just ran on an sf linestring feature collection of 6000+ features and it rasterized in .17 seconds (of course depends on desired raster resolution....)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment