Skip to content
Prev 8350 / 29559 Next

reading KML files in R

On Thu, 27 May 2010, Wade Wall wrote:

            
That is what I said. Each driver has its own understanding of what the 
dsn= and layer= arguments are, and your usage doesn't match what the 
driver is expecting. The dsn= for the KML driver seems to be the file name 
(possibly with its complete path), and the layer= is the string in the:

<Document><Folder><name>cities</name>

line (conditional on the writing software using a similar logic to OGR), 
here in this example:

library(rgdal)
cities <- readOGR(system.file("vectors", package = "rgdal")[1], "cities")
is.na(cities$POPULATION) <- cities$POPULATION == -99
summary(cities$POPULATION)
td <- tempdir()
writeOGR(cities, paste(td, "cities.kml", sep="/"), "cities", driver="KML")
xx <- readOGR(paste(td, "cities.kml", sep="/"), "cities")

How one discovers this name is not obvious, since the KML driver in OGR 
needs it to access the file. One possibility is:

system(paste("ogrinfo", paste(td, "cities.kml", sep="/")), intern=TRUE)

which lists the layer name(s). Perhaps ogrInfo() could be revised to 
access the dsn in the same way when not given a layer= argument to 
discover layer names - anyone with lots of time on their hands like to 
help?

Roger

PS. ogrDrivers() lists the drivers seen by OGR as running in rgdal, but 
for drivers with write but not read capability, it cannot distinguish. In 
that case, you only find out if a read driver is present and has its 
external dependencies properly satisfied by using it, I'm afraid. The 
example above will show whether your rgdal/GDAL/Expat do work or not.