raster

Bring rasters to same extent by filling with NA - in R

故事扮演 提交于 2019-12-05 20:29:58
I have several cropped rasters with different geometry/outlines. Specifically spatial yield maps from several years of the same field, but the extent varies - the measurements were not always overall the whole field, but in some years only part of it. I want to calculate a mean value of those maps and combine them into one mean-value-raster. That does mean however, that not for every pixel in let's say 5 layers/rasters there is a value. I could accept these missing values to be NA, so the final mean value would only be calculated by let's say 3 rasters for parts of the field, where the maps

Removing data outside country map boundary in R

眉间皱痕 提交于 2019-12-05 20:17:09
I know this is simple but could get this working. I want to remove the excess data points on the map below. How do i do it? Below code gave me the results. ggplot() + geom_polygon(data = rwa2, aes(x = long, y = lat, group= group), colour = "black", size = 0.5, fill = "white") + geom_tile(data = df, aes(x = Lon, y = Lat, z = z, fill = z), alpha = 0.8) + ggtitle("State Data") + xlab("Longitude") + ylab("Latitude") + scale_fill_distiller(type = "div", palette = "Spectral")+ theme_bw() + theme(plot.title = element_text(size = 25, face = "bold"), legend.title = element_text(size = 15), axis.text =

R - get a specific band of a RasterLayer

孤街浪徒 提交于 2019-12-05 18:31:12
A have a RasterLayer with 4 bands: >rx<-raster("/media/karimdion/Passport/Essais/po_3804017_bgrn_0000000 tif") > str(rx) Formal class 'RasterLayer' [package "raster"] with 12 slots @ file :Formal class '.RasterFile' [package "raster"] with 12 slots @ nbands : int 4 @ bandorder : chr "BIL" @ data :Formal class '.SingleLayerData' [package "raster"] with 13 slots @ min : num 0 @ max : num 65535 @ band : int 1 @ history : list() @ title : chr(0) @ extent :Formal class 'Extent' [package "raster"] with 4 slots @ xmin: num 655781 @ xmax: num 666701 @ ymin: num 4071522 @ ymax: num 4084598 @ rotated :

How to make bmp image from pixel byte array in java

若如初见. 提交于 2019-12-05 14:13:22
I have a byte array containing pixel values from a .bmp file. It was generated by doing this: BufferedImage readImage = ImageIO.read(new File(fileName)); byte imageData[] = ((DataBufferByte)readImage.getData().getDataBuffer()).getData(); Now I need to recreate the .bmp image. I tried to make a BufferedImage and set the pixels of the WritableRaster by calling the setPixels method. But there I have to provide an int[], float[] or double[] array. Maybe I need to convert the byte array into one of these. But I don't know how to do that. I also tried the setDataElements method. But I am not sure

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

Plot issues - legend bar scale, breaks, legend, decimals

旧街凉风 提交于 2019-12-05 10:31:44
I want to plot a bunch of rasters and I created a code to adjust breaks for each one and plot them trough a for loop. But i'm getting a problematic color scale bar, and my efforts haven't being effective to solve that. Example: I have precipitation ranging from 0 to 11.000...but most part of the data is between 0 and 5.000... and very few up to 11.000. So I need to change the breaks to capture this variation... more breaks where I have more data. Then I created a breaks object for that. But when I plot the raster, the scale color bar gets awful, very messy... #get predictors (These are a way

R - Finding least cost path through raster image (maze)?

一笑奈何 提交于 2019-12-05 10:27:41
How can I find a non-linear path through raster image data? e.g., least cost algorithm? Starting and ending points are known and given as: Start point = (0,0) End point = (12,-5) For example, extract the approximate path of a winding river through a (greyscale) raster image. # fake up some noisy, but reproducible, "winding river" data set.seed(123) df <- data.frame(x=seq(0,12,by=.01), y=sapply(seq(0,12,by=.01), FUN = function(i) 10*sin(i)+rnorm(1))) # convert to "pixels" of raster data # assumption: image color is greyscale, only need one numeric value, v img <- data.frame(table(round(df$y,0),

Parallel processing of big rasters in R (windows)

安稳与你 提交于 2019-12-05 03:32:42
I'm using the doSNOW package and more specifically the parLapply function to perform reclassification (and subsequently other operations) on a list of big raster datasets (OS: Windows x64). The code looks a little like this minimalistic example: library(raster) library(doSNOW) #create list containing test rasters x <- raster(ncol=10980,nrow=10980) x <- setValues(x,1:ncell(x)) list.x <- replicate( 9 , x ) #setting up cluster NumberOfCluster <- 8 cl <- makeCluster(NumberOfCluster) registerDoSNOW(cl) junk <- clusterEvalQ(cl,library(raster)) #perform calculations on each raster list.x <- parLapply

How to write rasters after stacking them?

随声附和 提交于 2019-12-04 23:58:28
问题 There are several raster files that I want to manipulate and then write them again. rasterfiles <- list.files("C:\\data", "*.envi", full.names = TRUE) d1 <- overlay(stack(rasterfiles ), fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE)) d2=unstack(d1) I am grateful to any idea on how we write d2 (rasters) 回答1: writeRaster(d1, file="d1.nc") #other file formats such as .envi work as well works since d1 is one single raster and not a list of rasters: indeed the result of overlay is one

R - Chaging specific cell values in a large raster layer

巧了我就是萌 提交于 2019-12-04 19:14:10
I am working with R "raster" package and have a large raster layer (62460098 cells, 12 Mb for the object). My cell values range from -1 to 1. I have to replace all negative values with a 0 (example: a cell that has -1 as value has to become a 0). I tried to do this: raster[raster < 0] <- 0 But it keeps overloading my RAM because of the raster size. OS: Windows 7 64-bits RAM size: 8GB Tks! You can do r <- reclassify(raster, c(-Inf, 0, 0)) This will work on rasters of any size (no memory limitation) 42- There are several postings that discuss memory issue s and it's not clear if you have