More

How to do union that results to a SpatialPolygonDataFrame in R?

How to do union that results to a SpatialPolygonDataFrame in R?


I have the following code

coastline<-readOGR(dsn=".",layer="shp") wetlands<-readOGR(dsn=".",layer="shp") wetb<-gBuffer(wetlands,width=500,byid=T) wetb2<-gBuffer(wetlands,width=1000,byid=T) u<-gUnion(wetb2,wetb) plot(u)

EDIT

u<-raster::union(wetb2,wetb) u2<-raster::union(coastline, u) plot(u2)

gives

Error in if (is.numeric(i) && i < 0) { : missing value where TRUE/FALSE needed In addition: Warning message: In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") : Self-intersection at or near point 699045.17841763003 4339815.8708455302

gUnion results to a SpatialPolygon but i need SpatialPolygonDataFrame. How can i do union that has that outcome?


All the g… functions seem to discard the data by design, and you have to fiddle them back into a spatial dataframe (Don't ask me how… )

Try using theunionfrom theraster package. Haven't tried union, but intersect etc. worked as expected, producing spatial dataframes.


Watch the video: Spatial Data in R