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!
Asked
Active
Viewed 3,800 times
2
-
2as easy as converting your lat/long dataframe to a `SpatialPointsDataFrame` and then use the function `over` from `library(sp)` – G. Cocca Apr 25 '16 at 21:28
-
This may be helpful: http://stackoverflow.com/questions/13316185/r-convert-zipcode-or-lat-long-to-county/20131701#20131701 – ano Apr 25 '16 at 21:35
-
Worked great. Thanks. – user3786999 Apr 28 '16 at 13:46
1 Answers
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)
:
- Convert lat/long point dataframe to a
SpatialPointsDataFrame
usingsp
. - Use
sp
'sproj4string
function to reproject theSpatialPointsDataFrame
so that it uses the same projection as my polygon shapefile. - Use
EcoSpatial
'spoints.in.poly
function to spatially join the two datasets. This is the key step, as it creates a newSpatialPointsDataframe
that affixes relevant polgyon data to each xy point. - Use
as.data.frame
to coerce theSpatialPointsDataframe
back into a regularR
data.frame
.
Thanks again for the help!

patL
- 2,259
- 1
- 17
- 38

user3786999
- 1,037
- 3
- 13
- 24