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")
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 

2 Answers 2


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

a <- aggregate(okresy, 'KOD_CZNUTS3')

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

This is my 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 
  • You nailed my biggest headache: the code indeed works on my Windows laptop, and so I am fairly certain it is not utterly wrong, but fails on my main server (where I need it to run in sort of production mode). Very annoying... :( Commented Dec 11, 2017 at 17:55
  • any chance you can update to a newer (r)geos on the server? Commented Dec 11, 2017 at 18:20
  • I will try. I have the geos library that came with the regular Ubuntu package, slightly out of date but not hopelessly so. About one minor version off. The rgeos package is the latest. Commented Dec 11, 2017 at 19:16

by chance I am working also on Czech NUTS data, and when I tried to perform the same action (aggregate) I got, sometimes, the same error. Yes, the error is not always occurring and this is particularly annoying. However, I had the impression(!) that this could be due to the island polygon of NUTS region CZ02 (Prague), but happens in deed also for NUTS region CZ07. I do get sometimes error message:"Error in rgeos::gUnaryUnion(x) : comm2comment: buffer overflow" and sometimes "Error in SpatialPolygons(pl, proj4string = CRS(proj4string(dots[[1]]))) : cannot get a slot ("coords") from an object of type "NULL"" Did not find out yet the real reason

  • The Czech Republic is a complicated place... I plan to transfer the RCzechia package from sp to sf package format soon, and it should get easier. Commented Dec 18, 2017 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.