Skip to content
Prev 22101 / 29559 Next

gIntersection fails

I have no idea to generate the scale programmatically. 
Well, the EuroStat dataset goes up to French Guiana to the west, then to
Reunion Island to the South etc. It is deprived of any index to identify
those overseas regions.
Either we create a new index of we clip the area to be plotted. In both
cases we have to manually retrieve the extreme points of this area. In the
present case, one can start from "Cabo de Roca" in Portugal etc. A much
simple solution might be to exclude France, Spain and Portugal from the EU
and plot the remaining countries...

I slightly changed the script so that the outcome sp object is now of class
SpatialPolygonsDataFrame.

I couldn't find out Andorra in the Eurostat dataset. No NUTS_ID starting
with AD.


library(rgdal)
library(RColorBrewer)
library(raster)
library(rgeos)
s <- readOGR('EUmaps/NUTS_2010_10M_SH/data',layer="NUTS_RG_10M_2010")
s1 <- s[s$STAT_LEVL_==2 & substring(s$NUTS_ID,1,2) %in%
c("BE","CH","DE","DK","ES","IE","FR","IT","LU","NL","PT","UK"),]
#  ETRS89 / ETRS-LAEA
# as in: http://spatialreference.org/ref/epsg/3035/proj4/
proj.laea <- CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000
+ellps=GRS80 +units=m +no_defs")
s1.laea <- spTransform(s1,proj.laea)
summary(s1.laea)
#clip.extent <- as(extent(2.5e6, +5e6, 1.5e6, 4.1e6), "SpatialPolygons")
# proj4string(clip.extent) <- CRS(proj4string(s1.laea))
clip.extent <- as(extent(-10, 19, 35, 59), "SpatialPolygons")
proj4string(clip.extent) <- CRS(proj4string(s1))
clip.extent <- spTransform(clip.extent,proj.laea)
summary(clip.extent)
EuropeW <- gIntersection(s1.laea, clip.extent, byid=c(TRUE, FALSE))
# side effect: the data frale is removed
# You may need a SpatialPolygonDataFrame for future use
EuropeW <- s1.laea[sapply(slot(s1.laea,"polygons"),slot,"ID") %in%
sapply(slot(EuropeW,"polygons"),slot,"ID"),]
EuropeW$NUTS_ID <- EuropeW$NUTS_ID[drop=T]
plot(EuropeW,col=brewer.pal(10,"Set3"))
plot(EuropeW,col=brewer.pal(12,"Set3")[as.factor(substring(EuropeWdf$NUTS_ID
,1,2))])

-----Message d'origine-----
De?: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Envoy??: mercredi 17 d?cembre 2014 13:19
??: Philippe Liege
Cc?: r-sig-geo at r-project.org
Objet?: RE: [R-sig-Geo] gIntersection fails
On Wed, 17 Dec 2014, Philippe Liege wrote:

            
OK, thanks for this. The resolution is either as you chose, or:

ce_ll <- spTransform(clip.extent, CRS(proj4string(s1))) oS <- getScale() oS
# [1] 1e+08 EuropeW <- gIntersection(s1, ce_ll, byid=c(TRUE, FALSE)) # Error
in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, #
"rgeos_intersection") :
#   TopologyException: Input geom 0 is invalid: Self-intersection at or 
# near point 10.648729489383225 57.7454124997018 at 10.648729489383225 #
57.7454124997018
setScale(1e+9)
EuropeW <- gIntersection(s1, ce_ll, byid=c(TRUE, FALSE)) # OK
setScale(oS)

One has to find the appropriate scale manually in such cases, curiously
setScale(1e+7) also works, but for geographical coordinates one probably
wants to increase rather than decrease.

Roger

(I'm assuming that NL hasn't been flooded?)
particular, the:
<-
--
Roger Bivand
Department of Economics, Norwegian School of Economics, Helleveien 30,
N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no