2

What I want from python script, searching a workspace containing Geodatabases and feature classes(Iterate Feature Classes), then clip those by interested Area (polygon). the resulted clipped files most not empty. I tried to convert Model builder to Python script and added "If" condition to transfer not empty features to final geodatabase:

import arcpy
from arcpy import env
#script arguments
env.workspace=arcpy.GetParameterASText(0)
clip_Features = arcpy.GetParameterASText(1)
FeatureClass_clip = arcpy.GetParameterASText(2)


#Local variables:
Recursive = "true"
Name = "FeatureClass"
Feature_Class = "\\FeatureClass"
FeatureClass_Clip__2_= FeatureClass_clip

FeatureClass = arcpy.ListFeatueClasses("*")
#Process : Clip
arcpy.Clip_analysis(FeatureClass, Clip_Features, FeatureClass_Clip, "")
#Process: Delet Features
for fc in FeatureClass_Clip:
    Count1=str(arcpy.Getcount_managment(fc))
    if count1 <> "0":
       arcpy.FeatureClassToGeodatabase_conversion(FeatureClass_Clip, "")

I imported the script to toolbox and defined parameters, but script not ran because of clip raise error.

  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. You say it doesn't work, but haven't included any info about what it does that isn't what you want. Please edit your question to include more detail about what your script does do including any error messages. – Midavalo May 24 '17 at 14:56
0

Try this thing. Here it works with gdb files. Make two input parameters: the first is *.gbd with feature classes you want to check, the second is clip feature class

import arcpy
import  os

from arcpy import env


#script arguments


gdb=arcpy.GetParameterAsText(0) #type - workspace
clip_Features = arcpy.GetParameterAsText(1) #type - feature class
out_gdb = arcpy.GetParameterAsText(2) #type - workspace


arcpy.env.workspace = gdb


#get feature classes full paths
#this thing will search only for feature classes in the root of gdb file, not in datasets


flist = arcpy.ListFeatureClasses("*")
list_to_clip = flist



#make feature layer of clip feature class


arcpy.MakeFeatureLayer_management(clip_Features ,"clip_layer")


#iterate through feature classes of gdb

for n in list_to_clip:
    in_fc = os.path.join(gdb, n)
    arcpy.MakeFeatureLayer_management(in_fc,"f_layer")
    arcpy.SelectLayerByLocation_management("f_layer","INTERSECT","clip_layer","","NEW_SELECTION")   

    #check if selection contains features

    check = arcpy.Describe("f_layer")

    if len(check.FIDset)!=0:

        out_fc = os.path.join (out_gdb, (n + '_clipped'))

        # at this moment you can do whatever you want with your selection, i.e. clip, copy, convert

        arcpy.Clip_analysis("f_layer","clip_layer",out_fc)
        arcpy.AddMessage("Layer "+ n + " clipped")

        arcpy.Delete_management("f_layer")



    else:
        arcpy.Delete_management("f_layer")

UPDATE: to use script with output gdb:

Added these strings:

out_gdb = arcpy.GetParameterAsText(2) #type - workspace

Changed the process of making feature layers: input layer:

for n in list_to_clip:
    in_fc = os.path.join(gdb, n)
    arcpy.MakeFeatureLayer_management(in_fc,"f_layer")

output feature class (calculated with gdb set by user in [2] parameter):

out_fc = os.path.join (out_gdb, (n + '_clipped'))

changed method of calculating the selection (len("f_layer") will give the exact number of features in feature class if they are not selected):

check = arcpy.Describe("f_layer")

    if len(check.FIDset)!=0:
| improve this answer | |
  • I think script needs a destination geodatabase to get as parameter! how can I re struct the script to transfer clipped feature classes to a new geodatabase? – J. Sobhan May 27 '17 at 5:14
  • @J.Sobhan sorry for long answer, check the code above I made some corrections, now you can use it with output gdb. Relace the text and add a parameter like Output GDB in Script Properties. – Pavel Pereverzev May 29 '17 at 8:26
  • @ Pavel Pereverzev, I appreciate your attention and time to my question. I converted the script to the tool but an error happened " clip feature dataset exists! ..." I changed source workspace, clip feature, and output workspace, the error still persists. – J. Sobhan May 31 '17 at 12:26
  • @J.Sobhan you mean you converted it into pythonaddin tool or what exactly? – Pavel Pereverzev May 31 '17 at 12:50
  • @J.Sobhan well, this script is made for *.gdb bases with feature classes. You can try to change type of arcpy.GetParameterAsText(0) to "Folder" and loop through shapefiles in folder – Pavel Pereverzev Jun 1 '17 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.