-3

So the code runs until inserting new row, at which time I get>>>
'attempting to insert [ 3 item result] into these columns [ 5 items]. I have tried to discover where my code is causing a loss in results, but cannot. Any suggestions would be great. Additional information, my feature class I am inserting has five fields and they are they same as the fields as the source fields. It reaches to my length != and my error prints. Please assist if you anyone would like.

# coding: utf8

import arcpy
import os, sys
from arcpy import env
arcpy.env.workspace = r"E:\Roseville\ScriptDevel.gdb"

arcpy.env.overwriteOutput = bool('TRUE')
# set as python bool, not string "TRUE"     

fc_buffers = "Parcels" # my indv. parcel buffers

fc_Landuse = "Geology" # my land use layer 


outputLayer = "IntersectResult" # output layer   


outputFields = [f.name for f in arcpy.ListFields(outputLayer) if f.type not in ['OBJECTID', "Geometry"]] + ['SHAPE@']

    landUseFields = [f.name for f in arcpy.ListFields(fc_Landuse) if f.type not in ['PTYPE']]


parcelBufferFields = [f.name for f in arcpy.ListFields(fc_buffers) if f.type not in ['APN']]


intersectionFeatureLayer = arcpy.MakeFeatureLayer_management(fc_Landuse, 'intersectionFeatureLayer').getOutput(0)

selectedBuffer = arcpy.MakeFeatureLayer_management(fc_buffers, 'selectedBuffer').getOutput(0)


def orderFields(luFields, pbFields):

    ordered = []

    for field in outputFields:

        # append the matching field

        if field in landUseFields:

            ordered.append(luFields[landUseFields.index(field)])

        if field in parcelBufferFields:

            ordered.append(pbFields[parcelBufferFields.index(field)])

    return ordered
    print pbfields


with arcpy.da.SearchCursor(fc_buffers, ["OBJECTID", 'SHAPE@'] + parcelBufferFields) as sc, arcpy.da.InsertCursor(outputLayer, outputFields) as ic:

    for row in sc:

        oid = row[0]
        shape = row[1]
        print (oid)     
        print  "Got this far"

        selectedBuffer.setSelectionSet('NEW', [oid])

        arcpy.SelectLayerByLocation_management(intersectionFeatureLayer,"intersect", selectedBuffer)


        with arcpy.da.SearchCursor(intersectionFeatureLayer, ['SHAPE@'] + landUseFields) as intersectionCursor:

            for record in intersectionCursor:

                recordShape = record[0]
                print "list made"


                outputShape = shape.intersect(recordShape, 4)

                newRow = orderFields(row[2:], record[1:]) + [outputShape] 

                if len(newRow) != len(outputFields):

                    print 'there is a problem.  the number of columns in the record you are attempting to insert into', outputLayer, 'does not match the number of destination columns'

                    print '\tattempting to insert:', newRow

                    print '\tinto these columns:', outputFields

                    continue

                # insert into the outputFeatureClass

                ic.insertRow(newRow)
KVHarmon
  • 1
  • 2
  • 1
    plaese specify what your input is, what is the expected output, and what you are getting instead – Daniele Jun 07 '18 at 21:23
  • Input are individual 0.25 mile parcel buffers and the Geology is a geology id feature class. Result needed (for a total of 41k buffers) intersect each individual parcel buffer with geology layer and write results to new feature class. It seems to work until ..for row in sc: – KVHarmon Jun 07 '18 at 21:34
  • then abuptly stops after print 'Got this far/ – KVHarmon Jun 07 '18 at 21:35
  • so no features are inserted. it just ends. – KVHarmon Jun 07 '18 at 21:35
  • 1
    please clean your indentation. just paste your code, select it and hit the code button – bobrobbob Jun 07 '18 at 22:24

1 Answers1

0

Your with statement where you define the cursors is creating a input cursor with 5 fields, but your row you are trying to feed it is only 3 fields. You need to make sure your insert cursor is the same length as the row. I suspect the problem is actually in the orderfields method. Or what you pass to it.

Adam B
  • 3,662
  • 2
  • 24
  • 33