Problem With lry.refreshDataSource

1497
5
Jump to solution
11-28-2016 11:05 AM
ChrisHolmes
Occasional Contributor III

Good day everyone,

I am playing around with the following code to fix broken data sources for file gedatabases. There's something I am getting wrong in it and I am getting the following error messages displayed. If anyone can point out what I'm doing wrong I would greatly appreciate it.

Thanks very much,

Chris

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == 'roads':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb|data','FILEGDB_WORKSPACE','roads')
    elif lyr.name == 'railways':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb|data','FILEGDB_WORKSPACE','railways')
    elif lyr.name == 'rivers':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb|data','FILEGDB_WORKSPACE','rivers')
    elif lyr.name == 'lakes':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb|data','FILEGDB_WORKSPACE','lakes')
    elif lyr.name == 'bridge_polygons':
        lyr.replaceDataSource('X:/GIS/geo_databases/planning_data.gdb|data','FILEGDB_WORKSPACE','bridge_polygons')
del mxd
Result = True
arcpy.SetParameterAsText(0,Result)

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Esteemed Contributor

Just a guess, without looking at it in detail a couple observations....

  • do you need to save the mxd before you delete it?
  • why is there a pipe ( |  )  in your argument?

You may want to look at the samples near the end of this help doc

Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop 

View solution in original post

5 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

Just a guess, without looking at it in detail a couple observations....

  • do you need to save the mxd before you delete it?
  • why is there a pipe ( |  )  in your argument?

You may want to look at the samples near the end of this help doc

Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop 

ChrisHolmes
Occasional Contributor III

Hi Rebecca,

1. I added in mxd.save, makes no difference to the error.

2. The pipe, I believe indicates that 'data' is a feature dataset in the file geodatabase. I have used it successfully in other scripts, but not sure if it works for this instance?

Hopefully this helps provide a bit more info.

thanks

0 Kudos
MitchHolley1
MVP Regular Contributor

This path doesn't seem right to me:

X:/GIS/geo_databases/labelling_features.gdb|data

Do you mean to put '/' instead of '|'?

X:/GIS/geo_databases/labelling_features.gdb/data

 

ChrisHolmes
Occasional Contributor III

In the link provided by Rebecca: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/updatingandfixingdatasources.htm#ESRI...

I read:

Do not include the names of geodatabase feature datasets in the workspace path. Feature datasets are part of the workspace. If a feature class, for example, is moved from being a stand-alone feature class into a geodatabase feature dataset, a map document will still open without the layer being broken.

So I removed the reference to the feature datasets in each workspace "|data".

The script works now, but it doesn't do anything. lol

I'll have to recheck the parameters I'm using.

ChrisHolmes
Occasional Contributor III

I figured it out. I needed to add arcpy.RefreshTOC(). duh. I'll give the correct answer to Rebecca as she got me onto the path of getting this working. Funny that I had previously looked at that link just didn't see the item about the feature datasets. Thanks also for your suggestions Mitch.

Here is the finished working script:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == 'roads':
        lyr.replaceDataSource(r"X:\GIS\geo_databases\labelling_features.gdb","FILEGDB_WORKSPACE","roads")
    if lyr.name == 'railways':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','railways')
    elif lyr.name == 'rivers':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','rivers')
    elif lyr.name == 'lakes':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','lakes')
    elif lyr.name == 'bridge_polygons':
        lyr.replaceDataSource('X:/GIS/geo_databases/planning_data.gdb','FILEGDB_WORKSPACE','bridge_polygons')
arcpy.RefreshTOC()
mxd.save
del mxd
Result = True
arcpy.SetParameterAsText(0,Result)