Python Script: loop through selected features and and copy to geodatabase

4353
14
04-13-2014 12:35 PM
DavidBailey
New Contributor II
I am working with surface types that I derived from LIDAR data. These surface types are in one feature class (polygon) -
associated with the feature class is a column called "grid_code"; the grid_code column has values from 0 to 145. These are elevation values and will be selected out to classify three surface types (ground surface, shrubs and trees).

I want to write a script that selects each surface type and creates a feature class for each of them. My output would have three
feature classes: ground, shrub and tree.

Here is the code I am working with but I am getting errors:

Thanks!



import arcpy, os

aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"

arcpy.env.workspace = aWS

arcpy.env.overwriteOutput = True

try:
  
    # Create query statements for each land cover type
    LIDAR_tree = '"grid_code" > \'3\''
    LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''
    LIDAR_ground = '"grid_code" <= \'1\''

    aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]
   
    for item in aListLIDAR:
       
        arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", P303_layerL)

        arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", aListLIDAR)
       
        arcpy.CopyFeatures_management(P303_layerL, "P303_" + str(item))

    del aListLIDAR
    del item
    del P303_layerL

   
except:
   
    # Code to run when an error occured
    print "An ERROR Occured!"
    print "\n" + arcpy.GetMessages()

else:
   
    # Message when there was no error
    print "\nNo Error occurred"
   
# Script end message
print "\nEnd of the script!"
Tags (2)
0 Kudos
14 Replies
DavidBailey
New Contributor II
This is what my code looks like now (It works - but my outputs still have the same attributes as the P303_LIDAR_Poly):

Any ideas?

# Developed by: David Bailey
# Description: Providence Creek Watershed: land cover processing


# import arcpy site-package and os module
import arcpy, os

# Set geoprocessing environment:
# a) set the workspace environment 
aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"
arcpy.env.workspace = aWS

# Set geoprocessing environment:
# b) the overwriteOutput parameter controls whether tools will
#    automatically overwrite any existing output 
arcpy.env.overwriteOutput = True

try:
   
    # Create query statements for each land cover type
    LIDAR_tree = '"grid_code" > \'3\''
    LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''
    LIDAR_ground = '"grid_code" <= \'1\''

    aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]
    layerName = ["tree","shrub","ground"]

    n = 0
    for query in aListLIDAR:

        arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer", query)
        arcpy.CopyFeatures_management("temp_layer", "P303_{0}_layer".format(layerName))
    arcpy.Delete_management("temp_layer")
    n += 1

    
except:
    
    # Code to run when an error occured
    print "An ERROR Occured!"
    print "\n" + arcpy.GetMessages()

else:
    
    # Message when there was no error
    print "\nNo Error occurred"
    
# Script end message
print "\nEnd of the script!" 
0 Kudos
MattEiben
Occasional Contributor
Hmmmm,

It sounds like the query isn't working when the feature layer is being made.  To figure out what's going on, my suggestion would be to make your feature layer and then run a select by attribute.  I put a code snippet below.  Try replace your for loop with that code.  I also added in an arcpy.AddMessage that should echo out the Query you're using for each layer.  This isn't really required, just included that for error checking.

    n = 0
    for query in aListLIDAR:

        arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer")

        # You can use "print" instead of "arcpy.AddMessage" if you're directly in the console.
        arcpy.AddMessage("Query For: {0}: {1}".format(layerName,query))
        arcpy.SelectLayerByAttribute_management ("temp_layer", "NEW_SELECTION",query)

        arcpy.CopyFeatures_management("temp_layer", "P303_{0}_layer".format(layerName))
        arcpy.Delete_management("temp_layer")
    n += 1


Are there three layers being made with the names
P303_tree_layer,
P303_shrub_layer,
P303_ground_layer
currently?

Hope this helps!
0 Kudos
DavidBailey
New Contributor II
Yes, with my current script I am getting three different layers with the names: P303_tree_layer, P303_shrub_layer, P303_ground_layer - however they are all the same. They should be different according to the query that was created.

I tried the code you gave me and it came back with this error:

Executing: SelectLayerByAttribute temp_layer NEW_SELECTION ""grid_code" > '3'"
Start Time: Sat Apr 19 15:13:12 2014
ERROR 000358: Invalid expression
An invalid SQL statement was used.
An invalid SQL statement was used. [P303_LIDAR_Poly]
Failed to execute (SelectLayerByAttribute).
Failed at Sat Apr 19 15:13:12 2014 (Elapsed Time: 0.00 seconds)

End of the script!




It appears that my SQL statements are not written correctly? I don't see where
0 Kudos
MattEiben
Occasional Contributor
Make sure to null out the quotes inside your expression using backslashes like this on all your SQL queries:

"\"grid_code\" > '3'"


If you don't do that, Python will see those other quotes as symbols to close and open your string instead of as literal parts of your string.
0 Kudos
DavidBailey
New Contributor II
I did this and it still gives me this error:

An ERROR Occured!

Executing: SelectLayerByAttribute temp_layer NEW_SELECTION ""grid_code" > '3'"
Start Time: Tue Apr 22 23:29:17 2014
ERROR 000358: Invalid expression
An invalid SQL statement was used.
An invalid SQL statement was used. [P303_LIDAR_Poly]
Failed to execute (SelectLayerByAttribute).
Failed at Tue Apr 22 23:29:17 2014 (Elapsed Time: 0.00 seconds)

End of the script!
0 Kudos