canderson156
canderson156

Reputation: 1281

Creating sf polygons from a dataframe

I have a dataframe containing the coordinates of a set of polygons. This is how I would convert it to a spatialPolygons (package 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)

How can I create an sf object (package sf) containing these two polygons directly from my.df?


Edit: My question is partially answered in this question, but their response only illustrates how to create a single polygon. How do I create multiple polygons?

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

Upvotes: 9

Views: 9468

Answers (3)

SymbolixAU
SymbolixAU

Reputation: 26248

library(sfheaders) lets you construct an sf object from a data.frame directly

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

Upvotes: 8

canderson156
canderson156

Reputation: 1281

I figured out an answer based on paqmo's suggestion to look at Convert sequence of longitude and latitude to polygon via sf in R

The answer provided in that question groups all the points in the data frame as a single polygon. I've added a step to group the dataframe by the variable that identifies the polygon.

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

Upvotes: 6

mrhellmann
mrhellmann

Reputation: 5489

This is somewhat convoluted since your points are not in the right order to create a polygon, but it works. There is likely a simpler answer that I'm overlooking.

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)

Created on 2020-04-14 by the reprex package (v0.3.0)

Upvotes: 0

Related Questions