I am trying to intersect two SpatialPolygonsDataFrames and get a SpatialPolygonsDataFrame as the result. Unfortunately, using the gIntersection function from rgeos (which works impressively quickly to intersect the polygons), I cannot seem to retrieve the associated dataframes. Consider the following example:
> fracPoly <- gIntersection( toSingle, fromSingle )
> class(toSingle)
[1] "SpatialPolygonsDataFrame"
> class(fromSingle)
[1] "SpatialPolygonsDataFrame"
> class(fracPoly)
[1] "SpatialPolygons"
I can write a wrapper function which handles the transfer of data.frames, but it will be a minor pain to get all the checking right and before I did I was hoping someone could either confirm that there's no better way or point me towards another function (or option for gIntersection) which would allow me to retain the associated data.frames.
Update
On further reflection, this may may be very deliberate behavior by gIntersection. After all, of the two SPDFs, whose data.frame do you pass along? So I may have to write a wrapper which merges the two.
Answer
For some project i had the same need. Much more than keeping the data.frame, we had to put in place further code to manage output geometry type, and proceed to some cleaning (e.g. clean geometry collections), to have some complete intersection geoprocess. In case you still need to do such a task in R, you can try the RFigisGeo package:
#install RFigisGeo
require(devtools)
install_github("RFigisGeo", "openfigis")
require(RFigisGeo)
#compute intersection
result <- getIntersection(features1, features2)
No comments:
Post a Comment