1

How can a true intersection/clipping of polygons be done in R?

Problem description: Shapefile1 contains only one feautre without attributes (or - at least - attibutes don't matter). Shapefile2 contains several features, some of them fall entirely, some of the partly in the feature contained in Shapefile1. Other features fall totally outside.

I need to perform a true clip or intersection (just like in QGIS Vector > Geoprocessing Tools > Intersect). So far, the solution is described here. However, in addition to the cited I also DO need the clip to inherit attributes of Shapefile2.

At the end, the result needs to be exported to an ESRI Shapefile.

Any answer, suggestion is welcome!

Contemplavit
  • 167
  • 1
  • 1
  • 8

1 Answers1

1

Solved with help of this thread.

clipped_geom <- gIntersection(shp1, shp2, byid = TRUE, drop_lower_td = T) #clip polygon2 with polygon1
id_list <- sapply(clipped_geom@polygons, function(x) x@ID)
intersect_list <- gIntersects(shp1, shp2, byid = TRUE)
clipped_data <-  at@data[intersect_list,]
row.names(clipped_data ) <- id_list
clipped <- SpatialPolygonsDataFrame(clipped_geom,clipped_data )

writeOGR(clipped,mypath,"myfile","ESRI Shapefile")
Ian Turton
  • 81,417
  • 6
  • 84
  • 185
Contemplavit
  • 167
  • 1
  • 1
  • 8
  • Here the handicap of stackoverflow.com and stackexchange.com being separate sites comes out: the answer has been present on stackoverflow for >2 years. See https://stackoverflow.com/questions/15075361/how-to-perform-a-vector-overlay-of-two-spatialpolygonsdataframe-objects – Contemplavit Dec 11 '17 at 10:00