Checking selection state on Featurelayer

894
5
05-13-2011 02:34 AM
ArkadiuszMatoszka
Occasional Contributor II
I need to write script that changes some attributes for selected features.
Is there any way to chceck if featurelayer represent selection? Cause now when I'm using layer without any selection it changes attribiutes for all features and that not exactly what I want. 😞
Tags (2)
0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
You can specify a query with the arcpy.UpdateCursor.  The update will only update records that are returned from this query.  Ex:

fc = "parcels"

rows = arcpy.UpdateCursor(fc, "Area1 = 10")
for row in rows:
    row.Area2 = 200
    rows.updateRow(row)
0 Kudos
ArkadiuszMatoszka
Occasional Contributor II
The idea is, that user makes custom selection, not necessary by atributes or location, he may as well do it by simply clicking feature, so it doesn't solve the problem. Below is my code, maybe it helps somehow 🙂

DICT_VALUES = {'Tak': 'T',
               'Nie': 'F',
               'Bez zmian': None}

def updateDb(cur, field, value, teryts):
    update_string = "Update PLOTS set " + str(field) + " = '" + str(value) + "' where TERYT = :teryt"
    cur.prepare(update_string)
    for teryt in teryts:
        arcpy.AddMessage('Setting ' + field + ' to ' + value + ' for plot ' + teryt)
        cur.execute(None, {'teryt': str(teryt)})
    con.commit()

import arcpy
import sys
try:
    import cx_Oracle as cxo
except ImportError:
    arcpy.AddError('cx_Oracle module not found. Add module to sys.path or install module if necessary')
    sys.exit()

plots = sys.argv[1]
arcpy.AddMessage('Processing selection')
rows = arcpy.SearchCursor(plots)
teryt_nr = []
for plot in rows:
    if plot.TERYT:
        teryt_nr.append(plot.TERYT)
arcpy.AddMessage('Connecting to DB...')
con = cxo.connect('user/pass@db')
cur = con.cursor()
#
## POI
if DICT_VALUES[sys.argv[2]]:
    updateDb(cur, 'POI', DICT_VALUES[sys.argv[2]], teryt_nr)


What I need is empty list "teryt_nr" in case where there is no selection on layer in arcmap 🙂
0 Kudos
RDHarles
Occasional Contributor
Use arcpy.GetCount_management() to check if the layer has a selection.
It will report back how many records are selected (if any).
0 Kudos
KimOllivier
Occasional Contributor III
Run a gp.Describe(layer) and you can get out the Layer properties FIDSet. This is a clumsy string of all the FIDs that are in the selection. But it enables you to get a list of which features are selected.
0 Kudos
ArkadiuszMatoszka
Occasional Contributor II
Run a gp.Describe(layer) and you can get out the Layer properties FIDSet. This is a clumsy string of all the FIDs that are in the selection. But it enables you to get a list of which features are selected.


Thanks, that solves my problem 🙂
0 Kudos