Get Downstream Watersheds and PWS

Titleā€ƒ Get Downstream Watersheds and PWS

Summary

Usage

Geoprocessing widget within F2F 2.0 WebApp Builder executes the task.


Syntax

Parameter Explanation
Watershed

Selected watershed from F2F 2.0 dataset.

Code Samples

Get_DownStream_PWS.py

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Get_DownStream.py
# Created on: 2020-09-09 
# By:  Michelle Hawks
# Description: Finds subwatersheds (HUC12) downstream from
#              selected subwatershed.
# ---------------------------------------------------------------------------

import arcpy

# need the full layer to select from, and the selected HUC12 value
theLyr = arcpy.GetParameterAsText(0)

huclst = []    
theField = 'HUC12'
theField1 = 'ToHUC'

# Process: Get Field Value
fieldlist = ['HUC12','ToHUC']
with arcpy.da.SearchCursor(theLyr,fieldlist) as cursor:
    for row in cursor:
        huc12 = row[0]
        tohuc = row[1]
del cursor

# theLyr = "https://wmxbetatest.esri.com/server/rest/services/Hosted/Watersheds/FeatureServer/0"
theLyr = "https://usfs-dmsc-hst.esriemcs.com/server/rest/services/Hosted/F2FHUC12/FeatureServer/0"
theField = "huc12"
theField1 = "tohuc"
fieldlist = ["huc12", "tohuc"]

huclst.append(huc12)

def getDownStrm(tohuc):
    arcpy.AddMessage("Get Down Stream")
    while int(tohuc) > 0:
        arcpy.AddMessage(tohuc)
        where_clause = "{} = '{}'".format(theField, tohuc)
        selLyr = (
            arcpy.SelectLayerByAttribute_management(
                theLyr, "NEW_SELECTION", where_clause
            )
        )[0]
        tohuc1 = 0
        huc12 = None
        with arcpy.da.SearchCursor(selLyr, fieldlist) as cursor:
            for row in cursor:
                huc12 = row[0]
                tohuc1 = row[1]
                arcpy.AddMessage(huc12)
                arcpy.AddMessage(tohuc1)
        del cursor
        tohuc = str(tohuc1)
        if huc12:
            huclst.append(huc12)
    arcpy.AddMessage("Finished Down Stream")
    return huclst        
   
def unique_values(selLyr):
    with arcpy.da.SearchCursor(selLyr,fieldlist) as cursor:
        return sorted({row[0] for row in cursor})

if int(tohuc) > 0:
    huclst_d = getDownStrm(tohuc)
else:
    huclst_d = []

huclist_fulld = list(dict.fromkeys(huclst_d))  #(huclist_full))

final_sel = None
count = len(huclist_fulld)
if count > 1:
    where_clause = "{} IN {}".format(theField, tuple(huclst_d))
    if count > 2000:
        listchunks = [
            huclist_fulld[x : x + 2000] for x in range(0, len(huclist_fulld), 2000)
        ]
        for x in range(0, len(listchunks)):
            nlst = listchunks[x]
            where_clause = "{} IN {}".format(theField, tuple(nlst))
            if x == 0:
                final_sel = arcpy.SelectLayerByAttribute_management(
                    theLyr, "NEW_SELECTION", where_clause
                )
                numsel = int(arcpy.GetCount_management(theLyr).getOutput(0))
            else:
                final_sel = arcpy.SelectLayerByAttribute_management(
                    theLyr, "ADD_TO_SELECTION", where_clause
                )
                numsel = int(arcpy.GetCount_management(theLyr).getOutput(0))
elif count != 0:
    where_clause = "{} = '{}'".format(theField, huclst_d[0])
else:
    where_clause = None
    
if not final_sel and where_clause:
    final_sel = (
        arcpy.SelectLayerByAttribute_management(theLyr, "NEW_SELECTION", where_clause)
    )[0]
    arcpy.SetParameter(1, final_sel)

Tags

F2F, Downstream, Watersheds, PWS

Credits

Michelle Hawks, GIS Specialist, Enterprise Program, USDA Forest Service


Use limitations