Sunday 21 April 2019

arcmap - Generating sequential numbering for duplicate values in a field sorted by ObjectID


I am trying to figure out a solution to an field calculation problem. What I'm trying to do is automatically (using either ArcPy or the Field Calculator) generate sequential numbers, beginning at 1, in a new blank integer field (called 'Point_ID') for every recurrence of a value in a second field (called 'Line_ID'). The sorting of the sequential values in the second field ('Line_ID') will be based on the order in a third field ('FID'). Can anyone help me do this? I am very green when it comes to advanced field calculations and ArcPy (and Python in general, for that matter). So your explicitness is much appreciated.


Another way to put it: there are duplicate values in the Line_ID field. I would like to create a new field that counts up for every duplicate occurrence in the Line_ID field, with the sort order based on the FID field. So, if there are nine values in the Line_ID field that have the value "A," the new Point_ID field will go from 1 to 9, with the order based on the sort on FID.



Answer




I'm not sure how green you are, so here are "pretty explicit" instructions...



  1. Open up Field Calculator on the Point_ID field

  2. At the top, choose the "Python" parser

  3. Click the checkbox beside "Show Codeblock"


  4. In the "Pre-Logic script code", paste the following code...


    prevFieldValue = ''
    counter = 1
    def GetDuplicateCounter(myFieldValue):

    global prevFieldValue
    global counter
    if myFieldValue == prevFieldValue:
    counter += 1
    else:
    counter = 1
    prevFieldValue = myFieldValue
    return counter



  5. In the "Point_ID = " box, type in GetDuplicateCounter(!Line_ID!)




Note: If your Line_ID field is not a string field, then change the first line of code to prevFieldValue = 0 or something similar...


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...