public async Task <FoursquareVenue> LookupMatchingVenue(KmlPlacemark placemark, string language, CancellationToken cancellationToken)
        {
            if (placemark.Coordinates.Length == 0 ||
                _kmlCalculator.PlacemarkIsShape(placemark))
            {
                return(null);
            }

            FoursquareResponseVenue venue;
            var venueUrl = ExtractFoursquareUrlFromDescription(placemark);

            if (string.IsNullOrEmpty(venueUrl))
            {
                var coord = _formatter.FormatCoordinates(null, placemark);
                var url   = $"{VENUES_SEARCH_URL}ll={coord}&query={Uri.EscapeUriString(placemark.Name)}&intent=match&limit={LIMIT_LOOKUP}";
                var data  = await DownloadString(url, language, cancellationToken);

                if (data == null)
                {
                    return(null);
                }
                var response = JsonConvert.DeserializeObject <FoursquareResponse <FoursquareSearchResponseBody> >(data);
                if (!CheckMeta(url, response))
                {
                    return(null);
                }
                if (response.Response.Venues.Length == 0)
                {
                    return(null);
                }
                venue = response.Response.Venues[0];
                venue = await DownloadVenueDetails(venue.Id, language, cancellationToken) ?? venue;
            }
            else
            {
                var venueId = ExtractVenueIdFromUrl(venueUrl);
                venue = await DownloadVenueDetails(venueId, language, cancellationToken);

                placemark.Description = placemark.Description.Replace(venueUrl, string.Empty).Trim();
            }

            return(CreateVenueModel(venue));
        }
Beispiel #2
0
        public MooiPlacemark Create(KmlPlacemark kmlPlacemark, IEnumerable <VenueBase> venues, string reportTempPath)
        {
            var descriptionAndImages = ExtractImagesFromContent(kmlPlacemark.Description);
            var description          = FilterContent(descriptionAndImages.filteredContent);

            var placemark = new MooiPlacemark
            {
                Name           = kmlPlacemark.Name,
                Description    = description,
                AttachedVenues = venues?.ToArray(),
                Coordinates    = kmlPlacemark.Coordinates,
                IconPath       = kmlPlacemark.IconPath
            };

            if (descriptionAndImages.images != null)
            {
                placemark.Images.AddRange(descriptionAndImages.images);
            }

            if (placemark.IconPath != null && !placemark.IconPathIsOnWeb)
            {
                placemark.IconPath = Path.Combine(reportTempPath, placemark.IconPath);
            }

            placemark.ThumbnailMapFilePath = Path.Combine(reportTempPath,
                                                          _resourceName.CreateFileNameForPlacemarkThumbnail(placemark));
            placemark.IsShape = _kmlCalculator.PlacemarkIsShape(placemark);

            if (placemark.IsShape)
            {
                var distanceInMeters = _kmlCalculator.CalculateRouteDistanceInMeters(placemark);
                placemark.Distance = _formatter.FormatDistance(distanceInMeters);
            }

            ExtendPlacemarkWithVenueData(placemark);

            return(placemark);
        }
Beispiel #3
0
        public async Task <VenueBase> LookupMatchingVenue(KmlPlacemark placemark, string culture, CancellationToken cancellationToken)
        {
            try
            {
                if (placemark.Coordinates.Length == 0)
                {
                    return(null);
                }
                if (_kmlCalculator.PlacemarkIsShape(placemark))
                {
                    return(null);
                }
                var coord = _formatter.FormatCoordinates(null, placemark);
                var url   = $"{PLACES_DISCOVER_URL}at={coord}&refinements=true&size=4&q={Uri.EscapeUriString(placemark.Name)}";

                // Refer to: https://developer.here.com/rest-apis/documentation/places/topics_api/resource-search.html
                var jsonValue = await DownloadString(url, culture, cancellationToken);

                if (jsonValue == null)
                {
                    return(null);
                }

                var response = JsonConvert.DeserializeObject <HereDiscoverSearchResponse>(jsonValue);
                var place    = response.Results.Items.FirstOrDefault(x => x.Distance < LOOKUP_PLACES_WITHIN_DISTANCE_IN_METERS);
                if (place == null)
                {
                    return(null);
                }

                var discoveredPlace = new HereVenue {
                    Id           = place.Id,
                    Title        = place.Title,
                    Coordinate   = new GeoCoordinate(place.Position[0], place.Position[1]),
                    Address      = ReplaceHtmlNewLines(place.Vicinity),
                    IconUrl      = new Uri(place.Icon),
                    OpeningHours = ReplaceHtmlNewLines(place.OpeningHours?.Text),
                    Category     = place.Category.Title,
                    Websites     = new string[0]
                };

                if (place.Href != null)
                {
                    var extraInfoUrl   = place.Href + "&show_content=wikipedia";
                    var jsonValueExtra = await DownloadString(extraInfoUrl, culture, cancellationToken);

                    if (jsonValueExtra != null)
                    {
                        var extra = JsonConvert.DeserializeObject <HerePlace>(jsonValueExtra);

                        if (extra.Location?.Address != null)
                        {
                            discoveredPlace.Region = string.Join(", ", extra.Location.Address.State, extra.Location.Address.Country);
                        }

                        discoveredPlace.Tags = extra.Tags?.Select(x => x.Title).ToArray();

                        if (!string.IsNullOrEmpty(extra.View))
                        {
                            discoveredPlace.Websites = new [] { extra.View };
                        }

                        if (extra.Contacts?.Phone?.Length > 0)
                        {
                            discoveredPlace.ContactPhone = extra.Contacts.Phone[0].Value;
                        }

                        if (extra.Contacts?.Website?.Length > 0)
                        {
                            discoveredPlace.Websites = discoveredPlace.Websites
                                                       .Concat(extra.Contacts.Website.Select(x => x.Value)).ToArray();
                        }

                        var wikipedia = extra.Media?.Editorials?.Items
                                        .FirstOrDefault(x => x.Supplier.Id.Equals("wikipedia", StringComparison.OrdinalIgnoreCase));
                        if (wikipedia != null)
                        {
                            var requestedLanguage = culture.Split('-')[0];
                            if (wikipedia.Language.Equals(requestedLanguage, StringComparison.OrdinalIgnoreCase) ||
                                wikipedia.Language.Equals(ACCEPTABLE_LANGUAGE, StringComparison.OrdinalIgnoreCase))
                            {
                                discoveredPlace.WikipediaContent = FilterContent(wikipedia.Description);
                            }
                        }
                    }
                }

                if (discoveredPlace.IsUseless())
                {
                    return(null);
                }

                return(discoveredPlace);
            }
            catch (Exception e)
            {
                _logger.Exception(e);
                return(null);
            }
        }