[R-sig-Geo] writeOGR with 2+ layers in 1 KML file

Roger Bivand Roger.Bivand at nhh.no
Tue Apr 5 22:47:22 CEST 2011

On Tue, 5 Apr 2011, David Holstius wrote:

> Hi, I would like to write two different Spatial* objects, as two 
> different layers, to the same KML file using rgdal::writeOGR().
> If I do something like:
>  > writeOGR(bar, driver="KML", dsn="foo.kml", layer="bar")
> ... I get a KML Document with a Folder element inside named "bar". So 
> far, so good.
> But if I follow that with:
>  > writeOGR(baz, driver="KML", dsn="foo.kml", layer="baz")
> ... the contents of foo.kml are obliterated and replaced with new 
> contents. I would prefer that another Folder, named "baz", be created in 
> "foo.kml" without erasing any other Folder elements.
> Is this possible? Is there a flag I didn't find? Am I doing it wrong? 
> Many thanks in advance,

First, document which platform you are using, which version of rgdal, 
which version of GDAL, and how you installed them. The OGR/GDAL drivers 
change in behaviour between versions of GDAL. Next, check that ogr2ogr can 
write what you want outside R - that will show that the driver is capable 
of writing multiple layers to a file (that is, updating an existing file) 
for your driver of choice.

If it can, think how this might be done in writeOGR() and contribute your 
solution. My guess, based on http://www.gdal.org/ogr/drv_kml.html and 
http://www.gdal.org/ogr/drv_libkml.html, is that the representation of 
vector objects in sp is not compatible with a multi-layer OGC SFS 
representation (for this driver). I cannot see how one could merge 
multiple *.kml files either. It is in general not desirable to try to 
tweak writeOGR() to suit particular drivers.

Hope this clarifies,


> Very best,
> David
> --
> David Holstius
> PhD Student in Environmental Health Sciences
> UC Berkeley School of Public Health
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> 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

More information about the R-sig-Geo mailing list