GeoFence.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using InABox.Core;
  8. namespace Comal.Classes
  9. {
  10. public class GeoJSONCrsProperties
  11. {
  12. public string Name { get; set; }
  13. }
  14. public class GeoJSONCrs
  15. {
  16. public string Type { get; set; }
  17. public GeoJSONCrsProperties Properties { get; set; }
  18. }
  19. public class GeoJSONFeatureGeometry
  20. {
  21. public string Type { get; set; }
  22. public List<List<List<List<float>>>> Coordinates { get; set; }
  23. public List<PointF> Points()
  24. {
  25. var result = new List<PointF>();
  26. var list = Coordinates?.FirstOrDefault()?.FirstOrDefault() ?? new List<List<float>>();
  27. foreach (var item in list)
  28. {
  29. if (item.Count == 2)
  30. result.Add(new PointF(item[0], item[1]));
  31. }
  32. return result;
  33. }
  34. public Tuple<PointF,PointF> Bounds()
  35. {
  36. float minX = float.MaxValue, minY = float.MaxValue, maxX = float.MinValue, maxY = float.MinValue;
  37. var points = Points();
  38. foreach (var point in points)
  39. {
  40. if (point.X < minX) minX = point.X;
  41. if (point.X > maxX) maxX = point.X;
  42. if (point.Y < minY) minY = point.Y;
  43. if (point.Y > maxY) maxY = point.Y;
  44. }
  45. return new Tuple<PointF,PointF>(new PointF(minX, minY), new PointF(maxX, maxY));
  46. }
  47. }
  48. public class GeoJSONFeatureProperties
  49. {
  50. public int? Land_ID { get; set; }
  51. public string? Road_Number_type { get; set; }
  52. public string? Road_Number_1 { get; set; }
  53. public string? Road_Number_2 { get; set; }
  54. public string? Lot_Number { get; set; }
  55. public string? Road_Name { get; set; }
  56. public string? Road_Type { get; set; }
  57. public string? Road_Suffix { get; set; }
  58. public string? Locality { get; set; }
  59. public string? View_Scale { get; set; }
  60. public double? ST_Area_Shape_ { get; set; }
  61. public double? ST_Perimeter_Shape_ { get; set; }
  62. public string ID() => Land_ID?.ToString() ?? "";
  63. public string FullAddress() => $"{Street()} {Suburb()} {State()} {Country()}".Trim().ToUpper();
  64. public String Street()
  65. {
  66. List<String> result = new List<string>();
  67. if (string.IsNullOrWhiteSpace(Lot_Number))
  68. {
  69. if (!string.IsNullOrWhiteSpace(Road_Number_2))
  70. result.Add($"{Road_Number_1 ?? ""}/{Road_Number_2 ?? ""}");
  71. else
  72. result.Add(Road_Number_1 ?? "");
  73. }
  74. else
  75. result.Add($"LOT {Lot_Number ?? ""}");
  76. result.Add(Road_Name ?? "");
  77. result.Add(Road_Type ?? "");
  78. return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.Join(" ", result).ToLower());
  79. }
  80. public String Suburb() => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Locality?.ToLower() ?? "");
  81. public String State() => "";
  82. public String Postcode() => "";
  83. public String Country() => "";
  84. }
  85. public class GeoJSONFeature
  86. {
  87. public string? Type { get; set; }
  88. public GeoJSONFeatureProperties? Properties { get; set; }
  89. public GeoJSONFeatureGeometry? Geometry { get; set; }
  90. }
  91. public class GeoJSONFile
  92. {
  93. public string? Type { get; set; }
  94. public string? Name { get; set; }
  95. public GeoJSONCrs? Crs { get; set; }
  96. public List<GeoJSONFeature>? Features { get; set; }
  97. public static GeoJSONFile? Load(string filename)
  98. {
  99. var fs = new FileStream(filename, FileMode.Open);
  100. var geojson = Serialization.Deserialize<GeoJSONFile>(fs);
  101. return geojson;
  102. }
  103. }
  104. public class GeoFence : Entity, IRemotable, IPersistent
  105. {
  106. public String Street { get; set; }
  107. public String City { get; set; }
  108. public String State { get; set; }
  109. public String Country { get; set; }
  110. public String PostCode { get; set; }
  111. public string FullAddress { get; set; }
  112. public double MinX { get; set; }
  113. public double MinY { get; set; }
  114. public double MaxX { get; set; }
  115. public double MaxY { get; set; }
  116. public string Geometry { get; set; }
  117. }
  118. }