blob: 6b8f9012cc56ccb0c6549a6dcc4257d77c1b6481 [file] [log] [blame]
Craig Tiller897e4fe2015-12-22 15:03:40 -08001// Copyright 2015, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30using Newtonsoft.Json;
Jan Tattermusche003f012015-05-22 09:34:50 -070031using Newtonsoft.Json.Linq;
32using System;
33using System.Collections.Generic;
34using System.IO;
35using System.Linq;
36using System.Text;
37using System.Threading.Tasks;
38
Jan Tattermusch73f9dbb2015-09-09 09:39:14 -070039namespace Routeguide
Jan Tattermusche003f012015-05-22 09:34:50 -070040{
Jan Tattermusch121c4d12015-08-31 10:27:27 -070041 /// <summary>
42 /// Utility methods for the route guide example.
43 /// </summary>
Jan Tattermusche003f012015-05-22 09:34:50 -070044 public static class RouteGuideUtil
45 {
46 public const string DefaultFeaturesFile = "route_guide_db.json";
47
48 private const double CoordFactor = 1e7;
49
50 /// <summary>
51 /// Indicates whether the given feature exists (i.e. has a valid name).
52 /// </summary>
Jan Tattermusch121c4d12015-08-31 10:27:27 -070053 public static bool Exists(this Feature feature)
Jan Tattermusche003f012015-05-22 09:34:50 -070054 {
55 return feature != null && (feature.Name.Length != 0);
56 }
57
Jan Tattermusch121c4d12015-08-31 10:27:27 -070058 public static double GetLatitude(this Point point)
Jan Tattermusche003f012015-05-22 09:34:50 -070059 {
60 return point.Latitude / CoordFactor;
61 }
62
Jan Tattermusch121c4d12015-08-31 10:27:27 -070063 public static double GetLongitude(this Point point)
Jan Tattermusche003f012015-05-22 09:34:50 -070064 {
65 return point.Longitude / CoordFactor;
66 }
67
68 /// <summary>
Jan Tattermusch121c4d12015-08-31 10:27:27 -070069 /// Calculate the distance between two points using the "haversine" formula.
70 /// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
71 /// </summary>
72 /// <param name="start">the starting point</param>
73 /// <param name="end">the end point</param>
74 /// <returns>the distance between the points in meters</returns>
75 public static double GetDistance(this Point start, Point end)
76 {
77 double lat1 = start.GetLatitude();
78 double lat2 = end.GetLatitude();
79 double lon1 = start.GetLongitude();
80 double lon2 = end.GetLongitude();
81 int r = 6371000; // metres
82 double phi1 = ToRadians(lat1);
83 double phi2 = ToRadians(lat2);
84 double deltaPhi = ToRadians(lat2 - lat1);
85 double deltaLambda = ToRadians(lon2 - lon1);
86
87 double a = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) + Math.Cos(phi1) * Math.Cos(phi2) * Math.Sin(deltaLambda / 2) * Math.Sin(deltaLambda / 2);
88 double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
89
90 return r * c;
91 }
92
93 /// <summary>
94 /// Returns <c>true</c> if rectangular area contains given point.
95 /// </summary>
96 public static bool Contains(this Rectangle rectangle, Point point)
97 {
98 int left = Math.Min(rectangle.Lo.Longitude, rectangle.Hi.Longitude);
99 int right = Math.Max(rectangle.Lo.Longitude, rectangle.Hi.Longitude);
100 int top = Math.Max(rectangle.Lo.Latitude, rectangle.Hi.Latitude);
101 int bottom = Math.Min(rectangle.Lo.Latitude, rectangle.Hi.Latitude);
102 return (point.Longitude >= left && point.Longitude <= right && point.Latitude >= bottom && point.Latitude <= top);
103 }
104
105 private static double ToRadians(double val)
106 {
107 return (Math.PI / 180) * val;
108 }
109
110 /// <summary>
Jan Tattermusche003f012015-05-22 09:34:50 -0700111 /// Parses features from a JSON file.
112 /// </summary>
113 public static List<Feature> ParseFeatures(string filename)
114 {
115 var features = new List<Feature>();
116 var jsonFeatures = JsonConvert.DeserializeObject<List<JsonFeature>>(File.ReadAllText(filename));
117
Jan Tattermusche003f012015-05-22 09:34:50 -0700118 foreach(var jsonFeature in jsonFeatures)
119 {
Jan Tattermusch121c4d12015-08-31 10:27:27 -0700120 features.Add(new Feature
121 {
122 Name = jsonFeature.name,
123 Location = new Point { Longitude = jsonFeature.location.longitude, Latitude = jsonFeature.location.latitude}
124 });
Jan Tattermusche003f012015-05-22 09:34:50 -0700125 }
126 return features;
127 }
128
129 private class JsonFeature
130 {
131 public string name;
132 public JsonLocation location;
133 }
134
135 private class JsonLocation
136 {
137 public int longitude;
138 public int latitude;
139 }
140 }
141}