arcpy.mapping.Lauyer replaceDataSource method failing to add shp

2616
2
02-07-2013 05:45 AM
BenjaminZank1
New Contributor III
Hello,
I have created a Python toolbox to makes a set of three maps for projects within user specified township, range and section. On one of these maps I wanted like to replace a statewide sections layer with a definition query set to show only the project section with a the same layer pointing to a  shapefile of the single section created within the script itself. The reason I am doing this is that I would like the users to later package the maps and I would prefer them not to have to package sections for a good chunk of the state.

The code to create the shapefile itself runs fine, however when I try and replace the data source of the layer file I have set up for this layer with the script generated shapefile (using the replaceDataSource method), it fails every time. I have tried every combination of feeding the the work space path and dataset name to the method but nothing seems to work.

Here is the code that creates the shapefile and sets up the path to it to a variable:
     
#Create a shapefile of the selected  section gird
data_path = os.path.dirname(params[7].valueAsText)
arcpy.FeatureClassToFeatureClass_conversion("grid_layer", data_path,refgridno+'.shp')
shp_path = os.path.join(data_path, refgridno+'.shp')
 


Here is the code that sets up the project sections layer and should (hypothetically) replace the layers data source with the newly created shapefile.

  
#Set the project section label layer to the selected T.,R.,Sec value and add it to the main dataframe
prj_sec = arcpy.mapping.Layer(layerPath+"\\"+"Project Section Label.lyr")
prj_sec.replaceDataSource(os.path.dirname(shp_path),"SHAPEFILE_WORKSPACE", os.path.basename(shp_path),0)
arcpy.mapping.AddLayer(df, prj_sec, "AUTO_ARRANGE")


Anyone out there had an issue like this before?

Thanks,
Ben Z
0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
I looks to me that your workspace is not path to the folder containing the shapefiles but rather pointing to the shapefile itself.  Don't you want to use data_path instead?

Here is an example.  I have a workspace ("C:\Temp") with two shapefiles: "Lakes.shp" and "NewLakes.shp".  My MXD has a LAKES layer that points Lakes.shp and I want to change it to NewLakes.shp.

Here is the code:

mxd = arcpy.mapping.MapDocument(path to mxd)
lyr = arcpy.mapping.ListLayers(mxd, "LAKES")[0]
lyr.replaceDataSource(r"C:\Temp", "SHAPEFILE_WORKSPACE", "NewLakes")
mxd.save()


The first parameter is the path to the shape file folder (c:\temp), the third parameter is to the NewLakes shapefile, notice I'm not using the ".shp".  Do have to because it is a shapefile workspace.

Jeff
0 Kudos
BenjaminZank1
New Contributor III
I looks to me that your workspace is not path to the folder containing the shapefiles but rather pointing to the shapefile itself.  Don't you want to use data_path instead?

Here is an example.  I have a workspace ("C:\Temp") with two shapefiles: "Lakes.shp" and "NewLakes.shp".  My MXD has a LAKES layer that points Lakes.shp and I want to change it to NewLakes.shp.

Here is the code:

mxd = arcpy.mapping.MapDocument(path to mxd)
lyr = arcpy.mapping.ListLayers(mxd, "LAKES")[0]
lyr.replaceDataSource(r"C:\Temp", "SHAPEFILE_WORKSPACE", "NewLakes")
mxd.save()


The first parameter is the path to the shape file folder (c:\temp), the third parameter is to the NewLakes shapefile, notice I'm not using the ".shp".  Do have to because it is a shapefile workspace.

Jeff


Jeff,
Thank you so much, that did the trick. The code works exactly as expected now.

-Ben
0 Kudos