R SF – How to Create Polygon from Minimum X and Y Coordinates

rsf

I am trying to create a polygon from a coordinate dataframe. I have the minimum and maximum x-y coordinate information. But, I am getting the following error. How can this be fixed?

library(tidyverse)
library(sf)
lon = c(756065.70, 757428.78)
lat = c(4074435.19,4075144.12)

Poly_Coord_df = data.frame(lon, lat)

Poly = Poly_Coord_df %>% 
  st_as_sf(coords = c("lon", "lat"), crs = 32611) %>% 
  summarise((geometry = st_combine(geometry))) %>% 
  st_cast("POLYGON")

Error

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': polygons require at least 4 points

Best Answer

In the specific context you describe - having top right and bottom left corner of your desired polygon - you may be able to get by with sf::st_bbox(). It returns the bounding box of an object, in your case of the Poly_Coords_df data frame (as intepreted in context of EPSG:32611).

poly <- Poly_Coord_df %>% 
  st_as_sf(coords = c("lon", "lat"), 
           crs = 32611) %>% 
  st_bbox() %>% 
  st_as_sfc()