Package 'pukRhelpers'

Title: Helper Functions for `pukR` as in My Workflows
Description: This package contains a set of random helper functions that I created to help me with my various data analysis tasks.
Authors: Pukar Bhandari [aut, cre] (ORCID: <https://orcid.org/0000-0002-1043-2937>)
Maintainer: Pukar Bhandari <[email protected]>
License: GPL (>= 3) + file LICENSE
Version: 0.0.0.9100
Built: 2026-06-01 17:11:11 UTC
Source: https://github.com/ar-puuk/pukRhelpers

Help Index


Read an ArcGIS layer (or table or ImageServer) and optionally encode domains

Description

Read an ArcGIS layer (or table or ImageServer) and optionally encode domains

Usage

arc_read_encode(
  url,
  col_names = TRUE,
  col_select = NULL,
  n_max = Inf,
  name_repair = "unique",
  crs = NULL,
  ...,
  fields = NULL,
  alias = "drop",
  token = arcgisutils::arc_token(),
  encode_field_values = FALSE,
  codes = c("replace", "label")
)

Arguments

url

The URL of the FeatureLayer, Table, or ImageServer.

col_names

Default TRUE. Column names or name handling rule. col_names can be TRUE, FALSE, NULL, or a character vector:

  • If TRUE, use existing default column names for the layer or table. If FALSE or NULL, column names will be generated automatically: X1, X2, X3 etc.

  • If col_names is a character vector, values replace the existing column names. col_names can't be length 0 or longer than the number of fields in the returned layer.

col_select

Default NULL. A character vector of the field names to be returned. By default, all fields are returned.

n_max

Defaults to Inf or an option set with ⁠options("arcgislayers.n_max" = <max records>)⁠. Maximum number of records to return.

name_repair

Default "unique". See vctrs::vec_as_names() for details. If name_repair = NULL and alias = "replace" may include invalid names.

crs

the spatial reference to be returned. If the CRS is different than the CRS for the input FeatureLayer, a transformation will occur server-side. Ignored if x is a Table.

...

Additional arguments passed to arcgislayers::arc_select() if URL is a FeatureLayer or Table or arcgislayers::arc_raster() if URL is an ImageLayer.

fields

Default NULL a character vector of the field names to returned. By default all fields are returned. Ignored if col_names is supplied.

alias

Use of field alias values. Default c("drop", "label", "replace"),. There are three options:

  • "drop", field alias values are ignored.

  • "label": field alias values are assigned as a label attribute for each field.

  • "replace": field alias values replace existing column names. col_names

token

your authorization token.

encode_field_values

Logical, character vector, or list of character vectors.

  • FALSE (the default): do not encode any fields.

  • TRUE: encode all fields that have coded-value domains.

  • Specific field or list of fields to replace. Fields that do not have coded value domains are ignored.

codes

Character scalar, one of "replace" or "label". Passed through to arcgislayers::encode_field_values() to control whether domain labels replace the raw codes or just attach as an attribute.

Value

If url is an ImageServer, a SpatRaster; otherwise an sf or data.frame.


Convert FlatGeoBuf (FGB) Files to Geodatabase (GDB) Layers

Description

This function converts all FlatGeoBuf (.FGB) files within a specified folder into layers of a specified Geodatabase (GDB) file.

Usage

convert_fgb_to_gdb(
  input_folder,
  gdb_path,
  overwrite = FALSE,
  crs = "EPSG:3857"
)

Arguments

input_folder

Character. Path to the folder containing FGB files.

gdb_path

Character. File path to the output GDB file.

overwrite

Logical. Whether to overwrite existing layers in the GDB. Default is FALSE.

crs

Character. Coordinate Reference System to transform the data. Default is "EPSG:3857".

Examples

## Not run: 
convert_fgb_to_gdb("/path/to/fgb_folder", "/path/to/output.gdb", overwrite = FALSE, crs = "EPSG:3857")

## End(Not run)

Convert XML to HTML using XSLT

Description

This function applies an XSLT transformation to an XML file and saves the output as an HTML file.

Usage

convert_xml_html(root, xml_filename, xsl_filename, output_filename)

Arguments

root

A character string specifying the root directory where the XML and XSL files are located.

xml_filename

A character string specifying the name of the XML file.

xsl_filename

A character string specifying the name of the XSL file.

output_filename

A character string specifying the name of the output HTML file.

Value

This function does not return a value but saves the transformed HTML file to the specified output path.

Examples

## Not run: 
convert_xml_html("/path/to/files", "input.xml", "stylesheet.xsl", "output.html")

## End(Not run)

Extract table following a specific anchor tag by name attribute

Description

This function extracts the summary and trip tables from Warren TDM HTML file.

Usage

extract_html_table(html_file, anchor_id)

Arguments

html_file

A character string specifying the path to the HTML file.

anchor_id

A character string specifying the name attribute of the anchor (⁠<a>⁠) tag to locate.

Value

A data frame containing the extracted table.

Examples

## Not run: 
summary_table <- extract_html_table("Model.html", "id6")
# View the extracted table
View(summary_table[[1]])

trip_table <- extract_html_table("Model.html", "id52")
# View the extracted table
View(trip_table[[1]])

## End(Not run)

Split Lines by Maximum Length

Description

Splits LINESTRING geometries longer than a given threshold into multiple smaller segments, ensuring all segments are under the threshold.

Usage

geo_split_lines(input_lines, max_length, id = "ID")

Arguments

input_lines

A data.frame of class sf containing LINESTRING geometries.

max_length

The maximum length allowed for any segment.

id

The name of the ID column in the input data.frame.

Value

A data.frame of class sf containing the split LINESTRING geometries.

Examples

## Not run: 
library(sf)
library(dplyr)
library(lwgeom)

geojson <- '{
"type": "FeatureCollection",
"name": "sample",
"features": [
  { "type": "Feature", "properties": { "COMID": 5329303 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.916097119649635, 38.229575873993497 ], [ -122.916154986316201, 38.229346873993904 ], [ -122.916676986315338, 38.228614207328235 ], [ -122.917430786314128, 38.227148873997294 ], [ -122.917488319647418, 38.226210273998674 ], [ -122.917371986314322, 38.22579827399926 ], [ -122.917400319647584, 38.224516407334704 ], [ -122.918995719645125, 38.223348274003115 ], [ -122.920127119643382, 38.223004607336975 ], [ -122.921171719641791, 38.222546407337802 ], [ -122.922186919640126, 38.221950807338601 ], [ -122.922795786305926, 38.221286674006421 ] ] } },
  { "type": "Feature", "properties": { "COMID": 5329293 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.913574186320261, 38.233262207321104 ], [ -122.914618986318601, 38.233261873987772 ], [ -122.915228386317608, 38.23307847398803 ], [ -122.915547519650431, 38.23282660732184 ], [ -122.915866386316679, 38.232231273989385 ], [ -122.915778986316809, 38.231475873990462 ], [ -122.915430386317382, 38.230880807324809 ], [ -122.915400986317422, 38.23044600732544 ], [ -122.91548798631726, 38.23024000732579 ], [ -122.916097119649635, 38.229575873993497 ] ] } },
  { "type": "Feature", "properties": { "COMID": 5329305 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.895114186348906, 38.228161607328957 ], [ -122.895375386348462, 38.22809287399582 ], [ -122.895636519681375, 38.227864007329401 ], [ -122.895897586347701, 38.227337407330253 ], [ -122.89607138634733, 38.226559073998033 ], [ -122.896245519680463, 38.226330073998497 ], [ -122.896071186347342, 38.225597607333043 ], [ -122.896186986347232, 38.224750474000984 ], [ -122.896070786347309, 38.224407207334878 ], [ -122.896041519680807, 38.223354207336399 ], [ -122.895867319681031, 38.223079474003612 ], [ -122.895867186347687, 38.222598874004348 ], [ -122.896070186347345, 38.222324207338033 ], [ -122.896940719679321, 38.221843274005437 ], [ -122.897492119678532, 38.221934674005354 ], [ -122.897985519677775, 38.222323807338 ], [ -122.898565986343442, 38.222529674004477 ], [ -122.89937871967561, 38.223056074003637 ], [ -122.900336386340712, 38.2235366073362 ], [ -122.90106218633963, 38.224406274001467 ], [ -122.901323386339243, 38.224566474001222 ], [ -122.901903719671566, 38.224497674001384 ], [ -122.902164786337892, 38.22429160733509 ], [ -122.902193586337887, 38.223673607336025 ], [ -122.902048519671439, 38.223444674003076 ], [ -122.901438919672387, 38.222941207337044 ], [ -122.901380786339189, 38.222597807337593 ], [ -122.901119519672761, 38.222300207338037 ], [ -122.901119386339587, 38.221819607338773 ], [ -122.901873386338366, 38.220743607340466 ], [ -122.902279586337784, 38.220651874007388 ], [ -122.902714986337116, 38.220651807340687 ], [ -122.902976186336616, 38.220766207340432 ], [ -122.903237519669631, 38.221338474006302 ], [ -122.903556919669086, 38.221704607339007 ], [ -122.904195386334777, 38.222139407338375 ], [ -122.905704586332433, 38.222574007337755 ], [ -122.906662319664292, 38.222940007337115 ], [ -122.908577919661298, 38.223878074002357 ], [ -122.910203519658751, 38.224564407334583 ], [ -122.910580919658173, 38.224999207333951 ], [ -122.910552119658121, 38.225457073999792 ], [ -122.910378119658446, 38.225754673999347 ], [ -122.909623786326392, 38.22628140733184 ], [ -122.908927586327422, 38.227037007330807 ], [ -122.908289519661764, 38.22751787399659 ], [ -122.907767519662571, 38.228456607328496 ], [ -122.90782578632917, 38.22902900732754 ], [ -122.908087186328771, 38.229463673993621 ], [ -122.908377519661599, 38.229738273993235 ], [ -122.909451386326623, 38.230012873992848 ], [ -122.9101481196588, 38.230333073992369 ], [ -122.910409386325057, 38.230653473991879 ], [ -122.910438519658385, 38.230836607324875 ], [ -122.910903119657689, 38.231271473990773 ], [ -122.911367519657006, 38.231591807323582 ], [ -122.911454786323532, 38.231958007323158 ], [ -122.911832319656298, 38.23234707398916 ], [ -122.912557986321815, 38.232667273988682 ], [ -122.913574186320261, 38.233262207321104 ] ] } }
]
}'

lines <- sf::st_transform(sf::st_read(geojson), 5070)

split_lines <- geo_split_lines(lines, 500, id = "COMID")
plot(split_lines)

## End(Not run)

Load and Query an ESRI Feature Service Layer

Description

This function connects to an ESRI Feature Service, displays available layers if no ID is provided, and allows users to download and query a specific layer.

Usage

load_esri(furl, ..., id = NULL)

Arguments

furl

Character string. URL to the ESRI Feature Service endpoint.

...

Additional arguments passed to arcgislayers::arc_select for querying the layer.

id

Integer, optional. The ID of the layer to download. If NULL, available layers are displayed, and the user is prompted to specify an ID interactively.

Details

The function opens a connection to the specified ESRI Feature Service, lists all available layers if no id is provided, and then downloads and queries the specified layer. Users can pass query arguments via ... to filter the results.

Value

An sf object containing the queried features from the selected layer.

Examples

## Not run: 
# Example 1: Load and query a specific layer by ID
sf_data <- load_esri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer", id = 1)

# Example 2: Interactively select a layer
sf_data <- load_esri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer")

## End(Not run)

Convert KML to Simple Features (sf) Object with Preserved Attributes

Description

This function reads a KML file and processes the specified columns to extract feature identifiers and descriptive attributes, returning an sf object that retains the geometry and parsed attribute data.

Usage

load_kml_sf(kml_path, ..., id_col = "Name", details_col = "Description")

Arguments

kml_path

A character string specifying the path to the KML file. The path can be a local file or a URL, and zipped files are supported.

...

Additional arguments passed to sf::read_sf() for reading the KML file.

id_col

A character string indicating the column in the KML file that contains feature identifiers (default is "Name").

details_col

A character string specifying the column that contains descriptive data to be parsed (default is "Description").

Details

The function parses the details_col to extract key-value pairs in the format "label: value", ensuring that all features have a consistent structure. Any missing values are handled appropriately.

Value

An sf object containing the geometry and parsed attributes extracted from the KML file.

Examples

## Not run: 
# Example usage:
# kml_file <- "path/to/your/file.kml"
# result_sf <- load_kml_sf(kml_file)

## End(Not run)

Load and Install R Packages

Description

This function checks if the specified R packages are installed and loads them. If a package is not installed, it attempts to install it from the specified CRAN repositories.

Usage

load_packages(..., silent = FALSE)

Arguments

...

A list of package names to be installed and/or loaded. Names should be provided as unquoted identifiers.

silent

Logical, default is FALSE. If TRUE, suppresses printing of package loading statuses.

Details

This function ensures that all specified packages are installed and loaded in the current R session. Missing packages are automatically installed from CRAN. The function also provides an option to suppress output messages.

Value

A named logical vector indicating whether each package was successfully loaded.

Examples

## Not run: 
# Example 1: Load and install ggplot2 and dplyr
load_packages(ggplot2, dplyr)

# Example 2: Load packages silently
load_packages(ggplot2, dplyr, silent = TRUE)

## End(Not run)

Load Dataset from UGRC SGID

Description

Loads a dataset from the UGRC SGID database. By default, the dataset is returned as an sf object for spatial data analysis.

Usage

load_ugrc_data(table_name, as_sf = TRUE)

Arguments

table_name

A character string specifying the name of the table to retrieve (e.g., "schema.table_name").

as_sf

Logical. If TRUE (default), the data is returned as an sf object. If FALSE, the geometry column is dropped and a regular data frame is returned.

Value

An sf object or a data frame containing the data from the specified table.

Examples

## Not run: 
# Load UTA Transit Lines
uta_lines <- load_ugrc_data("transportation.uta_routes_and_ridership")

# Load UTA Transit Stops
uta_stops <- load_ugrc_data("transportation.uta_stops_and_ridership")

## End(Not run)

Load List of Datasets from UGRC SGID

Description

Retrieves a list of available datasets in the UGRC SGID database, including their schema and table names.

Usage

load_ugrc_vars()

Value

A data frame containing two columns: table_schema and table_name, listing the schemas and tables in the SGID database.

Examples

## Not run: 
# Check list of available datasets
ugrc_vars <- load_ugrc_vars()
View(ugrc_vars)

## End(Not run)

Aggregate sf objects

Description

Geometries and attributes are aggregated. Source: https://gist.github.com/rCarto/bb47aff0a02e808d2bf64f2d8c5db7d8#file-st_aggregate-r

Usage

st_aggregate(x, by, var, fun)

Arguments

x

sf object

by

name of the variable of grouping elements

var

name(s) of the variable(s) to aggregate

fun

function(s) to compute the summary statistics

Value

An sf object is returned

Examples

## Not run: 
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
nc$dummy <- "ZONE_A"
nc$dummy[25:50] <- "ZONE_B"
nc$dummy[51:100] <- "ZONE_C"
r <- st_aggregate(nc, "dummy", c("BIR74", "NWBIR74"), c("mean", "median"))
plot(nc)
plot(r)

## End(Not run)

Split Lines by Maximum Length

Description

Splits LINESTRING geometries longer than a given threshold into multiple smaller segments, ensuring all segments are under the threshold. Source: https://gist.github.com/dblodgett-usgs/cf87392c02d73f1b7d16153d2b66a8f3

Usage

st_split_lines(input_lines, max_length, id = "ID")

Arguments

input_lines

A data.frame of class sf containing LINESTRING geometries.

max_length

The maximum length allowed for any segment.

id

The name of the ID column in the input data.frame.

Value

A data.frame of class sf containing the split LINESTRING geometries.

Examples

## Not run: 
library(sf)
library(dplyr)

geojson <- '{
"type": "FeatureCollection",
"name": "sample",
"features": [
  { "type": "Feature", "properties": { "COMID": 5329303 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.916097119649635, 38.229575873993497 ], [ -122.916154986316201, 38.229346873993904 ], [ -122.916676986315338, 38.228614207328235 ], [ -122.917430786314128, 38.227148873997294 ], [ -122.917488319647418, 38.226210273998674 ], [ -122.917371986314322, 38.22579827399926 ], [ -122.917400319647584, 38.224516407334704 ], [ -122.918995719645125, 38.223348274003115 ], [ -122.920127119643382, 38.223004607336975 ], [ -122.921171719641791, 38.222546407337802 ], [ -122.922186919640126, 38.221950807338601 ], [ -122.922795786305926, 38.221286674006421 ] ] } },
  { "type": "Feature", "properties": { "COMID": 5329293 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.913574186320261, 38.233262207321104 ], [ -122.914618986318601, 38.233261873987772 ], [ -122.915228386317608, 38.23307847398803 ], [ -122.915547519650431, 38.23282660732184 ], [ -122.915866386316679, 38.232231273989385 ], [ -122.915778986316809, 38.231475873990462 ], [ -122.915430386317382, 38.230880807324809 ], [ -122.915400986317422, 38.23044600732544 ], [ -122.91548798631726, 38.23024000732579 ], [ -122.916097119649635, 38.229575873993497 ] ] } },
  { "type": "Feature", "properties": { "COMID": 5329305 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.895114186348906, 38.228161607328957 ], [ -122.895375386348462, 38.22809287399582 ], [ -122.895636519681375, 38.227864007329401 ], [ -122.895897586347701, 38.227337407330253 ], [ -122.89607138634733, 38.226559073998033 ], [ -122.896245519680463, 38.226330073998497 ], [ -122.896071186347342, 38.225597607333043 ], [ -122.896186986347232, 38.224750474000984 ], [ -122.896070786347309, 38.224407207334878 ], [ -122.896041519680807, 38.223354207336399 ], [ -122.895867319681031, 38.223079474003612 ], [ -122.895867186347687, 38.222598874004348 ], [ -122.896070186347345, 38.222324207338033 ], [ -122.896940719679321, 38.221843274005437 ], [ -122.897492119678532, 38.221934674005354 ], [ -122.897985519677775, 38.222323807338 ], [ -122.898565986343442, 38.222529674004477 ], [ -122.89937871967561, 38.223056074003637 ], [ -122.900336386340712, 38.2235366073362 ], [ -122.90106218633963, 38.224406274001467 ], [ -122.901323386339243, 38.224566474001222 ], [ -122.901903719671566, 38.224497674001384 ], [ -122.902164786337892, 38.22429160733509 ], [ -122.902193586337887, 38.223673607336025 ], [ -122.902048519671439, 38.223444674003076 ], [ -122.901438919672387, 38.222941207337044 ], [ -122.901380786339189, 38.222597807337593 ], [ -122.901119519672761, 38.222300207338037 ], [ -122.901119386339587, 38.221819607338773 ], [ -122.901873386338366, 38.220743607340466 ], [ -122.902279586337784, 38.220651874007388 ], [ -122.902714986337116, 38.220651807340687 ], [ -122.902976186336616, 38.220766207340432 ], [ -122.903237519669631, 38.221338474006302 ], [ -122.903556919669086, 38.221704607339007 ], [ -122.904195386334777, 38.222139407338375 ], [ -122.905704586332433, 38.222574007337755 ], [ -122.906662319664292, 38.222940007337115 ], [ -122.908577919661298, 38.223878074002357 ], [ -122.910203519658751, 38.224564407334583 ], [ -122.910580919658173, 38.224999207333951 ], [ -122.910552119658121, 38.225457073999792 ], [ -122.910378119658446, 38.225754673999347 ], [ -122.909623786326392, 38.22628140733184 ], [ -122.908927586327422, 38.227037007330807 ], [ -122.908289519661764, 38.22751787399659 ], [ -122.907767519662571, 38.228456607328496 ], [ -122.90782578632917, 38.22902900732754 ], [ -122.908087186328771, 38.229463673993621 ], [ -122.908377519661599, 38.229738273993235 ], [ -122.909451386326623, 38.230012873992848 ], [ -122.9101481196588, 38.230333073992369 ], [ -122.910409386325057, 38.230653473991879 ], [ -122.910438519658385, 38.230836607324875 ], [ -122.910903119657689, 38.231271473990773 ], [ -122.911367519657006, 38.231591807323582 ], [ -122.911454786323532, 38.231958007323158 ], [ -122.911832319656298, 38.23234707398916 ], [ -122.912557986321815, 38.232667273988682 ], [ -122.913574186320261, 38.233262207321104 ] ] } }
]
}'

lines <- sf::st_transform(sf::st_read(geojson), 5070)

split_lines <- st_split_lines(lines, 500, id = "COMID")
plot(split_lines)

## End(Not run)