Following on from an answer about intersecting polygons with lines to chop up a polygon into smaller polygon units (in QGIS), I wanted to try the same thing in R. However, I cannot seem to find a method that works!
over() doesn't have a method for polygons intersecting with lines; I found gIntersection() from rgeos but it fails:
require(sp)
require(rgeos)
poly <- readShapePoly("polygon.shp")
lines <- readShapeLines("lines.shp")
chopped <- gIntersection(poly, lines)
Giving:
Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, "rgeos_intersection") :
UnsupportedOperationException: GeometryGraph::add(Geometry *): unknown geometry type: N4geos4geom18GeometryCollectionE
Update: Here's a link to the files in question.
Update 2: PaulG notes that it works and after updating rgeos and R I got rid of the error above. Thanks PaulG ...
However, gIntersection results in a SpatialLines object no matter whether I put in (poly, lines) or (lines, poly) - whereas the operation I did in QGIS (or Arc, back in the bad ol' days) will divide a polygon with the lines and result in a polygon object, not line.
So, how do I chop up my polygon with the lines and get polygons out?
rgeos::gIntersection()to also be quite fussy about the nature of theSpatial*object you pass it. I have also solved very cryptic error messages from this function by using thebyid=TRUEparameter. Not sure if this will work in your case. – digitalmaps Apr 22 '12 at 01:38Spatial*object. Try producing aSpatialLinesand aSpatialPolygonsobject from first principles and compare what you have there to the object you load usingreadShapePoly(). It could be something like IDs are not continuous, or hole attributes. – digitalmaps Apr 22 '12 at 14:16poly <- readShapePoly("poly.shp"). There are no filespolygon.*in the zip you provided. It then worked for me, performing the intersection as expected. Could the error be as simple as loading the wrong shape file? (I am usingrgeospackage version 0.2-1 under R 2.14.2) – digitalmaps Apr 22 '12 at 20:11gIntersection()will really do what you want. If your lines, forming a grid, were polygonized, it might work, but this is probably not what you want. I suggest asking this either on StackOverflow where more spatial R folks hang out, or on the R-sig-geo list where you'll reach the architects ofrgeosand related tools. – digitalmaps Apr 30 '12 at 01:34