Exemplo n.º 1
0
        public static async Task <double> CalculateTotalPolygonAreaAsync(Uri gdbUri, string featureClassName)
        {
            double dblRetVal = 0;

            try
            {
                bool bExists = await GeodatabaseTools.FeatureClassExistsAsync(gdbUri, featureClassName);

                if (!bExists)
                {
                    return(dblRetVal);
                }

                await QueuedTask.Run(() =>
                {
                    using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(gdbUri)))
                        using (Table table = geodatabase.OpenDataset <Table>(featureClassName))
                        {
                            QueryFilter queryFilter = new QueryFilter();
                            using (RowCursor aCursor = table.Search(queryFilter, false))
                            {
                                while (aCursor.MoveNext())
                                {
                                    using (Feature feature = (Feature)aCursor.Current)
                                    {
                                        var geometry = feature.GetShape();
                                        var area     = GeometryEngine.Instance.Area(geometry);
                                        dblRetVal    = dblRetVal + area;
                                    }
                                }
                            }
                        }
                });
            }
            catch (Exception e)
            {
                Module1.Current.ModuleLogManager.LogError(nameof(CalculateTotalPolygonAreaAsync),
                                                          "Exception: " + e.Message);
                dblRetVal = -1;
            }

            return(dblRetVal);
        }
Exemplo n.º 2
0
        private async void RunImplAsync(object param)
        {
            // Create initial log entry
            string strLogEntry = DateTime.Now.ToString("MM/dd/yy H:mm:ss ") + "Starting batch tool to publish in " +
                                 Path.GetDirectoryName(_strLogFile) + "\r\n";

            File.WriteAllText(_strLogFile, strLogEntry);    // overwrite file if it exists

            // Check for existing map package files and warn user
            if (ArchiveChecked)
            {
                string[] filePaths = Directory.GetFiles(AoiFolder + "\\" + Constants.FOLDER_MAP_PACKAGE, "*.pdf",
                                                        SearchOption.TopDirectoryOnly);
                if (filePaths.Length > 0)
                {
                    System.Windows.MessageBoxResult res = ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("BAGIS-PRO found at least one .pdf document in the " +
                                                                                                           "maps\\publish folder. These document(s) may be overwritten during the batch process. Uncheck " +
                                                                                                           "the 'Copy Reports' checkbox to stop copying documents to the maps\\publish folder. " +
                                                                                                           "The map packages will still be created in each AOI. Do you wish to continue and overwrite " +
                                                                                                           "the documents ?", "BAGIS-PRO",
                                                                                                           System.Windows.MessageBoxButton.YesNo);
                    if (res != System.Windows.MessageBoxResult.Yes)
                    {
                        return;
                    }
                }
            }

            // Save off the publisher name if it is different than previous
            string strPublisher = (string)Module1.Current.BatchToolSettings.Publisher;

            if (!Publisher.Trim().Equals(strPublisher))
            {
                Module1.Current.BatchToolSettings.Publisher = Publisher;
                String json = JsonConvert.SerializeObject(Module1.Current.BatchToolSettings, Formatting.Indented);
                File.WriteAllText(SettingsFile, json);
            }

            // Make directory for required folders if they don't exist
            // Make sure that maps and maps_publish folders exist
            for (int idxRow = 0; idxRow < Names.Count; idxRow++)
            {
                if (Names[idxRow].AoiBatchIsSelected)
                {
                    int errorCount = 0;                                                 // keep track of any non-fatal errors
                    AoiFolder = Names[idxRow].FilePath;
                    Names[idxRow].AoiBatchStateText = AoiBatchState.Started.ToString(); // update gui
                    string[] arrFolders = { AoiFolder + "\\" + Constants.FOLDER_MAPS, AoiFolder + "\\" + Constants.FOLDER_MAP_PACKAGE,
                                            AoiFolder + "\\" + Constants.FOLDER_LOGS };
                    foreach (var directory in arrFolders)
                    {
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }
                    }

                    // Set logger to AOI directory
                    string logFolderName = AoiFolder + "\\" + Constants.FOLDER_LOGS;
                    Module1.Current.ModuleLogManager.UpdateLogFileLocation(logFolderName);

                    // Set current AOI
                    BA_Objects.Aoi oAoi = await GeneralTools.SetAoiAsync(AoiFolder);

                    if (Module1.Current.CboCurrentAoi != null)
                    {
                        FrameworkApplication.Current.Dispatcher.Invoke(() =>
                        {
                            // Do something on the GUI thread
                            Module1.Current.CboCurrentAoi.SetAoiName(oAoi.Name);
                        });
                    }

                    // Create opening log entry for AOI
                    strLogEntry = DateTime.Now.ToString("MM/dd/yy H:mm:ss ") + "Starting batch PDF export for " +
                                  oAoi.Name + "\r\n";
                    File.AppendAllText(_strLogFile, strLogEntry);       // append

                    // Bring GP History tool forward
                    var cmdShowHistory = FrameworkApplication.GetPlugInWrapper("esri_geoprocessing_showToolHistory") as ICommand;
                    if (cmdShowHistory != null)
                    {
                        if (cmdShowHistory.CanExecute(null))
                        {
                            cmdShowHistory.Execute(null);
                        }
                    }

                    oAoi = Module1.Current.Aoi;
                    // Elevation zones
                    BA_ReturnCode success = await AnalysisTools.CalculateElevationZonesAsync(Module1.Current.Aoi.FilePath);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Slope zones
                    string strLayer = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Surfaces, true) +
                                      Constants.FILE_SLOPE;
                    string strZonesRaster = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Analysis, true) +
                                            Constants.FILE_SLOPE_ZONE;
                    string strMaskPath = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Aoi, true) + Constants.FILE_AOI_BUFFERED_VECTOR;
                    IList <BA_Objects.Interval> lstInterval = AnalysisTools.GetSlopeClasses();
                    success = await AnalysisTools.CalculateZonesAsync(AoiFolder, strLayer,
                                                                      lstInterval, strZonesRaster, strMaskPath, "SLOPE");

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Check for PRISM buffer units
                    string[] arrPrismBufferInfo = await GeneralTools.QueryBufferDistanceAsync(AoiFolder, GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Aoi),
                                                                                              Constants.FILE_AOI_PRISM_VECTOR, false);

                    string pBufferDistance = arrPrismBufferInfo[0];
                    string pBufferUnits    = arrPrismBufferInfo[1];

                    // Clip PRISM
                    string strDefaultBufferDistance = (string)Module1.Current.BatchToolSettings.PrecipBufferDistance;
                    string strDefaultBufferUnits    = (string)Module1.Current.BatchToolSettings.PrecipBufferUnits;
                    success = await AnalysisTools.ClipLayersAsync(AoiFolder, Constants.DATA_TYPE_PRECIPITATION,
                                                                  pBufferDistance, pBufferUnits, strDefaultBufferDistance, strDefaultBufferUnits);

                    if (success == BA_ReturnCode.Success)
                    {
                        success = await AnalysisTools.UpdateSitesPropertiesAsync(Module1.Current.Aoi.FilePath, SiteProperties.Precipitation);
                    }
                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // PRISM Zones
                    strLayer = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Prism, true) +
                               Path.GetFileName((string)Module1.Current.BatchToolSettings.AoiPrecipFile);
                    strZonesRaster = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Analysis, true) +
                                     Constants.FILE_PRECIP_ZONE;
                    success = await AnalysisTools.CalculatePrecipitationZonesAsync(strLayer, strZonesRaster);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Winter Precipitation
                    success = await AnalysisTools.GenerateWinterPrecipitationLayerAsync(oAoi);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Clip SWE
                    success = await AnalysisTools.ClipSweLayersAsync(pBufferDistance, pBufferUnits,
                                                                     strDefaultBufferDistance, strDefaultBufferUnits);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Generate SWE Delta Layers
                    success = await AnalysisTools.CalculateSWEDeltaAsync(AoiFolder);

                    // Clip Snotel and Snow Course
                    double dblDistance = -1;
                    bool   isDouble    = Double.TryParse((string)Module1.Current.BatchToolSettings.SnotelBufferDistance, out dblDistance);
                    if (!isDouble)
                    {
                        dblDistance = 0;
                    }
                    Module1.Current.ModuleLogManager.LogDebug(nameof(RunImplAsync), "Buffer distance from settings: " + dblDistance);
                    string snoBufferDistance = dblDistance + " " + (string)Module1.Current.BatchToolSettings.SnotelBufferUnits;
                    Module1.Current.ModuleLogManager.LogDebug(nameof(RunImplAsync), "Sites buffer distance string: " + snoBufferDistance);
                    success = await AnalysisTools.ClipSnoLayersAsync(Module1.Current.Aoi.FilePath, true, snoBufferDistance,
                                                                     true, snoBufferDistance);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Represented Area
                    if (success == BA_ReturnCode.Success)
                    {
                        double siteBufferDistanceMiles = (double)Module1.Current.BatchToolSettings.SiteBufferDistMiles;
                        double siteElevRangeFeet       = (double)Module1.Current.BatchToolSettings.SiteElevRangeFeet;
                        success = await AnalysisTools.GenerateSiteLayersAsync(siteBufferDistanceMiles, siteElevRangeFeet);

                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }

                        // Sites Zones
                        Uri  uri       = new Uri(GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Layers));
                        bool hasSnotel = await GeodatabaseTools.FeatureClassExistsAsync(uri, Constants.FILE_SNOTEL);

                        bool hasSnowCourse = await GeodatabaseTools.FeatureClassExistsAsync(uri, Constants.FILE_SNOW_COURSE);

                        if (hasSnotel || hasSnowCourse)
                        {
                            success = await AnalysisTools.CalculateSitesZonesAsync(Module1.Current.Aoi.FilePath, hasSnotel, hasSnowCourse);

                            if (success != BA_ReturnCode.Success)
                            {
                                errorCount++;
                            }
                        }
                        else
                        {
                            Module1.Current.ModuleLogManager.LogError(nameof(CmdRun),
                                                                      "No sites found to create sites zone layers!!");
                        }
                    }
                    // Precipitation Contribution; Passing in -1 for threshold so we use STDEV
                    success = await AnalysisTools.CalculatePrecipitationContributionAsync(Module1.Current.Aoi.FilePath, -1);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Quarterly Precipitation Contribution
                    success = await AnalysisTools.CalculateQuarterlyPrecipitationAsync(Module1.Current.Aoi);

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    // Aspect zones
                    success = await AnalysisTools.CalculateAspectZonesAsync();

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }

                    string[] arrUnmanagedBufferInfo = await GeneralTools.QueryBufferDistanceAsync(AoiFolder, GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Aoi),
                                                                                                  Constants.FILE_AOI_BUFFERED_VECTOR, false);

                    string unmanagedBufferDistance = arrPrismBufferInfo[0];
                    string unmanagedBufferUnits    = arrPrismBufferInfo[1];
                    if (SiteAnalysisChecked)
                    {
                        // Clip Roads
                        string strOutputFc = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Layers, true)
                                             + Constants.FILE_ROADS;
                        success = await AnalysisTools.ClipFeatureLayerAsync(AoiFolder, strOutputFc, Constants.DATA_TYPE_ROADS,
                                                                            unmanagedBufferDistance, unmanagedBufferUnits);

                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }
                        if (success == BA_ReturnCode.Success)
                        {
                            // Buffer clipped roads for analysis
                            Uri  uri     = new Uri(GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Layers));
                            bool bExists = await GeodatabaseTools.FeatureClassExistsAsync(uri, Constants.FILE_ROADS);

                            if (!bExists)
                            {
                                Module1.Current.ModuleLogManager.LogDebug(nameof(CmdRun),
                                                                          "Unable to buffer roads because fs_roads layer does not exist. Process stopped!!");
                            }
                            else
                            {
                                string strDistance = Module1.Current.BatchToolSettings.RoadsAnalysisBufferDistance + " " +
                                                     Module1.Current.BatchToolSettings.RoadsAnalysisBufferUnits;
                                success = await AnalysisTools.GenerateProximityRoadsLayerAsync(uri, strDistance);

                                if (success != BA_ReturnCode.Success)
                                {
                                    errorCount++;
                                }
                            }
                        }

                        // Clip public lands
                        strOutputFc = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Layers, true)
                                      + Constants.FILE_PUBLIC_LAND;
                        success = await AnalysisTools.ClipFeatureLayerAsync(AoiFolder, strOutputFc, Constants.DATA_TYPE_PUBLIC_LAND,
                                                                            unmanagedBufferDistance, unmanagedBufferUnits);

                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }
                        if (success == BA_ReturnCode.Success)
                        {
                            // Create public lands layer for potential site analysis
                            success = await AnalysisTools.GetFederalNonWildernessLandsAsync(AoiFolder);

                            if (success != BA_ReturnCode.Success)
                            {
                                errorCount++;
                            }
                        }

                        // Clip Vegetation layer
                        string strOutputRaster = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Layers, true)
                                                 + Constants.FILE_VEGETATION_EVT;
                        success = await AnalysisTools.ClipRasterLayerAsync(AoiFolder, strOutputRaster, Constants.DATA_TYPE_VEGETATION,
                                                                           unmanagedBufferDistance, unmanagedBufferUnits);

                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }
                        if (success == BA_ReturnCode.Success)
                        {
                            // Create area below treeline layer for potential site analysis
                            success = await AnalysisTools.ExtractBelowTreelineAsync(AoiFolder);

                            if (success != BA_ReturnCode.Success)
                            {
                                errorCount++;
                            }
                        }

                        // Generate Potential Sites layer
                        success = await AnalysisTools.CalculatePotentialSitesAreaAsync(AoiFolder);

                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }
                    }

                    // Clip Land cover
                    success = await AnalysisTools.ClipLandCoverAsync(AoiFolder, unmanagedBufferDistance, unmanagedBufferUnits);

                    // Generate Elevation Precipitation Correlation layer
                    strLayer = GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Prism, true) +
                               Path.GetFileName((string)Module1.Current.BatchToolSettings.AoiPrecipFile);
                    Uri uriPrism = new Uri(GeodatabaseTools.GetGeodatabasePath(AoiFolder, GeodatabaseNames.Prism));
                    success = await AnalysisTools.CalculateElevPrecipCorrAsync(AoiFolder, uriPrism,
                                                                               Path.GetFileName((string)Module1.Current.BatchToolSettings.AoiPrecipFile));

                    if (success != BA_ReturnCode.Success)
                    {
                        errorCount++;
                    }
                    if (success == BA_ReturnCode.Success)
                    {
                        Module1.Current.ModuleLogManager.LogDebug(nameof(CmdRun),
                                                                  "Generated Elevation Precipitation Correlation layer");
                    }

                    // Generate complete PDF document
                    try
                    {
                        // Delete any old PDF files
                        //string[] arrAllPdfFiles = new string[Constants.FILES_EXPORT_WATERSHED_PDF.Length + FILES_EXPORT_SITE_ANALYSIS_PDF.Length];
                        //Array.Copy(Constants.FILES_EXPORT_WATERSHED_PDF, arrAllPdfFiles, Constants.FILES_EXPORT_WATERSHED_PDF.Length);
                        //Array.Copy(Constants.FILES_EXPORT_SITE_ANALYSIS_PDF, 0, arrAllPdfFiles,
                        //    Constants.FILES_EXPORT_WATERSHED_PDF.Length, Constants.FILES_EXPORT_SITE_ANALYSIS_PDF.Length);
                        foreach (var item in Constants.FILES_EXPORT_WATERSHED_PDF)
                        {
                            string strPath = Module1.Current.Aoi.FilePath + "\\" + Constants.FOLDER_MAP_PACKAGE
                                             + "\\" + item;
                            if (System.IO.File.Exists(strPath))
                            {
                                try
                                {
                                    System.IO.File.Delete(strPath);
                                }
                                catch (Exception)
                                {
                                    System.Windows.MessageBoxResult res =
                                        MessageBox.Show("Unable to delete file before creating new pdf. Do you want to close the file and try again?",
                                                        "BAGIS-PRO", System.Windows.MessageBoxButton.YesNo);
                                    if (res == System.Windows.MessageBoxResult.Yes)
                                    {
                                        return;
                                    }
                                }
                            }
                        }

                        Layout oLayout = await MapTools.GetDefaultLayoutAsync(Constants.MAPS_DEFAULT_LAYOUT_NAME);

                        // Always load the maps in case we are running through multiple Aois
                        success = await MapTools.DisplayMaps(Module1.Current.Aoi.FilePath, oLayout, false);

                        if (success != BA_ReturnCode.Success)
                        {
                            MessageBox.Show("Unable to load maps. The map package cannot be exported!!", "BAGIS-PRO");
                            Names[idxRow].AoiBatchStateText = AoiBatchState.Failed.ToString();
                            return;
                        }
                        // Legend
                        success = await MapTools.DisplayLegendAsync(Constants.MAPS_DEFAULT_MAP_FRAME_NAME, oLayout,
                                                                    "ArcGIS Colors", "1.5 Point", true);

                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }

                        if (oLayout != null)
                        {
                            bool bFoundIt = false;
                            //A layout view may exist but it may not be active
                            //Iterate through each pane in the application and check to see if the layout is already open and if so, activate it
                            foreach (var pane in FrameworkApplication.Panes)
                            {
                                if (!(pane is ILayoutPane layoutPane))  //if not a layout view, continue to the next pane
                                {
                                    continue;
                                }
                                if (layoutPane.LayoutView != null &&
                                    layoutPane.LayoutView.Layout == oLayout) //if there is a match, activate the view
                                {
                                    (layoutPane as Pane).Activate();
                                    bFoundIt = true;
                                }
                            }
                            if (!bFoundIt)
                            {
                                await FrameworkApplication.Current.Dispatcher.Invoke(async() =>
                                {
                                    // Do something on the GUI thread
                                    ILayoutPane iNewLayoutPane = await FrameworkApplication.Panes.CreateLayoutPaneAsync(oLayout); //GUI thread
                                    (iNewLayoutPane as Pane).Activate();
                                });
                            }
                        }

                        success = await MapTools.PublishMapsAsync(ReportType.Watershed); // export the watershed maps to pdf

                        if (success != BA_ReturnCode.Success)
                        {
                            MessageBox.Show("An error occurred while generating the watershed characteristics maps!!", "BAGIS-PRO");
                            errorCount++;
                        }

                        //if (SiteAnalysisChecked)
                        //{
                        //    success = await MapTools.PublishMapsAsync(ReportType.SiteAnalysis); // export the site analysis maps to pdf
                        //    if (success != BA_ReturnCode.Success)
                        //    {
                        //        MessageBox.Show("An error occurred while generating the site analysis maps!!", "BAGIS-PRO");
                        //        errorCount++;
                        //    }
                        //}

                        success = await GeneralTools.GenerateTablesAsync(false);   // export the tables to pdf

                        if (success != BA_ReturnCode.Success)
                        {
                            MessageBox.Show("An error occurred while generating the Excel tables!!", "BAGIS-PRO");
                            errorCount++;
                        }
                        else
                        {
                            // Generate the crtical precip map; It has to follow the tables
                            Uri uriAnalysis = new Uri(GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Analysis));
                            if (await GeodatabaseTools.FeatureClassExistsAsync(uriAnalysis, Constants.FILE_CRITICAL_PRECIP_ZONE))
                            {
                                success = await MapTools.DisplayCriticalPrecipitationZonesMap(uriAnalysis);

                                string strButtonState = "MapButtonPalette_BtnCriticalPrecipZone_State";
                                if (success.Equals(BA_ReturnCode.Success))
                                {
                                    Module1.ActivateState(strButtonState);
                                }
                                int      foundS1      = strButtonState.IndexOf("_State");
                                string   strMapButton = strButtonState.Remove(foundS1);
                                ICommand cmd          = FrameworkApplication.GetPlugInWrapper(strMapButton) as ICommand;
                                Module1.Current.ModuleLogManager.LogDebug(nameof(RunImplAsync),
                                                                          "About to toggle map button " + strMapButton);
                                if ((cmd != null))
                                {
                                    do
                                    {
                                        await Task.Delay(TimeSpan.FromSeconds(0.4));      // build in delay until the command can execute
                                    }while (!cmd.CanExecute(null));
                                    cmd.Execute(null);
                                }

                                do
                                {
                                    await Task.Delay(TimeSpan.FromSeconds(0.4));       // build in delay so maps can load
                                }while (Module1.Current.MapFinishedLoading == false);
                                success = await GeneralTools.ExportMapToPdfAsync(150); // export map to pdf

                                if (success == BA_ReturnCode.Success)
                                {
                                    // append the map and chart together for posting
                                    IList <string> lstToConcat = new List <string>();
                                    lstToConcat.Add(GeneralTools.GetFullPdfFileName(Constants.FILE_EXPORT_MAP_CRITICAL_PRECIPITATION_ZONES_PDF));
                                    lstToConcat.Add(GeneralTools.GetFullPdfFileName(Constants.FILE_EXPORT_TABLE_PRECIP_REPRESENT_PDF));
                                    success = GeneralTools.ConcatenatePagesInPdf(GeneralTools.GetFullPdfFileName(Constants.FILE_EXPORT_CRITICAL_PRECIPITATION_ZONES_PDF),
                                                                                 lstToConcat);
                                }
                                else
                                {
                                    Module1.Current.ModuleLogManager.LogError(nameof(RunImplAsync),
                                                                              "Unable to generate critical precipitation zones map!!");
                                }
                            }
                        }

                        success = await GeneralTools.GenerateSitesTableAsync(Module1.Current.Aoi);

                        success = await GeneralTools.GenerateMapsTitlePageAsync(ReportType.Watershed, strPublisher, Comments);

                        if (success != BA_ReturnCode.Success)
                        {
                            MessageBox.Show("An error occurred while generating the Title page!!", "BAGIS-PRO");
                            errorCount++;
                        }
                        string outputPath = GeneralTools.GetFullPdfFileName(Constants.FILE_EXPORT_WATERSHED_REPORT_PDF);
                        success = GeneralTools.PublishFullPdfDocument(outputPath, ReportType.Watershed);    // Put it all together into a single pdf document
                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }
                        //if (SiteAnalysisChecked)
                        //{
                        //    success = await GeneralTools.GenerateMapsTitlePageAsync(ReportType.SiteAnalysis, strPublisher, Comments);
                        //    outputPath = GeneralTools.GetFullPdfFileName(Constants.FILE_EXPORT_SITE_ANALYSIS_REPORT_PDF);
                        //    success = GeneralTools.PublishFullPdfDocument(outputPath, ReportType.SiteAnalysis);    // Put it all together into a single pdf document
                        //}
                        if (success != BA_ReturnCode.Success)
                        {
                            errorCount++;
                        }
                        else if (ArchiveChecked)
                        {
                            string reportName = Constants.FILE_EXPORT_WATERSHED_REPORT_PDF;
                            // Copy final watershed analysis report to a central location
                            if (File.Exists(outputPath))
                            {
                                File.Copy(outputPath, GeneralTools.GetFullPdfFileName(reportName), true);
                            }
                            //if (SiteAnalysisChecked)
                            //{
                            //    reportName = Constants.FILE_EXPORT_SITE_ANALYSIS_REPORT_PDF;
                            //    File.Copy(outputPath, GeneralTools.GetFullPdfFileName(reportName), true);
                            //}
                        }
                        // Create closing log entry for AOI
                        if (errorCount == 0)
                        {
                            strLogEntry = DateTime.Now.ToString("MM/dd/yy H:mm:ss ") + "Completed batch PDF export for " +
                                          oAoi.Name + ". The output is located at " + oAoi.FilePath + "\\" + Constants.FOLDER_MAP_PACKAGE + "\r\n";
                            Names[idxRow].AoiBatchStateText = AoiBatchState.Completed.ToString();
                        }
                        else
                        {
                            strLogEntry = DateTime.Now.ToString("MM/dd/yy H:mm:ss ") + "Completed batch PDF export WITH ERRORS for " +
                                          oAoi.Name + ". The output is located at " + oAoi.FilePath + "\\" + Constants.FOLDER_MAP_PACKAGE + "\r\n" +
                                          "Check for errors in the logs at " + oAoi.FilePath + "\\" + Constants.FOLDER_LOGS + "! \r\n";
                            Names[idxRow].AoiBatchStateText = AoiBatchState.Errors.ToString();
                        }
                        File.AppendAllText(_strLogFile, strLogEntry);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("An error occurred while running the Batch PDF Tool!! " + e.Message, "BAGIS PRO");
                        Module1.Current.ModuleLogManager.LogError(nameof(RunImplAsync),
                                                                  e.StackTrace);
                        strLogEntry = DateTime.Now.ToString("MM/dd/yy H:mm:ss ") + "Batch PDF export failed for " +
                                      oAoi.Name + ". Check for errors in the logs at " + oAoi.FilePath + "\\" + Constants.FOLDER_LOGS + "!\r\n";
                        File.AppendAllText(_strLogFile, strLogEntry);
                        Names[idxRow].AoiBatchStateText = AoiBatchState.Failed.ToString();
                    }
                }
            }
            MessageBox.Show("Done!");

            // Concluding log entry
            strLogEntry = DateTime.Now.ToString("MM/dd/yy H:mm:ss ") + "Batch tool finished!! \r\n";
            using (StreamWriter sw = File.AppendText(_strLogFile))
            {
                sw.WriteLine(strLogEntry);
            }
        }
Exemplo n.º 3
0
        private async Task GenerateLayersAsync(bool calculateRepresented, bool calculatePrism, bool calculateAspect,
                                               bool calculateSlope, bool calculateElevation, bool bufferRoads, bool extractPublicLand,
                                               bool extractBelowTreeline, bool elevPrecipCorr, bool calculateSitesZones, bool calculateSweDelta,
                                               bool calculatePrecipContrib, bool winterPrecip)
        {
            try
            {
                if (String.IsNullOrEmpty(Module1.Current.Aoi.Name))
                {
                    MessageBox.Show("No AOI selected for analysis !!", "BAGIS-PRO");
                    return;
                }

                if (calculateRepresented == false && calculatePrism == false && calculateAspect == false &&
                    calculateSlope == false && calculateElevation == false && bufferRoads == false &&
                    extractPublicLand == false && extractBelowTreeline == false && elevPrecipCorr == false &&
                    calculateSitesZones == false && calculateSweDelta == false && calculatePrecipContrib == false &&
                    winterPrecip == false)
                {
                    MessageBox.Show("No layers selected to generate !!", "BAGIS-PRO");
                    return;
                }

                var cmdShowHistory = FrameworkApplication.GetPlugInWrapper("esri_geoprocessing_showToolHistory") as ICommand;
                if (cmdShowHistory != null)
                {
                    if (cmdShowHistory.CanExecute(null))
                    {
                        cmdShowHistory.Execute(null);
                    }
                }

                var           layersPane = (DockAnalysisLayersViewModel)FrameworkApplication.DockPaneManager.Find("bagis_pro_DockAnalysisLayers");
                BA_ReturnCode success    = BA_ReturnCode.Success;

                if (calculateRepresented)
                {
                    double siteBufferDistanceMiles = (double)Module1.Current.BatchToolSettings.SiteBufferDistMiles;
                    double siteElevRangeFeet       = (double)Module1.Current.BatchToolSettings.SiteElevRangeFeet;
                    success = await AnalysisTools.GenerateSiteLayersAsync(siteBufferDistanceMiles, siteElevRangeFeet);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.RepresentedArea_Checked = false;
                    }
                }

                if (calculatePrism)
                {
                    string strLayer = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Prism, true) +
                                      Path.GetFileName((string)Module1.Current.BatchToolSettings.AoiPrecipFile);
                    string strZonesRaster = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Analysis, true) +
                                            Constants.FILE_PRECIP_ZONE;
                    success = await AnalysisTools.CalculatePrecipitationZonesAsync(strLayer, strZonesRaster);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.PrismZones_Checked = false;
                    }
                }

                if (calculateAspect)
                {
                    success = await AnalysisTools.CalculateAspectZonesAsync();

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.AspectZones_Checked = false;
                    }
                }

                if (calculateSlope)
                {
                    string strLayer = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Surfaces, true) +
                                      Constants.FILE_SLOPE;
                    string strZonesRaster = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Analysis, true) +
                                            Constants.FILE_SLOPE_ZONE;
                    string strMaskPath = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Aoi, true) + Constants.FILE_AOI_BUFFERED_VECTOR;
                    IList <BA_Objects.Interval> lstInterval = AnalysisTools.GetSlopeClasses();
                    success = await AnalysisTools.CalculateZonesAsync(Module1.Current.Aoi.FilePath, strLayer,
                                                                      lstInterval, strZonesRaster, strMaskPath, "SLOPE");

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.SlopeZones_Checked = false;
                    }
                }

                if (calculateElevation)
                {
                    success = await AnalysisTools.CalculateElevationZonesAsync(Module1.Current.Aoi.FilePath);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.ElevationZones_Checked = false;
                    }
                }

                if (calculateSitesZones)
                {
                    Uri  uri       = new Uri(GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Layers));
                    bool hasSnotel = await GeodatabaseTools.FeatureClassExistsAsync(uri, Constants.FILE_SNOTEL);

                    bool hasSnowCourse = await GeodatabaseTools.FeatureClassExistsAsync(uri, Constants.FILE_SNOW_COURSE);

                    if (hasSnotel || hasSnowCourse)
                    {
                        success = await AnalysisTools.CalculateSitesZonesAsync(Module1.Current.Aoi.FilePath, hasSnotel, hasSnowCourse);
                    }
                    else
                    {
                        MessageBox.Show("No sites found to create sites zone layers!!", "BAGIS-PRO");
                    }

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.SitesZones_Checked = false;
                    }
                }

                bool bSkipPotentialSites = false;
                if (bufferRoads)
                {
                    Uri  uri     = new Uri(GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Layers));
                    bool bExists = await GeodatabaseTools.FeatureClassExistsAsync(uri, Constants.FILE_ROADS);

                    if (!bExists)
                    {
                        MessageBox.Show("The roads layer is missing. Clip the roads layer before creating the roads analysis layer!!", "BAGIS-PRO");
                        Module1.Current.ModuleLogManager.LogDebug(nameof(GenerateLayersAsync),
                                                                  "Unable to buffer roads because fs_roads layer does not exist. Process stopped!!");
                        return;
                    }

                    // This could come from the UI eventually
                    string strDistance = Module1.Current.BatchToolSettings.RoadsAnalysisBufferDistance + " " +
                                         Module1.Current.BatchToolSettings.RoadsAnalysisBufferUnits;
                    success = await AnalysisTools.GenerateProximityRoadsLayerAsync(uri, strDistance);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.Roads_Checked = false;
                    }
                    else
                    {
                        bSkipPotentialSites = true;     // may skip combined potential sites because this layer couldn't be generated
                    }
                }

                if (extractPublicLand)
                {
                    success = await AnalysisTools.GetFederalNonWildernessLandsAsync(Module1.Current.Aoi.FilePath);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.FederalLand_Checked = false;
                    }
                    else
                    {
                        bSkipPotentialSites = true;     // may skip combined potential sites because this layer couldn't be generated
                    }
                }

                if (extractBelowTreeline)
                {
                    success = await AnalysisTools.ExtractBelowTreelineAsync(Module1.Current.Aoi.FilePath);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.BelowTreeline_Checked = false;
                    }
                    else
                    {
                        bSkipPotentialSites = true;     // may skip combined potential sites because this layer couldn't be generated
                    }
                }

                if (!bSkipPotentialSites)
                {
                    if (bufferRoads || extractPublicLand || extractBelowTreeline)
                    {
                        // if either of the underlying layers changed, we need to recalculate the
                        // potential sites layer
                        success = await AnalysisTools.CalculatePotentialSitesAreaAsync(Module1.Current.Aoi.FilePath);

                        if (success != BA_ReturnCode.Success)
                        {
                            MessageBox.Show("An error occurred while generating the potential sites layer!!", "BAGIS-PRO");
                        }
                    }
                }

                if (elevPrecipCorr == true)
                {
                    string strLayer = GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Prism, true) +
                                      Path.GetFileName((string)Module1.Current.BatchToolSettings.AoiPrecipFile);

                    Uri uriPrism = new Uri(GeodatabaseTools.GetGeodatabasePath(Module1.Current.Aoi.FilePath, GeodatabaseNames.Prism));
                    success = await AnalysisTools.CalculateElevPrecipCorrAsync(Module1.Current.Aoi.FilePath, uriPrism,
                                                                               Path.GetFileName((string)Module1.Current.BatchToolSettings.AoiPrecipFile));


                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.ElevPrecipCorr_Checked = false;
                    }
                }

                if (calculateSweDelta == true)
                {
                    success = await AnalysisTools.CalculateSWEDeltaAsync(Module1.Current.Aoi.FilePath);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.SWE_Delta_Checked = false;
                    }
                }

                if (calculatePrecipContrib == true)
                {
                    success = await AnalysisTools.CalculatePrecipitationContributionAsync(Module1.Current.Aoi.FilePath, -1);

                    success = await AnalysisTools.CalculateQuarterlyPrecipitationAsync(Module1.Current.Aoi);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.Precip_Contrib_Checked = false;
                    }
                }

                if (winterPrecip == true)
                {
                    success = await AnalysisTools.GenerateWinterPrecipitationLayerAsync(Module1.Current.Aoi);

                    if (success == BA_ReturnCode.Success)
                    {
                        layersPane.Winter_Precip_Checked = false;
                    }
                }

                if (success == BA_ReturnCode.Success)
                {
                    MessageBox.Show("Analysis layers generated !!", "BAGIS-PRO");
                }
            }
            catch (Exception ex)
            {
                Module1.Current.ModuleLogManager.LogError(nameof(GenerateLayersAsync),
                                                          "Exception: " + ex.Message);
            }
        }