示例#1
0
        private static bool CheckInside(Location loc1, Location loc2, Circle circle)
        {
            HaversineFormula haversineFormula = new HaversineFormula();

            return(haversineFormula.Dist(
                       loc1, loc2, HaversineFormula.TypeOfDistance.Kilometres) < circle.Rad);
        }
示例#2
0
        public ActionResult Index()
        {
            string resultFormula;
            string info;
            string inOutCircle;
            string outcircleAlt;

            Location loc1 = new Location(52.343348, -8.970760); //End point of section of local field in home village on edge of field boundary at one side of the field
            Location loc2 = new Location(52.343095, -8.972363); //  Middle point of section of local field in home village
            Location loc3 = new Location(52.350095, -8.962363); //Asset outside geofence test

            Circle circleGeofence = new Circle(52.343095, -8.972363, 0.2);

            Debug.Write("The centre of the circle formed is at (" + circleGeofence.XPoint + " ," + circleGeofence.YPoint + ")");
            Debug.Write("\nRadius of circle geofence is " + circleGeofence.Rad);

            HaversineFormula formula = new HaversineFormula();
            double           res     = formula.Dist(loc1, loc2, HaversineFormula.TypeOfDistance.Kilometres);

            resultFormula = "Result of Haversine Formula based on distance between 2 points of field at home village (Notably middle of field whose real-world coordinates are (" + loc1.Lat + ", " + loc1.Lon + ") " +
                            "and point at edge of field boundary whose real-world coordinates (" + loc2.Lat + ", " + loc2.Lon + ")) is " + res + " km. ";
            info = "\nValues in Haversine Formula Equation based on real-world coordinates seen at starting point of map where custom-made marker is situated with custom made circle representing a geofence.";

            bool checker = CheckInside(loc1, loc2, circleGeofence);

            if (checker)
            {
                inOutCircle = "\n1st Check\nThe predefined cattle asset point before the addition of markers for testing which is not the point at the middle of the field represented as (" + loc1.Lat + ", " + loc1.Lon + ") " + " is inside the geofence.";
            }
            else
            {
                inOutCircle = "\n1st Check\nThe predefined cattle asset point before the addition of markers for testing which is not the point at the middle of the field represented as (" + loc1.Lat + ", " + loc1.Lon + ") " + " is outside the geofence";
            }

            bool checkOut = CheckInside(loc2, loc3, circleGeofence);

            if (checkOut)
            {
                outcircleAlt = "\n\n2nd Check\nAnother predefined cattle asset point before the addition of markers for testing which is not the point at the middle of the field represented as (" + loc3.Lat + ", " + loc3.Lon + ") " + " is inside the geofence.";
            }
            else
            {
                outcircleAlt = "\n\n2nd Check\nAnother predefined cattle asset point before the addition of markers for testing which is not the point at the middle of the field represented as (" + loc3.Lat + ", " + loc3.Lon + ") " + " is outside the geofence";
            }

            ViewBag.Message = resultFormula + info + inOutCircle + outcircleAlt;
            return(View());
        }
示例#3
0
        public void HaversineFormula()
        {
            Location loc = new Location(48.54, 50.56);

            Location location = new Location(55.55, 52.80);

            HaversineFormula haversine = new HaversineFormula();

            double res = haversine.Dist(loc, location, TypeOfDistance.Kilometres);

            HaversineFormula haversine1 = new HaversineFormula();

            double res1 = haversine.Dist(loc, location, TypeOfDistance.Kilometres);


            Assert.AreEqual(res, res1);
        }
        public IHttpActionResult UpdateVehiclePosition()
        {
            string data           = string.Empty;
            var    linesWithBuses = unitOfWork.TransportLine.GetAll().Where(x => x.Vehicles.Count > 0 && x.TransportLineID == "64A");

            foreach (var lwb in linesWithBuses)
            {
                var    vehiclesOnLine   = lwb.Vehicles.ToList();
                var    path             = lwb.LinePoints.ToList();
                var    pathCountEnd     = path.Count - 1;
                double distanceTraveled = 10.0; //m
                for (int i = 0; i < vehiclesOnLine.Count; i++)
                {
                    double bearing = 0.0;
                    if (vehiclesOnLine[i].X == path[path.Count - 1].X && vehiclesOnLine[i].Y == path[path.Count - 1].Y)
                    {
                        vehiclesOnLine[i].X = path[0].X;
                        vehiclesOnLine[i].Y = path[0].Y;
                    }

                    (double, double)move = (path[0].X, path[0].Y);
                    for (int j = 0; j < path.Count - 1; j++)
                    {
                        var initVehLatitude  = path[j].X;
                        var initVehLongitude = path[j].Y;
                        distanceTraveled += 0.05;
                        bearing           = HaversineFormula.Bearing(move.Item1, move.Item2, path[j + 1].X, path[j + 1].Y);
                        move              = HaversineFormula.CalculateNewSetOfPoints(initVehLatitude, initVehLongitude, bearing, distanceTraveled);
                        data              = $"{vehiclesOnLine[i].VehicleID},{move.Item1},{move.Item2},{vehiclesOnLine[i].TransportLineID}|";

                        if (data.EndsWith("|"))
                        {
                            data = data.Substring(0, data.Length - 1);
                        }

                        var hubContext = GlobalHost.ConnectionManager.GetHubContext <VehicleHub>();
                        hubContext.Clients.All.newPositions(data);
                        Thread.Sleep(2000);
                    }
                }
            }
            return(Ok());
        }
 private void ReturnMeasurementValues()
 {
     ModifiedPythagorousResultTxtBx.Text = ModifiedPythagoras.Measure(StartLocationInDegrees, EndLocationInDegrees).ToString();
     GreaterCircleResultTxtBx.Text       = GreaterCircle.Measure(StartLocationInRadian, EndLocationInRadian, EarthRadius).ToString();
     HaversineFormulaResultTxtBx.Text    = HaversineFormula.Measure(StartLocationInRadian, EndLocationInRadian, EarthRadius).ToString();
 }
示例#6
0
        private async Task GetGeofenceStateChangedReports(Geoposition pos)
        {
            _geofenceBackgroundEvents.Clear();
            FillEventCollectionWithExistingEvents();

            GeofenceMonitor monitor      = GeofenceMonitor.Current;
            Geoposition     posLastKnown = monitor.LastKnownGeoposition;

            Windows.Globalization.Calendar calendar = new Windows.Globalization.Calendar();
            Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatterLongTime;
            formatterLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "hr-HR" }, "HR", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour);

            bool eventOfInterest = true;

            // NOTE TO DEVELOPER:
            // Registered geofence events can be filtered out if the
            // geofence event time is stale.
            var eventDateTime = pos.Coordinate.Timestamp;

            calendar.SetToNow();
            var nowDateTime  = calendar.GetDateTime();
            var diffTimeSpan = nowDateTime - eventDateTime;

            var deltaInSeconds = diffTimeSpan.TotalSeconds;

            if (deltaInSeconds > 120)
            {
                eventOfInterest = false;
            }

            if (eventOfInterest)
            {
                // NOTE TO DEVELOPER:
                // This event can be filtered out if the
                // geofence event location is too far away.
                var distance = 0.0d;

                if ((posLastKnown.Coordinate.Point.Position.Latitude != pos.Coordinate.Point.Position.Latitude) ||
                    (posLastKnown.Coordinate.Point.Position.Longitude != pos.Coordinate.Point.Position.Longitude))
                {
                    distance = new HaversineFormula().Distance(posLastKnown.Coordinate.Point.Position, pos.Coordinate.Point.Position, HaversineFormula.DistanceType.Kilometers);

                    // NOTE TO DEVELOPER:
                    // Use an algorithm like the Haversine formula or Vincenty's formulae to determine
                    // the distance between the current location (pos.Coordinate)
                    // and the location of the geofence event (latitudeEvent/longitudeEvent).
                    // If too far apart set eventOfInterest to false to
                    // filter the event out.
                }

                if (eventOfInterest)
                {
                    string geofenceItemEvent   = null;
                    int    numEventsOfInterest = 0;

                    // Retrieve a vector of state change reports
                    var reports = GeofenceMonitor.Current.ReadReports();

                    foreach (var report in reports)
                    {
                        GeofenceState state = report.NewState;
                        geofenceItemEvent = report.Geofence.Id + " " + formatterLongTime.Format(eventDateTime);

                        if (state == GeofenceState.Removed)
                        {
                            GeofenceRemovalReason reason = report.RemovalReason;
                            if (reason == GeofenceRemovalReason.Expired)
                            {
                                geofenceItemEvent += " (Removed/Expired)";
                            }
                            else if (reason == GeofenceRemovalReason.Used)
                            {
                                geofenceItemEvent += " (Removed/Used)";
                            }
                        }
                        else if (state == GeofenceState.Entered)
                        {
                            geofenceItemEvent += " (Entered)";
                        }
                        else if (state == GeofenceState.Exited)
                        {
                            geofenceItemEvent += " (Exited)";
                        }

                        geofenceItemEvent += $" - {deltaInSeconds}";
                        geofenceItemEvent += $"\nDistance - {distance}";

                        AddGeofenceEvent(geofenceItemEvent);
                        ++numEventsOfInterest;

                        var userLocation = new UserLocation()
                        {
                            Latitude   = report.Geoposition.Coordinate.Point.Position.Latitude,
                            Longitude  = report.Geoposition.Coordinate.Point.Position.Longitude,
                            Name       = report.Geofence.Id,
                            UpdatedUtc = eventDateTime.UtcDateTime,
                            Status     = (LocationStatus)(int)state
                        };

                        await UploadDataToCloud(userLocation);
                    }

                    if (eventOfInterest == true && numEventsOfInterest != 0)
                    {
                        SaveExistingEvents();
                        // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates.
                        //DoToast(numEventsOfInterest, geofenceItemEvent);
                    }
                }
            }
        }