|
|
Transform and Export Point Locations From GPS Using R
This
case presents an R script that: 1) Downloads position points collected with GPS receiver as text file; 2) transforms the points from Latitude/Longitude into Universal Transverse Mercator (UTM) coordinate system 3) Exports the transformed location coordinates (along with corresponding point IDs) as a Comma-Separated Value (CSV) file; 4) display the transformed point locations in a simple map with basic terrain features for context. Step 1) is performed with software supplied with the GPS device. Steps 2) through 4) are performed using the R environment.
Getting Started:
A task facing many field scientists is to download the spatial coordinates of point locations corresponding to field measurements onto desktop workstations for use in data analysis. A critical part of this task is transforming the point measurements from the vendor-specific format used by hand-held GPS units into a coordinate system and data format preferred by the scientist.
In this Use Case, we download, correct, and export to a text file a set of six point locations from a handheld GPS, using the proprietary software provided by the GPS manufacturer to transfer the points to an ASCII file on our computer. Then we use the R Programming Environment to perform the remaining steps. Note: this use case includes a brief discussion of a specific GPS software package, The R procedures discussed will work with GPS points downloaded from any GPS device, providing that the points can be downloaded into an industry-standard file format, for example, ASCII Comma-Separated Value (.CSV).
This example uses the following R packages:
-
maptools provides functions for spatial and space-time point pattern analysis of polygons.
-
sp is a critical base package
for spatial operations in R. It
provides definitions for basic spatial classes (points, lines,
polygons,
pixels, and grids) in an attempt to unify the way R packages represent
and manage these sorts of data. It also includes several core functions
for creating and manipulating these data structures. The sp package is automatically
loaded when the maptools package is used.
-
The base grDevices package provides base graphics services to other R packages, including point, line, and polygon drawing as well as color and pattern control.
If you have not already added these packages to your
local R installation, you can do so using the Packages/Install Packages option in the R GUI toolbar (MS Windows XP users), or using the
following statement at the R command line:
install.packages(c("maptools")) # sp also installed
library(maptools) # sp also loaded
For tips on installing R on the Ubuntu Linux platform widely used at NCEAS visit the link:
http://help.nceas.ucsb.edu/index.php/R
To run the sample code discussed here, download and expand this .zip archive:
(ExportGPSPointsUsingR.zip-20kb).
Step 1) Download and apply Differential Correction to point locations from GPS (using vendor-specific software)
The GPS coordinates used in this example were collected using a Trimble GPS receiver. While point files from some other GPS recievers can be read with open source software, at this writing (August 2007), there is no Trimble-compatible open-source GPS software. Therefore, we use the Trimble Pathfinder software, supplied with the GPS unit, to download and apply Differential Correction to the points. Here is a screen shot of the Trimble Pathfinder Differential Correction and Export (to ASCII file) graphical interface, which is representative of the features in other GPS receiver. utility programs:
Trimble Pathfinder GPS Software Interface: Differential Correction (Left), Export to ASCII File(Right)
Step 2) Import Point set into R, create a simple plot
Now that the point locations have been exported to the local computer, we can perform the rest of the processing steps using the R Programming Environment. First, we start R, load the maptools package, read the point shape file into an R Spatial Points Data Frame object, and display the points using a simple plot with labeled X (longitude) and Y (latitude) axes. The R code follows the plot diagram:
>library(maptools)
>setwd("c:/UseCases/ExportGPSPoints")
#
# Read and create simple plot using the ASCII input point file
# Default output shape file from Trimble Pathfinder is 'Gpoint'
#
>PointsFromGPS <- read.csv("Gpoint.csv")
#
# Create a SpatialPointsDataFrame data object, which streamlines
# use of the plot() and spTransform() methods.
# Column 1 is the single point attribute (ID number),
# Columns 2 - 3 are Longitude (X) and Latitude (Y) components.
#
>PointsAsFrame <- SpatialPointsDataFrame(PointsFromGPS[2:3],PointsFromGPS[1])
>plot(PointsAsFrame@coords[,1:2],xlab="longitude",ylab="latitude")
>title("Points From GPS (latitude/longitude in decimal degrees)"
|
 |
Step 3) Transform Points into the Universal Transverse Mercator (UTM) coordinate system
Because it uses a single-axis grid to represent any point on the Earth surface, latitude/longitude is the preferred coordinate system for geographic field coordinates. However, in some cases, scientists may need to re-project geographic coordinates into a different coordinate system. For example,all previous field measurements have been stored in a different coordinate system, such as the Universal Transverse Mercator (UTM) system. Therefore, to demonstrate R's point transformation capability, we convert the input points from latitude/longitude into UTM, using R's rgdal library version of the widely-used PROJ.4 Cartographic Projections software library:
 |
>library(rgdal)
#
# we need to set the projection string attribute to the point set to be transformed.
# The following line sets the incoming point set to the closet guess to the GPS environment:
#
>proj4string(PointsAsFrame) <- CRS("+proj=longlat +datum=NAD27")
#
# perform the transformation and plot the points
#
>ThePointsUTM <- spTransform(PointsAsFrame,CRS("+proj=utm +zone=11 +datum=NAD27"))
>dev.set(1)
>plot(ThePointsUTM@coords[,1:2],axes=TRUE)
>title("Points From GPS (latitude/longitude in UTM-meters)",xlab="longitude",ylab="latitude")
|
Step 4) Export points to CSV-format file
Next, we write the point file's latitude and longitude coordinates, along with the ID code for each point, to a CSV file.
Let's have a look at the fields/columns in the point file, in order to select the columns to include in the output file, along with
a screen shot of the data, in both Latitude/Longitude and UTM, as displayed in a standard text editor:
> attributes(ThePointsUTM)
$bbox
min max
coords.x1 537581.1 544890.9
coords.x2 4305702.7 4315291.5
$proj4string
CRS arguments:
+proj=utm +zone=11 +datum=NAD27 +ellps=clrk66
+nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat
$coords
coords.x1 coords.x2
[1,] 538241.5 4313928
[2,] 538918.3 4312980
[3,] 539749.7 4312335
[4,] 540040.2 4312119
[5,] 540499.1 4311955
[6,] 541898.2 4311682
[7,] 542372.7 4310662
[8,] 542282.0 4309891
[9,] 542113.7 4309578
[10,] 542367.2 4308874
[11,] 542699.5 4308117
[12,] 543365.2 4307364
[13,] 544489.9 4306441
[14,] 544890.9 4306147
[15,] 544661.2 4305703
[16,] 544135.1 4306436
[17,] 537581.1 4315291
$data
id
0 DO01
1 DO02
2 DO03
3 DO04
4 DO05
5 DO06
6 DO07
7 DO09
8 DO10
9 DO11
10 DO12
11 DO13
12 DO15
13 DO16
14 DO17
15 DO14
16 DO00
$coords.nrs
numeric(0)
$class
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
#
# The default behavior for the write.table()
# method is to write the $coords and $data fields
#
> write.table(ThePointsUTM,"CoordsUTM.csv",sep=",",row.names=FALSE,
col.names=c("ID","easting","northing"))
|
|  |
End of Example.
Learning More:
UTM Coordinates: What You Need to Know: A Good Description for GPS Navigators
The R Programming Language / CRAN Task View: Analysis of Spatial Data
Point of Contact for this Use Case: Rick Reeves, NCEAS Scientific Programmer reeves@nceas.ucsb.edu reeves@nceas.ucsb.edu
This Use Case was compiled August, 2007.
|