Friday 11 January 2019

arcgis desktop - How to convert GeoDa .GAL or ArcMap .SWM file to Matlab (.dat)?


I want to convert a spatial weight matrix from either ArcMap (.SWM file) or GeoDa (.GAL file) to an .dat Matlab file.


So far, I only found the STATA package spwmatrix to do this. But the command line . spwmatrix import using C:\data\wcontig.gal, wname(wcontig) xport(wcontig, dat)


only results in an error "File wcontig.dat cannot be opened" .


Does anybody know how to fix this or another way to do this?




Answer



I authored the SWM file format for Esri. All the methods are of course within ArcGIS to read/write/convert etc... but the format is relatively simple and can be read w/o any license whatsoever. The file is written in little endian... with the first line being the header written in text. You should open it with the "rb" option... Here is how to read the header and the info needed to scan the rest:


    import numpy as NUM
fo = open(swmFile, "rb")
header = fo.readline().strip().split(";")
numObs, rowStandard = NUM.fromfile(fo, '

Unfortunately, there are actually two versions of the SWM... one where all the weights are stored even when they are identical [old version], and a newer version that is smart enough to avoid this.


    if header[0][0:8] == "VERSION@":
swmType = "new"

else:
swmType = "old"

If it is a new version then you still need to assess whether the weights are of variable type... i.e. each neighbor for a record can have a different weight [e.g. Inverse Distance].


    fixed = False
if swmType == "new":
for headerVal in header:
headerInfo = headerVal.split("@")
if headerInfo[0] == "FIXEDWEIGHTS":
fixed = headerInfo[-1].upper() == "TRUE"


Now, reading is based on whether it is fixed or not...


    if fixed:
for i in xrange(numObs):
masterID, nn = NUM.fromfile(fo, ' if nn != 0:
nhs = NUM.fromfile(self.fo, ' weight = NUM.fromfile(self.fo, ' weights = NUM.ones(nn) * weight
sumUnstandard = NUM.fromfile(self.fo, '
else:
for i in xrange(numObs):
masterID, nn = NUM.fromfile(fo, ' if nn != 0:
nhs = NUM.fromfile(self.fo, ' weights = NUM.fromfile(self.fo, ' sumUnstandard = NUM.fromfile(self.fo, '
fo.close()


I hope this helps.


MJ


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...