Lines to Polygon to Raster in R & QGIS
I want to provide details the process of converting a line shapefile to a polygon using both R and QGIS. I found a line shapefile of the everglades, and wanted to eventually have a raster where all cells within the national park (raster) are given a value of 1 like this:
First, I imported to the line shapefile of ENP into QGIS and converted it to a polygon using VECTOR >> GEOMETRY TOOLS >> LINES TO POLYGON. The resulting polygon was a bit messy. Certain portions of the park were inverted. To the rescue--the node editing tool (picture below). I selected problematic nodes that caused the automated process to invert certain regions. I dragged and added nodes where necessary until the final shapefile looked exactly like the originaly imported line shapefile.
library(rgdal) library(raster) ## Import the new shapefile everglades_boundary <- readOGR(dsn = ".", layer = "everglades_newest") ## Create a raster that has the extent of the shapefile, and also set desired resolution/projection. everglades_boundary_raster <- raster(ext = extent(everglades_boundary), crs = projection(suitability_map_as_raster_20_sec), res = 0.005555556) ## Set all values of the raster to 1 values(everglades_boundary_raster) <- 1 ## If you plot the raster it will look like a sqaure (that's fine for now!) plot(everglades_boundary_raster) ## Here, use the very helpful mask() function to retain only raster cells within the park boundaries. everglades_boundary_outline <- raster::mask(everglades_boundary_raster, everglades_boundary) plot(everglades_boundary_outline)