2

I want to find which polygons in a spatialpolygonsdataframe overlay each polygon in another spatialpolygonsdataframe. I have tried to use the function over from the package sp and it works fine... except that (from the help page):

points on a polygon boundary and points corresponding to a polygon vertex are considered to be inside the polygon

This is a problem for me because I end up including all neighbouring polygons as well as ones that effectively intersect. Is there any way to get around this? If possible, I'd rather avoid 'hacky' solutions like shrinking the polygons in one of the dataframes.

EDIT: the argument minDimension = 2 should do what I need:

minDimension: minimal dimension for an intersection to be counted; -1 takes any intersection, and does not order; 0 takes any intersection but will order according to dimensionality of the intersections (if returnList is TRUE, 1 (2) selects intersections with dimension 1, meaning lines (2, meaning areas)

Unfortunately, it does not seem to work with my data... (I am intersecting level 1 administrative areas for GADM and ethnic homelands from Geo-EPR - if everything works fine, I should only get matches from homelands in the same country as the administrative area, but I get ones in neighbouring countries as well).

1

Perhaps you could use {rgeos}gContainsProperly().

For instance, if you have 3 polygons

polygon1 <- readWKT("POLYGON((-100 0, -100 50, 10 50, 50 0, -100 0))")
polygon2 <- readWKT("POLYGON((-80 20,  -80 40, 0 40, 0 10, -80 20))")
polygon3 <- readWKT("POLYGON((-100 0, -100 50, 10 50, 0 0, -100 0))")
  • polygon1 is the largest one
  • polygon2 is completely within polygon1
  • polygon3 partially shares its segments with polygon1

Then

gContainsProperly(polygon1, polygon2)     # Is polygon2 totally within polygon1? 
[1] TRUE
gContainsProperly(polygon1, polygon3)     # Is polygon3 totally within polygon1?
[1] FALSE

Application using GADM dataset would be:

# Obtain GADM data for France
library(raster)
FRA<- getData('GADM', country= 'FRA', level=1)
# Use rgeos::gContainsProperly to find France regions which do not touch country border 
library(rgeos)
FRA_outline <- gUnaryUnion(FRA)
result <- apply(gContainsProperly(FRA_outline, FRA, byid = TRUE), 1, any)
# Plot
plot(FRA, border= "gray")
plot(FRA_outline, col= NA, add= TRUE)
plot(FRA[result, ], border= "blue", add= TRUE)

enter image description here

The regions outlined in blue are not touching the country borders, selected by gContainsProperly function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.