1\name{writeOGR} 2\alias{writeOGR} 3%- Also NEED an '\alias' for EACH other topic documented here. 4\title{Write spatial vector data using OGR} 5\description{ 6 The function is an interface with the OGR abstraction library for spatial vector data, allowing data to be written out using supported drivers. The drivers supported will depend on the local installation, and the capabilities of those drivers (many are read-only). The objects exported are SpatialPointsDataFrame, SpatialLinesDataFrame, or SpatialPolygonsDataFrame objects as defined in the sp package. 7} 8\usage{ 9writeOGR(obj, dsn, layer, driver, dataset_options = NULL, 10 layer_options=NULL, verbose = FALSE, check_exists=NULL, 11 overwrite_layer=FALSE, delete_dsn=FALSE, morphToESRI=NULL, 12 encoding=NULL, shp_edge_case_fix=FALSE, dumpSRS = FALSE) 13} 14%- maybe also 'usage' for other objects documented here. 15\arguments{ 16 \item{obj}{a SpatialPointsDataFrame, SpatialLinesDataFrame, or a SpatialPolygonsDataFrame object.} 17 \item{dsn}{data source name (interpretation varies by driver --- for some drivers, dsn is a file name, but may also be a folder)} 18 \item{layer}{layer name (varies by driver, may be a file name without extension)} 19 \item{driver}{a character string equal to one of the driver names returned by \code{\link{ogrDrivers}}} 20 \item{dataset_options}{a character vector of options, which vary by driver, and should be treated as experimental} 21 \item{layer_options}{a character vector of options, which vary by driver, and should be treated as experimental} 22 \item{verbose}{if TRUE, returns a list of information about the attempted write operation} 23 \item{check_exists}{default NULL, which tests for the GDAL version, and sets FALSE if < 1.8.0, or TRUE for >= 1.8.0} 24 \item{overwrite_layer}{default FALSE, if TRUE and \code{check_exists=TRUE}, delete the existing layer of the same name from the data source before writing the new layer; this will delete data and must be used with extreme caution, its behaviour varies between drivers, and accommodates changes that may appear in GDAL 1.8} 25 \item{delete_dsn}{default FALSE, may be set to TRUE if \code{overwrite_layer} reports that the data source cannot be updated; this will delete data and must be used with extreme caution, its behaviour varies between drivers, and accommodates changes that may appear in GDAL 1.8} 26 \item{morphToESRI}{default NULL, in which case set TRUE if driver is \dQuote{ESRI Shapefile} or FALSE otherwise; may be used to override this default} 27 \item{encoding}{default NULL, if set to a character string, it will be used to convert output strings from the given value to UTF-8 encoding.} 28 \item{shp_edge_case_fix}{default FALSE, if TRUE, attempt to work around MULTIPOLYGON to POLYGON degradation in ESRI Shapefile output with two touching exterior rings in a single feature (not yet implemented).} 29 \item{dumpSRS}{dump SRS to stdout from inside GDAL to debug conversion - developer use only} 30} 31\details{ 32 Working out which combination of dsn, layer, and driver (and option) values give the desired output takes time and care, and is constrained by the ability of drivers to write output; many are read-only. Use of the references given is highly advisable, with searches in the archives of other software using GDAL/OGR. Note that for the \dQuote{ESRI Shapefile} driver and GDAL >= 1.9, the \code{layer_options} value of \sQuote{ENCODING=\dQuote{LDID/CP1252}} or other values found on \url{http://www.autopark.ru/ASBProgrammerGuide/dbfstruc.htm} to set the encoding byte of the output DBF file (link refered to in ogr/ogrsf_frmts/shape/ogrshapelayer.cpp. The effect of setting the LDID may vary depending on whether GDAL is built with iconv or not, and on the setting of the CPL Option \dQuote{SHAPE_ENCODING}. 33 34While there is no certainty, newer drivers such as KML, GML, SQLite and Geopackage (GPKG) may encode string fields as UTF-8. Users are advised to explore this on a case to case basis using \code{\link[base]{Encoding}} on string fields of objects to be output, converting where necessary with \code{\link[base]{iconv}} or assigning the appropriate value with \code{\link[base]{Encoding}}. 35 36} 37 38\value{ 39 if verbose=TRUE, a list of information about the attempted write operation 40} 41 42\note{Only a subset of possible data slot column classes may be written out; if the function returns an error that the data type of stated columns is unknown, examine the classes and check that they are one of \code{c("numeric", "character", "factor", "POSIXt", "integer", "logical")}, and if not convert to such classes. Classes \code{c("factor", "POSIXt")} are converted to character strings, and \code{c("logical")} to integer internally. 43 44For writing with the KML and GPX drivers, note that the geometries should be in geographical coordinates with datum WGS84. 45} 46\section{Warning}{ 47The \code{overwrite_layer} and \code{delete_dsn} arguments are provided only for experienced script writers who need to be able to destroy data, for example during repetetive simulation runs. They should never be used by anyone who is not confident about deleting files. 48} 49\section{\code{writeOGR} Polygon bug in 1.1-1}{ 50In fixing a bug in the correct handling of SFS polygon geometries in version 1.1-1, a further bug was introduced affecting cases of wkbPolygon (not wkbMultiPolygon) output where SFS hole status in the output object was (correctly) defined in the comment to Polygons objects. The error only occurred when all the Polygons objects had one exterior ring, and zero or more interior rings. The error led to the coordinates of the rings cumulating, because the rings were not emptied before assigning the next ring. Version 1.1-2 corrects the error; thanks to JamesWorrall for a complete bug report \url{https://stat.ethz.ch/pipermail/r-sig-geo/2015-December/023796.html}. 51} 52\references{\url{https://gdal.org/drivers/vector/index.html}, \url{https://resources.oreilly.com/examples/9780596008659}} 53\author{Roger Bivand} 54 55\seealso{\code{\link{readOGR}}} 56\examples{ 57set_thin_PROJ6_warnings(TRUE) 58cities <- readOGR(system.file("vectors", package = "rgdal")[1], "cities") 59is.na(cities$POPULATION) <- cities$POPULATION == -99 60summary(cities$POPULATION) 61td <- file.path(tempdir(), "rgdal_examples"); dir.create(td) 62# BDR 2016-12-15 (MapInfo driver fails writing to directory with ".") 63if(nchar(Sys.getenv("OSGEO4W_ROOT")) > 0) { 64 OLDPWD <- getwd() 65 setwd(td) 66 td <- "." 67} 68writeOGR(cities, td, "cities", driver="ESRI Shapefile") 69try(writeOGR(cities, td, "cities", driver="ESRI Shapefile")) 70writeOGR(cities, td, "cities", driver="ESRI Shapefile", overwrite_layer=TRUE) 71cities2 <- readOGR(td, "cities") 72summary(cities2$POPULATION) 73if ("SQLite" \%in\% ogrDrivers()$name) { 74 tf <- tempfile() 75 try(writeOGR(cities, tf, "cities", driver="SQLite", layer_options="LAUNDER=NO")) 76} 77if ("GeoJSON" \%in\% ogrDrivers()$name) { 78 js <- '{ 79 "type": "MultiPolygon", 80 "coordinates": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], 81 [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], 82 [100.0, 0.0]]]] 83 }' 84 spdf <- readOGR(js, layer='OGRGeoJSON') 85 in1_comms <- sapply(slot(spdf, "polygons"), comment) 86 print(in1_comms) 87 tf <- tempfile() 88 writeOGR(spdf, tf, "GeoJSON", driver="GeoJSON") 89 #spdf1 <- readOGR(tf, "GeoJSON") 90 spdf1 <- readOGR(tf) 91 in2_comms <- sapply(slot(spdf1, "polygons"), comment) 92 print(in2_comms) 93 print(isTRUE(all.equal(in1_comms, in2_comms))) 94} 95\dontrun{if ("GML" \%in\% ogrDrivers()$name) { 96 airports <- try(readOGR(system.file("vectors/airports.gml", 97 package = "rgdal")[1], "airports")) 98 if (class(airports) != "try-error") { 99 writeOGR(cities, paste(td, "cities.gml", sep="/"), "cities", driver="GML") 100 cities3 <- readOGR(paste(td, "cities.gml", sep="/"), "cities") 101 } 102}} 103# The GML driver does not support coordinate reference systems 104if ("KML" \%in\% ogrDrivers()$name) { 105 data(meuse) 106 coordinates(meuse) <- c("x", "y") 107 proj4string(meuse) <- CRS("+init=epsg:28992") 108 meuse_ll <- spTransform(meuse, CRS("+proj=longlat +datum=WGS84")) 109 writeOGR(meuse_ll["zinc"], paste(td, "meuse.kml", sep="/"), "zinc", "KML") 110} 111list.files(td) 112roads <- readOGR(system.file("vectors", package = "rgdal")[1], 113 "kiritimati_primary_roads") 114summary(roads) 115if (strsplit(getGDALVersionInfo(), " ")[[1]][2] < "2") { 116# For GDAL >= 2, the TAB driver may need a BOUNDS layer option 117 writeOGR(roads, td, "roads", driver="MapInfo File") 118 roads2 <- readOGR(paste(td, "roads.tab", sep="/"), "roads") 119 summary(roads2) 120} 121scot_BNG <- readOGR(system.file("vectors", package = "rgdal")[1], "scot_BNG") 122summary(scot_BNG) 123if (strsplit(getGDALVersionInfo(), " ")[[1]][2] < "2") { 124# For GDAL >= 2, the TAB driver may need a BOUNDS layer option 125 writeOGR(scot_BNG, td, "scot_BNG", driver="MapInfo File") 126 list.files(td) 127 scot_BNG2 <- readOGR(paste(td, "scot_BNG.tab", sep="/"), "scot_BNG", 128 addCommentsToPolygons=FALSE) 129 summary(scot_BNG2) 130} 131writeOGR(scot_BNG, td, "scot_BNG", driver="MapInfo File", 132 dataset_options="FORMAT=MIF") 133list.files(td) 134scot_BNG3 <- readOGR(paste(td, "scot_BNG.mif", sep="/"), "scot_BNG") 135summary(scot_BNG3) 136if(nchar(Sys.getenv("OSGEO4W_ROOT")) > 0) { 137 setwd(OLDPWD) 138} 139} 140% Add one or more standard keywords, see file 'KEYWORDS' in the 141% R documentation directory. 142\keyword{spatial} 143 144