Exemplo n.º 1
0
        private async Task FetchClusterMapImage(MooiCluster cluster, string tempPath)
        {
            var imageBytes = await _hereAdapter.FetchOverviewMap(cluster);

            if (imageBytes == null)
            {
                _logger.Warn($"Was unable to download overview map image for '{cluster.Id}'");
                return;
            }
            var filePath = Path.Combine(tempPath, _resourceName.CreateFileNameForOverviewMap(cluster));
            await _file.WriteBytesAsync(filePath, imageBytes);

            _logger.Info($"An overview map image for '{cluster.Id}' has been successfully downloaded");
        }
Exemplo n.º 2
0
        public List <MooiCluster> CreateList(KmlFolder folder, List <DiscoveredPlace> discoveredPlaces, string reportTempPath)
        {
            var clusters = new List <MooiCluster>();

            var placemarksConverted = folder.Placemarks
                                      .Select(x => _mooiPlacemarkFactory.Create(x,
                                                                                discoveredPlaces?.Where(dp => dp.AttachedToPlacemark == x).Select(dp => dp.Venue),
                                                                                reportTempPath))
                                      .ToList();

            if (placemarksConverted.Count <= MIN_COUNT_PER_CLUSTER)
            {
                return(CreateSingleCluster(placemarksConverted, reportTempPath));
            }

            if (folder.ContainsRoute && _kmlCalculator.CompleteFolderIsRoute(folder))
            {
                return(CreateSingleCluster(placemarksConverted, reportTempPath));
            }

            // TODO: Add support of lines within a folder which are not 'routes'
            placemarksConverted = placemarksConverted.Where(x => x.Coordinates.Length == 1).ToList();
            // ^^^

            var placemarksWithNeighbors       = GetPlacemarksWithNeighbors(placemarksConverted).ToList();
            var placemarksWithNeighborsLookup = placemarksWithNeighbors.ToDictionary(x => x.Placemark);

            var currentCluster = new MooiCluster();

            clusters.Add(currentCluster);

            var placemarksToProcess = placemarksWithNeighbors.ToList();

            while (placemarksToProcess.Any())
            {
                var startingPoint = placemarksToProcess[0];
                placemarksToProcess.RemoveAt(0);

                // Skip if the placemark has been added to any cluster before
                if (clusters.Any(g => g.Placemarks.Any(p => p == startingPoint.Placemark)))
                {
                    continue;
                }

                AppendPlacemarkToCluster(startingPoint.Placemark, currentCluster);

                // Add its closest neighbor to current cluster
                if (!clusters.Any(g => g.Placemarks.Any(p => p == startingPoint.NeighborWithMinDistance.Placemark)))
                {
                    AppendPlacemarkToCluster(startingPoint.NeighborWithMinDistance.Placemark, currentCluster);
                }

                foreach (var pm in placemarksToProcess.Skip(1).ToList())
                {
                    if (currentCluster.Placemarks.Any(x => x == pm.Placemark ||
                                                      x == pm.NeighborWithMinDistance.Placemark ||
                                                      pm.Neighbors.Any(n => n.Placemark == x && pm.NeighborWithMinDistance.AllowedDistance > n.Distance)
                                                      ))
                    {
                        if (currentCluster.Placemarks.Count >= MIN_COUNT_PER_CLUSTER)
                        {
                            var maxDistanceAmongAddedPlacemarks = placemarksWithNeighborsLookup[currentCluster.Placemarks[0]]
                                                                  .Neighbors.Where(x => currentCluster.Placemarks.Any(y => y == x.Placemark))
                                                                  .Select(x => x.AllowedDistance)
                                                                  .Max();

                            if (pm.NeighborWithMinDistance.Distance > maxDistanceAmongAddedPlacemarks)
                            {
                                continue;
                            }
                        }

                        AppendPlacemarkToCluster(pm.Placemark, currentCluster);
                        placemarksToProcess.Remove(pm);
                    }

                    if (currentCluster.Placemarks.Count == MAX_COUNT_PER_CLUSTER)
                    {
                        break;
                    }
                }

                currentCluster.OverviewMapFilePath = Path.Combine(reportTempPath, _resourceName.CreateFileNameForOverviewMap(currentCluster));
                currentCluster = new MooiCluster();
                clusters.Add(currentCluster);
            }

            // Trim out the last cluster which is always empty
            clusters = clusters.Where(x => x.Placemarks.Count > 0).ToList();

            MergeClusters(clusters);

            return(clusters);
        }