[R-sig-Geo] Reading ArcGIS 9.3 spatial weight matrix [swm] file

Jay Douillard jay.douillard at ubc.ca
Thu Mar 25 21:16:29 CET 2010


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:

> Dear Roger and all,
>
> Sorry for not putting a sample. Yes, Windows seems to be overloaded.
> http://sites.google.com/site/tehtareknow/home/spatial-weight-matrix/

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


>
> Thanks for the tip and any more help much appreciated,
> Gindo
>
> -----Original Message-----
> From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
> Sent: 18 March 2010 14:23
> 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:
>
>> Dear all,
>>
>> Can anyone tell me how to read ArcGIS 9.3 generated .swm file [spatial
>> weight matrix] into R?
>
> Well, make a number of small samples available on a website - most people
> do not have access to Arc, so helping is hard. Is there an ESRI site with
> shapefiles and matching sample *.swm files?
>
>>
>> I am stumped at page 242 of the ASDAR book [Bivand et al 2008]. I use a
>> SpatialPolygonsDataFrame object and tried to generate neighbours list
>> using poly2nb(). It took more than 24 hours [European data NUTS level 3
>> about 1500 regions]. After finishing, the result is a null set.
>>
>
> Always try a subset first. Most likely you need the snap= argument, if the
> boundaries do not touch but are close to each other. Almost certainly your
> input map also had far too much detail in shorelines and river borders,
> leading to your machine getting overloaded (Windows?). Displaying on such
> a map would also be dominated by line detail.
>
>> So I generate a rook weight matrix in ArcGIS 9.3 [and convert it to a
>> table there]. How do I import this matrix to R?
>
> Contribute by making sample files available. To read a matrix if you must,
> use read.table().
>
> Roger
>
>>
>>
>> I looked at RArcInfo and rgdal packages but could not find anything
>> obvious; I'm experimenting with read.gwt2nb() but ArcGIS created swm
>> based on numeric ID instead of NUTS_CODE alphanumeric; needs tweaking
>> perhaps.
>>
>> Many thanks
>>
>> Gindo
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> --
> 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
>
>
>

--
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



More information about the R-sig-Geo mailing list