• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 using Newtonsoft.Json;
16 using Newtonsoft.Json.Linq;
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Text;
22 using System.Threading.Tasks;
23 
24 namespace Routeguide
25 {
26     /// <summary>
27     /// Utility methods for the route guide example.
28     /// </summary>
29     public static class RouteGuideUtil
30     {
31         public const string DefaultFeaturesFile = "route_guide_db.json";
32 
33         private const double CoordFactor = 1e7;
34 
35         /// <summary>
36         /// Indicates whether the given feature exists (i.e. has a valid name).
37         /// </summary>
Exists(this Feature feature)38         public static bool Exists(this Feature feature)
39         {
40             return feature != null && (feature.Name.Length != 0);
41         }
42 
GetLatitude(this Point point)43         public static double GetLatitude(this Point point)
44         {
45             return point.Latitude / CoordFactor;
46         }
47 
GetLongitude(this Point point)48         public static double GetLongitude(this Point point)
49         {
50             return point.Longitude / CoordFactor;
51         }
52 
53         /// <summary>
54         /// Calculate the distance between two points using the "haversine" formula.
55         /// The formula is based on http://mathforum.org/library/drmath/view/51879.html
56         /// </summary>
57         /// <param name="start">the starting point</param>
58         /// <param name="end">the end point</param>
59         /// <returns>the distance between the points in meters</returns>
GetDistance(this Point start, Point end)60         public static double GetDistance(this Point start, Point end)
61         {
62             int r = 6371000;  // earth radius in metres
63             double lat1 = ToRadians(start.GetLatitude());
64             double lat2 = ToRadians(end.GetLatitude());
65             double lon1 = ToRadians(start.GetLongitude());
66             double lon2 = ToRadians(end.GetLongitude());
67             double deltalat = lat2 - lat1;
68             double deltalon = lon2 - lon1;
69 
70             double a = Math.Sin(deltalat / 2) * Math.Sin(deltalat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(deltalon / 2) * Math.Sin(deltalon / 2);
71             double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
72             return r * c;
73         }
74 
75         /// <summary>
76         /// Returns <c>true</c> if rectangular area contains given point.
77         /// </summary>
Contains(this Rectangle rectangle, Point point)78         public static bool Contains(this Rectangle rectangle, Point point)
79         {
80             int left = Math.Min(rectangle.Lo.Longitude, rectangle.Hi.Longitude);
81             int right = Math.Max(rectangle.Lo.Longitude, rectangle.Hi.Longitude);
82             int top = Math.Max(rectangle.Lo.Latitude, rectangle.Hi.Latitude);
83             int bottom = Math.Min(rectangle.Lo.Latitude, rectangle.Hi.Latitude);
84             return (point.Longitude >= left && point.Longitude <= right && point.Latitude >= bottom && point.Latitude <= top);
85         }
86 
ToRadians(double val)87         private static double ToRadians(double val)
88         {
89             return (Math.PI / 180) * val;
90         }
91 
92         /// <summary>
93         /// Parses features from a JSON file.
94         /// </summary>
ParseFeatures(string filename)95         public static List<Feature> ParseFeatures(string filename)
96         {
97             var features = new List<Feature>();
98             var jsonFeatures = JsonConvert.DeserializeObject<List<JsonFeature>>(File.ReadAllText(filename));
99 
100             foreach(var jsonFeature in jsonFeatures)
101             {
102                 features.Add(new Feature
103                 {
104                     Name = jsonFeature.name,
105                     Location = new Point { Longitude = jsonFeature.location.longitude, Latitude = jsonFeature.location.latitude}
106                 });
107             }
108             return features;
109         }
110 
111         private class JsonFeature
112         {
113             public string name;
114             public JsonLocation location;
115         }
116 
117         private class JsonLocation
118         {
119             public int longitude;
120             public int latitude;
121         }
122     }
123 }
124