从数据框创建SF多边形

时间:2020-04-14 19:47:59

标签: r sf

我有一个数据框,其中包含一组多边形的坐标。这就是我将其转换为spatialPolygons(程序包sp)的方式

my.df <- data.frame(
  Plot = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
  Corner = c("SW", "NW", "NE", "SE", "SW2", "SW", "NW", "NE", "SE", "SW2"),
  Easting = c(511830, 512230, 512230, 511830, 511830, 511730, 512130, 512130, 511730, 511730),
  Northing = c(7550903, 7550903, 7550503, 7550503, 7550903, 7550803, 7550803, 7550403, 7550403, 7550803))


utm18 <- CRS("+init=EPSG:26918")

my.sp <- df_to_SpatialPolygons(my.df, keys = "Plot", coords = c("Easting", "Northing"), utm18)

plot(my.sp)

如何直接从my.df中创建一个包含这两个多边形的sf对象(包sf)?


编辑:我的问题在这个问题中得到部分回答,但是他们的回答仅说明了如何创建单个多边形。如何创建多个多边形?

Convert sequence of longitude and latitude to polygon via sf in R

3 个答案:

答案 0 :(得分:2)

library(sfheaders)可让您直接从data.frame构造sf对象

library(sf)
library(sfheaders)

sf <- sfheaders::sf_polygon(
  obj = my.df
  , x = "Easting"
  , y = "Northing"
  , polygon_id = "Plot"
)
sf::st_crs( sf ) <- 26918

sf

# Simple feature collection with 2 features and 1 field
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 511730 ymin: 7550403 xmax: 512230 ymax: 7550903
# z_range:        zmin: NA zmax: NA
# m_range:        mmin: NA mmax: NA
# CRS:            EPSG:26918
# id                       geometry
# 1  1 POLYGON ((511830 7550903, 5...
# 2  2 POLYGON ((511730 7550803, 5...


plot( sf )

enter image description here

答案 1 :(得分:0)

这有点令人费解,因为您的点创建多边形的顺序不正确,但是可以使用。我可能会忽略一个更简单的答案。

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(tidyverse)

# Your data
my_df <- data.frame(
  Plot = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
  Corner = c("SW", "NW", "NE", "SE", "SW2", "SW", "NW", "NE", "SE", "SW2"),
  Easting = c(511830, 512230, 512230, 511830, 511830, 511730, 512130, 512130, 511730, 511730),
  Northing = c(7550903, 7550903, 7550503, 7550503, 7550903, 7550803, 7550803, 7550403, 7550403, 7550803))


## Create an sf object from your data,
##  grouped by 'Plot' column summarise() combines the geomtries by group,
##  st_convex_hull() since the points are out of order for a polygon
my_df_sf <- st_as_sf(my_df,
                     coords = c('Easting', 'Northing')) %>%
  st_set_crs(26918) %>%
  group_by(Plot) %>%
  summarise() %>%
  ungroup() %>%  # Just in case
  st_convex_hull()


## A look at the data as an sf object
my_df_sf
#> Simple feature collection with 2 features and 1 field
#> geometry type:  POLYGON
#> dimension:      XY
#> bbox:           xmin: 511730 ymin: 7550403 xmax: 512230 ymax: 7550903
#> CRS:            EPSG:26918
#> # A tibble: 2 x 2
#>   Plot                                                                  geometry
#>   <fct>                                                            <POLYGON [m]>
#> 1 A     ((511830 7550503, 511830 7550903, 512230 7550903, 512230 7550503, 51183…
#> 2 B     ((511730 7550403, 511730 7550803, 512130 7550803, 512130 7550403, 51173…

## ggplot2 plot, colored by 'Plot' column
ggplot(my_df_sf) + 
  geom_sf(aes(color = Plot), fill = NA)

reprex package(v0.3.0)于2020-04-14创建

答案 2 :(得分:0)

我根据paqmo的建议Convert sequence of longitude and latitude to polygon via sf in R

得出了答案

该问题中提供的答案将数据框中的所有点分组为一个多边形。我添加了一个步骤,以通过标识多边形的变量对数据框进行分组。

polygon <- my.df %>%
  st_as_sf(coords = c("Easting", "Northing"), crs = utm18) %>%
  group_by(Plot) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON")