r-raster

Aggregate raster in R with NA values

≡放荡痞女 提交于 2019-12-11 02:25:22
问题 I have a 1km resolution raster in R with widespread NA values throughout, but at irregular locations (i.e. the cells with data are not contiguous and have NA values scattered throughout). I am trying to aggregate this raster (using aggregate() command in the {raster} package) at, say, 5km resolution (factor=5) with a user-defined function for averaging circular angles (included below). As of now, I can't figure how to get aggregate() (or my function, if that's the problem) to provide a result

R: read GeoTiff from gz file with a connection and raster package

你。 提交于 2019-12-11 01:41:04
问题 I would like to read a gzipped GeoTIFF from a server without downloading it. I just don't want to create a lot of temporary files that I have to delete later on. I see it is possible with .csv.gz files. With download I do it in the following way: library(raster) link <- "ftp://ftp.glcf.umd.edu/glcf/SRTM/Degree_Tiles/n000/SRTM_ff03_n000e010 /SRTM_ff03_n000e010.tif.gz" download.file(link, "test.tif.gz") gunzip("test.tif.gz") myras <- raster("test.tif") plot(myras) I can read an uncompressed

“Globe”-shaped map of Russia

大兔子大兔子 提交于 2019-12-10 13:27:31
问题 I draw a map of regions of Russia using GADM data: setwd("~/Desktop/Master thesis/") library(sp) library(RColorBrewer) library(raster) data <- getData('GADM', country='RUS', level=1) #exclude columns I don't need data <- data[,-c(2,3,4,5,7,8,9,10,11,12,13)] data$region <- as.factor(iconv(as.character(data$NAME_1))) png(file = "~/Desktop/myplot2.png", width = 1300, height = 700, units = "px") spplot(data, "region", xlim=c(15,190), ylim=c(40,83), col.regions = colorRampPalette(brewer.pal(12,

Extracting pixels values and coordinates in neighborhood of given buffer with NA values

醉酒当歌 提交于 2019-12-10 12:16:45
问题 I want to get values (pixels values), coordinates (x and y) and attribute ( status ) in the neighborhood (for example in a buffer=6 meters) of random coordinates ( pts ), using extract function in raster package. I try to organize the results in data.frame without NA values and this problem is solved by @Robert Hijmans in Extracting pixels values and coordinates in neighborhood of given buffer in R. But, if I have some coordinates outside of one of raster (and I create the s2 raster with this

R - Chaging specific cell values in a large raster layer

你。 提交于 2019-12-09 23:29:20
问题 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! 回答1: You can do r <- reclassify(raster, c(-Inf, 0, 0)) This will work on rasters of any size (no memory

Identify position of a click on a raster in leaflet, in R

痴心易碎 提交于 2019-12-09 05:52:56
问题 I am plotting a large lat-lon NetCDF raster over an R leaflet map using shinydashboard . When I click on the map, a popup comes out and shows row, column, lat-lon position and value of the clicked raster point. (See reproducible code below) The problem is that I am experiencing a shift in the raster if the raster is large enough. For example, here I clicked on a point which should have a value, but the result is that the identified point is the one above. I believe this has to do with the

Writing R raster stack to NetCDF

北城以北 提交于 2019-12-08 09:18:09
问题 I've got an R grid file containing monthly temperature data for the year 1981 which I read in and tried to write to NetCDF using the following code: library(raster) library(ncdf4) library(RNetCDF) test <- raster('.../TavgM_1981.gri', package = "raster") rstack = stack(test) writeRaster(rstack, "rstack.nc", overwrite=TRUE, format="CDF", varname="Temperature", varunit="degC", longname="Temperature -- raster stack to netCDF", xname="X", yname="Y",zname="nbands", zunit="numeric") This writes the

Fastest way to select a valid range for raster data

早过忘川 提交于 2019-12-08 05:47:26
问题 Using R, I need to select the valid range for a given raster (from package raster ) in the fastest possible way. I tried this: library(raster) library(microbenchmark) library(ggplot2) library(compiler) r <- raster(ncol=100, nrow=100) r[] <- runif(ncell(r)) #Let's see if precompiling helps speed... f <- function(x, min, max) reclassify(x, c(-Inf, min, NA, max, Inf, NA)) g <- cmpfun(f) #Benchmark! compare <- microbenchmark( calc(r, fun=function(x){ x[x < 0.2] <- NA; x[x > 0.8] <- NA; return(x)}

R how to control spacing of colour bar/legend

 ̄綄美尐妖づ 提交于 2019-12-08 02:27:12
问题 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

Raster image seems to be shifted using leaflet for R

喜欢而已 提交于 2019-12-07 14:26:38
问题 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,