2

I am trying to create a new set of SpatialPoints from the gDifference of two sp geometries. Suppose the following:

You have two SpatialPolygons:

library(rgeos)
library(sp)

#Create SpatialPlygons objects
polygon1 <- readWKT("POLYGON((-190 -50, -200 -10, -110 20, -190 -50))")          
#polygon 1
polygon2 <- readWKT("POLYGON((-180 -20, -140 55, 10 0, -140 -60, -180 -20))") #polygon 2

#Plot both polygons
par(mfrow = c(1,2)) #in separate windows
plot(polygon1, main = "Polygon1") #window 1
plot(polygon2, main = "Polygon2") #window 2

Now, you want to get the gDifference between them:

polygon_set <- readWKT(paste("POLYGON((-180 -20, -140 55, 10 0, -140 -60, -180 -20),",
                             "(-190 -50, -200 -10, -110 20, -190 -50))"))

par(mfrow = c(1,1)) #now, simultaneously
plot(polygon_set, main = "Polygon1 & Polygon2")


clip <- gDifference(polygon2, polygon1, byid = TRUE, drop_lower_td = T) #clip polygon 2 with polygon 1
plot(clip, col = "red", add = T)

enter image description here

How can I get a sp geometry with only the non-intersecting points of the polygon2 (i.e., the red points in the following image)?

enter image description here

Thanks in advance!

Hack-R
  • 22,422
  • 14
  • 75
  • 131
topcat
  • 586
  • 1
  • 6
  • 30
  • turn them into individual lines and test for intersection then pick the ones that don't? – hrbrmstr Jul 31 '16 at 19:25
  • I have tried that, but the whole `polygon2` is returned. I assume that it only consider intersection when the whole line is "contained" – topcat Jul 31 '16 at 19:32
  • 2
    After you [determine the points of intersection](http://stackoverflow.com/questions/20519431/finding-point-of-intersection-in-r), you need to determine the ordering that produces the desired polygon. The `ggplot2::fortify` function will give a nice data.frame for your use in plotting, after you adjust the ordering. – shayaa Jul 31 '16 at 19:32

1 Answers1

1

I think the comparison of the coords of clip and polygon2 gives you the non-intersecting points.

library(ggplot2)                         # as @shayaa commented, ggplot2::fortify is useful.

clip_coords <- fortify(clip)[,1:2]          # or, clip@polygons[[1]]@Polygons[[1]]@coords
polygon2_coords <- fortify(polygon2)[,1:2]  # or, polygon2@polygons[[1]]@Polygons[[1]]@coords
duplicated_coords <- merge(clip_coords, polygon2_coords) 
  # duplicated_coords is the non-intersecting points of the polygon2
res <- SpatialPoints(duplicated_coords)

plot(clip)
plot(res, col="red", pch=19, add=T)

enter image description here

[ Additional code : ver. intersecting points (suppose above code has run) ]

## an independent method
library(ggplot2); library(dplyr)

clip2 <- gIntersection(polygon2, polygon1, byid = TRUE, drop_lower_td = T)
res2.1 <- fortify(clip2)[,1:2] %>% setdiff(polygon2_coords) %>%   # not_duplicated_coords
  SpatialPoints()


## a method usign a gDifference.sp.coords
res2.2 <- fortify(clip2)[,1:2] %>% merge(clip_coords) %>% 
  distinct() %>% SpatialPoints()    # res2.2 is equal to res2.1 in elements.


plot(polygon_set, main = "Polygon1 & Polygon2")
plot(clip2, col="red", add=T)
plot(res2.1, col="blue", pch=19, add=T)

enter image description here

cuttlefish44
  • 6,586
  • 2
  • 17
  • 34
  • I used `tidy` instead of `fortify`, the outcomes remains equal. I have been trying the same code to get the `gIntersection` **and not the `gDifference`**, but the resulting `sp` object is clip and not the `SpatialPoints` of the polygon. `merge(clip_coords, polygons2_coords, all.x = T)` do not work. – topcat Aug 02 '16 at 17:43
  • 1
    OK, I edited. Is this the answer you were hoping for ? – cuttlefish44 Aug 03 '16 at 02:28