zoukankan      html  css  js  c++  java
  • SWM格式稀疏权重矩阵转换为方阵形式全过程分享

    在进行空间统计实验过程中,经常涉及到空间权重矩阵的处理,有时候需要将ArcGIS生成的swm格式的权重矩阵转换为形如“0 1”的方阵格式。这里将我的办法整理出来。

    1.用如下工具箱生成swm格式的权重矩阵

      

    2.将swm格式的权重矩阵转换为dbf属性表

        

    3.用excel打开dbf将其转换为txt文本文件

    4.写程序转换格式并保存

    代码如下:

     1 static void Main(string[] args)
     2         {
     3             //读取文件并转换格式
     4             StreamReader sr = File.OpenText("E:\AcaDissertation\Data\weight_arc.txt");
     5             double[,] weights = new double[47, 47];
     6             while (sr.ReadLine() != null)
     7             {
     8                 string[] line = sr.ReadLine().Split('	');
     9 
    10                 weights[int.Parse(line[0])-1, int.Parse(line[1])-1] = double.Parse(line[2]);
    11             }
    12             SaveMatrix(weights, "E:\AcaDissertation\Data\weight_mat.txt");
    13             Console.WriteLine("好了!");
    14         }
    15 
    16         // 保存矩阵到文件
    17         public static void SaveMatrix(double[,] InMatrix, string OutFileName)
    18         {
    19             int row = InMatrix.GetLength(0), col = InMatrix.GetLength(1);
    20             FileStream aFile = new FileStream(OutFileName, FileMode.OpenOrCreate);
    21             StreamWriter sw = new StreamWriter(aFile);
    22             for (int i = 0; i < row; i++)
    23             {
    24                 for (int j = 0; j < col; j++)
    25                 {
    26                     sw.Write("{0}{1}", InMatrix[i, j], " ");
    27                 }
    28                 sw.WriteLine();
    29             }
    30             sw.Close();
    31         }

    最后的最后,转换后的权重矩阵为:

  • 相关阅读:
    surfer插值方法及提取插值结果 转载
    Surfer的grd数据转换成gmt可用的grd数据方法
    Appium+Python3+ Android入门
    Flask入门的第一个项目
    测试报告模板
    火狐浏览器之伪造IP地址
    获取apk的签名信息
    初识kibana
    Fiddler模拟post四种请求数据
    Python-正则表达式
  • 原文地址:https://www.cnblogs.com/yif1991/p/5241663.html
Copyright © 2011-2022 走看看