read PostGIS table directly through DBI and RPostgreSQL interface, converting binary
st_read(dsn, layer = default_layer(dsn), ..., options = NULL, quiet = FALSE, iGeomField = 1L, type = 0, promote_to_multi = TRUE)
st_read_db(conn = NULL, table, query = paste("select * from ", table, ";"), geom_column = NULL, EWKB, ...)
TRUE
conn
is of class codePostgreSQLConnection or PqConnection
type
values see https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary, but note that not every target value may lead to succesful conversion. The typical conversion from POLYGON (3) to MULTIPOLYGON (6) should work; the other way around (type=3), secondary rings from MULTIPOLYGONS may be dropped without warnings.layer name may be guessed in some cases e.g. when dsn
contains the full path of a geopackage or a shapefile (with extension .shp), and it will try to do so; this may fail in other cases, though; it is often a good idea to specify both basename and layer name.
in case geom_column is missing: if table is missing, this function will try to read the name of the geometry column from table geometry_columns
, in other cases, or when this fails, the geom_column is assumed to be the last column of mode character.
if (Sys.getenv("USER") %in% c("edzer", "travis")) { # load meuse to postgis library(sp) example(meuse, ask = FALSE, echo = FALSE) st_write(st_as_sf(meuse), "PG:dbname=postgis", "meuse", driver = "PostgreSQL", options = "OVERWRITE=true") (s = st_read("PG:dbname=postgis", "meuse")) summary(s) } # nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), "nc.gpkg", crs = 4267) nc = st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267) summary(nc) if (Sys.getenv("USER") %in% c("travis", "edzer")) { library(RPostgreSQL) conn = dbConnect(PostgreSQL(), dbname = "postgis") st_read_db(conn, query = "select * from meuse limit 3;") dbDisconnect(conn) }