You could try using RSAGA. I'm not too familiar with it myself, but the command would be something like:
rsaga.geoprocessor("libshapes_polygons", "Polygon-Line Intersection", list(POLYGONS="polygonshape.shp",LINES="lineshape.shp",INTERSECT="result.shp"))
I cannot as yet offer a complete solution, but the immediate issue is that the parcel
object has invalid geometry. If you can fix the geometry, we should be able to get the intersections to work. As an example of problematic geometry, this expression is fine:
gIsValid(hr_kud, reason = TRUE)
But this one will consistently crash R:
gIsValid(parcel, reason = TRUE)
So, one or more of the 20,069 polygons in parcel
appears to be broken in some way. I don't know how to validate geometry within R but Quantum GIS does have a tool for this. I assume that you have the original shapefile or other data source from which you created parcel
but you can recreate this by using the writeOGR
function from the rgdal
library like so:
library(rgeos)
library(rgdal)
setwd("your_directory_name_here")
load("Data.RData")
class(parcel)
class(hr_kud)
object.size(hr_kud)
object.size(parcel)
parcel@proj4string
hr_kud@proj4string
gIsValid(hr_kud, reason = TRUE)
#gIsValid(parcel, reason = TRUE) # warning! this will crash R
writeOGR(parcel, ".", driver = "ESRI Shapefile", layer = "poly")
This should produce 4 files:
poly.shp
poly.shx
poly.dbf
poly.prj
In QGIS, first load your shapefile by going to the 'Layer' menu and clicking on 'Add Vector Layer..." then select the poly.shp
file or the folder in which the file resides. Then go to the 'Vector' menu, click on the 'Geometry Tools' menu item then on the 'Check geometry validity' item. Then click OK on the next dialog box. With more than 20k polys this took quite some time (about half an hour I think), but the result was this:
[EDIT: I upgraded to QGIS 1.8 Lisboa with two results - a massive increase in validation speed and the discovery of other errors, as per the revised screenshot below.]
So, it looks like you have a number of issues there. Before you close the dialog box, click on the error in the dialog box list and QGIS will show you the offending polygon. I'm not experienced in this kind of GIS but the tips in this tutorial might help. Google QGIS and "self intersecting" for more ideas.
Best Answer
I emphasize that this function might not be useful for everyone. It will depend on what you're trying to do, and your inputs, and how possible it is to do what you're trying to do given your inputs. For instance, if you have a complicated shape with concave sides, forget about it. In my case, I just wanted to cut a box filled with random points down to a polygon (the outline of an island) where only points inside the polygon were retained. It had taken over an hour already when I got annoyed, stopped it, and programmed this. This only took 100 seconds when run in parallel on a quad core OS X machine. I was trying to find the intersection of a SpatialPolygonsDataFrame (extracted.coords) and some points (to.check). The question was which points in to.check fell outside the extracted.coords. Here's what I did to do that.
Extract all the coordinates of the SpatialPolygonDataFrame. I offered a possible way to do that here.
Then, this function will find the convex hull of those points, extract just the points that compose that hull, then successively rbind each point in to.check into the points that compose the hull, see if the area of the hull increases, and mark the point for cutting if so. In the end it cuts all such points and returns a matrix of points that do fall within the convex hull of extracted.coords.
The function can easily be modified to run sequentially and not in parallel (just change dopar to do, or change the foreach loop to a standard for loop).