GISHydroNXT System Documentation

#*********************************************************************************
# Author:       UMD
# Date:         24-07-2018
# Modified:     n/a
# Classes:      WatershedDelineation() 
# Functions:    n/a
# Modules:      arcpy 
# Comments:     n/a
#*********************************************************************************
class WatershedDelineation(object):
    """Implementation for GISHydroNXT_addin.tool2 (Tool)"""
    def __init__(self):
        self.enabled = False
        self.cursor = 3
        
    def onMouseDownMap(self, x, y, button, shift):
        #*******************************************************************************************************
        # Extract outlet points from point geometry
        #*******************************************************************************************************        
        arcpy.env.scratchWorkspace = scratchfolder
        arcpy.env.workspace = optfolder
        arcpy.env.extent = "MAXOF"
        arcpy.env.addOutputsToMap = True
        outletxy = ExtractByPoints(optfolder + "/InfStreams", [arcpy.Point(x,y)], "INSIDE")
        outletxy.save(optfolder + "/outletcell")
        
        arcpy.CopyFeatures_management(arcpy.PointGeometry(arcpy.Point(x,y)), optfolder + "/outlet_ws.shp")
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layers = arcpy.mapping.ListLayers(mxd, "", df)
        for lyr in layers:
            if lyr.name == "outlet_ws":
                arcpy.mapping.RemoveLayer(df, lyr)

        # outlet from point geometry
        #*****************************************************************************
        pnt = arcpy.Point(x,y)
        global xoutletstring
        xoutletstring = str(pnt.X)
        
        global youtletstring
        youtletstring = str(pnt.Y)
        outletcell = arcpy.management.GetCellValue(optfolder + "/dem", "{} {}".format(x, y))
        
        global outletelev
        outletelev = float(outletcell.getOutput(0))

        flowdir_dir = optfolder + "/flowdir"
        flowdir_dem_dir = optfolder + "/flowdir_dem"
        watershed_dir = optfolder + "/watershed.shp"
        basin_dir = optfolder + "/basingrid"

        wshed = arcpy.sa.Watershed(flowdir_dem_dir, optfolder + "/outletcell", "VALUE") # watershed tool execution
        shed = arcpy.sa.Con(wshed >= 0, 1, IsNull(wshed))
        shed.save(basin_dir)
        arcpy.RasterToPolygon_conversion(basin_dir, watershed_dir, "NO_SIMPLIFY", "VALUE")
        arcpy.CopyRaster_management(flowdir_dem_dir,flowdir_dir)

        #*******************************************************************************************************
        # turn layers ON/OFF in current data frame
        #*******************************************************************************************************
        
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        wshExtent = df.extent
        wshExtent.XMin, wshExtent.YMin = extent.XMin, extent.YMin
        wshExtent.XMax, wshExtent.YMax = extent.XMax, extent.YMax
        df.extent = wshExtent
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
            if lyr.name == "flowdir":
                lyr.visible = False
            if lyr.name == "watershed":
                arcpy.ApplySymbologyFromLayer_management(lyr,r"" + Directory + "/data/mdfiles/legends/watershed.lyr")
        
        global glob_lu_count
        glob_lu_count = 0
        #*******************************************************************************************************
        # turn watershed delineation OFF and Basin Composition ON
        #*******************************************************************************************************
        tool2.enabled = False
        tool3.enabled = True
        button1.enabled = True
        button2.enabled = True
        tool7.enabled = False
        
        try:
            mxd = arcpy.mapping.MapDocument("CURRENT")
            df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
            lyr = arcpy.mapping.ListLayers(mxd, "watershed", df)[0]
            df.extent = lyr.getSelectedExtent()
        except:
            return