How can I update multiple feature class attribute tables with the same values?

2449
2
05-25-2016 01:12 AM

How can I update multiple feature class attribute tables with the same values?

Dear Xander Bakker,

Please find the attached for the my pgdb.

I am still getting some error from the script you have sent me. please advise. thanks

I have a geodatabase with multiple feature classes in multiple feature datasets.  Each feature class has same fields which are identical and will carry the same value. plzhelp me.

I have fields in the below.

BUILDING_ID

COMMENT

DISCIPLINE_CODE

DRAWING_REFERENCE

FLOOR_ID

OWNERSHIP_CODE

PARCEL_ID

SUBDISCIPLINE_CODE

  1. import arcpy 
  2. import os 
  3. import sys 
  4.  
  5. #Set Overwrite Option 
  6. arcpy.env.overwriteOutput = True 
  7.  
  8. # additional parameter to provide the workspace 
  9. ws = r"D:\Xander\GeoNet\MultiAttsUpdate\gdb\Sample.gdb" 
  10.  
  11. # fixed values to assing to fields 
  12. building_id = 'your building id' 
  13. comment = 'your comment' 
  14. discipline_code = 'your discipline code' 
  15. drawing_ref = 'your drawing reference' 
  16. floor_id = 'your floor id' 
  17. ownership_code = 'your ownership code' 
  18. parcel_id = 'your parcel id' 
  19. subdiscipline_code = 'your subdiscipline code' 
  20.  
  21. # create a dictionary with the field name and corresponding value 
  22. dct_fld_val = {'BUILDING_ID': building_id, 
  23.                'COMMENT': comment, 
  24.                'DISCIPLINE_CODE': discipline_code, 
  25.                'DRAWING_REFERENCE': drawing_ref, 
  26.                'FLOOR_ID': floor_id, 
  27.                'OWNERSHIP_CODE': ownership_code, 
  28.                'PARCEL_ID': parcel_id, 
  29.                'SUBDISCIPLINE_CODE': subdiscipline_code} 
  30.  
  31. # fields to update 
  32. flds_upd = dct_fld_val.keys() 
  33.  
  34. # you have to set the workspace to be able to use the ListDatasets and ListFeatureClasses 
  35. arcpy.env.workspace = ws 
  36. fds = arcpy.ListDatasets(feature_type='Feature'
  37. fds.append('') # to include featureclasses that are not within a feature dataset 
  38. for fd in fds: 
  39.     fcs = arcpy.ListFeatureClasses(feature_dataset=fd) 
  40.     for fc in fcs: 
  41.         # verify that the fields are actually in the featureclass 
  42.         fc_pathname = os.path.join(ws, fd, fc) 
  43.         flds_fc = [fld.name for fld in arcpy.ListFields(fc_pathname)] 
  44.         flds = list(set(flds_upd) & set(flds_fc)) 
  45.  
  46.         # loop through rows of fc 
  47.         with arcpy.da.UpdateCursor(fc_pathname, flds) as curs: 
  48.             for row in curs: 
  49.                 for fld in flds: 
  50.                     row[flds.index(fld)] = dct_fld_val[fld] 
  51.                 curs.updateRow(row)

Thanks

Santhosh

Attachments
Comments

It would be best to create a question and not a document, since now comments do not offer the advanced editor to structure code. Below an example of the code that I executed on a copy of your file geodatabase. Note that the bold line points to a location on my drive. You should change this to the location where you have your data.

However, before you do that, be sure what you are doing. Always make a backup of the data before running the script. You data will be updated.

I noticed that there are many attributes the currently have different values which makes sense. By applying a single value to all features you will probably end up with information which is not valid. Your data has information that identifies the different floors.

By assigning a single value to all the feature, they will all be on the same floor...

Be careful with fields that have coded domain values. You will have to assign a valid code (not description).

import arcpy

import os

import sys

#Set Overwrite Option

arcpy.env.overwriteOutput = True

# additional parameter to provide the workspace

ws = r"D:\Xander\GeoNet\MultiupdateAttribs\COSC_SSUC_PGDB_FINAL.gdb"

# fixed values to assing to fields

building_id = '02'

comment = 'MB'

discipline_code = 'FK'

drawing_ref = 'C0400-GS-0114-01-EL-FA-A0-B2-001-999'

floor_id = 'UR' # coded domain - Upper Roof

ownership_code = '99COSC'

parcel_id = '115'

subdiscipline_code = 'GB'

# create a dictionary with the field name and corresponding value

dct_fld_val = {'BUILDING_ID': building_id,

               'COMMENT': comment,

               'DISCIPLINE_CODE': discipline_code,

               'DRAWING_REFERENCE': drawing_ref,

               'FLOOR_ID': floor_id,

               'OWNERSHIP_CODE': ownership_code,

               'PARCEL_ID': parcel_id,

               'SUBDISCIPLINE_CODE': subdiscipline_code}

# fields to update

flds_upd = dct_fld_val.keys()

errs = {}

# you have to set the workspace to be able to use the ListDatasets and ListFeatureClasses

arcpy.env.workspace = ws

fds = arcpy.ListDatasets(feature_type='Feature')

fds.append('') # to include featureclasses that are not within a feature dataset

for fd in fds:

    fcs = arcpy.ListFeatureClasses(feature_dataset=fd)

    for fc in fcs:

        # verify that the fields are actually in the featureclass

        print fc

        fc_pathname = os.path.join(ws, fd, fc)

        flds_fc = [fld.name for fld in arcpy.ListFields(fc_pathname)]

        flds = list(set(flds_upd) & set(flds_fc))

        # loop through rows of fc

        with arcpy.da.UpdateCursor(fc_pathname, flds) as curs:

            for row in curs:

                for fld in flds:

                    row[flds.index(fld)] = dct_fld_val[fld]

                try:

                    curs.updateRow(row)

                except Exception as e:

                    err = '{0} - {1}'.format(fc, e)

                    if err in errs:

                        errs[err] += 1

                    else:

                        errs[err] = 1

for err, cnt in errs.items():

    print err, cnt

Dear,

It's working above the code.

Thank you so much your help.

Thanks

Santhosh

Version history
Last update:
‎05-25-2016 01:12 AM
Updated by:
Contributors