[GIS] gUnaryUnion from rgeos packge fails randomly

geosrrgeos

I am using gUnaryUnion() function from the rgeos package to "melt" administrative regions into bigger units.

At times I do get the expected result, and other times (seemingly randomly, so I suspect a memory issue) it fails – the most common error message is :

Error in SpatialPolygons(pl, proj4string = CRS(proj4string(dots[[1]]))):  
    cannot get a slot ("coords") from an object of type "NULL"

The intriguing thing is that at other times it runs perfectly. I am positively certain that the object I run the Union on has CRS set. It is a sp object with size in the tens of megabytes, so not huge but sizeable.

Is there a way to work around this? Assign more memory to Geos for example? Tune some parameters?

An example of my problem is

library(RCzechia) # set of shapefiles for the Czech Republic - devtools::install_github("jlacko/RCzechia")
library(rgeos)
set_RGEOS_polyThreshold(1/1000)
set_RGEOS_dropSlivers(TRUE)
okresy <- RCzechia::okresy 
nuts <- okresy@data$KOD_CZNUTS3
kraje <- gUnaryUnion(okresy, nuts) # this fails!!

My session info is

R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.3 LTS

rgeos version: 0.3-26, (SVN revision 560)
 GEOS runtime version: 3.5.1-CAPI-1.9.1 r4246 
 Linking to sp version: 1.2-5 
 Polygon checking: TRUE 

Best Answer

Works for me on windows, with some warnings about minuscule polygons. The easier way to do this (because it keeps the attributes) is:

library(raster)
a <- aggregate(okresy, 'KOD_CZNUTS3')

But that should not help with your problem, as it uses rgeos under the hood.

This is my rgeos:

library(rgeos)
rgeos version: 0.3-25, (SVN revision 555)
 GEOS runtime version: 3.6.1-CAPI-1.10.1 r0 
 Linking to sp version: 1.2-5 
 Polygon checking: TRUE 
Related Question