<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: arcpy.mapping.Lauyer replaceDataSource method failing to add shp in Mapping Questions</title>
    <link>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339970#M3712</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I looks to me that your workspace is not path to the folder containing the shapefiles but rather pointing to the shapefile itself.&amp;nbsp; Don't you want to use data_path instead?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example.&amp;nbsp; I have a workspace ("C:\Temp") with two shapefiles: "Lakes.shp" and "NewLakes.shp".&amp;nbsp; My MXD has a LAKES layer that points Lakes.shp and I want to change it to NewLakes.shp.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxd = arcpy.mapping.MapDocument(path to mxd)
lyr = arcpy.mapping.ListLayers(mxd, "LAKES")[0]
lyr.replaceDataSource(r&lt;STRONG&gt;"C:\Temp"&lt;/STRONG&gt;, "SHAPEFILE_WORKSPACE", &lt;STRONG&gt;"NewLakes")&lt;/STRONG&gt;
mxd.save()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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".&amp;nbsp; Do have to because it is a shapefile workspace.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:04:44 GMT</pubDate>
    <dc:creator>JeffBarrette</dc:creator>
    <dc:date>2021-12-11T16:04:44Z</dc:date>
    <item>
      <title>arcpy.mapping.Lauyer replaceDataSource method failing to add shp</title>
      <link>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339969#M3711</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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&amp;nbsp; 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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code that creates the shapefile and sets up the path to it to a variable:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
#Create a shapefile of the selected&amp;nbsp; 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')
 &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code that sets up the project sections layer and should (hypothetically) replace the layers data source with the newly created shapefile.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; 
#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")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyone out there had an issue like this before?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ben Z&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:04:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339969#M3711</guid>
      <dc:creator>BenjaminZank1</dc:creator>
      <dc:date>2021-12-11T16:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.Lauyer replaceDataSource method failing to add shp</title>
      <link>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339970#M3712</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I looks to me that your workspace is not path to the folder containing the shapefiles but rather pointing to the shapefile itself.&amp;nbsp; Don't you want to use data_path instead?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example.&amp;nbsp; I have a workspace ("C:\Temp") with two shapefiles: "Lakes.shp" and "NewLakes.shp".&amp;nbsp; My MXD has a LAKES layer that points Lakes.shp and I want to change it to NewLakes.shp.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxd = arcpy.mapping.MapDocument(path to mxd)
lyr = arcpy.mapping.ListLayers(mxd, "LAKES")[0]
lyr.replaceDataSource(r&lt;STRONG&gt;"C:\Temp"&lt;/STRONG&gt;, "SHAPEFILE_WORKSPACE", &lt;STRONG&gt;"NewLakes")&lt;/STRONG&gt;
mxd.save()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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".&amp;nbsp; Do have to because it is a shapefile workspace.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:04:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339970#M3712</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2021-12-11T16:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.Lauyer replaceDataSource method failing to add shp</title>
      <link>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339971#M3713</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I looks to me that your workspace is not path to the folder containing the shapefiles but rather pointing to the shapefile itself.&amp;nbsp; Don't you want to use data_path instead?&lt;BR /&gt;&lt;BR /&gt;Here is an example.&amp;nbsp; I have a workspace ("C:\Temp") with two shapefiles: "Lakes.shp" and "NewLakes.shp".&amp;nbsp; My MXD has a LAKES layer that points Lakes.shp and I want to change it to NewLakes.shp.&lt;BR /&gt;&lt;BR /&gt;Here is the code:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxd = arcpy.mapping.MapDocument(path to mxd)
lyr = arcpy.mapping.ListLayers(mxd, "LAKES")[0]
lyr.replaceDataSource(r&lt;STRONG&gt;"C:\Temp"&lt;/STRONG&gt;, "SHAPEFILE_WORKSPACE", &lt;STRONG&gt;"NewLakes")&lt;/STRONG&gt;
mxd.save()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;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".&amp;nbsp; Do have to because it is a shapefile workspace.&lt;BR /&gt;&lt;BR /&gt;Jeff&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you so much, that did the trick. The code works exactly as expected now.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Ben&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:04:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/arcpy-mapping-lauyer-replacedatasource-method/m-p/339971#M3713</guid>
      <dc:creator>BenjaminZank1</dc:creator>
      <dc:date>2021-12-11T16:04:46Z</dc:date>
    </item>
  </channel>
</rss>

