Question: SelectLayerByAttributeManagement

169 views
Skip to first unread message

Lew.Gr...@gov.bc.ca

unread,
Aug 27, 2013, 6:13:09 PM8/27/13
to geop...@googlegroups.com
Hi Pythoneers,
 
I am trying to select attributes from a layer that has 1639 records.  In this case I am selecting 'FIRESTATUS <> \'Out\'')
 
        arcpy.SelectLayerByAttribute_management(layerName, "NEW_SELECTION", 'FIRESTATUS <> \'Out\'')       
        result = int(arcpy.GetCount_management(layerName).getOutput(0))
 
 
 
It should return 345 records but it returns all of them (1639 records).
 
When I try a different selection, say   'FIRESTATUS <> \'Mop-up\'' , it again return 1639 records instead of a subset of records.
 
All the SQL statements used run without error messages.
 
Any thoughts on what I am overlooking are appreciated please.
 
Thanks
Lew
 
 

Steinke, Candice FLNR:EX

unread,
Aug 27, 2013, 6:19:24 PM8/27/13
to geop...@googlegroups.com

Hi Lew,

In my experience it works best to save the expression as a variable prior to your select by statement and then call the variable instead of having the expression in the select statement......

 

selFire = “FIRESTATUS <> ‘Out’”

arcpy.SelectLayerByAttribute_management(layerName, “NEW_SELECTION”,selFire)

 

Not sure if that will solve your issue or not?

 

Candice Steinke
GIS/Data Analyst
BC Timber Sales - Kamloops TSO
1265 Dalhousie Drive
Kamloops, BC V2C 5Z5
Phone: 250-371-4450
Fax: 250-371-6565

--
You received this message because you are subscribed to the Google Groups "Python - ARCGIS geoprocessing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geopython+...@googlegroups.com.
To post to this group, send email to geop...@googlegroups.com.
Visit this group at http://groups.google.com/group/geopython.
For more options, visit https://groups.google.com/groups/opt_out.

Greentree, Lew FLNR:EX

unread,
Aug 27, 2013, 6:27:51 PM8/27/13
to geop...@googlegroups.com

Hi Pythoneers,

 

The solution to this was to create a Feature Layer first.  I

 

 

def selection():

        if arcpy.Exists(layerName):

            print "Deleting outLayer"

            arcpy.DeleteFeatures_management(layerName)

   

        arcpy.MakeFeatureLayer_management(tmp_layername,layerName)

        arcpy.SelectLayerByAttribute_management(layerName, "NEW_SELECTION", 'FIRESTATUS <> \'Out\'')       

        result = int(arcpy.GetCount_management(layerName).getOutput(0))

        print "+++++++++++++++++++++++++++++++++++++++++++"

        print layerName

        print result       

        print "+++++++++++++++++++++++++++++++++++++++++++"

 

 

... Result = 345

 

Cheers

Lew

--
You received this message because you are subscribed to a topic in the Google Groups "Python - ARCGIS geoprocessing" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/geopython/J114eO47jy8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to geopython+...@googlegroups.com.

Mike Neal

unread,
Aug 28, 2013, 4:09:12 PM8/28/13
to geop...@googlegroups.com
Just a note about your code:  To delete a layer, rather than the features IN the layer, you need to get a handle first, then delete the handle:
 
myLayer = arcpy.MakeFeatureLayer_management(tmp_layername,layerName)
arcpy.Delete_management(myLayer)
 
This is how you can release layer locks too, if you want to delete an FGDB you were working on or something higher level like that in a script.
 
Cheers,
Mike

Steinke, Candice FLNR:EX

unread,
Nov 6, 2013, 1:57:29 PM11/6/13
to geop...@googlegroups.com

Hi Mike,

I am working on a script right now where I’m trying to delete a FGDB at the end of my process, but I keep running into lock issues.

Can you explain how that would work?  Do I have to use Delete_management to first delete all of the feature classes and feature layers associated with the FGDB before it will unlock and allow me to delete it?

 

Candice Steinke
GIS/Data Analyst
BC Timber Sales - Kamloops TSO
1265 Dalhousie Drive
Kamloops, BC V2C 5Z5
Phone: 250-371-4450
Fax: 250-371-6565

Greentree, Lew FLNR:EX

unread,
Nov 6, 2013, 2:10:16 PM11/6/13
to geop...@googlegroups.com, Steinke, Candice FLNR:EX

Hi Candice,

 

A solution is offered by one of the comments in this thread.

 

http://forums.arcgis.com/threads/7904-Can-t-delete-a-file-GDB-in-Python-Script-Tool

 

Also, as it appears that you are creating a temporary FGDB, could you not just create it on the E drive and have it automatically deleted at reboot or log-off?

Mike Neal

unread,
Nov 6, 2013, 4:35:42 PM11/6/13
to geop...@googlegroups.com
Hi Candice,
 
You won't have to delete each item in the FGDB before deleting the whole thing IF you clean up all the processes you run along the way.  So any time you open a cursor, the cursor needs to be deleted.  Any time you create a layer (feature or table), you need to delete the layer as soon as you are done with it.  And you can't have ArcMap, ArcCatalog, or any of the other tools (Server, ArcScene, etc. can all be culprits) open and looking at that data either.  Depending on workflow it can be easier to simply designate a temp directory, create your FGDB using a random number or a uuid or something, and just clean that out from time to time when nothing is running.  Unfortunately this concurrency stuff can get tricky if you have complicated workflows.
 
Good Luck!
Mike

Steinke, Candice FLNR:EX

unread,
Nov 6, 2013, 5:40:46 PM11/6/13
to geop...@googlegroups.com

Thanks Mike.  I generally try to clean up as I go but I’ve probably missed a few things.......this one seems to only intermittently give me grief so it’s frustrating because one time it works and the next it doesn’t!

Mike Neal

unread,
Nov 8, 2013, 12:36:15 PM11/8/13
to geop...@googlegroups.com
I hear you!
 
Try something like this, it allows the system to catch up, especially with a network share:
 
import time
 
while arcpy.Exists_management(FGDB):
    try:
        arcpy.Delete_management(FGDB)
    except:   
        time.sleep(10)
 
Reply all
Reply to author
Forward
0 new messages