Previous steps

If you would like to return to information from the previous section, please click here

Context

Progressively, field monitoring programmes are relying more on accurate spatial information to locate permanent transects, plots, and creating photomosaics. In many cases, handheld GPS units, smartphones, and other instruments are GPS enabled allowing for spatial information to be readily collected.

Data from these types of devices are often saved in different formats (e.g. *.gpx). As part of data training for coral reef monitoring, it is helpful to know how to directly import these into R and join with biological, habitat and other monitoring data.

This wiki page provides an example from one of the most common formats (i.e. *.gpx) with the idea that this skill can be applied to other similar formats.

The code for these examples can be found here:

creation_code/examples/mapping/create_coral_gps_waypoints.R and creation_code/examples/mapping/create_coral_gps_transects.R.

Importing Points

To illustrate the basic techniques for importing and manipulating spatial point data, we will start with an example from coastal surveys of the Galápagos Islands. In later exercises, we will work with data from the WIO region.

The start up for this example begins with calling additional functionality and setting utm details:

  # call to additional functionality
    library(tmaptools)

  # set utm details
    utm_details <-
      paste0("+proj=utm +zone=15 +south +datum=WGS84",
             " +units=m +no_defs +ellps=WGS84") %>% CRS()

  # set details for gpx
    gpx_details <-
      paste0("+proj=longlat +datum=WGS84 ",
             "+no_defs +ellps=WGS84") %>% CRS()

We are needing to set different utm details, as our GPS coordinates are in WGS84 (i.e. Latitude, Longitude) and our coastline is in UTM (i.e. zone 17 South).

The strategy for importing starts by identifying files that have "Waypoints_" in the file name and then importing each one in a for() loop. The main reason for this is that we will be manipulating the individual files (i.e. projecting WGS84 to UTM), binding the different coordinate systems together, specifying the date-time format, et cetera).

The first step looks like this:

##
## 2. Import data
##
  # set data locale
    data_locale <- "data_raw/examples/mapping/gpx/"

 ## -- load  gps tracks -- ##
  # set individual transects
    file_list <-
      data_locale %>% list.files(pattern = "Waypoints_")

  # create empty object to hold results
    coral_gps_waypoints <- tibble()

  # loop to importing   # i=1  ## -- for testing -- ##
    for(i in 1:length(file_list)){

      # print progress to screen
        cat(paste0("   ...importing ", file_list[i],
            " [ ", i, " of ", file_list %>% length(), " ]\n"))

      # call to data
        dat <-
          paste0(data_locale, file_list[i]) %>%
          read_GPX()

Prior to starting the importing loop, we will create an empty object coral_gps_waypoints to gather the results from all of our "Waypoints_" files. To help with a long list of files, we will print the progress to screen using cat(). The functionality for importing files from a Garmin GPS waypoint file (i.e. *.gpx) comes from the tmaptools package (i.e. read_GPX() ).

The next step extracts the waypoint data from the object, converting it to a spatial object, and then converting the projections:

     # create spatial point object
        dat_points <-
          dat$waypoints %>%
          as("Spatial")

      # project to utm
        dat_utm <-
          dat_points %>%
          SpatialPoints(proj4string = gpx_details) %>%
          spTransform(CRS = utm_details)

      # get utm coordinates
        utm_coordinates <-
          dat_utm %>%
          coordinates() %>%
          data.frame() %>%
          mutate(track_id = point_list[k]) %>%
          dplyr::select(track_id,
                        easting  = coords.x1,
                        northing = coords.x2) %>%
          as_tibble()

      # get wgs 84 coordinates
        wgs84_coordinates <-
          dat_points %>%
          coordinates() %>%
          data.frame() %>%
          mutate(track_id = point_list[k]) %>%
          dplyr::select(track_id,
                        longitude = coords.x1,
                        latitude  = coords.x2) %>%
          as_tibble()

The next step is to convert the time and select only the necessary columns:

      # extract data
        position_data <-
          dat_points@data %>%
          mutate(time = time %>%
                          strptime(format = "%Y-%m-%d %H:%M:%S") %>%
                          as.POSIXct()) %>%
          dplyr::select(waypoint_id = name,
                        ele,
                        time,
                        desc,
                        sym) %>%
          as_tibble()

Users can modify this to their particular application. For example, the symbols (or icons) that can be added to individual waypoints can be saved as part of the data object (e.g. if different symbols refer to “start” or “end” points of a transect, habitat types or taxa).

The last step is to bind the different coordinate systems into a single file and stack them as part of the main object coral_gps_waypoints:

      # bind with positions
        dat_utm <-
          utm_coordinates %>%
          bind_cols(wgs84_coordinates %>%
                      dplyr::select(longitude,
                                    latitude)) %>%
          bind_cols(position_data)

       # harvest data
         coral_gps_waypoints %<>%
           bind_rows(dat_utm)

We then save this object as an *.rda file for linking with additional information (e.g. percent cover data) and visualising. This will be put to practise in the Exercises and Homework for this module.

Importing Tracks

The strategy for importing track data is the same as waypoint data. We will start by identifying the files with the pattern "Track_" in the file name and then use a for() loop to import and modify the individual files:

##
## 2. Import data
##
  # set data locale
    data_locale <- "data_raw/examples/mapping/gpx/"

 ## -- load  gps tracks -- ##
  # set individual transects
    file_list <-
      data_locale %>% list.files(pattern = "Track_")

  # create empty object to hold results
    coral_gps_transects <- tibble()

  # loop to importing   # i=1  ## -- for testing -- ##
    for(i in 1:length(file_list)){

      # print progress to screen
        cat(paste0("   ...importing ", file_list[i],
            " [ ", i, " of ", file_list %>% length(), " ]\n"))

      # call to data
        dat <-
          paste0(data_locale, file_list[i]) %>%
          read_GPX()

      # set to points
        dat_points <-
          dat$track_points %>%
          as("Spatial")

There are subtle differences with format of track spatial data, but the process for converting to UTM and binding the data objects is the same as above.

After saving the resulting file to the data_intermediate folder as an *.rda file, we can then link it other data and visualise.

Next Steps

Another task commonly encountered from the collection of spatial data in the field is manual recording of coordinates, which can be in a myriad of different formats and symbology. This is particularly common when aggregating data across different years and sites, where GPS units were configured for displaying coordinates in different ways.

For our next lesson, we will draw on our skills of data wrangling to get spatial coordinates into shape.