Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Big Data Zone
Free Resource

NoSQL & Big Data Integration through standard drivers (ODBC, JDBC, ADO.NET). Free Download

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)

Easily connect any BI, ETL, or Reporting tool to any NoSQL or Big Data database with CData Drivers (ODBC, JDBC, ADO.NET). Download Now

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}