KML generator

From Kbwiki
Revision as of 00:13, 29 September 2007 by Aubrey Moore (Talk | contribs) (New page: <pre> ####################################################################################################### # # KMLgenerator.R # # A. Moore (aubreymoore.guam.net) 29 SEP 2007 # # This R ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#######################################################################################################
#
# KMLgenerator.R
#
# A. Moore (aubreymoore.guam.net) 29 SEP 2007
#
# This R program converts a tab delimited text file into a KML file to facilitate plotting points
# with Google Eath.
#
# Column labels in the first row of the tab delimited text file must include:
# Name, Description, Latitude, Longitude, Icon, Color.
# Additional columns may be in the file, but they will not be used.
# The KML file will have the same name as the input file, except for extension.
#
# Example input file:
#
# Name	Description	Latitude	Longitude	Color	Icon
# G1	13-Sep-07	13.50882	144.80062	ff0000ff	http://maps.google.com/mapfiles/kml/paddle/G.png
# G2	19-Sep-07	13.52416	144.80399	ff0000ff	http://maps.google.com/mapfiles/kml/paddle/G.png
# G3	19-Sep-07	13.52428	144.80405	ff0000ff	http://maps.google.com/mapfiles/kml/paddle/G.png
#
#######################################################################################################

filename <- file.choose()
data <- read.delim(filename)
substring(filename,nchar(filename)-2,nchar(filename))<-'kml'
kml <- file(filename,"w")

cat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",file=kml)
cat("<kml xmlns=\"http://earth.google.com/kml/2.2\">\n",file=kml)
cat("<Document>\n",file=kml)
for (i in 1:nrow(data)){
  cat("  <Placemark>\n",file=kml)
  cat("    <name>", as(data$Name[i],"character"), "</name>\n",file=kml,sep="")
  cat("    <description>", as(data$Description[i],"character"), "</description>\n",file=kml,sep="")
  cat("    <Style>\n",file=kml)
  cat("        <IconStyle>\n",file=kml)
  cat("          <color>", as(data$Color[i],"character"), "</color>\n",file=kml,sep="")
  cat("          <Icon>\n",file=kml)
  cat("            <href>", as(data$Icon[i],"character"), "</href>\n",file=kml,sep="")
  cat("          </Icon>\n",file=kml)
  cat("        </IconStyle>\n",file=kml)
  cat("        <LineStyle>\n",file=kml)
  cat("          <color>", as(data$Color[i],"character"), "</color>\n",file=kml,sep="")
  cat("          <width>2</width>\n",file=kml)
  cat("        </LineStyle>\n",file=kml)
  cat("    </Style>\n",file=kml)
  cat("    <Point>\n",file=kml)
  cat("      <extrude>1</extrude>\n",file=kml)
  cat("      <altitudeMode>relativeToGround</altitudeMode>\n",file=kml,sep="")
  cat("      <coordinates>",data$Longitude[i],",",data$Latitude[i],",50</coordinates>\n",file=kml,sep="")
  cat("    </Point>\n",file=kml)
  cat("  </Placemark>\n",file=kml)
}
cat("</Document>\n", file=kml)
cat("</kml>",file=kml)
close(kml)