1

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?

gsa
  • 1,246
  • 6
  • 19
  • 29

1 Answers1

1

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 the union from the raster package. Haven't tried union, but intersect etc. worked as expected, producing spatial dataframes.

Bernd V.
  • 3,179
  • 25
  • 49
  • Functions from the raster package cannot be applied to vectors. – gsa Jul 21 '15 at 12:57
  • 1
    Have you even tried ??? See my older answer on an intersect problem: http://gis.stackexchange.com/questions/35060/rgeos-drops-associated-values-when-intersecting-polygons/128088#128088 – Bernd V. Jul 21 '15 at 13:00
  • Can you tell me how to use the union from the raster package since if i write union it will use the base function. – gsa Jul 21 '15 at 13:07
  • you can use (generically) package::function to access a particular function in a package – Ellis Valentiner Jul 21 '15 at 13:11
  • Have you loaded the raster library? If yes, try raster::union See:http://stackoverflow.com/questions/5564564/r-2-functions-with-the-same-name-in-2-different-packages – Bernd V. Jul 21 '15 at 13:12
  • it says 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 – gsa Jul 21 '15 at 13:20
  • Hard to tell from the distance, but for me that sounds like that you have topological errors in one or both layers. Did you produce them within R or are they just imported? If imported, check for errors in GIS. Or try using a tiny buffer (1cm or so) before union, maybe this will resolve the problem. – Bernd V. Jul 21 '15 at 13:27
  • Actually i used it two times. The first worked well but the second returned this error.i will edit my question so you can understand. – gsa Jul 21 '15 at 13:31
  • h, just have to read your question. Imported, and already applies a buffer ... mhmm. No idea. Am not the "real" R guru ;) – Bernd V. Jul 21 '15 at 13:32