Updating the data source of a feature class in a feature dataset using Python in ArcGIS Pro

543
2
Jump to solution
11-10-2021 11:57 PM
MattHowe
New Contributor III

I have an ArcGIS Pro document with a number of layers in and have created three scripts to; copy the layers to a new FGDB, create a datasource lookup table where the user can then enter a new target name if the copied feature class was renamed, and update the data sources using the csv lookup table. 

Everything works great apart from feature classes found in feature datasets, which aren't repathed. I'm using the dictionary approach and layer.updateConnectionProperties(). Should the feature dataset name be part of the workspace ('database:') or possibly in with the feature class name ('dataset:')?

e.g. r"C:\GIS\Test.gdb\Fields\Oil"

Should the find dictionary database be r"C:\GIS\Test.gdb\Fields" and the dataset "Oil" or..

the find dictionary database be r"C:\GIS\Test.gdb" and the dataset "Fields\Oil" ?

# if the layer datasource matches the datasource in the LU table (row[6])
# row[4] is the source workspace path (FGDB)
# row[5] is the feature class name
# row[7] is the target workspace path (FGDB)
# row[8] is the target feature class name
if layer.dataSource == row[6]:
    find_dict = {'connection_info': {'database': row[4]},
                 'dataset': row[5],
                 'workspace_factory': 'File Geodatabase'}
    replace_dict = {'connection_info': {'database': row[7]},
                    'dataset': row[8],
                    'workspace_factory': 'File Geodatabase'}
    layer.updateConnectionProperties(find_dict, replace_dict)

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Using the connectionProperties dictionary in

Updating and fixing data sources—ArcGIS Pro | Documentation and

Updating data sources via the CIM

featuredatasets are mentioned in the latter section.

At least you can examine your existing structure to determine what the appropriate approach would be


... sort of retired...

View solution in original post

2 Replies
DanPatterson
MVP Esteemed Contributor

Using the connectionProperties dictionary in

Updating and fixing data sources—ArcGIS Pro | Documentation and

Updating data sources via the CIM

featuredatasets are mentioned in the latter section.

At least you can examine your existing structure to determine what the appropriate approach would be


... sort of retired...
MattHowe
New Contributor III

Thanks Dan. I completely overlooked the CIM part assuming it wasn't relevant. I need new glasses.

else:
    if not ".gdb" in layer.dataSource.split(os.path.sep)[-2]:
        arcpy.AddMessage(layer.dataSource)
        with arcpy.da.SearchCursor(input_csv, csv_headers) as cursor:
            for row in cursor:
                if layer.dataSource == row[6]:
                    lyr_cim = layer.getDefinition('V2')
                    dc = arcpy.cim.CreateCIMObjectFromClassName('CIMStandardDataConnection', 'V2')
                    dc.workspaceConnectionString = f"DATABASE={row[7]}"
                    dc.workspaceFactory = "FileGDB"
                    dc.dataset = row[8]
                    lyr_cim.featureTable.dataConnection = dc
                    layer.setDefinition(lyr_cim)