Skip to content
Prev 22095 / 29559 Next

gIntersection fails

Please never post HTML, only plain text.

Always include a link to data, but not this data, as this requires 
acceptance of conditions to download, so is not automatic.

Note that the SO posting you quote was from April 2013 - it is always 
important to give your sessionInfo() output, as how things work changes 
over time, leaving legacy advice stale. Your results might match the SO 
ones if your platform is the same as that was then. In particular, the:

http://cran.r-project.org/web/packages/rgeos/ChangeLog

entry for 2014-02-08 indicates that non-polygon output from polygon 
intersections is now handled more gracefully.

The underlying problem may be that these coordinates are geographical, not 
projected, and the integer discretisation used by GEOS is failing for some 
input - have you tried projecting to planar coordinates in a different 
metric and doing the same operation? Or using setScale()?

For my 20M 2006:

library(rgdal)
s <- readOGR('.', layer="NUTS_RG_20M_2006")
s1 <- s[s$STAT_LEVL_==2 & substring(s$NUTS_ID,1,2) %in% 
c("FR","BE","ES","IT","PT","DE","CH","UK","IE","DK","AT"),]
library(raster)
clip.extent <- as(extent(-10, 19, 36, 51), "SpatialPolygons")
proj4string(clip.extent) <- CRS(proj4string(s))
EuropeW <- gIntersection(s1, clip.extent, byid=c(TRUE, FALSE))
plot(EuropeW)

just works.

Hope this clarifies,

Roger

PS:
R version 3.1.2 (2014-10-31)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C
  [3] LC_TIME=en_US.utf8        LC_COLLATE=en_US.utf8
  [5] LC_MONETARY=en_US.utf8    LC_MESSAGES=en_US.utf8
  [7] LC_PAPER=en_US.utf8       LC_NAME=C
  [9] LC_ADDRESS=C              LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rgeos_0.3-8   raster_2.3-12 rgdal_0.9-1   sp_1.0-16

loaded via a namespace (and not attached):
[1] grid_3.1.2      lattice_0.20-29
[1] "3.4.2-CAPI-1.8.2 r3921"
On Tue, 16 Dec 2014, Philippe Liege wrote: