Route.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. namespace ARLocation.MapboxRoutes
  5. {
  6. using Vendor.SimpleJSON;
  7. [CreateAssetMenu(fileName = "ARLocationConfig", menuName = "AR+GPS/Route")]
  8. public class Route : ScriptableObject
  9. {
  10. [Serializable]
  11. public class Geometry
  12. {
  13. public List<Location> coordinates = new List<Location>();
  14. public string type;
  15. public static Geometry Parse(JSONNode node)
  16. {
  17. var result = new Geometry();
  18. var coordinatesNode = node["coordinates"];
  19. if (coordinatesNode == null)
  20. {
  21. throw new System.NullReferenceException("No 'coordinates' field!");
  22. }
  23. var coordinatesArray = coordinatesNode.AsArray;
  24. if (coordinatesArray == null)
  25. {
  26. throw new System.NullReferenceException("No 'coordinates' field!");
  27. }
  28. if (coordinatesArray.Count == 2 && coordinatesArray[0].AsArray == null)
  29. {
  30. var x = coordinatesArray[0].AsFloat;
  31. var y = coordinatesArray[1].AsFloat;
  32. result.coordinates.Add(new Location(y, x, 0));
  33. }
  34. else
  35. {
  36. for (int i = 0; i < coordinatesArray.Count; i++)
  37. {
  38. var x = coordinatesArray[i][0].AsFloat;
  39. var y = coordinatesArray[i][1].AsFloat;
  40. result.coordinates.Add(new Location(y, x, 0));
  41. }
  42. }
  43. result.type = node["type"];
  44. return result;
  45. }
  46. public override string ToString()
  47. {
  48. var result = "";
  49. foreach (var c in coordinates)
  50. {
  51. result += $"({c.Latitude}, {c.Longitude}, {c.Altitude})" + ",";
  52. }
  53. result += $"[{type}]";
  54. return result;
  55. }
  56. }
  57. [Serializable]
  58. public class RouteLeg
  59. {
  60. public float distance;
  61. public List<Step> steps;
  62. public override string ToString()
  63. {
  64. string result = "";
  65. result += $"RouteLeg{{ distance = {distance}, steps = [";
  66. foreach (var s in steps)
  67. {
  68. result += s + ",";
  69. }
  70. result += "]}";
  71. return result;
  72. }
  73. public static RouteLeg Parse(JSONNode node)
  74. {
  75. var result = new RouteLeg();
  76. result.distance = node["distance"].AsFloat;
  77. result.steps = new List<Step>();
  78. var steps = node["steps"].AsArray;
  79. for (int i = 0; i < steps.Count; i++)
  80. {
  81. result.steps.Add(Step.Parse(steps[i]));
  82. }
  83. return result;
  84. }
  85. }
  86. [Serializable]
  87. public class Step
  88. {
  89. public float distance;
  90. public Geometry geometry;
  91. public Maneuver maneuver;
  92. public string name;
  93. public override string ToString()
  94. {
  95. return $"Step{{ distance = {distance}, geometry = {geometry}, maneuver = {maneuver}, name = {name} }}";
  96. }
  97. public static Step Parse(JSONNode node)
  98. {
  99. var result = new Step();
  100. result.distance = node["distance"].AsFloat;
  101. result.geometry = Geometry.Parse(node["geometry"]);
  102. result.maneuver = Maneuver.Parse(node["maneuver"]);
  103. result.name = node["name"];
  104. return result;
  105. }
  106. }
  107. [Serializable]
  108. public class Maneuver
  109. {
  110. public int bearing_before;
  111. public int bearing_after;
  112. public string instruction;
  113. public Location location;
  114. public string type;
  115. public override string ToString()
  116. {
  117. string result = "";
  118. result += $"Maneuver{{ bearing_before = {bearing_before}, bearing_after = {bearing_after}, instruction = {instruction}, location = {location}, type = {type} }}";
  119. return result;
  120. }
  121. public static Maneuver Parse(JSONNode node)
  122. {
  123. var result = new Maneuver();
  124. result.bearing_before = node["bearing_before"].AsInt;
  125. result.bearing_after = node["bearing_after"].AsInt;
  126. result.instruction = node["instruction"];
  127. result.type = node["type"];
  128. var loc = node["location"].AsArray;
  129. result.location = new Location(loc[1].AsDouble, loc[0].AsDouble, 0);
  130. return result;
  131. }
  132. }
  133. public float distance;
  134. public Geometry geometry;
  135. public List<RouteLeg> legs;
  136. public static Route Parse(JSONNode node)
  137. {
  138. var result = ScriptableObject.CreateInstance<Route>(); //Route();
  139. result.distance = node["distance"].AsFloat;
  140. result.geometry = Geometry.Parse(node["geometry"]);
  141. result.legs = new List<RouteLeg>();
  142. var legs = node["legs"].AsArray;
  143. for (int i = 0; i < legs.Count; i++)
  144. {
  145. result.legs.Add(RouteLeg.Parse(legs[i]));
  146. }
  147. return result;
  148. }
  149. public override string ToString()
  150. {
  151. string result = "";
  152. result += $"Route{{ distance = {distance}, geometry = {geometry}, legs = [";
  153. foreach (var leg in legs)
  154. {
  155. result += leg;
  156. }
  157. result += "]}";
  158. return result;
  159. }
  160. }
  161. }