Any way to import a self defined neighborhood matrix?

602
2
08-25-2011 10:38 AM
XiaominRuan
New Contributor
Hi, I created a self-defined neighborhood matrix in SAS. So now the matrix has each row attached to a UID and each column with a special variable name. I wonder if there is a way to read in this matrix and further do spatial auto-correlation analysis. Which function or toolbox should I use?

I tried to use some other open source software to import it but failed as well.

Any help is appreciated. Thank you!


Xiaomin Ruan

Geospatial and Population Studies, University of New Mexico
0 Kudos
2 Replies
XiaominRuan
New Contributor
Dear all,

Sorry to keep bothering with this, but I don't know where else to go with these questions. Perhaps I should make my question more specific. I read about how ArcGIS construct an optimized weight matrix like the example below (saw this from archived forum link).
http://forums.esri.com/Thread.asp?c=93&f=1528&t=257453&ESRISessionID=lmlYQEgTGcEloQtIpmMhyO5uYBy1MWw...

StationID
1 1 0
1 2 1/10
1 3 1/7
2 1 1/10
2 3 1/20
3 1 1/6
3 2 1/15
3 3 0

But what I created is a nXn matrix standardized by row, in tab deliminated text format, with the unique ID at the first column, like the example below.

IDName
1  0  0.5  0.5  0  0  0  0  0  0
2  0.5  0  0.5  0  0  0  0  0  0
3  0.5  0.5  0  0  0  0  0  0  0
4  0  0  0  0  0.2  0.2  0.2  0.2  0.2
5  0  0  0  0.2  0  0.2  0.2  0.2  0.2
6  0  0  0  0.2  0.2  0  0.2  0.2  0.2
7  0  0  0  0.2  0.2  0.2  0  0.2  0.2
8  0  0  0  0.2  0.2  0.2  0.2  0  0.2
9  0  0  0  0.2  0.2  0.2  0.2  0.2  0

My Python script is also attached for further diagnostics.

Any help appreciated! 

Xiaomin
0 Kudos
MarkJanikas
New Contributor III
Here is some code that places your NbyN matrix into a GWT sparse format....

import locale as LOCALE

fo = open("weights.txt", "r")
fw = open("weights.gwt", "w")
header = fo.readline()
fw.write("%s" % header)
data = fo.readlines()
for row in data:
    rowVals = row.split()
    rowID = rowVals[0]
    weights = rowVals[1:]
    for colID, weight in enumerate(weights):
        w = LOCALE.atof(weight)
        if w != 0.0:
            fw.write("%s %i %s\n" % (rowID, colID+1, weight))

fo.close()
fw.close()


I put your format into "weights.txt" and get out "weights.gwt".  This will allow you to use it directly in most of our tools.  However, if you want to construct a SWM format... it is perhaps best to change the header line to:

fw.write("%s %s %s\n" % (header.strip(), "NID", "WEIGHT"))


Then open the output file in excel and save in DBF format.  Lastly, call the "Generate Spatial Weights Matrix" tool in ArcGIS and use the "Convert from Table" conceptualization in order to give you the binary SWM format. 

There are many ways to skin this cat and I really like that you are attempting to use PySAL for it.  Unfortunately, I have been to busy with release to get to working with conversions in the latest and greatest PySAL.  Perhaps Ill wrap some code into my WeightsUtilities to convert full NumPy arrays to SWM formats?....
0 Kudos