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

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

  • 相关阅读:
    大文件上传
    zabbix接口
    Vue 在不同的环境使用不同的接口地址
    Vue发布流程
    RabbitMQ集群一些使用细节
    Watcher 系统整体流程图
    监控系统各个模块部署
    deepin安装node和npm最新
    google安装json插件
    数据库访问性能优化 Oracle
  • 原文地址:https://www.cnblogs.com/yif1991/p/5241663.html
Copyright © 2020-2023  润新知