<?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: Exporting selected featurs in a Shapfile in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212185#M16356</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You're right - previos code was not working. It seem that GetCount output has to be casted to integer before comparing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This code should work - added int() around GetCount:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Exporting the selected features to new Shapefiles if there're selected features
if int(arcpy.GetCount_management(Output_Layer).getOutput(0)) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if int(arcpy.GetCount_management(Output_Layer__6_).getOutput(0)) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 10:26:04 GMT</pubDate>
    <dc:creator>MarcinGasior</dc:creator>
    <dc:date>2021-12-11T10:26:04Z</dc:date>
    <item>
      <title>Exporting selected featurs in a Shapfile</title>
      <link>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212182#M16353</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;BR /&gt;&lt;SPAN&gt;I have a script that:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1) makes a buffer around the input.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) intersects the buffer with other shapefiles.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) selects the intersecting features in every shapefile. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;then i want to export the selected featurs (only selected featurs) from each shapefile into new shapefiles. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Ive tried using arcpy.CopyFeatures_managment () and also arcpy.Select_analysis ().&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Both work good and give me the new shapefiles.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But here is the problam: not all the shapefiles intersects with the buffer, so some of them have no selected features at all.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But both arcpy.CopyFeatures_managment () and&amp;nbsp; arcpy.Select_analysis () make new shapefiles for all of them, even if the original shapefile had no selected features.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This means some of the new shapefiles i get are empty and have no rows in them (becouse there were no selected features to export). so why does the script export those empty shapefiles?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;how can i export only the selected features in the shapefiles that do intersect with the buffer?&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example of the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Import the arcgisscripting module. import arcpy import os import sys import string&amp;nbsp; # Setting the Variabls.&amp;nbsp; # Input Shapefile or GDB Feature class (must be Polygon). Input_Kav_Kachol = arcpy.GetParameterAsText(0)&amp;nbsp; # Output Location (the folder where the resualt will be saved, can be GDB or Dataset inside GDB). Workspace = arcpy.GetParameterAsText(1)&amp;nbsp; # Setting the Buffer distance around the Input Shapefile or Feature class. Distance = arcpy.GetParameterAsText(2)&amp;nbsp; arcpy.env.workspace = Workspace&amp;nbsp; Output_Feature_Class = "Buffer"&amp;nbsp;&amp;nbsp; # Creating the Buffer. result = arcpy.Buffer_analysis(Input_Kav_Kachol, Output_Feature_Class, Distance, "", "", "all") buffc = result.getOutput(0)&amp;nbsp; # Setting Local variables for the Make Feature Layer tool: topo_line_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\topo_line.shp" infra_line_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\infra_line.shp" Output_Layer = "topo_line_Layer" Output_Layer__6_ = "infra_line_Layer"&amp;nbsp; # Making temporary Feature Layers from the mamag_mapi shapefils. arcpy.MakeFeatureLayer_management(topo_line_shp, Output_Layer) arcpy.MakeFeatureLayer_management(infra_line_shp, Output_Layer__6_)&amp;nbsp; # Selecting all the Lines and Polygons from mamag_mapi Shapefiles # that intersects with the buffer. arcpy.SelectLayerByLocation_management(Output_Layer, "INTERSECT", buffc, "", "NEW_SELECTION") arcpy.SelectLayerByLocation_management(Output_Layer__6_, "INTERSECT", buffc, "", "NEW_SELECTION")&amp;nbsp; # Exporting the selected features to new Shapefiles. arcpy.CopyFeatures_management(Output_Layer, "topo_lines") arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So Output_Layer (topo_lines) does intersect with the buffer and has selected features in it, so the new Shapefile has rows in it. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But Output_Layer__6_ (infrastructure) does not intersect with the buffer. Regardles i get a new shapefile which is empty - and i dont want to get this empty shapefile at all. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Will apriciate any tip &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Jul 2012 09:17:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212182#M16353</guid>
      <dc:creator>IlanGoichman</dc:creator>
      <dc:date>2012-07-10T09:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting selected featurs in a Shapfile</title>
      <link>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212183#M16354</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try using &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Get_Count/0017000000n7000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;Get Count&lt;/A&gt;&lt;SPAN&gt; to test if there're selected features before copying features to new feature class:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Exporting the selected features to new Shapefiles if there're selected features
if arcpy.GetCount_management(Output_Layer).getOutput(0) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if arcpy.GetCount_management(Output_Layer__6_).getOutput(0) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:25:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212183#M16354</guid>
      <dc:creator>MarcinGasior</dc:creator>
      <dc:date>2021-12-11T10:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting selected featurs in a Shapfile</title>
      <link>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212184#M16355</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Try using &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Get_Count/0017000000n7000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;Get Count&lt;/A&gt; to test if there're selected features before copying features to new feature class:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Exporting the selected features to new Shapefiles if there're selected features
if arcpy.GetCount_management(Output_Layer).getOutput(0) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if arcpy.GetCount_management(Output_Layer__6_).getOutput(0) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for the help, but still no good. i get 2 new shapfiles:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) topo_lines, which is ok.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2) infrastructure, which is empty. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;maybe there is a way of deleting the empty shapefiles after they are made, in the script?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:26:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212184#M16355</guid>
      <dc:creator>IlanGoichman</dc:creator>
      <dc:date>2021-12-11T10:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting selected featurs in a Shapfile</title>
      <link>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212185#M16356</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You're right - previos code was not working. It seem that GetCount output has to be casted to integer before comparing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This code should work - added int() around GetCount:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Exporting the selected features to new Shapefiles if there're selected features
if int(arcpy.GetCount_management(Output_Layer).getOutput(0)) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if int(arcpy.GetCount_management(Output_Layer__6_).getOutput(0)) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:26:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212185#M16356</guid>
      <dc:creator>MarcinGasior</dc:creator>
      <dc:date>2021-12-11T10:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting selected featurs in a Shapfile</title>
      <link>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212186#M16357</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You're right - previos code was not working. It seem that GetCount output has to be casted to integer before comparing.&lt;BR /&gt;This code should work - added int() around GetCount:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Exporting the selected features to new Shapefiles if there're selected features
if int(arcpy.GetCount_management(Output_Layer).getOutput(0)) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if int(arcpy.GetCount_management(Output_Layer__6_).getOutput(0)) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes! int () solved the problam.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Now i recieve only 1 shapefile, topo_line, which intersects with the buffer.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:26:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/exporting-selected-featurs-in-a-shapfile/m-p/212186#M16357</guid>
      <dc:creator>IlanGoichman</dc:creator>
      <dc:date>2021-12-11T10:26:06Z</dc:date>
    </item>
  </channel>
</rss>

