Site icon R-bloggers

Spatial Overlays with R – Retrieving Polygon Attributes for a Set of Points

[This article was first published on theBioBucket*, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A short tutorial for spatial overlays using R-GIS..

library(sp)
library(dismo)

# spatial data
alt <- getData('alt', country = "AT")
gadm <- getData('GADM', country = "AT", level = 2)

# view
plot(alt)
plot(gadm, add=T)

# some addresses
pts <- geocode(c("Aldrans, Grubenweg", "Wien, Stephansdom", "Salzburg, Mozartplatz"))

# make it spatial
coords <- SpatialPoints(pts[, c("longitude", "latitude")])
spdf_pts <- SpatialPointsDataFrame(coords, pts)

# assign CRS/projection
proj4string(spdf_pts) <- CRS(" +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

# check data
str(spdf_pts)

# plot it on top
plot(spdf_pts, cex = 2, col = 2, add = T)

# do an intersection (points in polygon)
# yielding the polygon's attribute data
over(spdf_pts, gadm)

To leave a comment for the author, please follow the link and comment on their blog: theBioBucket*.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.