cloudslobi.blogg.se

Croping a raster in r
Croping a raster in r






croping a raster in r
  1. Croping a raster in r how to#
  2. Croping a raster in r code#

SourceFile <- file.path('cigGhana_25.geojson') I wonder if it has anything to do with the versions of raster/rgdal and the GDAL/PROJ libraries that these packages are compiled against? I've tried setExtent and the same thing happens. However, cropping to the minimum extent does not change the extents for the three rasters. All three rasters will have the same resolution and projection. No values associated with this RasterLayerīoth the raster and sf object are in the same crs/projection. I am writing a script that will take any three rasters, and crop them to the minimum possible extent. Also note that the SpatRaster returned may not have the exactly the same extent as the SpatExtent supplied because you can only. You can only crop rectangular areas, but see mask for setting cell values within SpatRaster to NA. plotraster2(x, col = col, maxpixels = maxpixels, add = add, : Cut out a part of a SpatRaster with a SpatExtent, or another object from which an extent can be obtained. The raster is cropped with no errors but when I try and plot the cropped raster I get the following error: Error in. Result = foreach(i = 1:dim(NDVI_stack_h24v06).packages='raster'.combine=rbind.I'm trying something very trivial - cropping a large raster to a smaller extent using a sf object. R = rasterFromCells(NDVI_stack_h24v06, cell],values=F) NDVI_stack_h24v06 = stack( mget( rep( "NDVI_stack_h24v06", 500 ) ) )Ĭell = cellFromPolygon(NDVI_stack_h24v06, pt, weights=FALSE)

croping a raster in r

# 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.

Croping a 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 3 Answers Sorted by: 18 Create a box as a Spatial object and crop your raster by the box. 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

Croping a 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.

croping a raster in r

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. The crop function clips the data to a terra extent object, passed in the function using ext. We wrap it all in stassf to coerce back to an sf object.

croping a raster in r

This can be done easily on-the-fly within crop using vect. I'm extracting the area and percent cover of different land use types from a raster based on several thousand polygon boundaries. However please note that, to work your vector data needs to be a terra vect class object.








Croping a raster in r