0

I would like to create sequential numbers in a field called "virtual _Li".

enter image description here

I have used the field calculator using def autoincrement() function and manually calculate the interval number. https://support.esri.com/en/technical-article/000011137

enter image description here

But I would like to have it in python.

The start point should be always the number in "Z_2018" that has "0" in "First_Dist" (Start = 1.006). End number is the number in "Z_2018" that has the Maximum number in "FIRST_DIST" (End = 1.04).

The interval is:( (End) – (Start) ) / ( max in "FIRST_DIST" field/a constant number )

Constant number is always = 0.15

Start and end number can be + or number.

In this case interval is 0.002875793

( (1.040) – (1.006) ) / (1.773424/0.15) = 0.002875793

First create a field:

arcpy.AddField_management(ProfileGraph1WR1, "Virtual_Li", "DOUBLE", 10, 3)

then I need to create a "codeblock" and "Expression" which I don’t know How.

asked Oct 24, 2018 at 7:44
3
  • To confirm, this is working within the ArcMap window but you are just looking to convert it to arcpy? Commented Oct 24, 2018 at 14:11
  • Yes! it works with field calculator in ArcMap and manually calculating the Interval. but I want it automatically with arcpy. Commented Oct 24, 2018 at 14:14
  • I think I'm still missing how you are making the choice for increment value, but the answer below has the base code for making the codeblock/expression for use in arcpy. Commented Oct 24, 2018 at 14:25

1 Answer 1

1

Code Example 3 on Esri's documentation for Calculate Field is probably the closest to your question.

Since you have up through the AddField, the code below proceeds from there based on that example.

with arcpy.da.SearchCursor("ProfileGraph1WR1", ["First_Dist", "Z_2018"]) as cursor:
 firstdistvals = dict() 
 for row in cursor:
 if row[0] == 0: 
 startval = row[1]
 firstdistvals[row[0]] = row[1]
 maxval = max(firstdistvals.keys())
 endval = firstdistvals[maxval]
# Set local variables
inTable = "ProfileGraph1WR1"
fieldName = ""Virtual_Li"
expression = "autoIncrement({}, {}, {})".format(startval, endval, maxval) 
codeblock = """
 rec = 0
 def autoIncrement(start, end, maximum): 
 global rec
 const = 0.15
 pStart = float(start)
 pInterval = (float(end) – float(start)) / (float(maximum)/const)
 if (rec == 0):
 rec = pStart
 else:
 rec += pInterval
 return rec"""
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON_9.3", codeblock)
answered Oct 24, 2018 at 14:24
8
  • How the code calculate the interval? I want it automatically calculated. Commented Oct 24, 2018 at 14:26
  • 1
    I'll add in SearchCursor based checks to determine start/end. Commented Oct 24, 2018 at 14:30
  • Updated answer posted; please test Commented Oct 24, 2018 at 14:43
  • I got error:ExecuteError: Failed to execute. Parameters are not valid. ERROR 000800: The value is not a member of VB | PYTHON | PYTHON_9.3. ERROR 000989: Python syntax error: Parsing error IndentationError: unexpected indent (line 2) Failed to execute (CalculateField). Commented Oct 24, 2018 at 14:54
  • 1
    it works, my python gives error because of the spaces in PInterval. now it works :pInterval = (float(end)-float(start))/(float(maximum)/const). Thanks! Commented Oct 25, 2018 at 9:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.