sf (version 0.2-8)

st_read: Read simple features or layers from file or database

Description

Read simple features from file or database, or retrieve layer names and their geometry type(s)

Read PostGIS table directly through DBI and RPostgreSQL interface, converting binary

Usage

st_read(dsn, layer, ..., options = NULL, quiet = FALSE, iGeomField = 1L, type = 0, promote_to_multi = TRUE, stringsAsFactors = default.stringsAsFactors(), int64_as_string = FALSE)
st_read_db(conn = NULL, table, query = paste("select * from ", table, ";"), geom_column = NULL, EWKB, ...)

Arguments

dsn
data source name (interpretation varies by driver - for some drivers, dsn is a file name, but may also be a folder, or contain the name and access credentials of a database)
layer
layer name (varies by driver, may be a file name without extension); in case layer is missing, st_read will read the first layer of dsn, give a warning and (unless quiet = TRUE) print a message when there are multiple layers, or give an error if there are no layers in dsn.
...
parameter(s) passed on to st_as_sf
options
character; driver dependent dataset open options, multiple options supported.
quiet
logical; suppress info on name, driver, size and spatial reference, or signaling no or multiple layers
iGeomField
integer; in case of multiple geometry fields, which one to take?
type
integer; ISO number of desired simple feature type; see details. If left zero, in case of mixed feature geometry types, conversion to the highest numeric type value found will be attempted.
promote_to_multi
logical; in case of a mix of LineString and MultiLineString, or of Polygon and MultiPolygon, convert all to the Multi variety; defaults to TRUE
stringsAsFactors
logical; logical: should character vectors be converted to factors? The `factory-fresh' default is TRUE, but this can be changed by setting options(stringsAsFactors = FALSE).
int64_as_string
logical; if TRUE, Int64 attributes are returned as string; if FALSE, they are returned as double and a warning is given when precision is lost (i.e., values are larger than 2^53).
conn
open database connection
table
table name
query
SQL query to select records
geom_column
character or integer: indicator of name or position of the geometry column; if not provided, the last column of type character is chosen
EWKB
logical; is the WKB is of type EWKB? defaults to TRUE if conn is of class codePostgreSQLConnection or PqConnection

Value

object of class sf when a layer was succesfully read; in case argument layer is missing and data source dsn does not contain a single layer, an object of class sf_layers is returned with the layer names, each with their geometry type(s). Note that the number of layers may also be zero.

Details

for iGeomField, see also https://trac.osgeo.org/gdal/wiki/rfc41_multiple_geometry_fields; for 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.

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 table is missing, the SRID cannot be read and resolved into a proj4string by the database, and a warning will be given.

Examples

Run this code
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", 
     layer_options = "OVERWRITE=true")
 (s = st_read("PG:dbname=postgis", "meuse"))
 summary(s)
}
# nc = st_read(system.file("gpkg/nc.gpkg", package="sf"))
nc = st_read(system.file("shape/nc.shp", package="sf"))
summary(nc)
if (Sys.getenv("USER") %in% c("edzer", "travis")) {
  library(RPostgreSQL)
  conn = dbConnect(PostgreSQL(), dbname = "postgis")
  x = st_read_db(conn, "meuse", query = "select * from meuse limit 3;")
  x = st_read_db(conn, table = "public.meuse")
  print(st_crs(x)) # SRID resolved by the database, not by GDAL!
  dbDisconnect(conn)
}

Run the code above in your browser using DataLab