r-raster

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

How to batch process geoTIFFs in R with lapply

余生颓废 提交于 2019-12-04 17:12:31
I have some large geoTIFFs, now I want to convert them to ASCII files, after doing some searches, I write these codes: library(raster) f <- list.files("inputFolder", pattern = "*.tif", full.names = TRUE) r <- lapply(f, raster) a <- lapply(r, writeRaster, filename = "output", format = "ascii") What confused me is that how can I name the output files respectively, according to its original names? I tried: a <- lapply(r, writeRaster, filename = "outputFolder" + f, format = "ascii") But I received error: non-numeric argument to binary operator Then I tried: a <- lapply(r, writeRaster, filename =

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

人走茶凉 提交于 2019-12-04 12:25:18
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 <- pts2[idx, 1] Now, I would like to plot the raster stack with levelplot from rasterVis in R. I would

How to get contour lines around the grids in R-raster?

安稳与你 提交于 2019-12-04 04:12:46
问题 Having a raster in R, how can I draw a contour line around the grids (not joining the centers or anything else, really following the boundaries of the grids) having some value (or identified by some mask)? The following example shows how to get the contour lines around areas with value 0.6: how to do the same but with the lines following the borders of the grids? The function should return an object to add to a plot (as a SpatialLinesDataFrame for rasterToContour ), and adjacent grids should

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

雨燕双飞 提交于 2019-12-04 03:31:15
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 cell is present in the output (value 209) - the weight of 3 other cells was rounded to zero and they

Combining polygons and calculating their area (i.e. number of cells) in R

风流意气都作罢 提交于 2019-12-04 03:24:10
I have a simple raster (created with R-package: raster). Using the function "rasterToPolygons" I get polygons of all raster cells that contain the value "1": library(raster) dat = list() dat$x = seq(1.5, by = 10, len = 10) dat$y = seq(3.5, by = 10, len = 15) dat$z = matrix(sample(c(0,1), size = 10*15, replace = T), 10, 15) r=raster(dat);plot(r) r_poly = rasterToPolygons(r, fun = function(r) {r == 1}, dissolve = F) plot(r_poly, add = T) I do not use "dissolve = T" to avoid that all polygons are merged into one big polygon. Instead, I wish to obtain a new SpatialPolygonsDataFrame in which all

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

大憨熊 提交于 2019-12-03 08:11:51
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 fact that the raster used by leaflet is projected, while the raw data I use to identify the points is Lat

Interactive plotting with R raster: values on mouseover

一个人想着一个人 提交于 2019-12-03 06:59:19
问题 I'd like to do a small program in R for interactive visualization and modification of some raster datasets, seen as colored images. The user should open a file (from the terminal it's OK), plot it, select the points to edit with mouse clicks, and insert the new values. So far I achieved that easily. I use the plot() function from the raster package to visualize the plot, then click() to select the points and edit their value via the terminal. I'd like to add the ability to show the values on

R: Crop GeoTiff Raster using packages “rgdal” and “raster”

半腔热情 提交于 2019-12-03 03:50:17
I'd like to crop GeoTiff Raster Files using the two mentioned packages, "rgdal" and "raster". Everything works fine, except that the quality of the resulting output tif is very poor and in greyscale rather than colour. The original data are high quality raster maps from the swiss federal office of Topography, example files can be downloaded here . This is my code: ## install.packages("rgdal") ## install.packages("raster") library("rgdal") library("raster") tobecroped <- raster("C:/files/krel_1129_2012_254dpi_LZW.tif") ex <- raster(xmn=648000, xmx=649000, ymn=224000, ymx=225000) projection(ex)

Interactive plotting with R raster: values on mouseover

喜夏-厌秋 提交于 2019-12-02 20:39:20
I'd like to do a small program in R for interactive visualization and modification of some raster datasets, seen as colored images. The user should open a file (from the terminal it's OK), plot it, select the points to edit with mouse clicks, and insert the new values. So far I achieved that easily. I use the plot() function from the raster package to visualize the plot, then click() to select the points and edit their value via the terminal. I'd like to add the ability to show the values on mouse over. I've searched for ways on how to do this, but this doesn't seem to be possible with the