Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

So, I have about 50 mxd's with all broken layers that I have been going through and manually resetting. The data was moved from another computer and the folder structure was changed so it isn't just a simple process of changing (example):

"F:\Data\Rivers.shp" to "C:\Data\Rivers.shp"

it is more like :

"F:\Data\Rivers.shp" to C:\Data\Park\Water\Rivers.shp

and so on for all kinds of data (boundaries, veg cover, roads, etc.) each in their own sub-folder.

I wanted to make a script that I could run repeatedly whenever I opened one of these mxd's that would at least repair some of the more common layers that are found in most of my mxd's, and then I could manually repair the rest. That way I at least save myself some time.

I have been using something along the lines of :

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr == "Rivers": #Should this be the layner name in TOC or FC name?
        lyr.replaceDataSource(r"C:\Data\Park\Water", "SHAPEFILE_WORKSPACE", "Rivers")

The idea then would be to somehow add multiple "replace" scripts in the above for other common layers that are in my mxd's. However the above script doesn't even fix the Rivers layer. How can I get it to work, and also add in other layers to be repaired that have different file paths?

Sorry, I am a total novice at this stuff and I am probably going about it all wrong.

Thanks.

share|improve this question

1 Answer 1

up vote 11 down vote accepted

You are on the right track with your script. It looks like your problem lies in how you are comparing the Layer object, to the Name of the Layer in the Table of Contents.

When you use the ListLayers function, what is returned is a Layer object. You cannot then compare this to a text string to see if they are equal, you need to access the Name of the Layer instead.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name.lower() == "rivers": #This should be the Layer name in the TOC?
        lyr.replaceDataSource(r"C:\Data\Park\Water", "SHAPEFILE_WORKSPACE", "Rivers")

To add more selectors, you simply expand the if/then portion of the code to look for different layer names.

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name.lower() == "rivers": #This should be the Layer name in the TOC?
        lyr.replaceDataSource(r"C:\Data\Park\Water", "SHAPEFILE_WORKSPACE", "Rivers")
    elif lyr.name.lower() == "streets":
        lyr.replaceDataSource(r"C:\Data\Facilities\Streets", "SHAPEFILE WORKSPACE", "Streets")

Simply continue to modify the name selector to find any particular unique layer in your .mxd, and add a replaceDataSource function to handle it. Please notice that I used the lower function when testing for the name. This is also used by ESRI in their help documents, with the reasoning likely being that it removes the chance of inconsistencies in capitalization, causing a mismatch in the layer name.

Here is the ESRI Help article on Updating and Fixing Data Sources with Arcpy Mapping

The Help files for ArcGIS in general are very useful as they contain clear explanations, and a lot of Python code samples that you can simply copy/paste and modify to suit your specific needs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.2.26.2347\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Geographic Information Systems Stack Exchange works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\""], "copyright__copyright__copyright": ["Here is the ESRI Help article on Updating and Fixing Data Sources with Arcpy Mapping\n\nThe Help files for ArcGIS in general are very useful as they contain clear explanations, and a lot of Python code samples that you can simply copy/paste and modify to suit your specific needs.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}