<?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: Pyhton: Create Layer from Selected Features? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218644#M16809</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, this sounds like what I want to do. Basically, I have point feature class in a template mxd, properly symbolized using Unique Values, many Fields. The points lie within one of 5 polygons from a polygon feature class. I want to select the points that lie in the first polygon (so a select by location, not a select by attribute like I mentioned below) and then save only the selected points in a layer file exactly how the right click on layer--&amp;gt;Selection--&amp;gt;Create Layer from Selected Features does so that the layer only retains the symbology for the selected features. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What was your method?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 27 Jul 2012 17:08:05 GMT</pubDate>
    <dc:creator>MikeMacRae</dc:creator>
    <dc:date>2012-07-27T17:08:05Z</dc:date>
    <item>
      <title>Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218642#M16807</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm wondering if there is a python method/workflow model that will allow me to do the same thing as right clicking on a layer in ArcMap and going to Selection--&amp;gt;Create Layer from Selected Features? I've tried to create a model using select by attribute --&amp;gt; Make layer File --&amp;gt; Save to Layer File, but this doesn't work. The symbology for all feature still persist. I doubt this is doable, but I thought I'd throw it out there.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Jul 2012 14:52:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218642#M16807</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2012-07-27T14:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218643#M16808</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, in my humble opinon and experience the 'Create Layer from Selected Features' tool has created more problems than has resolved. But understanding what you want to do here, I've been able to 'emulate' this behavior with the end result being a layer created from a selection but which has a properly defined selection query so you don't see all the other features. Is this the sort of thing you want to do?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Jul 2012 15:56:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218643#M16808</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-07-27T15:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218644#M16809</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, this sounds like what I want to do. Basically, I have point feature class in a template mxd, properly symbolized using Unique Values, many Fields. The points lie within one of 5 polygons from a polygon feature class. I want to select the points that lie in the first polygon (so a select by location, not a select by attribute like I mentioned below) and then save only the selected points in a layer file exactly how the right click on layer--&amp;gt;Selection--&amp;gt;Create Layer from Selected Features does so that the layer only retains the symbology for the selected features. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What was your method?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Jul 2012 17:08:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218644#M16809</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2012-07-27T17:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218645#M16810</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I want to select the points that lie in the first polygon (so a select by location, not a select by attribute like I mentioned below) and then save only the selected points in a layer&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is exactly the scenario that I was thinking about.&amp;nbsp; I'm away from my office today but on Monday I'll post the code that I used so you can see an example.&amp;nbsp; Basically, the strategy is follows these steps:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;make your polygon selection&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;select by location which gives you basically a selection set&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;use a search cursor to collect some sort of ID value from the selected features into a python list&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;use the python list to create a string list of values that can be used in a definition query of the form "select from &amp;lt;fc&amp;gt; where &amp;lt;ID&amp;gt; in ('values from python list')"&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Make a feature layer from your original &lt;SPAN style="font-style:italic;"&gt;unselected&lt;/SPAN&gt; point data using the sql query&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;save the feature layer to a .lyr file on disk using arcpy.SaveToFeatureLayer&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;once you've done this, you'll probably want to work with the arcpy.mapping module to add that layer to your map and use the UpdateLayer function to apply symbology to that layer from your existing symbolized point layer.&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 29 Jul 2012 12:10:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218645#M16810</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-07-29T12:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218646#M16811</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, here is the code I promised.. this is an example of how to accomplish this, you'd have to tweak it to be specific to your data:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def make_paz_layer(keyhole): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; paz_units = [] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SelPaz = arcpy.SelectLayerByLocation_management (PAZ_lyr, 'Intersect', keyhole) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(SelPaz," \"POWERPLANT\" = 'North Anna Power Station'", "", "PAZ;PAZ_NUM;PAZ_ALPHA","PAZ_NUM A;PAZ_ALPHA A") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; unit = str(row.PAZ) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; unum = "'" + unit + "'" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; paz_units.append(unum) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; txt_paz = ','.join(paz_units) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; paz_list = '(' + txt_paz + ')' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; paz_qry = ' "POWERPLANT" = '+ station_name + ' AND "PAZ" in ' + paz_list &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_paz = arcpy.MakeFeatureLayer_management(paz, keyhole + '_paz', paz_qry) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management(new_paz, outfolder + '\\' + keyhole + '_paz.lyr')&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;A couple of notes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;This is a function that accepts a layer as an argument - the layer could be a .lyr file on disk or one created in-memory&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;PAZ_lyr (in the SelectByLocation line) references an already created layer using MakeFeatureLayer_management&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;the line &lt;SPAN style="text-decoration:underline;"&gt;unum = "'" + unit + "'"&lt;/SPAN&gt; is needed if your unique values are strings/text... this isn't necessary if those values are numeric&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;SPAN&gt;Let me know if this helps, you have questions or if i can help more.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jul 2012 13:58:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218646#M16811</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-07-31T13:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218647#M16812</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Christopher, thanks for you contribution and persistence in getting me an answer. Your script took me in a little different direction than I was on. Specifically, building a list and converting to string to be used in a definition query. I wasn't even thinking definition query, but then again, I haven't used them much in the past.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This also resolved the issue with creating a second layer. I don't need to do that now because I can edit the current layer in the map. Basically, I have a map template with template layers. I can now edit the template layer by using the definition query and saveACopy of the mxd elsewhere.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm going to attempt to use this script in a python addin button in the template, so that the user can just select a polygon and press the button to update the layer. Something like that anyways.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my script so far, for those who are interested:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "Point":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Point = lyr
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "Polygon":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Polygon = lyr

arcpy.SelectLayerByAttribute_management(Polygon, "NEW_SELECTION", "\"SITE\" = 'Pad 1'")
arcpy.SelectLayerByLocation_management(Point, "WITHIN", Polygon, "", "NEW_SELECTION")

pointList = []

for row in arcpy.SearchCursor(Point):
&amp;nbsp;&amp;nbsp;&amp;nbsp; strrow = str(row.OBJECTID)
&amp;nbsp;&amp;nbsp;&amp;nbsp; soilList.append(strrow)&amp;nbsp;&amp;nbsp;&amp;nbsp; 

txt_row = ','.join(soilList)
row_list = '(' + txt_row + ')'
row_qry = ' "OBJECTID" in ' + row_list

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "Point":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = row_qry

arcpy.SelectLayerByAttribute_management(Point, "CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management(Polygon, "CLEAR_SELECTION")

arcpy.RefreshActiveView()
arcpy.RefreshTOC()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

mxd.save()

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:40:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218647#M16812</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2021-12-11T10:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218648#M16813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey, glad to help! I'll be interested to know how your project works out.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jul 2012 19:06:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218648#M16813</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-07-31T19:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: Pyhton: Create Layer from Selected Features?</title>
      <link>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218649#M16814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks again Chris.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I installed the addin as a "Button". I also discovered that the script will honor any selections in the "CURRENT" MapDocument, so I commented out the 'SelectByAttribute' syntax (which was used to select a polygon to be used in the 'SelectByLocation" syntax). Now, I manually select a polygon, click the button and it does all the processing to update only the points that lie within the selected polygon. Some nice success on my first python addin attempt!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jul 2012 20:25:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyhton-create-layer-from-selected-features/m-p/218649#M16814</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2012-07-31T20:25:07Z</dc:date>
    </item>
  </channel>
</rss>

