Fetch table from database with geometry support
fetch_from_db.RdFetches 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.
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.
See also
connect_to_pops, db_table_exists, db_table_has_geometry, read_from_db
Other io utilities:
clean_data_dict(),
cut_and_label(),
db_table_has_geometry(),
fetch_acs(),
fetch_all_hts_tables(),
fetch_hts_table(),
fetch_pums(),
fetch_study_region(),
find_project_root(),
fix_value_labels_on_load(),
get_db_table_name(),
print_params(),
pums_checksum(),
read_from_db(),
read_pums_codebook(),
record_checksum(),
sampled_latlon_to_bg()