Select to view content in your preferred language

Select by Attribute in ArcPy

2822
4
Jump to solution
01-14-2023 12:51 AM
FynnScharpen
Regular Contributor

 

import arcpy
def Vorverarbeitung(klasse1, klasse2, klasse3, klasse4, klasse5, ffh, stationen, ordner):
    
    #Import Messstationen
    arcpy.conversion.ExcelToTable(stationen,
                                  "messstationen")
    #Puffer der FFH Gebiete (5km)
    arcpy.analysis.Buffer(ffh,
                          "puffer_ffh",
                          "5 Kilometers",
                          "FULL")
    #Klassierung der Klasse1 (Siedlung)
    klasse1_fl = arcpy.management.MakeFeatureLayer(klasse1, "klasse1_fl")
    arcpy.management.SelectLayerByAttribute(klasse1_fl,
                                            "NEW_SELECTION",
                                            "'CLC15' = '111'")
    arcpy.management.CopyFeatures(klasse1, "siedlung")
    #Klassierung der Klasse1 (Industrie)
    arcpy.management.SelectLayerByAttribute(klasse1_fl,
                                            "NEW_SELECTION",
                                            "'CLC15' = '121' OR 'CLC15' = '131' OR 'CLC15' = '132' OR 'CLC15' = '133'")
    arcpy.management.CopyFeatures(klasse1, "industrie")
    
    #Klassierung der Klasse1 (Verkehr)
    arcpy.management.SelectLayerByAttribute(klasse1_fl,
                                            "NEW_SELECTION",
                                            "'CLC15' = '122' OR 'CLC15' = '123' OR 'CLC15' = '124'")
    arcpy.management.CopyFeatures(klasse1, "verkehr")
    
    return
# This is used to execute code if the file was run but not imported
if __name__ == '__main__':
    
    # Tool parameter accessed with GetParameter or GetParameterAsText
    klasse1 = arcpy.GetParameter(0)
    klasse2 = arcpy.GetParameter(1)
    klasse3= arcpy.GetParameter(2)
    klasse4 = arcpy.GetParameter(3)
    klasse5 = arcpy.GetParameter(4)
    ffh = arcpy.GetParameter(5)
    stationen = arcpy.GetParameter(6)
    ordner = arcpy.GetParameter(7)
    
    Vorverarbeitung(klasse1, klasse2, klasse3, klasse4, klasse5, ffh, stationen, ordner)
    
    # Update derived parameter values using arcpy.SetParameter() or arcpy.SetParameterAsText()

 

Hello there...today I struggle with the "Select by Attribute Tool" using ArcPy (Screenshot).

I tried my SQL Expression manually and it worked out fine but in ArcPy the result is always the complete dataset. I have also tried "Feature Class to Feature Class" but my selection is again not considered. I am still a beginner in the Arcpy environment so the error may well be extremely stupid.

I hope one of you can help me out on this rainy Saturday.
Thanks in advance, Fynn

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

There's a difference between a feature class and a layer.

Very basically:

  • the feature class is the actual data, the physical object being saved on your disc
  • the feature layer is a representation of that data. Things like coordinate transformation, symbology, labeling, popups are done on the layer, not on the data. If you symbolize your layer, the underlying feature class doesn't know anything about that.

Importantly, one other thing that only works on the layer, not on the feature class, is selection.

You start out doing the right thing: Create a layer (klasse1_f1) of the data and do the selection on that layer.

But then you copy the feature class (klasse1). The feature class has no selection, because you selected on your layer and there is no feedback to the feature class.

 

So this should work as intended:

arcpy.management.CopyFeatures(klasse1_f1, "siedlung")

 

 

As a side note: you don't actually need to do the selection. You could just use your queries in the MakeFeatureLayer tool. This sets the definition query of the resulting layer. So you could do this:

    #Klassierung der Klasse1 (Siedlung)
    klasse1_siedlung = arcpy.management.MakeFeatureLayer(klasse1, "klasse1_siedlung", "'CLC15' = '111'")
    arcpy.management.CopyFeatures(klasse1_siedlung , "siedlung")
    #Klassierung der Klasse1 (Industrie)
    klasse1_industrie = arcpy.management.MakeFeatureLayer(klasse1, "klasse1_industrie ", "'CLC15' = '121' OR 'CLC15' = '131' OR 'CLC15' = '132' OR 'CLC15' = '133'")
    # you could also write this as
    # CLC15 IN ('121', '131', '132', '133')
    arcpy.management.CopyFeatures(klasse1_industrie , "industrie")

Have a great day!
Johannes

View solution in original post

4 Replies
Luke_Pinner
MVP Regular Contributor

Screenshots of code are hard to read - How to insert code in your post 

FynnScharpen
Regular Contributor

Maybe someone is able to help now 🙂 Thanks for the tip! Didn't know it was possible

0 Kudos
JohannesLindner
MVP Frequent Contributor

There's a difference between a feature class and a layer.

Very basically:

  • the feature class is the actual data, the physical object being saved on your disc
  • the feature layer is a representation of that data. Things like coordinate transformation, symbology, labeling, popups are done on the layer, not on the data. If you symbolize your layer, the underlying feature class doesn't know anything about that.

Importantly, one other thing that only works on the layer, not on the feature class, is selection.

You start out doing the right thing: Create a layer (klasse1_f1) of the data and do the selection on that layer.

But then you copy the feature class (klasse1). The feature class has no selection, because you selected on your layer and there is no feedback to the feature class.

 

So this should work as intended:

arcpy.management.CopyFeatures(klasse1_f1, "siedlung")

 

 

As a side note: you don't actually need to do the selection. You could just use your queries in the MakeFeatureLayer tool. This sets the definition query of the resulting layer. So you could do this:

    #Klassierung der Klasse1 (Siedlung)
    klasse1_siedlung = arcpy.management.MakeFeatureLayer(klasse1, "klasse1_siedlung", "'CLC15' = '111'")
    arcpy.management.CopyFeatures(klasse1_siedlung , "siedlung")
    #Klassierung der Klasse1 (Industrie)
    klasse1_industrie = arcpy.management.MakeFeatureLayer(klasse1, "klasse1_industrie ", "'CLC15' = '121' OR 'CLC15' = '131' OR 'CLC15' = '132' OR 'CLC15' = '133'")
    # you could also write this as
    # CLC15 IN ('121', '131', '132', '133')
    arcpy.management.CopyFeatures(klasse1_industrie , "industrie")

Have a great day!
Johannes
FynnScharpen
Regular Contributor

i would like to call myself stupid. sometimes you can't see the forest for the trees. thanks for your help. kind regards fynn 😌

0 Kudos