r-raster

R how to control spacing of colour bar/legend

和自甴很熟 提交于 2019-12-06 04:43:19
I'd like to plot a map of chlorophyll concentration, but the values are dispersed in such a way that the legend becomes unreadable (see picture) So I am trying to control the spacing of the colours in the colour bar/legend. I'd like to get it evenly spaced (while keeping the uneven breaks on the map itself). The the example below is much simplified and is for a raster plot, but the same goes for image.plot. library(raster) r <- raster(ncol=5, nrow=4) r[] <- 1:20 plot(r, breaks = c(0,1,2,3,5,10,20), col = rainbow(6)) I thought about converting the data to log values, but that doesn't give me a

writeRaster output file size

馋奶兔 提交于 2019-12-06 00:40:38
I have a function that reads a multi-band image in as a raster brick object, iterates through the bands doing various calculations, and then writes the raster out as a new .tif. All of this works fine, but the file size of the new image file is roughly four times greater (I assume because the original image has 4 bands). I'm wondering if there's a parameter in the writeRaster() function that I'm unaware of, or if there's some other way I can ensure that the output image is basically the same file size as the input. Original file size is 134 MB; output ranges from 471 to 530 MB or so, depending

extract() data from raster with small polygons - rounded weights too small

╄→尐↘猪︶ㄣ 提交于 2019-12-05 23:23:46
问题 Using R, I am trying to extract data from a raster layer using a polygon layer. The polygons are much smaller than the raster cells: Now I call extract() from raster library: a <- extract(raster, polygons, weights = TRUE, small = TRUE) a # ... # [[1551]] # value weight # 209 0.03 # top left cell - more than 50% of the polygon area There are two problems - the weight is the proportion of the cell area covered by the polygon, and the weights are rounded to 1/100. In my case, only the top left

R: Error thrown while using RGDAL and RASTER packages

谁说胖子不能爱 提交于 2019-12-05 22:08:33
To whom this may concern: Here is the source code: GRA_D1<- raster(files[[1]]) //Sets up an empty output raster: GRA_D1<- writeStart(GRA_D1,filename='GRA_D1.tif', format='GTiff', overwrite=TRUE) //Write to the raster, for loop: for(i in 1:dim(GRA_D1)[1]){ //Extract raster values at rows d.Frame<- matrix(NA,ncol=2,nrow=dim(GRA_D1)[2]) d.Frame[,1]<- getValues(r1[[1]],i) d.Frame[,2]<- getValues(r1[[2]],i) w.Frame<- as.data.frame(d.Frame) names(w.Frame)<- c("D1_pred_disAg","D1_pred_RK") //Apply the predictive model: m.pred<-predict(mod.1, w.Frame) //Write the predictions to the empty TIFF raster

Raster image seems to be shifted using leaflet for R

夙愿已清 提交于 2019-12-05 21:03:15
I want to plot some spatial data using the leaflet package in R, however the generated raster image seems to be shifted compared to a reference grid. I suspect map projection issue, but I am not expert on the topic, so any help would be appreciated. Here is a minimal code to plot the map: library(leaflet) library(sp) library(raster) set.seed(111) # create dummy data -rectangular grid with random values m = 10 n = 10 x = seq(45,48,length.out = m) y = seq(15,18,length.out = n) X = matrix(rep(x, each = n), nrow = n) Y = matrix(rep(y, m), nrow = n) # collector dataframe points = data.frame(value =

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

徘徊边缘 提交于 2019-12-05 15:08:08
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? Issue may be due to having another package with the same method name, obfuscating the raster extract method. The tidyr package has an

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

一个人想着一个人 提交于 2019-12-05 14:13:20
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 I can interpret time_d <- as.Date(time, format="%j", origin=as.Date("1800-01-01")) Now I'd like to

Use animate() with series of levelplots in R raster

Deadly 提交于 2019-12-05 12:35:16
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 ? Function animate only accepts raster objects as input. You can try saveGIF to animate levelplots: library(raster) library

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

一笑奈何 提交于 2019-12-05 06:39:07
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) ## seg_line = Lines objects Second step: to extract land cover types in the buffer from the raster ha <

Crop a RasterLayer with a SpatialPolygonDataFrame

只谈情不闲聊 提交于 2019-12-04 21:15:29
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@bbox<-as.matrix(ext) # create the origin in WGS84 CRS ll = CRS("+init=epsg:4326") origin =