Knowledge Base - Technical Articles


Technical Article   HowTo:  Calculate X, Y, and Z values in an attribute table using Field Calculator

Article ID: 19643
Software:  ArcGIS - ArcEditor 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 8.0.1, 8.0.2, 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcView 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1
Platforms: N/A

Summary

Instructions provided describe how to calculate X, Y, and Z values in an attribute table using the Field Calculator and Visual Basic for Applications (VBA).

 These code examples return a value of type 'double', so use them to calculate either an existing field of type 'double' or a new field of type 'double' that has been added to the table.


Procedure

This procedure can be used with new or existing fields in an attribute table. If it is necessary to add new fields to the attribute table, see the ArcGIS Desktop Help topic 'Adding and deleting fields in a table'.

  1. Add the point layer to ArcMap.
  2. Start Editing. -show me-

    A. Select 'Editor Toolbar' from the Tools menu, if the Editor Toolbar is not already open.
    B. Click Editor and select 'Start Editing' from the pop-up menu.
    C. Select the data layer for the list box, and click OK if prompted by the 'Start Editing' dialog box.
    D. Set the Target drop-down list to the layer to edit.
  3. Right-click the layer and select Open Attribute Table.
  4. Right-click the field to modify, and select Calculate Values.
  5. Check Advanced. Two empty text entry boxes are present.
  6. Paste the following code into the 'Pre-Logic VBA Script Code' text box.

    Dim Output As Double
    
    Dim pPoint As IPoint
    Set pPoint = [Shape]
    Output = pPoint.X

  7. In the Expression line, the small, one line area below the Pre-Logic box, type 'Output' and click OK to calculate the field.

     The units of the calculated values are the units that the features are stored in, not the map units or display units of the data frame currently worked on.

     If the data is stored in feet, the calculated values appear in feet. To allow calculated data to be in different units, other than the data's units,do one of the following:
    Add a conversion into the calculation expression.
    Project the data into a coordinate system that uses the units the values should be in, and then perform the calculation.

  8. Use the procedure described above for the Y and Z fields by changing the Output value.

    To calculate for Y:

    Output = pPoint.Y

    To calculate for Z:

    Output = pPoint.Z


     A 3D Analyst license is not required to extract Z values from the shape field of an existing 3D layer.


Related Information

  • Use VBA functions in the Field Calculator
    Instructions provided describe how to use the Field Calculator to use some common VBA functions. The Field Calculator dialog box allows for calculating the values of a field by specifying a calculation expression. You can type the expression di...
  • Is it possible to use VBA Script Code for the Calculate Field tool in ArcToolbox?
    It is not possible to use VBA Script Code for the Calculate Field tool prior to 9.2. This has not been implemented into the tool. When VBA Script Code is necessary, the Field Calculator, by way of right-clicking on the field heading in the Attribu...

Created: 6/7/2001
Last Modified: 9/22/2014

Article Rating: (3)
If you would like to post a comment, please login

Comments

By Anonymous - 12/09/2008 2:29 PM

The article contains a typographical or grammatical error.

Article references "Virtual Basic" instead of "Visual Basic"

By Anonymous - 10/29/2006 5:56 PM

Other - See details below.

How to do that programmatically with VBA? I mean, i need to create a tool / button with VBA to add 2 numeric fields (x and y) and instantly populate the newly created fields with x value and y value of my point shapefile. Thank you.

Rating:

By Anonymous - 02/03/2005 6:27 AM

I cannot visualize circumstances that would use this "fix". What is my hypothetical need? Thanks.

Rating:

By Anonymous - 05/19/2004 7:15 AM

yes, but I need to project my data into decimal degrees. What is the script for that?

Rating: