How to calculate field as cursor name?

849
2
09-14-2012 05:32 AM
LucaMoiana
New Contributor
First time dealing with cursor. I have lines intersecting polygons, I want to get a table with all attributes of the lines, add a field and calculate the field as the name of the polygon. I was able to get a table for each polygon but I can't get to calculate the field and add the polygon name. How do I do? here is my code:

import arcpy
arcpy.env.overwriteOutput = True

arcpy.env.workspace = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Default.gdb'
ws = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Default.gdb'
Comuni = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Sardegna3.gdb\\Comuni'
Linee = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Sardegna3.gdb\\Linee' 

rows = arcpy.SearchCursor(Comuni)

for row in rows:
    print row.NOME
    feat = row.Shape
    arcpy.SelectLayerByLocation_management("Linee", "INTERSECT", feat, selection_type="NEW_SELECTION");
    out_feature_class = arcpy.ValidateTableName("Linee_Clip_" + row.NOME);
    table = arcpy.TableToTable_conversion("Linee", ws, out_feature_class);
    arcpy.AddField_management(out_feature_class, 'Clip', 'TEXT', '', '', '60');
    expression = arcpy.ValidateTableName(row.NOME);
    arcpy.CalculateField_management(table, "Clip", expression);


here is the error I get:

Empty value for ObjectID = 1 The calculated value is invalid for the row with ObjectID = 1. For example, the calculated value may be too large for the field or you may be trying to add a string to a number field. This row will not be updated.
Tags (2)
0 Kudos
2 Replies
GeorgeNewbury
Occasional Contributor
Have you tried the 'Spatial Join' tool:

http://resources.arcgis.com/en/help/main/10.1/index.html#/Spatial_Join/00080000000q000000/

Seems like it should be able to do what you are trying to do.  I don't think you need to necessarily use a cursor here.

Also, on your code the SelectLayerByLocation_management tool's third argument should be a feature layer. You are putting a Geometry object in for it.
0 Kudos
LucaMoiana
New Contributor
Hi George,
Thanks for your help. I ended up, thank to the help of the forum, I ended up doing a second cursor, I'll attach the code below. Spatial Join is a solution I didn't explore, but I'll try.
Cause otherwise I now have to find a way to append the tables...


import arcpy
arcpy.env.overwriteOutput = True

arcpy.env.workspace = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Default.gdb'
ws = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Default.gdb'
Comuni = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Sardegna3.gdb\\Comuni'
Linee = 'C:\\Users\\a391665\\Documents\\ArcGIS\\Sardegna3.gdb\\Linee' 

rows = arcpy.SearchCursor(Comuni)

for row in rows:
    print row.NOME
    feat = row.Shape
    arcpy.SelectLayerByLocation_management("Linee", "INTERSECT", feat, selection_type="NEW_SELECTION");
    out_feature_class = arcpy.ValidateTableName("Linee_Clip_" + row.NOME);
    table = arcpy.TableToTable_conversion("Linee", ws, out_feature_class);
    arcpy.AddField_management(out_feature_class, 'Comune', 'TEXT', '', '', '60');
    arcpy.AddField_management(out_feature_class, 'ISTAT', 'TEXT', '', '', '6');
    
    rows2 = arcpy.UpdateCursor(table)
    for row2 in rows2:
        row2.Comune = str(row.NOME)
        row2.ISTAT = str(row.CODISTAT)
        rows2.updateRow(row2)
0 Kudos