Learn R Programming

spatialEco (version 1.0-0)

point.in.poly: Point and Polygon Intersect

Description

Intersects point and polygon feature classes and adds polygon attributes to points

Usage

point.in.poly(x, y, poly.id = NULL)

Arguments

x

sp SpatialPointsDataFrame or SpatialPoints object

y

sp SpatialPolygonsDataFrame object

poly.id

Name of column containing unique polygon ID's

Value

A SpatialPointsDataFrame with intersected polygon ID's

Examples

Run this code
# NOT RUN {
require(sp)
data(meuse)
coordinates(meuse) = ~x+y
meuse@data$test.na <- NA

sr1=Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,  
  181007, 180409, 180162, 180114), c(332349, 332057, 332342, 333250, 333558,  
  333676, 332618, 332413, 332349)))),'10')
sr2=Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142,  
  179437, 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 
  331133, 331623, 332152, 332357, 332373)))),'20')
sr3=Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 
  179875, 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
  c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 
  329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'30')
sr4=Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
  c(332791, 333204, 333635, 333058, 332791)))),'40')
sr=SpatialPolygons(list(sr1,sr2,sr3,sr4))
srdf=SpatialPolygonsDataFrame(sr, data.frame(row.names=c('10','20','30','40'), 
                              PIDS=1:4, y=runif(4)))
   srdf@data$pid <- srdf@data$PIDS + 100 

# Intersect points with polygons and add polygon IDS to pts@data. 
  pts.poly <- point.in.poly(meuse, srdf, poly.id = "pid") 
    head(pts.poly)

# Point counts for each polygon
tapply(pts.poly$lead, pts.poly$pid, FUN=length)  

# }

Run the code above in your browser using DataLab