How to create thiessen polygons from points using R packages?

前端 未结 3 1106
名媛妹妹
名媛妹妹 2020-12-05 12:01

I have multiple sets of points (for different years ~20)

I want to generate thiessen polygons for each set of points using r spatial packages.

I know this c

3条回答
  •  眼角桃花
    2020-12-05 12:42

    Note that Voronoi cells are also known as Thiessen polygons.

    Alternatively, one can make use of the Simple Features for R, specifically, the sf::st_voronoi() function. Below are examples inspired from this help page:

    library(sf)
    #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
    
    # generate some random points
    set.seed(2020-05-27)
    n <- 50
    points <- runif(n) %>% 
      matrix(ncol = 2) %>% 
      st_multipoint()
    
    # Voronoi tesselation
    voronoi_grid <- st_voronoi(points)
    
    plot(voronoi_grid, col = NA)
    plot(points, add = TRUE, col = "blue", pch = 16)
    

    Created on 2020-05-27 by the reprex package (v0.3.0)

    Since you mentioned that you have multiple sets of points, one set for a year, you can make use of lists and iterate over them, for example:

    library(sf)
    #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
    
    # generate a list of length 20, each element containing with random points 
    set.seed(2020-05-27)
    yrs <- 20
    n <- 50
    points_lst <- vector(mode = "list", length = yrs)
    for (i in 1:yrs) {
      points_lst[[i]] <- runif(n) %>% 
        matrix(ncol = 2) %>% 
        st_multipoint()
    }
    
    # Voronoi tesselation for each element of the list
    voronoi_grids_lst <- lapply(points_lst, st_voronoi)
    
    # plot 1st element
    plot(voronoi_grids_lst[[1]], col = NA)
    

    Created on 2020-05-27 by the reprex package (v0.3.0)

提交回复
热议问题