Select layer by attributes empty and null

1723
10
Jump to solution
11-27-2018 02:11 PM
CCWeedcontrol
Regular Contributor

I have the following, i am trying to select and delete all the empty and null attributes but when i run the code the output feature class is empty. I am not sure what i am doing wrong, the SelectLayerByAttribute_management works in Arcmap python window.

# Process: Make Feature Layer
TAX1 = "in_memory\TaxPar"
arcpy.MakeFeatureLayer_management(Taxparcels, TAX1)

#Delets  Parcels that are blank, NULL or start with Q

query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"
arcpy.SelectLayerByAttribute_management(TAX1, "NEW_SELECTION", query)
if int(arcpy.GetCount_management(TAX1).getOutput(0)) > 0:
    arcpy.DeleteFeatures_management(TAX1)

arcpy.FeatureClassToFeatureClass_conversion(TAX1, "D:/GIS Folder/Blah/blah.gdb", "Blah2")
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

All feature layers are created in memory, you don't have to put "in_memory" in the name.  If anything, putting "in_memory" in the name will confuse geoprocessing functions about the type of object you are passing to it.

I would start by changing line 2 to:

TAX1 = "TaxPar"

View solution in original post

10 Replies
JoshuaBixby
MVP Esteemed Contributor

You are trying to create a feature layer whose name is "in_memory\TaxPar".  I strongly suggest not doing this.  What if you change the layer name to something like "TAX1"?

CCWeedcontrol
Regular Contributor

I am not sure what you mean by change the layer name to "TAX1", i was trying to create a feature layer In_memory to work with which is why i have line 3.

I was thinking my query syntx was wrong.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

All feature layers are created in memory, you don't have to put "in_memory" in the name.  If anything, putting "in_memory" in the name will confuse geoprocessing functions about the type of object you are passing to it.

I would start by changing line 2 to:

TAX1 = "TaxPar"

View solution in original post

CCWeedcontrol
Regular Contributor

ok, i see, thanks

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Or just leave out line 2 and the assignment of TAX1 all together

# 
arcpy.MakeFeatureLayer_management(Taxparcels, 'taxparclesLYR')

#Delets  Parcels that are blank, NULL or start with Q

query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"
arcpy.SelectLayerByAttribute_management('taxparclesLYR', "NEW_SELECTION", query)
...
...
...
can't wait to retire....
CCWeedcontrol
Regular Contributor

Ok, so after pulling my hair out with this issue. I noticed that it removed the blanks and nulls from the original dataset not the dataset in MakeFeautreLayer_management. Why is it working off the original dataset? Should i be checking to see if there are attributes with 'Q', ' ' & NULL first? If so how do check with arcpy.SelectLayerByAttribute_management?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Feature Layers and Table Views don't contain any actual data, they simply reference data.  If you create a feature layer and use it in geoprocessing tasks, the underlying data is being modified.  If you need to leave the original data set intact, I recommend copying the data set either into memory or a scratch GDB and then making the feature layer on it, clean it up, and then do whatever you need to next.

JoeBorgione
MVP Esteemed Contributor

I was typing this as Joshua's latest response came in:

In order to work on a feature class with arcpy, you need to work 'through' a feature layer.  When you open up arcmap and make edits, ArcMap is doing the behind the scenes work for you with respect to a feature layer.  If you run an arcpy script in arcmap, you need to have arcpy do that behind the scenes work.  Consider this: if you run an arcpy script as a scheduled task and that script interacts with a feature class, having that middle man of a feature layer is the only thing you can do.

If you are trying to weed out null values and/or blank values, I think it is good practice to take a close look at your data first with ArcMap queries to get a handle on what you are facing.

can't wait to retire....
CCWeedcontrol
Regular Contributor

I took your guys recommendations and came up with the following, not sure if this correct or the best way but it seem to work.

arcpy.CopyFeatures_management(Taxparcels, "in_memory\TaxPar")
arcpy.MakeFeatureLayer_management("in_memory\TaxPar", "TaxPar")

query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"

with arcpy.da.SearchCursor("TaxPar", "DXF_TEXT") as cursor:
    for row in cursor:
        if row[0] in (""," ",None,"Q%"):
            arcpy.SelectLayerByAttribute_management("TaxPar", "NEW_SELECTION", query)
            if int(arcpy.GetCount_management("TaxPar").getOutput(0)) > 0:
                arcpy.DeleteFeatures_management("TaxPar")
        else:
            pass
0 Kudos