public CoordinateView(string title, Models.Coordinate coordinate) { Debug.Assert(title != null); Debug.Assert(coordinate != null); this.title = title; this.coordinate = coordinate; }
/// <summary> /// By Haversine https://en.wikipedia.org/wiki/Haversine_formula /// </summary> /// <returns></returns> public static double GetDistance(double srcLng, double srcLat, double destLng, double destLat, UnitOfLength unitOfLength) { var src = new Models.Coordinate(srcLng, srcLat); var dest = new Models.Coordinate(destLng, destLat); return(src.DistanceToByHaversine(dest, unitOfLength)); }
/// <summary> /// Get Bot Right Coordinate of square (out bound of circle) corner /// </summary> /// <param name="src"> </param> /// <param name="radiusKilometer"></param> /// <returns></returns> public static Models.Coordinate GetBotRightOfSquare(this Models.Coordinate src, double radiusKilometer) { var hypotenuseLength = GetHypotenuseLength(radiusKilometer); var botRight = GetDerivedPosition(src, hypotenuseLength, 135); return(botRight); }
/// <summary> /// Get Top Left Coordinate of square (out bound of circle) corner /// </summary> /// <param name="src"> </param> /// <param name="radiusKilometer"></param> /// <returns></returns> public static Models.Coordinate GetTopLeftOfSquare(this Models.Coordinate src, double radiusKilometer) { var hypotenuseLength = GetHypotenuseLength(radiusKilometer); var topLeft = GetDerivedPosition(src, hypotenuseLength, 315); return(topLeft); }
public static List <DirectionSteps> GetDirections( Models.Coordinate origin, Models.Coordinate destination, List <Models.Coordinate> waypoints, bool avoidHighways = false, bool avoidTolls = false, int unitSystem = 1, string travelMode = "DRIVING", string googleApiKey = "") { string originFormat = $"{origin.Latitude},{origin.Longitude}"; string destinationFormat = $"{destination.Latitude},{destination.Longitude}"; string waypointsFormat = string.Join("|", waypoints.Select(x => $"{x.Latitude},{x.Longitude}")); var requestUrl = $"https://maps.googleapis.com/maps/api/directions/xml?avoidHighways={avoidHighways}&avoidTolls={avoidTolls}&unitSystem={unitSystem}&travelMode={travelMode}&origin={originFormat}&destination={destinationFormat}&waypoints={waypointsFormat}&key={googleApiKey}"; try { var client = new WebClient { Encoding = Encoding.UTF8 }; var result = client.DownloadString(requestUrl); return(ParseDirectionResults(result, origin, destination, waypoints, avoidHighways, avoidTolls, unitSystem, travelMode)); } catch (Exception) { return(null); } }
private void NextChunk() { int maxSize = 10; ChunkNode = OkChunkNode; if (ChunkNode < Ways.Count) { var wayArrChunk = new List <Models.Coordinate>(); for (var i = 0; i < maxSize && i + ChunkNode < Ways.Count; ++i) { wayArrChunk.Add(Ways[ChunkNode + i]); } Models.Coordinate origin = wayArrChunk[0]; Models.Coordinate destination = wayArrChunk[wayArrChunk.Count - 1]; var wayArrChunk2 = new List <Models.Coordinate>(); for (var i = 1; i < wayArrChunk.Count - 1; i++) { wayArrChunk2.Add(wayArrChunk[i]); } ChunkNode += maxSize; if (ChunkNode < Ways.Count - 1) { ChunkNode--; } List <DirectionSteps> directionSteps = GMapHelper.GetDirections(origin, destination, wayArrChunk2); LegsTemp.AddRange(directionSteps); OkChunkNode = ChunkNode; NextChunk(); } else { DurationsTemp.AddRange(LegsTemp.Select(x => x.TotalDuration)); DistancesTemp.AddRange(LegsTemp.Select(x => x.TotalDistance)); CalculateTrip(); } }
public static HttpResponseMessage RideLocations(VeoRideClient Client, Models.Coordinate Coordinate) { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, $"https://manhattan-host.veoride.com:8444/api/v2/customers/vehicles?lat={Coordinate.Lat}&lng={Coordinate.Long}"); request.Headers.TryAddWithoutValidation("accept", "*/*"); request.Headers.TryAddWithoutValidation("content-type", "application/json"); request.Headers.TryAddWithoutValidation("connection", "keep-alive"); request.Headers.TryAddWithoutValidation("accept-language", "en-us"); request.Headers.TryAddWithoutValidation("accept-encoding", "br, gzip, deflate"); request.Headers.TryAddWithoutValidation("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1"); request.Headers.TryAddWithoutValidation("host", "manhattan-host.veoride.com:8444"); request.Headers.TryAddWithoutValidation("authorization", "Bearer " + Client.GetAuthToken()); return(Client.GetResponse(request)); }
/// <summary> /// By Haversine https://en.wikipedia.org/wiki/Haversine_formula /// </summary> /// <returns></returns> public static double DistanceToByHaversine(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var dLat = (dest.Latitude - src.Latitude) * CoordinateConst.DegreesToRadians; var dLon = (dest.Longitude - src.Longitude) * CoordinateConst.DegreesToRadians; var a = Math.Pow(Math.Sin(dLat / 2), 2) + Math.Cos(src.Latitude * CoordinateConst.DegreesToRadians) * Math.Cos(dest.Latitude * CoordinateConst.DegreesToRadians) * Math.Pow(Math.Sin(dLon / 2), 2); // central angle, aka arc segment angular distance var centralAngle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); var dist = CoordinateConst.EarthRadiusMile * centralAngle; return(unitOfLength.ConvertFromMiles(dist)); }
private void Remove(IMoveController moveController, ColocateCoordinateView colocateCoordinateView) { Error error = Error.NOT_ERROR; do { origin = colocateCoordinateView.GetCoordinate(); error = moveController.ValidateOrigin(origin); if (error != Error.NOT_ERROR) { io.Writeln("" + error); } } while (error != Error.NOT_ERROR); moveController.Remove(origin); }
/// <summary> /// By Geographical distance http://en.wikipedia.org/wiki/Geographical_distance /// </summary> public static double DistanceToByGeo(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var radLatSrc = src.Latitude * CoordinateConst.DegreesToRadians; var radLatDest = dest.Latitude * CoordinateConst.DegreesToRadians; var dLat = radLatDest - radLatSrc; var dLon = (dest.Longitude - src.Longitude) * CoordinateConst.DegreesToRadians; var a = dLon * Math.Cos((radLatSrc + radLatDest) / 2); // central angle, aka arc segment angular distance var centralAngle = Math.Sqrt(a * a + dLat * dLat); // great-circle (orthodromic) distance on Earth between 2 points var dist = CoordinateConst.EarthRadiusMile * centralAngle; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Spherical law of cosines http://en.wikipedia.org/wiki/Spherical_law_of_cosines /// </summary> public static double DistanceTo(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var theta = src.Longitude - dest.Longitude; var thetaRad = theta * CoordinateConst.DegreesToRadians; var targetRad = dest.Latitude * CoordinateConst.DegreesToRadians; var baseRad = src.Latitude * CoordinateConst.DegreesToRadians; var dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); // calculate to earth radius by miles dist = dist * CoordinateConst.EarthRadiusMile; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// Calculates the end-point from a given source at a given range (kilometers) and /// bearing (degrees). This methods uses simple geometry equations to calculate the end-point. /// </summary> /// <param name="src"> Point of origin </param> /// <param name="radiusKm"> Radius/Range in Kilometers </param> /// <param name="bearing"> Bearing in degrees from 0 to 360 </param> /// <returns> End-point from the source given the desired range and bearing. </returns> public static Models.Coordinate GetDerivedPosition(this Models.Coordinate src, double radiusKm, double bearing) { var latSrc = src.Latitude * CoordinateConst.DegreesToRadians; var lngSrc = src.Longitude * CoordinateConst.DegreesToRadians; var angularDistance = radiusKm / CoordinateConst.EarthRadiusKilometer; var trueCourse = bearing * CoordinateConst.DegreesToRadians; var lat = Math.Asin( Math.Sin(latSrc) * Math.Cos(angularDistance) + Math.Cos(latSrc) * Math.Sin(angularDistance) * Math.Cos(trueCourse)); var dlon = Math.Atan2( Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latSrc), Math.Cos(angularDistance) - Math.Sin(latSrc) * Math.Sin(lat)); var lon = (lngSrc + dlon + Math.PI) % (Math.PI * 2) - Math.PI; var result = new Models.Coordinate(lon * CoordinateConst.RadiansToDegrees, lat * CoordinateConst.RadiansToDegrees); return(result); }
public override Coordinate GetCoordinate() { origin = this.GetCoordinateController().GetOrigin(); this.GetCoordinateController().Accept(this); return(origin); }
/// <summary> /// Distance to Destination Coordinate By Spherical law of cosines /// </summary> /// <param name="src"> </param> /// <param name="dest"></param> /// <returns> UnitOfLength.Kilometer </returns> public static double DistanceTo(this Models.Coordinate src, Models.Coordinate dest) { return(DistanceTo(src, dest, UnitOfLength.Kilometer)); }
/// <summary> /// Distance to Destination Coordinate in Flat (2D) Map /// </summary> /// <param name="src"> </param> /// <param name="dest"></param> /// <returns> Miles </returns> public static double FlatDistanceTo(this Models.Coordinate src, Models.Coordinate dest) { return(Math.Abs(src.Latitude - dest.Latitude) + Math.Abs(src.Longitude - dest.Longitude)); }
public override Coordinate GetCoordinate() { target = this.GetCoordinateController().GetTarget(); this.GetCoordinateController().Accept(this); return(target); }
private static int GetClosestCoordinateIndex(IReadOnlyList <Models.Coordinate> coordinates, Models.Coordinate coordinate) { var result = 0; var minDistance = coordinate.FlatDistanceTo(coordinates[result]); for (int i = 1; i < coordinates.Count; i++) { var distance = coordinate.FlatDistanceTo(coordinates[i]); if (distance < minDistance) { result = i; } } return(result); }
protected void Show(String infix, Models.Coordinate coordinate) { new CoordinateView("La máquina " + infix + " ", coordinate).Write(); io.ReadString(". Pulse enter para continuar"); }
private static List <DirectionSteps> ParseDirectionResults(string result, Models.Coordinate origin, Models.Coordinate destination, List <Models.Coordinate> waypoints, bool avoidHighways, bool avoidTolls, int unitSystem, string travelMode) { var directionStepsList = new List <DirectionSteps>(); var xmlDoc = new XmlDocument { InnerXml = result }; if (xmlDoc.HasChildNodes) { var directionsResponseNode = xmlDoc.SelectSingleNode("DirectionsResponse"); var statusNode = directionsResponseNode?.SelectSingleNode("status"); if (statusNode != null && statusNode.InnerText.Equals("OK")) { var legs = directionsResponseNode.SelectNodes("route/leg"); if (legs == null) { return(directionStepsList); } foreach (XmlNode leg in legs) { int stepCount = 1; var stepNodes = leg.SelectNodes("step"); var steps = (from XmlNode stepNode in stepNodes select new DirectionStep { Index = stepCount++, Distance = Convert.ToDouble(stepNode.SelectSingleNode("distance/value").InnerText, System.Globalization.CultureInfo.InvariantCulture), DistanceText = stepNode.SelectSingleNode("distance/text").InnerText, Duration = Convert.ToDouble(stepNode.SelectSingleNode("duration/value").InnerText, System.Globalization.CultureInfo.InvariantCulture), DurationText = stepNode.SelectSingleNode("duration/text").InnerText, Description = WebUtility.HtmlDecode(stepNode.SelectSingleNode("html_instructions").InnerText) }).ToList(); var directionSteps = new DirectionSteps { OriginPoint = new Models.Coordinate { SequenceNo = -1, Latitude = Convert.ToDouble(leg.SelectSingleNode("start_location/lat").InnerText, System.Globalization.CultureInfo.InvariantCulture), Longitude = Convert.ToDouble(leg.SelectSingleNode("start_location/lng").InnerText, System.Globalization.CultureInfo.InvariantCulture) }, OriginAddress = leg.SelectSingleNode("start_address").InnerText, DestinationPoint = new Models.Coordinate { SequenceNo = -1, Latitude = Convert.ToDouble(leg.SelectSingleNode("end_location/lat").InnerText, System.Globalization.CultureInfo.InvariantCulture), Longitude = Convert.ToDouble(leg.SelectSingleNode("end_location/lng").InnerText, System.Globalization.CultureInfo.InvariantCulture) }, DestinationAddress = leg.SelectSingleNode("end_address").InnerText, TotalDistance = Convert.ToDouble(leg.SelectSingleNode("distance/value").InnerText, System.Globalization.CultureInfo.InvariantCulture), TotalDistanceText = leg.SelectSingleNode("distance/text").InnerText, TotalDuration = Convert.ToDouble(leg.SelectSingleNode("duration/value").InnerText, System.Globalization.CultureInfo.InvariantCulture), TotalDurationText = leg.SelectSingleNode("duration/text").InnerText, Steps = steps }; directionStepsList.Add(directionSteps); } } else if (statusNode != null && statusNode.InnerText.Equals("OVER_QUERY_LIMIT")) { Thread.Sleep(1000); directionStepsList = GetDirections(origin, destination, waypoints, avoidHighways, avoidTolls, unitSystem, travelMode); } else { throw new NotSupportedException(statusNode?.InnerText); } } return(directionStepsList); }