[R-sig-Geo] Generate a vector of names of intersecting polgons

Robert J. Hijmans r.hijmans at gmail.com
Tue Oct 28 04:04:15 CET 2014


Steven, I believe you can use something like the below

library(raster)
# example data (p, d)
p <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(p)
values(r) <- 1:ncell(r)
d <- as(r, 'SpatialPolygonsDataFrame')

# approach 1
x <- intersect(p, d)

# approach 2
y <- union(p,d)
y <- y[!is.na(y$ID_1), ])

The approach to take depends on the case (does B fall entirely within A?)



On Mon, Oct 27, 2014 at 5:43 PM, Steven Ranney <steven.ranney at gmail.com> wrote:
> All -
>
> As a disclaimer, I admit that I am relatively new to spatial data
> manipulation with R.  I am familiar with some of the more basic functions
> and packages but need some guidance.
>
> What I want to do:
>
> I have two .shp files, shpA and shpB, both of which contain many polygons.
> I would like to overlay shpA on shpB and get a vector of the polygon names
> in shpB that intersect with polygons in shpA.  In a perfect world, I would
> then be able to join the vector of names to each polygon in shpA as a new
> attribute.
>
> I tried using over() but over() didn't like the fact that both of my .shp
> files were SpatialPolygonsDataFrames.
>
> Is there a simple way to do this?  Can anyone offer some advice?
>
> Thanks -
>
> SR
> Steven H. Ranney
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list