public void Should_Encode_WithDefaultPrecison()
        {
            var hasher = new Geohasher();

            var hash = hasher.Encode(52.5174, 13.409);

            Assert.Equal("u33dc0", hash);
        }
        public void Should_Encode_WithGivenPrecision_11()
        {
            var hasher = new Geohasher();

            var hash = hasher.Encode(52.517395, 13.408813, 11);

            Assert.Equal("u33dc07zzzz", hash);
        }
        public async Task FullDump()
        {
            var lastAccess = await Mongo.GetLastDailyStats();

            Response.Headers[HeaderNames.LastModified] = lastAccess.Date.ToUniversalTime().ToString("R");
            Response.Headers[HeaderNames.ContentType]  = "text/csv";
            Response.StatusCode = (int)HttpStatusCode.OK;

            await Response.WriteAsync("Date,TotalMinutesTracked,CentroidGeohash,CentroidLat,CentroidLong,GeohashBoxJSON,LocationCount,VehicleCount,EventCount,SampleCount,DiscardedSampleCount,BoundingBoxDiagonal,MinAtHome,MinAtWork,MinAtSchool,MinAtLocations,MinElsewhere" + Environment.NewLine);

            var cursor = await Mongo.FetchAllDailyStats();

            while (await cursor.MoveNextAsync())
            {
                foreach (var stat in cursor.Current)
                {
                    var centroid = stat.CentroidHash ?? Geohasher.Encode(stat.Centroid.Coordinates.Latitude, stat.Centroid.Coordinates.Longitude, 5);
                    var bbox     = Geohasher.GetBoundingBox(centroid);
                    var polygon  = new Polygon(new LineString[] {
                        new LineString(new IPosition[] {
                            new Position(bbox[0], bbox[2]),
                            new Position(bbox[0], bbox[3]),
                            new Position(bbox[1], bbox[3]),
                            new Position(bbox[1], bbox[2]),
                            new Position(bbox[0], bbox[2])
                        })
                    });

                    await Response.WriteAsync(string.Join(",",
                                                          stat.Date.ToString("yyyy-MM-dd"),
                                                          stat.TotalMinutesTracked,
                                                          centroid,
                                                          stat.Centroid.Coordinates.Latitude.ToString("F5"),
                                                          stat.Centroid.Coordinates.Longitude.ToString("F5"),
                                                          "\"" + JsonConvert.SerializeObject(polygon).Replace("\"", "\"\"") + "\"",
                                                          stat.LocationCount,
                                                          stat.VehicleCount,
                                                          stat.EventCount,
                                                          stat.SampleCount,
                                                          stat.DiscardedSampleCount,
                                                          stat.BoundingBoxDiagonal.ToString("F2"),
                                                          stat.LocationTracking.MinutesAtHome,
                                                          stat.LocationTracking.MinutesAtWork,
                                                          stat.LocationTracking.MinutesAtSchool,
                                                          stat.LocationTracking.MinutesAtOtherKnownLocations,
                                                          stat.LocationTracking.MinutesElsewhere
                                                          ) + Environment.NewLine);
                }
            }
        }
Example #4
0
        public bool Create(TrackingBinding binding)
        {
            using (var db = GetMainContext())
            {
                var geohasher = new Geohasher();

                var tracking = binding.ToEntity();
                tracking.Geohash = geohasher.Encode((double)binding.Latitude, (double)binding.Longitude, 9);
                tracking.UserId  = UserId;

                db.Trackings.Add(tracking);
                db.SaveChanges();

                return(true);
            }
        }
Example #5
0
        public async Task Create(IEnumerable <TrackingBinding> binding)
        {
            var geohasher = new Geohasher();

            using (var db = GetMainContext())
            {
                await db.Trackings.AddRangeAsync(binding.Select(x =>
                {
                    var entity     = x.ToEntity();
                    entity.Geohash = geohasher.Encode((double)x.Latitude, (double)x.Longitude, 9);
                    entity.UserId  = UserId;
                    return(entity);
                }));

                await db.SaveChangesAsync();
            }
        }
Example #6
0
        private async Task <IEnumerable <Location> > GetLocationsByCoordinates(double latitude, double longitude, double?radiusInKm, string iso3Code)
        {
            string geohash                    = null;
            var    locationsCount             = 0;
            IEnumerable <Location> locations  = null;
            const int locationsLimit          = 20;
            var       shouldSearchIteratively = !radiusInKm.HasValue;

            radiusInKm = radiusInKm ?? 8;

            //Try to get locations in radius if there are not locations in this radius,
            //increase it and try again until you find location or the radius exceeds the maximum allowed
            do
            {
                var geohashLevel = DistanceHelper.GetGeohashLevelByRadius(radiusInKm.Value);
                geohash = _geohasher.Encode(latitude, longitude, precision: geohashLevel);

                locations = await _locationRepository.GetLocationsByFilterAsync(geohash, iso3Code);

                if (locations.Any())
                {
                    var locationDistances = new Dictionary <Guid, double>();
                    foreach (var location in locations)
                    {
                        var distance = DistanceHelper.GetDistanceInKmBetweenTwoPoints(
                            latitude, longitude, location.Latitude.Value,
                            location.Longitude.Value);

                        if (distance <= radiusInKm.Value)
                        {
                            locationDistances.Add(location.Id, distance);
                        }
                    }

                    //Additional precise filtering
                    locations = locations
                                .Where(l => locationDistances.ContainsKey(l.Id))
                                .OrderBy(l => locationDistances[l.Id]);
                }

                locationsCount = locations.Count();
                radiusInKm    *= 2;
            } while (shouldSearchIteratively && radiusInKm.Value <= MaxRadiusInKm && locationsCount < locationsLimit);

            return(locations);
        }
Example #7
0
        public async Task <Guid[]> GetPartnerIdsInRadiusByCoordinatesAsync(double radiusInKm, double longitude, double latitude)
        {
            if (latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180)
            {
                throw new ArgumentException("Invalid argument value for get near partners request");
            }

            var geohashLevel = DistanceHelper.GetGeohashLevelByRadius(radiusInKm);
            var geohash      = _geohasher.Encode(latitude, longitude, precision: geohashLevel);
            var locations    = await _locationRepository.GetLocationsByGeohashAsync(geohash);

            var result = locations
                         .Where(l => DistanceHelper.GetDistanceInKmBetweenTwoPoints(latitude, longitude, l.Latitude.Value,
                                                                                    l.Longitude.Value) <= radiusInKm)
                         .Select(l => l.PartnerId)
                         .Distinct()
                         .ToArray();

            return(result);
        }
Example #8
0
 public string Encode(Point p, int precision = 6)
 {
     return(hasher.Encode(p.Y, p.X, precision));
 }
Example #9
0
 private void SetGeohash(Location location)
 {
     location.Geohash = IsCoordinatesDetermined(location)
         ? _geohasher.Encode(location.Latitude.Value, location.Longitude.Value, precision: 9)
         : null;
 }
Example #10
0
        public string Encode(Geometry geom, int precision)
        {
            var hash = GeohashIndexUtil.LongestCommonPrefix(geom.Envelope.Coordinates.Select(coord => hasher.Encode(coord.Y, coord.X, precision)).ToArray());

            return(hash);
        }
        public void Should_Throw_Given_Incorrect_Lng()
        {
            var hasher = new Geohasher();

            Assert.Throws <ArgumentException>(() => hasher.Encode(52.517395, 183.408813, 12));
        }