计算二进制存在-不存在映射的像素;计算题

问题描述 投票:0回答:0

我想计算一个 tif 文件格式的物种出现的区域。为此,我将栅格对象转换为二进制存在-不存在图层,所有非零值都设置为 1。我计算了表示物种出现的二进制栅格的总面积。我认为这段代码会将每个出现的彩色像素计算为栖息地的一部分,并将排除所有其他无人居住的像素。但是,我为每个 tif 文件获得了相同的值。每个文件的分辨率和范围都相同(res = 0.0306 0.0180),结果始终为 583.35 平方米。

# Load the raster package
library(raster)

# Set working directory
setwd("E:/univ/biologie/3e bach/bachelorproef R/plotten total area")

# Create an empty dataframe to store the results
results_df <- data.frame(filename = character(),
                         total_area = numeric())

# Get the list of tif files in the directory
tif_files <- list.files(pattern = "\\.tif$")

# Loop through all tif files in the directory
for (tif_file in tif_files) {
  
  # Read the TIFF file into R as a raster object
  species_raster <- raster(tif_file)
  
  # Convert the raster object into a binary presence-absence layer, with all non-zero values set to 1
  binary_raster <- species_raster
  binary_raster[species_raster > 0] <- 1
  
  # Calculate the total area of the binary raster that represents occurrences of the species
  total_area <- cellStats(binary_raster, sum) * res(species_raster)[1] * res(species_raster)[2]
  
  # Print the total area in square meters
  cat(paste0("Total area inhabited by the species in ", tif_file, " is ", round(total_area, 2), " square meters.\n"))
  
  # Add the results to the dataframe
  results_df <- rbind(results_df, data.frame(filename = tif_file, total_area = total_area))
}

# Print the final results dataframe
results_df
r maps pixel tiff
© www.soinside.com 2019 - 2024. All rights reserved.