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

sfick sfick at ucdavis.edu
Fri Feb 13 02:06:29 CET 2015


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,
> 
> 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@

> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Since kml is essentially xml, one can create multiple files then combine
them manually, provided each file has the same structure...

... something like... 

mergeKML <- function(filename, x = list() ) { 
	require(XML)
	doc <- xmlRoot(xmlTreeParse(x[[1]], getDTD=F))
	for(i in 2:length(x)){
		y <- xmlRoot(xmlTreeParse(x[[i]]))
		doc[["Document"]][[i+1]] <- y[["Document"]][["Folder"]]
		doc[["Document"]][[i+2]] <- y[["Document"]][["Schema"]]
		}
	saveXML(doc, file= filename)
}
	
writeOGR(bar, driver="KML", dsn="foo.kml", layer="bar")
writeOGR(baz, driver="KML", dsn="baz.kml", layer="baz")
mergeKML('out.kml', list('foo.kml','baz.kml') )


-Steve




--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/writeOGR-with-2-layers-in-1-KML-file-tp6243676p7587788.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list