Пример #1
0
        public async Task FindLocationAsync_ValidPointIncludingCountryRegion_ValidLocation()
        {
            IRestRequest request     = null;
            var          serviceMock = new Mock <BingLocations>();

            serviceMock.Setup(zc => zc.ExecuteAsync <Response>(It.IsAny <IRestRequest>()))
            .Callback <IRestRequest>(r => request = r)
            .CallBase();
            var service    = serviceMock.Object;
            var parameters = new FindLocationByPointParameters();

            parameters.Point = GeoPoint.Create(47.64054, -122.12934);
            parameters.IncludeEntityTypes = new[] { IncludeEntityType.CountryRegion };

            var response = await service.FindLocationAsync(parameters);

            serviceMock.Verify(zc => zc.ExecuteAsync <Response>(It.IsAny <IRestRequest>()), Times.Once);
            Assert.That(response, Is.Not.Null);
            Assert.That(response.ResourceSets.Length, Is.GreaterThan(0));
            Assert.That(response.ResourceSets.First().Resources.OfType <Location>().Count(), Is.GreaterThan(0));
            Assert.That(response.ResourceSets.First().Resources.OfType <Location>().First().Name, Is.EqualTo("United States"));
            Assert.That(request, Is.Not.Null);
            Assert.That(request.Method, Is.EqualTo(Method.GET));
            Assert.That(request.Resource, Is.EqualTo("Locations/{Point}"));
            Assert.That(request.Parameters.Find(x => x.Name == "version"), Is.Not.Null);
            Assert.That(request.Parameters.Find(x => x.Name == "key"), Is.Not.Null);
            Assert.That(request.Parameters.Find(x => x.Name == "o"), Is.Not.Null);
            Assert.That(request.Parameters.Find(x => x.Name == "c"), Is.Not.Null);
            Assert.That(request.Parameters.Find(x => x.Name == "Point"), Is.Not.Null);
            Assert.That(request.Parameters.Find(x => x.Name == "includeEntityTypes"), Is.Not.Null);
            Assert.That(request.Parameters.Find(x => x.Name == "includeEntityTypes").Value, Is.EqualTo(IncludeEntityType.CountryRegion.Key));
        }
Пример #2
0
        private async Task <GeoLocation?> GetGpsLocation(IReadOnlyList <MetadataEx.Directory> meta, CancellationToken cancellationToken)
        {
            var geo = GetMetadataValue(meta, "QuickTime Metadata Header/GPS Location");

            if (geo != null)
            {
                var             regex   = new Regex(@"(\+|\-)(\d{2,}\.\d{2,})");
                MatchCollection?matches = regex.Matches(geo);

                var coordintates = new List <double>();

                foreach (string?value in matches.Select(x => x?.ToString()))
                {
                    double coordValue;
                    if (double.TryParse(value, out coordValue))
                    {
                        coordintates.Add(coordValue);
                    }
                }

                if (coordintates.Count == 2)
                {
                    var gps = new GeoLocation()
                    {
                        Type  = "gps",
                        Point = GeoPoint.Create(coordintates[0], coordintates[1])
                    };
                    try
                    {
                        gps.GeoHash = GeoHash.Encode(gps.Point.Coordinates[1], gps.Point.Coordinates[0]);
                        gps.Address = await _geoDecoderService.DecodeAsync(
                            coordintates[0],
                            coordintates[1],
                            cancellationToken);

                        return(gps);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            return(null);
        }
Пример #3
0
        private async Task <MediaOperationCompletedMessage> UpdateMetadataAsync(
            Guid id,
            DateTimeOffset?dateTaken,
            UpdateMedataGeoLocation?geoLocation,
            CancellationToken cancellationToken)
        {
            MediaOperationCompletedMessage msg = new MediaOperationCompletedMessage
            {
                Type    = MediaOperationType.UpdateMetadata,
                MediaId = id,
            };

            try
            {
                if (dateTaken.HasValue)
                {
                    await _mediaService.UpdateDateTakenAsync(id, dateTaken, cancellationToken);
                }
                if (geoLocation is { } geo&& geo.Latitude.HasValue && geo.Longitude.HasValue)
                {
                    Media media = await _mediaStore.GetByIdAsync(id, cancellationToken);

                    media.GeoLocation = new GeoLocation
                    {
                        Point   = GeoPoint.Create(geo.Latitude.Value, geo.Longitude.Value),
                        Type    = "User",
                        GeoHash = GeoHash.Encode(geo.Latitude.Value, geo.Longitude.Value),
                        Address = GetAddress(geoLocation)
                    };

                    await _mediaStore.UpdateAsync(media, cancellationToken);
                }

                msg.IsSuccess = true;
            }
            catch (Exception ex)
            {
                msg.IsSuccess = false;
                msg.Message   = ex.Message;
            }

            return(msg);
        }