df2sf: Convert data.frame object into simple features object

View source: R/df2sf.R

df2sfR Documentation

Convert data.frame object into simple features object

Description

Function takes in x-, y-coordinates, and a data.frame of variables (optional) and returns an SFC object

Usage

df2sf(
  x_coord,
  y_coord,
  input_data = NULL,
  file = NULL,
  n_max = Inf,
  start = 0,
  projection_input = "EPSG:4326",
  zero.policy = FALSE,
  show_removed = FALSE
)

Arguments

x_coord

Numeric vector with longitude or easting projected coordinates. When input_data or file is supplied, can be either column name or numeric vector of the same length as nrow(input_data).

y_coord

Numeric vector with latitude or northing projected coordinates. Must be equal to the vector length of x_coord. When input_data or file is supplied, can be either column name or numeric vector of the same length as nrow(input_data).

input_data

Optional data frame object, containing x_coord and y_coord. nrow(input_data) must be equal to the vector length of x_coord. NOTE: Rows corresponding to non-usable coordinates are removed from the final output.

file

Optional path to csv file. Overrides input_data.

n_max

Maximum number of rows to read in file. Default is Inf.

start

Number of rows to skip in file. Default is 0 (start on first row).

projection_input

Projection string associated with x_coord and y_coord. Default is '+proj=longlat'.

zero.policy

If TRUE, removes rows where corresponding coordinates equals (0,0). Default is FALSE.

show_removed

If TRUE, returns a vector of indices corresponding to non-usable coordinates. Default is FALSE.

Value

If show_removed==FALSE, returns an sf object, with rows corresponding to non-usable coordinates removed. If show_removed==TRUE, returns a list, with an sf object (Spatial_Coordinates), and a vector of indices corresponding to non-usable coordinates removed (Removed_Rows).

Examples

# Coordinates supplied as vectors
## Not run: 
data(clea_deu2009_df)
out_1 <- df2sf(x_coord=clea_deu2009_df$longitude,y_coord = clea_deu2009_df$latitude)
class(out_1)
plot(out_1$geometry)

## End(Not run)
# Coordinates supplied as column mames
## Not run: 
out_2 <- df2sf(x_coord="longitude",y_coord ="latitude", input_data = clea_deu2009_df)
plot(out_2["geometry"])

## End(Not run)
# Load from external file
## Not run: 
tmp <- tempfile()
write.csv(clea_deu2009_df,file=tmp)
out_3 <- df2sf(x_coord="longitude",y_coord ="latitude", file=tmp)
plot(out_3["geometry"])

## End(Not run)

zhukovyuri/SUNGEO documentation built on Nov. 5, 2023, 1:07 p.m.