BjnNowak / frex

6 stars 1 forks source link

How to create the grid base map of a country? #1

Closed lbarqueira closed 7 months ago

lbarqueira commented 7 months ago

Hello, first of all, thank you for sharing your knowledge.

Could you please tell how to create a map hexagonal grid of a country? You use France, but how did you get it?

Thank you very much.

Luis Barqueira

BjnNowak commented 7 months ago

Hi,

There is the function st_make_grid() for that. https://rdrr.io/cran/sf/man/st_make_grid.html

You can choose between rectangular or hexagonal grid.

Best

Le mar. 7 mai 2024 à 01:15, Luis Barqueira @.***> a écrit :

Hello, first of all, thank you for sharing your knowledge.

Could you please tell how to create a map hexagonal grid of a country? You use France, but how did you get it?

Thank you very much.

Luis Barqueira

— Reply to this email directly, view it on GitHub https://github.com/BjnNowak/frex/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AT4TWIWO5DHQIYVJ3XDB3NDZBAFJZAVCNFSM6AAAAABHJ43AYWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI4DCOJUGIYDAOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

lbarqueira commented 7 months ago

Hi, thank you for your response.

I tried to obtain the map of France as it exists in your repo: https://github.com/BjnNowak/frex_db/raw/main/map/hex_grid.gpkg

Could you please see if the code is correct. It seems that the number of cells do not match in both maps (from your repo and from my code)!!!!

Thank you, Luis

# Load packages
library(tidyverse)
library(sf)

# Load basemap

# basemap of "gridded" France.
# gridded map of France in hexagons of about 450 km2

# see: https://github.com/BjnNowak/frex

hex <- read_sf("https://github.com/BjnNowak/frex_db/raw/main/map/hex_grid.gpkg")

st_crs(hex) # EPSG:2154

# Number of cells
terra::ncell(hex) # 4098

# Make plot
ggplot(hex) +
  geom_sf()

# Create grid map of France 

nuts1_france <- giscoR::gisco_get_nuts(
  nuts_id = c(
    "FR1",
    "FRB",
    "FRC",
    "FRD",
    "FRE",
    "FRF",
    "FRG",
    "FRH",
    "FRI",
    "FRJ",
    "FRK",
    "FRL",
    "FRM"
  ),
  year = "2021", # depends on the data, if old or recent
  resolution = "1", 
  nuts_level = "1",
  update_cache = TRUE
)
plot(nuts1_france)

france_sf <- nuts1_france |>
  st_union() |>
  st_as_sf() |>
  st_transform(crs = "EPSG:2154") |>
  st_make_valid()

plot(france_sf)

st_crs(france_sf)

hex_grid <- france_sf |>
  sf::st_make_grid(square = FALSE, cellsize = 25000) |>
  st_intersection(france_sf)

hex_grid |>
  ggplot() +
  geom_sf() +
  coord_sf(crs = 2154)

# Number of cells: different from original map !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
terra::ncell(hex_grid) # [1] 1151
BjnNowak commented 7 months ago

In st_make_grid there are arguments to control for number of cells in lat and lon (may be easier that to control for cell size)

Le mar. 7 mai 2024 à 14:37, Luis Barqueira @.***> a écrit :

Hi, thank you for your response.

I tried to obtain the map of France as it exists in your repo: https://github.com/BjnNowak/frex_db/raw/main/map/hex_grid.gpkg

Could you please see if the code is correct. It seems that the number of cells do not match in both maps (from your repo and from my code)!!!!

Thank you, Luis

Load packages

library(tidyverse) library(sf)

Load basemap

basemap of "gridded" France.

gridded map of France in hexagons of about 450 km2

see: https://github.com/BjnNowak/frex

hex <- read_sf("https://github.com/BjnNowak/frex_db/raw/main/map/hex_grid.gpkg")

st_crs(hex) # EPSG:2154

Number of cells

terra::ncell(hex) # 4098

Make plot

ggplot(hex) + geom_sf()

Create grid map of France

nuts1_france <- giscoR::gisco_get_nuts( nuts_id = c( "FR1", "FRB", "FRC", "FRD", "FRE", "FRF", "FRG", "FRH", "FRI", "FRJ", "FRK", "FRL", "FRM" ), year = "2021", # depends on the data, if old or recent resolution = "1", nuts_level = "1", update_cache = TRUE ) plot(nuts1_france)

france_sf <- nuts1_france |> st_union() |> st_as_sf() |> st_transform(crs = "EPSG:2154") |> st_make_valid()

plot(france_sf)

st_crs(france_sf)

hex_grid <- france_sf |> sf::st_make_grid(square = FALSE, cellsize = 25000) |> st_intersection(france_sf)

hex_grid |> ggplot() + geom_sf() + coord_sf(crs = 2154)

Number of cells: different from original map !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

terra::ncell(hex_grid) # [1] 1151

— Reply to this email directly, view it on GitHub https://github.com/BjnNowak/frex/issues/1#issuecomment-2098308604, or unsubscribe https://github.com/notifications/unsubscribe-auth/AT4TWITCIDVRAXOLXIBY6MLZBDDJPAVCNFSM6AAAAABHJ43AYWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJYGMYDQNRQGQ . You are receiving this because you commented.Message ID: @.***>

lbarqueira commented 7 months ago

Thank you.