1

I have a point shapefile with two columns i want to process. The first column is aspect and the second is aspect_m60, there is only one row with the aspect of my point. I have tried to write a script in Python for ArcGIS 10.2.2 because i want to put it in my code but with no success. The code is this:

# Calculate Field
import arcpy
# Set environment settings
arcpy.env.workspace = "c:/W/Sik"
# Set local variables
inTable = "Point"
fieldName = "aspect_m60"
expression = "getCalc(!aspect!, !aspect_m60!)"
codeblock = """def getCalc(aspect, aspect_m60):
   if (aspect < 60):
      aspect_m60 = (aspect - 60) + 360
   if (aspect = 60):
      aspect_m60 = aspect - 60
   else:
      aspect_m60 = aspect - 60"""
  • Judging from the answers you've received and what hasn't yet worked for you, I think you should give a more detailed outline of how you're implementing this code. – mr.adam Apr 23 '15 at 14:59
4

It is much more intuitive, in my opinion, to work with Cursors (rather than trying to emulate the field calculator in a script) for this type of problem. This is how you would port the problem over to an Update Cursor:

import arcpy

# The input FC
fc = "C:/W/Sik.gdb/yourFC"

with arcpy.da.UpdateCursor(fc, ["aspect", "aspect_m60"]) as cursor:
    for row in cursor:
        # row[0] = "aspect"
        # row[1] = "aspect_m60"
        if row[0] < 60:
            row[1] = (row[0] - 60) + 360
        elif row[0] == 60:
            row[1] = row[0] - 60
        else:
            row[1] = row[0] - 60

        cursor.updateRow(row)
  • thanks for your response unfortunately i don't have a gdb or mxd. – por.bet Apr 23 '15 at 14:49
  • You can use a cursor on tables, feature classes or shapefiles. Just replace the path with your own (e.g. fc = "C:/W/Sik/Point.shp" for a shapefile or fc = "C:/W/Sik/Point.dbf" for a .dbf table. – Aaron Apr 23 '15 at 14:51
  • thank you i put it but i get an: Traceback (most recent call last): File "<string>", line 4, in <module> AttributeError: exit – por.bet Apr 23 '15 at 14:57
  • Sorry, I made an error in the "untested" script. I needed to add da to the cursor function. Please see the updated script. – Aaron Apr 23 '15 at 14:59
3

You are trying to set the field within the code block, when actually you need the code block to return the value you're looking for. If you just just add return aspect_m60 after the else block, it should work fine.

Think of the code block as a place to write functions whose results can be used in your field calculator expression.

  • thanks for your response, where should i put return aspect_m60? at the last line ir in each command after if....: ? cause i tried both but.. – por.bet Apr 23 '15 at 14:50
3

Your function is not returning anything. I've modified your code to return the value of aspect_m60.

# Calculate Field
import arcpy
# Set environment settings
arcpy.env.workspace = "c:/W/Sik"
# Set local variables
inTable = "Point"
fieldName = "aspect_m60"
expression = "getCalc(!aspect!)"
codeblock = """def getCalc(aspect):
   if (aspect < 60):
      aspect_m60 = (aspect - 60) + 360
   if (aspect = 60):
      aspect_m60 = aspect - 60
   else:
      aspect_m60 = aspect - 60
   return aspect_m60"""
  • thanks for the reply, but i tried your code form python window and i didn't get a result in column aspect_m60 – por.bet Apr 23 '15 at 14:48
0

As a couple others have pointed out, you're missing a return value. You're also confusing the assignment operator = for an equality operator == (in the 2nd if). The if structure is also incorrect (if/if/else), and will calculate the wrong value for anything less than 60 (should be if/elif/else).

If you still want to use CalculateField (the other answer with UpdateCursor is perfectly valid), the code block below would be what you intended.

codeblock = """def getCalc(aspect, aspect_m60):
    if aspect < 60:
        aspect_m60 = (aspect - 60) + 360
    elif aspect == 60:
        aspect_m60 = aspect - 60
    else:
       aspect_m60 = aspect - 60
    return aspect_m60
"""

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.