I am trying to crop a raster by a polygon in R to the state of Sergipe-Brazil. My raster is a raster brick criated by 7 .tif files which are EVI (vegetation index) time series. Since the EVI archives are larger then the state I want, I'm trying to crop it by using a Polygon shapefile of Sergipe. Here is the script:
# Loading the packages
library(raster)
library(sp)
library(rgdal)
library(RCurl)
library(bitops)
library(hddtools)
library(XML)
library(zoo)
# Creating the rasterbrick
evi <- list.files(pattern = '^.*\\.tif$')
evistack <- stack(evi)
evibrick <- brick(evistack)
# Converting from -1 to 1 (EVI)
evit1<-evibrick/10000
#Saving the RASTER BRICK
writeRaster(evit1, filename="evi_2000t1.tif", format="GTiff", overwrite=TRUE)
# Importing the shapefile of Sergipe
se <- readOGR(dsn = "C:/ArquivosR/EVI/Dados_anuais/2000", layer = "Estado")
### Transforming the CRS os the shapefile
SE.utm <- SpatialPolygons(list(Polygons(list(Polygon(se)), "1")))
proj4string(SE.utm) = CRS("+proj=utm +zone=24 +south +datum=WGS84 +units=m +no_defs")
SE.utm@bbox; SE.utm@proj4string
### CRS GEO (GeoTiff)
SE.geo<- spTransform(SE.utm, CRS("+proj=longlat +datum=WGS84"))
SE.geo@bbox; SE.geo@proj4string
# Reprojecting the RasterLayer
projection(evit1) <- CRS("+proj=longlat +datum=WGS84")
alt.utm <- projectRaster(evit1, crs="+proj=longlat +datum=WGS84")
### Cropping
evit1.crop <- crop(alt.utm, extent(SE.geo), snap='out')
image(evit1.crop, asp=1)
I've changed the CRS os the shapefile to let both rasterbrick and shapefile in the same coordinate system. BUT, when I run it:
cr <- crop(alt.utm, extent(SE.geo), snap="out")
Error in .local(x, y, ...) : extents do not overlap
Checking out the extent of the files I got this:
extent(alt.utm)
class : Extent
xmin : -38.24549
xmax : -36.39112
ymin : -11.56996
ymax : -9.51354
extent(SE.geo)
class : Extent
xmin : -37.44375
xmax : -37.44375
ymin : -10.58074
ymax : -10.58074
I'll aprecciate any help!