[Raster-commits] r430 - in pkg/raster: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Apr 24 16:47:20 CEST 2009


Author: rhijmans
Date: 2009-04-24 16:47:20 +0200 (Fri, 24 Apr 2009)
New Revision: 430

Added:
   pkg/raster/R/KML.R
   pkg/raster/man/KML.Rd
Log:


Added: pkg/raster/R/KML.R
===================================================================
--- pkg/raster/R/KML.R	                        (rev 0)
+++ pkg/raster/R/KML.R	2009-04-24 14:47:20 UTC (rev 430)
@@ -0,0 +1,51 @@
+# Derived, with only minor changes, from functions GE_SpatialGrid and kml Overlay 
+# in the maptools package. These were written by Duncan Golicher, David Forrest and Roger Bivand 
+# Adaptation for the raster packcage by Robert J. Hijmans, r.hijmans at gmail.com
+# Date : March 2009
+# Version 0.8
+# Licence GPL v3
+
+
+KML <- function (raster, filename, col=rainbow(255), maxdim=1000) {
+    if (!isLatLon(raster)) { 
+        stop("raster must be in geographical coordinates")
+	}
+	raster <- sampleSkip(raster, maxdim=maxdim,  asRaster = TRUE)
+
+	imagefile <- filename
+	ext(imagefile) <- '.png'
+	kmlfile <- filename
+	ext(kmlfile) <- '.kml'
+
+	png(file = imagefile, width=ncol(raster), height=nrow(raster), bg="transparent")
+	par(mar=c(0,0,0,0), xaxs="i", yaxs="i")
+	image(raster, col=col)
+	dev.off()
+
+	name <- layerNames(raster)[1]
+	if (name == "") { name <- 'raster' }
+    bb <- extent(raster)
+    W <- xmin(bb)
+    E <- xmax(bb)
+    S <- ymin(bb)
+    N <- ymax(bb)
+	
+    kmlheader <- c("<?xml version='1.0' encoding='UTF-8'?>", 
+        "<kml xmlns='http://earth.google.com/kml/2.0'>", "<GroundOverlay>")
+    kmname <- paste("<name>", name, "</name>", sep = "")
+	
+    icon <- paste("<Icon><href>", imagefile, "</href><viewBoundScale>0.75</viewBoundScale></Icon>", 
+        sep = "")
+    latlonbox <- paste("<LatLonBox><north>", N, "</north><south>", 
+        S, "</south><east>", E, "</east><west>", W, "</west></LatLonBox>", 
+        sep = "")
+    footer <- "</GroundOverlay></kml>"
+    x <- (kmlheader)
+    x <- append(x, kmname)
+    x <- append(x, icon)
+    x <- append(x, latlonbox)
+    x <- append(x, footer)
+    cat(paste(x, sep = "", collapse = "\n"), file = kmlfile, sep = "")
+}
+
+

Added: pkg/raster/man/KML.Rd
===================================================================
--- pkg/raster/man/KML.Rd	                        (rev 0)
+++ pkg/raster/man/KML.Rd	2009-04-24 14:47:20 UTC (rev 430)
@@ -0,0 +1,34 @@
+\name{KML}
+
+\alias{KML}
+
+\title{KML}
+
+\description{
+Export raster to a KML file
+}
+
+\usage{
+KML(raster, filename, col=rainbow(255), maxdim=1000) 
+}
+
+\arguments{
+\item{raster}{RasterLayer}
+\item{filename}{output filename}
+\item{col}{color scheme to be used}
+\item{maxdim}{sampling scheme}
+}
+
+\details{
+}
+
+\value{
+None. But two files are written to disk. 
+}
+
+\author{adapted for the raster package by Robert J. Hijmans; based on functions in the maptools package by Duncan Golicher, David Forrest and Roger Bivand }
+
+\examples{
+}
+
+\keyword{spatial}



More information about the Raster-commits mailing list