如何制作保留国家边境网格的地图网格?

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

我想制作一个网格地图(六边形),同时保留边界的网格。
正如您从我在下面分享的代码中看到的,网格在国家(葡萄牙)边境被裁剪,但我希望保留边境的网格。

你能帮我解决这个问题吗?

谢谢

library(tidyverse)
library(sf)


nuts1_continente <- giscoR::gisco_get_nuts(
  nuts_id = "PT1",
  year = "2021", # depends on the data, if old or recent
  resolution = "1", # 3
  nuts_level = "1",
  update_cache = TRUE
)


continente_utm <-
  st_transform(nuts1_continente, 25829)

################### Create Grid - HEX Intersected ####

hex_grid_map <- continente_utm |>
  sf::st_make_grid(
    square = FALSE, cellsize = 22800, crs = st_crs(continente_utm)
  ) |>
  st_intersection(continente_utm) |>
  st_sf() |>
  mutate(grid_id = seq_len(n()))

ggplot() +
  geom_sf(data = continente_utm) +
  geom_sf(data = hex_grid_map, alpha = 0.3) +
  theme_void()
r ggplot2
1个回答
0
投票

这是我生成六角网格的工作流程。此过程可确保六边形根据您指定的边界保持未剪裁

# 1. Code for downloading data is the same as yours.

# 2. I create an object and update the CRS according to the projection.

portugal_update_crs <- st_transform(continente_utm, crs = 25829)

# 3. Build a second object for the hex grid. 

hex_grid <- continente_utm %>% 
  st_make_grid(square = FALSE,
               cellsize = 22800,  
               crs = st_crs(continente_utm)) 

# 4. Select the hexagons that are included in the first object. This process does not crop the hexagons.

portugal_hex <- hex_grid[port_new_crs]

# final plot

portugal_hex %>%
ggplot()+
geom_sf()+
theme_void()

Portugal hex bins

© www.soinside.com 2019 - 2024. All rights reserved.