How To: Calculate XY coordinates using Python


Instructions provided describe how to calculate XY coordinates using Python.

There are a number of ways to calculate XY coordinates using Python and ArcGIS tools. Two simple Python methods include using the Add XY Coordinates tool syntax or the Calculate Field tool syntax in combination with the Python Extent class within a script.


Both these options can be used from within the tool, the Python window, or a Python script. Below are Python script versions. The !shape.extent.XMax! and !shape.extentYMax! options can also be used in the Field Calculator when the parser is set to Python. More complicated extraction of XY coordinates from points can be found in the ArcPy-Point document in the Related Information section.

  • The Add XY Coordinates tool creates new fields for Point_X and POINT_Y, calculates their values, and appends POINT_Z, and POINT_M if the input features are Z and M enabled.

    The following is an example of calculating the XY coordinates after projecting the feature class:
    # Import arcpy module
    import arcpy
    # Local variables:
    arcpy.env.workspace = r"C:\Test.gdb"
    Point = "Point"
    Point_Project = "ProjectPoint"
    # Process: Project
    arcpy.Project_management(Point, Point_Project, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "NAD_1983_To_WGS_1984_5", "PROJCS['NAD_1983_Alaska_Albers',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-154.0],PARAMETER['Standard_Parallel_1',55.0],PARAMETER['Standard_Parallel_2',65.0],PARAMETER['Latitude_Of_Origin',50.0],UNIT['Meter',1.0]]")
    #Calculate the XY Coordinates

  • The next workflow uses the Calculate Field syntax to access the Extent class properties. There are a number of properties that can be calculated, including the XMax and YMax values.
    Below is a sample:
    import arcpy
    fc = r"C:\New File Geodatabase.gdb\Point"
    arcpy.AddField_management("Point", "X", "DOUBLE")
    arcpy.AddField_management("Point", "Y", "DOUBLE")
    arcpy.CalculateField_management("Point", "X", "!shape.extent.XMax!","PYTHON_9.3")
    arcpy.CalculateField_management("Point", "Y", "!shape.extent.YMax!","PYTHON_9.3")

    This functionality is also accessible in ArcGIS 9.3 and 9.3.1 using arcgisscripting. Please see the Related Information section for more information. 

Related Information