r-raster

Turn extraction list to csv file

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 09:10:57
问题 I have uploaded a raster file and polyline shapefile into R and use the extract function to to extract the data from every pixel along the polyline. How do I turn the list output by extract into a CSV file? 回答1: If I understand what it is you're asking, I think you could resolve your situation by using unlist(). d <- c(1:10) # creates a sample data frame to use d <- as.list(d) # converts the data frame into a list d <- unlist(d) # converts the list into a vector 回答2: Always include a simple

Focal function: How do I get the value of the center pixel and use this value within the focal fun argument?

百般思念 提交于 2019-12-13 05:54:04
问题 Example with a 3*3 neighborhood where the sum is multiplied with the center value "center". library(raster) r <- raster(ncol=10, nrow=10) r[] <- 1:100 f<-function(x,center) {sum(x)*center} r.f <- focal(r, w=matrix(1,3,3),fun=f} 回答1: Carl's approach will work. It might be more efficient to do: library(raster) r <- raster(ncol=10, nrow=10) r[] <- 1:100 r.f <- focal(r, w=matrix(1,3,3)) * r 回答2: Interesting. Since the argument fun must accept multiple values, it may well be the case that you

Can color palette be specified for rasterImage?

余生颓废 提交于 2019-12-13 04:35:45
问题 Title says it all, pretty much. So far as I can tell, trying to pass a simple color palette by using the col argument, e.g., rasterImage(mydata,xleft=1,ybottom=1,xright=2,ytop=2,col=grey(0:4095/4096)) fails. What's more confusing is that the default palette seems to depend on the class of mydata . If I have a matrix, rasterImage(as.raster(mymatrix), {etc} plots in grayscale, while using a RasterLayer object, rasterImage(as.raster(myRaster),{etc} plots in a topo color set. Any suggestions for

Extract number of raster cells included in a SpatialPolygons

最后都变了- 提交于 2019-12-12 16:51:23
问题 I would like to create a function to count the number of raster cells within the polygons of a SpatialPolygonsDataframe object and adding the value as a new column without using a loop. I cannot find how to do it... Here is my code: library(sp) library(raster) # Create a SpatialPolygonsDataframe and a raster objets to overlay # Polygons p1 <- rbind(c(-180,-20), c(-140,55), c(-50, 0), c(-140,-60), c(-180,-20)) p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)) p3 <- rbind(c(-125

How to batch process geoTIFFs in R with lapply

ぃ、小莉子 提交于 2019-12-12 09:04:30
问题 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

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

孤者浪人 提交于 2019-12-12 08:44:41
问题 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

Sum nlayers of a rasterStack in R

折月煮酒 提交于 2019-12-12 07:13:23
问题 I am working with daily observation of climate data organized in .nc files. I read them using the stack command of the raster package. Each file (corresponding to a year) is a RasterStack element with the following characteristics: class : RasterStack dimensions : 360, 720, 259200, 365 (nrow, ncol, ncell, nlayers) resolution : 0.5, 0.5 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) each layer is the raster of values of a day. I would like to sum the layers in order to calculate

How to filter HadISST raster data in a range of months in R?

ε祈祈猫儿з 提交于 2019-12-12 06:36:34
问题 I'm trying to study the Sea Surface Temperature (SST) correlation with tropical-cyclone activity in a certain range of months. The data I'm using is from the Hadley Centre (in NetCDF format) using the get_anual_ssts() function from the hadsstR package. get_annual_ssts <- function(hadsst_raster, years = 1969:2011) { mean_rasts <- apply(matrix(years), 1, function(x) { yearIDx <- which(chron::years(hadsst_raster@z$Date) == x) subset_x <- raster::subset(hadsst_raster, yearIDx) means <- raster:

Calculating number of three consecutive values above a threshold (in raster stack)

老子叫甜甜 提交于 2019-12-12 04:34:14
问题 I need to compute number of three consecutive days when value of each pixel in a raster stack ( x ) is above a given threshold (defined by another raster y ). I tried using rle for the purpose with calc as follows after stacking x and y together into new raster a : library(raster) fn<-function(a) with(rle(a), sum(lengths>=3 & values>a[[nlayers(a)]])) calc(b,fn) However, I am getting the error: Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function Reproducible

R: Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'— raster data

自作多情 提交于 2019-12-12 03:15:05
问题 Im working in Studio with a raster .tif image. I have watched a tutorial on plotting the raster with the code below, however it does not work for me. I get the error: Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double' I have loaded the necessary packages( raster and rgdal ) I have also tried loading the arulesViz , yet get the same error YIELD <- raster("//Users//DevinOsborne//Desktop//Thesis//QGIS projects //Project//Rasters//Images//Yield_wheat.tif") plot(YIELD,main=