GeoLLA.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Ips.Library.Entity
  6. {
  7. public class GeoLLA
  8. {
  9. public GeoLLA() : this(LonNan.Value, LatNan.Value, 0) { }
  10. public GeoLLA(double lon, double lat) : this(lon, lat, 0) { }
  11. public GeoLLA(double lon, double lat, double alt)
  12. {
  13. Lon = lon;
  14. Lat = lat;
  15. Alt = alt;
  16. }
  17. /// <summary>
  18. /// 经度
  19. /// </summary>
  20. public double Lon { get; set; }
  21. /// <summary>
  22. /// 纬度
  23. /// </summary>
  24. public double Lat { get; set; }
  25. /// <summary>
  26. /// 高度
  27. /// </summary>
  28. public double Alt { get; set; }
  29. public const double MinLon = -180;
  30. public const double MaxLon = 180;
  31. public const double MinLat = -90;
  32. public const double MaxLat = 90;
  33. public override string ToString()
  34. {
  35. return $"{Lon},{Lat},{Alt}";
  36. }
  37. public bool IsSome(GeoLLA item)
  38. {
  39. if (item == null) return false;
  40. if (ReferenceEquals(this, item)) return true;
  41. return Lon == item.Lon && Lat == item.Lat && Alt == item.Alt;
  42. }
  43. public bool IsValid()
  44. {
  45. return Lon >= MinLon && Lon <= MaxLon && Lat >= MinLat && Lat <= MaxLat;
  46. }
  47. public double[] ToArray()
  48. {
  49. return new double[] { Lon, Lat, Alt };
  50. }
  51. public static GeoLLA FromString(string val, string itemsep = ",")
  52. {
  53. var items = val.Split(itemsep.ToCharArray());
  54. double lon = LonNan.Value, lat = LatNan.Value, alt = 0;
  55. if (items.Length > 1)
  56. {
  57. lon = double.Parse(items[0].Trim());
  58. lat = double.Parse(items[1].Trim());
  59. }
  60. if (items.Length > 2)
  61. {
  62. alt = double.Parse(items[2].Trim());
  63. }
  64. return new GeoLLA(lon, lat, alt);
  65. }
  66. }
  67. public class LonNan
  68. {
  69. public const double Value = 181;
  70. }
  71. public class LatNan
  72. {
  73. public const double Value = 91;
  74. }
  75. }