Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. library(spdep)
  2.  
  3. countries<-c("CZE","DEU","AUT","SVK","POL")
  4. EUR<-getCountries(countries,level=1) # Level 1
  5. CZE<-subset(EUR,ISO=="CZE")
  6. plot(EUR)
  7.  
  8. # create neighbour structure with ID_1 as names
  9. # each item in this list contains id of all neighbouring polygons
  10. nb = poly2nb(pl=EUR, row.names=EUR$ID_1)
  11.  
  12. # plot nb structure, collection of lines and points
  13. plot(nb, coordinates(EUR), col=2, lty=2, add=T)
  14.  
  15. # get ID_1 names from nb items, stored in attributes
  16. nb_attr = attributes(nb)$region.id
  17.  
  18. # take only those nb items that belong to the CZE polygons
  19. nb1 = which(nb_attr %in% CZE$ID_1)
  20.  
  21. # convert the numeric id in the selected items to ID_1 format
  22. neighbour_ids = unique(nb_attr[unlist(nb[nb1])])
  23.  
  24. # select polygons in EUR dataset that correspond with the found ID_1's
  25. CZE_neighbours = subset(EUR,ID_1 %in% neighbour_ids)
  26.  
  27. # plot neighbours and CZE
  28. plot(CZE_neighbours, col=3, add=T)
  29. plot(CZE, col='steelblue4', add=T)
  30.  
  31. # add legend
  32. legend('topleft',fill=c('steelblue4','green'),legend=c('CZE','neighbours'))
  33.  
  34. # get relations between EUR and CZE polygons
  35. x<-gRelate(EUR, CZE, byid = TRUE)
  36.  
  37. # Select polygon ids which border at least 1 of the CZE polygons
  38. # ("FF2F11212" are bordering polygons)
  39. id_bordering = EUR$ID_1[apply(x,2,function(y) sum(which(y=="FF2F11212"))>=1)]
  40. poly_bordering = subset(EUR, ID_1 %in% id_bordering)
  41.  
  42. # plot results
  43. plot(EUR)
  44. plot(poly_bordering, add=T, col=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement