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

June 19, 2013
By

(This article was first published on theBioBucket*, and kindly contributed to R-bloggers)

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 his blog: theBioBucket*.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.