2

I'm hoping to write an R program that reads in a data frame of lat/long points and a shapefile of 13 polygons, and then identifies which polygon each lat/long point is located within. Is there an R package that enables me to do this type of spatial join? I've been using rgdal to read the polygon shapefile, but I don't see an option for conducting a spatial join with the lat/long columns in my dataframe. Thanks!

user3786999
  • 1,037
  • 3
  • 13
  • 24

1 Answers1

2

G. Cocca's solution worked great after some minor modifications.

Ultimately, the solution required four steps using R's library(sp) and library(EcoSpatial):

  1. Convert lat/long point dataframe to a SpatialPointsDataFrame using sp.
  2. Use sp's proj4string function to reproject the SpatialPointsDataFrame so that it uses the same projection as my polygon shapefile.
  3. Use EcoSpatial's points.in.poly function to spatially join the two datasets. This is the key step, as it creates a new SpatialPointsDataframe that affixes relevant polgyon data to each xy point.
  4. Use as.data.frame to coerce the SpatialPointsDataframe back into a regular R data.frame.

Thanks again for the help!

patL
  • 2,259
  • 1
  • 17
  • 38
user3786999
  • 1,037
  • 3
  • 13
  • 24