How do I Calculate x,y from existing shape geometry using Arcpy?

10453
11
Jump to solution
09-13-2013 10:34 AM
MichaelBishopp
Occasional Contributor
This seems very easy, but I cannot figure it out.  I need to calculate the X-Coordinate and Y-Coordinate to the appropriate fields in my featureclass (XCoord, YCoord).  I am trying something like this to no avail:

arcpy.CalculateField_management(Maxtbl, "XCoord", "!shape.extent.XMax!", "PYTHON_9.3", "")


Maxtbl is set to: r"V:\is\work\mb\Util\MaximoMobile\geodb\TestSnappingSigns.gdb\MaximoSigns_Test" (my feature class).

The shape properties/methods don't seem to be documented very well--where do I find those?
Tags (2)
0 Kudos
11 Replies
RichardFairhurst
MVP Honored Contributor
Use @ and the units of measure, which can be CENTIMETERS | DECIMALDEGREES | DECIMETERS | FEET | INCHES | KILOMETERS | METERS | MILES | MILLIMETERS | NAUTICALMILES | POINTS | UNKNOWN | YARDS.  I am assuming this code is somehow running in the Field Calculator given the field syntax.  I would not expect your original code to run in a standalone Python script outside of a Field Calculator code block string and I am not sure that the @ units syntax would work in a standalone script either.

Is there a way to choose what unit of measure you have?  It defaults to the current coordinate systems.

urows = arcpy.da.UpdateCursor(FEATURECLASS)
for urow in urows:
    urow.LATITUDE = !shape.extent.XMax@meters! 
    urow.LONGITUDE = !shape.extent.YMax@meters! 
    urows.updateRow(urow)
del urow
0 Kudos
RichardFairhurst
MVP Honored Contributor
Is there a way to calculate lat/long in python at all?


See this link for how to change the spatial reference of the cursor.  The steps can be used to switch between linear coordinate systems and lat/long and vice versa.
0 Kudos