PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

[FIXED] How to create a list of several rasters from a crop and mask (cut) from shapefile in R

 July 26, 2022     crop, mask, r, raster, shapefile     No comments   

Issue

I have 14 raster files from each year of land use. I would like to crop these rasters using my shapefile area of interest. With these cut rasters I would like to create a list where each element represents each year cut by the shapefile.

I tried in a very simple and fast way using the for, crop and mask commands, but without success.

my data exemplo: raster and shape files : https://drive.google.com/file/d/1IOGWZ3_ckKj3UoZVd7ikR5l9n04xd6He/view?usp=sharing

my code

    library(raster)
    library(rgdal)
    library(sf)
     
    setwd('dir')
    
    raster_solo_2005<-raster('utm-50-2005.tif')
    
    raster_solo_2006<-raster('utm-50-2006.tif')
    
    raster_solo_2007<-raster('utm-50-2007.tif')
    
    raster_solo_2008<-raster('utm-50-2008.tif')
    
    raster_solo_2009<-raster('utm-50-2009.tif')
    
    raster_solo_2010<-raster('utm-50-2010.tif')
    
    raster_solo_2011<-raster('utm-50-2011.tif')
    
    raster_solo_2012<-raster('utm-50-2012.tif')
    
    raster_solo_2013<-raster('utm-50-2013.tif')
    
    raster_solo_2014<-raster('utm-50-2014.tif')
    
    raster_solo_2015<-raster('utm-50-2015.tif')
    
    raster_solo_2016<-raster('utm-50-2016.tif')
    
    raster_solo_2017<-raster('utm-50-2017.tif')
    
    raster_solo_2018<-raster('utm-50-2018.tif')
   
 #list raster
    list_raster<-as.list(raster_solo_2005, raster_solo_2006, raster_solo_2007, raster_solo_2008, raster_solo_2009, raster_solo_2010, raster_solo_2011, raster_solo_2012, raster_solo_2013, raster_solo_2014, raster_solo_2015, raster_solo_2016, raster_solo_2017, raster_solo_2018)
      
    #shapefile
    shp_area<-sf::st_read('500m_utm.shp')
    
    #creat list empity
    list_raster_cut<-list()
      
    #loop operation
    for(i in 1:10){
      
      
      # crop e mask
      list_raster_cut[[i]] <- list_raster %>% 
        raster::crop(shp_area)%>%
        raster::mask(shp_area)
      
    }

Update: exit error

Error in h(simpleError(msg, call)) : error evaluating argument 'x' when selecting method for function 'mask': 'unable to find an inherited method for function 'crop' for signature '"list" , "sf"''


Solution

The problem is that you need to also subset the list_raster object inside the for loop.

list_raster_cut[[i]] <- list_raster[[i]] %>% 
        raster::crop(shp_area)%>%
        raster::mask(shp_area)


Answered By - Jonathan V. Solórzano
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing