Hello,
I have a very simple problem, which I know will have a simple solution,
but I just can't get my head wrapped around it.
I have a shapefile with regions, including an attribut containing a
regional id.
I would like to extract a simple neighbourhood file, with neighbourhood
defined by simple adjacency with symmetry of relationship in both
directions.
Ideally, I would like to get something like this:
id_region_1 id_neighbour_1
id_region_1 id_neighbour_2
id_region_1 id_neighbour_3
id_region_2 id_neighbour_1
id_region_2 id_neighbour_2
id_region_3 id_neighbour_1
etc
What I've done so far:
originalmap<-readShapePoly("/path/to/shapefile.shp")
nb<-poly2nb(originalmap, row.names=originalmap$idcolumn)
With nb[] I can see that the list of lists of neighbours and the list of
id's.
Here's where I'm stuck. How do I get a list as above with the row names
defined in the poly2nb call ?
Or as an intermediate solution: how do I replace the line id's in the
lists with the regional id's in the list of id's to at least get
something like this:
id_region_1 id_neighbour_1 id_neighbour_2 id_neighbour_3
id_region_2 id_neighbour_1 id_neighbour_2
id_region_3 id_neighbour_1 etc..
?
I imagine that this is a very simple operation, but I just can't seem to
find the way. Any pointers to doc is welcome.
Moritz
extracting simple list of adjacent neighbours
6 messages · Moritz Lennert, Roger Bivand
On Tue, 6 Sep 2011, Moritz Lennert wrote:
Hello,
I have a very simple problem, which I know will have a simple solution, but I
just can't get my head wrapped around it.
I have a shapefile with regions, including an attribut containing a regional
id.
I would like to extract a simple neighbourhood file, with neighbourhood
defined by simple adjacency with symmetry of relationship in both directions.
Ideally, I would like to get something like this:
id_region_1 id_neighbour_1
id_region_1 id_neighbour_2
id_region_1 id_neighbour_3
id_region_2 id_neighbour_1
id_region_2 id_neighbour_2
id_region_3 id_neighbour_1
etc
What I've done so far:
originalmap<-readShapePoly("/path/to/shapefile.shp")
nb<-poly2nb(originalmap, row.names=originalmap$idcolumn)
Roughly: lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) then the first two columns of the "spatial.neighbour" object sn (used in the S-PLUS SpatialStats module) are what you need. They can also be exported for use in Matlab. Hope this helps, Roger
With nb[] I can see that the list of lists of neighbours and the list of id's. Here's where I'm stuck. How do I get a list as above with the row names defined in the poly2nb call ? Or as an intermediate solution: how do I replace the line id's in the lists with the regional id's in the list of id's to at least get something like this: id_region_1 id_neighbour_1 id_neighbour_2 id_neighbour_3 id_region_2 id_neighbour_1 id_neighbour_2 id_region_3 id_neighbour_1 etc.. ? I imagine that this is a very simple operation, but I just can't seem to find the way. Any pointers to doc is welcome. Moritz
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
6 days later
On 06/09/11 21:41, Roger Bivand wrote:
On Tue, 6 Sep 2011, Moritz Lennert wrote:
Hello,
I have a very simple problem, which I know will have a simple
solution, but I just can't get my head wrapped around it.
I have a shapefile with regions, including an attribut containing a
regional id.
I would like to extract a simple neighbourhood file, with
neighbourhood defined by simple adjacency with symmetry of
relationship in both directions.
Ideally, I would like to get something like this:
id_region_1 id_neighbour_1
id_region_1 id_neighbour_2
id_region_1 id_neighbour_3
id_region_2 id_neighbour_1
id_region_2 id_neighbour_2
id_region_3 id_neighbour_1
etc
What I've done so far:
originalmap<-readShapePoly("/path/to/shapefile.shp")
nb<-poly2nb(originalmap, row.names=originalmap$idcolumn)
Roughly: lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) then the first two columns of the "spatial.neighbour" object sn (used in the S-PLUS SpatialStats module) are what you need. They can also be exported for use in Matlab.
This does give me the right format, but I don't get the ids in originalmap$idcolumn but rather the internal ids of the polygons. I've tried using the IDvar parameter in readShapePoly (IDvar="idcolumn"), but the result in sn is still the same. How do I get the sn result to contain the ids from the column I would like to use ? Moritz
On Tue, 13 Sep 2011, Moritz Lennert wrote:
On 06/09/11 21:41, Roger Bivand wrote:
On Tue, 6 Sep 2011, Moritz Lennert wrote:
Hello,
I have a very simple problem, which I know will have a simple
solution, but I just can't get my head wrapped around it.
I have a shapefile with regions, including an attribut containing a
regional id.
I would like to extract a simple neighbourhood file, with
neighbourhood defined by simple adjacency with symmetry of
relationship in both directions.
Ideally, I would like to get something like this:
id_region_1 id_neighbour_1
id_region_1 id_neighbour_2
id_region_1 id_neighbour_3
id_region_2 id_neighbour_1
id_region_2 id_neighbour_2
id_region_3 id_neighbour_1
etc
What I've done so far:
originalmap<-readShapePoly("/path/to/shapefile.shp")
nb<-poly2nb(originalmap, row.names=originalmap$idcolumn)
Roughly: lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) then the first two columns of the "spatial.neighbour" object sn (used in the S-PLUS SpatialStats module) are what you need. They can also be exported for use in Matlab.
This does give me the right format, but I don't get the ids in originalmap$idcolumn but rather the internal ids of the polygons.
example(columbus) nb <- poly2nb(columbus, row.names=as.character(columbus$NEIGNO)) lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) head(sn) sn1 <- cbind(attr(sn, "region.id")[sn$from], attr(sn, "region.id")[sn$to]) head(sn1) using the region.id attribute. Hope this helps, Roger
I've tried using the IDvar parameter in readShapePoly (IDvar="idcolumn"), but the result in sn is still the same. How do I get the sn result to contain the ids from the column I would like to use ? Moritz
Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
On 13/09/11 18:39, Roger Bivand wrote:
On Tue, 13 Sep 2011, Moritz Lennert wrote:
On 06/09/11 21:41, Roger Bivand wrote:
On Tue, 6 Sep 2011, Moritz Lennert wrote:
Hello,
I have a very simple problem, which I know will have a simple
solution, but I just can't get my head wrapped around it.
I have a shapefile with regions, including an attribut containing a
regional id.
I would like to extract a simple neighbourhood file, with
neighbourhood defined by simple adjacency with symmetry of
relationship in both directions.
Ideally, I would like to get something like this:
id_region_1 id_neighbour_1
id_region_1 id_neighbour_2
id_region_1 id_neighbour_3
id_region_2 id_neighbour_1
id_region_2 id_neighbour_2
id_region_3 id_neighbour_1
etc
What I've done so far:
originalmap<-readShapePoly("/path/to/shapefile.shp")
nb<-poly2nb(originalmap, row.names=originalmap$idcolumn)
Roughly: lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) then the first two columns of the "spatial.neighbour" object sn (used in the S-PLUS SpatialStats module) are what you need. They can also be exported for use in Matlab.
This does give me the right format, but I don't get the ids in originalmap$idcolumn but rather the internal ids of the polygons.
example(columbus) nb <- poly2nb(columbus, row.names=as.character(columbus$NEIGNO)) lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) head(sn) sn1 <- cbind(attr(sn, "region.id")[sn$from], attr(sn, "region.id")[sn$to]) head(sn1) using the region.id attribute. Hope this helps,
Yep, this gives me exactly what I was looking for. And I learned something new: never used attr() before. Is there no other way to access the region.id info in sn ? BTW: Is there any documentation about which attributes are available in these objects other than running attributes() ? Thanks in any case ! Moritz
On Wed, 14 Sep 2011, Moritz Lennert wrote:
On 13/09/11 18:39, Roger Bivand wrote:
On Tue, 13 Sep 2011, Moritz Lennert wrote:
On 06/09/11 21:41, Roger Bivand wrote:
On Tue, 6 Sep 2011, Moritz Lennert wrote:
Hello,
I have a very simple problem, which I know will have a simple
solution, but I just can't get my head wrapped around it.
I have a shapefile with regions, including an attribut containing a
regional id.
I would like to extract a simple neighbourhood file, with
neighbourhood defined by simple adjacency with symmetry of
relationship in both directions.
Ideally, I would like to get something like this:
id_region_1 id_neighbour_1
id_region_1 id_neighbour_2
id_region_1 id_neighbour_3
id_region_2 id_neighbour_1
id_region_2 id_neighbour_2
id_region_3 id_neighbour_1
etc
What I've done so far:
originalmap<-readShapePoly("/path/to/shapefile.shp")
nb<-poly2nb(originalmap, row.names=originalmap$idcolumn)
Roughly: lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) then the first two columns of the "spatial.neighbour" object sn (used in the S-PLUS SpatialStats module) are what you need. They can also be exported for use in Matlab.
This does give me the right format, but I don't get the ids in originalmap$idcolumn but rather the internal ids of the polygons.
example(columbus) nb <- poly2nb(columbus, row.names=as.character(columbus$NEIGNO)) lw <- nb2listw(nb, style="B") sn <- listw2sn(lw) head(sn) sn1 <- cbind(attr(sn, "region.id")[sn$from], attr(sn, "region.id")[sn$to]) head(sn1) using the region.id attribute. Hope this helps,
Yep, this gives me exactly what I was looking for. And I learned something new: never used attr() before. Is there no other way to access the region.id info in sn ?
No.
BTW: Is there any documentation about which attributes are available in these objects other than running attributes() ?
It isn't in the help page; most often attributes are only used internally. You can also run str() on an object to see whether any attributes are present, and if so, what they seem to be. Roger
Thanks in any case ! Moritz
Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no