0

The script will run without error, but the updatecursor at line 123 does not update the table. the addmessage before and aftr the loop shows right numbers but update does not seem to work.

    # -*- coding: utf-8 -*-

    '''---------------------------------------------------------------------------
    # Exposed.py
    # Created on: 2019-04-10 Jason Graham
    #
    # Usage: For classifying hazard level from winds on cops of trees near 
    # Anadromous streams Primary and secondary previaling winds.
    # 
    # Paramters <Workspace> 
    #           <Prevailing_Wind_Direction> 
    #           <Secondary_Prevailing_Wind_Direction>  
    #           Selected Stream with a GNIS_Name field
    #
    # Description:
    # When a stream is selected this tool will create a buffer for each side of a
    # stream and use the direction of the prevailing and secondary winds to 
    # calculate a Wind Exposure class for each section of major angles along 
    # the stream.
    # ---------------------------------------------------------------------------
    '''
    # Import arcpy module (14)
    import arcpy as arcpy, datetime
    import os
    #import inspect

    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = 'in_memory'

    startTime = datetime.datetime.now()
    arcpy.AddMessage ('Start processing at :')
    arcpy.AddMessage (startTime.strftime('%Y-%m-%d %H:%M'))

    # Get Parameters from user
    workspace = arcpy.GetParameterAsText(0)
    windP = arcpy.GetParameterAsText(1)
    windS = arcpy.GetParameterAsText(2)

    # Current workspace
    aprx = arcpy.mp.ArcGISProject('CURRENT')
    m = aprx.activeMap

    # Get Selected stream info
    lyrs = m.listLayers()

    for lyr in lyrs:
        if lyr.getSelectionSet():
           sel_lyr = lyr.dataSource
           field = 'GNIS_Name'
           rows = arcpy.da.SearchCursor(lyr,field)
           for row in rows:
                # get name of selected stream
                Stream_Pre = row[0]
                # replaces spaces with underscore
                Stream_Name = Stream_Pre.replace(' ','_')

    # Local variables:
    stream = os.path.join(str(workspace),Stream_Name)

    # Main Body of Script

    arcpy.cartography.SimplifyLine(sel_lyr, stream, 'POINT_REMOVE', '45 Meters', 
                                   'RESOLVE_ERRORS', 'NO_KEEP', 'CHECK', '')

    arcpy.management.SplitLine(stream,stream + '_Split')

    # Buffer the stream(Right)

    arcpy.Buffer_analysis(stream + '_Split' , stream + '_Buff', '63 Feet', 'RIGHT'
                          , 'FLAT', 'NONE', '', 'PLANAR')

    # Add Field named Angle
    fc = stream + '_Buff'
    arcpy.AddField_management(fc, 'Angle', 'LONG', '', '', '', '', 
                              'NULLABLE', 'NON_REQUIRED', '')

    # Process: Calculate Polygon Main Angle (R)

    arcpy.CalculatePolygonMainAngle_cartography(fc, 'Angle', 
                                                'GEOGRAPHIC')

    # Process: Add Field (Wind)

    arcpy.AddField_management(fc, 'wind', 'LONG', '', '', '20', '', 
                              'NULLABLE', 'REQUIRED', '')

    arcpy.CalculateField_management(fc, 'wind', windP)

    # Process: Calculate Field (Adjust Angle R)
    expression1 = 'getClass((!Angle!)+ !wind!)'

    codeblock1 = '''
    def getClass(n):
        if n < 0:
            return n * -1
        else:   
            return n'''

    arcpy.CalculateField_management(fc, 'wind', expression1, 
                                    'PYTHON3', codeblock1)

    # Process: Add Field (Wind2_R)
    arcpy.AddField_management(fc, 'wind2', 'LONG', '', '', '20', '', 
                              'NULLABLE', 'NON_REQUIRED', '')

    arcpy.CalculateField_management(fc, 'wind2', windS)

    # Process: Calculate Field (Adjust Secondary Angle R)
    expression2 = 'getClass((!Angle!)+ !wind2!)'

    arcpy.CalculateField_management(fc, 'wind2', expression2, 
                                    'PYTHON3', codeblock1)

    # Process: Add Field (Index_R)
    arcpy.AddField_management(fc, 'Index', 'LONG', '', '', '20', '', 
                              'NULLABLE', 'REQUIRED', '')

    # Process: Calculate Field (Index R)



  fields = ['Index', 'wind']
    arcpy.AddMessage(fc)

    with arcpy.da.UpdateCursor(fc,fields) as cursor:
        for row in cursor:
            arcpy.AddMessage(row)
            if (row[1] >= 0 and row[1] <= 20):
                row[0] = 5
            elif (row[1] > 20 and row[1] <= 70):
                row[0] = 4
            elif (row[1] > 70 and row[1] <= 110):
                row[0] = 3
            elif (row[1] > 110 and row[1] <= 160):
                row[0] = 2
            elif (row[1] > 160 and row[1] <= 200):
                row[0] = 1
            elif (row[1] > 200 and row[1] <= 250):
                row[0] = 2
            elif (row[1] > 250 and row[1] <= 290):
                row[0] = 3
            elif (row[1] > 290 and row[1] <= 340):
                row[0] = 4
            elif (row[1] > 340):
                row[0] = 5

            cursor.updateRow(row)
            arcpy.AddMessage(row)



    arcpy.AddMessage('Index')
    arcpy.AddMessage(arcpy.GetMessages())
    # Process: Add Field (Index2_R)
    arcpy.AddField_management(stream + '_Buff', 'Index2', 'LONG', '', '', '20', '',
                              'NULLABLE', 'NON_REQUIRED', '')

    # Process: Calculate Field (Index2 R)
    expressionReclass2 = "Reclass(!wind2!)"
    codeblock_wind2 = '''def Reclass(wind2):
        if (wind2 >= 0 and wind2 <= 20):
            return 5
        elif (wind2 > 20 and wind2 <= 70):
                return 4
        elif (wind2 > 70 and wind2 <= 110):
                return 3
        elif (wind2 > 110 and wind2 <=160):
                return 2
        elif(wind2 > 160 and wind2 <=200):
                return 1
        elif(wind2 > 200 and wind2 <=250):
                return 2
        elif(wind2 > 250 and wind2 <=290):
                return 3
        elif(wind2 > 290 and wind2 <=340):
                return 4
        elif(wind2 > 340):
                return 5'''

    arcpy.CalculateField_management(stream + '_Buff', 'Index', expressionReclass2, 
                                    'PYTHON_9.3', codeblock_wind2)

    # Process: Add Field (Sum_R)
    arcpy.AddField_management(stream + '_Buff', 'SumOfRanks', 'SHORT', '', '', 
                              '20', '', 'NULLABLE', 'NON_REQUIRED', '')

    # Process: Calculate Field (Add Index R)
    expressionAdd = "Add(!Index! + !Index2!)"
    arcpy.CalculateField_management(stream + '_Buff', 'Index', expressionAdd,
                                    'PYTHON_9.3', '')

    # Process: Add Field (Class_R)
    arcpy.AddField_management(stream + '_Buff', 'Class', 'SHORT', '', '', '20', '',
                              'NULLABLE', 'NON_REQUIRED', '')

    # Process: Calculate Field (Class R)
    codeblock3 = '''
    def Reclass(SumOfRanks):
        if (SumOfRanks >= 0 and SumOfRanks <= 1):
            return 1
        elif (SumOfRanks > 1 and SumOfRanks <= 4):
            return 2
        elif (SumOfRanks > 4 and SumOfRanks <= 6):
            return 3
        elif (SumOfRanks > 6 and SumOfRanks <= 8):
            return 4
        elif (SumOfRanks > 8 and SumOfRanks <= 10):
            return 5'''

    arcpy.CalculateField_management(stream + '_Buff', 'Index', 
                                    'Reclass(!SumOfRanks!)', 'PYTHON_9.3'
                                    ,codeblock3)

    # add new layer and delete extra layers
    m.addDataFromPath (stream + '_Buff')

    #arcpy.DeleteFeatures_management (stream)
    #arcpy.DeleteFeatures_management (stream + '_Split')

    endTime = datetime.datetime.now()
    arcpy.AddMessage ('Finished processing at :')
    arcpy.AddMessage (endTime.strftime('%Y-%m-%d %H:%M'))
    arcpy.AddMessage ('Process duration:')
    arcpy.AddMessage (endTime - startTime)

MWE version: this will update the table

# -*- coding: utf-8 -*-
"""
Created on Fri Jun  7 15:04:31 2019

@author: jdgraham
"""

import arcpy 
import os
workspace = arcpy.GetParameterAsText(0)
windP = arcpy.GetParameterAsText(1)
windS = arcpy.GetParameterAsText(2)
# Current workspace
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.activeMap

# Current workspace
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.activeMap
# Get Selected stream info
lyrs = m.listLayers()

for lyr in lyrs:
    if lyr.getSelectionSet():
       sel_lyr = lyr.dataSource
       field = 'GNIS_Name'
       rows = arcpy.da.SearchCursor(lyr,field)
       for row in rows:
            # get name of selected stream
            Stream_Pre = row[0]
            # replaces spaces with underscore
            Stream_Name = Stream_Pre.replace(' ','_')
# Local variables:

stream = os.path.join(str(workspace),Stream_Name)
arcpy.cartography.SimplifyLine(sel_lyr, stream, 'POINT_REMOVE', '45 Meters', 
                               'RESOLVE_ERRORS', 'NO_KEEP', 'CHECK', '')

arcpy.management.SplitLine(stream,stream + '_Split')

# Buffer the stream(Right)

arcpy.Buffer_analysis(stream + '_Split' , stream + '_Buff', '63 Feet', 'RIGHT'
                      , 'FLAT', 'NONE', '', 'PLANAR')
fc = stream + '_Buff'
# Process: Add Field (Index_R)

arcpy.AddField_management(fc, 'wind', 'LONG', '', '', '20', '', 
                          'NULLABLE', 'REQUIRED', '')

arcpy.CalculateField_management(fc, 'wind', windP)

# Process: Calculate Field (Adjust Angle R)
expression1 = 'getClass((!Angle!)+ !wind!)'

codeblock1 = '''
def getClass(n):
    if n < 0:
        return n * -1
    else:   
        return n'''
# Process: Calculate Field (Index R)
arcpy.AddField_management(fc, 'Index', 'LONG', '', '', '20', '', 
                          'NULLABLE', 'REQUIRED', '')    
fields = ['Index', 'wind']
arcpy.AddMessage(fc)

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        arcpy.AddMessage(row)
        if (row[1] >= 0 and row[1] <= 20):
            row[0] = 5
        elif (row[1] > 20 and row[1] <= 70):
            row[0] = 4
        elif (row[1] > 70 and row[1] <= 110):
            row[0] = 3
        elif (row[1] > 110 and row[1] <= 160):
            row[0] = 2
        elif (row[1] > 160 and row[1] <= 200):
            row[0] = 1
        elif (row[1] > 200 and row[1] <= 250):
            row[0] = 2
        elif (row[1] > 250 and row[1] <= 290):
            row[0] = 3
        elif (row[1] > 290 and row[1] <= 340):
            row[0] = 4
        elif (row[1] > 340):
            row[0] = 5

        cursor.updateRow(row)
        arcpy.AddMessage(row)

m.addDataFromPath (stream + '_Buff')    
3

1 Answer 1

1

Thanks for the nudge, when creating the MWE I found I was reusing a field name by mistake. This was overwriting the results to appear like it did not do anything. The code now does what I tell it. Not sure yet if thats what I want.

This code now works:

    # -*- coding: utf-8 -*-

'''---------------------------------------------------------------------------
# Exposed.py
# Created on: 2019-04-10 Jason Graham
#
# Usage: For classifying hazard level from winds on cops of trees near 
# Anadromous streams Primary and secondary previaling winds.
# 
# Paramters <Workspace> 
#           <Prevailing_Wind_Direction> 
#           <Secondary_Prevailing_Wind_Direction>  
#           Selected Stream with a GNIS_Name field
#
# Description:
# When a stream is selected this tool will create a buffer for each side of a
# stream and use the direction of the prevailing and secondary winds to 
# calculate a Wind Exposure class for each section of major angles along 
# the stream.
# ---------------------------------------------------------------------------
'''
# Import arcpy module (14)
import arcpy, datetime
import os
#import inspect

arcpy.env.overwriteOutput = True
arcpy.env.workspace = 'in_memory'

startTime = datetime.datetime.now()
arcpy.AddMessage ('Start processing at :')
arcpy.AddMessage (startTime.strftime('%Y-%m-%d %H:%M'))

# Get Parameters from user
workspace = arcpy.GetParameterAsText(0)
windP = arcpy.GetParameterAsText(1)
windS = arcpy.GetParameterAsText(2)

# Current workspace
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.activeMap

# Get Selected stream info
lyrs = m.listLayers()

for lyr in lyrs:
    if lyr.getSelectionSet():
       sel_lyr = lyr.dataSource
       field = 'GNIS_Name'
       rows = arcpy.da.SearchCursor(lyr,field)
       for row in rows:
            # get name of selected stream
            Stream_Pre = row[0]
            # replaces spaces with underscore
            Stream_Name = Stream_Pre.replace(' ','_')

# Local variables:
stream = os.path.join(str(workspace),Stream_Name)

# Main Body of Script

arcpy.cartography.SimplifyLine(sel_lyr, stream, 'POINT_REMOVE', '45 Meters', 
                               'RESOLVE_ERRORS', 'NO_KEEP', 'CHECK', '')

arcpy.management.SplitLine(stream,stream + '_Split')

# Buffer the stream(Right)

arcpy.Buffer_analysis(stream + '_Split' , stream + '_Buff', '63 Feet', 'RIGHT'
                      , 'FLAT', 'NONE', '', 'PLANAR')

# Add Field named Angle
fc = stream + '_Buff'
arcpy.AddField_management(fc, 'Angle', 'LONG', '', '', '', '', 
                          'NULLABLE', 'NON_REQUIRED', '')

# Process: Calculate Polygon Main Angle (R)

arcpy.CalculatePolygonMainAngle_cartography(fc, 'Angle', 
                                            'GEOGRAPHIC')

# Process: Add Field (Wind)

arcpy.AddField_management(fc, 'wind', 'LONG', '', '', '20', '', 
                          'NULLABLE', 'REQUIRED', '')

arcpy.CalculateField_management(fc, 'wind', windP)

# Process: Calculate Field (Adjust Angle R)
expression1 = 'getClass((!Angle!)+ !wind!)'

codeblock1 = '''
def getClass(n):
    if n < 0:
        return n * -1
    else:   
        return n'''

arcpy.CalculateField_management(fc, 'wind', expression1, 
                                'PYTHON3', codeblock1)

# Process: Add Field (Wind2_R)
arcpy.AddField_management(fc, 'wind2', 'LONG', '', '', '20', '', 
                          'NULLABLE', 'NON_REQUIRED', '')

arcpy.CalculateField_management(fc, 'wind2', windS)

# Process: Calculate Field (Adjust Secondary Angle R)
expression2 = 'getClass((!Angle!)+ !wind2!)'

arcpy.CalculateField_management(fc, 'wind2', expression2, 
                                'PYTHON3', codeblock1)

# Process: Add Field (Index_R)
arcpy.AddField_management(fc, 'Index', 'LONG', '', '', '20', '', 
                          'NULLABLE', 'REQUIRED', '')

# Process: Calculate Field (Index R)

fields = ['Index', 'wind']
arcpy.AddMessage(fc)

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        arcpy.AddMessage(row)
        if (row[1] >= 0 and row[1] <= 20):
            row[0] = 5
        elif (row[1] > 20 and row[1] <= 70):
            row[0] = 4
        elif (row[1] > 70 and row[1] <= 110):
            row[0] = 3
        elif (row[1] > 110 and row[1] <= 160):
            row[0] = 2
        elif (row[1] > 160 and row[1] <= 200):
            row[0] = 1
        elif (row[1] > 200 and row[1] <= 250):
            row[0] = 2
        elif (row[1] > 250 and row[1] <= 290):
            row[0] = 3
        elif (row[1] > 290 and row[1] <= 340):
            row[0] = 4
        elif (row[1] > 340):
            row[0] = 5

        cursor.updateRow(row)
        arcpy.AddMessage(row)


arcpy.AddMessage('Index')
arcpy.AddMessage(arcpy.GetMessages())
# Process: Add Field (Index2_R)
arcpy.AddField_management(stream + '_Buff', 'Index2', 'LONG', '', '', '20', '',
                          'NULLABLE', 'NON_REQUIRED', '')

# Process: Calculate Field (Index2 R)
expressionReclass2 = "Reclass(!wind2!)"
codeblock_wind2 = '''def Reclass(wind2):
    if (wind2 >= 0 and wind2 <= 20):
        return 5
    elif (wind2 > 20 and wind2 <= 70):
            return 4
    elif (wind2 > 70 and wind2 <= 110):
            return 3
    elif (wind2 > 110 and wind2 <=160):
            return 2
    elif(wind2 > 160 and wind2 <=200):
            return 1
    elif(wind2 > 200 and wind2 <=250):
            return 2
    elif(wind2 > 250 and wind2 <=290):
            return 3
    elif(wind2 > 290 and wind2 <=340):
            return 4
    elif(wind2 > 340):
            return 5'''

arcpy.CalculateField_management(stream + '_Buff', 'Index2', expressionReclass2, 
                                'PYTHON_9.3', codeblock_wind2)

# Process: Add Field (Sum_R)
arcpy.AddField_management(stream + '_Buff', 'SumOfRanks', 'SHORT', '', '', 
                          '20', '', 'NULLABLE', 'NON_REQUIRED', '')

# Process: Calculate Field (Add Index R)
expressionAdd = "(!Index! + !Index2!)"
arcpy.CalculateField_management(stream + '_Buff', 'SumOfRanks', expressionAdd,
                                'PYTHON_9.3', '')

# Process: Add Field (Class_R)
arcpy.AddField_management(stream + '_Buff', 'Class', 'SHORT', '', '', '20', '',
                          'NULLABLE', 'NON_REQUIRED', '')

# Process: Calculate Field (Class R)
expression3 = "Reclass3(!SumOfRanks!)"
codeblock3 = '''def Reclass3(SumOfRanks):
    if (SumOfRanks >= 0 and SumOfRanks <= 1):
        return 1
    elif (SumOfRanks > 1 and SumOfRanks <= 4):
        return 2
    elif (SumOfRanks > 4 and SumOfRanks <= 6):
        return 3
    elif (SumOfRanks > 6 and SumOfRanks <= 8):
        return 4
    elif (SumOfRanks > 8 and SumOfRanks <= 10):
        return 5'''

arcpy.CalculateField_management(stream + '_Buff', 'Class', 
                                expression3, 'PYTHON_9.3'
                                ,codeblock3)

# add new layer and delete extra layers
m.addDataFromPath (stream + '_Buff')

#arcpy.DeleteFeatures_management (stream)
#arcpy.DeleteFeatures_management (stream + '_Split')

endTime = datetime.datetime.now()
arcpy.AddMessage ('Finished processing at :')
arcpy.AddMessage (endTime.strftime('%Y-%m-%d %H:%M'))
arcpy.AddMessage ('Process duration:')
arcpy.AddMessage (endTime - startTime)

Not the answer you're looking for? Browse other questions tagged or ask your own question.