Learn R Programming

rpostgis

The rpostgis package provides an interface between R and PostGIS-enabled PostgreSQL databases to transparently transfer spatial data. Both vector (points, lines, polygons) and raster data are supported in read and write modes. Also provides convenience functions to execute common procedures in PostgreSQL/PostGIS.

Installation of the released versions

You can install the latest released version from CRAN:

pak::pak("rpostgis")

Installation of the development versions

A stable version of the package is always available on the project’s GitHub page, and may be ahead of the CRAN version. To install it, use the following command:

pak::pak("Cidree/rpostgis")

For the latest (possibly unstable) development version, use:

remotes::install_github("Cidree/rpostgis", ref = "dev")

Getting started

rpostgis relies on a working connection provided by the RPostgreSQL package to a PostgreSQL database, e.g.:

conn <- RPostgreSQL::dbConnect(
  drv      = "PostgreSQL", 
  host     = "localhost",
  dbname   = "<DB_NAME>", 
  user     = "<USER>", 
  password = "<PASSWORD>"
)

Note: as of rpostgis 1.4.3 the RPostgres::Postgres() driver is also allowed for connection objects; however, this should be considered experimental and is not recommended for most use cases.

Once the connection is established, the first step is to check if the database has PostGIS already installed (and install it if it’s not the case):

pgPostGIS(conn)

If the function returns TRUE, the database is ready and functional. You can check the geometries and rasters present in the database with:

pgListGeom(conn, geog = TRUE)
pgListRast(conn)

To terminate the session, close and clear the connection with:

RPostgreSQL::dbDisconnect(conn)

Documentation

Full documentation with the complete list of functions of the package can be found on rpostgis homepage.

Copy Link

Version

Install

install.packages('rpostgis')

Monthly Downloads

599

Version

1.6.0

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Adrián Cidre González

Last Published

December 6th, 2024

Functions in rpostgis (1.6.0)

pgGetSRID

Get SRID(s) from a geometry/geography column in a full table
pgMakePts

Add a POINT or LINESTRING geometry field.
pgInsert

Inserts data into a PostgreSQL table.
pgPostGIS

Check and create PostGIS extension.
pgListGeom

List geometries/rasters
dbTableNameFix

Format input for database schema/table names.
pgGetRast

Load raster from PostGIS database into R.
warn_deprecated_rc

Warning for deprecated sp and raster
warn_deprecated_sp

Warning for deprecated sp and raster
pgInsertizeGeom

Format R data objects for insert into a PostgreSQL table.
pgGetGeomQ

Load geometries from a full query and return a Spatial* object
pgSRID

Find (or create) PostGIS SRID based on CRS object.
rpostgis-package

rpostgis: R Interface to a PostGIS Database
pgWriteRast

Write raster to PostGIS database table.
pgWriteGeom

Inserts data into a PostgreSQL table.
dbColumn

Add or remove a column.
bs

Return indexes for an exact number of blocks for a raster
dbComment

Comment table/view/schema.
dbDrop

Drop table/view/schema.
dbAsDate

Converts to timestamp.
dbAddKey

Add key.
pgGetBoundary

Retrieve bounding envelope of geometries or rasters.
dbVacuum

Vacuum.
dbWriteDataFrame

Write/read in data frame mode to/from database table.
dbIndex

Create an index.
dbGetDefs

Get definitions for data frame mode reading
dbExistsTable

Check if a PostgreSQL table/view exists
dbVersion

Returns major.minor version of PostgreSQL (for version checking)
dbConnCheck

Check if a supported PostgreSQL connection
pgCheckGeom

Check if geometry or geography column exists in a table, and return the column name for use in a query.
dbBuildTableQuery

Builds CREATE TABLE query for a data frame object.
dbSchema

Check and create schema.
pgGetGeom

Load a PostGIS geometry from a PostgreSQL table/view/query into R.
dbTableInfo

Get information about table columns.