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)
wrld_simpl
is/are causing this by cropping subsets - eg ifout <- 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).