r-spatialecology / belg

Boltzmann entropy of a landscape gradient
https://r-spatialecology.github.io/belg/
Other
18 stars 5 forks source link

Use of other datasets #18

Closed Nowosad closed 9 months ago

Nowosad commented 9 months ago
> library(dplyr)
> library(tmap)
> library(rcartocolor)
> library(belg)
> library(raster)
> library(sf)
> ah_dem = raster("F:hfdem202412/hfdem202412.tif")
> 
> ah_grid_geom = st_as_sfc(st_bbox(ah_dem))
> ah_grid = st_make_grid(ah_grid_geom, n = c(30, 40))
> ah_grid = st_sf(id = seq_along(ah_grid),
+                  geom = ah_grid)
> 
> # 使用disaggregate函数来调整栅格数据的大小
> r_resampled <- disaggregate(ah_dem, fact=c(new_cols/ncols, new_rows/nrows))
> # 计算新的行数和列数,使其能够被2整除
> new_rows <- ceiling(nrows / 2) * 2
> new_cols <- ceiling(ncols / 2) * 2
> 
> tm_slo_dem = tm_shape(ah_dem) +
+   tm_raster(style = "cont", 
+             breaks = c(-100, 0, 100, 200, 300, 400, 500, 600),
+             labels = c("-100","0","100","200","300","400","500","600"),
+             title = "Elevation \n(m asl)",
+             palette = carto_pal(n = 8, name = "Temps"),
+             midpoint = NA) +
+   tm_shape(ah_grid, is.master = TRUE) +
+   tm_borders(col = "black", lwd = 0.2) +
+   tm_layout(legend.outside = TRUE,
+             frame = FALSE)
> 
> 
> 
> tm_slo_dem
> 
> # it takes a few minutes
> ah_grid$results = NA
> for (i in seq_len(nrow(ah_grid))){
+   small_raster = crop(ah_dem, ah_grid[i, ])
+   if(!all(is.na(getValues(small_raster)))){
+     ah_grid$results[i] = get_boltzmann(small_raster)
+   } 
+ }
错误: Number of rows and columns must be a multiple of 2.

I'm sorry for the inconvenience. I've verified that the window size can be evenly divided both horizontally and vertically. Besides that, I have another question: how can I incorporate other datasets, such as slope and land use, into the entropy calculation? Thank you very much.

Originally posted by @Anali618 in https://github.com/r-spatialecology/belg/issues/2#issuecomment-1873966563

Nowosad commented 9 months ago

Hi @Anali618,

yes, you could use other datasets to calculate Boltzmann entropy. There are some caveats, though:

  1. Slope is a continuous variable. Thus, to use it, I would suggest you round its values to the integers.
  2. Land use, on the other hand, is a categorical variable. The approach used in this package is based on averaging values in smaller windows. However, calculating an average does not make sense for categorical variables. If you want to calculate Boltzmann entropy for categorical data, you may take a look at https://github.com/nowosad/bespatial, and, for example, function bes_m_zhao().
Anali618 commented 9 months ago

library(dplyr) library(tmap) library(rcartocolor) library(belg) library(raster) library(sf)

svn_dem = raster("F:anqingdem202412/202414anqing1.tif")

svn_grid_geom = st_as_sfc(st_bbox(svn_dem)) svn_grid = st_make_grid(svn_grid_geom, n = c(24, 30)) svn_grid = st_sf(id = seq_along(svn_grid),

  • geom = svn_grid)

tm_slo_dem = tm_shape(svn_dem) +

  • tm_raster(style = "cont",
  • breaks = c(-100,0, 500, 1000, 1500, 2000),
  • labels = c("-100", "0", "500", "1000", "1500", "2000"),
  • title = "Elevation \n(m asl)",
  • palette = carto_pal(n = 7, name = "Temps"),
  • midpoint = NA) +
  • tm_shape(svn_grid, is.master = TRUE) +
  • tm_borders(col = "black", lwd = 0.2) +
  • tm_layout(legend.outside = TRUE,
  • frame = FALSE)

tm_slo_dem stars object downsampled to 944 by 1059 cells. See tm_shape manual (argument raster.downsample)

it takes a few minutes

svn_grid$results = NA for (i in seq_len(nrow(svn_grid))){

  • small_raster = crop(svn_dem, svn_grid[i, ])
  • if(!all(is.na(getValues(small_raster)))){
  • svn_grid$results[i] = get_boltzmann(small_raster)
  • }
  • } 错误: Number of rows and columns must be a multiple of 2. well,Sorry, I have debugged the TIFF file I input, and its dimensions (1798, 2016) are divisible by 2. However, the program still reports an error. How should we proceed? Also, I have adjusted the pixel size of my raster data to match the resolution in your SVN example. Are there any specific requirements for the input TIFF file? 60ab9dca95583889b618cc61e39a58a
Nowosad commented 9 months ago

Hi @Anali618 sorry -- I was not clear -- Number of rows and columns must be a multiple of 2, but also both dimensions must be the same (square raster).

See https://r-spatialecology.github.io/belg/articles/belg2.html. In your case, I would suggest relative hierarchy-based method.

Anali618 commented 9 months ago

Thanks to Dr. Nowosad for your great support and sharing your knowledge on land complexity metrics! I sincerely thank you for your insightful response and valuable corrections to my questions about ecological complexity.Your expertise in this field has significantly enhanced my understanding, and I deeply appreciate the time and effort you took to clarify my doubts. I have decided to reconsider the structure and role of the entropy calculation based on DEM in my thesis, and to make adjustments according to your suggestions. Once again, I express my gratitude for your selfless sharing.I look forward to further exchanges with you in my later research.

Nowosad commented 9 months ago

@Anali618 good luck with your work!