In the process of spatial statistical experiment, it is often involved in the processing of spatial weights matrix, sometimes it is necessary to convert the weight matrix of SWM format generated by ArcGIS into a square matrix form such as "0 1". Here is my way of sorting out.
1. Generate the SWM-formatted weights matrix using the following toolbox
2. Convert the weight matrix of the SWM format to the DBF attribute table
3. Open dbf in Excel to convert it to a txt text file
4. Write program conversion format and save
The code is as follows:
1 Static voidMain (string[] args)2 {3 //read the file and convert the format4StreamReader sr = File.OpenText ("E:\\acadissertation\\data\\weight_arc.txt");5 Double[,] weights =New Double[ -, -];6 while(Sr.) ReadLine ()! =NULL)7 {8 string[] line = Sr. ReadLine (). Split ('\ t');9 Tenweights[int. Parse (line[0])-1,int. Parse (line[1])-1] =Double. Parse (line[2]); One } ASavematrix (Weights,"E:\\acadissertation\\data\\weight_mat.txt"); -Console.WriteLine ("It's all right! "); - } the - //save Matrix to file - Public Static voidSavematrix (Double[,] Inmatrix,stringoutfilename) - { + introw = Inmatrix.getlength (0), col = Inmatrix.getlength (1); -FileStream Afile =NewFileStream (Outfilename, filemode.openorcreate); +StreamWriter SW =NewStreamWriter (afile); A for(inti =0; i < row; i++) at { - for(intj =0; J < Col; J + +) - { -Sw. Write ("{0}{1}", Inmatrix[i, J]," "); - } - SW. WriteLine (); in } - SW. Close (); to}
Finally, the converted weight matrix is:
SWM format weights matrix conversion to binary format all-in-process sharing