[R-sig-Geo] reading KML files in R
Roger Bivand
Roger.Bivand at nhh.no
Fri May 28 09:29:46 CEST 2010
On Thu, 27 May 2010, Wade Wall wrote:
> I am not exactly sure what you mean by identify the correct layer
> name. Using R under Linux, this is what happens.
>
> I have a file in the current directory named ASMI_GA.kml
>
> library(rgdal)
> tmp<-readOGR(".", layer="ASMI_GA.kml")
>
> However, I get the following message.
>
> Error in ogrInfo(dsn = dsn, layer = layer, input_field_name_encoding =
> input_field_name_encoding) :
> Cannot open file
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.
>
> I have expat XML parser installed and gdal. This probably sounds
> rather stupid, but I am not sure what the following means in the link
> that Dr. Bivand provided: "KML reading is only available if GDAL/OGR
> is built with the Expat XML Parser, otherwise only KML writing will be
> supported." I installed both as debian packages (using get-apt).
> Should I install both from source code, Expat XML Parser first and
> then GDAL?
>
> Sorry if I seem dense here, but it is not real clear to me. Also,
> even though this may be beyond the scope of R-sig-geo, I think that
> other people are also struggling with this.
>
> Thanks for any help.
>
> Wade
>
>
> On Thu, May 27, 2010 at 2:15 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>> On Thu, 27 May 2010, rick reeves wrote:
>>
>>> I do note that the example I provided does not demonstrate how to READ a
>>> KML
>>> file into R. But, SOON, I plan to modify the example to demonstrate
>>> reading of KMLs
>>> into some R-compatible format.
>>
>> readOGR() does read KML files, but you do have to identify the correct layer
>> name - see:
>>
>> http://www.gdal.org/ogr/drv_kml.html
>>
>> Roger
>>
>>>
>>> RR
>>>
>>> On 5/27/2010 10:15 AM, Wade Wall wrote:
>>>>
>>>> Hi all,
>>>>
>>>> Does anyone have experience reading and writing KML files in R? I
>>>> have seen several threads that seem to suggest that it is difficult if
>>>> not impossible, but it is convenient to use KML files when working
>>>> with Google Earth and inconvenient to read and write shapefiles and
>>>> then convert to KML using other software.
>>>>
>>>> If anyone is able to read and write KML files, if they could provide a
>>>> description of how to do it, I am sure a lot of people would
>>>> appreciate it. I have tried to do so under both Windows and Linux to
>>>> no avail. The Windows approach looks more difficult, judging by the
>>>> readme file associated with rgdal.
>>>>
>>>> Thanks,
>>>>
>>>> Wade
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at stat.math.ethz.ch
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>>
>>>
>>>
>>>
>>
>> --
>> Roger Bivand
>> Economic Geography Section, Department of Economics, Norwegian School of
>> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
>> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>> e-mail: Roger.Bivand at nhh.no
>>
>>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list