[R-sig-Geo] Converting kml to shape (readOGR)

Tomislav Hengl T.Hengl at uva.nl
Tue Jan 6 13:10:56 CET 2009


Dear Henk,

My experiences is that the easiest way to read KML files and convert them to sp-type of objects is
to use the XML package.

Here is a small example:

> library(XML)
> meuse_lead.kml <- xmlTreeParse("meuse_lead.kml")
> lengthp <- length(meuse_lead.kml$doc[[1]][[1]][[1]])-1
> lead_sp <- data.frame(Longitude=rep(0,lengthp), Latitude=rep(0,lengthp), Var=rep(0,lengthp))
> for(j in 1:lengthp) {
>     LatLon <- unclass(meuse_lead.kml$doc[[1]][[1]][[1]][j+1][[1]][2][[1]][[1]][[1]])$value
>     Var <- unclass(meuse_lead.kml$doc[[1]][[1]][[1]][j+1][[1]][1][[1]][[1]])$value
>      lead_sp$Longitude[[j]] <- as.numeric(matrix(unlist(strsplit(LatLon, split=",")), ncol=2)[1])
>      lead_sp$Latitude[[j]] <- as.numeric(matrix(unlist(strsplit(LatLon, split=",")), ncol=2)[2])
>      lead_sp$Var[[j]] <- as.numeric(matrix(unlist(strsplit(strsplit(Var, split="<i>")[[1]][2],
split="</i>")), ncol=2)[1])
> }
> coordinates(lead_sp) <-~Longitude+Latitude
> proj4string(lead_sp) <- CRS("+proj=longlat +ellps=WGS84")
> bubble(lead_sp, "Var")

every KML can have a completely different structure, so you will need to modify position of
coordinates / variables where needed.

see also:
http://spatial-analyst.net/wiki/index.php?title=Export_maps_to_GE 

HTH



-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch [mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of
Edzer Pebesma
Sent: Tuesday, January 06, 2009 12:16 PM
To: Henk Sierdsema
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Converting kml to shape (readOGR)



Henk Sierdsema wrote:
> Hi,
>
> I've been trying to convert a vector kml-file to an ESRI shape with readOGR(), but I don't manage
to read the kml-file. Can anyone help me with this? 
>   
http://www.gdal.org/ogr/ogr_formats.html mentions at KML that "read 
support needs libexpat". Further it says "KML reading is only available 
if GDAL/OGR is built with the Expat XML Parser, otherwise only KML 
writing will be supported."

Maybe this was missing while your gdal/rgdal was built?
--
Edzer
> Thnx,
>
> Henk Sierdsema
>
>
> SOVON Vogelonderzoek Nederland / SOVON Dutch Centre for Field Ornithology
>
> Rijksstraatweg 178
> 6573 DG  Beek-Ubbergen
> The Netherlands
> tel: +31 (0)24 6848145
> fax: +31 (0)24 6848122
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>   

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de/
http://www.springer.com/978-0-387-78170-9 e.pebesma at wwu.de

_______________________________________________
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