This document demonstrates how to use the soilDB package to export pedon locations from a local NASIS database and then use the plotKML package to generate KML files for viewing in Google Earth.

Setup Local NASIS DB

  1. load selected set with pedons and sites of interest
  2. select site-level attributes of interest to use as point labels
  3. modify output directory accordingly

Install R Packages

With a recent version of R, it should be possible to get all of the packages that this tutorial depends on with the following commands. Note that you only need to do this once.

# run these commands in the R console
install.packages('soilDB', dep=TRUE) # stable version from CRAN + dependencies
install.packages('rgdal', dep=TRUE)
install.packages('sp', dep=TRUE)
install.packages('plotKML', dep=TRUE)

Load Pedons and Generate KML files

Copy and paste the following code into a new R script document. Step through the lines of code (e.g. run each line starting from the top) by moving the cursor to the top line and then press ctrl + enter repeatedly.


# check working directory path and set output location

# load pedon data from selected set in NASIS
f <- fetchNASIS(from='pedons')

# some pedon data may be missing coordinates in NASIS
# filter to keep only those pedons with coordinates
good.idx <- which(!$x_std) & !$y_std))
f <- f[good.idx, ]

# initialize coordinates from WGS84 decimal degrees
coordinates(f) <- ~ x_std + y_std
# assign the appropriate projection string
proj4string(f) <- '+proj=longlat +datum=WGS84'

# extract only site-level data
s <- as(f, 'SpatialPointsDataFrame')

# graphically check by plotting them in R

# subset to columns from the site data that may useful as labels
s <- s[, c('site_id', 'pedon_id', 'obs_date', 'pedlabsampnum', 'taxonname', 'hillslopeprof', 'elev_field', 'slope_field', 'aspect_field', 'plantassocnm', 'bedrckdepth', 'bedrckkind', 'pmkind', 'pmorigin')]

# generate a basic KML file of site locations with site_id specified as the placemark labels
# set KML file path
kml_file_path <- 'C:/temp/R_sites.kml'
kml_open(,'Sites', overwrite=TRUE)
kml_layer.SpatialPoints(s, title='Sites', colour='royalblue', labels=site_id, shape="")

# could also be scripted this way using the plotKML() which automatically wraps the above functions
# notice that in this version, the paste function is used to concatenate the site_id and taxonname for labels
plotKML(s, colour_scale=rep('royalblue', 2), points_names=paste(s$site_id, s$taxonname, sep=", "),"Sites","R_sites.kml")

Screenshot from Google Earth