RouteGuideUtil.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2015 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using Newtonsoft.Json;
  15. using Newtonsoft.Json.Linq;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Reflection;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. namespace Routeguide
  24. {
  25. /// <summary>
  26. /// Utility methods for the route guide example.
  27. /// </summary>
  28. public static class RouteGuideUtil
  29. {
  30. public const string DefaultFeaturesResourceName = "RouteGuide.route_guide_db.json";
  31. private const double CoordFactor = 1e7;
  32. /// <summary>
  33. /// Indicates whether the given feature exists (i.e. has a valid name).
  34. /// </summary>
  35. public static bool Exists(this Feature feature)
  36. {
  37. return feature != null && (feature.Name.Length != 0);
  38. }
  39. public static double GetLatitude(this Point point)
  40. {
  41. return point.Latitude / CoordFactor;
  42. }
  43. public static double GetLongitude(this Point point)
  44. {
  45. return point.Longitude / CoordFactor;
  46. }
  47. /// <summary>
  48. /// Calculate the distance between two points using the "haversine" formula.
  49. /// The formula is based on http://mathforum.org/library/drmath/view/51879.html
  50. /// </summary>
  51. /// <param name="start">the starting point</param>
  52. /// <param name="end">the end point</param>
  53. /// <returns>the distance between the points in meters</returns>
  54. public static double GetDistance(this Point start, Point end)
  55. {
  56. int r = 6371000; // earth radius in metres
  57. double lat1 = ToRadians(start.GetLatitude());
  58. double lat2 = ToRadians(end.GetLatitude());
  59. double lon1 = ToRadians(start.GetLongitude());
  60. double lon2 = ToRadians(end.GetLongitude());
  61. double deltalat = lat2 - lat1;
  62. double deltalon = lon2 - lon1;
  63. double a = Math.Sin(deltalat / 2) * Math.Sin(deltalat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(deltalon / 2) * Math.Sin(deltalon / 2);
  64. double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
  65. return r * c;
  66. }
  67. /// <summary>
  68. /// Returns <c>true</c> if rectangular area contains given point.
  69. /// </summary>
  70. public static bool Contains(this Rectangle rectangle, Point point)
  71. {
  72. int left = Math.Min(rectangle.Lo.Longitude, rectangle.Hi.Longitude);
  73. int right = Math.Max(rectangle.Lo.Longitude, rectangle.Hi.Longitude);
  74. int top = Math.Max(rectangle.Lo.Latitude, rectangle.Hi.Latitude);
  75. int bottom = Math.Min(rectangle.Lo.Latitude, rectangle.Hi.Latitude);
  76. return (point.Longitude >= left && point.Longitude <= right && point.Latitude >= bottom && point.Latitude <= top);
  77. }
  78. private static double ToRadians(double val)
  79. {
  80. return (Math.PI / 180) * val;
  81. }
  82. /// <summary>
  83. /// Parses features from an embedded resource.
  84. /// </summary>
  85. public static List<Feature> LoadFeatures()
  86. {
  87. var features = new List<Feature>();
  88. var jsonFeatures = JsonConvert.DeserializeObject<List<JsonFeature>>(ReadFeaturesFromResource());
  89. foreach(var jsonFeature in jsonFeatures)
  90. {
  91. features.Add(new Feature
  92. {
  93. Name = jsonFeature.name,
  94. Location = new Point { Longitude = jsonFeature.location.longitude, Latitude = jsonFeature.location.latitude}
  95. });
  96. }
  97. return features;
  98. }
  99. private static string ReadFeaturesFromResource()
  100. {
  101. var stream = typeof(RouteGuideUtil).GetTypeInfo().Assembly.GetManifestResourceStream(DefaultFeaturesResourceName);
  102. if (stream == null)
  103. {
  104. throw new IOException(string.Format("Error loading the embedded resource \"{0}\"", DefaultFeaturesResourceName));
  105. }
  106. using (var streamReader = new StreamReader(stream))
  107. {
  108. return streamReader.ReadToEnd();
  109. }
  110. }
  111. #pragma warning disable 0649 // Suppresses "Field 'x' is never assigned to".
  112. private class JsonFeature
  113. {
  114. public string name;
  115. public JsonLocation location;
  116. }
  117. private class JsonLocation
  118. {
  119. public int longitude;
  120. public int latitude;
  121. }
  122. #pragma warning restore 0649
  123. }
  124. }