Updating fields based on other fields - Python Script

1048
6
Jump to solution
09-06-2016 02:07 PM
DavidBuehler
Occasional Contributor III

My conundrum is, is it possible to calculate fields based on other fields in a stand-alone python script.  An example would be a group of values bases on two fields dictates what gets calculated in two other fields such as GeneralConcern = 1 (subytype) and SpecificConcern = "Pothole" and ContactDept IS NULL and DeptInfo IS NULL, calculate ContactDept = "Street Dept" and calculates DeptInfo = "715-555-5555".?

This is what I have come up with so far, but I am having a hard time wrapping my head around it.

import arcpy

# Set workspaces
workspace = r'C:\ESRITest\GeoReportingSystem\Testing9116\Testing9116.gdb'
outWorkspace = r'C:\ESRITest\GeoReportingSystem\Testing9116\Scratch.gdb'

# Set feature classes
tbl = r'C:\ESRITest\GeoReportingSystem\Testing9116\Testing9116.gdb\GeoReporting'

# Set other variables
pContact = "PrimaryContact"
contactInfo = "PrimaryInformation"
sql = """{} is NULL AND {} IS NULL""".format(pContact, contactInfo)

specificCon1 = ['Commercial Signage Concern','Temporary Signage Concern', 'Building Maintenance Issue','Fence Concern', 'Land Use Concern', 'Property Maintenance Concern', 'Setback or Property Concern', 'Other'] # Mulitple General Concern  Numbers
specificCon2 = ['Athletic Field Issue', 'Fairgrounds Issue', 'Park Maintenance Issue', 'Pool Issue', 'Restroom Issue', 'Shelter Issue', 'Skate Park Issue', 'Trail Maintenance Issue', 'Unsafe or Downed Tree', 'Zoo Issue', 'Other' ]


# Starting an Edit Session - DMB
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()


# What I am trying to do is update the Null PrimaryContact and PrimaryInformation fields based on a General Concern (subtype number) and a specific concern.
#Example would be General Concern = 6 and Specific Concern = Pool Issue then PrimaryContact = Parks and Recreation and PrimaryInformation = 715-555-5555
# See specificCon1.  That variable will be a mix of several General Concern numbers like
with arcpy.da.UpdateCursor(tbl, ['PrimaryContact', 'PrimaryInformation'] , where_clause = sql) as cursor:
    for row in cursor:
        if value == specificCon1:
            row.setValue("PrimaryContact", "Development Services"
            row.setValue("PrimaryInformation", "715-555-5555")
        elif value == specificCon2:
            row.setValue("PrimaryContact", "Parks and Recreation"
            row.setValue("PrimaryInformation", "715-555-5555")
        else: == "??????":


# Closing the Edit Session - DMB
edit.stopOperation()
edit.stopEditing(True)
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

There are many possibilities, but getting your options into some sort of data structure (list, dictionary, table, etc.) would be my first step. Then, if the cursor values match the option values, fill in the other values.

 

options = [[1,'Traffic Issue','Engineering','715-555-5555'],
[1,'Traffic Signal Issue','Engineering','715-555-5555'],
[1,'Vision Obstruction','Engineering','715-555-5555'],
[2,'Cracked Sidewalks','Engineering','715-555-5555'],
[2,'Curb Ramp Issue','Engineering','715-555-5555'],
[2,'Slope Issue','Engineering','715-555-5555'],
[2,'Tree or Brush Clearance Issue','Engineering','715-555-5555'],
[2,'Uneven Sidewalk','Engineering','715-555-5555'],
[2,'Other','Engineering','715-555-5555'],
[8,'Culvert Replacement','Engineering','715-555-5555'],
[8,'Ditch Flow Issue','Engineering','715-555-5555'],
[8,'Ponding Water','Engineering','715-555-5555'],
[8,'Other','Engineering','715-555-5555'],
[5,'Fire Code Violation','Fire Department','715-555-5555'],
[5,'Illegal Burning','Fire Department','715-555-5555']]

with arcpy.da.UpdateCursor(tbl, ['GeneralConcern', 'SpecificConcern', 'PrimaryContact', 'PrimaryInformation'] , where_clause = sql) as cursor:
    for row in cursor:
        for option in options:
            if option[0] == row[0] and option[1] == row[1]:
                row[2] = option[2]
                row[3] = option[3]
        if not row[2] and not row[3]: # not 100% sure about this
            print 'No Match'
        cursor.updateRow(row) # update row in table with values held in row variable‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

View solution in original post

6 Replies
JoeBorgione
MVP Emeritus

I don't know about doing it in python, but the attribute assistant can do this for you...

That should just about do it....
0 Kudos
DavidBuehler
Occasional Contributor III

I have GeoEvent Processor, which I know will do this, but I was hoping to off load it to a script as it does not need to get updated all that often in the grander scheme of the project.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Please see da.UpdateCursor. You're going back and forth between the old (e.g. setValue) and new cursor syntax.

with arcpy.da.UpdateCursor(tbl, ['GeneralConcern', 'SpecificConcern', 'PrimaryContact', 'PrimaryInformation'] , where_clause = sql) as cursor:
    for row in cursor:
        if row[0] == 'SomeGeneralConcern1' and row[1] == 'SomeSpecificConcern1':
            row[2] = 'SomePrimaryContact1'
            row[3] = 'SomePrimaryInformation1'
        elif row[0] == 'SomeGeneralConcern2' and row[1] == 'SomeSpecificConcern2':
            row[2] = 'SomePrimaryContact2'
            row[3] = 'SomePrimaryInformation2'
        elif row[0] == 'SomeGeneralConcern3' and row[1] == 'SomeSpecificConcern3':
            row[2] = 'SomePrimaryContact3'
            row[3] = 'SomePrimaryInformaton3'
        etc.
    cursor.updateRow(row) # update row in table with values held in row variable‍‍‍‍‍‍‍‍‍‍‍‍‍

I'm not quite sure how best to set up the logic without knowing more about how you want to make decisions for primary contact and information.

DavidBuehler
Occasional Contributor III

Darren,

I am not sure how to set it up either.  Hopefully, this will help clarify:

GeneralConcern will have a integer 1 through 8

SpecificConcern will have a text value

PrimaryContact and PrimaryInformation should be null (no sense recalculating what is already calculated)

So if all the criteria are met, calculate PrimaryContact with a text value (a Department) and and PrimaryInformation with a text value (Phone Number) else do nothing and end.

Example would be:

   Before:

GeneralConcernSpecificConcernPrimaryContactPrimaryInformation
1Traffic IssueEngineering715-555-5555
2Slope Issue<null><null>
1Traffic Signal Issue<null><null>

   After:

GeneralConcernSpecificConcernPrimaryContactPrimaryInformation
1Traffic IssueEngineering715-555-5555
2Slope IssueEngineering715-555-5555
1Traffic Signal IssueEngineering715-555-5555

Here is sample of what present in the GeneralConcern and SpecificConcern fields and what PrimaryContact and PrimaryInformation should be calculated to:

GeneralConcernSpecificConcernPrimaryContactPrimaryInformation
1Traffic IssueEngineering715-555-5555
1Traffic Signal IssueEngineering715-555-5555
1Vision ObstructionEngineering715-555-5555
2Cracked SidewalksEngineering715-555-5555
2Curb Ramp IssueEngineering715-555-5555
2Slope IssueEngineering715-555-5555
2Tree or Brush Clearance IssueEngineering715-555-5555
2Uneven SidewalkEngineering715-555-5555
2OtherEngineering715-555-5555
8Culvert ReplacementEngineering715-555-5555
8Ditch Flow IssueEngineering715-555-5555
8Ponding WaterEngineering715-555-5555
8OtherEngineering715-555-5555
5Fire Code ViolationFire Department715-555-5555
5Illegal BurningFire Department715-555-5555
0 Kudos
DarrenWiens2
MVP Honored Contributor

There are many possibilities, but getting your options into some sort of data structure (list, dictionary, table, etc.) would be my first step. Then, if the cursor values match the option values, fill in the other values.

 

options = [[1,'Traffic Issue','Engineering','715-555-5555'],
[1,'Traffic Signal Issue','Engineering','715-555-5555'],
[1,'Vision Obstruction','Engineering','715-555-5555'],
[2,'Cracked Sidewalks','Engineering','715-555-5555'],
[2,'Curb Ramp Issue','Engineering','715-555-5555'],
[2,'Slope Issue','Engineering','715-555-5555'],
[2,'Tree or Brush Clearance Issue','Engineering','715-555-5555'],
[2,'Uneven Sidewalk','Engineering','715-555-5555'],
[2,'Other','Engineering','715-555-5555'],
[8,'Culvert Replacement','Engineering','715-555-5555'],
[8,'Ditch Flow Issue','Engineering','715-555-5555'],
[8,'Ponding Water','Engineering','715-555-5555'],
[8,'Other','Engineering','715-555-5555'],
[5,'Fire Code Violation','Fire Department','715-555-5555'],
[5,'Illegal Burning','Fire Department','715-555-5555']]

with arcpy.da.UpdateCursor(tbl, ['GeneralConcern', 'SpecificConcern', 'PrimaryContact', 'PrimaryInformation'] , where_clause = sql) as cursor:
    for row in cursor:
        for option in options:
            if option[0] == row[0] and option[1] == row[1]:
                row[2] = option[2]
                row[3] = option[3]
        if not row[2] and not row[3]: # not 100% sure about this
            print 'No Match'
        cursor.updateRow(row) # update row in table with values held in row variable‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

DavidBuehler
Occasional Contributor III

Thanks Darren,

After seeing an example, I get it now.  I appreciate your help on this. That works.

0 Kudos