Managing Animal Movement Data with 'PostGIS' and R

Integrates R and the 'PostgreSQL/PostGIS' database system to build and manage animal trajectory (movement) data sets. The package relies on 'ltraj' objects from the R package 'adehabitatLT', building the analogous 'pgtraj' data structure in 'PostGIS'. Functions allow users to seamlessly transfer between 'ltraj' and 'pgtraj', as well as build new 'pgtraj' directly from location data stored in the database.


This is the development area for the package rpostgisLT, a companion package to rpostgis and adehabitatLT, which provides functions to store ltraj (the trajectory data object from adehabitatLT) in a PostGIS-enabled PostgreSQL database.

The package was originally developed during Google Summer of Code 2016 by Balázs Dukai, with mentors Clément Calenge, David Bucklin, and Mathieu Basille.

You can install the latest released version (0.4) from CRAN:

install.packages("rpostgisLT")

You can use update.packages() to update to the latest CRAN version.

A stable development version of the package will be available on the project's Github page, which may be ahead the CRAN version. To install it, use the devtools package from Hadley Wickham:

library(devtools)
install_github("mablab/rpostgisLT")

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

install_github("mablab/rpostgisLT",ref="dev")

Of course, it is necessary to have PostgreSQL with PostGIS extension (ideally, versions 9.5+ and 2.2+, respectively; not tested on older versions). An installer for Windows, Linux and Mac systems can be found at EnterpriseDB. During the install process, make sure to also install the PostGIS extension.

Once you are up and running with PostgreSQL, make sure to enable the PostGIS extension on each database, using:

CREATE EXTENSION postgis;

You're now ready to use rpostgisLT. As a first test, you can try sending a sample ltraj dataset from the adehabitatLT package to the database. First, initiate the connection to the database:

library(rpostgisLT)
con <- dbConnect("PostgreSQL", dbname = <dbname>, host = <host>, user = <user>, password = <password>)

rpostgisLT stores ltraj in a data format called pgtraj (i.e., the PostGIS version of a trajectory). To store data, rpostgisLT builds the pgtraj data model in a dedicated schema in a database. All rpostgisLT functions specify the name of this schema to be traj by default, but it can be assigned any name you like (and you can have as many pgtraj schemas in a database as you would like). The functions that create pgtraj in rpostgisLT all will create this schema if it doesn't exist prior to building the pgtraj, but you can also create it ahead of time using the function pgtrajSchema.

pgtrajSchema(con)

Since we don't specify the schema name, it defaults to "traj". The data model is now built in that schema. To learn more about the pgtraj data model, view the rpostgisLT: Database model vignette.

We can now load a test dataset, and send it to the database using ltraj2pgtraj:

data(capreochiz)
head(capreochiz)
## Create an object of class "ltraj"
cap <- as.ltraj(xy = capreochiz[,c("x","y")], date = capreochiz$date,
            id = "Roe.Deer", typeII = TRUE,
            infolocs = capreochiz[,4:8])
            
# send to database
ltraj2pgtraj(con, cap, pgtraj = "test_data")

pgtraj stored in the database can be re-imported as ltraj using the pgtraj2ltraj function:

cap.db<-pgtraj2ltraj(con, "test_data")

rpostgisLT also can create pgtraj from data already stored in a database. Consider the following table storing animal relocations:

-- this is SQL!!
CREATE TABLE gps_data.relocations
(
  gid serial PRIMARY KEY,                       -- primary key (unique integer value)
  animal_id character varying,                  -- unique animal name or ID
  acquisition_time timestamp with time zone,    -- timestamp of the relocations 
  x double precision,                           -- x coordinate
  y double precision,                           -- y coordinate
  geom geometry(Point,26917),                   -- POINT geometry object created from x and y with SRID = 26917
  error_est int,                                -- estimated error for the relocation
  land_cover int                                -- land cover code for the relocation
);

To create one pgtraj named "test" from all the data in this table, you could use the as_pgtraj function with the following arguments:

as_pgtraj(con,
          relocations_table = c("gps_data","relocations"),
          pgtrajs = "test",
          animals = "animal_id",
          relocations = "geom",
          timestamps = "acquisition_time",
          rids = "gid",
          note = "pgtraj containing all data from gps_data.relocations.")

Alternatively, you could create one pgtraj for each distinct animal_id in the table, by specifying the pgtraj argument as a column name, e.g.:

as_pgtraj(con,
          relocations_table = c("gps_data","relocations"),
          pgtrajs = "animal_id",
          animals = "animal_id",
          relocations = "geom",
          timestamps = "acquisition_time",
          rids = "gid"
          )

You can also provide a column storing burst names, to further subdivide single animal's trajectories. Also, if you wish to only create pgtraj from a subset of the table, you can specify additional SQL using the clauses argument, as in this example, where only data from the year 2013 is selected:

as_pgtraj(con,
          relocations_table = c("gps_data","relocations"),
          pgtrajs = "test_2013",
          animals = "animal_id",
          relocations = "geom",
          timestamps = "acquisition_time",
          rids = "gid",
          clauses = "WHERE extract(year FROM acquisition_time) = 2013"
          )

Finally, you can also attach information on locations (infolocs in an ltraj) using the info_cols argument. By default, the function assumes that these columns are also in relocations_table, but you can specify an alternate table (info_table) and its ID column (info_rids) that matches (JOINs) with the rids column in relocations_table.

as_pgtraj(con,
          relocations_table = c("gps_data","relocations"),
          pgtrajs = "test_winfolocs",
          animals = "animal_id",
          relocations = "geom",
          timestamps = "acquisition_time",
          rids = "gid",
          info_cols = c("error_est","land_cover")
          )

All pgtraj can be directly imported into R ltraj using pgtraj2ltraj:

test_2013<-pgtraj2ltraj(con, pgtraj = "test_2013")

To see more demonstrations on how ltraj can be modified, written to pgtraj, and re-imported into R without any data alteration, refer to the Use Cases vignette.

News

rpostgisLT 0.4

** INITIAL CRAN RELEASE **

OVERALL CHANGE

  • Added handling of infolocs
  • Added support for Type I (no time recorded) trajectories
  • New pgtraj and burst summary views
  • faster conversion to pgtraj from database data in as_pgtraj()

rpostgisLT 0.3 (Release date: 2016-08-23)

Changes:

  • Add pgTrajVacuum(), pgTrajDrop()
  • Add ltraj2pgtraj(overwrite = TRUE/FALSE)
  • Add dl_opt(rnames=TRUE/FALSE)

Breaking changes:

  • The traj database model has changed by adding r_rowname, r2n, rel_angle to the table 'step'.
  • Remove pgTrajR2TempT().
  • Renaming <pgtraj_name>_step_geom –> <pgtraj_name>_step_geometry; <pgtraj_name>_params –> <pgtraj_name>_parameters

rpostgisLT 0.2.3 (Release date: 2016-08-10)

Changes:

  • Depends on adehabitatLT package.
  • Add <pgtraj_name>_step_geom view for visualizing pgtrajes in QGIS.

Breaking changes:

  • The traj database model has changed, including field and table name changes.
  • Remove pgDropTempT().

rpostgisLT 0.2.2 (Release date: 2016-08-01)

Changes:

  • Bugfix version, no changes visible to the user.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rpostgisLT")

0.5.0 by David Bucklin, 9 days ago


https://github.com/mablab/rpostgisLT


Report a bug at https://github.com/mablab/rpostgisLT/issues


Browse source code at https://github.com/cran/rpostgisLT


Authors: Balázs Dukai [aut] (Package creator during Google Summer of Code 2016), Mathieu Basille [aut], David Bucklin [aut, cre], Clément Calenge [ctb]


Documentation:   PDF Manual  


GPL (>= 3) license


Imports sp, testthat

Depends on DBI, RPostgreSQL, rpostgis, adehabitatLT

Suggests knitr, raster, rmarkdown

System requirements: PostgreSQL with PostGIS extension


See at CRAN