Select to view content in your preferred language

replaceDataSource script for toolbox

1223
4
Jump to solution
10-10-2012 04:18 AM
bogdanpalade1
Deactivated User
Dear all,

I have written a small script that looks in many mxds and updates a certain layer's data source with another's. When it is hardcoded it works well. I tried to include it in a toolbox, it runs, raises no errors, but it does not do nothing. I ve been trying for the last 2 days and I do not succeed in finding what's happening.

here is the code:

import arcpy, os folderPath = arcpy.GetParameterAsText(0) #path to mxds Name = arcpy.GetParameterAsText(1)#Name of bad layer in mxds DataS= arcpy.GetParameterAsText(2)#path to new workspace NewS= arcpy.GetParameterAsText(3)#new dataset    for filename in os.listdir(folderPath):     fullpath = os.path.join(folderPath, filename)     if fullpath[-4:] == ".mxd":         mxd = arcpy.mapping.MapDocument(fullpath)         for df in arcpy.mapping.ListDataFrames(mxd):             lyrs = arcpy.mapping.ListLayers(df)             for lyr in lyrs:                 if lyr.name == Name:                     lyr.replaceDataSource(DataS, "SHAPEFILE_WORKSPACE",NewS,True)         mxd.save() del mxd


Thank you very much,

Bogdan
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
Check the value of your NewS shapefile variable.  The string includes the full path to the shapefile (e.g., "C:\Data\parks.shp"), the parameter should only include the feature class name (e.g., "parks").

I tested a couple of other parameter types and they all returned the full path.  You may need to parse it yourself.

Jeff

View solution in original post

0 Kudos
4 Replies
MarcinGasior
Frequent Contributor
In third line of the loop try:
    if fullpath[:-4] == ".mxd":
0 Kudos
bogdanpalade1
Deactivated User
In third line of the loop try:
    if fullpath[:-4] == ".mxd":


Hello,

Thanks for the reply, but I am writting fullpath[-4:] to access the last 4 characters(extension), that is the ".mxd".

I am beginning to think that the problem is how the parameters are set when I create the tool in ArcMap.

For example, here is the hardcoded script that works very well:

import arcpy, os
folderPath = r"C:\student\bgd"
for filename in os.listdir(folderPath):
    fullpath = os.path.join(folderPath, filename)
    if fullpath[-4:] == ".mxd":
        mxd = arcpy.mapping.MapDocument(fullpath)
        print str(mxd)
        for df in arcpy.mapping.ListDataFrames(mxd):
            print "The dataframe is ", df.name
            lyrs = arcpy.mapping.ListLayers(df)
            for lyr in lyrs:
                print "Layer name: ", str(lyr)
                if lyr.name =="parks":
                    lyr.replaceDataSource(r"C:\student\bgd\test_test", "SHAPEFILE_WORKSPACE","parks")
        mxd.save()

del mxd


when I set the parameters, I set them:
(0) : folder
(1): string
(2): workspace
(3): shapefile
0 Kudos
JeffBarrette
Esri Regular Contributor
Check the value of your NewS shapefile variable.  The string includes the full path to the shapefile (e.g., "C:\Data\parks.shp"), the parameter should only include the feature class name (e.g., "parks").

I tested a couple of other parameter types and they all returned the full path.  You may need to parse it yourself.

Jeff
0 Kudos
bogdanpalade1
Deactivated User
Thank you Jeff,

I changed the parameter type from shapefile to string and now it works as it should.

Regards,

Bogdan
0 Kudos