Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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