5

I have a feature class of points: AL_CB_Parcel_Intersect. This feature class is already in GCS_WGS_1984. I am trying to use arcpy.AddGeometryAttributes to create X, Y attributes, but I keep getting this error:

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

This works in ArcMap.

What am I doing wrong in my script?

import arcpy
import os
parcels_dir = r"...\Processed"
stateabb = {'Alabama': 'AL',....}

coord_sys = "GCS_WGS_1984"

for k, v in stateabb.items():
if k == 'Alabama':
    print(k)
    parcel_gdb = "{}.gdb".format(k)
    parcel_cent = "{}_CB_Parcel_Intersect".format(v)
    parcel_path = os.path.join(parcels_dir, parcel_gdb)
    parcel_feat = os.path.join(parcel_path, parcel_cent)
    arcpy.env.workspace = parcel_path
    arcpy.AddGeometryAttributes_management(parcel_feat, "POINT_X_Y_Z_M", "", "", coord_sys)
    print("{} is done!".format(parcel_cent))
2
  • 2
    Why specify it at all if the data's already in the coordinate system that you want? I think that "coord_sys" is still just a string.
    – mkennedy
    Feb 24, 2020 at 23:37
  • Thanks. All the below solutions worked and it also seemed to work fine if I just left out the coord system (it automatically put it in the coord sys of the feature class (which was already WGS_1984).
    – gwydion93
    Feb 25, 2020 at 15:50

3 Answers 3

7

Since your feature class is in GCS_WGS_1984, you don't need to specify any Coordinate System. Though, if you want to add coord_sys explicitly, you can use one of those:

  • EPSG code as string e.g. "4326"
  • EPSG code as integer e.g. 4326
  • SpatialReference object
# as string
arcpy.AddGeometryAttributes_management(parcel_feat, "POINT_X_Y_Z_M", "", "", "4326")
#OR as integer
arcpy.AddGeometryAttributes_management(parcel_feat, "POINT_X_Y_Z_M", "", "", 4326)

#OR as SpatialReference object 
coord_sys = arcpy.SpatialReference(4326) # EPSG:4326 -> GCS_WGS_1984
arcpy.AddGeometryAttributes_management(parcel_feat, "POINT_X_Y_Z_M", "", "", coord_sys)
2
  • I tried in ArcGIS 10.2.2. I guess it's the same in other 10.x versions. Feb 24, 2020 at 23:51
  • Thanks, this works and I chose it as using coord_sys = arcpy.SpatialReference(4326) was the easiest solution.
    – gwydion93
    Feb 25, 2020 at 15:49
7

Another way is to specify the spatial Reference by name, for instance, Geographic Coordinate Systems/World/WGS 1984.

The following snippet tested on ArcGIS 10.5 and worked as expected:

import arcpy
from arcpy import env
ws = env.workspace = r"F:\Ahmad\Test\PT"
fc = "Cities.shp"

sr = arcpy.SpatialReference("Geographic Coordinate Systems/World/WGS 1984")
geom = arcpy.AddGeometryAttributes_management(fc, "POINT_X_Y_Z_M","","",sr)

Here is the output:

enter image description here

POINT_X and POINT_Y are the new geometry columns added by the code above. As you can see, it is exactly the same coordinates specified in LATITUDE and LONGITUDE columns defined already.

5

The only thing I can guess is that the coordinate system parameter needs to be a coordinate system data type. You're passing a string "GCS_WGS_1984", however, the documentation states that it should be a coordinate system instead.

Try modifying your code to declare coord_sys as follows:

coord_sys = arcpy.SpatialReference(4326)

The sample code in the documentation is a bit ambiguous, because they're setting the coordinate system to an empty string. However, before that, they set the environment coordinate system explicitly to the coordinate system of the input features. You could try that too. You could try getting the coordinate system of your feature class and then set it in the code to be the same.

2
  • arcpy.SpatialReference("GCS_WGS_1984") throws an error in v10.2.2. Feb 24, 2020 at 23:53
  • Thanks. That's weird. It seems to work with other coordinate systems. I've modified my answer to use the factory code.
    – Fezter
    Feb 25, 2020 at 0:13

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.