Skip to contents

Reads a CSV file of public transport accessibility data and converts it into a spatial vector format (sf or terra::SpatVector). This function builds polygon geometries directly from the point coordinates in a vectorized manner using the sfheaders package, which can be faster for sparse datasets than methods requiring an intermediate raster.

Usage

tph_to_vector(
  input_file,
  out_column_name = NULL,
  crs_src = 4326,
  crs_dst = 3035,
  x_offset = -500,
  y_offset = -500,
  resolution_m = 1000,
  out_vector_file = NULL,
  return_as = c("sf", "SpatVector"),
  add_id = TRUE
)

Arguments

input_file

Character. Path to the input CSV (gzipped) containing at least columns 'lon', 'lat', and one numeric data column.

out_column_name

Character or NULL. Name to assign to the data column in the output raster. If NULL (default), the original column name from the CSV is used.

crs_src

Integer or character. Source CRS (EPSG code or WKT); default is 4326.

crs_dst

Integer or character. Destination CRS (EPSG code or WKT); default is 3035.

x_offset

Numeric. An offset subtracted from the projected x-coordinate. This is used to convert the source point's reference (e.g., a lower-left corner) to the cell's theoretical centroid. For centroid-based data like GISCO, the default of 0 is correct.

y_offset

Numeric. An offset subtracted from the projected y-coordinate, similar to x_offset.

resolution_m

Numeric. Cell size in units of the destination CRS; default is 1000 (1 km).

out_vector_file

Character or NULL. If a file path is provided (e.g., "data/luxembourg.gpkg"), the output is written to that file using sf::st_write, and the file path is returned. If NULL (the default), the function returns an in-memory spatial object.

return_as

Character. Specifies the class of the returned object when out_vector_file is NULL. Can be either "sf" (the default) or "SpatVector". This argument is ignored if out_vector_file is not NULL.

add_id

Logical. If TRUE (the default), an INSPIRE-compliant grid cell ID is generated and added as a column named "id". Set to FALSE to omit this step.

Value

Depending on the arguments:

  • If out_vector_file is NULL, returns an sf object (default) or a SpatVector object (if return_as = "SpatVector") with polygon geometries.

  • If out_vector_file is a character string, the path to the created file is returned.

Examples

# This function requires the 'sfheaders' package
if (requireNamespace("sfheaders", quietly = TRUE)) {
  tph_file <- system.file(
    "extdata",
    "ver1_0_LU_1km_pt_ppl_within_10-20_min.csv.gz",
    package = "tphconv"
  )

  # Convert a CSV file to an sf object using the direct method
  sf_obj_direct <- tph_to_vector(input_file = tph_file)
  plot(sf_obj_direct)
}