Buffering Multiple feature classes in GDB (python)

9567
27
05-08-2015 07:55 AM
EthanHeidtman
New Contributor III

I am having trouble buffering multiple feature classes within a GDB. These feature classes were just created based on a select by attribute and copy features tool run.

I have attached a copy of my script. The buffer analysis begins at the bottom, I have tried various parameters and dont know the variable that will be my input features that are the selected krogers from each county.

Thanks a lot!

Tags (3)
0 Kudos
27 Replies
DarrenWiens2
MVP Honored Contributor

Are you getting an error or is it just not doing what you want it to do?

0 Kudos
EthanHeidtman
New Contributor III

It won't even run the buffer analysis.

I think I need to put it in a loop somehow.

Something like list featureclasses with a where clause being a wild card

*krogerselection

Then the loop being for fc in fcs

Buffer analysis

I'm stumped!

0 Kudos
IanMurray
Frequent Contributor

So are trying to buffer selected features of each features class that you copied to your GDB?

Original Script(for anyone else who wants to chime in)

import arcpy, os
arcpy.env.workspace = "F:/GEO443/FinalPrj"
arcpy.env.overwriteOutput = True
#Variables holding location of each counties LBRS points
brown = "F:/GEO443/FinalPrj/LBRS_data/Brown_cty/BRO_ADDS.shp"
clermont = "F:/GEO443/FinalPrj/LBRS_data/Clermont_cty/CLE_ADDS.shp"
hancock = "F:/GEO443/FinalPrj/LBRS_data/Hancock_cty/HAN_ADDS.shp"
muskingum = "F:/GEO443/FinalPrj/LBRS_data/Muskingum_cty/MUS_ADDS.shp"
richland = "F:/GEO443/FinalPrj/LBRS_data/Richland_cty/RIC_ADDS.shp"
try:
    print "Creating new file geodatabase..."
    #Creating a new GeoDatabase where shapefiles will be moved
    ##CreateFileGDB_management (out_folder_path, out_name, {out_version})
    arcpy.CreateFileGDB_management ("F:/GEO443/FinalPrj","Final.gdb")
    print "New geodatabase has been created in your FinalPrj folder"
    #Move shapefiles to geodatabase using variables created above
    ##FeatureClassToGeodatabase_conversion (Input_Features, Output_Geodatabase)
    arcpy.FeatureClassToGeodatabase_conversion([brown,clermont,hancock,muskingum,richland],"F:/GEO443/FinalPrj/Final.gdb")
    print "Shapefiles have been moved to the geodatabase"
    arcpy.env.workspace = "F:/GEO443/FinalPrj/Final.gdb"
    print "New workspace: " + arcpy.env.workspace
    ##MakeFeatureLayer_management(in_features,out_layer,{where_clause},{workspace},{field_info})
    ##SelectLayerByAttribute_management (in_layer_or_view,{selection_type},{where_clause})
    ##CopyFeatures_management (in_features,out_feature_class,{config_keyword},{spatial_grid_1},{spatial_grid_2},{spatial_grid_3})
    kroger_selection = ('"COMMENT" = \'KROGER\'')
    fcs = arcpy.ListFeatureClasses()
    for fc in fcs:
        print fc
        print ""
        fc_kroger = fc + "kroger"
        fc_lyr = arcpy.MakeFeatureLayer_management (fc,fc_kroger)
        arcpy.SelectLayerByAttribute_management(fc_lyr,"NEW_SELECTION",kroger_selection)
        arcpy.CopyFeatures_management(fc_lyr,fc + "krogerselection")
    print "Features Copied"
    input_features = arcpy.CopyFeatures_management(fc_lyr,fc + "krogerselection")
    ##Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field, {line_side}, {line_end_type}, {dissolve_option}, {dissolve_field})    
    #set input_features variable for buffer anlaysis

    arcpy.Buffer_analysis(input_features, "KrogerBuffer","1 Mile","","","ALL")
    print "buffer created"

except:
    print arcpy.GetMessages()

You have a few problems, first the input for the buffer tool is invalid, it needs to have a layer or fc as an input, you are setting it equal to a geoprocessing operation.  If you are trying to buffer each fc selection individually, you need to put the buffer inside you for-loop and make sure its input is either a feature class or feature layer( fc + "krogerselection" perhaps?).  Also, unless you get each output buffer a unique name, they will be overwritten each time through the loop since they will all have the same name.  If you want to do a single buffer of all fc selected krogers, you will need to have them all in one feature layer or feature class and you could do the buffer outside the loop.

You have made some good headway on this, keep up the good work.

EthanHeidtman
New Contributor III

After the script runs before without the buffer analysis it provides me

with the the county address points feature classss in my GDB and 5 new

feature classes that contain only the Kroger points from each. I.e.

BRO_ADDSkrogerselection

CLE_ADDSkrogerselection

Etc.

I then want to create a 1 mile buffer around each Kroger selection feature

class within the GDB...

This is where I am stuck.

Thank you all for the insight I truly appreciate it.

I could complete this task very easily without using Python. But it is for

a final project and the prof will not answer any questions

0 Kudos
DarrenWiens2
MVP Honored Contributor

The for loop runs once per feature class it encounters. If you want to do something once per feature class, put it inside the loop.

0 Kudos
IanMurray
Frequent Contributor

This might be a good opportunity for you to use model builder to set-up your workflow.

Set it up for one fc in model builder, then you can export the model to a python script, then use your list feature classes to loop through and use each fc as input.  This way you know your parameters are working for the buffer to start with(or at least can compare to what is going on now).

If not, you are very close, you just need to make sure for each buffer you are using the fc you made as the input for the buffer.  Since the new feature classes are already in your workspace, you would just need to use their name as a string as input for the buffer tool.  I would put the buffer in the loop and have it put different names depending on the input fc name.

EthanHeidtman
New Contributor III

would it be something like this....

  ##MakeFeatureLayer_management(in_features,out_layer,{where_clause},{workspace},{field_info})

    ##SelectLayerByAttribute_management (in_layer_or_view,{selection_type},{where_clause})

    ##CopyFeatures_management (in_features,out_feature_class,{config_keyword},{spatial_grid_1},{spatial_grid_2},{spatial_grid_3})

    kroger_selection = ('"COMMENT" = \'KROGER\'')

    fcs = arcpy.ListFeatureClasses()

    for fc in fcs:

        print fc

        print ""

        fc_kroger = fc + "kroger"

        fc_lyr = arcpy.MakeFeatureLayer_management (fc,fc_kroger)

        arcpy.SelectLayerByAttribute_management(fc_lyr,"NEW_SELECTION",kroger_selection)

        arcpy.CopyFeatures_management(fc_lyr,fc + "krogerselection")

    print "Features Copied"

   

    ##Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field, {line_side}, {line_end_type}, {dissolve_option}, {dissolve_field})   

    #set input_features variable for buffer anlaysis

   

    arcpy.env.workspace = "F:/GEO443/FinalPrj/Final.gdb"

    fcList = arcpy.ListFeatureClasses("*krogerselection")

    for fc in fcList:

        print fc

        arcpy.Buffer_analysis(fcList, fcList + "Buff", "1 Mile")

0 Kudos
DarrenWiens2
MVP Honored Contributor

fcList is a list, but fc is a feature class name. Buffer takes a feature class name, not a list.

If you're not sure what type of object something is, do something like:

print type(fcList)
print type(fc)
EthanHeidtman
New Contributor III

final_script_buffer.JPG

0 Kudos