I have some contours where the z value is stored in the geometry and I want to display it as an attribute. I want to include this in a python script.
I have an expression which does this in python field calculator:
!Shape!.firstpoint.z
This works perfectly, but when I include it in my script:
arcpy.CalculateField_management( shapefile , "VALUE" , "!Shape!.firstpoint.z", "PYTHON")
this does not work, which surprises me because I thought that the Calulate Field was exactly the same as field calcultor, but accessed through arcpy.
The error is that its a string, so has no z attribute: " File "", line 1, in AttributeError: 'str' object has no attribute 'z"
So I've tried:
arcpy.CalculateField_management( shapefile , "VALUE" , "!Shape.firstpoint.z!", "PYTHON")
and that tells me that "firstpoint.z" isnt an attribute of shape
I then tried:
arcpy.CalculateField_management( shapefile , "VALUE" , "!Shape.firstpoint!.z", "PYTHON")
And that seemed to view the first point as a unicode rather than a point. "File "", line 1, in AttributeError: 'unicode' object has no attribute 'z'"
It seems that my problem is getting it to view firstpoint as a point, but I'm not not why this is.
1 Answer 1
Expression type needs to be PYTHON_9.3
This code worked for me:
from arcpy import *
fc = r"C:\test\test.gdb\test"
fld = "testfld"
shapeFldName = Describe (fc).shapeFieldName
CalculateField_management (fc,
fld,
"!{}!.firstPoint.Z".format (shapeFldName),
"PYTHON_9.3")
Happy pythoning!
-
1Right on! I noticed @whatahitson you were typing "firstpoint.z" instead of "firstPoint.Z", which results in the "firstpoint.z" isnt an attribute of shape" error. The caps matter in this case. It may be that the Field Calculator GUI can adjust for the syntax errors but the pure python implementation can't.John– John2015年07月22日 13:13:43 +00:00Commented Jul 22, 2015 at 13:13
-
@John Yeah I was surprised that it works with incorrect capitalisation, but it still seems to now that i've switched to python 9.3 its working there toowhatahitson– whatahitson2015年07月22日 13:36:11 +00:00Commented Jul 22, 2015 at 13:36
Explore related questions
See similar questions with these tags.
!<SHAPE_FIELD_NAME>!.firstPoint.Z
with the exact caps. Another workaround I can think of is to go to your Results pane and find a working Calculate Field result. Right click on it and select Copy As Python Snippet option. The code that you will see when you paste it to Python interpreter should work (theoretically at least).