0

QuestionCalculateField

If vb code is also possible I could give that a try aswell.

  • 4
    Can you try and reword your question? I am having a very hard time trying to decipher what it is you are trying to do here. – Radar Jun 26 '15 at 17:24
  • 3
    Instead of image, write your question in clear text. It is more searchable and search engine friendly – Farid Cheraghi Jun 26 '15 at 17:37
  • Oh, yeah sorry I did not realise it. – newgisuser Jun 26 '15 at 17:53
  • So I would like to reclass a field with the use of two other fields. I am using ArcMap 10.2.1 and would like to use the tool calculate field. All fields are of type string. The following is the case: Field 1 and Field 2 are existing/ calculated fields with a value, field 3 is the field I would like to be calculated. Field 1 should be used to update field 2 and give this updated value in field 3. So field 1 has unique values from 1-1000. And field 2 has values from 1-3000 of which some are similar. At the end I would like to have only the values 1-1000 in field 3. – newgisuser Jun 26 '15 at 18:01
  • 1
    There is an edit button beneath your question that you should use to revise it with the text of your question. Comments are only temporary. – PolyGeo Jun 26 '15 at 21:14
2

Use the ModelBuilders' Calculate Field to insert a python code like this:

Expression:

calculate(!Field1!, !Field2!)

And a code block of:

previousValue1 = ""
previousValue2 = ""
previousUnchangedValue1 = ""
def calculate(field1, field2):
 global previousValue1
 global previousValue2
 global previousUnchangedValue1
#First item in list
 if not previousValue1:
    previousValue1 = field1
    previousUnchangedValue1 = field1
    previousValue2 = field2
    return field1
#Current value equals previous value
 if (field2 == previousValue2):
    if previousUnchangedValue1 == previousValue1:
        newValue = previousUnchangedValue1
        previousValue1 = field1
        previousUnchangedValue1 = previousValue1
        previousValue2 = field2
    else:
        newValue = previousUnchangedValue1
        previousValue1 = field1            
        previousValue2 = field2
#Current value doesn't equal previous value
 else:
    if previousUnchangedValue1 == previousValue1:
        newValue = previousValue1
        previousValue1 = field1            
        previousValue2 = field2
    else:
        newValue = previousValue1    
        previousValue1 = field1
        previousUnchangedValue1 = previousValue1
        previousValue2 = field2     
 return newValue

The global variable in the python script will hold the previous fields' string value enabling a comparison to the current field value.

There is a lot of if statements in this code, but it will give the desired output.

  • @ David Gonzalez. Will this also assign the same values to the values in Field 2 when for example in Field 2 the ObjectID 10 has a value of 'B300' and the ObjectID 236 has a value of 'B300' and I want these values both to be given the same value of Field 1? – newgisuser Jun 26 '15 at 18:07
  • Unfortunately, I was not able to find a cleaner way to solve this problem. My VBScript skills are rusty so I used python instead. – David Gonzalez Jun 26 '15 at 20:04
  • 1
    David, have a look at a dictionary object in python, it will save you a bunch of pain. They are like lists but have a 'key' and a 'value' which can be any type, so for situations where you need to keep 1 = "Rabbit", 2 = "Dog" equality they become really handy (or "Rabbit" = 100, "Dog" = 7 for counts with the ability to add new objects at any time). – Michael Stimson Jun 28 '15 at 23:20
  • Thanks! I've used dictionaries before, but the way I wrote this code, I was just trying to figure out the problem rather than focusing on structure of the code. – David Gonzalez Jun 29 '15 at 17:36
1

There is a much simpler way using a Dictionary object. To be accessible/updateable for each iteration it must be outside the scope of the function (global) otherwise it's reset for each calculation.

preDict = {}
def CalcField3(Field1,Field2):
    global preDict # tell python this isn't a new variable but the other one
    if preDict.has_key(Field2): # if the value has already been encountered
        return preDict[Field2]
    else:
        preDict[Field2] = Field1 # add this key : value pair to the dictionary
        return Field1

enter image description here

Which returns:

enter image description here

Which isn't quite what you had in your question; the directions and example table provided don't match so I went with the instructions.

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.