UpdateCursor Lat and Long

965
4
06-05-2017 05:16 AM
JenniferMcCarthy
New Contributor

We are currently trying to update existing null values with X and Y coordinates.

We are getting an error:

>>>
Traceback (most recent call last):
File "C:\FLRmodel\CalculatePlotNumberByAccess3.py", line 72, in <module>
row[2] = round(row[0], 6)
TypeError: a float is required
>>> 

with arcpy.da.UpdateCursor(projectedFC, ['SHAPE@X', 'SHAPE@Y', 'PLOT_LONG', 'PLOT_LAT'], "" , spatialRef) as cursor:
    for row in cursor:
        row[2] = round(row[0], 6)
        row[3] = round(row[1], 6)

        cursor.updateRow(row)

Why does it ask for a float? 

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

It is asking for a float because the round function requires a floating point number to round, or an integer it can convert to a floating point and round back to itself.  In short, you are passing it an invalid data type.  In your case, since you are talking about NULLs, I suspect you are passing it a None.

Before I provide a specific suggestion, do you have records without geometries or with empty geometries?  If so, what do you want PLOT_LONG and PLOT_LAT to be in those cases?  Just keep them NULL?

JenniferMcCarthy
New Contributor

we have null values that we want to update with projected coordinates. The coordinates will then be rounded to 6 decimal places.  

0 Kudos
VinceAngelo
Esri Esteemed Contributor

The error indicates that the source shape is also NULL (or empty). Joshua asked about the shape contents, not the target fields.  You can add a "if (row[0] and row[1]):" above your row assignment to prevent the error, with an else to report trouble.

- V

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Implementing Vince's suggestion:

with arcpy.da.UpdateCursor(projectedFC, ['SHAPE@X', 'SHAPE@Y', 'PLOT_LONG', 'PLOT_LAT'], "" , spatialRef) as cursor:
    for row in cursor:
        if (row[0] and row[1]):
            row[2] = round(row[0], 6)
            row[3] = round(row[1], 6)
            cursor.updateRow(row)

Adding the extra logical check will likely prevent the script from bombing, but it isn't going to address those records where there isn't any geometry or where the geometry is empty.