Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
1  
How are you loading your polgyons - readShape* or readOGR? I get weird behaviour with gIntersection depending on which library I've used to load SPDFs, which I've not gotten to the bottom of. –  Simbamangu Oct 6 '12 at 7:27
    
@Simbamangu Interesting. I'm using readShapePoly and then merging in a data.frame.... –  Ari B. Friedman Oct 6 '12 at 10:01
    
gIntersection SHOULD give the data.frames merged for the overlapping areas - if I run Vector|Geoprocessing|Intersect in QGIS, the output is a set of merged attributes for the overlap, and doesn't QGIS use the same GEOS library for spatial operations like this? –  Simbamangu Oct 15 '12 at 7:30
    
Well if it should then I'm stumped. I already rewrote my code to merge the data.frames by hand and munge them back in, so I'm not going to spend any more time on this for now. But the hint that the function reading in the shapefile matters is helpful. –  Ari B. Friedman Oct 15 '12 at 13:55
1  
This clearly is not an answer, but I don't have enough points to leave a comment... Ari, I was wondering if you would share your chunk of code to extract the variables following rgeos overlay functions. I am having trouble wrapping my head around a good way to retain the original polygon id's from various rgeos operations such as gUnion... –  jed.a.long Feb 28 '13 at 21:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.