[R-sig-Geo] reading KML files in R

Agustin Lobo alobolistas at gmail.com
Sat May 29 09:21:03 CEST 2010


Excuses for the quite adjacent question, does anybody know if kml files require
using the google spherical projection?
My understanding is that, in theory, you can use any projection, but,
in fact, you must use
google spherical to get a correct display in google maps

Agus

2010/5/28 Wade Wall <wade.wall at gmail.com>:
> Thanks Dr. Bivand,
>
> I was able to import my kml file using your example.  I opened up the
> kml file in a text editor to find out the layer name, but
> system(paste("ogrinfo", paste(td, "cities.kml", sep="/")),
> intern=TRUE) also works to find the name.
>
> As for modifying ogrinfo(), I will take a look and see if it is above
> my skill level or not.
>
> Wade
>
> On Fri, May 28, 2010 at 3:29 AM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>> 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
>>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list