コード例 #1
0
        public static async Task <FolderType> GetWeaselAoiFolderTypeAsync(string folderPath)
        {
            if (folderPath.IndexOf(".gdb") > -1)
            {
                // The calls below will fail if the folder is a geodatabase folder
                return(FolderType.FOLDER);
            }
            IList <string> lstAoiLayers = new List <string>
            {
                folderPath + @"\aoi",
                folderPath + @"\aoibagis"
            };
            IList <string> lstExistingLayers = await GeneralTools.RasterDatasetsExistAsync(lstAoiLayers);

            if (lstExistingLayers.Count > 0)
            {
                return(FolderType.AOI);
            }
            lstAoiLayers.Clear();
            lstAoiLayers.Add(folderPath + @"\aoi_v.shp");
            lstExistingLayers = await GeneralTools.ShapefilesExistAsync(lstAoiLayers);

            if (lstExistingLayers.Count > 0)
            {
                return(FolderType.BASIN);
            }
            return(FolderType.FOLDER);
        }
コード例 #2
0
        private async void RunImplAsync(object param)
        {
            // Bring GP History tool forward
            var cmdShowHistory = FrameworkApplication.GetPlugInWrapper("esri_geoprocessing_showToolHistory") as ICommand;

            if (cmdShowHistory != null)
            {
                if (cmdShowHistory.CanExecute(null))
                {
                    cmdShowHistory.Execute(null);
                }
            }
            foreach (var oAoi in Names)
            {
                if (oAoi.AoiBatchIsSelected)
                {
                    // Currently only support AOI conversion but BASIN may be added in future
                    FolderType fType = await GeodatabaseTools.GetWeaselAoiFolderTypeAsync(oAoi.FilePath);

                    IList <string> lstExistingGdb = null;
                    if (fType == FolderType.AOI)
                    {
                        lstExistingGdb = CheckForBagisGdb(oAoi.FilePath);
                    }
                    else
                    {
                        lstExistingGdb = CheckForBasinGdb(oAoi.FilePath);
                    }

                    // Make directory for log if it doesn't exist
                    if (!Directory.Exists(oAoi.FilePath + "\\" + Constants.FOLDER_LOGS))
                    {
                        DirectoryInfo info = Directory.CreateDirectory(oAoi.FilePath + "\\" + Constants.FOLDER_LOGS);
                        if (info == null)
                        {
                            MessageBox.Show("Unable to create logs directory in Aoi folder!!", "BAGIS-PRO");
                        }
                    }
                    // Set logger to AOI directory
                    string logFolderName = oAoi.FilePath + "\\" + Constants.FOLDER_LOGS;
                    Module1.Current.ModuleLogManager.UpdateLogFileLocation(logFolderName);

                    // Delete old geodatabases if they exist
                    foreach (var geodatabasePath in lstExistingGdb)
                    {
                        IGPResult gpResult = await QueuedTask.Run(() =>
                        {
                            var parameters = Geoprocessing.MakeValueArray(geodatabasePath);
                            return(Geoprocessing.ExecuteToolAsync("Delete_management", parameters, null,
                                                                  CancelableProgressor.None, GPExecuteToolFlags.AddToHistory));
                        });

                        if (gpResult.IsFailed)
                        {
                            Module1.Current.ModuleLogManager.LogError(nameof(RunImplAsync),
                                                                      "Unable to delete geodatabase. Error code: " + gpResult.ErrorCode);
                            MessageBox.Show("Unable to delete geodatabase " + geodatabasePath + "!");
                        }
                    }

                    // Create new geodatabases
                    BA_ReturnCode success = await GeodatabaseTools.CreateGeodatabaseFoldersAsync(oAoi.FilePath, fType);

                    if (success == BA_ReturnCode.Success)
                    {
                        Module1.Current.ModuleLogManager.LogInfo(nameof(RunImplAsync),
                                                                 "Created geodatabases in " + oAoi.FilePath);
                    }
                    else
                    {
                        MessageBox.Show("Unable to create geodatabases in " + oAoi.FilePath + ". Check logs!");
                    }

                    // Assemble a dictionary with rasters we want to copy
                    IDictionary <string, string> rastersToCopy = GetDictOfReqRasters(oAoi.FilePath, fType);
                    // Accomodate two possible names for raster aoi boundary layer (aoibagis or aoi)
                    IList <string> lstTest = new List <string>
                    {
                        oAoi.FilePath + @"\aoibagis"
                    };
                    string         aoiGdb         = GeodatabaseTools.GetGeodatabasePath(oAoi.FilePath, GeodatabaseNames.Aoi, true);
                    IList <string> existingLayers = null;
                    if (fType == FolderType.AOI)
                    {
                        existingLayers = await GeneralTools.RasterDatasetsExistAsync(lstTest);

                        if (existingLayers.Count == 0)
                        {
                            lstTest.Clear();
                            string strLayer = oAoi.FilePath + @"\aoi";
                            lstTest.Add(strLayer);
                            existingLayers = await GeneralTools.RasterDatasetsExistAsync(lstTest);

                            if (existingLayers.Count > 0)
                            {
                                rastersToCopy[strLayer] = aoiGdb + Constants.FILE_AOI_RASTER;
                            }
                        }
                        else
                        {
                            rastersToCopy[oAoi.FilePath + @"\aoibagis"] = aoiGdb + Constants.FILE_AOI_RASTER;
                        }
                    }
                    // Check to see if optional layers are present
                    IDictionary <string, string> optRasterDict = GetDictOptWeaselRasters(oAoi.FilePath, fType);
                    existingLayers = await GeneralTools.RasterDatasetsExistAsync(optRasterDict.Keys);

                    foreach (var layerPath in existingLayers)
                    {
                        string gdbPath = optRasterDict[layerPath];
                        rastersToCopy[layerPath] = gdbPath;
                    }
                    // Raster layers with non-deterministic names in analysis and layers folders
                    string         strWeaselFolder = oAoi.FilePath + @"\layers";
                    string         strGdbPath      = GeodatabaseTools.GetGeodatabasePath(oAoi.FilePath, GeodatabaseNames.Layers, true);
                    IList <string> lstRasters      = await GeneralTools.GetLayersInFolderAsync(strWeaselFolder, "Raster Dataset");

                    foreach (var item in lstRasters)
                    {
                        rastersToCopy[strWeaselFolder + "\\" + item] = strGdbPath + item;
                    }
                    strWeaselFolder = oAoi.FilePath + @"\analysis";
                    strGdbPath      = GeodatabaseTools.GetGeodatabasePath(oAoi.FilePath, GeodatabaseNames.Analysis, true);
                    lstRasters      = await GeneralTools.GetLayersInFolderAsync(strWeaselFolder, "Raster Dataset");

                    foreach (var item in lstRasters)
                    {
                        rastersToCopy[strWeaselFolder + "\\" + item] = strGdbPath + item;
                    }

                    // Use Geoprocessor to copy the files
                    int errorCount = 0;
                    foreach (var key in rastersToCopy.Keys)
                    {
                        IGPResult gpResult = await QueuedTask.Run(() =>
                        {
                            var environments = Geoprocessing.MakeEnvironmentArray(workspace: oAoi.FilePath, cellSize: "MINOF");
                            var parameters   = Geoprocessing.MakeValueArray(key, rastersToCopy[key]);
                            return(Geoprocessing.ExecuteToolAsync("CopyRaster_management", parameters, null,
                                                                  CancelableProgressor.None, GPExecuteToolFlags.AddToHistory));
                        });

                        if (gpResult.IsFailed)
                        {
                            Module1.Current.ModuleLogManager.LogError(nameof(RunImplAsync),
                                                                      "Failed to copy raster " + key + "!");
                            errorCount++;
                        }
                    }
                    Module1.Current.ModuleLogManager.LogDebug(nameof(RunImplAsync),
                                                              "Raster copy completed with " + errorCount + " errors.");

                    // Assemble a dictionary with vectors we want to copy
                    IDictionary <string, string> vectorsToCopy = GetDictOfReqWeaselVectors(oAoi.FilePath, fType);
                    // Check for an optional vector
                    lstTest.Clear();
                    lstTest.Add(oAoi.FilePath + @"\unsnappedpp.shp");
                    existingLayers = await GeneralTools.ShapefilesExistAsync(lstTest);

                    if (existingLayers.Count > 0)
                    {
                        vectorsToCopy[oAoi.FilePath + @"\unsnappedpp.shp"] = aoiGdb + Constants.FILE_UNSNAPPED_POURPOINT;
                    }

                    // Vector layers with non-deterministic names in analysis and layers folders
                    strWeaselFolder = oAoi.FilePath + @"\layers";
                    strGdbPath      = GeodatabaseTools.GetGeodatabasePath(oAoi.FilePath, GeodatabaseNames.Layers, true);
                    IList <string> lstVectors = await GeneralTools.GetLayersInFolderAsync(strWeaselFolder, "Shapefile");

                    foreach (var item in lstVectors)
                    {
                        string noExtension = Path.GetFileNameWithoutExtension(item);
                        vectorsToCopy[strWeaselFolder + "\\" + item] = strGdbPath + noExtension;
                    }
                    strWeaselFolder = oAoi.FilePath + @"\analysis";
                    strGdbPath      = GeodatabaseTools.GetGeodatabasePath(oAoi.FilePath, GeodatabaseNames.Analysis, true);
                    lstVectors      = await GeneralTools.GetLayersInFolderAsync(strWeaselFolder, "Shapefile");

                    foreach (var item in lstVectors)
                    {
                        string noExtension = Path.GetFileNameWithoutExtension(item);
                        vectorsToCopy[strWeaselFolder + "\\" + item] = strGdbPath + noExtension;
                    }

                    // Use Geoprocessor to copy the files
                    errorCount = 0;
                    foreach (var entry in vectorsToCopy)
                    {
                        string strKey = entry.Key;
                    }
                    string strTempFile  = Path.GetFileName("tmpVector");
                    string strDirectory = "";
                    foreach (var entry in vectorsToCopy)
                    {
                        IGPResult gpResult = await QueuedTask.Run(() =>
                        {
                            var environments = Geoprocessing.MakeEnvironmentArray(workspace: oAoi.FilePath);
                            strDirectory     = Path.GetDirectoryName(entry.Value);
                            var parameters   = Geoprocessing.MakeValueArray(entry.Key, strDirectory, strTempFile);
                            return(Geoprocessing.ExecuteToolAsync("FeatureClassToFeatureClass_conversion", parameters, null,
                                                                  CancelableProgressor.None, GPExecuteToolFlags.AddToHistory));
                        });

                        if (gpResult.IsFailed)
                        {
                            Module1.Current.ModuleLogManager.LogError(nameof(RunImplAsync),
                                                                      "Failed to convert vector " + entry.Key + "!");
                            errorCount++;
                        }
                        else
                        {
                            //There is a bug with using converted shapefiles in Pro; We need to rename the converted file
                            //so that functions related to extent work
                            gpResult = await QueuedTask.Run(() =>
                            {
                                var environments = Geoprocessing.MakeEnvironmentArray(workspace: oAoi.FilePath);
                                strDirectory     = Path.GetDirectoryName(entry.Value);
                                var parameters   = Geoprocessing.MakeValueArray(strDirectory + "\\" + strTempFile, entry.Value);
                                return(Geoprocessing.ExecuteToolAsync("Rename_management", parameters, null,
                                                                      CancelableProgressor.None, GPExecuteToolFlags.AddToHistory));
                            });

                            if (gpResult.IsFailed)
                            {
                                Module1.Current.ModuleLogManager.LogError(nameof(RunImplAsync),
                                                                          "Failed to copy feature class " + entry.Key + "!");
                                errorCount++;
                            }
                        }
                    }
                    Module1.Current.ModuleLogManager.LogDebug(nameof(RunImplAsync),
                                                              "Vector copy completed with " + errorCount + " errors.");
                }
            }

            MessageBox.Show("Done!");
        }