r-raster

Rscript : Why is Error in UseMethod(“extract_”) : being indicated when attempting to use raster::extract?

China☆狼群 提交于 2019-12-07 09:46:33
问题 I attempting to use raster package's extract method to extract values from a Raster* object. RStudioPrompt> jpnpe <- extract(jpnp, jpnb, fun = mean, na.rm = T) where jpnp is the raster object and jpnb is SpatialPolygonsDataFrame However the following error is indicated: Error in UseMethod("extract_") : no applicable method for 'extract_' applied to an object of class "c('RasterStack', 'Raster', 'RasterStackBrick', 'BasicRaster')" How can I get passed this error? 回答1: Issue may be due to

time and geographical subset of netcdf raster stack or raster brick using R

风流意气都作罢 提交于 2019-12-07 08:32:51
问题 For the following netcdf file with daily global sea surface temperatures for 2016, I'm trying to (i) subset temporally, (ii) subset geographically, (iii) then take long-term means for each pixel and create a basic plot. Link to file: here library(raster) library(ncdf4) open the netcdf after setting my working directory nc_data <- nc_open('sst.day.mean.2016.v2.nc') change the time variable so it's easy to interpret time <- ncdf4::ncvar_get(nc_data, varid="time") head(time) change to dates that

Use animate() with series of levelplots in R raster

北城余情 提交于 2019-12-07 07:15:13
问题 I have a time series of 25 yearly land cover rasters. As this is categorical data, I use levelplot(inputRaster) (part of the rasterVis library) to plot a single raster. However, I would like to sequentially plot the yearly rasters, as the animate function of the raster library does. When I use rasStack <- stack(listOfRasters) animate(rasStack) The result does not have a categorical legend. So in short: how can I combine the functionalities of levelplot and animate ? 回答1: Function animate only

How can I speed up extraction of the proportion of land cover types in a buffer from a raster?

人走茶凉 提交于 2019-12-07 04:07:26
问题 I would like to extract spatial data in a buffer of 10 km around 30 000 objects of class SpatialLines and calculate proportion of each land cover type around buffered lines. In a first time, I used the function crop to crop my raster. Then, I used the function extract (package raster) to calculate proportion of 10 land cover types. Here is my code: lapply(1:nrow(tab_lines), FUN=function(k){ First step: to build a buffer of 10 km around the line buf_line <- gBuffer(seg_line[k], width=10000) ##

Draw heat map (or similar) of 2D population distribution

老子叫甜甜 提交于 2019-12-06 16:44:33
I am wondering how I can draw an image of the population proportion (pop.prop) at these locations (x and y) so that I can see the population distribution clearly? The data is shown below: pts.pr = pts.cent[pts.cent$PIDS==3, ] pop = rnorm(nrow(pts.pr), 0, 1) pop.prop = exp(pop)/sum(exp(pop)) pts.pr.data = as.data.frame(cbind(pts.pr@coords, cbind(pop.prop))) x y pop.prop 3633 106.3077 38.90931 0.070022855 3634 106.8077 38.90931 0.012173106 3756 106.3077 38.40931 0.039693085 3878 105.8077 37.90931 0.034190747 3879 106.3077 37.90931 0.057981214 3880 106.8077 37.90931 0.089484103 3881 107.3077 37

Crop a RasterLayer with a SpatialPolygonDataFrame

杀马特。学长 韩版系。学妹 提交于 2019-12-06 15:20:18
问题 I have a raster grid I want to crop according to land borders of the world map provided by the data of the package 'maptools'. By doing some reasearch, I found that I have to use the crop() function and then the mask() function, but I get an error message. Here is my code : # load the worldmap SpatialPolygonDataFrame library(maptools) data(wrld_simpl) ll=CRS("+init=epsg:4326") world<-spTransform(wrld_simpl, ll) ext<-extent(-10.417,31.917,34.083,71.083) # get only region covering europe world

How to change directory for temporary files - problems with huge temporary raster files

ぐ巨炮叔叔 提交于 2019-12-06 14:15:46
问题 I'm desperately trying to writeRaster() but, since the raster is quite large I need a lot of temp memory. The space on my C:/ drive is limited and therefore I want to change the temporary dir to D:/TEMP/ . I tried different approaches I found on the Internet like: Change temporary directory http://r.789695.n4.nabble.com/How-do-I-set-the-Windows-temporary-directory-in-R-td876483.html etc. rasterOptions(tmpdir = "D:/RTEMP/") didn't solve the problem. Even if it worked while the calculation of

Only positive latitude and longitude when importing netcdf in R from paleoView probably wrong projection

不打扰是莪最后的温柔 提交于 2019-12-06 11:13:56
I used the paleoview software to download some variables from past climate, including the mean temperature from 15000BP-10000BP (I could upload a file on request but its a GB at least). The main problem is that when I read the raster, it contains only positive latitude and positive longitude. I know from the article that this has a 2.5*2.5 resolution. Using Raster I loaded both the raster and ncdf4 libraries to read it using raster library(raster) library(ncdf4) When I read it using the following code r <- raster("mean_temperature-15000BP-10000BP.nc", varname = "14000BP-13000BP/13300BP") I get

Why is crop sometimes introducing NAs on a categorical raster?

房东的猫 提交于 2019-12-06 10:56:04
library(raster) r <- raster('glc2000_v1_1') # http://forobs.jrc.ec.europa.eu/products/glc2000/products/glc2000_v1_1_Grid.zip extent(r) # class : Extent # xmin : -180.0045 # xmax : 179.9955 # ymin : -56.01339 # ymax : 89.99554 ext <- extent(-69,-63,-3,3) r1 <- crop(r,ext) #Warning message: #In .getRat(x, ratvalues, ratnames, rattypes) : NAs introduced by coercion If I try to crop a smaller area, it works fine. ext <- extent(-68,-64,-2,2) r1 <- crop(r,ext) # works fine This error is preventing me from saving the file with writeRaster, but I have no idea of what's going on. Another user found the

Plot specific point data on each layer in stack with rasterVis in R

江枫思渺然 提交于 2019-12-06 07:36:49
问题 I have 2 rasters stacked together: library(rasterVis) r1 <- raster(system.file("external/test.grd", package="raster")) r2 <- r1 / 2 r.stack <- stack(r1, r2) Since I would like to highlight some areas later for each specific layer in the stack, I create two point datasets based on the raster values: pts1 <- rasterToPoints(r1, spatial=T) idx <- which(as.data.frame(pts1)[, 1] >= 400) pts1 <- pts1[idx, 1] pts2 <- rasterToPoints(r2, spatial=T) idx <- which(as.data.frame(pts2)[, 1] >= 400) pts2 <-