5

I have data in shapefile format :: UK_boundaries I am new to R and Spatial data as well, I am trying to join some areas to be as one area no line(s) between them. Based on my search I understand that unionSpatialPolygons function dissolves polygons but when I tried to use it I got error: Error: TopologyException: Input geom 0 is invalid: Self-intersection at or near point 162544.73551388 516922.55880385998 at 162544.73551388 516922.55880385998

libs <- c("rgdal", "maptools", "gridExtra", "rgeos", "raster")
lapply(libs, require, character.only = TRUE)

    LA <- readOGR(dsn = ".", layer = "infuse_dist_lyr_2011_clipped")
    IDs <- LA$geo_code

IDs[IDs %in% c("95DD","95II","95QQ","95EE","95RR","95UU","95TT","95CC","95BB")] <- c("95AA","95GG","95HH","95JJ","95JJ","95JJ","95KK","95OO","95XX")

LA_new <- unionSpatialPolygons(LA, IDs)

In summary I want areas of the same colour shown here to be joined.

NIR_Geos

Any help please?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Iman
  • 63
  • 2
  • 5

3 Answers3

3

I can reproduce your error. A current workaround is to use gpclib (free for non-commercial use) instead of rgeos:

# install.packages("gpclib")
library("gpclib")
gpclibPermit()
# [1] TRUE
# Warning message:
# In gpclibPermit() :
#  support for gpclib will be withdrawn from maptools at the next major release

LA_new <- unionSpatialPolygons(LA, IDs, avoidGEOS=TRUE)
rcs
  • 3,894
  • 1
  • 27
  • 30
  • That is very interesting. Do you know why/how gpclib avoids this error. I get that same error in Postgis (also from GEOS, obviously), quite often on large union jobs. There are workarounds, but it is annoying. – John Powell Feb 13 '15 at 09:45
  • gpclibPermit() returns a warning message saying "support for gpclib will be withdrawn from maptools at the next major release" - seems like it won't be much longer before this very nice workaround is doomed to fail... – fdetsch Feb 13 '15 at 09:57
  • @rcs Thanks for you effort, I could not install "gpclib", I get Installing package into ‘\hudson/jpdg26/My_Documents/R/win-library/3.0’ (as ‘lib’ is unspecified)

    package ‘gpclib’ is available as a source package but not as a binary

    Warning in install.packages : package ‘gpclib’ is not available (for R version 3.0.2)

    although according to its description it should be installed for R( > 2.14.0)gpclib

    – Iman Feb 13 '15 at 10:32
  • okay, you are using windows :/ See this http://cran.r-project.org/bin/windows/contrib/r-release/ReadMe – rcs Feb 13 '15 at 10:46
  • @rcs I try to force gpclib works in R but I could not! Is there any other way to dissolve polygons rather than using gpclib? – Iman Feb 18 '15 at 12:01
1

Looks like I'm a year late but I still want to post the answer so maybe it can help others later.

The error message is self-explanatory -- there's topology error (Self-intersection) in your shapefile. You can use gIsValid() function in rgeos to reproduce the error and detect any other topology error in your shapefile.

As for the fix, I would suggest to remove the topology error by editing shapefile using QGIS or ArcMap.

fengly20
  • 11
  • 1
0

An Interesting way to avoid/fix TopologyException errors was proposed by @hrbrmstr here: forcing a zero-width buffer. So try this before applying unionSpatialPolygons. In the given example of @Iman, maybe something like: (I didn't test it)

LA_2 <- gBuffer(LA, byid=TRUE, width=0)
LA_new <- unionSpatialPolygons(LA_2, IDs)
Valentin_Ștefan
  • 397
  • 2
  • 16