r-spatialecology / belg

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

Add parallelization #2

Open Nowosad opened 6 years ago

Nowosad commented 4 years ago

Parallelization works for the hierarchy method (https://github.com/r-spatialecology/belg/tree/parallel2), however for the aggregation method to work the whole needs to be rewritten.

Nowosad commented 3 years ago

https://github.com/r-spatialecology/belg/tree/p - parallelization seems to work - it is openmp (linux mostly).

Anali618 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.