This function converts a data frame containing SQL-style geometry coordinates
(four corner points per polygon) into an sf polygon object.
The SQL-style geometry is returned from spatial SQL tables like SURVEY_SITE
and SURVEY_SITE_HISTORIC in GFBioSQL.
Arguments
- .d
A data frame with columns
pt1_lon,pt1_lat,pt2_lon,pt2_lat,pt3_lon,pt3_lat,pt4_lon, andpt4_lat, representing the four corners of polygons. The function expects these exact column names Additional columns will be preserved in the output.- crs
An integer specifying the coordinate reference system (CRS). Defaults to
4326(WGS 84). The function will warn if you specify WGS84 (4326) but provide coordinates that appear to be in a projected system (e.g., UTM coordinates with values outside the longitude range -180 to 180).
Examples
if (FALSE) { # \dontrun{
# Example with data from GFBioSQL
.d <- gfdata::get_active_survey_blocks(active_only = TRUE)
sql_geom_to_sf(.d, crs = 4326)
} # }
# Example with WGS84 coordinates (standalone, no database required)
wgs84_data <- data.frame(
pt1_lon = -123.0, pt1_lat = 48.0,
pt2_lon = -122.9, pt2_lat = 48.0,
pt3_lon = -122.9, pt3_lat = 48.1,
pt4_lon = -123.0, pt4_lat = 48.1,
site_id = "A1"
)
sql_geom_to_sf(wgs84_data, crs = 4326) # WGS84 coordinates
#> Simple feature collection with 1 feature and 1 field
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -123 ymin: 48 xmax: -122.9 ymax: 48.1
#> Geodetic CRS: WGS 84
#> site_id geometry
#> 1 A1 POLYGON ((-123 48, -122.9 4...