AnsweredAssumed Answered

How to set an separate input and output workspace environment?

Question asked by Playa on Mar 13, 2017

I have written a Python module that handles the pre-processing and ArcHydro. As part of the pre-processing it generates a set of rasters that are saved in a separate directory. The following rasters are then used as input to generate Feature Classes that are then saved in a File Geodatabase in a separate directory. I have split the following module into two functions, one for the rasters and the other for the Feature Classes. I then have a main function below that calls both the functions to pre-process the rasters and feature classes. In the first function I have set the workspace environment to the rasters directory that works well. The problem that I have is within the second function where I need to access the rasters as input but need to alter the output so that the Feature Classes are saved into the File Geodatabase without having to specify the output path manually for each Feature Class. I have attached below the directory structure of the rasters folder as well as the File Geodatabase where the Feature Classes are saved, as well as my current code.

 

Any advice in how to handle separate input and output workspace environments without having to specify the paths manually will be appreciated.

 

 

 

'''
Created on March 13, 2017

ArcHydro Main Model

@author: PeterW
'''


# import system modules and site-packages
from pathlib import Path
import arcpy
import ArcHydroTools


# check out extensions and set environment settings
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True


def rasters_main(rasters_workspace, dem):
    """ Create Rasters as part of ArcHydro Pre-processing"""
    # set current workspace
    arcpy.env.workspace = rasters_workspace
    # create Fil Sinks raster
    arcpy.AddMessage("Processing Fill Sinks")
    ArcHydroTools.FillSinks(dem, "fil")
    # create Flow Direction raster
    arcpy.AddMessage("Processing Flow Direction")
    ArcHydroTools.FlowDirection("fil", "fdr")
    # create Flow Accumulation raster
    arcpy.AddMessage("Processing Flow Accumulation")
    ArcHydroTools.FlowAccumulation("fdr", "fac")
    # create Stream Threshold raster
    arcpy.AddMessage("Processing Flow Stream Threshold")
    maxcellsResult = arcpy.GetRasterProperties_management("fac",
                                                          "MAXIMUM")
    maxcells = maxcellsResult.getOutput(0)
    arcpy.AddMessage("{} Maximum Cells".format(maxcells))
    stream_threshold_numcells = (int(maxcells)*0.03125/100)
    arcpy.AddMessage("{} Stream Threshold".format(stream_threshold_numcells))
    # Create Stream Definition raster
    arcpy.AddMessage("Processing Stream Definition")
    ArcHydroTools.StreamDefinition("fac", stream_threshold_numcells, "str")
    # Create Stream Link raster
    arcpy.AddMessage("Processing Stream Segmentation")
    ArcHydroTools.StreamSegmentation("str", "fdr", "strlnk")
    # Create Catchment Grid raster
    arcpy.AddMessage("Processing Catchment Grid Delineation")
    ArcHydroTools.CatchmentGridDelineation("fdr", "strlnk",
                                           "cat")


def vector_main(fgdb_workspace):
    """Create Feature Classes as part of
    ArcHydro Pre-processing"""

    # Create Catchment
    catchments = "{0}\\{1}".format(Path(fgdb_workspace, "Layers"), "Catchment") # repeating output workspace
    arcpy.AddMessage("Processing Catchment Polygon")
    ArcHydroTools.CatchmentPolyProcessing("cat", catchments)
    # Create DrainageLine
    drainageline = "{0}\\{1}".format(Path(fgdb_workspace, "Layers"),
                                     "DrainageLine") # repeating output workspace
    arcpy.AddMessage("Processing DrainageLine")
    ArcHydroTools.DrainageLineProcessing("strlnk", "fdr",
                                         drainageline)
    # Create AdjointCatchment
    adjointcatchment = "{0}\\{1}".format(Path(fgdb_workspace, "Layers"),
                                         "AdjointCatchment") # repeating output workspace
    arcpy.AddMessage("Processing Adjoint Catchment")
    ArcHydroTools.AdjointCatchment(drainageline, catchments,
                                   adjointcatchment)


arcpy.CheckInExtension("Spatial")


def main(rasters_workspace, dem, fgdb_workspace):
    rasters_main(rasters_workspace, dem)
    vector_main(fgdb_workspace)


if __name__ == "__main__":
    main(rasters_workspace = r"E:\Projects\2016\01_Bertrand_Small_Projects\G113268\ArcHydro\Model03\Layers03",
         dem = r"E:\Projects\2016\01_Bertrand_Small_Projects\G113268\ArcHydro\DEM2\raw",
         fgdb_workspace = r"E:\Projects\2016\01_Bertrand_Small_Projects\G113268\ArcHydro\Model03\Model03.gdb")

 

Outcomes