def function(params): common.runSystemChecks( ) # Run to ensure config.xml is copied across to user_settings.xml if needed. This line can be removed after 31/10/18. # Get inputs p = common.paramsAsText(params) scratchPath = p[1] developerMode = common.strToBool(p[2]) if developerMode == True: developerMode = 'Yes' else: developerMode = 'No' # Override the default values from user settings file (if they exist in the file) try: configValues = [('scratchPath', scratchPath), ('developerMode', developerMode)] common.writeXML(configuration.userSettingsFile, configValues) log.info('Scratch path updated: ' + scratchPath) log.info('Developer mode updated: ' + developerMode) except Exception: raise
def function(params): try: pText = common.paramsAsText(params) # Get inputs runSystemChecks = common.strToBool(pText[1]) outputFolder = pText[5] yearAFolder = pText[6] yearBFolder = pText[7] slopeOption = pText[8] slopeAngle = pText[9] yearARain = pText[10] yearBRain = pText[11] yearASupport = pText[12] yearBSupport = pText[13] # Set option for LS-factor if slopeOption == 'Calculate based on slope and length only': lsOption = 'SlopeLength' elif slopeOption == 'Include upslope contributing area': lsOption = 'UpslopeArea' else: log.error('Invalid LS-factor option') sys.exit() # System checks and setup if runSystemChecks: common.runSystemChecks() # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # Set up logging output to file log.setupLogging(outputFolder) # Call RUSLE_scen_acc function RUSLE_scen_acc.function(outputFolder, yearAFolder, yearBFolder, lsOption, slopeAngle, yearARain, yearBRain, yearASupport, yearBSupport) # Set up filenames for display purposes soilLossA = os.path.join(outputFolder, "soillossA") soilLossB = os.path.join(outputFolder, "soillossB") soilLossDiff = os.path.join(outputFolder, "soillossDiff") arcpy.SetParameter(2, soilLossA) arcpy.SetParameter(3, soilLossB) arcpy.SetParameter(4, soilLossDiff) log.info("RUSLE accounts operations completed successfully") except Exception: log.exception("RUSLE accounts tool failed") raise
def function(params): try: pText = common.paramsAsText(params) runSystemChecks = common.strToBool(pText[1]) if params[2].name == 'Output_folder': outputFolder = pText[2] elif params[2].name == 'Land_extent_accounts': outputFolder = os.path.join(arcpy.env.scratchFolder, 'LCaccounts') LCaccounts = pText[2] lcOption = pText[3] inputLC = pText[4] openingLC = pText[5] closingLC = pText[6] openingField = pText[7] closingField = pText[8] lcTable = pText[9] lcCodeField = pText[10] lcNameField = pText[11] # System checks and setup if runSystemChecks: common.runSystemChecks() # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # Set up logging output to file log.setupLogging(outputFolder) # Call aggregation function lcOutputs = land_accounts.function(outputFolder, lcOption, inputLC, openingLC, closingLC, openingField, closingField, lcTable, lcCodeField, lcNameField) # Set up filenames for display purposes lcOpening = lcOutputs[0] lcClosing = lcOutputs[1] lcOpeningWithAccounts = lcOutputs[2] outCSV = lcOutputs[3] arcpy.SetParameter(12, lcOpening) arcpy.SetParameter(13, lcClosing) arcpy.SetParameter(14, outCSV) return lcOpeningWithAccounts, lcClosing, outCSV log.info("Land extent accounting operations completed successfully") except Exception: log.exception("Land extent accounting tool failed") raise
def function(params): try: pText = common.paramsAsText(params) runSystemChecks = common.strToBool(pText[1]) # Get inputs if params[2].name == 'Output_folder': outputFolder = pText[2] elif params[2].name == 'Species_richness': outputFolder = os.path.join(arcpy.env.scratchFolder, 'Species_richness') speciesRichness = pText[2] IUCN_rl_data = pText[4] studymask = pText[5] speciesdisplayname = pText[6] #aggregateMask = pText[7] # will add optional mask if want to calculate over aggregrate spatial units # System checks and setup if runSystemChecks: common.runSystemChecks() # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # Set up logging output to file log.setupLogging(outputFolder) # Call aggregation function outputStats = PAspeciesRIchness.function(outputFolder, dataSetsToAggregate, aggregateMask, maskFullyWithinSAM, dataToAggregate) # Set up filenames for display purposes RareSpeciesRichness = os.path.join(outputFolder, "RareSpeciesRichness.shp") arcpy.CopyFeatures_management(outputStats[0], RareSpeciesRichness) arcpy.SetParameter(3, SpeciesRichness) return outputStats[0], PAspeciesRIchness log.info("Rare species richness operations completed successfully") except Exception: log.exception("Rare species richness tool failed") raise
def function(params): try: pText = common.paramsAsText(params) # Get inputs runSystemChecks = common.strToBool(pText[1]) outputFolder = pText[2] inputRaster = pText[5] aggregationZones = pText[6] aggregationColumn = pText[7] rerun = False # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # System checks and setup if runSystemChecks: common.runSystemChecks(outputFolder, rerun) # Set up logging output to file log.setupLogging(outputFolder) # Set up progress log file progress.initProgress(outputFolder, rerun) # Write input params to XML common.writeParamsToXML(params, outputFolder) # Call zonal statistics function CalcZonal.function(outputFolder, inputRaster, aggregationZones, aggregationColumn) # Set up filenames for display purposes outRaster = os.path.join(outputFolder, 'statRaster') outTable = os.path.join(outputFolder, 'statTable.dbf') # Set up outputs arcpy.SetParameter(3, outRaster) arcpy.SetParameter(4, outTable) log.info("Zonal statistics operations completed successfully") except Exception: log.exception("Zonal statistics tool failed") raise
def function(params): try: pText = common.paramsAsText(params) # Get inputs runSystemChecks = common.strToBool(pText[1]) outputFolder = pText[2] preprocessFolder = pText[4] # R-factor rData = pText[5] # LS-factor slopeOption = pText[6] slopeAngle = pText[7] # K-factor kOption = pText[8] soilData = pText[9] soilCode = pText[10] # C-factor cOption = pText[11] landCoverData = pText[12] landCoverCode = pText[13] # P-factor supportData = pText[14] saveFactors = common.strToBool(pText[15]) # Rerun parameter may not present when tool run as part of a batch run tool. If it is not, set rerun to False. try: rerun = common.strToBool(pText[16]) except IndexError: rerun = False except Exception: raise # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # System checks and setup if runSystemChecks: common.runSystemChecks(outputFolder, rerun) # Set up logging output to file log.setupLogging(outputFolder) # Set up progress log file progress.initProgress(outputFolder, rerun) # Write input params to XML common.writeParamsToXML(params, outputFolder) # Set option for LS-factor if slopeOption == 'Calculate based on slope and length only': lsOption = 'SlopeLength' elif slopeOption == 'Include upslope contributing area': lsOption = 'UpslopeArea' else: log.error('Invalid LS-factor option') sys.exit() # Set soilOption for K-factor if kOption == 'Use preprocessed soil data': soilOption = 'PreprocessSoil' elif kOption == 'Use local K-factor dataset': soilOption = 'LocalSoil' else: log.error('Invalid soil erodibility option') sys.exit() # Set lcOption for C-factor if cOption == 'Use preprocessed land cover data': lcOption = 'PrerocessLC' elif cOption == 'Use local C-factor dataset': lcOption = 'LocalCfactor' else: log.error('Invalid C-factor option') sys.exit() # Call RUSLE function soilLoss = RUSLE.function(outputFolder, preprocessFolder, lsOption, slopeAngle, soilOption, soilData, soilCode, lcOption, landCoverData, landCoverCode, rData, saveFactors, supportData, rerun) # Set up filenames for display purposes soilLoss = os.path.join(outputFolder, "soilloss") arcpy.SetParameter(3, soilLoss) return soilLoss log.info("RUSLE operations completed successfully") except Exception: log.exception("RUSLE tool failed") raise
def function(params): try: ################### ### Read inputs ### ################### pText = common.paramsAsText(params) outputFolder = pText[1] inputDEM = common.fullPath(pText[2]) inputStudyAreaMask = pText[3] inputLC = pText[4] lcCode = pText[5] inputSoil = pText[6] soilCode = pText[7] reconDEM = common.strToBool(pText[8]) inputStreamNetwork = pText[9] streamAccThresh = pText[10] riverAccThresh = pText[11] smoothDropBuffer = pText[12] smoothDrop = pText[13] streamDrop = pText[14] rerun = common.strToBool(pText[15]) log.info('Inputs read in') ########################### ### Tool initialisation ### ########################### # Create Baseline folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # Set up logging output to file log.setupLogging(outputFolder) # Run system checks common.runSystemChecks(outputFolder, rerun) # Set up progress log file progress.initProgress(outputFolder, rerun) # Write input params to XML common.writeParamsToXML(params, outputFolder, 'PreprocessDEM') log.info('Tool initialised') ######################## ### Define filenames ### ######################## files = common.getFilenames('preprocess', outputFolder) studyAreaMask = files.studyareamask outputLCras = files.lc_ras outputLCvec = files.lc_vec outputSoilras = files.soil_ras outputSoilvec = files.soil_vec ############################### ### Set temporary variables ### ############################### prefix = os.path.join(arcpy.env.scratchGDB, 'base_') DEMTemp = prefix + 'DEMTemp' clippedDEM = prefix + 'clippedDEM' clippedLC = prefix + 'clippedLC' clippedSoil = prefix + 'clippedSoil' clippedStreamNetwork = prefix + 'clippedStreamNetwork' studyAreaMaskTemp = prefix + "studyAreaMaskTemp" studyAreaMaskBuff = prefix + "studyAreaMaskBuff" studyAreaMaskDiss = prefix + "studyAreaMaskDiss" log.info('Temporary variables set') # Check formats of inputs lcFormat = arcpy.Describe(inputLC).dataType soilFormat = arcpy.Describe(inputSoil).dataType ################### ### Data checks ### ################### codeBlock = 'Data checks 1' if not progress.codeSuccessfullyRun(codeBlock, outputFolder, rerun): inputFiles = [inputDEM, inputStudyAreaMask, inputLC, inputSoil] if inputStreamNetwork is not None: inputFiles.append(inputStreamNetwork) for file in inputFiles: common.checkSpatialRef(file) # Set environment variables arcpy.env.snapRaster = inputDEM arcpy.env.cellSize = inputDEM arcpy.env.compression = "None" cellsizedem = float( arcpy.GetRasterProperties_management(inputDEM, "CELLSIZEX").getOutput(0)) # Get spatial references of DEM and study area mask DEMSpatRef = arcpy.Describe(inputDEM).SpatialReference maskSpatRef = arcpy.Describe(inputStudyAreaMask).SpatialReference # Reproject study area mask if it does not have the same coordinate system as the DEM if not common.equalProjections(DEMSpatRef, maskSpatRef): warning = "Study area mask does not have the same coordinate system as the DEM" log.warning(warning) common.logWarnings(outputFolder, warning) warning = "Mask coordinate system is " + maskSpatRef.Name + " while DEM coordinate system is " + DEMSpatRef.Name log.warning(warning) common.logWarnings(outputFolder, warning) warning = "Reprojecting study area mask to DEM coordinate system" log.warning(warning) common.logWarnings(outputFolder, warning) arcpy.Project_management(inputStudyAreaMask, studyAreaMaskTemp, DEMSpatRef) arcpy.CopyFeatures_management(studyAreaMaskTemp, studyAreaMask) else: arcpy.CopyFeatures_management(inputStudyAreaMask, studyAreaMask) # If DEM is large, clip it to a large buffer around the study area mask (~5km) inputDEM = baseline.clipLargeDEM(inputDEM, studyAreaMask) rasterInputFiles = [] fcInputFiles = [] # Sort land cover and soil into appropriate arrays based on data type if lcFormat in ['RasterDataset', 'RasterLayer']: rasterInputFiles.append(inputLC) outputLC = os.path.join(outputFolder, 'landcover') elif lcFormat in ['ShapeFile', 'FeatureClass']: fcInputFiles.append(inputLC) outputLC = os.path.join(outputFolder, 'landcover.shp') if soilFormat in ['RasterDataset', 'RasterLayer']: rasterInputFiles.append(inputSoil) outputSoil = os.path.join(outputFolder, 'soil') elif soilFormat in ['ShapeFile', 'FeatureClass']: fcInputFiles.append(inputSoil) outputSoil = os.path.join(outputFolder, 'soil.shp') if reconDEM is True and inputStreamNetwork is None: log.error( 'Cannot recondition the DEM without an input stream network' ) log.error('Please provide an input stream network') sys.exit() # If the user has provided a stream network, add it to the list of inputs to check if inputStreamNetwork is not None: fcInputFiles.append(inputStreamNetwork) # Check that the inputs contain data for ras in rasterInputFiles: if ras is not None: # Check file size fileSizeGB = baseline.checkRasterSizeGB(ras) if fileSizeGB < 1.0: baseline.checkInputRaster(ras, outputFolder) else: log.warning( "Cannot check if raster is empty or all NoData because it is too large" ) log.warning( "Please ensure this raster is not empty or all NoData: " + str(ras)) for fc in fcInputFiles: if fc is not None: baseline.checkInputFC(fc, outputFolder) # Check that the land cover and soil FCs have the linking codes specified by the user if lcFormat in ['ShapeFile', 'FeatureClass']: if len(arcpy.ListFields(inputLC, lcCode)) != 1: log.error('Field ' + lcCode + 'does not exist in feature class ' + inputLC) sys.exit() if soilFormat in ['ShapeFile', 'FeatureClass']: if len(arcpy.ListFields(inputSoil, soilCode)) != 1: log.error('Field ' + soilCode + 'does not exist in feature class ' + inputSoil) sys.exit() progress.logProgress(codeBlock, outputFolder) ############################### ### Tidy up study area mask ### ############################### codeBlock = 'Tidy up study area mask' if not progress.codeSuccessfullyRun(codeBlock, outputFolder, rerun): # Check how many polygons are in the mask shapefile numPolysInMask = int( arcpy.GetCount_management(studyAreaMask).getOutput(0)) if numPolysInMask > 1: # Reduce multiple features where possible arcpy.Union_analysis(studyAreaMask, studyAreaMaskDiss, "ONLY_FID", "", "NO_GAPS") arcpy.Dissolve_management(studyAreaMaskDiss, studyAreaMask, "", "", "SINGLE_PART", "DISSOLVE_LINES") # Buffer study area mask baseline.bufferMask(inputDEM, studyAreaMask, outputStudyAreaMaskBuff=studyAreaMaskBuff) log.info('Study area mask buffered') progress.logProgress(codeBlock, outputFolder) ####################### ### Clip input data ### ####################### codeBlock = 'Clip inputs' if not progress.codeSuccessfullyRun(codeBlock, outputFolder, rerun): baseline.clipInputs(outputFolder, studyAreaMaskBuff, inputDEM, inputLC, inputSoil, inputStreamNetwork, outputDEM=clippedDEM, outputLC=clippedLC, outputSoil=clippedSoil, outputStream=clippedStreamNetwork) progress.logProgress(codeBlock, outputFolder) ############################################## ### Coverage checks on soil and land cover ### ############################################## codeBlock = 'Do coverage checks on clipped land cover and soil' if not progress.codeSuccessfullyRun(codeBlock, outputFolder, rerun): # Do coverage checks on land cover and soil and copy to outputFolder if lcFormat in ['RasterDataset', 'RasterLayer']: lcMask = common.extractRasterMask(clippedLC) common.checkCoverage(lcMask, studyAreaMaskBuff, inputLC) arcpy.CopyRaster_management(clippedLC, outputLCras) elif lcFormat in ['ShapeFile', 'FeatureClass']: lcMask = common.dissolvePolygon(clippedLC) common.checkCoverage(lcMask, studyAreaMaskBuff, inputLC) arcpy.CopyFeatures_management(clippedLC, outputLCvec) if soilFormat in ['RasterDataset', 'RasterLayer']: soilMask = common.extractRasterMask(clippedSoil) common.checkCoverage(soilMask, studyAreaMaskBuff, inputLC) arcpy.CopyRaster_management(clippedSoil, outputSoilras) elif soilFormat in ['ShapeFile', 'FeatureClass']: soilMask = common.dissolvePolygon(clippedSoil) common.checkCoverage(soilMask, studyAreaMaskBuff, inputSoil) arcpy.CopyFeatures_management(clippedSoil, outputSoilvec) progress.logProgress(codeBlock, outputFolder) ###################################### ### Convert LC and soil to rasters ### ###################################### # For the RUSLE tool, the LC and soil must be in raster format codeBlock = 'Convert land cover and soil to rasters' if not progress.codeSuccessfullyRun(codeBlock, outputFolder, rerun): if lcFormat in ['ShapeFile', 'FeatureClass']: arcpy.PolygonToRaster_conversion(clippedLC, lcCode, outputLCras, "CELL_CENTER", "", cellsizedem) log.info('Land cover raster produced') if soilFormat in ['ShapeFile', 'FeatureClass']: arcpy.PolygonToRaster_conversion(clippedSoil, soilCode, outputSoilras, "CELL_CENTER", "", cellsizedem) log.info('Soil raster produced') # Delete intermediate files arcpy.Delete_management(clippedLC) arcpy.Delete_management(clippedSoil) progress.logProgress(codeBlock, outputFolder) ########################### ### Run HydTopo process ### ########################### log.info("*** Preprocessing DEM ***") preprocess_dem.function(outputFolder, clippedDEM, studyAreaMask, clippedStreamNetwork, streamAccThresh, riverAccThresh, smoothDropBuffer, smoothDrop, streamDrop, reconDEM, rerun) except Exception: arcpy.SetParameter(0, False) log.exception("Preprocessing DEM functions did not complete") raise
def function(params): class DataToAggregate: def __init__(self, dataSet, linkCode): self.dataSet = dataSet self.linkCode = linkCode try: pText = common.paramsAsText(params) # Get inputs if params[2].name == 'Output_folder': outputFolder = pText[2] elif params[2].name == 'Aggregated_data': outputFolder = os.path.join(arcpy.env.scratchFolder, 'AggregatedData') aggregatedData = pText[2] runSystemChecks = common.strToBool(pText[1]) dataToAggregate = pText[7] classificationColumn = pText[8] aggregateMask = pText[9] maskFullyWithinSAM = common.strToBool(pText[10]) # System checks and setup if runSystemChecks: common.runSystemChecks() # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # Set up logging output to file log.setupLogging(outputFolder) # Initialise variables dataSetsToAggregate = [DataToAggregate(dataToAggregate, classificationColumn)] # Call aggregation function outputStats = aggregate_data.function(outputFolder, dataSetsToAggregate, aggregateMask, maskFullyWithinSAM, dataToAggregate) # Set up filenames for display purposes InvSimpson = os.path.join(outputFolder, "InverseSimpsonIndex.shp") Shannon = os.path.join(outputFolder, "ShannonIndex.shp") meanPatch = os.path.join(outputFolder, "MeanPatchSize.shp") numCovers = os.path.join(outputFolder, "NumCovers.shp") arcpy.CopyFeatures_management(outputStats[0], InvSimpson) arcpy.CopyFeatures_management(outputStats[0], Shannon) arcpy.CopyFeatures_management(outputStats[0], meanPatch) arcpy.CopyFeatures_management(outputStats[0], numCovers) arcpy.SetParameter(3, InvSimpson) arcpy.SetParameter(4, Shannon) arcpy.SetParameter(5, numCovers) arcpy.SetParameter(6, meanPatch) return outputStats[0], InvSimpson, Shannon, numCovers, meanPatch log.info("Aggregation operations completed successfully") except Exception: log.exception("Aggregate data tool failed") raise
def function(params): try: pText = common.paramsAsText(params) # Get inputs runSystemChecks = common.strToBool(pText[1]) outputFolder = pText[5] yearAFolder = pText[6] yearBFolder = pText[7] # Inputs constant between the two years slopeOption = pText[8] slopeAngle = pText[9] rData = pText[10] soilData = pText[11] soilCode = pText[12] # Land covers YearALCData = pText[13] YearALCCode = pText[14] YearBLCData = pText[15] YearBLCCode = pText[16] # Support factors YearAPData = pText[17] YearBPData = pText[18] saveFactors = False # Set option for LS-factor if slopeOption == 'Calculate based on slope and length only': lsOption = 'SlopeLength' elif slopeOption == 'Include upslope contributing area': lsOption = 'UpslopeArea' else: log.error('Invalid LS-factor option') sys.exit() # System checks and setup if runSystemChecks: common.runSystemChecks() # Create output folder if not os.path.exists(outputFolder): os.mkdir(outputFolder) # Set up logging output to file log.setupLogging(outputFolder) # Call RUSLE_accounts function RUSLE_accounts.function(outputFolder, yearAFolder, yearBFolder, lsOption, slopeAngle, rData, soilData, soilCode, YearALCData, YearALCCode, YearBLCData, YearBLCCode, YearAPData, YearBPData, saveFactors) # Set up filenames for display purposes soilLossA = os.path.join(outputFolder, "soillossA") soilLossB = os.path.join(outputFolder, "soillossB") soilLossDiff = os.path.join(outputFolder, "soillossDiff") arcpy.SetParameter(2, soilLossA) arcpy.SetParameter(3, soilLossB) arcpy.SetParameter(4, soilLossDiff) log.info("RUSLE accounts operations completed successfully") except Exception: log.exception("RUSLE accounts tool failed") raise