1

I'm attempting run a calculate field for a feature layer that I'm creating in this tutorial. I can't seem to set it up to run though. ERROR 999999: Error executing function. Failed to execute (CalculateField)... I want the expression to run and populate the new field in the feature layer,fc. enter image description here

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Import the mod
import arcpy
from arcpy import env

try:    
    # Set up the environment
    env.workspace = \
        r"E:\GISTutorial_for_PyScripting\GISTPython\Data\FireDepartment.gdb"
    env.overwriteOutput = True

    # Create a list of all the box zone feature classes
    fcBoxZones = arcpy.ListFeatureClasses('FireBoxMap*')

    # fcBoxZones = arcpy.ListFeatureClasses("FireBoxMap_14")
    # Start a for statment to iterate through the files

    for fc in fcBoxZones:
        # Get the first file - it's stored in fc
        print fc
        break  # If you want the first otherwise it's the last

    # Add field to hold the results
    arcpy.AddField_management(fc, 'LaneMiles', 'LONG')

    # Select the street lanes
    path = r"E:\GISTutorial_for_PyScripting\GISTPython\Data\City of Oleander.gdb\Street_Centerlines"
    arcpy.MakeFeatureLayer_management(path, 'Lanes_lyr')

    # Replace a layer/table view name with a path to a dataset
    # (which can be a layer file) or create the layer/table view
    # within the script
    # The following inputs are layers or table views:
    # "FireBoxMap", "Street Centerlines"
    arcpy.SelectLayerByLocation_management(
        in_layer='Lanes_lyr',
        overlap_type='INTERSECT',
        select_features=fc,
        search_distance='',
        selection_type='NEW_SELECTION',
        invert_spatial_relationship='NOT_INVERT')
    laneCount = int(arcpy.GetCount_management('Lanes_lyr').getOutput(0))
    print str(laneCount)

    # Update field
    arcpy.CalculateField_management(in_table=fc, field='LaneMiles',
                                    expression=' [Lanes] * [SHAPE_Length]',
                                    expression_type='VB',
                                    code_block='')
    print 'updated LaneMiles field'

except Exception, e:
    print e
6
  • Do you have a try block to go along with the except block? – Branco Dec 3 '15 at 20:50
  • This isn't causing your error, but it looks like you need to indent everything below "print fc". Otherwise, it will only process the last feature class. Generally you want to avoid the try/except clauses, unless you're catching specific exceptions. – Tom Dec 3 '15 at 20:50
  • You're using Python 3.x syntax in your exception print, but 2.x syntax elsewhere. Since you're using arcpy, I assume you meant to use 2.x throughout. Print statements in 2.x don't use parentheses. After you fix that, you should be able to better tell what you initial error is. – Tom Dec 3 '15 at 20:53
  • I had the try/except block set and when i removed it got: Traceback (most recent call last): File "E:\GIST 602\Labs\602B\Lesson1_Lab1_2-1.py\EX2_5.py", line 40, in <module> arcpy.CalculateField_management(in_table=fc, field="LaneMiles", expression="[Lanes] * [SHAPE_Length]", expression_type="VB", code_block="") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 3453, in CalculateField raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (CalculateField). – Caitlin Ryan Dec 3 '15 at 21:12
  • @Tom, see here. It's a good habit, imo, to start using parenthesis in print statements. – Paul Dec 3 '15 at 22:04
2

The calculate field tool is notoriously finicky. Everything has to be so exact to get it to execute properly. Long ago, I gave up on it for the faster (to process), far more flexible, and more straight-forward cursors.

import arcpy
from arcpy import env

# Set up the environment
env.workspace = r"E:\GISTutorial_for_PyScripting\GISTPython\Data\FireDepartment.gdb"
env.overwriteOutput = True

fc = arcpy.ListFeatureClasses('FireBoxMap*')[0]
print fc

# Add field to hold the results
arcpy.AddField_management(fc, 'LaneMiles', 'LONG')

# Select the street lanes
path = r"E:\GISTutorial_for_PyScripting\GISTPython\Data\City of Oleander.gdb\Street_Centerlines"
arcpy.MakeFeatureLayer_management(path, 'Lanes_lyr')

# Replace a layer/table view name with a path to a dataset
# (which can be a layer file) or create the layer/table view
# within the script
# The following inputs are layers or table views:
# "FireBoxMap", "Street Centerlines"
arcpy.SelectLayerByLocation_management(in_layer='Lanes_lyr', \
                                    overlap_type='INTERSECT', \
                                    select_features=fc, \
                                    search_distance='', \
                                    selection_type='NEW_SELECTION', \
                                    invert_spatial_relationship='NOT_INVERT')
laneCount = int(arcpy.GetCount_management('Lanes_lyr').getOutput(0))
print str(laneCount)

with arcpy.da.UpdateCursor(fc, ['Lanes', 'SHAPE@LENGTH', 'LaneMiles']) as cur:
    for row in cur:
        row[-1] = row[0] * row[1]
        cur.updateRow(row)

print 'updated LaneMiles field on the feature class {}'.format(fc)
3
  • i have edit post because # Get the first file - it's stored in fc print fc... It need break to get just first and not all fc is pass in loop – GeoStoneMarten Dec 4 '15 at 12:58
  • 1
    @GeoStoneMarten, Oh. Well the fact that you were using a loop was confusing. I've updated the answer to only process the first file found in the ListFeatureClasses tool. – Tom Dec 4 '15 at 18:59
  • you don't need \ after declaration param function – GeoStoneMarten Dec 4 '15 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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