| Title: | Convert Transport Poverty Hub Data to Rasters and Vectors with GISCO IDs |
|---|---|
| Description: | Convert Transport Poverty Hub Data to Rasters and Vectors. The source data comes in CSV files with coordinates in WGS84 (EPSG:4326). To work with this data more efficiently, this package provides helper functions to quickly convert this data to spatial raster grids and vector polygon grids with GISCO IDs. Alternatively, GISCO IDs can be assigned to the data without any geometry generation in a 'data.frame' with optional coordinates columns. |
| Authors: | Egor Kotov [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6690-5345>) |
| Maintainer: | Egor Kotov <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-19 08:44:39 UTC |
| Source: | https://github.com/e-kotov/tphconv |
Reads a CSV file (optionally gzipped) of public transport accessibility data, reprojects point coordinates, and rasterizes the data onto a regular grid. Optionally, it can add a second raster layer containing INSPIRE-compliant grid cell IDs.
tph_to_raster( input_file, out_column_name = NULL, crs_src = 4326, crs_dst = 3035, x_offset = 0, y_offset = 0, resolution_m = 1000, out_raster_file = NULL, add_id = TRUE )tph_to_raster( input_file, out_column_name = NULL, crs_src = 4326, crs_dst = 3035, x_offset = 0, y_offset = 0, resolution_m = 1000, out_raster_file = NULL, add_id = TRUE )
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 |
resolution_m |
Numeric. Cell size in units of the destination CRS; default is 1000 (1 km). |
out_raster_file |
Character or NULL. Output filename for the raster. If provided, the raster is written to this file (creating directories as needed) and the filename is returned. If NULL (default), the SpatRaster is returned directly. |
add_id |
Logical. If |
If out_raster_file is NULL, a terra::SpatRaster object. This will be a
multi-layer raster if add_id = TRUE. If out_raster_file is provided, a character
string of the file path is returned.
tph_file <- system.file( "extdata", "ver1_0_LU_1km_pt_ppl_within_10-20_min.csv.gz", package = "tphconv" ) library(terra) # Convert a CSV to a raster with the ID layer (default) r_with_id <- tph_to_raster(input_file = tph_file) plot(r_with_id)tph_file <- system.file( "extdata", "ver1_0_LU_1km_pt_ppl_within_10-20_min.csv.gz", package = "tphconv" ) library(terra) # Convert a CSV to a raster with the ID layer (default) r_with_id <- tph_to_raster(input_file = tph_file) plot(r_with_id)
Reads a transport opportunities CSV file and efficiently generates a table containing the corresponding INSPIRE/GISCO grid cell IDs and data values.
tph_to_table( input_file, out_column_name = NULL, crs_src = 4326, crs_dst = 3035, x_offset = -500, y_offset = -500, resolution_m = 1000, add_centroid_coords = FALSE, add_gisco_corner_coords = FALSE )tph_to_table( input_file, out_column_name = NULL, crs_src = 4326, crs_dst = 3035, x_offset = -500, y_offset = -500, resolution_m = 1000, add_centroid_coords = FALSE, add_gisco_corner_coords = FALSE )
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 |
resolution_m |
Numeric. Cell size in units of the destination CRS; default is 1000 (1 km). |
add_centroid_coords |
Logical. If |
add_gisco_corner_coords |
Logical. If |
This function is the fastest method for converting TPH data, as it operates directly on coordinates and avoids creating any intermediate spatial objects (rasters, polygons, or sf points).
A data.frame with the gisco_id, the data value, and optional
coordinate columns.
tph_file <- system.file( "extdata", "ver1_0_LU_1km_pt_ppl_within_10-20_min.csv.gz", package = "tphconv" ) # Generate a table with both centroid and corner coordinates full_table <- tph_to_table( tph_file, add_centroid_coords = TRUE, add_gisco_corner_coords = TRUE ) head(full_table)tph_file <- system.file( "extdata", "ver1_0_LU_1km_pt_ppl_within_10-20_min.csv.gz", package = "tphconv" ) # Generate a table with both centroid and corner coordinates full_table <- tph_to_table( tph_file, add_centroid_coords = TRUE, add_gisco_corner_coords = TRUE ) head(full_table)
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.
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 )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 )
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 |
resolution_m |
Numeric. Cell size in units of the destination CRS; default is 1000 (1 km). |
out_vector_file |
Character or |
return_as |
Character. Specifies the class of the returned object when
|
add_id |
Logical. If |
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.
# 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) }# 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) }