1

I would like to add the overlapping data of a worldwide shapefile, to the different European nuts3 regions (polygon shapefile). However, there is something wrong with the script below as it does not work on the shapefiles that I am using.

### download the files available through this link:
https://drive.google.com/open?id=0By9u5m3kxn9yUy1xVDF2NV9TajA

#set your working directory
nuts <- readOGR(".", layer = "NUTS_RG_60M_2010")
grid <- readOGR(".", layer = "a3_mean_annual_runoff_1950_2000")


#projectstring?
#Do I have to do something here to make sure the projections are fine?

#then:
library(raster)
new_spdf <- intersect(grid,nuts) # (this normally works but it does not with these files?

grid_nuts <- gIntersects(new_spdf,nuts,byid = TRUE)
#Take all the values in a particular NUTS polygon, multiply by each the areas of the 
#corresponding new grid cells then divide by the total area of the NUTS polygon

#Added the if statement to avoid non-overlapping NUTS polygons

for(i in 1:length(nuts)){
  if(any(grid_nuts[i,])){
    nuts@data$average_spatial_value[i] <- mean(new_spdf@data$value[grid_nuts[i,]]*
                                                 gArea(new_spdf[grid_nuts[i,],])/
                                                 gArea(nuts[i,]))
  } else {
    nuts@data$average_spatial_value[i] <- NA
  }

}

***EDIT : I installed the latest version of R (see sessionInfo() below). The error message I receive is the following:

> new_spdf <- intersect(grid,nuts)
Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false,  : 
  TopologyException: Input geom 1 is invalid: Self-intersection at or near point 13.3081265 48.608032999999999 at 13.3081265 48.608032999999999
In addition: Warning message:
In intersect(grid, nuts) : non identical CRS


> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] rgeos_0.3-21 raster_2.5-8 rgdal_1.1-10 sp_1.2-3    

loaded via a namespace (and not attached):
[1] tools_3.3.1     Rcpp_0.12.7     grid_3.3.1      lattice_0.20-33
  • I have a completely different setup than yours - so this may not apply (R version 3.3.1, x86_64-apple-darwin13.4.0, OS X 10.11.6). However I also experienced that R crashed whenever I used the intersect() function. Updating to the newest version of rgeos (3-21) fixed that problem. – cengel Oct 31 '16 at 16:16
  • I installed the latest version of R. I now receive the following error message: > new_spdf <- intersect(grid,nuts) Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false, : TopologyException: Input geom 1 is invalid: Self-intersection at or near point 13.3081265 48.608032999999999 at 13.3081265 48.608032999999999 In addition: Warning message: In intersect(grid, nuts) : non identical CRS Not sure if this should be a separate question now. I'll edit my system info above. – user33125 Oct 31 '16 at 18:43
0

In the end, I asked a different question specifically to solve a self-intersection error. It turned out that that was the answer to the question. The new question (and therefore also the answer to this question) can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.