1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
| using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Data; using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra; namespace GPS卫星坐标与单点定位计算 { public struct Canshu { public double a, dn, to, mo, e, omega, gpsweek; public double cuc, cus, crc, crs, cic, cis, idot, io, OMEGA, OMEGA_DOT; public int prn, year, month, day, hour, minute; public double second; } public struct XYZ { public double X, Y, Z; } public struct Inputdata { public double second, X, Y, Z; public int prn, year, month, day, hour, minute; } class SatelliteCoordinate { int year, month, day, hour, minute; double second; public SatelliteCoordinate(int year, int month, int day, int hour, int minute, double second) { this.year = year; this.month = month; this.day = day; this.hour = hour; this.minute = minute; this.second = second; } private int GetGPSTime( ref int weekno, ref double gpstime) { int dayofw, dayofy, yr, ttlday, m; int[] dinmth = new int[13]; dinmth[1] = 31; dinmth[2] = 28; dinmth[3] = 31; dinmth[4] = 30; dinmth[5] = 31; dinmth[6] = 30; dinmth[7] = 31; dinmth[8] = 31; dinmth[9] = 30; dinmth[10] = 31; dinmth[11] = 30; dinmth[12] = 31; if (year > 80) year = year + 1900; if (year < 80) year = year + 2000; if (year < 1981 || month < 1 || month > 12 || day < 1 || day > 31) weekno = 0; if (month == 1) dayofy = day; else { dayofy = 0; for (m = 1; m <= (month - 1); m++) { dayofy += dinmth[m]; if (m == 2) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) dayofy += 1; } } dayofy += day; } ttlday = 360; for (yr = 1981; yr <= (year - 1); yr++) { ttlday += 365; if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0) ttlday += 1; } ttlday += dayofy; weekno = ttlday / 7; dayofw = ttlday - 7 * (weekno); gpstime = (double)(hour * 3600 + minute * 60 + second + dayofw * 86400); return yr; } private XYZ caculate(Canshu canshu) { int weekno=0;double gpst=0.0; GetGPSTime(ref weekno, ref gpst);
double gm = 3.978655e+14; double ve = 7.2921151467e-5; XYZ xyz; xyz.X = xyz.Y = xyz.Z = 0; double n = Math.Sqrt(gm / Math.Pow(canshu.a, 3)) + canshu.dn; double tk = gpst - canshu.to; double mk = canshu.mo + n * tk; double ek0 = canshu.mo; double ek1 = 0.0; while (Math.Abs(ek0 - ek1) > 0.0000000001) { ek1 = ek0; ek0 = canshu.mo + canshu.e * Math.Sin(ek0); } double vk = Math.Atan(Math.Sqrt(1 - canshu.e * canshu.e) * Math.Sin(ek1) / (Math.Cos(ek1) - canshu.e));
double fei = vk + canshu.omega;
double rk = canshu.a * (1 - canshu.e * Math.Cos(ek1));
double stu = canshu.cuc * Math.Cos(2 * fei) + canshu.cus * Math.Sin(2 * fei); double str = canshu.crc * Math.Cos(2 * fei) + canshu.crs * Math.Sin(2 * fei); double sti = canshu.cic * Math.Cos(2 * fei) + canshu.cis * Math.Sin(2 * fei); double uk = fei + stu; double ik = canshu.io + sti + canshu.idot * tk; rk = rk + str; double xk = rk * Math.Cos(uk); double yk = rk * Math.Sin(uk); double OMEGAk = canshu.OMEGA + tk * (canshu.OMEGA_DOT - ve) - ve * canshu.to;
xyz.X = xk * Math.Cos(OMEGAk) - yk * Math.Cos(ik) * Math.Sin(OMEGAk); xyz.Y = xk * Math.Sin(OMEGAk) + yk * Math.Cos(ik) * Math.Cos(OMEGAk); xyz.Z = yk * Math.Sin(ik); return xyz; } public DataSet EphemerisCac(string sprns) { DataBase db = new DataBase(); string sql = "select * from wxc where (" + " prn= " + sprns + " and year = " + year + " and month = " + month + " and day = " + day + " and hour = " + hour + " and minute = " + minute + " and second = " + second + " )"; DataSet ds = new DataSet();
db.SearchDatabase(sql, ref ds); List<string> scs = new List<string>(); foreach (DataRow dr in ds.Tables[0].Rows) { foreach (DataColumn dc in ds.Tables[0].Columns) scs.Add(dr[dc].ToString()); }
#region Canshu canshu = new Canshu(); canshu.prn = int.Parse(scs[0]); canshu.year = int.Parse(scs[1]); canshu.month = int.Parse(scs[2]); canshu.day = int.Parse(scs[3]); canshu.hour = int.Parse(scs[4]); canshu.minute = int.Parse(scs[5]);
canshu.second = double.Parse(scs[6]); canshu.a = double.Parse(scs[7]); canshu.dn = double.Parse(scs[8]); canshu.to = double.Parse(scs[9]); canshu.mo = double.Parse(scs[10]); canshu.e = double.Parse(scs[11]); canshu.omega = double.Parse(scs[12]); canshu.cuc = double.Parse(scs[13]); canshu.cus = double.Parse(scs[14]); canshu.crc = double.Parse(scs[15]); canshu.crs = double.Parse(scs[16]); canshu.cic = double.Parse(scs[17]); canshu.cis = double.Parse(scs[18]); canshu.idot = double.Parse(scs[19]); canshu.io = double.Parse(scs[20]); canshu.OMEGA = double.Parse(scs[21]); canshu.OMEGA_DOT = double.Parse(scs[22]); #endregion XYZ xYZ = caculate(canshu); XyzDatabase db2 = new XyzDatabase(); db2.InsertDatabase(xYZ, canshu); DataSet ds2 = new DataSet(); string pre = " Where ( prn= " + scs[0] + " and year = " + scs[1] + " and month = " + scs[2] + " and day = " + scs[3] + " and hour =" + scs[4] + " and minute = " + scs[5] + " and second = " + scs[6]+")";
db2.WatchDatabase(ref ds2, "xyz",pre); return ds2; } public Inputdata CoordinateCac(string sprns) { DataSet ds = new DataSet(); DataBase db = new DataBase(); string sql = "select * from wxc where ( prn =" + sprns + " and year = " + year.ToString() + " and month = " + month.ToString() + " and day = " + day.ToString() + " )";
DataSet ds0 = new DataSet(); db.SearchDatabase(sql, ref ds0); double mintime=90000;int minidex=0; for(int i=0;i<ds0.Tables[0].Rows.Count;i++) { int chour = int.Parse(ds0.Tables[0].Rows[i][4].ToString()); int cminute = int.Parse(ds0.Tables[0].Rows[i][5].ToString()); double csecond = double.Parse(ds0.Tables[0].Rows[i][5].ToString()); double time = Math.Abs(chour - hour) * 3600 + Math.Abs(cminute - minute) * 60 + Math.Abs(csecond - second); if(time < mintime) { minidex = i; mintime = time; } } #region//canshu参数赋值 Canshu canshu = new Canshu(); canshu.prn = int.Parse(ds0.Tables[0].Rows[minidex][0].ToString()); canshu.year = int.Parse(ds0.Tables[0].Rows[minidex][1].ToString()); canshu.month = int.Parse(ds0.Tables[0].Rows[minidex][2].ToString()); canshu.day = int.Parse(ds0.Tables[0].Rows[minidex][3].ToString()); canshu.hour = int.Parse(ds0.Tables[0].Rows[minidex][4].ToString()); canshu.minute = int.Parse(ds0.Tables[0].Rows[minidex][5].ToString());
canshu.second = double.Parse(ds0.Tables[0].Rows[minidex][6].ToString()); canshu.a = double.Parse(ds0.Tables[0].Rows[minidex][7].ToString()); canshu.dn = double.Parse(ds0.Tables[0].Rows[minidex][8].ToString()); canshu.to = double.Parse(ds0.Tables[0].Rows[minidex][9].ToString()); canshu.mo = double.Parse(ds0.Tables[0].Rows[minidex][10].ToString()); canshu.e = double.Parse(ds0.Tables[0].Rows[minidex][11].ToString()); canshu.omega = double.Parse(ds0.Tables[0].Rows[minidex][12].ToString()); canshu.cuc = double.Parse(ds0.Tables[0].Rows[minidex][13].ToString()); canshu.cus = double.Parse(ds0.Tables[0].Rows[minidex][14].ToString()); canshu.crc = double.Parse(ds0.Tables[0].Rows[minidex][15].ToString()); canshu.crs = double.Parse(ds0.Tables[0].Rows[minidex][16].ToString()); canshu.cic = double.Parse(ds0.Tables[0].Rows[minidex][17].ToString()); canshu.cis = double.Parse(ds0.Tables[0].Rows[minidex][18].ToString()); canshu.idot = double.Parse(ds0.Tables[0].Rows[minidex][19].ToString()); canshu.io=double.Parse(ds0.Tables[0].Rows[minidex][20].ToString()); canshu.OMEGA = double.Parse(ds0.Tables[0].Rows[minidex][21].ToString()); canshu.OMEGA_DOT = double.Parse(ds0.Tables[0].Rows[minidex][22].ToString()); #endregion XYZ xYZ = caculate(canshu); XyzDatabase db2 = new XyzDatabase(); Inputdata ida = new Inputdata(); ida.X = xYZ.X; ida.Y = xYZ.Y; ida.Z = xYZ.Z;
ida.prn = canshu.prn; ida.year = year; ida.minute = minute; ida.month = month; ida.day = day; ida.hour = hour; ida.second = second;
db2.InsertDatabase(ida);
return ida; } } class DataBase { SqlConnection myconnection = new SqlConnection(); public DataBase() { string a = "Data Source=DESKTOP-PNEN86K;Initial Catalog=canshu;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;max pool size=30000"; myconnection.ConnectionString = a; myconnection.Open(); } public DataBase(string catalog) { string a = "Data Source=DESKTOP-PNEN86K;Initial Catalog="+catalog+";Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; myconnection.ConnectionString = a; myconnection.Open(); } public void InsertDatabase(Canshu canshu) { if (myconnection.State == System.Data.ConnectionState.Open) { string prns = canshu.prn.ToString(); string year = canshu.year.ToString(); string month = canshu.month.ToString(); string day = canshu.day.ToString(); string hour = canshu.hour.ToString(); string minute = canshu.minute.ToString(); string second = canshu.second.ToString(); string a = canshu.a.ToString(); string dn = canshu.dn.ToString(); string t0 = canshu.to.ToString(); string m0 = canshu.mo.ToString(); string e = canshu.e.ToString(); string omega = canshu.omega.ToString(); string cuc = canshu.cuc.ToString(); string cus = canshu.cus.ToString(); string crc = canshu.crc.ToString(); string crs = canshu.crs.ToString(); string cic = canshu.cic.ToString(); string cis = canshu.cis.ToString(); string idot = canshu.idot.ToString(); string i0 = canshu.io.ToString(); string bigomega = canshu.OMEGA.ToString(); string bigomegadot = canshu.OMEGA_DOT.ToString(); string col = "prn,year,month,day,hour,minute,second,a,dn,t0,m0,e,omega,cuc,cus,crc,crs,cic,cis,idot,i0,bigomega,bigomegadot"; string res = prns + "," + year + "," + month + "," + day + "," + hour + "," + minute + "," + second + "," + a + "," + dn + "," + t0 + "," + m0 + "," + e + "," + omega + "," + cuc + "," + cus + "," + crc + "," + crs + "," + cic + "," + cis + "," + idot + "," + i0 + "," + bigomega + "," + bigomegadot; string SQL1 = "insert into wxc (" + col + ") values(" + res + ")"; SqlCommand com = new SqlCommand(SQL1, myconnection); com.ExecuteNonQuery(); } } public void WatchDatabase(ref DataSet ds,string biao="wxc",string pre= "") { DataBase db = new DataBase(); string SQL2 = "Select * From " + biao + pre; SqlDataAdapter mydataadapter = new SqlDataAdapter(); mydataadapter.SelectCommand = new SqlCommand(SQL2, myconnection); ds = new DataSet(); mydataadapter.Fill(ds, biao); } public void SearchDatabase(string SQL2,ref DataSet ds) { DataBase db = new DataBase();
string biao = "wxc"; SqlDataAdapter mydataadapter = new SqlDataAdapter(); mydataadapter.SelectCommand = new SqlCommand(SQL2, myconnection); ds = new DataSet(); mydataadapter.Fill(ds, biao); } public DataSet ReviseDatabase(string SQL1) { DataSet ds = new DataSet(); if (myconnection.State==System.Data.ConnectionState.Open) { SqlCommand com = new SqlCommand(SQL1, myconnection); com.ExecuteNonQuery(); string SQL2 = "Select * From wxc" ; SqlDataAdapter mydataadapter = new SqlDataAdapter(); mydataadapter.SelectCommand = new SqlCommand(SQL2, myconnection); mydataadapter.Fill(ds, "wxc"); } return ds; } public DataSet DeleteDatabase(string SQL) { DataSet ds = new DataSet(); if(myconnection.State == System.Data.ConnectionState.Open) { SqlCommand com = new SqlCommand(SQL, myconnection); com.ExecuteNonQuery(); string SQL2 = "Select * From wxc"; SqlDataAdapter mydataadapter = new SqlDataAdapter(); mydataadapter.SelectCommand = new SqlCommand(SQL2, myconnection); mydataadapter.Fill(ds, "wxc"); }
return ds; } }
class XyzDatabase:DataBase { SqlConnection myconnection = new SqlConnection(); public XyzDatabase() { string a = "Data Source=DESKTOP-PNEN86K;Initial Catalog=canshu;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;max pool size=30000"; myconnection.ConnectionString = a; myconnection.Open(); } public void InsertDatabase(XYZ xYZ, Canshu canshu) { if (myconnection.State == System.Data.ConnectionState.Open) { string sx = xYZ.X.ToString(); string sy = xYZ.Y.ToString(); string sz = xYZ.Z.ToString(); string prns = canshu.prn.ToString(); string year = canshu.year.ToString(); string month = canshu.month.ToString(); string day = canshu.day.ToString(); string hour = canshu.hour.ToString(); string minute = canshu.minute.ToString(); string second = canshu.second.ToString(); string col = "prn,year,month,day,hour,minute,second,x,y,z"; string res = prns + " , " + year + " , " + month + " , " + day + " , " + hour + " , " + minute + " , " + second + " , " + sx + " , " + sy + " , " + sz; string SQL = "insert into xyz (" + col + " ) values (" + res + ")"; SqlCommand com = new SqlCommand(SQL, myconnection); com.ExecuteNonQuery(); } } public void InsertDatabase(Inputdata ida) { if (myconnection.State == System.Data.ConnectionState.Open) { string sx = ida.X.ToString(); string sy = ida.Y.ToString(); string sz = ida.Z.ToString(); string prns = ida.prn.ToString(); string year = ida.year.ToString(); string month = ida.month.ToString(); string day = ida.day.ToString(); string hour = ida.hour.ToString(); string minute = ida.minute.ToString(); string second = ida.second.ToString(); string col = "prn,year,month,day,hour,minute,second,x,y,z"; string res = prns + " , " + year + " , " + month + " , " + day + " , " + hour + " , " + minute + " , " + second + " , " + sx + " , " + sy + " , " + sz; string SQL = "insert into xyz (" + col + " ) values (" + res + ")"; SqlCommand com = new SqlCommand(SQL, myconnection); com.ExecuteNonQuery(); } } } class CzDatabase:DataBase { SqlConnection myconnection = new SqlConnection(); public CzDatabase() { string a = "Data Source=DESKTOP-PNEN86K;Initial Catalog=canshu;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;max pool size=30000"; myconnection.ConnectionString = a; myconnection.Open(); } public void InsertDatabase(XYZ xYZ) { if (myconnection.State == System.Data.ConnectionState.Open) { string sx = xYZ.X.ToString(); string sy = xYZ.Y.ToString(); string sz = xYZ.Z.ToString();
string col = "x,y,z"; string res = sx + " , " + sy + " , " + sz; string SQL = "insert into cz (" + col + " ) values (" + res + ")"; SqlCommand com = new SqlCommand(SQL, myconnection); com.ExecuteNonQuery(); } } }
class SinglePoint { private double L0(XYZ xyz0,XYZ xyzs) { double l0; l0 = Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2); l0 = Math.Sqrt(l0); return l0; } public double[,] singlecac(XYZ xyz0,List<XYZ> xyzlist) { if(xyzlist.Count==4) { double[,] A = new double[4,4]; double[,] l0 = new double[4,1]; for(int i =0;i<4;i++) { XYZ xyzs = new XYZ(); xyzs = xyzlist[i]; A[i, 0] = -(xyzs.X - xyz0.X) / (Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2)); A[i, 1] = -(xyzs.Y - xyz0.Y) / (Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2)); A[i, 2] = -(xyzs.Z - xyz0.Z) / (Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2)); A[i, 3] = 3E8; l0[i, 0] = -L0(xyz0, xyzs); } double[,] x = new double[4, 1]; x = Matrix.MultiplyMatrix(Matrix.Athwart(A), l0); return x; } else { int n = xyzlist.Count; double[,] A = new double[n, n]; double[,] l0 = new double[n, 1]; for (int i = 0; i <n; i++) { XYZ xyzs = new XYZ(); xyzs = xyzlist[i]; A[i, 0] = -(xyzs.X - xyz0.X) / (Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2)); A[i, 1] = -(xyzs.Y - xyz0.Y) / (Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2)); A[i, 2] = -(xyzs.Z - xyz0.Z) / (Math.Pow((xyz0.X - xyzs.X), 2) + Math.Pow((xyz0.Y - xyzs.Y), 2) + Math.Pow((xyz0.Z - xyzs.Z), 2)); A[i, 3] = 3E8; l0[i, 0] = -L0(xyz0, xyzs); } double[,] x = new double[4, 1]; x = Matrix.MultiplyMatrix(Matrix.Athwart((Matrix.MultiplyMatrix(Matrix.Transpose(A), A))) ,Matrix.MultiplyMatrix(Matrix.Transpose(A),l0)); return x; } } } }
|