[R-sig-Geo] Preparing KML files for GoogleMaps/Earth: Why no kmlPoint() in maptools package?
Robert J. Hijmans
r.hijmans at gmail.com
Thu Aug 27 18:35:57 CEST 2009
Rick, I wondered the same thing a while ago and wrote this script; I
worked for my purposes but never really finished it (e.g.
SpatialPoints objects support; and I wanted to add folders). Perhaps
you or someone else can finish it (or do a better one) and send it to
the maptools maintainers? Robert
kmlPoints <- function(obj, kmlfile, kmlname = "") {
# obj = x,y,desc
if (kmlname=="") {kmlname <- basename(kmlfile)}
kmlHeader <- c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
"<kml xmlns=\"http://earth.google.com/kml/2.2\">", "<Document>",
paste("<name>", kmlname, "</name>", sep = ""))
kml <- ""
obj[,3] <- gsub('&', 'and', obj[,3])
for (p in 1:length(obj[,1])) {
# kml <- append(kml, "<Folder>")
kml <- append(kml, "<Placemark>")
kml <- append(kml, paste("<name>", obj[p,3], "</name>", sep = ""))
kml <- append(kml, paste("<Point><coordinates>", obj[p,1], ',',
obj[p,2], ",0</coordinates></Point>", sep = ""))
kml <- append(kml, "</Placemark>")
# kml <- append(kml, "</Folder>")
}
kmlFooter <- c("</Document>", "</kml>")
cat(paste(c(kmlHeader, kml, kmlFooter), sep = "", collapse =
"\n"), "\n", file = kmlfile, sep = "")
}
> x <- -10:10
> y <- -10:10
> z <- 1:21
>
> obj <- cbind(x, y, z)
> kmlPoints(obj, 'd:/tst.kml')
On Wed, Aug 26, 2009 at 11:58 AM, rick reeves<reeves at nceas.ucsb.edu> wrote:
> Hello List:
>
> I'm investigating ways to convert Shape Files to KML format, and of course
> have
> taken note of the kmlLine() and kmlPolygon() routines in the maptools
> package.
>
> It is easy to review the code underlying these routines (e.g., > kmlLine),
> and to see
> how straightforward the coding is. My question is? Why hasn't a similar
> routine
> for Point shape files (eg, kmlPoint() ) been added to maptools?
>
> As it stands, one has to use rgdal package / writeOGR(), or fwtools/ogr2ogr,
> to convert Point shape to KML format. This works, but can present problems
> for Mac users who haven't built the rgdal library on their machines.
>
> Or, is there another way?
>
> Thanks,
> Rick R
>
> --
> Rick Reeves
> Scientific Programmer/Analyst and Data Manager
> National Center for Ecological Analysis and Synthesis
> UC Santa Barbara
> www.nceas.ucsb.edu
> 805 892 2533
>
> _______________________________________________
> 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