OLD | NEW |
(Empty) | |
| 1 // 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 |
| 30 using Newtonsoft.Json; |
| 31 using Newtonsoft.Json.Linq; |
| 32 using System; |
| 33 using System.Collections.Generic; |
| 34 using System.IO; |
| 35 using System.Linq; |
| 36 using System.Text; |
| 37 using System.Threading.Tasks; |
| 38 |
| 39 namespace Routeguide |
| 40 { |
| 41 /// <summary> |
| 42 /// Utility methods for the route guide example. |
| 43 /// </summary> |
| 44 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> |
| 53 public static bool Exists(this Feature feature) |
| 54 { |
| 55 return feature != null && (feature.Name.Length != 0); |
| 56 } |
| 57 |
| 58 public static double GetLatitude(this Point point) |
| 59 { |
| 60 return point.Latitude / CoordFactor; |
| 61 } |
| 62 |
| 63 public static double GetLongitude(this Point point) |
| 64 { |
| 65 return point.Longitude / CoordFactor; |
| 66 } |
| 67 |
| 68 /// <summary> |
| 69 /// Calculate the distance between two points using the "haversine" form
ula. |
| 70 /// This code was taken from http://www.movable-type.co.uk/scripts/latlo
ng.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.Co
s(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> |
| 111 /// 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 |
| 118 foreach(var jsonFeature in jsonFeatures) |
| 119 { |
| 120 features.Add(new Feature |
| 121 { |
| 122 Name = jsonFeature.name, |
| 123 Location = new Point { Longitude = jsonFeature.location.long
itude, Latitude = jsonFeature.location.latitude} |
| 124 }); |
| 125 } |
| 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 } |
OLD | NEW |