【原创】C#写的水准网平差程序

Matrix类详见http://hi.baidu.com/aragon2/blog/item/1edbba124ab4beecc2ce796e.html?timeStamp=1311046817866

一个基本的水准平差控制台程序。

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.IO;
   6:   
   7:  namespace Adjustment
   8:  {
   9:      public class LPointClass
  10:      {
  11:          public string PID { get; set; }
  12:          public double H { get; set; }
  13:          public bool IsControlP { get; set; }
  14:      }
  15:   
  16:      public class LineClass
  17:      {
  18:          public int CZCount { get; set; }
  19:          public LPointClass SP { get; set; }
  20:          public LPointClass EP { get; set; }
  21:          public double dH { get; set; }
  22:          public double Distance { get; set; }
  23:      }
  24:   
  25:      public class AdjustedPart
  26:      {
  27:          List<LPointClass> AdjustedPoints = new List<LPointClass>();
  28:          public Matrix B { get; set; }
  29:          public Matrix Q { get; set; }
  30:      }
  31:   
  32:      public class AdjustClass
  33:      {
  34:          public Matrix B { get; set; }
  35:          public Matrix P { get; set; }
  36:          public Matrix l { get; set; }
  37:          public Matrix Q { get; set; }
  38:          public Matrix x { get; set; }
  39:          public Matrix V { get; set; }
  40:   
  41:          public void LevelAdjust(List<LineClass> CurrentSegments, List<LPointClass> CurrentPoints, List<LPointClass> ControlPoints)
  42:          {
  43:              int n = CurrentSegments.Count;
  44:              int t = CurrentPoints.Count - ControlPoints.Count;
  45:              B = new Matrix(n, t);
  46:              P = new Matrix(n, n);
  47:              l = new Matrix(n, 1);
  48:              List<LPointClass> TPoints = new List<LPointClass>();
  49:              foreach (LPointClass Tp in CurrentPoints)
  50:              {
  51:                  if (Tp.IsControlP == false)
  52:                      TPoints.Add(Tp);
  53:              }
  54:              for (int k = 0; k < n; k++)
  55:              { 
  56:                  int i = Belong(TPoints, CurrentSegments[k].SP.PID);
  57:                  int j = Belong(TPoints, CurrentSegments[k].EP.PID);
  58:                  P[k, k] = 1.0 / CurrentSegments[k].Distance;
  59:                  l[k, 0] = CurrentSegments[k].dH - (CurrentSegments[k].EP.H - CurrentSegments[k].SP.H);
  60:                  if (CurrentSegments[k].SP.IsControlP == false)
  61:                      B[k, i] = -1;
  62:                  if (CurrentSegments[k].EP.IsControlP == false)
  63:                      B[k, j] = 1;
  64:              }
  65:              Q = (B.Transpose() * P * B).Inverse();
  66:              x = Q * (B.Transpose() * P * l);
  67:              V = B * x - l;
  68:          }
  69:   
  70:          private static int Belong(List<LPointClass> CurrentPoints, string s)
  71:          {
  72:              foreach (LPointClass TPoint in CurrentPoints)
  73:              {
  74:                  if (TPoint.PID == s)
  75:                      return CurrentPoints.IndexOf(TPoint);
  76:              }
  77:              return -1;
  78:          }
  79:      }    
  80:   
  81:      class Program
  82:      {
  83:          public static List<LineClass> CurrentSegments = new List<LineClass>();
  84:          public static List<LPointClass> ControlPoints = new List<LPointClass>();
  85:          public static List<LPointClass> CurrentPoints = new List<LPointClass>();
  86:          public static AdjustClass Adjust = new AdjustClass();
  87:   
  88:          private static void InputData(List<LPointClass> CurrentPoints, List<LineClass> CurrentSegments)
  89:          {
  90:              StreamReader MyReader = new StreamReader("./Data/Control.txt");
  91:              while (MyReader.EndOfStream != true)
  92:              {
  93:                  List<string> TempString = MyReader.ReadLine().Split(' ').ToList<string>();
  94:                  TempString.RemoveAll(IsSpace);
  95:   
  96:                  ControlPoints.Add(new LPointClass
  97:                  {
  98:                      PID = TempString[0],
  99:                      H = double.Parse(TempString[1]),
 100:                      IsControlP =true
 101:                  });
 102:              }
 103:              MyReader.Close();                                                           //输入控制点信息
 104:   
 105:              MyReader = new StreamReader("./Data/Raw.txt");
 106:              int n = 0;
 107:              while (MyReader.EndOfStream != true)
 108:              {
 109:                  n++;
 110:                  List<string> TempString = MyReader.ReadLine().Split(' ').ToList<string>();
 111:                  TempString.RemoveAll(IsSpace);
 112:   
 113:                  CurrentSegments.Add(new LineClass
 114:                  {
 115:                      SP = new LPointClass { PID = TempString[0] },
 116:                      EP = new LPointClass { PID = TempString[1] },
 117:                      dH = double.Parse(TempString[2]),
 118:                      Distance = double.Parse(TempString[3]),
 119:                      CZCount = n
 120:                  });
 121:              }
 122:              MyReader.Close();                                                           //输入观测值信息
 123:   
 124:              foreach (LineClass TLine in CurrentSegments)
 125:              {
 126:                  if (Belong(CurrentPoints, TLine.SP.PID) == -1)
 127:                  {
 128:                      int Index = Belong(ControlPoints, TLine.SP.PID);
 129:                      if (Index == -1)
 130:                      {
 131:                          CurrentPoints.Add(new LPointClass
 132:                          {
 133:                              PID = TLine.SP.PID,
 134:                              H = -99999.99,
 135:                              IsControlP = false
 136:                          });
 137:                          TLine.SP.H = -99999.99;
 138:                          TLine.SP.IsControlP = false;
 139:                      }
 140:                      else
 141:                      {
 142:                          CurrentPoints.Add(ControlPoints[Index]);
 143:                          TLine.SP = ControlPoints[Index];
 144:                      }
 145:                  }
 146:                  if (Belong(CurrentPoints, TLine.EP.PID) == -1)
 147:                  {
 148:                      int Index = Belong(ControlPoints, TLine.EP.PID);
 149:                      if (Index == -1)
 150:                      {
 151:                          CurrentPoints.Add(new LPointClass
 152:                          {
 153:                              PID = TLine.EP.PID,
 154:                              H = -99999.99,
 155:                              IsControlP = false
 156:                          });
 157:                          TLine.EP.H = -99999.99;
 158:                          TLine.EP.IsControlP = false;
 159:                      }
 160:                      else
 161:                      {
 162:                          CurrentPoints.Add(ControlPoints[Index]);
 163:                          TLine.EP = ControlPoints[Index];
 164:                      }
 165:                  }
 166:              }
 167:          }
 168:   
 169:          private static void CaculateH(List<LPointClass> CurrentPoints, List<LineClass> CurrentSegments)
 170:          {
 171:              int TempNo = 0;
 172:              
 173:              for (int i = 0; ; i++)
 174:              {                
 175:                  foreach (LineClass TLine in CurrentSegments)
 176:                  {
 177:                      int Ti = Belong(CurrentPoints, TLine.SP.PID);
 178:                      int Tj = Belong(CurrentPoints, TLine.EP.PID);
 179:                      if (CurrentPoints[Ti].H > -99999.99 && CurrentPoints[Tj].H == -99999.99)
 180:                      {
 181:                          CurrentPoints[Tj].H = CurrentPoints[Ti].H + TLine.dH;
 182:                          TempNo++;
 183:                      }
 184:                      else if (CurrentPoints[Ti].H == -99999.99 && CurrentPoints[Tj].H > -99999.99)
 185:                      {
 186:                          CurrentPoints[Ti].H = CurrentPoints[Tj].H - TLine.dH;
 187:                          TempNo++;
 188:                      }
 189:                      TLine.SP = CurrentPoints[Ti];
 190:                      TLine.EP = CurrentPoints[Tj];
 191:                  }
 192:                  if (TempNo == CurrentPoints.Count - ControlPoints.Count)
 193:                      break;
 194:                      
 195:                  if (i > CurrentPoints.Count - ControlPoints.Count)
 196:                      Console.WriteLine("Error!");
 197:              };
 198:          }
 199:   
 200:          private static int Belong(List<LPointClass> CurrentPoints, string s)
 201:          {
 202:              foreach (LPointClass TPoint in CurrentPoints)
 203:              {
 204:                  if (TPoint.PID == s)
 205:                      return CurrentPoints.IndexOf(TPoint);                
 206:              }
 207:              return -1;
 208:          }
 209:   
 210:          private void PrintData()
 211:          { }
 212:   
 213:          private static bool IsSpace(string s)
 214:          {
 215:              if (s == "")
 216:                  return true;
 217:              else return false;
 218:          }
 219:   
 220:          static void Main(string[] args)
 221:          {
 222:              InputData(CurrentPoints, CurrentSegments);
 223:              CaculateH(CurrentPoints, CurrentSegments);
 224:              AdjustClass MyAdjust = new AdjustClass();
 225:              MyAdjust.LevelAdjust(CurrentSegments, CurrentPoints, ControlPoints);
 226:              Console.ReadLine();
 227:          }
 228:      }
 229:  }
posted @ 2011-08-13 09:08  lcxu2  阅读(6290)  评论(8编辑  收藏  举报