/// <summary>
 /// Runs tree analytics on seperate thread
 /// </summary>
 /// <param name="sender">Sender</param>
 /// <param name="e">Event</param>
 private void TreeButton_Click(object sender, RoutedEventArgs e)
 {
     OpenFileDialog file = new OpenFileDialog();
     file.Filter = "Map Data|*.kml;*.kmz";
     file.ShowDialog();
     TreeLineDetection temp = new TreeLineDetection();
     Task.Run(() => temp.ConvertFolder((new FileInfo(file.FileName)).DirectoryName, this));
 }
        /// <summary>
        /// "Add Layer" button callback
        /// Pops up a window that gets the user to input the necessary information
        /// for adding a new layer and then adds the layer.
        /// </summary>
        /// <param name="parameter">The window being passed.</param>
        public void AddLayer(object parameter)
        {
            // Generate list of location names
            ObservableCollection<string> locations = new ObservableCollection<string>();
            foreach (MenuItem item in this.treeViewItems)
            {
                locations.Add(item.Title);
            }

            // Popup a window to get the layer information
            AddLayer addLayer = new AddLayer(locations);
            addLayer.Owner = (Window)parameter; // In the XAML we pass the window as the parameter
            addLayer.ShowDialog();

            // Get the data from the AddLayer window
            Dataset newLayer = addLayer.DatasetToAdd;
            addLayer.Close();

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

            // Save new layer to flat JSON file
            if (layers == null)
            {
                layers = new List<Dataset>();
            }

            layers.Add(newLayer);
            json = JsonConvert.SerializeObject(layers.ToArray());
            System.IO.File.WriteAllText("../../Layers.json", json);

            if (!string.IsNullOrEmpty(newLayer.FilePath))
            {
                // TODO - somewhere around here execute the code to do the tree analysis on the new image
                // Presently, this will not require a change in the treeview, so the new image can go straight
                // to LoadKML()
                TreeLineDetection temp = new TreeLineDetection();
                Task.Run(() => temp.ConvertFolder((new FileInfo(newLayer.FilePath)).DirectoryName, this.mainWindow));

                // Save the new dataset
                this.datasetList.Add(newLayer);

                // See if the Location already exists
                bool locationExists = false;

                foreach (MenuItem location in this.TreeViewItems)
                {
                    // If so, then add the new layer as a child of that Location
                    if (location.Title == newLayer.Location)
                    {
                        MenuItem newChild = new MenuItem(newLayer.Time.ToString(), newLayer.FilePath, location.TreeCanopyFilePath);
                        location.Items.Add(newChild);

                        // Sort the children based on time
                        location.Items = new ObservableCollection<MenuItem>(location.Items.OrderBy(time => time.Title).ToList());

                        this.UpdateCurrentLocation(location, newChild);
                        locationExists = true;
                        break;
                    }
                }

                // If not, then we also need to add the Location to the treeview
                if (!locationExists)
                {
                    // Add it to the TreeView on the UI
                    MenuItem root = new MenuItem(newLayer.Location, newLayer.FilePath, newLayer.TreeCanopyFilePath);
                    root.Items.Add(new MenuItem(newLayer.Time.ToString(), newLayer.FilePath, newLayer.TreeCanopyFilePath));
                    this.TreeViewItems.Add(root);
                    this.UpdateCurrentLocation(root);
                }

                // Open the new layer
                this.LoadKml(newLayer.FilePath, newLayer.ZoomTo, false);
            }

            Debug.WriteLine("Location: " + newLayer.Location);
            Debug.WriteLine("Time: " + newLayer.Time);
            Debug.WriteLine("File Path: " + newLayer.FilePath);
        }