spatial_write: Wrapper functions for writing spatial objects.

spatial_writeR Documentation

Wrapper functions for writing spatial objects.

Description

Write spatial object to disk. If spatial object is Simple Features, then appropriate functions from package sf are used. If spatial objest are abstract of class Spatial then appropriate functions from packages sp and rgdal are used.

Usage

spatial_write(obj, fname, layer, driver = NA, compress = "",
              ogr2ogr = nchar(Sys.which("ogr2ogr")) > 0, verbose = FALSE)

Arguments

obj

Spatial object: either Simple Features (sf) or Spatial Abstract (sp). List of spatial objects can be used.

fname

Character. File name with or without extension. If extension is missed, then argument driver must be specified.

layer

Character. Layer name. If missed, then basename of fname is used.

driver

Character. Driver for specification of output file format. Default is NA; value is determined from extension of fname.

compress

Character or logical. Will output file or list of files be packed after writing and what archive format will be used. Available character values are "" (default; no compression), "gz", "gzip", "bz2", "bzip2", "zip", "xz". If logical and TRUE, then "zip" is used for driver "ESRI Shapefile" and "gzip" otherwise. If logical and FALSE, then no compression.

ogr2ogr

Logical. If "ogr2ogr" is found in system path, then GDAL utils are used for speed up and layer merge (if obj is list of spatial objects)

verbose

Logical. Value TRUE provides information on console. Default is FALSE.

Details

Based on sf::st_write and rgdal::writeOGR functions with additonal options: compressing of output file(s), coordinates trasforming (to longitudes and latitudes for driver="GeoJSON"), creating multi-layer destination (for driver="SQLite").

Value

invisible NULL.

Author(s)

Nikita Platonov platonov@sevin.ru

See Also

write_sf (valid if package sf is installed)

writeOGR (package rgdal)

spatial_read

Examples

session_grid(NULL)
n <- 1e2
x <- runif(n,min=25,max=65)
y <- runif(n,min=55,max=65)
z <- runif(n,min=1,max=10)
da <- data.frame(x=x,y=y,z=z)
if (requireNamespace("sf",quietly=TRUE)) {
   obj1 <- sf::st_as_sf(da,coords=c("x","y"),crs=4326)
   print(series(obj1))
   fname1 <- file.path(tempdir(),"res1.shp")
   print(fname1)
   spatial_write(obj1,fname1)
   res1 <- spatial_read(fname1,engine="sf")
   print(series(res1))
}
if (requireNamespace("sp")) {
   obj2 <- da
   sp::coordinates(obj2) <- c("x","y")
   sp::proj4string(obj2) <- sp::CRS("+init=epsg:4326")
   print(series(obj2))
   print(spatial_crs(obj2))
   fname2 <- file.path(tempdir(),"res2.shp")
   print(fname2)
   spatial_write(obj2,fname2)
   res2 <- spatial_read(fname2,engine="sp")
   print(series(obj2))
}
print(spatial_dir(tempdir()))

ursa documentation built on Dec. 28, 2022, 2:18 a.m.