11

I am trying to remove the intersect from Poly2 onto Poly1. Below are the coords of the 2 polygons.

> coords1
          [,1]     [,2]
 [1,] 992.0161 7462.531
 [2,] 950.1962 7413.532
 [3,] 902.3632 7421.207
 [4,] 706.1985 7472.378
 [5,] 654.0139 7711.760
 [6,] 657.5960 7726.950
 [7,] 786.2667 7844.380
 [8,] 789.5935 7842.925
 [9,] 981.7046 7498.659
[10,] 983.4246 7493.271
[11,] 990.6680 7469.888
[12,] 992.0161 7462.531
> coords2
         [,1]     [,2]
[1,] 930.3464 7607.313
[2,] 979.3528 7502.470
[3,] 865.8662 7484.337
[4,] 850.5665 7594.947
[5,] 930.3464 7607.313

Poly1<-Polygon(coords1)
Poly2<-Polygon(coords2)

The 2 polygons looks like thisenter image description here What I would like to get at the end is the dark blue polygon (Poly1) without the light blue (Poly2) area i.e., new eastern coordinates.

3 Answers 3

8

Try the gDifference function from the rgeos package:

coords1 <- as.matrix(read.table(textConnection("
992.0161 7462.531
950.1962 7413.532
902.3632 7421.207
706.1985 7472.378
654.0139 7711.760
657.5960 7726.950
786.2667 7844.380
789.5935 7842.925
981.7046 7498.659
983.4246 7493.271
990.6680 7469.888
992.0161 7462.531
")))

coords2 <- as.matrix(read.table(textConnection("
930.3464 7607.313
979.3528 7502.470
865.8662 7484.337
850.5665 7594.947
930.3464 7607.313
")))

library("sp")
poly1 <- Polygon(coords1)
poly2 <- Polygon(coords2)
# create SpatialPolygons objects
p1 <- SpatialPolygons(list(Polygons(list(poly1), "p1")))
p2 <- SpatialPolygons(list(Polygons(list(poly2), "p2")))

library("rgeos")
res <- gDifference(p1, p2)
plot(res, col="blue")

gDifference result

5

The solution using rgeos::gDifference is much faster, but it returns a simple SpatialPolygons object.

If you are working with two SpatialPolygonsDataFrames and you want to keep the dataframe information, you can simply do this, and the result will be a SpatialPolygonsDataFrame:

library(raster)

res <- p1 - p2
6
  • Using this solution, I get the error: "non-numeric argument to binary operator"
    – Ashe
    Commented Mar 17, 2017 at 15:13
  • @Ashe , type class(my_obj) to check whether both of your objects are SpatialPolygonsDataFrame Commented Mar 17, 2017 at 15:22
  • Indeed, I get SpatialPolygonsDataFrame as the class.
    – Ashe
    Commented Mar 17, 2017 at 15:25
  • Hmmm strange. I'm not sure why this would happen. Perhaps you should open a new question of how to Remove intersect of two SpatialPolygonsDataFrames while keeping the data info Commented Mar 17, 2017 at 15:48
  • 1
    I have fixed the issue. I needed to install the raster library for this solution to work. That addition to the answer would be helpful. Also, credit to this answer: gis.stackexchange.com/a/169597/93643
    – Ashe
    Commented Mar 17, 2017 at 15:48
0

If you want to use the sf package, you can use the st_difference() function.

library(sf)

coords1 <- as.matrix(read.table(textConnection("
992.0161 7462.531
950.1962 7413.532
902.3632 7421.207
706.1985 7472.378
654.0139 7711.760
657.5960 7726.950
786.2667 7844.380
789.5935 7842.925
981.7046 7498.659
983.4246 7493.271
990.6680 7469.888
992.0161 7462.531
")))

coords2 <- as.matrix(read.table(textConnection("
930.3464 7607.313
979.3528 7502.470
865.8662 7484.337
850.5665 7594.947
930.3464 7607.313
")))

poly1 <- st_polygon(list(poly1 = coords1))
poly2 <- st_polygon(list(poly1 = coords2))
pols <- st_sfc(poly1, poly2)
plot(pols)

res <- st_difference(pols[[1]], pols[[2]])
plot(res)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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