<?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: Trouble Accessing Layers with Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709483#M55008</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah, yes, there are some subtle differences running Python inside or outside of ArcMap. Glad it's making sense.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 13 Apr 2015 18:47:44 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2015-04-13T18:47:44Z</dc:date>
    <item>
      <title>Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709473#M54998</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In case you've been following my fairly boring saga: I am making progress on my program!&amp;nbsp; I have my files going where they need to go! Yay!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to remove intermediate and unnecessary layers from the map document.&amp;nbsp; I have a raster file, "SoilUnclass", that I want to remove from my map document to de-clutter my Table of Contents. I put the following code together based on what I've seen and read. I left my variable definitions out of the code but my inputs are all initialized with the proper file paths.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;SoilUnclass = arcpy.PolygonToRaster_conversion(soil, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"HSG"&lt;/SPAN&gt;, ScratchPath + &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"\SoilUnclass"&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"MAXIMUM_COMBINED_AREA"&lt;/SPAN&gt;)
SoilClass = arcpy.sa.Reclassify(SoilUnclass, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"HSG"&lt;/SPAN&gt;, arcpy.sa.RemapValue([[&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"A"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"B"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"C"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;3&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"D"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"A/D"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;14&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"B/D"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;24&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"C/D"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;34&lt;/SPAN&gt;],
&amp;nbsp; [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"---"&lt;/SPAN&gt;, &lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;]]), &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"NODATA"&lt;/SPAN&gt;)
SoilClass.save(WorkPath + &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"\SoilClass"&lt;/SPAN&gt;)
SoilClass.save(GDBpath + &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"\SoilClass"&lt;/SPAN&gt;)
df = arcpy.mapping.ListDataFrames(mxd,&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"*"&lt;/SPAN&gt;)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]
arcpy.mapping.RemoveLayer(df, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;SoilUnclass&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; When I run that code, I receive the following error:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;Runtime error &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 2, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\utils.py", line 182, in fn_&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return fn(*args, **kw)&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\mapping.py", line 1845, in RemoveLayer&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; assert isinstance(remove_layer, Layer)&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;AssertionError&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #303030;"&gt;...?? It may be worth noting that when I do &lt;EM&gt;arcpy.mapping.ListLayers(mxd)&lt;/EM&gt;, "SoilUnclass" is not a layer that shows up.&amp;nbsp; Only the files that were in the map document before I ran my code are listed.&amp;nbsp; I saved my document and did &lt;EM&gt;ListLayers()&lt;/EM&gt; again but nothing changed.&amp;nbsp; How can I get access to these layers to modify them? &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:25:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709473#M54998</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2021-12-12T06:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709474#M54999</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I believe this is because SoilUnclass is a raster object (data), not a layer object in your mxd. Since you never added this data to your map, there is no need (or way) to remove it. To delete data on disk, use &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Delete/001700000052000000/"&gt;Delete&lt;/A&gt;. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 16:54:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709474#M54999</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-04-13T16:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709475#M55000</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ListLayers() does bring up two raster datasets in addition to my shapefiles, so I am not sure that the problem is caused because SoilUnclass is a raster.&amp;nbsp; When I run the code in the ArcGIS python window, it adds SoilUnclass to the table of contents, so it's definitely in my map.&amp;nbsp; I don't necessarily want to delete the file, either, just get it out of my Table of Contents.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 17:09:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709475#M55000</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-13T17:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709476#M55001</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ListLayers() does not return raster datasets, it returns layers saved in your mxd that point to raster datasets. If you hit save in ArcMap (or, potentially, "mxd.save()", although I don't believe you've actually added SoilUnclass to the mxd saved on disk) once SoilUnclass has been added, you could then reference the layer that has been saved in the table of contents.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 17:18:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709476#M55001</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-04-13T17:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709477#M55002</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah, I see.&amp;nbsp; I closed and opened the MXD and now ListLayers is showing those layers. So even though the raster is showing up in the Table of Contents, it's not officially a layer until the map is saved and closed and reopened, apparently?&amp;nbsp; This poses a problem...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 17:26:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709477#M55002</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-13T17:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709478#M55003</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If I save and redefine my mxd, then it works. Should that be necessary?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 17:34:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709478#M55003</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-13T17:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709479#M55004</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This may or may not help. I started with one polygon layer ('asdasd') and ran PolygonToRaster, saving to the in_memory workspace ('OutLocation'). The 'type' of the SoilUnclass object is a '&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#/Result/018z00000046000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;Result&lt;/A&gt;' not a '&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#/Layer/00s300000008000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;Layer&lt;/A&gt;'. ListLayers() does return layer objects, including the newly created and added 'OutLocation' layer. You can remove the new layer by calling RemoveLayer referencing the layer object, not the result object. This leaves me with only my original layer, 'asdasd'. 'OutLocation' would still be saved on disk, though.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; mxd = arcpy.mapping.MapDocument('CURRENT')
... df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
... SoilUnclass = arcpy.PolygonToRaster_conversion('asdasd', "Id", r"in_memory\OutLocation", "MAXIMUM_COMBINED_AREA") 
... print type(SoilUnclass)
... print arcpy.mapping.ListLayers(mxd)
... print type(arcpy.mapping.ListLayers(mxd)[1])
... arcpy.mapping.RemoveLayer(df, arcpy.mapping.ListLayers(mxd)[1])
... print arcpy.mapping.ListLayers(mxd)
... 
&amp;lt;class 'arcpy.arcobjects.arcobjects.Result'&amp;gt;
[&amp;lt;map layer u'asdasd'&amp;gt;, &amp;lt;map layer u'OutLocation'&amp;gt;]
&amp;lt;class 'arcpy._mapping.Layer'&amp;gt;
[&amp;lt;map layer u'asdasd'&amp;gt;]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:25:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709479#M55004</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T06:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709480#M55005</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It helped because now I know about Results vs Layers!&amp;nbsp; Sure enough, my SoilUnclass is a Result data type. I am not sure how you managed to get it to add the OutLocation layer, however.&amp;nbsp; Is it because it is in the memory and it adds the layer automatically in that case?&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 18:19:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709480#M55005</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-13T18:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709481#M55006</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Actually, now that I'm running things out of PyCharm instead of the GIS Python window, I see now how the results aren't being added and how it doesn't make layers for it.&amp;nbsp; It's much faster to run my program from PyCharm than from the GIS Python window, too!&amp;nbsp; So I see that removing the results/layers is eventually going to be superfluous.&amp;nbsp; I can add any layers to the map when my code is all complete, I assume.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for taking the time to help me!&amp;nbsp; I learn so much every time I post on here.&amp;nbsp; I hope one day I know enough about Python to help with other people's problems!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 18:43:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709481#M55006</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-13T18:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709482#M55007</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No, saving to in_memory is not the issue - it also adds a layer saving to disk:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.env.scratchWorkspace = r'C:/junk'
... mxd = arcpy.mapping.MapDocument('CURRENT')
... df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
... SoilUnclass = arcpy.PolygonToRaster_conversion('asdasd', "Id", 'OutLocation', "MAXIMUM_COMBINED_AREA")
... print type(SoilUnclass)
... print arcpy.mapping.ListLayers(mxd)
... print type(arcpy.mapping.ListLayers(mxd)[1])
... arcpy.mapping.RemoveLayer(df, arcpy.mapping.ListLayers(mxd)[1])
... print arcpy.mapping.ListLayers(mxd)
...
&amp;lt;class 'arcpy.arcobjects.arcobjects.Result'&amp;gt;
[&amp;lt;map layer u'asdasd'&amp;gt;, &amp;lt;map layer u'OutLocation'&amp;gt;]
&amp;lt;class 'arcpy._mapping.Layer'&amp;gt;
[&amp;lt;map layer u'asdasd'&amp;gt;]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:25:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709482#M55007</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T06:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709483#M55008</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah, yes, there are some subtle differences running Python inside or outside of ArcMap. Glad it's making sense.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 18:47:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709483#M55008</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-04-13T18:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble Accessing Layers with Python</title>
      <link>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709484#M55009</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your problem sounds familiar, not sure if I have run into it before or helped someone who has.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyhow, part of your problem is caused by misunderstanding the &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-classes/result.htm"&gt;ArcPy Result &lt;/A&gt;object.&amp;nbsp; The result object from calling &lt;A href="http://desktop.arcgis.com/en/desktop/latest/tools/conversion-toolbox/polygon-to-raster.htm"&gt;&lt;SPAN style="color: #000080; text-decoration: underline;"&gt;Polygon to Raster (Conversion)&lt;/SPAN&gt;&lt;/A&gt;, specifically the &lt;SPAN style="font-family: courier new,courier;"&gt;getOutput&lt;/SPAN&gt; method of that result object, isn't a &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/layer-class.htm"&gt;Layer &lt;/A&gt;object or even the name of a layer in the map document.&amp;nbsp; In this case, the result object is returning the path to the newly created raster dataset.&amp;nbsp; RemoveLayer is throwing an AssertionError because it needs a "reference to a Layer object representing the layer to be removed," which the string you are passing it from the result object isn't.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It appears you are having difficulties enumerating layers in the map document.&amp;nbsp; Can you provide a code snippet where you run the geoprocessing tool and attempt to list all the layers afterwards?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 19:06:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-accessing-layers-with-python/m-p/709484#M55009</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-04-13T19:06:26Z</dc:date>
    </item>
  </channel>
</rss>

