英文:
How define the cell size from a hexagonal grid?
问题
我试图从生态区边界(cerrado,但也适用于任何其他政治边界)创建一个六边形网格。在sf文档中,我看到可以使用st_make_grid函数来实现,但没有明确说明如何定义单元格的大小。我想要创建一个面积为25平方公里的六边形网格。
我尝试使用以下函数:
grid = sf::st_make_grid(x = shpCerrado, cellsize = 0.4, square = FALSE)
我使用了大小为0.4,因为一位朋友在QGis中进行了比较,并告诉我这个大小有25米的边。但我们不确定单元格的总面积(我们希望它是25平方公里)。
我尝试寻找一些包的文档或其他教程,解释如何定义六边形网格的单元格面积,但我找不到。有人知道如何进行这个计算吗?或者是否有其他方法在R中创建这个网格?
英文:
I'm trying to create a hexagonal grid from a biome boundaries (cerrado, but it could be able to apply to any other political boundaries). In sf documentation I saw that it is possible to do it with the st_make_grid function, but does not clear how the size cell are defined. I would like to make a hexagonal grid with 25km2 size cell.
I've tried to use this function:
> grid = sf::st_make_grid(x= shpCerrado, cellsize = 0.4, square = FALSE)
I used the size 0.4 because a friend compared in QGis and told that this size had a 25m side. But we dont have sure about the total area of the cell (which we would like that was 25km2).
I tried to find some package docs or another tutorial that explained how to define the cell area of a hexagonal grid, but I couldn't. Does anyone know how this calculation could be done? Or is there another way to make this grid in R?
答案1
得分: 1
你可以从六边形的面积通用公式推导出目标单元格大小;通过一点代数运算,你会得到 sqrt(2*cell_area/sqrt(3))
。
或者考虑这段代码;它覆盖了来自 nc.shp
(随 {sf}
一起提供的)的亚斯县,在一个25平方千米的网格中。
library(sf)
library(dplyr)
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
slice(1) %>%
st_geometry() %>%
st_transform(3857) # 转换为度量CRS
cell_area <- units::as_units(25, "km^2") # 目标网格大小
grid_spacing <- sqrt(2*cell_area/sqrt(3)) # 从面积计算的六边形大小
grid <- nc %>%
st_make_grid(square = FALSE, cellsize = grid_spacing) # 创建网格
plot(nc, col = "red")
plot(grid, add = TRUE)
<details>
<summary>英文:</summary>
You can derive the target cell size from the generic formula for area of hexagon; with a little algebra you will arrive at `sqrt(2*cell_area/sqrt(3))`
Or consider this piece of code; it covers county Ashe from the well known `nc.shp` that ships with `{sf}` in a 25 km² grid.
```r
library(sf)
library(dplyr)
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
slice(1) %>% # coiunty Ashe only
st_geometry() %>% # no need for data anymore
st_transform(3857) # transform to a metric CRS
cell_area <- units::as_units(25, "km^2") # target grid size
grid_spacing <- sqrt(2*cell_area/sqrt(3)) # size of hexagon calculated from area
grid <- nc %>%
st_make_grid(square = F, cellsize = grid_spacing) # make the grid
plot(nc, col = "red")
plot(grid, add = T)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论