I'm working on a project involving bringing together multiple subjects circling difficult to find parts of an image. The output from users is pretty awesome.
Cool, right?
However, what I want is to get the area of the image that has 1, 2, 3, 4, etc. users selecting it. And I'm using R for the analysis with the sp, raster, and maptools packages (with a little rgeos throw in when need be). Essentially, my workflow is to 1) create a single polygon from each user's input (I actually have the track of each individual selection per user, but it seemed easier to combine them into a single SpatialPolygon per user) 2) merge them all together into a single SpatialPolygonsDataFrame Object 3) Rasterize the object using the count function 4) Assess area of the raster with n number of users selecting it
Here's the output SpatialPolygonsDataFrame to play with
This works great, but is slooow due to the rasterization. I feel like I should be able to do something with the SpatialPolygons object. And I've tried a few things with union, gIntersects, gUnion, and intersect. However, I keep getting errors such as the following
Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, "rgeos_difference") :
TopologyException: Input geom 1 is invalid: Self-intersection at or near point
Fair enough. Often a given users SpatialPolygons are invalid when I test them using gIsValid. Looking at, say, just the first two, this becomes obvious that there are many overlapping points.
You can see the intersecting points pretty clearly. Furthermore, in poly2 (the blue) there appears to be some self-intersection, causing gIsValid to return false.
I've also tried unionSpatialPolygons (with avoidRGEOS=T) which creates a unified object
unionSpatialPolygons(polysData[1:2,],
IDs=names(polysData[1:2,]@polygons))
But the borders still overlap like in the above image, rather than being one smooth polygon outer border - i.e., the polygons are not dissolved together. Then using any further functions on this new SpatialPolygons object, I have the same problems.
So, is there a way to use polygons instead of going to rasters? It sure would make my life faster, which would be excellent. I feel like this must be a common problem with a wheel invented that I have yet to find. Thoughts?