Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

cursor dictionary help please

$
0
0
I need some help with arcpy.da.SearchCursor and dictionary.
I currently have the old Search.Cursor here
Code:

# init rowW and rowR
        curR = arcpy.SearchCursor(fcOutput)
        join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])
        del curR

I would like to use the new arcpy.da.SearchCursor with a dictionary.
I am trying to put it together with the following,
Code:

# init rowW and rowR
        with arcpy.da.SearchCursor(fcOutput,fields) as cursor:
            for row in cursor:
                cursor = dict([(r.JOIN_FID,[r.getValue(f) for f in arcpy.da.SearchCursor (fcTarget,add_fields)])
        del cursor

But no luck i would really appreciate some help please.
my fully code,
Code:

import arcpy
import pythonaddins
import os
import time
from arcpy import env

class Timer:   
    def __enter__(self):
        self.start = time.clock()
        return self

    def __exit__(self, *args):
        self.end = time.clock()
        self.interval = self.end - self.start

class Add_points(object):
    """Implementation for AddPoints_addin.Add_points (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
    def onMouseDownMap(self, x, y, button, shift):

        fc = "TonyTwoWay.DBO.TT"
        workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to dsd15_sqlexpress.sde"
        arcpy.env.overwriteOutput = True
        #arcpy.ChangeVersion_management('TonyTwoWay.DBO.TT','TRANSACTIONAL','dbo.DEFAULT', "")
        # Start an edit session. Must provide the worksapce.
        edit = arcpy.da.Editor(workspace)

        # Edit session is started without an undo/redo stack for versioned data
        #  (for second argument, use False for unversioned data)
        edit.startEditing(True)

        # Start an edit operation
        edit.startOperation()

        CC_list = []
        with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor:
            for row in cursor:
                try:
                    if "CC" in row[0]:
                        CC_list.append(int(row[0].strip("CC"))) 
                except TypeError:
                    pass       
        del cursor

        t = Timer()
        with t:
            CC_list.sort()
            AddressID = CC_list[-1] + 1
            AddressID = 'CC' + str(AddressID)

            row_values = [(x, y, (x, y), AddressID)]
            cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"])

            for row in row_values:
                cursor.insertRow(row)
            del cursor

            # Stop the edit operation.
            edit.stopOperation()

            # Stop the edit session and save the changes
            edit.stopEditing(True)
           
        timetest = "completed in %.02f secs." % (t.interval)
        print timetest
#####################################################
        fcTarget = "TonyTwoWay.DBO.TT"
        fcJoin = "testParcelsAdmit"
        add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","predir","StreetType","SubName"]

        # fix args
        if not isinstance(add_fields, list):
            # from script tool
            add_fields = add_fields.split(';')

        # do not need this scratch file
        fcOutput = r'in_memory\temp_join'
        arcpy.SpatialJoin_analysis(fcJoin, fcTarget,fcOutput, 'JOIN_ONE_TO_MANY')


        # grab oid field from points
        oid_t = arcpy.Describe(fcTarget).OIDFieldName

        fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","predir","StreetType","SubName"]
        # init rowW and rowR
        with arcpy.da.SearchCursor(fcOutput,fields) as cursor:
            for row in cursor:
                cursor = dict([(r.JOIN_FID,[r.getValue(f) for f in arcpy.da.SearchCursor (fcTarget,add_fields)])
        del cursor

        # Now update the new target
        curW = arcpy.UpdateCursor(fcTarget)
        for row in curW:
            t_oid = row.getValue(oid_t)
            if t_oid in join_dict:
                for f in add_fields:
                    row.setValue(f, join_dict[t_oid][add_fields.index(f)])
            curW.updateRow(row)
        del row, curW
        arcpy.AddMessage('Updated all records sucussefully')
           
        pass


Viewing all articles
Browse latest Browse all 2485

Trending Articles