Select to view content in your preferred language

Referencing a Table to populate a field with missing information

1245
6
06-05-2017 09:11 AM
LarryAdgate
Regular Contributor

I would really appreciate some help on this to help solve a long term problem in our office.

We have 114 feature classes where one of the fields is void of information or is wrong. We also have a very large table with the corrected information and there is commonality between the Feature class and the table. As a solution but very time consuming , I can joint the feature class and the table and using the field calculator transfer the information. Provided is an attachment to Illustrate what needs to happen. 

Once again, thank You for your help on this,

Larry     

0 Kudos
6 Replies
ClintonDow1
Frequent Contributor

Instead of using the field calculator, it should be possible to use cursors in a Python script. 

A script could consume the table and your set of feature classes. First, use an arcpy.da.SearchCursor to read the values from the table into a Python dictionary (key of the unique identifier, value of the correct data). Once the dictionary is populated, loop through the set of feature classes, and for each fc create an arcpy.da.UpdateCursor.

After creating the UpdateCursor for a feature class, loop through the rows - for each row, using the unique identifier again, check against the dictionary containing the correct values. If the value in your UpdateCursor for that row doesn't match the correct data in the dictionary, replace the value in the row. 

You're essentially 'joining' the UpdateCursor representing each feature class to the dictionary representing the table one at a time, then the field calculation is done one row at a time as you loop through the Cursor and check its value against the correct one in the dictionary. This should allow you to compare an arbitrary number of feature classes against the field with one run of the script (assuming there is some common unique ID per feature between them and the table).

LarryAdgate
Regular Contributor

Thank You for your thoughts, at least now I have some ideas how to solve the problem.

Larry

ClintonDow1
Frequent Contributor

No problem, I have had to create similar scripts in the past. Feel free to ask here if you run into any issues since I'm subscribed to this thread now  

LarryAdgate
Regular Contributor

Hi Clinton,

Can you show me what a python dictionary would look like, a production set preferably , and is the data stored in a table?   

0 Kudos
ClintonDow1
Frequent Contributor

Good morning,

A dictionary in Python is a built in data structure (not specific to ArcPy), similar to an array/list in other languages. Instead of referencing values by a numerical index as you would in a list, you are giving it custom index keys. In terms of tables, a list is like a table with a single column indexed by row number, and a dictionary is like a single row with many columns, indexed by column names.

For example:

>>> array = ['bob', 'chris', 'dave']
>>> array[0] 
'bob'
>>> dictionary = {'name': 'bob', 'age': 42}
>>> dictionary['name']
'bob'‍‍‍‍‍‍‍‍‍‍‍‍
>>> # add a key/value
>>> dictionary['weight'] = 120
>>> dictionary
{'name': 'bob', 'age': 42, 'weight': 120}

In what I'm describing in my above post, you would add key/values (similar to lines 8 & 9 above) to a dictionary that could be thought of as a table with a single 'correct values' row, with columns of each unique ID and its correct value in the cell. This dictionary would then be referenced for correct values when looping through the problematic feature classes. 

Admittedly I'm suggesting some fairly advanced usage of the concepts so it might be confusing at first if you're unfamiliar with dictionaries and cursors. It might help to see the documentation for them: 

Python dictionaries: 5. Data Structures — Python documentation 

Cursors with examples Accessing data using cursors—ArcPy Get Started | ArcGIS Desktop 

LarryAdgate
Regular Contributor

This is Great stuff, Thank You