zankb

arcpy.mapping.Lauyer replaceDataSource method failing to add shp

Discussion created by zankb on Feb 7, 2013
Latest reply on Feb 8, 2013 by zankb
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

Outcomes