2

I have 200 shape files which have been named using an integer at the beginning of the name...i.e 01.03.shp 01.04.shp etc. They are all symbolized within an mxd document.

I need to import these shapes into a file-GDB which is not permitted due to the fact that the feature class starts with a number - I can iterate through the shapes and rename them using a prefix, but then i´ll need to change the mxd reference for every single feature class because the names are different. Is there a clever way to get around having to fix every single reference in the mxd?

1 Answer 1

2

I had a similar problem to yours when my work upgraded its computer and changed around the network. In my instance it resulted in hundreds of maps with broken data links. I wrote a script that, for all intensive purposes, should work, but fails whenever it reaches the lyr.replaceDataSource, and ultimately crashes when a datasource is a personal geodatabase.

However, it is a start for you, especially around line 32 - newDataSource = brokenDataSource.replace(r'N:', r'c:\users\athom\Documents\GIS\GIS_computer_06_20_2013\GIS_LOCAL_MASTER')

Hopefully you can use this as a start.

import arcpy, os, sys, traceback
workspace = r'c:\users\athom\Documents\GIS\GIS_computer_06_20_2013\GIS_LOCAL_MASTER\MAPS\Map_Projects\TEST'
arcpy.env.workspace = workspace
mxdList = arcpy.ListFiles("*.mxd")
for Map in mxdList:
    filePath = os.path.join(workspace, Map)
    mxd = arcpy.mapping.MapDocument(filePath)
    brkLayerList = arcpy.mapping.ListBrokenDataSources(mxd)
    for lyr in brkLayerList:
        if lyr.supports("DATASOURCE") and lyr.supports("WORKSPACEPATH"):
            brokenDataSource = lyr.dataSource
            print brokenDataSource
            if brokenDataSource.startswith('N'):
                if brokenDataSource == brokenDataSource:
                    newDataSource = brokenDataSource.replace(r'N:', r'c:\users\athom\Documents\GIS\GIS_computer_06_20_2013\GIS_LOCAL_MASTER')
                    layerName = lyr.datasetName
                    ChangedDataSet = newDataSource.replace(layerName, '')
                    print 'newDataSource = ' + newDataSource
                    print 'ChangedDataSet = ' + ChangedDataSet
                    print 'layerName = ' + layerName
                    try:
                        if "shp" in newDataSource:
                            ChangedDataSet =ChangedDataSet.replace(' ','')[:-5]
                            print ChangedDataSet
                            layerName = layerName+'.shp'
                            print layerName
                            lyr.replaceDataSource(ChangedDataSet, "SHAPEFILE_WORKSPACE", layerName, True)
                            lyr.save()
                        elif "mdb" in newDataSource:
                           accessName = 'mdb'
                           ChangedDataSet = ChangedDataSet.split(accessName,1)[0]
                           ChangedDataSet = ChangedDataSet + 'mdb'
                           print 'ChangedDataSet = ' + ChangedDataSet
                           #lyr.replaceDataSource(ChangedDataSet, "ACCESS_WORKSPACE", layerName, True)
                           lyr.save()
                        elif "gdb" in newDataSource:
                            lyr.replaceDataSource(ChangedDataSet, "FILEGDB_WORKSPACE", layerName, True)
                        else:
                            lyr.replaceDataSource(ChangedDataSet, "RASTER_WORKSPACE", layerName, True)
                    except Exception as e:
                        print arcpy.AddError(e)
                        print "N FAILED"
            elif brokenDataSource.startswith ('n'):
                newDataSource = brokenDataSource.replace(r'n:', r'c:\users\athom\Documents\GIS\GIS_computer_06_20_2013\GIS_LOCAL_MASTER')
                layerName = lyr.datasetName
                ChangedDataSet = newDataSource.replace(layerName, '')
                print newDataSource
                print ChangedDataSet
                print layerName
                try:
                    if "shp" in newDataSource:
                        ChangedDataSet =ChangedDataSet.replace(' ','')[:-5]
                        print ChangedDataSet
                        layerName = layerName+'.shp'
                        print layerName
                        lyr.replaceDataSource(ChangedDataSet, "SHAPEFILE_WORKSPACE", layerName, True)
                    elif "mdb" in newDataSource:
                        lyr.replaceDataSource(ChangedDataSet, "ACCESS_WORKSPACE", layerName, True)
                    elif "gdb" in newDataSource:
                        lyr.replaceDataSource(ChangedDataSet, "FILEGDB_WORKSPACE", layerName, True)
                    else:
                        lyr.replaceDataSource(ChangedDataSet, "RASTER_WORKSPACE", layerName, True)
                except arcpy.ExecuteError:
                    arcpy.AddError(GetMessages(2))
                    print "n FAILED"

                else:
                    print 'Data Source Lost'
        mxd.save()
    #mxd.save()
    del mxd
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.