GISHydroNXT System Documentation

#*********************************************************************************
# Author:       UMD
# Date:         24-07-2018
# Modified:     n/a
# Classes:      ControlBoundaries()
# Functions:    n/a  
# Modules:      arcpy ; os ; time
# Comments:     n/a
#*********************************************************************************
class ControlBoundaries(object):
    """Implementation for GISHydroNXT_addin.tool3 (Tool)"""
    def __init__(self):
        self.enabled = False
        self.cursor = 3
        self.shape = "Line"
    def onLine(self, line_geometry):
        arcpy.env.scratchWorkspace = scratchfolder
        arcpy.env.workspace = optfolder
        controlbound_dir = optfolder + "/control_boundaries.shp"
        watershed_dir = optfolder + "/watershed.shp"
        watershed2_dir = optfolder + "/watershed2.shp"
        watershed_aux_dir = optfolder + "/watershed_aux.shp"
        controlbound_dir = optfolder + "/control_boundaries.shp"
        watershed_clip_dir = optfolder + "/watershed_clip.shp"
        outlet_ws_dir = optfolder + "/outlet_ws.shp"
        basin_dir = optfolder + "/basingrid"
        flowdir_dir = optfolder + "/flowdir"
        flowdir_cb_dir = optfolder + "/flowdir_cb"
        flowdir_dem_dir = optfolder + "/flowdir_dem"
        arcpy.CopyFeatures_management(line_geometry, controlbound_dir)
        
        user_prompt = pythonaddins.MessageBox("This tool will cut the watershed, are you sure?", "Control Boundaries", 4)

        if user_prompt == "Yes":
            arcpy.Rename_management(watershed_dir, watershed_aux_dir)

            arcpy.FeatureToPolygon_management([watershed_aux_dir,controlbound_dir],watershed_clip_dir,"", "NO_ATTRIBUTES", "")
            arcpy.MakeFeatureLayer_management(watershed_clip_dir,"clip_lyr") 
            arcpy.SelectLayerByLocation_management("clip_lyr", "intersect", outlet_ws_dir)
            arcpy.CopyFeatures_management("clip_lyr", watershed2_dir)

            mxd = arcpy.mapping.MapDocument("CURRENT")
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                if lyr.name == "watershed_clip":
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "watershed_aux":
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "clip_lyr":
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "flowdir":
                    arcpy.mapping.RemoveLayer(df, lyr)

            arcpy.Delete_management(watershed_clip_dir,"")
            arcpy.Delete_management(watershed_aux_dir,"")
            arcpy.Delete_management(basin_dir,"")
            arcpy.Delete_management(flowdir_dir,"")

            arcpy.Clip_management(flowdir_dem_dir,"#", flowdir_cb_dir, watershed2_dir,"", "ClippingGeometry")
            wshed = arcpy.sa.Watershed(flowdir_cb_dir, optfolder + "/outletcell", "VALUE")
            shed = arcpy.sa.Con(wshed >= 0, 1, IsNull(wshed))
            shed.save(basin_dir)
            arcpy.RasterToPolygon_conversion(basin_dir, watershed_dir, "NO_SIMPLIFY", "VALUE")
            arcpy.Clip_management(flowdir_dem_dir,"#", flowdir_dir,watershed_dir,"", "ClippingGeometry")
            
            mxd = arcpy.mapping.MapDocument("CURRENT")
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                if lyr.name == "watershed2":
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "flowdir_cb":
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "flowdir_cb2":
                    arcpy.mapping.RemoveLayer(df, lyr)
            arcpy.ApplySymbologyFromLayer_management(arcpy.mapping.Layer("watershed"),r"" + Directory + "/data/mdfiles/legends/watershed.lyr")
            arcpy.Delete_management(watershed2_dir,"")
            arcpy.Delete_management(flowdir_cb_dir,"")

        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layers = arcpy.mapping.ListLayers(mxd, "", df)
        for lyr in layers:
            if lyr.name == "control_boundaries":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "basingrid":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "flowdir":
                lyr.visible = False
        arcpy.Delete_management(controlbound_dir,"")