sp_read: Function to read spatial data files in a consistent way.

View source: R/sp_read.R

sp_readR Documentation

Function to read spatial data files in a consistent way.

Description

sp_read is a wrapper for rgdal::readOGR, but its usage is the same as the other file readers in R. Unlike rgdal::readOGR, sp_read will expand file paths and make a "best-guess" on what layer is to be loaded for spatial data files. Use sp_list_layers to find what layers a spatial data file contains.

Usage

sp_read(
  file,
  layer = NULL,
  geom = NULL,
  lower = TRUE,
  use_iconv = FALSE,
  verbose = TRUE
)

Arguments

file

Spatial data file name. For shapefiles and Mapinfo TAB files, a file extension is optional. For File Geodatabases, an extension is required (usually .gdb). For spatial data types which are composed of a single file, file can be a URL and the file will be downloaded into a temporary directory and then loaded.

layer

Layer within file to read. Default is NULL and should not be needed for shapefiles, Mapinfo TAB files, and GeoJSON files. Default behaviour for GPX files is to attempt to read the "tracks" layer.

geom

An override for when spatial data files contain more than one geometry type in a layer. This is commonly found in GeoJSON and KML files. geom can be one of: "points", "lines", or "polygons".

lower

Should the names of the data slot be forced to be lower case? Note that the default is TRUE.

use_iconv

Should the input strings attempted to be converted by iconv?

verbose

Should information about the data be printed when being loaded? Default is TRUE.

Details

sp_read will also wrap readRDS if an .rds file is detected, but will raise a warning if the file does not contain a spatial data type.

Author(s)

Stuart K. Grange

See Also

readOGR, sp_list_drivers, sp_list_layers

Examples

## Not run: 

# Load a shapefile without extension
sp_london <- sp_read("london_borough")

# Load a shapefile with an extension within home area
sp_london <- sp_read("~/Desktop/london_borough.shp")

# Load a gpx file
sp_hira <- sp_read("~/Desktop/hira_mtb_park.gpx", type = "tracks")

# Load GeoJSON file
sp_thames_locks <- sp_read("thames_locks.json", verbose = FALSE)

# Load mapinfo file
sp_coastline <- sp_read("nz-land-districts.map", verbose = FALSE)

# Geodatabase
sp_yemen <- sp_read("world.gdb", layer = "yemen", verbose = FALSE)

# Load a file by using a url
file <- "https://bit.ly/2FkUuAP"
sp_ridgeway <- sp_read(file, layer = "routes")


## End(Not run)


MohoWu/ricardor documentation built on March 24, 2022, 4:39 p.m.