If reading a shapefile, the data source name (dsn=
argument) is
the folder (directory) where the shapefile is, and the layer is the
name of the shapefile (without the .shp extension). For example to read
bounds.shp
from C:/Maps
, do
map <- readOGR(dsn="C:/Maps", layer="bounds")
. The logic behind
this is that typically one keeps all the shapefiles for a project in
one folder (directory).
As noted below, for other file type drivers, the dsn=
argument is
interpreted differently, and may be the file name itself, as for example
with the GPX
driver for reading GPS data as layer="tracks"
lines or layer="track_points"
points.
readOGR(dsn, layer, verbose = TRUE, p4s=NULL, stringsAsFactors=default.stringsAsFactors(), drop_unsupported_fields=FALSE, pointDropZ=FALSE, dropNULLGeometries=TRUE, useC=TRUE, disambiguateFIDs=FALSE, addCommentsToPolygons=TRUE, encoding=NULL, use_iconv=FALSE, swapAxisOrder=FALSE, require_geomType = NULL, integer64="allow.loss")
ogrInfo(dsn, layer, encoding=NULL, use_iconv=FALSE, swapAxisOrder=FALSE, require_geomType = NULL)
ogrFIDs(dsn, layer)
ogrDrivers()
OGRSpatialRef(dsn, layer)
ogrListLayers(dsn)
"print"(x, ...)
TRUE
, but this can be changed by setting options(stringsAsFactors = FALSE)
(see link[base]{data.frame}
).c("wkbPoint", "wkbLineString", "wkbPolygon")
, then in input with multiple geometry types, the chosen type will be readtype.convert
for numeric conversion: “allow.loss” which clamps to 32-bit signed integer, “warn.loss” - as “allow.loss” but warns when clamping occurs, and “no.loss”, which reads as a character string using the formatting applied by default by GDAL. The use of 64-bit integers is usually a misunderstanding, as such data is almost always a long key ID.ogrDrivers()
function shows which are available, and which may be written (but all are assumed to be readable). Note that stray files in data source directories (such as *.dbf) may lead to suprious errors that accompanying *.shp are missing.
SpatialPointsDataFrame-class
, SpatialLinesDataFrame-class
, SpatialPolygonsDataFrame-class
, readShapePoly
, iconv
ogrDrivers()
dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
ogrInfo(dsn=dsn, layer="cities")
owd <- getwd()
setwd(dsn)
ogrInfo(dsn="cities.shp", layer="cities")
setwd(owd)
cities <- readOGR(dsn=dsn, layer="cities")
summary(cities)
table(Encoding(as.character(cities$NAME)))
ogrInfo(dsn=dsn, layer="kiritimati_primary_roads")
OGRSpatialRef(dsn=dsn, layer="kiritimati_primary_roads")
kiritimati_primary_roads <- readOGR(dsn=dsn, layer="kiritimati_primary_roads")
summary(kiritimati_primary_roads)
ogrInfo(dsn=dsn, layer="scot_BNG")
OGRSpatialRef(dsn=dsn, layer="scot_BNG")
scot_BNG <- readOGR(dsn=dsn, layer="scot_BNG")
summary(scot_BNG)
if ("GML" %in% ogrDrivers()$name) {
dsn <- system.file("vectors/airports.gml", package = "rgdal")[1]
airports <- try(readOGR(dsn=dsn, layer="airports"))
if (class(airports) != "try-error") summary(airports)
}
dsn <- system.file("vectors/ps_cant_31.MIF", package = "rgdal")[1]
ogrInfo(dsn=dsn, layer="ps_cant_31")
ps_cant_31 <- readOGR(dsn=dsn, layer="ps_cant_31")
summary(ps_cant_31)
sapply(as(ps_cant_31, "data.frame"), class)
ps_cant_31 <- readOGR(dsn=dsn, layer="ps_cant_31", stringsAsFactors=FALSE)
summary(ps_cant_31)
sapply(as(ps_cant_31, "data.frame"), class)
dsn <- system.file("vectors/Up.tab", package = "rgdal")[1]
ogrInfo(dsn=dsn, layer="Up")
Up <- readOGR(dsn=dsn, layer="Up")
summary(Up)
dsn <- system.file("vectors/test_trk2.gpx", package = "rgdal")[1]
test_trk2 <- try(readOGR(dsn=dsn, layer="tracks"))
if (class(test_trk2) != "try-error") summary(test_trk2)
test_trk2pts <- try(readOGR(dsn=dsn, layer="track_points"))
if (class(test_trk2pts) != "try-error") summary(test_trk2pts)
dsn <- system.file("vectors", package = "rgdal")[1]
ogrInfo(dsn=dsn, layer="trin_inca_pl03")
birds <- readOGR(dsn=dsn, layer="trin_inca_pl03")
summary(birds)
dsn <- system.file("vectors/PacoursIKA2.TAB", package = "rgdal")[1]
try(ogrInfo(dsn, "PacoursIKA2"))
ogrInfo(dsn, "PacoursIKA2", require_geomType="wkbPoint")
plot(readOGR(dsn, "PacoursIKA2", require_geomType="wkbLineString"), col="red")
plot(readOGR(dsn, "PacoursIKA2", require_geomType="wkbPoint"), add=TRUE)
odir <- getwd()
setwd(system.file("vectors", package = "rgdal")[1])
ow <- options("warn")$warn
options("warn"=1)
ogrInfo("test64.vrt", "test64")
str(readOGR("test64.vrt", "test64", verbose=FALSE, integer64="allow.loss")$val)
str(readOGR("test64.vrt", "test64", verbose=FALSE, integer64="warn.loss")$val)
str(readOGR("test64.vrt", "test64", verbose=FALSE, integer64="no.loss")$val)
str(readOGR("test64.vrt", "test64", verbose=FALSE, stringsAsFactors=FALSE,
integer64="no.loss")$val)
options("warn"=ow)
setwd(odir)
Run the code above in your browser using DataLab