private void ReadKmlDocumentIntoViewModel(KmlDocument document) { ViewModel.FoldersToInclude.Clear(); foreach (var folder in document.Folders) { var isRoute = _kmlCalculator.CompleteFolderIsRoute(folder); var folderVm = new KmlFolderNodeViewModel(folder) { Enabled = true, IsPartOfRoute = isRoute }; foreach (var placemark in folder.Placemarks) { folderVm.Children.Add(new KmlPlacemarkNodeViewModel(placemark, folderVm) { Enabled = true, IsPartOfRoute = isRoute }); } ViewModel.FoldersToInclude.Add(folderVm); } }
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); }