Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
I think ArcPy and the Field Calculator are both overkill for this. I would try using the Summary Statistics tool with a case_field of Line_ID first and if that does not give you a solution then perhaps add a picture of the input and expected output for your process to your question using the edit button. I'll +1 your question in case you have insufficient reputation to do that. –  PolyGeo Aug 6 '13 at 5:54

1 Answer 1

up vote 1 down vote accepted

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...

share|improve this answer
    
This worked like a charm. I'm only green when it comes to the syntax of the code itself; I understand the logic and how to use the field calculator. But thanks a lot for a perfect solution and for being extra explicit! Exactly what I was looking for. –  jchapman311 Aug 6 '13 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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