XML Code

For this project, I took data from historical earthquake incidences, implemented it into Google Earth via XML, and create a short video in Google Earth to demonstrate earthquake incidences. The different magnitudes of earthquakes are represented with different sizes of the markers.

library(XML)
source(“~/Desktop/KMLPoints.R”)
quakes <- read.table(“~/Desktop/earthquakes.txt”, header=TRUE)
earthquakes <- readLines(“~/Desktop/earthquakes.txt”)

#Convert them to character vectors
quakes$Date <- as.character(quakes$Date)
quakes$Time <- as.character(quakes$Time)

#Put the Date and Time in the correct format as yyyy-mm-ddThh-mm-ssZ
quakes$Date <- paste(substr(quakes$Date, 1,4),”-“,substr(quakes$Date, 6,7),”-“,substr(quakes$Date, 9,10), sep = “”)
pattern <- “(^[[:digit:]]+:[[:digit:]]+:[[:digit:]]+)\\.[[:digit:]]+$”
quakes$Time <- gsub(pattern, ‘\\1’, quakes$Time)

#time.stamp is inputed into the for loop in KMLPoints.R file
time.stamp <- paste(quakes$Date,”T”, quakes$Time, “Z”, sep = “”)

#Inputed into the loop
ts <- newXMLNode(“TimeStamp”, parent=pm)
newXMLNode(“when”, time.stamp[i], parent=ts)

#Input styleUrl under Placemark
newXMLNode(“styleUrl”, “#msn_shaded_dot”, parent=pm)

#Input new color
sn <- newXMLNode(“Style”, attrs = list(id=”sh_shaded_dot”), parent=dmt)
is <- newXMLNode(“IconStyle”, parent=sn)
newXMLNode(“color”, “e64300ff”, parent=is)
newXMLNode(“scale”, magnitude[i], parent=is)
icon <- newXMLNode(“Icon”, parent = is)
newXMLNode(“href”, “http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png&#8221;, parent = icon)

#Create a different attribute for each unique magnitude
for(i in 1:length(unique(magnitude))){
sn <- newXMLNode(“Style”, attrs = list(id=unique(magnitude)[i]), parent=dmt)
is <- newXMLNode(“IconStyle”, parent=sn)
newXMLNode(“color”, “e64300ff”, parent=is)
newXMLNode(“scale”, unique(magnitude)[i], parent=is)
icon <- newXMLNode(“Icon”, parent = is)
newXMLNode(“href”, “http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png&#8221;, parent = icon)
}

#Input styleUrl into the 2nd for loop
newXMLNode(“styleUrl”, magnitude[i], parent=pm)

#Rerun KML
source(“~/Desktop/KMLPoints.R”)

###########################################################################
## The Edited KMLPoints.R file, which is also attached as a separate file##

library(XML)

## Load in the earthquake data
quakes <- read.table(“~/Desktop/earthquakes.txt”, header = TRUE)
dim(quakes)

## Extract the information we need;
## for now just work with first 10 earthquakes
longitude <- quakes$Lon; latitude <- quakes$Lat[1:100]
magnitude <- exp(floor(quakes$Mag))/100 ## Use later to size the icons

## Create a character vector of the numeric values in longitude and latitude –
## elements to be used as the text value for each coordinates node below
coord <-  paste(longitude,”,”, latitude, “,0 “, sep =””, paste = ” “)
head(coord)

## First create the document.
doc <- newXMLDoc()

## The root node is a kml node. Create this first.
kml <- newXMLNode(“kml”,
namespaceDefinitions = “http://www.opengis.net/kml/2.2&#8221;,
doc = doc)
doc

## Next comes the Document node, child of kml
dmt <- newXMLNode(“Document”, parent=kml)
doc

## Style & Scale
for(i in 1:length(unique(magnitude))){
sn <- newXMLNode(“Style”, attrs = list(id=unique(magnitude)[i]), parent=dmt)
is <- newXMLNode(“IconStyle”, parent=sn)
newXMLNode(“color”, “e64300ff”, parent=is)
newXMLNode(“scale”, unique(magnitude)[i], parent=is)
icon <- newXMLNode(“Icon”, parent = is)
newXMLNode(“href”, “http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png&#8221;, parent = icon)
}

## Name and description of the file
newXMLNode(“name”, “Earthquakes”, parent=dmt)
newXMLNode(“description”, “3+ Earthquakes, 1968-2008”, parent=dmt)
doc

## Look at
lookat <- newXMLNode(“LookAt”, parent=dmt)
newXMLNode(“longitude”, mean(longitude), parent = lookat)
newXMLNode(“latitude”, mean(latitude), parent = lookat)
newXMLNode(“altitude”, “1500000”, parent = lookat)
newXMLNode(“tilt”, “0”, parent = lookat)
newXMLNode(“heading”, “0”, parent = lookat)
doc

## For plotting individual points — each point is
## contained in its own Placemark node
for(i in 1:length(coord)){
if(i%%10 == 0) print(i)
pm <- newXMLNode(“Placemark”, parent=dmt)
pt <- newXMLNode(“Point”, parent = pm)
newXMLNode(“coordinates”, coord[i], parent=pt)
ts <- newXMLNode(“TimeStamp”, parent=pm)
newXMLNode(“when”, time.stamp[i], parent=ts)
newXMLNode(“styleUrl”, magnitude[i], parent=pm)
}
doc

## Save the tree as an xml document
saveXML(doc, file = “Earthquakes.kml”)

Advertisements
%d bloggers like this: