Select to view content in your preferred language

pointZ and getZ

2690
4
10-15-2013 12:32 AM
brucesimonson
New Contributor III
Hi Gang,

I'm chasing myself into corners, trying to add a field to a PointZ shapefile, that contains the hidden Z value associated with the shape.  Can't get it to work.  I expect a Python snippet like this to work, in field calculator, for example:

[Shape].Z


But I get nothing but errors.

Huh?
-Bruce
Tags (2)
0 Kudos
4 Replies
NeilAyres
MVP Alum
Why not use the Calculate Geometry option on the column, then select Z.
Cheers,
Neil
0 Kudos
RichardFairhurst
MVP Honored Contributor
Brackets only apply to VBA and VB Script fields.  With python you use exclamation marks for fields.  If you are at ArcGIS 10.0 or above, VBA is no longer supported by the Field Calculator and VB Script, which replaced it, cannot do geometry calculations.  You have to use Python.  Also you have to specify that you want the Centroid for a point feature class.  So check the Python option and use:

!Shape.Centroid.Z!
0 Kudos
brucesimonson
New Contributor III
Brackets only apply to VBA and VB Script fields.  With python you use exclamation marks for fields.  If you are at ArcGIS 10.0 or above, VBA is no longer supported by the Field Calculator and VB Script, which replaced it, cannot do geometry calculations.  You have to use Python.  Also you have to specify that you want the Centroid for a point feature class.  So check the Python option and use:

!Shape.Centroid.Z!


Both of the above suggestions work great, when run from the attribute table.  I'd like to extend this, though, to creating a python program which reads all of the shapefiles in a workspace, adds a field for z, and updates with the appropriate value.

For example, in a set of shapefiles that have PointZ values in meters, I'd like to add a field which captures the Z value in feet (from the source shapefiles, which are in meters).  Not sure why I can't get this to work:

import arcpy
from arcpy import env

env.workspace = "P:/path/to/some/directory"

fcList = arcpy.ListFeatureClasses( )

for fc in fcList:
    arcpy.AddField_management( fc, "Z_feet", "DOUBLE", "10", "3" )
    arcpy.CalculateField_management(fc, "Z_feet", "3.28083 * float(!SHAPE.CENTROID.Z!)", "PYTHON")


In particular, !SHAPE.CENTROID.Z! seems to work in the field calculator (Python mode), but not in a Python program.

Suggestions?
0 Kudos
RichardFairhurst
MVP Honored Contributor
Both of the above suggestions work great, when run from the attribute table.  I'd like to extend this, though, to creating a python program which reads all of the shapefiles in a workspace, adds a field for z, and updates with the appropriate value.

For example, in a set of shapefiles that have PointZ values in meters, I'd like to add a field which captures the Z value in feet (from the source shapefiles, which are in meters).  Not sure why I can't get this to work:

import arcpy
from arcpy import env

env.workspace = "P:/path/to/some/directory"

fcList = arcpy.ListFeatureClasses( )

for fc in fcList:
    arcpy.AddField_management( fc, "Z_feet", "DOUBLE", "10", "3" )
    arcpy.CalculateField_management(fc, "Z_feet", "3.28083 * float(!SHAPE.CENTROID.Z!)", "PYTHON")


In particular, !SHAPE.CENTROID.Z! seems to work in the field calculator (Python mode), but not in a Python program.

Suggestions?


Change "PYTHON" in your calculation to "PYTHON_9.3".  "PYTHON" is pre-9.3 and does not use the current syntax of the field calculator.  A new Python keyword was created so that "PYTHON" would remain unchanged for pre-9.3 scripts to maintain backward compatibility.

Additionally you don't need to cast to float and you don't need to do the multiplication to convert to Feet.  Change the calculation to:

!SHAPE.CENTROID.Z@FEET!
0 Kudos