4

Currently I am working on a GIS project where I have to update a field in a layer with some values to get my work done. Because the manual efforts are time consuming hence I added a custom addin to my project, but the addin is not updating the fields. Below is my code on the button class.

class C(object):
"""Implementation for Update_addin.btn5 (Button)"""
def __init__(self):
    C.enabled = False
    self.checked = False
def onClick(self):
    sel = ""
    mxd = arcpy.mapping.MapDocument("CURRENT")
    layers = arcpy.mapping.ListLayers(mxd)
    for layer in layers:
        desc = arcpy.Describe(layer)
        if desc.fidSet:
            sel = layer.name
            break

    if sel != "":
        if arcpy.Describe (sel).FIDSet:
            if selection == "Water Projects":         #for water projects.
                with arcpy.da.UpdateCursor (sel, "ACTION") as curs:
                    for row in curs:
                        row = ("C",)
                        curs.updateRow (row)

            else:          #for other projects.
                with arcpy.da.UpdateCursor (sel, "Remark") as curs:
                    for row in curs:
                        row = ("C",)
                        curs.updateRow (row)
        else:
            print("No selection")
        arcpy.RefreshActiveView()
    else:
        print("No selection")

After updating the "ACTION" field the selected feature should moves to another layer based on the query statement. I am getting below error in the console window.

Traceback (most recent call last):
File "C:\Users\ks38460\AppData\Local\ESRI\Desktop10.3\AssemblyCache\{4EE0D60C-BD1E-4348-8B27-CAF3B9D13FF6}\Update_addin.py", line 41, in onClick
desc = arcpy.Describe(layer)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\__init__.py", line 1246, in Describe
return gp.describe(value)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 374, in describe
self._gp.Describe(*gp_fixargs(args, True)))
RuntimeError: Object: Describe input value is not valid type

Could someone help me understand and rectify the problem in the above code.

I am using ArcMap 10.3 and Python 2.7.

9
  • 1
    mmm, your code looks fine to me. Maybe you can try with desc = arcpy.Describe(layer.dataSource) as suggested in gis.stackexchange.com/a/67049/9518.
    – umbe1987
    Sep 30 '19 at 10:50
  • @umbe1987 It says Traceback (most recent call last): File "C:\Users\ks38460\AppData\Local\ESRI\Desktop10.3\AssemblyCache\{4EE0D60C-BD1E-4348-8B27-CAF3B9D13FF6}\Update_addin.py", line 221, in onClick if desc.fidSet: AttributeError: DescribeData: Method fidSet does not exist. Sep 30 '19 at 11:41
  • 1
    Ok, the first problem then seems solved as you get the error on the next line. Anyway, it should be FIDSet not fidSet. Python is case sensitive. desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/…
    – umbe1987
    Sep 30 '19 at 11:56
  • @umbe1987 ohh....it still says method FIDSet does not exist. Sep 30 '19 at 12:58
  • Always in the same line (if desc.FIDSet:)?
    – umbe1987
    Sep 30 '19 at 13:05
3

Add a try/except block around the call to the desc.FIDSet property. It's likely that there are several layer types that do not support that method (group layer, raster layer, etc.). I'm not sure where the stdout is logged to for addins, but you seem to be getting a traceback so this should at least help you identify what layer is failing to report FIDSet.

class C(object):
    """Implementation for Update_addin.btn5 (Button)"""
    def __init__(self):
        C.enabled = False
        self.checked = False
    def onClick(self):
        sel = ""
        mxd = arcpy.mapping.MapDocument("CURRENT")
        layers = arcpy.mapping.ListLayers(mxd)
        for layer in layers:

            try:
                desc = arcpy.Describe(layer)
                if desc.FIDSet:
                    sel = layer.name
                    break
            except AttributeError as e:
                print("FIDSet failed on layer - {}".format(layer.name))

        if sel != "":
            if arcpy.Describe(sel).FIDSet:
                if selection == "Water Projects":         #for water projects.
                    with arcpy.da.UpdateCursor(sel, "ACTION") as curs:
                        for row in curs:
                            row = ("C",)
                            curs.updateRow(row)

                else:          #for other projects.
                    with arcpy.da.UpdateCursor (sel, "Remark") as curs:
                        for row in curs:
                            row = ("C",)
                            curs.updateRow (row)
            else:
                print("No selection")
            arcpy.RefreshActiveView()
        else:
            print("No selection")

Edit - Now, if you've identified the layer type that is causing the error you can account for it in an additional if statement - something like:

...
desc = arcpy.Describe(layer)
if desc.dataType == 'FeatureLayer':
    if desc.FIDSet: 
        sel = layer.name
        ... 
7
  • It says 'selection' is not defined. and 'FIDSet failed on layer - merged_landmarks'. I think the error is in the selection variable which I should write by referring to its class. Sep 30 '19 at 15:36
  • Its working now but still says that FIDSet failed on layer - merged_landmarks. Sep 30 '19 at 15:39
  • What type of layer is merged_landmarks?
    – smiller
    Sep 30 '19 at 19:30
  • 1
    See the edit to my answer. If you now know what layer type is causing the error, add an additional if statement to avoid calling .FIDSet on that layer type.
    – Ben Gosack
    Sep 30 '19 at 20:03
  • @smiller merged_landmarks its a group layer. Oct 1 '19 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.