insertrow with dictionary and list

3984
15
08-13-2015 02:01 PM
CCWeedcontrol
Occasional Contributor III

i have a code that has two issues 1. only one point gets updated with the parcel attributes no other points get updated and 2. the  insertRow i currently have does not work it give me .

line 63, in <module>

    insCursor.insertRow(row)

TypeError: sequence size must match size of the row

basically i am trying to create multiple points that spatially joins the new created points to parcels and updates the points attributes based on what parcel the points are created i am not sure why i am getting the following error.

line 63, in <module>

    insCursor.insertRow(row)

TypeError: sequence size must match size of the row

I have checked to see if the "point" is being updated by the updatecursor with arcpy.FeatureClassToFeatureClass_conversion(point, "C:\Temp\Default.gdb", "p_lyr")  after updateRows.updateRow(updateRow)  and it looks like only one point gets updated.
Separate question, Can i use the SpatialJoin to "In_memory" instead of to a gdb?
I would really appreciate some help please.
working with.

#import modules

import arcpy

# Allow overwrite

arcpy.env.overwriteOutput = True

arcpy.env.qualifiedFieldNames = False

# Script user input parameters

pointFC = "CCA

parcel = "Parcels1" #Taxparcels

arcpy.env.workspace = r"C:\Temp\Default.gdb"

parcel_lyr = 'parcel_lyr'

sjpoints = "C:\Temp\Default.gdb\sjpoints"

poly = "ACCOUNT_1"

Pnt =  "Account"

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

dfsr = df.spatialReference

fcsr = arcpy.Describe(pointFC).spatialReference

if dfsr.name == fcsr.name:

    """Now do your work"""

point = arcpy.GetParameterAsText(0)  #click

for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'): 

        x,y = prow[0]

del prow

point1 = arcpy.Point(x, y) 

ptGeometry = arcpy.PointGeometry(point1)

insCursor = arcpy.da.InsertCursor(pointFC,'SHAPE@XY') # create insert cursor

arcpy.MakeFeatureLayer_management(parcel,parcel_lyr) 

arcpy.SelectLayerByLocation_management(parcel_lyr,"INTERSECT",point)

#Run the Spatial Join tool, using the defaults for the join operation and join type

arcpy.SpatialJoin_analysis(point, parcel_lyr , sjpoints)

#arcpy.FeatureClassToFeatureClass_conversion(parcel_lyr, "C:\Temp\Default.gdb", "parcel_lyr")

# define the field list from the spatial join

sourceFieldsList = ["TARGET_FID", poly,"SiteAddress",'SiteNum_1', 'SiteStreet_1','SiteStreet_1','SiteNumSfx_1','Predir_1','Postdir_1', 'SiteCity_1', 'SiteZIP_1', 'OwnerName_1']

# define the field list to the original points

updateFieldsList = ["OID@", Pnt,"SiteAddres",'SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName']

# populate the dictionary from the polygon

valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}

with arcpy.da.UpdateCursor(point, updateFieldsList) as updateRows:

    for updateRow in updateRows:

        keyValue = updateRow[0]

        if keyValue in valueDict:

            for n in range (1,len(sourceFieldsList)):

                updateRow = valueDict[keyValue][n-1]

            updateRows.updateRow(updateRow)

            #arcpy.FeatureClassToFeatureClass_conversion(point, "C:\Temp\Default.gdb", "p_lyr")

            with arcpy.da.SearchCursor(point,('Account','SiteAddres','SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName')) as cursor: # loop through feature set

                for row in cursor:

                    row = []

                    insCursor.insertRow(row)

arcpy.RefreshActiveView()

0 Kudos
15 Replies
DanPatterson_Retired
MVP Emeritus

could you format your code please, so that people can refer to line numbers...

Posting Code blocks in the new GeoNet

0 Kudos
CCWeedcontrol
Occasional Contributor III

For some weird reason it lets me format the code to python when i first posted but noticed that it was just plain text after i posted, i tried to go edit my first post and it seems like it will let me format the text to python but when i hit post it goes back to plan text.

#import modules

import arcpy

# Allow overwrite

arcpy.env.overwriteOutput = True

arcpy.env.qualifiedFieldNames = False

# Script user input parameters

pointFC = "CCA

parcel = "Parcels1" #Taxparcels

arcpy.env.workspace = r"C:\Temp\Default.gdb"

parcel_lyr = 'parcel_lyr'

sjpoints = "C:\Temp\Default.gdb\sjpoints"

poly = "ACCOUNT_1"

Pnt =  "Account"

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

dfsr = df.spatialReference

fcsr = arcpy.Describe(pointFC).spatialReference

if dfsr.name == fcsr.name:

    """Now do your work"""

point = arcpy.GetParameterAsText(0)  #click

for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):

        x,y = prow[0]

del prow

point1 = arcpy.Point(x, y)

ptGeometry = arcpy.PointGeometry(point1)

insCursor = arcpy.da.InsertCursor(pointFC,'SHAPE@XY') # create insert cursor

arcpy.MakeFeatureLayer_management(parcel,parcel_lyr)

arcpy.SelectLayerByLocation_management(parcel_lyr,"INTERSECT",point)

#Run the Spatial Join tool, using the defaults for the join operation and join type

arcpy.SpatialJoin_analysis(point, parcel_lyr , sjpoints)

#arcpy.FeatureClassToFeatureClass_conversion(parcel_lyr, "C:\Temp\Default.gdb", "parcel_lyr")

# define the field list from the spatial join

sourceFieldsList = ["TARGET_FID", poly,"SiteAddress",'SiteNum_1', 'SiteStreet_1','SiteStreet_1','SiteNumSfx_1','Predir_1','Postdir_1', 'SiteCity_1', 'SiteZIP_1', 'OwnerName_1']

# define the field list to the original points

updateFieldsList = ["OID@", Pnt,"SiteAddres",'SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName']

# populate the dictionary from the polygon

valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}

with arcpy.da.UpdateCursor(point, updateFieldsList) as updateRows:

    for updateRow in updateRows:

        keyValue = updateRow[0]

        if keyValue in valueDict:

            for n in range (1,len(sourceFieldsList)):

                updateRow = valueDict[keyValue][n-1]

            updateRows.updateRow(updateRow)

            #arcpy.FeatureClassToFeatureClass_conversion(point, "C:\Temp\Default.gdb", "p_lyr")

            with arcpy.da.SearchCursor(point,('Account','SiteAddres','SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName')) as cursor: # loop through feature set

                for row in cursor:

                    row = []

                    insCursor.insertRow(row)

arcpy.RefreshActiveView()

edited by Xander Bakker​, formated code

0 Kudos
XanderBakker
Esri Esteemed Contributor

Odd... tried to edit the post by the OP, shows formatted code, but once saved it turn normal italic text...

#import modules
import arcpy
# Allow overwrite
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False
# Script user input parameters
pointFC = "CCA
parcel = "Parcels1" #Taxparcels
arcpy.env.workspace = r"C:\Temp\Default.gdb"
parcel_lyr = 'parcel_lyr'
sjpoints = "C:\Temp\Default.gdb\sjpoints"
poly = "ACCOUNT_1"
Pnt =  "Account"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
dfsr = df.spatialReference
fcsr = arcpy.Describe(pointFC).spatialReference
if dfsr.name == fcsr.name:
    """Now do your work"""
point = arcpy.GetParameterAsText(0)  #click
for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):
        x,y = prow[0]
del prow
point1 = arcpy.Point(x, y)
ptGeometry = arcpy.PointGeometry(point1)
insCursor = arcpy.da.InsertCursor(pointFC,'SHAPE@XY') # create insert cursor
arcpy.MakeFeatureLayer_management(parcel,parcel_lyr)
arcpy.SelectLayerByLocation_management(parcel_lyr,"INTERSECT",point)
#Run the Spatial Join tool, using the defaults for the join operation and join type
arcpy.SpatialJoin_analysis(point, parcel_lyr , sjpoints)
#arcpy.FeatureClassToFeatureClass_conversion(parcel_lyr, "C:\Temp\Default.gdb", "parcel_lyr")
# define the field list from the spatial join
sourceFieldsList = ["TARGET_FID", poly,"SiteAddress",'SiteNum_1', 'SiteStreet_1','SiteStreet_1','SiteNumSfx_1','Predir_1','Postdir_1', 'SiteCity_1', 'SiteZIP_1', 'OwnerName_1']
# define the field list to the original points
updateFieldsList = ["OID@", Pnt,"SiteAddres",'SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName']
# populate the dictionary from the polygon
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}
with arcpy.da.UpdateCursor(point, updateFieldsList) as updateRows:
    for updateRow in updateRows:
        keyValue = updateRow[0]
        if keyValue in valueDict:
            for n in range (1,len(sourceFieldsList)):
                updateRow = valueDict[keyValue][n-1]
            updateRows.updateRow(updateRow)
            #arcpy.FeatureClassToFeatureClass_conversion(point, "C:\Temp\Default.gdb", "p_lyr")
            with arcpy.da.SearchCursor(point,('Account','SiteAddres','SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName')) as cursor: # loop through feature set
                for row in cursor:
                    row = []
                    insCursor.insertRow(row)
arcpy.RefreshActiveView()

so... posted the code here and applied the formatting. I do wonder if lines 18 and 19 are correct? Could you confirm this please?

0 Kudos
CCWeedcontrol
Occasional Contributor III

I am making sure that i am in the same spatial reference with the following.

mxd = arcpy.mapping.MapDocument("CURRENT")   
df = arcpy.mapping.ListDataFrames(mxd)[0]
dfsr = df.spatialReference   
fcsr = arcpy.Describe(pointFC).spatialReference   
if dfsr.name == fcsr.name:
0 Kudos
XanderBakker
Esri Esteemed Contributor

Yes, that's the point, then the code starting from line 20 should be indented...

0 Kudos
DanPatterson_Retired
MVP Emeritus

As Xander Bakker says...your logic is flawed and the code needs to be indented...

>>> if "a" == "b":
...     print("Now do your work")
... else:
...     print("it just carries on anyway")
... 
Now do your work
>>> if "a" == "b":
...     print("Now do your work")
... else:
...     print("it just carries on anyway")
...
it just carries on anyway
>>>

not the subtle but oh so important difference in logic

BlakeTerhune
MVP Regular Contributor

This looks like a problem to me.

with arcpy.da.SearchCursor(point,('Account','SiteAddres','SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName')) as cursor:
    for row in cursor:
        row = []
        insCursor.insertRow(row)

You're trying to insert an empty row. I think you need the same number of values in the row list as there are fields in the cursor. If you're trying to just insert a blank row, fill the values with nulls (or defaults if nulls not allowed). Maybe something like this?

row = [
    None,  ## Account
    None,  ## SiteAddres
    None,  ## SiteNum
    None,  ## StreetName
    None,  ## SiteStreet
    None,  ## SiteNumSfx
    None,  ## Predir
    None,  ## Postdir
    None,  ## SiteCity
    None,  ## SiteZip
    None  ## OwnerName
]
PacoAlverez
New Contributor III

I have tried the following and both give me errors.

 with arcpy.da.SearchCursor(point,('Account','SiteAddres','SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName')) as cursor: # 'POINT_X', 'POINT_Y','SHAPE@XY'# loop through feature set  
                    for row in cursor:
                        row =[
                            None, #Account
                            None,  #SiteAddres
                            None, #SiteNum
                            None, #StreetName
                            None, #SiteStreet
                            None, #SiteNumSfx
                            None, #Predir
                            None, #Postdir
                            None, #SiteCity
                            None, #SiteZip
                            None,#OwnerName
                            ]
                        insCursor.insertRow(row)

error

insCursor.insertRow(row)

TypeError: sequence size must match size of the row

AND

with arcpy.da.SearchCursor(point,('Account','SiteAddres','SiteNum','StreetName', 'SiteStreet', 'SiteNumSfx','Predir', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName')) as cursor: # 'POINT_X', 'POINT_Y','SHAPE@XY'# loop through feature set  
                    for row in cursor:
                         row [0] = row[0] #Account
                         row [1] = row[1] #SiteAddres
                         row [2] = row[2] #SiteNum
                         row [3] = row[3] #StreetName
                         row [4] = row[4] #SiteStreet
                         row [5] = row[5] #SiteNumSfx
                         row [6] = row[6] #Predir
                         row [7] = row[7] #Postdir
                         row [8] = row[8] #SiteCity
                         row [9] = row[9] #SiteZip
                         row [10] = row[10] #OwnerName
                         insCursor.insertRow(row)

error

row [0] = row[0] #Account

TypeError: 'tuple' object does not support item assignment

0 Kudos
IanMurray
Frequent Contributor

You cannot change values with a SearchCursor, only access them.  You would need an UpdateCursor if you were setting values.