Exemplo n.º 1
0
        /// <summary>
        /// Load a geofence from the provided INI file path
        /// </summary>
        /// <param name="filePath">File path of geofence to load</param>
        /// <returns>Returns a geofence object of the file path</returns>
        private static List <GeofenceItem> FromIniFile(string filePath)
        {
            var list = new List <GeofenceItem>();
            // Read all lines of the file and remove all null, empty, or whitespace lines
            var    lines     = File.ReadAllLines(filePath).Where(line => !string.IsNullOrWhiteSpace(line)).ToArray();
            var    locations = new List <Location>();
            string name      = "";

            // Loop through each new line of the file
            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];

                // If the line starts with '[' then parse the Geofence name
                if (line.StartsWith("[", StringComparison.Ordinal))
                {
                    name = line.TrimStart('[').TrimEnd(']');
                    continue;
                }

                // Trim off any extra whitespace and split the line by a comma ','
                var coordinates = line.Trim('\0').Split(',');
                if (!double.TryParse(coordinates[0], out var lat))
                {
                    continue;
                }

                if (!double.TryParse(coordinates[1], out var lng))
                {
                    continue;
                }

                locations.Add(new Location(lat, lng));

                // If we have reached the end of the file or the start of another
                // geofence, add the current to the list of geofences
                var isEnd = i == lines.Length - 1 || lines[i + 1].StartsWith("[", StringComparison.Ordinal);

                if (isEnd)
                {
                    var geofence = new GeofenceItem(name, locations)
                    {
                        Filename = Path.GetFileName(filePath)
                    };

                    list.Add(geofence);
                    name      = "";
                    locations = new List <Location>();
                }
            }

            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load a geofence from the provided file path
        /// </summary>
        /// <param name="filePath">File path of geofence to load</param>
        /// <returns>Returns a geofence object of the file path</returns>
        public static List <GeofenceItem> FromFile(string filePath)
        {
            var list     = new List <GeofenceItem>();
            var lines    = File.ReadAllLines(filePath);
            var geofence = new GeofenceItem();

            // Loop through each new line of the file
            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                // If the line starts with '[' then parse the Geofence name
                if (line.StartsWith("[", StringComparison.Ordinal))
                {
                    geofence = new GeofenceItem(line.TrimStart('[').TrimEnd(']'));
                    continue;
                }

                // Trim off any extra whitespace and split the line by a comma ','
                var coordinates = line.Trim('\0').Split(',');
                if (!double.TryParse(coordinates[0], out var lat))
                {
                    continue;
                }

                if (!double.TryParse(coordinates[1], out var lng))
                {
                    continue;
                }

                geofence.Polygons.Add(new Location(lat, lng));

                // If we have reached the end of the file or the start of another
                // geofence, add the current to the list of geofences
                var isEnd = i == lines.Length - 1 || lines[i + 1].StartsWith("[", StringComparison.Ordinal);
                if (isEnd)
                {
                    list.Add(geofence);
                }
            }

            return(list);
        }
Exemplo n.º 3
0
        public static List <GeofenceItem> LoadGeofences(string geofencesFolder)
        {
            var geofences = new List <GeofenceItem>();

            foreach (var file in Directory.EnumerateFiles(geofencesFolder))
            {
                try
                {
                    var fileGeofences = GeofenceItem.FromFile(file);

                    geofences.AddRange(fileGeofences);
                }
                catch (Exception ex)
                {
                    _logger.Error($"Could not load Geofence file {file}:");
                    _logger.Error(ex);
                }
            }

            return(geofences);
        }
Exemplo n.º 4
0
        public static List <GeofenceItem> FromFile(string filePath)
        {
            var list     = new List <GeofenceItem>();
            var lines    = File.ReadAllLines(filePath);
            var geofence = new GeofenceItem();

            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                if (line.StartsWith("[", StringComparison.Ordinal))
                {
                    geofence = new GeofenceItem(line.TrimStart('[').TrimEnd(']'));
                    continue;
                }

                var coordinates = line.Replace(" ", null).Split(',');
                if (!double.TryParse(coordinates[0], out var lat))
                {
                    continue;
                }

                if (!double.TryParse(coordinates[1], out var lng))
                {
                    continue;
                }

                geofence.Polygons.Add(new Location(lat, lng));

                var isEnd = i == lines.Length - 1 || lines[i + 1].StartsWith("[", StringComparison.Ordinal);
                if (isEnd)
                {
                    list.Add(geofence);
                }
            }

            return(list);
        }
Exemplo n.º 5
0
        //private static readonly IEventLogger _logger = EventLogger.GetLogger();

        //public bool Contains(GeofenceItem geofence, Location point)
        //{
        //    //Credits: https://stackoverflow.com/a/7739297/2313836

        //    var contains = false;
        //    for (int i = 0, j = geofence.Polygons.Count - 1; i < geofence.Polygons.Count; j = i++)
        //    {
        //        if ((((geofence.Polygons[i].Latitude <= point.Latitude) && (point.Latitude < geofence.Polygons[j].Latitude))
        //                || ((geofence.Polygons[j].Latitude <= point.Latitude) && (point.Latitude < geofence.Polygons[i].Latitude)))
        //                && (point.Longitude < (geofence.Polygons[j].Longitude - geofence.Polygons[i].Longitude) * (point.Latitude - geofence.Polygons[i].Latitude)
        //                    / (geofence.Polygons[j].Latitude - geofence.Polygons[i].Latitude) + geofence.Polygons[i].Longitude))
        //        {
        //            contains = !contains;
        //        }
        //    }
        //    return contains;
        //}

        //public bool InPolygon(GeofenceItem geofence, Location point)
        //{
        //    var poly = geofence.Polygons;
        //    var c = false;
        //    for (int i = -1, l = poly.Count, j = l - 1; ++i < l; j = i)
        //    {
        //        c = ((poly[i].Latitude <= point.Latitude && point.Latitude < poly[j].Latitude) || (poly[j].Latitude <= point.Latitude && point.Latitude < poly[i].Latitude)) &&
        //            (point.Longitude < (poly[j].Longitude - poly[i].Longitude) * (point.Latitude - poly[i].Latitude) / (poly[j].Latitude - poly[i].Latitude) + poly[i].Longitude) &&
        //            (c = !c);
        //    }
        //    return c;
        //}
        public bool Contains(GeofenceItem geofence, Location point)
        {
            var numOfPoints          = geofence.Polygons.Count;
            var lats                 = geofence.Polygons.Select(x => x.Latitude).ToList();
            var lngs                 = geofence.Polygons.Select(x => x.Longitude).ToList();
            var polygonContainsPoint = false;

            for (int node = 0, altNode = (numOfPoints - 1); node < numOfPoints; altNode = node++)
            {
                if ((lngs[node] > point.Longitude != (lngs[altNode] > point.Longitude)) &&
                    (point.Latitude < (lats[altNode] - lats[node])
                     * (point.Longitude - lngs[node])
                     / (lngs[altNode] - lngs[node])
                     + lats[node]
                    )
                    )
                {
                    polygonContainsPoint = !polygonContainsPoint;
                }
            }
            lats.Clear();
            lngs.Clear();
            return(polygonContainsPoint);
        }