##

### Duncan Temple Lang

University of California at Davis
Department of Statistics

##

This is an example of creating a KML display for Google Earth. We use the housing price data and, for simplicity, use just the houses sold in Marin county. We have a Placemark for each house and use the street address as the label displayed for that placemark. When the viewer clicks on the placemark, we display a simple HTML table giving the details of that observation. This is a subset of the variables we have for that house.

We start by loading the housing data

    library(RKML)
    load("~/Data/housing.Rda")

We compute a subset of the data to reduce the number of points.

    # marin = subset(housing, county == "Marin County")
    housing = subset(housing, county == "Marin County")

Now we are ready to focus on creating the KML. We need to create the names and the HTML descriptions for each placemark. The names are just the string versions of the street names

    ids = as.character(housing$street)

The HTML description for a given house involves constructing an HTML <xml:table>table</xml:table> with a row for each of the variables. Each row has the name of the variable and then the value. We construct this as a string (rather than XML nodes). This is done with the following function

    makePropDesc =
       function(row) {
          tmp = sprintf("<tr><td>%s</td><td>%s</td></tr>", names(row), row)
          paste(c("<table>", tmp, "</table>"), collapse = "\n")
       }

We can then use this function for each observation in our data set using apply()

    description =
      apply(housing[, c("city", "zip", "street", "price", "br", "lsqft", "bsqft", "year", "date")], 1,
             makePropDesc)

We need to check the values in description are what we expect.

Now we have the relevant pieces for constructing our KML document. We can use the kml() function. We specify a formula and a data set. The formula specifies which variables in the dataset are the longitude and latitude.

    doc = kml(~ long + lat, housing, .names = ids, description = description)

Now we have the KML document as a tree in R. We write it to a file using saveXML() .

    saveXML(doc, "marin.kml", fixHTMLRefs = FALSE)

We can then load it into Google Earth or put in on a web site or email it to somebody.

We could do this for all houses in the data set and not just Marin county. Or we might want to split the observations into a separate folder for each citty. This would allow the viewer to easily hide or display all the houses for a given city. We can do this using the by turning our code above into a function and using the by() function. Alternatively, we can use a conditioning variable in our formula, e.g.

    doc = kml(~ long + lat | city, housing, .names = ids, description = description)
    saveXML(doc, "marinByCity.kml", fixHTMLRefs = FALSE)

| Note
This is not working at present. Also, if we specify city as the group argument, we don't look in the data for that variable.

We also may want to use a different colored icon for each placemark. For instance, we might use a color to indicate the price (as a categorical variable).