Download data and R Code for this example
Project Requirement:
- Read two geocoded point sets from Comma Separated Value (CSV) files into R data objects.
- Assign to each member of first point set the geographically closest point from second set.
Input Data - Measurements at two different point location sets:
File 1: Population density

File 2:Air Temperature

Workflow:
1) Read input files into R Spatial data objects.
2) Filter/remove invalid points from temperature measurements.
2) Assign closest temperature point to each population density point.
3) Write results - the point assignments file - as an ASCII .CSV file.
The R Script:
# Latest version: Assign closest points from a second point set
require(sp)
temperaturePoints <- read.csv("temperaturePoints.csv")
densityPoints <- read.csv("densityPoints.csv")
# promote the input lists to SpatialPointsDataFrames
coordinates(temperaturePoints) <- c("longitude", "latitude")
coordinates(densityPoints) <- c("longitude", "latitude")
# Remove temparature points with 'invalid' value of -199
validTempPoints <- temperaturePoints[!temperaturePoints$temperature %in% c(-199),]
# Define these vectors, used in the loop.
closestSiteVec <- vector(mode = "numeric",length = nrow(densityPoints))
minDistVec <- vector(mode = "numeric",length = nrow(densityPoints))
# Get the vector index of the temperature station closest to each field station.
# Use the spDistsN1 function to compute the distance vector between each
# field station site and all of the temperature stations. Then, find and
# retain the actual temperature, and the index of the closest temperature
# to each transect station.
#
# spDistsN1 usage: spDistsN1(pointList, pointToMatch, longlat)
#
# where:
# pointList : List of candidate points.
# pointToMatch: Single point for which we seek the closest point in pointList.
# longlat : TRUE computes Great Circle distance in km,
# FALSE computes Euclidean distance in units of input geographic coordinates
#
# We use Great Circle distance to increase distance calculation accuracy at high latitudes
# See the discussion of distance units in the header portion of this file
#
# minDistVec stores distance from the closest temperature station to each density measurement point.
# closestSiteVec stores the index of the closest temperature station to each density measurement point.
#
for (i in 1 : nrow(densityPoints))
{
distVec <- spDistsN1(validTempPoints,densityPoints[i,],longlat = TRUE)
minDistVec[i] <- min(distVec)
closestSiteVec[i] <- which.min(distVec)
}
#
# Create the Temperature Assignment table: merge the temperature point list with the transect point list
# into a five-column table by merging the temperature point and transect point lists.
#
PointAssignTemps <- as(validTempPoints[closestSiteVec,]$temperature,"numeric")
FinalTable = data.frame(coordinates(densityPoints),densityPoints$density,
closestSiteVec,minDistVec,PointAssignTemps)
#
# Update the Temperature Assignment table column names
#
names(FinalTable) <- c("Long","Lat","Density","CloseTempIndex","Distance","Temperature")
#
# And, at the end, write the point assignment file.
#
message("Write temperature/density assignment table to disk in .csv format")
write.csv(FinalTable,file="FinalTempAssignments.csv")
#
Results - Temperature Assignment table with point assignments and support data:
File 1: Temperature Assignment table

Learning More:
The GIS Primer: The Nature of Spatial Information
The CRAN (R) Task View summarizes the R Analytical Environment's spatial data analysis and management tools.
Point of Contact for this Use Case: reeves [at] nceas [dot] ucsb [dot] edu (Rick Reeves), NCEAS Scientific Programmer
This Use Case was updated June, 2010.