'NameError: name 'select_pour_point' is not defined

I am receiving this error when trying to setup variables for each pour point. It is a

cur_point = select_pour_point(POUR_POINTS, NAME_FIELD, name)

NameError: name 'select_pour_point' is not defined

HELP!

import shedtools as st

# Directories
SCRATCH = "C:\\SCRATCH"
OUT_FOLDER = "C:\\WATERSHEDS" 

# inputs
POUR_POINTS = "C:\\Hawaii_DEM\\Hawaii.shp"
FLOW_DIRECTION = "C:\\Hawaii_DEM\\hawaii_flwdir" 
STATS_RASTER = "C:\\HawaiiRFGrids_mm\\RF_MM_BI_Ann"                    
RASTER_NAME = "stats"
NAME_FIELD = "Id" 
STATISTICS = "MEAN"

with arcpy.da.SearchCursor(POUR_POINTS, NAME_FIELD) as cursor:
    for point in cursor:
        name = str(point[0])
        print "Working on pour point %s" % (name)
        cur_point = select_pour_point(POUR_POINTS, NAME_FIELD, name) 
        shed_raster = make_watershed_raster(FLOW_DIRECTION, cur_point, name, SCRATCH)

Error I am receiving is:

Traceback (most recent call last):
  File "C:\\exercise_5.py", line 30, in <module>
    cur_point = select_pour_point(POUR_POINTS, NAME_FIELD, name)
NameError: name 'select_pour_point' is not defined

Line 30 is:

cur_point = select_pour_point(POUR_POINTS, NAME_FIELD, name) 


Solution 1:[1]

You import your shedtools.py script using import shedtools as st. Therefore, you need to prefix select_pour_point and make_watershed_polygon with st.:

  • e.g. st.select_pour_point(...)

Not related to your error BUT open your arcpy.da.SearchCursor using with to make sure it is closed again after using.

import shedtools as st

# ...

with arcpy.da.SearchCursor(POUR_POINTS, NAME_FIELD) as cursor:
    for point in cursor:
        name = str(point[0])
        print "Working on pour point %s" % (name)
        cur_point = st.select_pour_point(POUR_POINTS, NAME_FIELD, name) 
        shed_raster = st.make_watershed_raster( \
                  FLOW_DIRECTION, cur_point, name, SCRATCH)

Alternatively, instead of prefixing your functions with st., you could also import your functions using:

  • from shedtools import select_pour_point, make_watershed_raster

If you do so, no prefixing is necessary:

from shedtools import select_pour_point, make_watershed_raster

# ...

with arcpy.da.SearchCursor(POUR_POINTS, NAME_FIELD) as cursor:
    for point in cursor:
        name = str(point[0])
        print "Working on pour point %s" % (name)
        cur_point = select_pour_point(POUR_POINTS, NAME_FIELD, name) 
        shed_raster = make_watershed_raster(FLOW_DIRECTION, cur_point, name, SCRATCH)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1