raster

extract from {raster} package using excessive memory

送分小仙女□ 提交于 2019-12-03 17:06:21
I have been using the extract function from the raster package to extract data from raster files using an area defined by shapefiles. However, I am having problems with the amount of memory that this process is now requiring. I do have a large number of shapefiles (~1000). The raster files are large (~1.6gb) My process is: shp <- mclapply(list.files(pattern="*.shp",full.names=TRUE), readShapePoly,mc.cores=6) ndvi <- raster("NDVI.dat") mc<- function(y) { temp <- gUnionCascaded(y) extract <- extract(ndvi,temp) mean <- range(extract, na.rm=T )[1:2] leng <- length(output) } output <- lapply(shp,

How to write rasters after stacking them?

混江龙づ霸主 提交于 2019-12-03 15:44:47
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) 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 single raster (see ?overlay ). Additionally the concept of stack is precisely to take several rasters having

Plotting a raster behind a shapefile

雨燕双飞 提交于 2019-12-03 13:19:14
问题 How can I plot a "raster" object behind a shapefile object? Both plot fine on their own but the points don't plot over the raster: require(rgdal) require(maptools) require(raster) myproj = "+proj=utm +zone=12 +north +ellps=WGS84 +units=m" shp = readShapeSpatial(fn.shp, proj4string = CRS(myproj)) ras = raster(fn.tif) plot(ras) plot(shp, bg="transparent", add=TRUE) 回答1: Overplotting raster plots with points, lines, and polygons should work just fine, as the following example shows. My best

Raster map with discrete color scale for negative and positive values R

帅比萌擦擦* 提交于 2019-12-03 09:31:42
I have two dataframes which I will like to map. The dfs have the same xy coordinates and I need a single colorbar with a visible discrete color scale for both dfs like the one shown here. I would like the colors in the colorkey to match the self-defined breaks. a more general solution that can be applied outside this example is much appreciated The RdYIBu color palette from the RcolorBrewer package is what I am after. My code so far: library(rasterVis) ras1 <- raster(nrow=10,ncol=10) set.seed(1) ras1[] <- rchisq(df=10,n=10*10) ras2=ras1*(-1)/2 s <- stack(ras1,ras2) Uniques <- cellStats(s,stat

R: Write RasterStack and preserve layer names

不羁岁月 提交于 2019-12-03 08:52:12
问题 I have a raster stack, stk , consisting of three raster images in R. Here is a simple example # set up a raster stack with three layers > library(raster) > r <- raster(nrows=10,ncols=10) > r[] <- rnorm(100) > stk <- stack(r,r,r) # layer names are set by default > names(stk) [1] "layer.1" "layer.2" "layer.3" I assign names to the raster layers: # set layer names to "one", "two" and "three" > names(stk) <- c('one','two','three') > names(stk) [1] "one" "two" "three" When I write the RasterStack

can raster create multi-layer objects with different modes?

↘锁芯ラ 提交于 2019-12-03 08:31:30
Can a raster object (in R) have layers of different mode (data type)? On the face of it it seems we are always forced to one type: library(raster) ## create a SpatialPixelsDataFrame with (trivially) two different "layer" types d <- data.frame(expand.grid(x = 1:10, y = 2:11), z = 1:100, a = sample(letters, 100, replace = TRUE), stringsAsFactors = FALSE) coordinates(d) <- 1:2 gridded(d) <- TRUE ## now coerce this to a raster brick or stack and our "a" is crushed to numeric NA all(is.na(getValues(brick(d)[[2]]))) [1] TRUE Is there anything like a rasterDataFrame? Also, note that we presumably

ggplot2: raster plotting does not work as expected when setting alpha values

半世苍凉 提交于 2019-12-03 07:12:14
问题 First post here, I hope I'm observing website etiquette. I couldn't find and answer on the site and I previously posted this to a ggplot2 specific group, but no solutions as yet. Basically I am trying to overlay two rasters using ggplot2 and require the top one to be semi-transparent. I have a hillShade raster which is computed from an elevation data raster, and I wish to overlay the elevation raster onto the hillshade raster so the resulting plot doesn't look 'flat'. You can see what I mean

How can I create raster plots with the same colour scale in R

人走茶凉 提交于 2019-12-03 04:43:50
问题 I'm creating some maps from raster files using the "raster" package in R. I'd like to create comparison rasters, showing several maps side by side. It's important for this that the colour scales used are the same for all maps, regardless of the values in each map. For example, if map 1 has values from 0-1, and map 2 has values from 0-0.5, cells with a value of 0.5 should have the same colour on both maps. For example: map 1 has values from 0 to 1 map 2 has values from 0 to 0.5 the colour goes

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)

How to replace NA's in a raster object

我只是一个虾纸丫 提交于 2019-12-03 02:10:11
I need to replace the NA 's in the raster object ( r ) from the example below. library(raster) filename <- system.file("external/test.grd", package="raster") r <- raster(filename) I also tried to remove these these (and place the result in a data.frame ), but to no avail. dfr <- as.data.frame(r, na.rm=T) summary(dfr) # test # Min. : 128.4 # 1st Qu.: 293.2 # Median : 371.4 # Mean : 423.2 # 3rd Qu.: 499.8 # Max. :1805.8 # NA's :6097 I'm not sure it makes sense to remove NA values from a raster object, but you can easily replace it. For example: oldpar <- par(mfrow=c(1, 2)) plot(r) r[is.na(r)] <-