Exporting selected featurs in a Shapfile

3487
4
07-10-2012 02:17 AM
IlanGoichman
New Contributor III
Hello,

I have a script that:
1) makes a buffer around the input.

2) intersects the buffer with other shapefiles.

3) selects the intersecting features in every shapefile.

then i want to export the selected featurs (only selected featurs) from each shapefile into new shapefiles.

Ive tried using arcpy.CopyFeatures_managment () and also arcpy.Select_analysis ().

Both work good and give me the new shapefiles.

But here is the problam: not all the shapefiles intersects with the buffer, so some of them have no selected features at all.
But both arcpy.CopyFeatures_managment () and  arcpy.Select_analysis () make new shapefiles for all of them, even if the original shapefile had no selected features.
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?
how can i export only the selected features in the shapefiles that do intersect with the buffer?  

Here is an example of the code:

# Import the arcgisscripting module. import arcpy import os import sys import string  # Setting the Variabls.  # Input Shapefile or GDB Feature class (must be Polygon). Input_Kav_Kachol = arcpy.GetParameterAsText(0)  # Output Location (the folder where the resualt will be saved, can be GDB or Dataset inside GDB). Workspace = arcpy.GetParameterAsText(1)  # Setting the Buffer distance around the Input Shapefile or Feature class. Distance = arcpy.GetParameterAsText(2)  arcpy.env.workspace = Workspace  Output_Feature_Class = "Buffer"   # Creating the Buffer. result = arcpy.Buffer_analysis(Input_Kav_Kachol, Output_Feature_Class, Distance, "", "", "all") buffc = result.getOutput(0)  # 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"  # 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_)  # 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")  # Exporting the selected features to new Shapefiles. arcpy.CopyFeatures_management(Output_Layer, "topo_lines") arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")


So Output_Layer (topo_lines) does intersect with the buffer and has selected features in it, so the new Shapefile has rows in it.
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.

Will apriciate any tip 🙂
Tags (2)
0 Kudos
4 Replies
MarcinGasior
Occasional Contributor III
Try using Get Count to test if there're selected features before copying features to new feature class:
# Exporting the selected features to new Shapefiles if there're selected features
if arcpy.GetCount_management(Output_Layer).getOutput(0) > 0:
    arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if arcpy.GetCount_management(Output_Layer__6_).getOutput(0) > 0:
    arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")
0 Kudos
IlanGoichman
New Contributor III
Try using Get Count to test if there're selected features before copying features to new feature class:
# Exporting the selected features to new Shapefiles if there're selected features
if arcpy.GetCount_management(Output_Layer).getOutput(0) > 0:
    arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if arcpy.GetCount_management(Output_Layer__6_).getOutput(0) > 0:
    arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")


Thank you for the help, but still no good. i get 2 new shapfiles:

1) topo_lines, which is ok.
2) infrastructure, which is empty.

maybe there is a way of deleting the empty shapefiles after they are made, in the script?
0 Kudos
MarcinGasior
Occasional Contributor III
You're right - previos code was not working. It seem that GetCount output has to be casted to integer before comparing.
This code should work - added int() around GetCount:
# Exporting the selected features to new Shapefiles if there're selected features
if int(arcpy.GetCount_management(Output_Layer).getOutput(0)) > 0:
    arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if int(arcpy.GetCount_management(Output_Layer__6_).getOutput(0)) > 0:
    arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")
0 Kudos
IlanGoichman
New Contributor III
You're right - previos code was not working. It seem that GetCount output has to be casted to integer before comparing.
This code should work - added int() around GetCount:
# Exporting the selected features to new Shapefiles if there're selected features
if int(arcpy.GetCount_management(Output_Layer).getOutput(0)) > 0:
    arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if int(arcpy.GetCount_management(Output_Layer__6_).getOutput(0)) > 0:
    arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure")


Yes! int () solved the problam.
Now i recieve only 1 shapefile, topo_line, which intersects with the buffer. 
Thank you very much 🙂
0 Kudos