AnsweredAssumed Answered

Python add-in help - restricting da.SearchCursor and da.UpdateCursor

Question asked by jayarem on Oct 8, 2013
Latest reply on Oct 8, 2013 by jayarem
I have a small add-in that I use for a building footprints map. Essentially, the add-in reads the data from the parcel layer, and copies the data to the buildings layer (I use this when I have to 'split' a building that lies over multiple parcels).

The code is set up to work with one (manually selected) parcel and one footprint, no more. However, if I were to click the add-in when no parcel or footprint was selected, it assigns the last record of the parcel data to every footprint! If I am in an edit session, it's OK, but I would like to let other people use this add-in.

I understand why I get this behavior (the search/update cursor are looping through every record if none is selected) Is there some pre-script logic I can add to make sure that only one record is updated?

Here is my code:

import arcpy
import pythonaddins
import webbrowser
import threading

mxd = arcpy.mapping.MapDocument("CURRENT")
layerList = arcpy.mapping.ListLayers(mxd)
tableList = arcpy.mapping.ListTableViews(mxd)
for layer in layerList:
    if "Footprints_Edit" in layer.name:
        footprints_fc = layer
    if "Parcels_Edit" in layer.name:
        parcels_fc = layer
    if "Address" in layer.name:
        address_fc = layer
for table in tableList:
    if "Assessing" in table.name:
        assessing_table = table
    if "Condos" in table.name:
        condo_table = table

def rows_as_update_dicts(cursor):
    colnames = cursor.fields
    for row in cursor:
        row_object = dict(zip(colnames, row))
        yield row_object
        cursor.updateRow([row_object[colname] for colname in colnames])

class assignparceldata(object):
    """Implementation for building_footprints_Addin_addin.assignParcelData (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        parcelfields = ["PIN","PADDRESS"]
        with arcpy.da.SearchCursor(parcels_fc,parcelfields) as rows:
            for row in rows:
                new_pin = row[0]
                new_paddress = row[1]
        print "Assigning parcel: PIN is %s, Address is %s" % (new_pin,new_paddress)
        footprint_fields = ["BUILDINGID", "TAXID","BADDRESS"]
        with arcpy.da.UpdateCursor(footprints_fc,footprint_fields) as brows:
            for row in rows_as_update_dicts(brows):
                row['TAXID'] = new_pin
                row['BADDRESS'] = new_paddress
                print "Updating building footprint %d with parcel info: PIN is (%s), address is (%s)" % (row['BUILDINGID'], new_pin, new_paddress)

Outcomes