Skip to content
Prev 7912 / 29559 Next

Reading ArcGIS 9.3 spatial weight matrix [swm] file

Gindo,

You can use the Convert Spatial Weight Matrix to Table function in the arcmap toolbox. This results in a GAL/GWT text based file. Open the file in a text editor and remove the headings, and place a number representing the number of neighbours you have in as the header of the first column( I have no idea why arcgis does just output a true GAL/GWT file).

Then:
 
nb <-read.gwt2nb("<weight file from arc>.gwt",region.id=<objectid>)
listw <-nb2listw(nb, glist=attr(nb, "GeoDa")$dist)

see:

http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/spdep/html/read.gwt2nb.html
http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/spdep/html/nb2listw.html


----
Jay Douillard
Geospatial Technical Lead
604.827.4401
Human Early Learning Partnership 
www.earlylearning.ubc.ca


----- Original Message -----
From: "Gindo Tampubolon" <Gindo.Tampubolon at manchester.ac.uk>
To: "Roger Bivand" <Roger.Bivand at nhh.no>
Cc: r-sig-geo at stat.math.ethz.ch
Sent: Friday, March 19, 2010 1:54:29 AM GMT -08:00 US/Canada Pacific
Subject: Re: [R-sig-Geo] Reading ArcGIS 9.3 spatial weight matrix [swm] file

Thanks ever so much Roger.
Prompt and precise.

Gindo
-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Sent: 18 March 2010 19:20
To: Gindo Tampubolon
Cc: r-sig-geo at stat.math.ethz.ch
Subject: RE: [R-sig-Geo] Reading ArcGIS 9.3 spatial weight matrix [swm] file
On Thu, 18 Mar 2010, Gindo Tampubolon wrote:

            
The *.swm file is a binary file in an unknown format. The DBF file can be
read as follows:

library(foreign)
x <- read.dbf("spatialweightmatrix3.dbf")
from_IDs <- sort(unique(x$FID2))
to_IDs <- sort(unique(x$NID))
# find the IDs used
all.equal(from_IDs, to_IDs)
o <- order(x$FID2, x$NID)
# order the object
xx <- x[o,]
# convert manually to a spatial.neighbours representation
sn <- data.frame(from=match(xx$FID2, from_IDs), to=match(xx$NID+1,
   to_IDs), weihts=xx$WEIGHT)
attr(sn, "n") <- length(from_IDs)
attr(sn, "region.id") <- as.character(from_IDs)
class(sn) <- c("spatial.neighbour", class(sn))
library(spdep)
lw <- sn2listw(sn)
lw
summary(sapply(lw$weights, sum))
table(card(lw$neighbours))

There are only 1331 observations, so unconnected ones appear to be
dropped, and are probably the "holes" in attr(lw, "region.id") which
should span 0:1356, but is missing 25 values. The FID2 are probably the
FID of the polygon objects.

Hope this helps,

Roger
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo