2

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.

asked Jul 21, 2015 at 11:12
2
  • 1
    It is interesting to find out that the first script example you gave did not work. I tried it and it is working fine with me. In fact legitimate attribute should be like this !<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). Commented Jul 21, 2015 at 13:15
  • ...and it does. thankyou. I've had a play with it and the thing that seemed to make the difference was the expression type: Python throws an error by python 9.3 works fine. I hadn't really given that much thought. Commented Jul 21, 2015 at 13:51

1 Answer 1

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!

answered Jul 21, 2015 at 17:39
2
  • 1
    Right 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. Commented 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 too Commented Jul 22, 2015 at 13:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.