Select to view content in your preferred language

Upate Geometry fields

1271
8
Jump to solution
04-08-2022 11:26 AM
TonyAlmeida
Occasional Contributor III

Trying to convert some of my ArcMap Scripts to Pro,  I am having a hard time with the arcpy.AddGeometryAttributes_management. but I keep getting the error and currently Running this in python window in Pro to test before I put it in a Pro toolbox script. I need some help please.

 

Error:

 

 

Traceback (most recent call last):
File "<string>", line 23, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3724, in AddGeometryAttributes
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3721, in AddGeometryAttributes
retval = convertArcObjectToPythonObject(gp.AddGeometryAttributes_management(*gp_fixargs((Input_Features, Geometry_Properties, Length_Unit, Area_Unit, Coordinate_System), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Traceback (most recent call last):
File "c:\program files\arcgis\pro\Resources\ArcToolbox\scripts\AddGeometryAttributes.py", line 397, in <module>
addGeomAtts.execute()
File "c:\program files\arcgis\pro\Resources\ArcToolbox\scripts\AddGeometryAttributes.py", line 98, in execute
edit.stopEditing(True)
RuntimeError: start edit session

 

 

Code:

 

 

 

import arcpy, string, os

workspace = r"C:\Users\***\AppData\Roaming\Esri\ArcGISPro\Favorites\Server.sde"

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyr = m.listLayers("FC_layer")[0]

workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)


##
if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:
    arcpy.AddGeometryAttributes_management(lyr,"POINT_X_Y_Z_M","", "", arcpy.SpatialReference("WGS 1984"))


##fields=['POINT_X','POINT_Y'] #Change to match your column names containing x and y coordinates
##
##with arcpy.da.UpdateCursor(lyr,['SHAPE@X','SHAPE@Y']+fields) as cursor:
##    for row in cursor:
##        if None in row:
##            row[0] = float(row[2])
##            row[1] = float(row[3])
##            cursor.updateRow(row)

 

 

 

I tried an updatecursor but get the following error.

row[0]=float(row[2])
TypeError: float() argument must be a string or a number, not 'NoneType'

 

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor

You are trying to set your SHAPE@X field equal to the value in the POINT_X field float() doesn't work on a geometry object.  Same with the Y.

You  want the exact opposite.  set the value of the POINT_X field to the X value of the geometry object.

 

with arcpy.da.UpdateCursor(lyr,['SHAPE@X','SHAPE@Y'] + fields) as cursor:
    for row in cursor:
        if row:
            row[2] = float(row[0])
            row[3] = float(row[2])
            cursor.updateRow(row)

 

Will take the x and y values of the geometry and populate the new x and y fields.

also, I changed it to if row:

that way, if the row is empty, it won't try to do anything with it.  Shouldn't really need since you are iterating through all the features in the FC.  However, might save an error if there are some empty geometries in the FC.

Of course, the cursor isn't needed since the AddGeometry... not only adds the fields, but populates them.

Cursor would be good to update that info if you move the point.

R_

View solution in original post

0 Kudos
8 Replies
DanPatterson
MVP Esteemed Contributor

arcpy.AddGeometryAttributes_management(""FC_layer"","POINT

this isn't going to work either it needs to be a variable not a string

Add Geometry Attributes (Data Management)—ArcGIS Pro | Documentation


... sort of retired...
TonyAlmeida
Occasional Contributor III

I did catch that after I posted my question. After doing some more digging/searching I did notice that if I had the attribute table open in Pro the X & Y fields didn't update. I closed the attribute table and re-opened it and the fields were updated with arcpy.AddGeometryAttributes_management, but I am still getting the error even after the fields update. The layer is check on the Contents List by editing and I have also unchecked it.

0 Kudos
RhettZufelt
MVP Notable Contributor

You already defined the layer you want as variable lyr.

You need to replace "FC_Layer" in the GetCount, AddGeometry and the cursor to lyr.

Also, on the cursor (if you use that):

if None in row:

 is telling it to only process on None (null/empty) data, so will throw the 'NoneType' error.

Did not test that, but replacing the text "FC_layer" with the variable lyr worked in my testing on the AddGeometry part.

R_

0 Kudos
TonyAlmeida
Occasional Contributor III

I have fixed line 14 to lyr, I was doing some testing.

 

import arcpy, string, os

workspace = r"C:\Users\***\AppData\Roaming\Esri\ArcGISPro\Favorites\Server.sde"

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyr = m.listLayers("FC_layer")[0]

workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)


##
if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:
    arcpy.AddGeometryAttributes_management(lyr,"POINT_X_Y_Z_M","", "", arcpy.SpatialReference("WGS 1984"))

 

When I first create the points they are all <Null> which is why I used None in row:

But even without "None in row:"  I get the same error.

 
float() argument must be a string or a number, not 'NoneType'"

 Code:

 

 

 

import arcpy, string, os

workspace = r"C:\Users\***\AppData\Roaming\Esri\ArcGISPro\Favorites\Server.sde"

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyr = m.listLayers("FC_layer")[0]

workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

fields=['POINT_X','POINT_Y'] #Change to match your column names containing x and y coordinates

with arcpy.da.UpdateCursor(lyr,['SHAPE@X','SHAPE@Y']+fields) as cursor:
    for row in cursor:
        if None in row:
            row[0] = float(row[2])
            row[1] = float(row[3])
            cursor.updateRow(row)

 

 

 

 

0 Kudos
RhettZufelt
MVP Notable Contributor

Well, you got better response that I did.  I had to completely close Pro and open it again before I could see the new fields.  Maybe because I tested in a Notebook.

I didn't test with versioned data, if that is the case, might explain the error.

Did you try encompassing the code in an editor session?

R_

0 Kudos
TonyAlmeida
Occasional Contributor III

I did, I get the same thing. It works but I still get that error message in the python window.

 I have a lot of "Add-in" from ArcMap that I need to convert. The more I use Pro the less I am liking it.

I do appreciate the help.

 

 

import arcpy, string, os

workspace = r"C:\Users\***\AppData\Roaming\Esri\ArcGISPro\Favorites\Server.sde"

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyr = m.listLayers("FC_layer")[0]

workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

### Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)
##
### Edit session is started without an undo/redo stack for versioned data
###  (for second argument, use False for unversioned data)
edit.startEditing(True)
##
### Start an edit operation
edit.startOperation()

##
if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:
    arcpy.AddGeometryAttributes_management(lyr,"POINT_X_Y_Z_M","", "", arcpy.SpatialReference("WGS 1984"))


##fields=['POINT_X','POINT_Y'] #Change to match your column names containing x and y coordinates
##
##with arcpy.da.UpdateCursor(lyr,['SHAPE@X','SHAPE@Y']+fields) as cursor:
##    for row in cursor:
##        if None in row:
##            row[0] = float(row[2])
##            row[1] = float(row[3])
##            cursor.updateRow(row)

### Stop the edit operation.
edit.stopOperation()
##
### Stop the edit session and save the changes
edit.stopEditing(True)

 

 

 

0 Kudos
RhettZufelt
MVP Notable Contributor

You are trying to set your SHAPE@X field equal to the value in the POINT_X field float() doesn't work on a geometry object.  Same with the Y.

You  want the exact opposite.  set the value of the POINT_X field to the X value of the geometry object.

 

with arcpy.da.UpdateCursor(lyr,['SHAPE@X','SHAPE@Y'] + fields) as cursor:
    for row in cursor:
        if row:
            row[2] = float(row[0])
            row[3] = float(row[2])
            cursor.updateRow(row)

 

Will take the x and y values of the geometry and populate the new x and y fields.

also, I changed it to if row:

that way, if the row is empty, it won't try to do anything with it.  Shouldn't really need since you are iterating through all the features in the FC.  However, might save an error if there are some empty geometries in the FC.

Of course, the cursor isn't needed since the AddGeometry... not only adds the fields, but populates them.

Cursor would be good to update that info if you move the point.

R_

0 Kudos
TonyAlmeida
Occasional Contributor III

Good catch, I was looking at this for a few hours LOL.

0 Kudos