<?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: Loop AddLayers - Help in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504405#M39610</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Matthew&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This code really helped. The code runs through the layers and outputs as Tiff etc. to output folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Upon verification however I've noticed that there are 524 input layers but only 480 output images. The output maps and the text on the maps are out of sync. Some of the data layers do not match up to the text title (TEXT_ELEMENT=Species) (they are quite often one layer out while others are correct.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have attached the code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am confused. I have considered a few possible causes - slow refresh (but I've added in a couple of extra view refreshes), longer layer names (none are beyond 30 characters) or the fact that the second layer added in is given the addition of _Layer1 instead of _Layer. Or it could be something else - I'm not sure. Do you have any thoughts on this?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 28 Oct 2011 19:53:45 GMT</pubDate>
    <dc:creator>LorraineBarry</dc:creator>
    <dc:date>2011-10-28T19:53:45Z</dc:date>
    <item>
      <title>Loop AddLayers - Help</title>
      <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504400#M39605</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to loop (for 630 layers) through the code to add a layer to the current mxd, change the title according to the layer name and export the map. My code is running fine for a single layer but I can't get it to loop using a list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I attach the working single layer code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd=arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lyrFile=arcpy.mapping.Layer(r"C:\Temp\LAYERS\Acer_campestre.lyr")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;df=arcpy.mapping.ListDataFrames(mxd,"Layers")[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.mapping.AddLayer(df,lyrFile)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.RefreshTOC()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.RefreshActiveView()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print mxd.filePath&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;maplayers=arcpy.mapping.ListLayers(mxd,"*Layer",df)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print maplayers&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for lyr in maplayers:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyrname=lyr.name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyrname2=lyrname.replace("_Layer","")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyrname3=lyrname2.replace("_"," ")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyrname3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if elm.name == "Species":&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = lyrname3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Element not updated!"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print elm.name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.RefreshActiveView()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.mapping.ExportToTIFF(mxd,r"C:\Temp\TIFF\_"+str(lyrname3)+".tif",resolution=300)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del mxd&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now here is a stripped down version of what's not working. I have stripped it down to the part where its falling over. It seems to be at addLayer I get an error - Object:create object layer invalid data source.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace="C:/Temp/LAYERS"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd=arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;slayer=arcpy.ListFiles("*.lyr")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lcount=len(slayer)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print lcount&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for filelay in slayer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.OverwriteOutput=True&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print filelay&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; df=arcpy.mapping.ListDataFrames(mxd,"Layers")[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyrFile=arcpy.mapping.Layer(filelay)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df,lyrFile)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.SaveACopy(r"C:/Temp/Project.mxd")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd,lyrFile&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From looking online this doesn't help either: (I just get an mxd error)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mapDoc=arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lyrFiles=arcpy.GetParameterAsText(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;layers=lyrFiles.split(";")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd=arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd=arcpy.mapping.MapDocument(mapDoc)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;df=arcpy.mapping.ListDataFrames(mxd)[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for lyrFile in layers:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr=arcpy.mapping.Layer(lyrFile)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df,lyr)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd.save()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anyone help please? I would really appreciate any advice - relatively new to python&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Oct 2011 19:57:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504400#M39605</guid>
      <dc:creator>LorraineBarry</dc:creator>
      <dc:date>2011-10-27T19:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: Loop AddLayers - Help</title>
      <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504401#M39606</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sorry my code is indented properly -but got lost through copy and paste&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Oct 2011 20:13:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504401#M39606</guid>
      <dc:creator>LorraineBarry</dc:creator>
      <dc:date>2011-10-27T20:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loop AddLayers - Help</title>
      <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504402#M39607</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
try using code tags
&amp;nbsp;&amp;nbsp;&amp;nbsp; for proper indentation
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Without the spaces&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[ CODE][/CODE ]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:07:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504402#M39607</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T22:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: Loop AddLayers - Help</title>
      <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504403#M39608</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;As for your code problem, try this. Notice how disrespectful of workspaces layers are.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os
from arcpy import env
arcpy.env.OverwriteOutput=True
inputWS = r"C:/Temp/LAYERS"
env.workspace=inputWS
mxd=arcpy.mapping.MapDocument("CURRENT")
df=arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
slayer=arcpy.ListFiles("*.lyr")
lcount=len(slayer)
print lcount
for filelay in slayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print filelay
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;lyrFile=arcpy.mapping.Layer(inputWS+os.sep+filelay)&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df,lyrFile)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:07:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504403#M39608</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T22:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loop AddLayers - Help</title>
      <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504404#M39609</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Brilliant Many thanks Matthew. This worked a treat. I can now add in the rest of the tasks for each layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You've really helped. I appreciate it&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Lorraine&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Oct 2011 21:33:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504404#M39609</guid>
      <dc:creator>LorraineBarry</dc:creator>
      <dc:date>2011-10-27T21:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Loop AddLayers - Help</title>
      <link>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504405#M39610</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Matthew&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This code really helped. The code runs through the layers and outputs as Tiff etc. to output folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Upon verification however I've noticed that there are 524 input layers but only 480 output images. The output maps and the text on the maps are out of sync. Some of the data layers do not match up to the text title (TEXT_ELEMENT=Species) (they are quite often one layer out while others are correct.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have attached the code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am confused. I have considered a few possible causes - slow refresh (but I've added in a couple of extra view refreshes), longer layer names (none are beyond 30 characters) or the fact that the second layer added in is given the addition of _Layer1 instead of _Layer. Or it could be something else - I'm not sure. Do you have any thoughts on this?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 28 Oct 2011 19:53:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-addlayers-help/m-p/504405#M39610</guid>
      <dc:creator>LorraineBarry</dc:creator>
      <dc:date>2011-10-28T19:53:45Z</dc:date>
    </item>
  </channel>
</rss>

