giftepi.blogg.se

Crop years raster in r
Crop years raster in r






  1. Crop years raster in r how to#
  2. Crop years raster in r code#

NDVI_stack_h24v06 = stack( mget( rep( "NDVI_stack_h24v06", 500 ) ) )Ĭell = cellFromPolygon(NDVI_stack_h24v06, pt, weights=FALSE) # Create a matrix with random data & use image() Proj4string(pt) <-"+proj=longlat +datum=WGS84 +ellps=WGS84" Pt <- SpatialPolygons(list(Polygons(list(Polygon(pt)), ID="a"))) Extract polygon or points data from raster stacks - library(raster) You can avoid some operations by using cellFromPolygon (or cellFromXY for points) and then clip and getValues.

Crop years raster in r code#

You must be a R wizard! Here is a very minor tweak to simplify your code (may improve performance slightly in some cases). Speed up extracting raster (raster stack) from point, XY or Polygon

crop years raster in r

So to use it, adjust the to fit with the column name of your identifying polygon (guess that could have been built into the function.) and put in: myoutput <- (cores, polygonlist, rasterlayer) #close(mypb) #not sure why but closing the pb while operating causes it to return an empty final dataset. Reproducible Example: library(maptools) # For wrld_simplĭata(wrld_simpl) #polygon of world countriesīound <- wrld_simpl #name it this to subset to 25 countries and because my loop is set up with that variableĬ <- raster(nrow=2e3, ncol=2e3, crs=proj4string(wrld_simpl), xmn=-180, xmx=180, ymn=-90, ymx=90)įastest Method so far result <- ame() #empty result dataframeįor (i in 1:nrow(bound)) ) #trycatch error so it doesn't kill the loop

Crop years raster in r how to#

I looked into those, but have had trouble with gdal in the past and don't know it well enough to know how to implement it. The only thing I've found related to this is this response by Roger Bivand who suggested using GDAL.open() and GDAL.close() as well as getRasterTable() and getRasterData(). Nonetheless, it's pretty slow, and I'm wondering if anyone has any suggestions for improving the efficiency and speed of my code. I've found that the extract function works much faster if I iterate through each individual polygon and crop then mask the raster down to the size of the particular polygon. P <- shapefile(system.file("external/lux.I'm extracting the area and percent cover of different land use types from a raster based on several thousand polygon boundaries. # crop a SpatialPolygon* object with another one Values within the extent of a Raster* object can be set to NA with mask See AlsoĮxtend, merge Examples r <- raster(nrow=45, ncol=90)ī <- as(extent(6, 6.4, 49.75, 50), 'SpatialPolygons') RasterLayer or RasterBrick object or SpatialLines or SpatialPolygons object. To crop by row and column numbers you can create an extent like this (for Raster x, row 5 to 10, column 7 to 12) New Extent objects can also be created with function extent and drawExtent by clicking twice on a plot. You can check this with the extent function. Objects from which an Extent can be extracted/created include RasterLayer, RasterStack, RasterBrick and objects of the Spatial* classes from the sp package.

crop years raster in r

Output dataType (by default it is the same as the input datatype) One of 'near', 'in', or 'out', for use with alignExtentĬharacter. Raster* object or SpatialPolygons*, SpatialLines*, or SpatialPoints* objectĮxtent object, or any object from which an Extent object can be extracted (see Details)Ĭharacter. Usage # S4 method for signature 'Raster'Ĭrop(x, y, filename="", snap='near', datatype=NULL. Areas included in y but outside the extent of x are ignored (see extend if you want a larger area). If x is a Raster* object, the Extent is aligned to x. Crop returns a geographic subset of an object as specified by an Extent object (or object from which an extent object can be extracted/created).








Crop years raster in r