The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
A major motivation we have when developing redlistr
was
to allow users to easily iterate through a large amount of data.
First, we provide an example showing how users can perform EOO and
AOO calculations on all .tif
files within a folder.
library(redlistr)
library(stringr)
# Example directory
<- # Path to folder with tif files
input_dir <- "C:/Users/Username/Desktop"
out_dir # List all files within input_dir that ends with .tif
<- list.files(input_dir, pattern = '.tif$')
input_list # Option to save shapefiles or not
<- T saveSHP
We also create an empty data frame to store our results in, with each row representing one file in the folder.
# set up data capture
<- data.frame (
results_df # Name of the raster
in.raster = NA,
# Estimated area of ecosystem
eco.area.km2 = NA,
# Spatial resolution of data
eco.grain = NA,
# EOO of ecosystem
eoo.area.km2 = NA,
# AOO of ecosystem
aoo.no = NA,
# AOO of ecosystem with at least 1% in each grid cell
aoo.1pc = NA,
# Time taken for the analysis to complete
time.taken = NA)
We use a for loop to tell R
to systematically go through
each tif file within the specified folder.
for (i in seq_along(input_list)){
# Prints out a message showing progress
message (paste("working on number... ", i, " of ", length(input_list)))
<- proc.time()
start_time <- input_list[i]
filename <- paste(input_dir, "\\", input_list[i], sep="")
input_string = raster(input_string)
rast NAvalue(rast) <- 0
<- getArea(rast)
eco.area.km2 message (paste("... area of ecosystem is", eco.area.km2, "km^2"))
<- paste(res(rast)[1], 'x', res(rast)[2])
eco.grain <- makeEOO(rast)
eoo.shp <- getAreaEOO(eoo.shp)
eoo.area.km2 message (paste("... area of EOO is", eoo.area.km2, "km^2"))
<- getAOO(rast, 10000, FALSE)
aoo.no message (paste("... number of occupied grid cells is", aoo.no, "10 x 10-km cells"))
.1pc <- getAOO(rast, 10000, TRUE)
aoomessage (paste("... number of AOO 1% grid cells is", aoo.1pc, "10 x 10-km cells"))
<- proc.time() - start_time
time_taken message (paste("file", i, "completed in ", time_taken))
# Saving the results into the data frame
$in.raster[i] <- filename
results_df$eco.area.km2[i] = eco.area.km2
results_df$eco.grain[i] = eco.grain
results_df$eoo.area.km2[i] = eoo.area.km2
results_df$aoo.no[i] = aoo.no
results_df$aoo.1pc[i] = aoo.1pc
results_df$time.taken[i] = time_taken
results_df
# Saving shapefiles
if(saveShps == TRUE){
shapefile(eoo.shp, paste0(out_dir, filename, "eoo"), overwrite=TRUE)
<- makeAOOGrid (rast, 10000, one.percent.rule = FALSE)
aoo.shp shapefile(aoo.shp, paste0(out_dir, filename, "aoo"), overwrite=TRUE)
<- makeAOOGrid (rast, 10000, one.percent.rule = TRUE)
aoo1.shp shapefile(aoo1.shp, paste0(out_dir, filename, "aoo1"), overwrite=TRUE)
}
}
# Printing a message when everything is completed
message ("Analysis complete.")
# Saving the outputs as a csv file
write.csv(results_df, paste(out_dir, "redlistr_analysis.csv"))
This example code demonstrates how a user could calculate the range
size metrics provided in redlistr
on all tif files within a
folder. Users can also parallelise the for loop using the
foreach
package.
Another case where users might want to iterate multiple inputs are when they have a single raster file which contains multiclass data.
The workflow here is very similar to the code provided above. The only difference is that we will be looping over every class within a raster, converting each of them into a binary layer and performing analyses on them iteratively.
library(redlistr)
library(stringr)
# Example directory
<- # raster(...)
input_rast <- "C:/Users/Username/Desktop"
out_dir # Option to save shapefiles or not
<- T saveSHP
We also create an empty data frame to store our results in, with each row representing one file in the folder.
# set up data capture
<- data.frame (
results_df # Name of the raster
raster.class = NA,
# Estimated area of ecosystem
eco.area.km2 = NA,
# Spatial resolution of data
eco.grain = NA,
# EOO of ecosystem
eoo.area.km2 = NA,
# AOO of ecosystem
aoo.no = NA,
# AOO of ecosystem with at least 1% in each grid cell
aoo.1pc = NA,
# Time taken for the analysis to complete
time.taken = NA)
We use a for loop to tell R
to systematically go through
each tif file within the specified folder.
<- freq(input_rast, useNA = "no") # get class values from raster
val_table <- val_table[,1] # convert table of values to vector
vals message('Raster has >>> ', length(vals) , ' <<< classes' )
for (val in vals){
# Prints out a message showing progress
message (paste("working on class", val))
<- proc.time()
start_time # Create temporary raster where values are the current class
<- input_rast == i
rast values(rast)[values(rast) == 0] <- NA
NAvalue(rast) <- 0
<- getArea(rast)
eco.area.km2 message (paste("... area of ecosystem is", eco.area.km2, "km^2"))
<- paste(res(rast)[1], 'x', res(rast)[2])
eco.grain <- makeEOO(rast)
eoo.shp <- getAreaEOO(eoo.shp)
eoo.area.km2 message (paste("... area of EOO is", eoo.area.km2, "km^2"))
<- getAOO(rast, 10000, FALSE)
aoo.no message (paste("... number of occupied grid cells is", aoo.no, "10 x 10-km cells"))
.1pc <- getAOO(rast, 10000, TRUE)
aoomessage (paste("... number of AOO 1% grid cells is", aoo.1pc, "10 x 10-km cells"))
<- proc.time() - start_time
time_taken message (paste("file", i, "completed in ", time_taken))
# Saving the results into the data frame
<- data.frame(
temp_df eco.class = val,
eco.area.km2 = eco.area.km2,
eco.grain = eco.grain,
eoo.area.km2 = eoo.area.km2,
aoo.no = aoo.no,
aoo.1pc = aoo.1pc,
time_taken = time_taken)
<- rbind(results_df, temp_df)
results_df # Saving shapefiles
if(saveSHP == TRUE){
shapefile(eoo.shp, paste0(out_dir, filename, "eoo"), overwrite=TRUE)
<- makeAOOGrid (rast, 10000, one.percent.rule = FALSE)
aoo.shp shapefile(aoo.shp, paste0(out_dir, filename, "aoo"), overwrite=TRUE)
<- makeAOOGrid (rast, 10000, one.percent.rule = TRUE)
aoo1.shp shapefile(aoo1.shp, paste0(out_dir, filename, "aoo1"), overwrite=TRUE)
}
}
# Printing a message when everything is completed
message ("Analysis complete.")
# Saving the outputs as a csv file
write.csv(results_df, paste(out_dir, "redlistr_analysis.csv"))
Similarly, the above code can be parallelised.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.