1

I have a point shapefile where I want to add two new fields "Lat" and "Long" and calculate the X and Y coordinates in decimal degrees for each field in the coordinate system: WGS 84 (EPSG-Code 4326).

This is my code so far for calculating the "Lat" field in "Point_X" geometry_propery:

# Add "Lat" field
arcpy.management.AddField("routenplanung", "Lat", "FLOAT")

# Add "Long" field
arcpy.management.AddField("routenplanung", "Long", "FLOAT")

# "Calculate Geometry" in Decimal Degrees with Output-CS WGS 1984
arcpy.management.CalculateGeometryAttributes("routenplanung", 
                                             ["Lat", "POINT_X"], 
                                             coordinate_system="WGS 1984",
                                             coordinate_format="DD",
                                             length_unit="",
                                             area_unit="")

I get this error message:

---------------------------------------------------------------------------
ExecuteError                              Traceback (most recent call last)
In  [20]:
Line 8:     arcpy.management.CalculateGeometryAttributes("routenplanung", 

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateGeometryAttributes:
Line 4254:  raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateGeometryAttributes:
Line 4251:  retval = convertArcObjectToPythonObject(gp.CalculateGeometryAttributes_management(*gp_fixargs((in_features, geometry_property, length_unit, area_unit, coordinate_system, coordinate_format), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: ERROR 000622: Failed to execute (Calculate Geometry Attributes). Parameters are not valid.
ERROR 000628: Cannot set input into parameter coordinate_system.

---------------------------------------------------------------------------

I tried different variations for the parameter "coordinate_system" like:

coordinate_system="PROJCS['WGS_1984_UTM_Zone_12N',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-111.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]]"

or

coordinate_system="EPSG 4326"

But nothing works. What value should I use?

4
  • Latitude is a Y value.
    – Vince
    Mar 23 at 13:59
  • In one of your attempts to set the coordinate_system, you are try a geographic coordinate system and the other you are trying a projected coordinate system. Which one specifically are you interested in?
    – bixb0012
    Mar 23 at 14:06
  • Have you tried using arcpy.SpatialReference(4326) as a parameter? or a .exportToString().split(';')[0]
    – Vince
    Mar 23 at 14:12
  • I tried those two options but it also doesn't work. If I do it manually in the attribute table it works without any problems.
    – Radde1683
    Mar 23 at 15:17

1 Answer 1

1

There are two issues with the current syntax, this specific error is dealing with one of the issues. According to Calculate Geometry Attributes (Data Management) - ArcGIS Pro | Documentation, the coordinate_system parameter takes a "Coordinate System" data type. Unfortunately, there is no "Coordinate System" data type in ArcPy. There is a SpatialReference - ArcGIS Pro | Documentation object, and a coordinate system is an important part of the spatial reference object, but they aren't one of the same.

If you run the tool manually and then look at the details of the job, you will see that "Coordinate System" Data Type is actually a String data type containing the Well-Known Text (WKT) description of the coordinate system. In this case for WGS 1984:

GEOGCS["GCS_WGS_1984",
    DATUM["D_WGS_1984", SPHEROID["WGS_1984",6378137.0,298.257223563]],
    PRIMEM["Greenwich",0.0],
    UNIT["Degree",0.0174532925199433]
]

One of the sample coordinate systems in the OP, WGS_1984_UTM_Zone_12N, works; but the problem then becomes the second syntax issue that prevents the tool from running successfully.

Before getting to the second syntax issue, I want to point out that coordinate_system=4326 will work since the parameters accepts WKID of coordinate systems, but I tend to prefer using an explicit ArcPy SpatialReference object to reduce any chance of confusion: coordinate_system=arcpy.SpatialReference(4326).

The second syntax issue relates to Value Tables. When using a Python list to represent an ArcGIS Value Table, the outer brackets represent the table itself, and the contents within the table need to be in their own brackets.

# incorrect Python representation of an ArcGIS Value Table
["Lat", "POINT_Y"]

# correct Python representation of an ArcGIS Value Table
[["Lat", "POINT_Y"]]
1
  • Yes this made it work, thnak you really much!
    – Radde1683
    Apr 3 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.