Skip to contents

Fetches a table from a database, automatically detecting and reading geometry columns if present. Use to retrieve tabular or spatial data from POPS or other supported databases, handling both standard and spatial tables.

Usage

fetch_from_db(columns = "*", table_name, dbname, schema)

Arguments

columns

character or character vector. Columns to fetch. Default: all columns ('*'). If vector, columns are joined with commas for SQL query.

table_name

character(1). Name of the table to fetch.

dbname

character(1). Name of the database to connect to.

schema

character(1). Name of the database schema.

Value

data.table or sf object. Table data as tabular or spatial object, depending on presence of geometry column. Returns NULL if table does not exist.

Details

  • Connects to the specified database using connect_to_pops().

  • Checks if the table exists in the given schema; returns NULL and prints a message if not found.

  • If the table contains a geometry column, reads it using sf::st_read() for spatial support.

  • Otherwise, reads the table using a standard SQL query via read_from_db().

  • Ensures database connection is closed on exit.

  • Returns a data.table or sf object depending on table type.

Settings

None directly referenced; connection parameters provided as arguments.

Examples

## Not run:
# dt <- fetch_from_db(columns = c('id', 'geom'), table_name = 'ex_hh', dbname = 'hts', schema = 'public')
## End(Not run)