GIS Tips – Repair broken links using python

I’m not going to lie, I don’t like python, probably because I’m turning into my dad and slowly find myself finding things a little harder to learn….I think being able to cope with C++, C#, VBA, HTML, CSS, Ferranti Fbasica, Cobal & (My favourite) Sinclair Basic, should be enough for anyone, python just tips me over the edge.

What grinds me even more though, is the ArcGIS broken links…even when you have the relative paths set, it still throws a fit every now and again and you lose all the links.

Of course, it should be easy, we all know about the trick of clicking on the red exclamation mark so that we can relink all the data again, don’t we? Here is a quick refresher fromt the ArcGIS help page:

From ArcGIS 10.0 help page 30th November 2013

When you open a map, ArcMap looks for the data referenced by each of the layers in the map. If ArcMap can’t find the data source for a particular layer, that layer won’t be drawn. You can immediately tell whether a layer on your map has a broken data link because it will have a red exclamation point next to its name in the table of contents, and the check box next to the layer will be unavailable.

Broken data link in the table of contents

A layer needs repairing if the data source it references has been moved, renamed, or deleted or is inaccessible for some other reason.

There are a number of ways to repair broken data links, which are described in the following sections:

Repairing a broken link for one layer

If you only want to repair a broken link for a specific layer, click the Set Data Source button on the Source tab of the Layer Properties dialog box.

Click the Set Data Source button to enter the correct dataset location.

This lets you specify the layer’s data source by browsing to it. Select the desired data source on the Data Source dialog box and click Add to repair the layer.

Repairing broken links for multiple layers

You may find that several layers in your map need repairing. For example, if a geodatabase containing data sources that are used for multiple layers in your map has been moved or renamed, all these layers will need to be repaired. If you want to repair several layers at once, right-click the layer with the broken link and click Data > Repair Data Source. You can also click the red exclamation mark to open the Set Data Source dialog box as shown here.

Repairing multiple data links

When the repair to the data link is made from the table of contents, ArcMap repairs the selected layer using the data source you browse to and automatically repairs other broken layers if it can find their data sources in the same location of the data source you specified.

Repairing broken links using python

Now, even with my limited python ability got the following to work. In this scenario, the data was located directly under the C:\Project\Data folder but was moved into a subfolder called Data2. This script updates a single map

 
document.import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") 
mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"C:\Project\Data2") 
mxd.saveACopy(r"C:\Project\Project2.mxd") del mxd
 

Easy peasy, but to be honest this may just be as easy as clicking on the red exclamation mark in most scenarios. But what if you tranformed all the data from shapefiles into your geodatabase?…This is where the python excels (using the exclamation mark method won’t work for this)

Re-linking data that has changed formats

This scenario involves updating two different workspaces in a map document. First, shapefiles are redirected to a file geodatabase called Parcels.gdb. Second, layers from a personal geodatabase are redirected to another file geodatabase called Transportation.gdb.

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
mxd.replaceWorkspaces(r"C:\Project\Data", "SHAPEFILE_WORKSPACE", r"C:\Project\Data\Parcels.gdb", "FILEGDB_WORKSPACE") 
mxd.replaceWorkspaces(r"C:\Project\Data\Transportation.mdb", "ACCESS_WORKSPACE", r"C:\Project\Data\Transportation.gdb", "FILEGDB_WORKSPACE") 
mxd.saveACopy(r"C:\Project\Project2.mxd") del mxd

That wasn’t so painful was it? Before I sign off, I have to credit TeachMeGIS.com for the inspiration for this post, as the first python script I reference is based on one which they posted way back in 2011 for ArcGIS 10.0Nick D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s