R: RasterToPolygon works slow, then stop working - r

I have a problem to process rasters in R, even if I've done it previously. I can read the data and plot them:
however, when I tried to convert my raster to polygon, the R stops working
I've tried to update packages, re-install R, on OSX, on Windows 7, always with the same result.
Moreover, I've tried to set my projection to NA, but this neither helped. Did you ever experienced problem like this? Do you have some suggestions how to solve the problem? I highly appreciate any suggestions !
MY data are here: https://ulozto.sk/x4pHuyra/data-to-stack-zip
And my code:
library(sp)
library(raster)
library(rgeos)
library(spatstat)
library(rgdal)
library(maptools)
require(spdep)
# read final GFW raster - in JTSK
gfw_13<-raster("H:/.../gfw_ext_forest_03.tif")
projection(gfw_13)<-NA
# read shp
manag<-readOGR(dsn = "H:/...",
layer = "cleared_management_by_NP")
projection(manag)<-NA
# convert raster GFW to shp # !!!!! here it stops to work
pol.gfw<-rasterToPolygons(gfw_13, dissolve = T)

I don't have 50 rep yet, so I can't add a comment, but have a look at this web-page. It deals with the rasterToPolygons function, which I've always found to be mega-slow, and the author (John Baumgartner) offers a function of his to speed up the process. It takes about 1/7000th of the time, according to his tests.

This doesn't solve the speed problem, but at least my processes run and R doesn't crashes... I need to define all arguments for rasterToPolygon function:
pol.gfw <- rasterToPolygons(gfw_13, fun=NULL, n=4, na.rm=TRUE, digits=12, dissolve=TRUE)
Maybe the problem was due to abundant numbers of NA values in the original raster..

Related

How can I create a reproducible example of a SpatRaster (terra)?

For a question that is specific to my particular dataset, how can I make a reproducible example of that dataset if I have it stored as a SpatRaster in R?
The data structure is complex enough that I don't know how to freehand invent a simpler version and read it as a SpatRast (i.e. x <- rast(????????)
I also haven't been able to figure out how I could use a package or command to extract enough information to provide what is functionally a reproducible example either
See my previous question for an example: How can I add a class name to numeric raster values in a terra SpatRaster?
You can create objects from scratch like this:
library(terra)
r <- rast()
s <- rast(ncols=22, nrows=25, nlyrs=5, xmin=0)
See ?terra::rast for additional arguments you can use and for alternative approaches.
You can also use a file that ships with R. For example:
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
You can also create from scratch a new SpatRaster with (mostly) the same properties with what is returned by
as.character(r)
And then recreate it with something like
r <- rast(ncols=95, nrows=90, nlyrs=1, xmin=5.74166666666667, xmax=6.53333333333333, ymin=49.4416666666667, ymax=50.1916666666667, names=c('elevation'), crs='GEOGCRS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",ELLIPSOID[\"WGS 84\",6378137,298.257223563,LENGTHUNIT[\"metre\",1]]],PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],CS[ellipsoidal,2],AXIS[\"geodetic latitude (Lat)\",north,ORDER[1],ANGLEUNIT[\"degree\",0.0174532925199433]],AXIS[\"geodetic longitude (Lon)\",east,ORDER[2],ANGLEUNIT[\"degree\",0.0174532925199433]],ID[\"EPSG\",4326]]')
r <- init(r, "cell")
If you cannot replicate your error with example data, this may give you a hint about the problem. Does it have to do with NAs? The file being on disk? The file format? One tricky situation is where there is a difference if the real file is much larger. You can simulate a large file by setting terraOptions(todisk=TRUE) and using a steps argument in a function, e.g.
b <- clamp(x, steps=5)
If none of that allows you to replicate an error, your last resort is to provide a link to the file so that others can download it. If you cannot do that, then at least show the content of the SpatRaster x with show(x) and provide the code to create a similar object with as.character(x)

Read specific raster files and create a mean raster in R

I am desesperate, because my problem seems very simple, but I cannot find out how to manage it.
Aim:
I would like to read 1 to 4 raster files from a folder. The names of the one that I need are listed in a list as character.
After having opened the files, I would like to create a new raster corresponding to the mean of the files.
I can manage it on QGIS, but I need to automatize hte process, as I have a lot of individuals!
1) It should work with list.files(pattern = ) but as the names are in a list, I do not know how to do.
Ex: for the first individual, I have to read 2 files named 2018-12-27_sic.tif and 2018-12-27_sic_con.tif
I tried to read with readGDAL , open.GDAL it didn't work
thanks a lot for your valuable help
I would use the stack and calc functions from the raster package. The function stack creates a stack of rasters, all with the same resolution and extent, and makes it easy to do operations like take the mean of every cell. So:
library(raster)
fs <- list.files(pattern='tif$')
rasterstack <- stack(fs)
rastermean <- calc(rasterstack, fun=mean)
Note, if your rasters are not the same resolution, you will have to use the resample function, and if they are not the same extent, you will have to use crop. Typing in ?resample and ?crop in RStudio will show you instructions for using those functions.

Load raster as matrix

Is there an easy way to load a raster directly into R as a matrix, instead of loading the raster, then using as.matrix() to transform it into a matrix, i.e.
myras <- raster("file.tif")
mymat <- as.matrix(myras)
Another possible solution would be to use as like this:
library(raster)
mymat <- as(raster("file.tif"), "matrix")
Keep in mind, that reading a matrix directly from a file is not always an option. Since you are using a tiff file, you might have a compressed raster (e.g. by LZW, Packbits, etc.). Thus it is necessary to load and uncompress the raster first, before accessing the raster values and transform them into a matrix.
There are similar alternatives, but I do not think there is an easier way. (except perhaps for png and some other graphics formats?). Without further explanation, it seems an odd question, as what you show is very concise. You can combine your two statements into one line (adding 8 characters to as.matrix)
library(raster)
myras <- as.matrix(raster("file.tif"))

Explaining Simple Loop in R

I successfully wrote a for loop in R. That is okay and I am very happy that it works. But I also want to understand what I've done exactly because I will have to work with loops later on in my analysis as well.
I work with Raster Data (DEMs). I load them into the environment as rasters and then I use the getValues function in the loop as I want to do some calculations. Looks as follows:
list <- dir(pattern=".tif", full.names=T)
tif.files <- list()
tif.files.values <- tif.files
for (i in 1: length(list)){
tif.files[[i]] <- raster (list[[i]])
tif.files.values[[i]] <- getValues(tif.files[[i]])
}
Okay, so far so good. I don't get why I have to specify tif.files and tif.files.values before I use them in the loop and I don't know why to specify them exactly how I did that. For the first part, the raster operation, I had a pattern. Maybe someone can explain the context. I really want to understand R.
When you do:
tif.files[[i]] <- raster (list[[i]])
then tif.files[[i]] is the result of running raster(list[[i]]), so that is storing the raster object. This object contains the metadata (extent, number of rows, cols etc) and the data, although if the tiff is huge it doesn't actually read it in at the time.
tif.files.values[[i]] <- getValues(tif.files[[i]])
that line calls getValues on the raster object, which reads the values from the raster and returns a vector. The values of the grid cells are now in tif.files.values[[i]].
Experiment by printing tif.files[[1]] and tif.files.values[[1]] at the R prompt.
Note
This is R, not RStudio, which is the interface you are using that has all the buttons and menus. The R language exists quite happily without it, and your question is just a language question. I've edited and tagged it now for you.

Handling multiple raster files and executing unit conversions on them: R

I've dug around a lot for an answer to this and wasn't able to find anything, so here I am.
I have a whole bunch of ascii raster files corresponding to air temperature and dew point temperature of a certain area over 744 hourly time steps. (So I have 744 air temp and 744 dew point files corresponding to a 31-day month). The files are only about 45 kB each.
I want to stack them together so I can perform some analyses on them, and I also want to convert their units from K to deg F.
The file names air Tair1.txt, Tair2.txt, ... Tair744.txt and Eair1.txt, Eair2.txt, ... Eair744.txt.
Using the raster package, I can easily load all the files as rasters:
for (i in 1:744) {
assign(paste0("Tair",i),raster(paste0("Tair",i,".txt")))
assign(paste0("Eair",i),raster(paste0("Tair",i,".txt")))
}
I've tried to use ls() with pattern or glob2rx to define just the raster file names and
then do conversions on them, or to do something similar to join them in a stack, but to no avail. I also tried mget, values(mget(filename)) and things like that to get at the values in a loop.
I know R doesn't handle large datasets very well, but I'm thinking these aren't really that large so there should be something pretty simple?
I would appreciate any help and advice! Thank you.
The raster package's RasterStack is for this:
library(raster)
files <- paste0("Tair",1:744,".txt")
rs <- stack(files)
Why do you have these files in text format though? Who imposed this disaster on you? I suspect your individual layers have insufficient metadata, so try one and see if it's sensible. You can use extent(rs) <- and projection(rs) <- to fix:
r <- raster(files[1])
print(r)
Don't use assign() that's just creating a mess.

Resources