Created
February 5, 2016 16:06
-
-
Save mhweber/bd4303b8cd5b7fb98068 to your computer and use it in GitHub Desktop.
Simple method to rasterize a shapefile using the raster package in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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....)