private static int Wind(GeographicalPoint location, Edge edge, Position position) { if (edge.RelativePositionOf(location) != position) { return(0); } return(1); }
public bool LocationInRange(GeographicalPoint location, Orientation orientation) { if (orientation == Orientation.Ascending) { return(_endPoint.Latitude > location.Latitude); } return(_endPoint.Latitude <= location.Latitude); }
private static int DescendingIntersection(GeographicalPoint location, Edge edge) { if (edge.AscendingRelativeTo(location)) { return(0); } if (!edge.LocationInRange(location, Orientation.Descending)) { return(0); } return(Wind(location, edge, Position.Right)); }
public bool Contains(GeographicalPoint location) { GeographicalPoint[] polygonPointsWithClosure = PolygonPointsWithClosure(); int windingNumber = 0; for (int pointIndex = 0; pointIndex < polygonPointsWithClosure.Length - 1; pointIndex++) { Edge edge = new Edge(polygonPointsWithClosure[pointIndex], polygonPointsWithClosure[pointIndex + 1]); windingNumber += AscendingIntersection(location, edge); windingNumber -= DescendingIntersection(location, edge); } return(windingNumber != 0); }
public Position RelativePositionOf(GeographicalPoint location) { double positionCalculation = (_endPoint.Longitude - _startPoint.Longitude) * (location.Latitude - _startPoint.Latitude) - (location.Longitude - _startPoint.Longitude) * (_endPoint.Latitude - _startPoint.Latitude); if (positionCalculation > 0) { return(Position.Left); } if (positionCalculation < 0) { return(Position.Right); } return(Position.Center); }
//public string GetAddress(double latitude, double longitude) //{ // string address = string.Empty; // try // { // Geocoder geocoder = new Geocoder(this); // IList<Address> addressList = geocoder.GetFromLocation(latitude, longitude, 1); // Address addressCurrent = addressList.FirstOrDefault(); // if (addressCurrent != null) // { // StringBuilder deviceAddress = new StringBuilder(); // for (int i = 0; i <= addressCurrent.MaxAddressLineIndex; i++) // { // if (addressCurrent.GetAddressLine(i).Length > 0) // { // deviceAddress.Append(addressCurrent.GetAddressLine(i)).AppendLine(","); // } // } // address = deviceAddress.ToString().Replace(System.Environment.NewLine, string.Empty).TrimEnd(','); // } // } // catch // { // address = "Can't get address. Are you online?"; // } // return address; //} void OnLocationResult(object sender, Location location) { try { RunOnUiThread(() => { Utils.Location = location; if (Utils.Location == null) { _txtLocation = "Unable to determine your location."; } else { // First time in, set some defaults $$$ if (Utils.StartLocation.Latitude == 0 && Utils.StartLocation.Longitude == 0) { ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); Utils.StartLocation = Utils.Location; ISharedPreferencesEditor editor = prefs.Edit(); editor.PutFloat("StartLatitude", (float)Utils.StartLocation.Latitude); editor.PutFloat("StartLongitude", (float)Utils.StartLocation.Longitude); editor.Apply(); _txtStartAddress = Utils.GetAddress(_instance); } _txtLocation = String.Format("{0:0.####},{1:0.####}", Utils.StartLocation.Latitude, Utils.StartLocation.Longitude); // This code measures distance from the origin var coord1 = new LatLng(Utils.StartLocation.Latitude, Utils.StartLocation.Longitude); var coord2 = new LatLng(Utils.Location.Latitude, Utils.Location.Longitude); Utils.HaversineDistance(coord1, coord2, Utils.DistanceUnit.Kilometers); //Utils.Distance = 10; // Hack _txtDistance.Text = Utils.Distance.ToString("#.###"); if (Utils.Distance == 0) { _txtRemarks.Text = string.Format("You are at your origin, "); } else { _txtRemarks.Text = "You are " + Utils.Distance.ToString("0.###") + " km from your origin, "; } _txtRemarks.Text += _txtStartAddress; Utils.PreviousLocation = location; // This code measures distance wrt rules foreach (Rule rule in Utils.Rules) { if (rule.LocationType == (int)Utils.LocationType.Radius) { coord1 = new LatLng((double)rule.MapLatitude, (double)rule.MapLongitude); rule.Distance = Utils.HaversineDistance(coord1, coord2); } else if (rule.LocationType == (int)Utils.LocationType.Polygon && rule.Polygon.Length > 0) { string payload = new string(rule.Polygon.Where(c => !char.IsControl(c)).ToArray()); int startIndex = payload.IndexOf("<coordinates>"); int endIndex = payload.IndexOf("</coordinates>"); payload = payload.Substring(startIndex + 13, endIndex - startIndex - 13) .Replace(",0", ",") .Replace(" ", "") .Trim(','); double[] doubles = Array.ConvertAll(payload.Split(','), double.Parse); List <GeographicalPoint> points = new List <GeographicalPoint>(); for (int i = 0; i <= doubles.Length - 1; i += 2) { points.Add(new GeographicalPoint(doubles[i + 1], doubles[i])); } Polygon polygon = new Polygon(points); GeographicalPoint point = new GeographicalPoint(coord2.Latitude, coord2.Longitude); // Negative int.MaxValue distance in a polygon rule means we're inside the shape $$$ we need code for nested polygons // Otherwise we point it at space // That means [for now] polygons get processed before radius rules // We'll move to a system where all the trigered rules are processed rule.Distance = polygon.Contains(point) ? int.MaxValue * -1 : int.MaxValue; } else { rule.Distance = int.MaxValue; } } // Reduce the rules to undismissed ones Utils.Rules = Utils.Rules.Where(x => !x.Dismissed).ToList(); if (Utils.Rules.Count > 0) { Utils.Rules = Utils.Rules.OrderBy(x => x.Distance - x.RadiusK).ToList(); if (Utils.Rules[0].LocationType == (int)Utils.LocationType.Radius) { decimal realDistance = Utils.Rules[0].Distance - Utils.Rules[0].RadiusK; if (realDistance > 0) { _txtRemarks.Text += Environment.NewLine + Environment.NewLine + "You are " + realDistance.ToString("0") + " km from the closest exclusion zone, " + Utils.Rules[0].LocationName + ". Coronalert will continue to monitor your position, and tell you when you are within " + Utils.Rules[0].WarnK.ToString("0.###") + " km of its " + Utils.Rules[0].RadiusK.ToString("0.###") + " km radius."; } } } ShowState(); } }); } catch (Exception ex) { Toast.MakeText(this, ex.Message, ToastLength.Long).Show(); } }
public Edge(GeographicalPoint startPoint, GeographicalPoint endPoint) { _startPoint = startPoint; _endPoint = endPoint; }
public bool AscendingRelativeTo(GeographicalPoint location) { return(_startPoint.Latitude <= location.Latitude); }