1# Copyright 2001-6 by Nicholas Lewin-Koh and Roger S. Bivand.
2#
3
4
5graph2nb <- function(gob, row.names=NULL,sym=FALSE) {
6	if (!inherits(gob, "Graph")) stop("Not a Graph object")
7	res <- vector(mode="list", length=gob$np)
8    	if (!is.null(row.names)) {
9		if(length(row.names) != gob$np)
10            		stop("row.names wrong length")
11		if (length(unique(row.names)) != length(row.names))
12	    		stop("non-unique row.names given")
13    	}
14	if (gob$np < 1) stop("non-positive gob$np")
15    	if (is.null(row.names)) row.names <- as.character(1:gob$np)
16        if(sym){
17          for (i in 1:gob$np) {
18		res[[i]] <- sort(unique(c(gob$to[gob$from==i],
19                                       gob$from[gob$to==i])))
20	  	if(length(res[[i]]) == 0L) res[[i]] <- 0L
21	  }
22        }
23        else{
24	  for (i in 1:gob$np) {
25		res[[i]] <- sort(gob$to[gob$from==i])
26	  	if(length(res[[i]]) == 0L) res[[i]] <- 0L
27	  }
28        }
29        attr(res, "region.id") <- row.names
30 	attr(res, "call") <- attr(gob, "call")
31 	attr(res, "type") <- attr(gob, "type")
32	class(res) <- "nb"
33	res <- sym.attr.nb(res)
34	res
35}
36