Get UpStream Watersheds and PWS

Titleā€ƒ Get UpStream Watersheds and PWS

Summary

Usage

Geoprocessing widget within F2F 2.0 WebApp Builder executes the task.


Syntax

Parameter Explanation
Watershed

Data source used by Script Get UpStream Watersheds and PWS is not registered with the server and will be copied to the server: https://apps.fs.usda.gov/fsgisx02/rest/services/NA_SPF/F2FHUC12/MapServer/0

Code Samples

Get_UpStream_PWS.py

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Get_UpStream.py
# Created on: 2020-09-09 
# By:  Michelle Hawks
# Description: Finds subwatersheds (HUC12) upstream 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
huclst.append(huc12)

# 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"]
        
def getUpStrm(huc12):
    arcpy.AddMessage("Getting Up Stream")
    huclist_u = []
    huclist_u.append(huc12)
    huclist_up = []
    huclist_up.append(huclist_u)
    numsel = len(huclist_u)
    while numsel > 0:
        if len(huclist_u) > 1:
            where_clause = "{} IN {}".format(theField1, tuple(huclist_u))
        else:
            where_clause = "{} = '{}'".format(theField1, huclist_u[0])
        selLyr = (
            arcpy.SelectLayerByAttribute_management(
                theLyr, "NEW_SELECTION", where_clause
            )
        )[0]
        huclist_u = unique_values(selLyr)
        numsel = len(huclist_u)
        if numsel > 0:
            arcpy.AddMessage(huclist_u)
            huclist_up.append(huclist_u)
    arcpy.AddMessage("Finished Up Stream")
    return huclist_up

    
def unique_values(selLyr):
    with arcpy.da.SearchCursor(selLyr,fieldlist) as cursor:
        return sorted({row[0] for row in cursor})

huclist_up = getUpStrm(huc12)
huclist_upl = [item for items in huclist_up for item in items]

huclist_fulld = list(dict.fromkeys(huclist_upl)) 

final_sel = None
count = len(huclist_fulld)
if count > 1:
    where_clause = "{} IN {}".format(theField, tuple(huclist_upl))
    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, huclist_up[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, Upstream, Watersheds, PWS

Credits

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


Use limitations