0

I have a raster layer with climate data from the north of Mexico, part of Canada and the US mainland. I am trying to restrict the climate data to only the zone of the US mainland. To do this I thought it would be easy to import a US mainland map and overlay it with my raster data (of course this is turning out to be a lot more difficult than I thought). So far, using the rgdal library, I have managed to import a shapefile including the USA mainland map with states divisions. Now I want to convert this into a raster layer so that can finally overlay it with my initial climate raster layer. This is the code that I am using:

setwd ("C:/Climate_data/USA map")
ogrInfo(".", "USA_mainland_states")
usa_state = readOGR(dsn=".", layer="USA_mainland_states")

##Convert to Raster 
r_usa_state <- raster()
extent(r_usa_state) <- extent(usa_state)
rasterize(usa_state,r_usa_state, fun='last')
overlay (r_usa_state, sms_av, fun='mask')

However, now I get the following error:

Error in .readCells(x, cells, 1) : no data on disk or in memory

sms_av is the climate rasterlayer (103936 elements, 823.3 kb).

Also, when I do:

hasValues(r_usa_state)

I get:

FALSE

What am I doing wrong?? Any advice would be GREATLY appreciated!!!!

horseoftheyear
  • 917
  • 11
  • 23
user2917283
  • 41
  • 1
  • 6
  • 1
    If I'm correct, you're trying to crop or clip a raster to a polygon. This can be achieved with the `?mask` function in the `raster` package. See also [this](http://stackoverflow.com/questions/23827015/crop-a-rasterlayer-with-a-spatialpolygondataframe/23829976#23829976) question and answer. – koekenbakker Jun 04 '14 at 20:01
  • you're not doing anything with the value returned by `rasterize` or `overlay`. R functions work by returning values. – Spacedman Jun 04 '14 at 21:25
  • Hi! yes, thank you. I added the following: r<-rasterize(usa_state,r_usa_state, fun='last'). Now I have tow raster layers (r and sms_av) but I still cannot overlay them, it says that they have different extent. Any advice? – user2917283 Jun 04 '14 at 21:35
  • Hi, thank you for your comment. I tried using mask but I get the following error: Error in compareRaster(x, mask) : different number or columns – user2917283 Jun 04 '14 at 21:39
  • Edit your question, show us the error messages (and which line is causing them) and if its an error with extents then print the extent of the relevant objects and show us that as well. Put the R code in a script and cut and paste the transcript from running it in the question. – Spacedman Jun 04 '14 at 22:00

0 Answers0