/// <summary>
        /// Takes a folder as an arguement and creates a copy that has the detection run 
        /// on it.
        /// </summary>
        /// <param name="path">Path to the folder</param>
        /// <param name="window">Reference to window for which we update the progressbar</param>
        public void ConvertFolder(string path, MainWindow window)
        {
            this.primaryWindow = window;
            this.fileCount = 0;
            if (Directory.Exists(path))
            {
                DirectoryInfo source = new DirectoryInfo(path),
                              target = new DirectoryInfo(path + "_treedetection");
                /*try
                {
                    target.Delete(true);
                }
                catch (Exception)
                {
                }*/

                target.Create();
                this.CopyFilesRecursively(source, target);
                this.RunImageProcessingRecursively(target);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel" /> class.
        /// This is the constructor for the MainViewModel.
        /// When the view model initializes, it reads the map from the App.xaml resources.
        /// </summary>
        /// <param name="window">The window the mainviewmodel belongs to.</param>
        public MainViewModel(MainWindow window)
        {
            this.mainWindow = window;

            this.viewTreeAnalytics = false;
            this.Map = App.Current.Resources["IncidentMap"] as Map;
            this.TreeViewItems = new ObservableCollection<MenuItem>();

            // Read JSON file to get all existing layers
            List<Dataset> layers;
            string json;
            using (StreamReader r = new StreamReader("../../Layers.json"))
            {
                json = r.ReadToEnd();
                layers = JsonConvert.DeserializeObject<List<Dataset>>(json);
            }

            // Backup to manually set up layers in case Layers.json is empty
            if (layers != null)
            {
                // Create MenuItems for all locations
                List<MenuItem> locations = new List<MenuItem>();
                foreach (Dataset ds in layers)
                {
                    MenuItem currentLocationRootNode = null;
                    foreach (MenuItem item in locations)
                    {
                        if (item.Title == ds.Location)
                        {
                            currentLocationRootNode = item;
                        }
                    }

                    // If location has not been added already
                    if (currentLocationRootNode == null)
                    {
                        // Construct new root level node
                        MenuItem node = new MenuItem()
                        {
                            Title = ds.Location,
                            FilePath = ds.FilePath,
                            TreeCanopyFilePath = ds.TreeCanopyFilePath
                        };

                        // Add first subnode to new root level node
                        node.Items.Add(new MenuItem()
                        {
                            Title = ds.Time.ToString(),
                            FilePath = ds.FilePath,
                            TreeCanopyFilePath = ds.TreeCanopyFilePath
                        });
                        locations.Add(node);
                    }
                    else
                    {
                        // If location has been added already
                        // Add another subnode to exisiting root level node
                        currentLocationRootNode.Items.Add(new MenuItem()
                        {
                            Title = ds.Time.ToString(),
                            FilePath = ds.FilePath,
                            TreeCanopyFilePath = ds.TreeCanopyFilePath
                        });
                    }
                }

                // Add root level nodes to tree
                foreach (MenuItem item in locations)
                {
                    this.TreeViewItems.Add(item);
                }

                this.UpdateCurrentLocation(locations.First());
                foreach (MenuItem item in locations.First().Items)
                {
                    this.LoadKml(item.FilePath, true, true);
                }
            }

            this.datasetList = new List<Dataset>();
            this.AddLayerCommand = new DelegateCommand(this.AddLayer);
            this.RemoveLayerCommand = new DelegateCommand(this.RemoveLayer);
        }