Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Try this:

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');

    rows2 = arcpy.UpdateCursor(table)
        for row2 in rows2:
        row2.Clip = str(row.NOME)
            rows2.updateRow(row2)

The second for loop goes through all the rows in the table and puts the value of row.NOME on the table's Clip column.

-----

Can you try this?

     expression = str(row.NOME);
     arcpy.CalculateField_management(table, "Clip", expression);

-------

I think an UpdateCursor would work.

    rows = arcpy.UpdateCursor(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');
        row.Clip = str(row.NOME)
        rows.updateRow(row)

It should be faster, too. From experience, the Calculate Field function can be rather slow. 

share|improve this answer
    
Hi thank for your help; I did as suggested but I get this error: Runtime error <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function. –  Luca Moiana Sep 14 '12 at 13:59
    
Oops. My bad, fixed my answer. instead of row.Clip = row.NOME, I set it to row.Clip = str(row.NOME) Could you try it again? –  R.K. Sep 14 '12 at 14:05
    
no way, same error there. –  Luca Moiana Sep 14 '12 at 14:14
    
My apologies, I misunderstood your question. Will formulate a better answer. –  R.K. Sep 14 '12 at 14:21
    
btw if I open the table in ArcGIS and use field calculator str(row.NOME) works... –  Luca Moiana Sep 14 '12 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.