Overlay shapefile and rasterlayer in R?

流过昼夜 提交于 2019-12-02 14:54:59

问题


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!!!!

来源:https://stackoverflow.com/questions/24045088/overlay-shapefile-and-rasterlayer-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!