GeoJSONFile.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 PRSServer.Engines.GPS;
  9. public class GeoJSONCrsProperties
  10. {
  11. public string Name { get; set; }
  12. }
  13. public class GeoJSONCrs
  14. {
  15. public string Type { get; set; }
  16. public GeoJSONCrsProperties Properties { get; set; }
  17. }
  18. public class GeoJSONFeatureGeometry
  19. {
  20. public string Type { get; set; }
  21. public List<List<List<List<float>>>> Coordinates { get; set; }
  22. public List<PointF> Points()
  23. {
  24. var result = new List<PointF>();
  25. var list = Coordinates?.FirstOrDefault()?.FirstOrDefault() ?? new List<List<float>>();
  26. foreach (var item in list)
  27. {
  28. if (item.Count == 2)
  29. result.Add(new PointF(item[0], item[1]));
  30. }
  31. return result;
  32. }
  33. public Tuple<PointF,PointF> BoundingBox()
  34. {
  35. float minX = float.MaxValue, minY = float.MaxValue, maxX = float.MinValue, maxY = float.MinValue;
  36. var points = Points();
  37. foreach (var point in points)
  38. {
  39. if (point.X < minX) minX = point.X;
  40. if (point.X > maxX) maxX = point.X;
  41. if (point.Y < minY) minY = point.Y;
  42. if (point.Y > maxY) maxY = point.Y;
  43. }
  44. return new Tuple<PointF,PointF>(new PointF(minX, minY), new PointF(maxX, maxY));
  45. }
  46. }
  47. public class GeoJSONFeatureProperties
  48. {
  49. public int? Land_ID { get; set; }
  50. public string? Road_Number_type { get; set; }
  51. public string? Road_Number_1 { get; set; }
  52. public string? Road_Number_2 { get; set; }
  53. public string? Lot_Number { get; set; }
  54. public string? Road_Name { get; set; }
  55. public string? Road_Type { get; set; }
  56. public string? Road_Suffix { get; set; }
  57. public string? Locality { get; set; }
  58. public string? View_Scale { get; set; }
  59. public double? ST_Area_Shape_ { get; set; }
  60. public double? ST_Perimeter_Shape_ { get; set; }
  61. public String Street()
  62. {
  63. List<String> result = new List<string>();
  64. if (string.IsNullOrWhiteSpace(Lot_Number))
  65. {
  66. if (!string.IsNullOrWhiteSpace(Road_Number_2))
  67. result.Add($"{Road_Number_1}/{Road_Number_2}");
  68. else
  69. result.Add(Road_Number_1);
  70. }
  71. else
  72. result.Add($"LOT {Lot_Number}");
  73. result.Add(Road_Name);
  74. result.Add(Road_Type);
  75. return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.Join(" ", result).ToLower());
  76. }
  77. public String Suburb() => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Locality?.ToLower() ?? "");
  78. public String State() => "";
  79. public String Country => "";
  80. }
  81. public class GeoJSONFeature
  82. {
  83. public string? Type { get; set; }
  84. public GeoJSONFeatureProperties? Properties { get; set; }
  85. public GeoJSONFeatureGeometry? Geometry { get; set; }
  86. }
  87. public class GeoJSONFile
  88. {
  89. public string? Type { get; set; }
  90. public string? Name { get; set; }
  91. public GeoJSONCrs? Crs { get; set; }
  92. public List<GeoJSONFeature>? Features { get; set; }
  93. public static GeoJSONFile? Load(string filename)
  94. {
  95. var serializer = new Newtonsoft.Json.JsonSerializer();
  96. using var sr = new StreamReader(filename);
  97. using var jtr = new Newtonsoft.Json.JsonTextReader(sr);
  98. var geojson = serializer.Deserialize<GeoJSONFile>(jtr);
  99. return geojson;
  100. }
  101. }