![]() ![]() Polygon_areas <- raster::shapefile("C:/yourshapefile. The solution!įirst we need to load the raster package and the shapefile: library(raster) This is a simple task, but if it is not automated, it’s a grueling one… As such, the natural step, for me, was using R to automate this work. ![]() I looked for solutions, maybe there are some, but I could not find any! I’m sure there are solutions out there… but let me show you mine! Well I had this problem, as you might have guessed. So, if you have 5 global rasters and a shapefile with 10 polygons, the output of this would be 50 rasters (a smaller raster for each polygon, cut from each of the 5 larger rasters). You want to generate a raster file using as mask each of the polygons in the shapefile for each of the original rasters. Here's a very simple plot of the results: plot(elevation.Imagine you have this situation: you have several global raster files and a shapefile with a few areas (e.g. elevation.sub <- mask(elevation.sub, state.sub) # Crop elevation data by extent of state subsetĮlevation.sub <- crop(elevation, extent(state.sub))Īs a final step, you need to identify those pixels of your elevation raster that lie within the borders of the given state polygons. elevation <- raster("/path/to/data/alt.bil") Next, import the raster data using raster() and crop it with the extent of the previously generated states subset. "Maryland", "Delaware", "Virginia", "West Virginia") "Rhode Island","New York","Pennsylvania", "New Jersey", Nestates <- c("Maine", "Vermont", "Massachusetts", "New Hampshire" ,"Connecticut", Pay attention to the use of capital and small initial letters! state <- readOGR(dsn = path.data, layer = "usa_state_shapefile") After setting the shapefile's CRS, I create a subset containing the desired states. Let's import the US shapefile now using readOGR(). ![]() You need to load rgdal and raster libraries in order to proceed. zip data and decompress it to your working directory. I tested it with altitude and mean temperature data from the WorldClim website (I limit this example to altitude, temperature works similar), and an appropriate shapefile of the US containing state borders is to be found here. Here is an approach using extract() from the raster package. The elevation and meantemp objects are the ones that need to be clipped to the area extent of the nestates object. Map(database="state", regions=nestates, interior=TRUE, add=TRUE, fill=TRUE, col="grey") Map(database="state", interior=T, add=TRUE, fill=FALSE) The numbers are proportional distances within the graphics window (xmin,xmax,ymin,ymax) on a scale of 0 to 1. # Next, we create a new graphics space in the lower-right hand corner. Map(database="state", regions = nestates, interior=T, lwd=2) "maryland", "delaware", "virginia", "west virginia") "rhode island","new york","pennsylvania", "new jersey", Nestates<- c("maine", "vermont", "massachusetts", "new hampshire" ,"connecticut", This is what I have so far: #load libraries I have two rasters from which give me these variables but I need to clip them to the extent of the states I am interested in. The map background needs to be either an altitude map or a mean annual temperature map. Import necessary libraries library (pacman) p. Otherwise, if one doesn't want to generate the data, the output raster (exampleraster.tif) and shapefile (fieldboundary.shp) can both also be found in the same link. I am building a map for the northeastern U.S.A. It is formatted so that users can copy and paste (from the web app) the text directly into an R window and generate a data frame. The problem Imagine you have this situation: you have several global raster files and a shapefile with a few areas (e.g. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |