st_postgis: Send 'sf' layer to a PostGIS query

Description Usage Arguments Value Examples

View source: R/st_postgis.R

Description

The function sends a query plus an sf layer to PostGIS, saving the trouble of manually importing the layer and exporting the result

Usage

1
st_postgis(x, con, query, prefix = "temporary_nngeo_layer_")

Arguments

x

Object of class sf

con

Connection to PostgreSQL database with PostGIS extension enabled. Can be created using function RPostgreSQL::dbConnect

query

SQL query, which may refer to layer x as x and to the geometry column of the x layer as geom (see examples)

prefix

Prefix for storage of temporarily layer in the database

Value

Returned result from the database: an sf layer in case the result includes a geometry column, otherwise a data.frame

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Not run: 

# Database connection and 'sf' layer
source("~/Dropbox/postgis_159.R")  ## Creates connection object 'con'
x = towns

# Query 1: Buffer
query = "SELECT ST_Buffer(geom, 0.1, 'quad_segs=2') AS geom FROM x LIMIT 5;"
st_postgis(x, con, query)

# Query 2: Extrusion
query = "SELECT ST_Extrude(geom, 0, 0, 30) AS geom FROM x LIMIT 5;"
st_postgis(x, con, query)

## End(Not run)

michaeldorman/nngeo documentation built on June 17, 2021, 7:52 a.m.