Over a million developers have joined DZone.

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

· Big Data Zone

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

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)

Hortonworks DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

Topics:

Published at DZone with permission of Kay Cichini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}