0

This code was working for me before I updated R to 4.0, but now it throws an error. I get this error on a Mac and a PC. I updated gdal on my mac, but this did not help. I made sure the rgeos package is up to date. It does not seem to be related to the extent I choose (i.e. extent(-180, 180, 50, 90) throws the same error).

data("wrld_simpl", package = "maptools") 
out <- crop(wrld_simpl, extent(130, 180, 40, 70))

x[i, ] is invalid
Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false,  : 
  TopologyException: Input geom 0 is invalid: Ring Self-intersection at or near point 143.66192817999999 49.312211990000002 at 143.66192817999999 49.312211990000002
In addition: Warning messages:
1: In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") :
  Ring Self-intersection at or near point 143.66192817999999 49.312211990000002
2: In rgeos::gIntersection(x[i, ], y, byid = TRUE, drop_lower_td = TRUE) :
  Invalid objects found; consider using set_RGEOS_CheckValidity(2L)
1
  • You could see which feature in wrld_simpl is/are causing this by cropping subsets - eg if out <- crop(wrld_simpl[1:123,], extent(130, 180, 40, 70)) fails you know there's something bad in the first 123 rows. If that works then the bad polygon is in the second 123 rows (I see 246 total rows).
    – Spacedman
    Commented Nov 19, 2020 at 23:07

1 Answer 1

3

This is because of changes in the rgeos package. The problem appears to go away if you do as the error message says: consider using set_RGEOS_CheckValidity(2L)

library(raster)
library(maptools)
data("wrld_simpl") 
out <- crop(wrld_simpl, extent(130, 180, 40, 70))

#x[i, ] is invalid
#Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false,  : 
#  TopologyException: Input geom 0 is invalid: Ring Self-intersection at or near point 143.66192817999999 49.312211990000002 at 143.66192817999999 49.312211990000002
#In addition: Warning messages:
#1: In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") :
#  Ring Self-intersection at or near point 143.66192817999999 49.312211990000002
#2: In rgeos::gIntersection(x[i, ], y, byid = TRUE, drop_lower_td = TRUE) :
#  Invalid objects found; consider using set_RGEOS_CheckValidity(2L)

Now change the rgeos setting and try again

rgeos::set_RGEOS_CheckValidity(2L)
out <- crop(wrld_simpl, extent(130, 180, 40, 70))

#x[i, ] is invalid
#Attempting to make x[i, ] valid by zero-width buffering
#Warning message:
#In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") :
#  Ring Self-intersection at or near point 143.66192817999999 49.312211990000002

Still a lot of messages, but not an error.

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.