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

8341
11
09-13-2013 10:34 AM
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)
1 Solution

Accepted Solutions
MVP Honored Contributor
Thanks,

But I bet your fc is a line feature class.  My feature class is a point.  Does XMax work for points??

No.  It does not.  There is no extent for a point, only for a buffer polygon around a point.  For the X coordinate of the point itself you calculate the X of the point's Centroid.  So it is:

!Shape.Centroid.X!

Here is the help that lists the valid geometry calculations allowed for each feature class type thro...  There are several others available through the Field Calculator, like extent, but an extent means an object has at least the potential to have both height and width, which a point cannot have.
11 Replies
Regular Contributor II
If you use a cursor, you can query each feature for its extent object, which has the properties you seek.
the Extent (arcpy) page in the Help should be of assistance.
There is a sample on that page that queries each feature in a fc for its extent, and prints the values
You would instead use an Update Cursor to perform the same query and then update the
table's fields you have provided for holding these values.
Occasional Contributor
If you use a cursor, you can query each feature for its extent object, which has the properties you seek.
the Extent (arcpy) page in the Help should be of assistance.
There is a sample on that page that queries each feature in a fc for its extent, and prints the values
You would instead use an Update Cursor to perform the same query and then update the
table's fields you have provided for holding these values.

This seems a bit tedious (and slow) when there is a way to calculate geometry in the ArcMap interface.  Isn't there something analgous in Arcpy that will allow me to just calculate the values from the shape properties instead of having to cursor through each record??
MVP Honored 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?

Many of the geometry calculation examples are contained in this help page.

This specific example seems to match the one you are doing, but is written as though you are doing the calculation in Desktop:

Parser:
Python

Expression:
!shape.extent.XMax!

This was built using ModelBuilder and I know it works for my data.

# Import arcpy module
import arcpy

# Local variables:
CENTERLINE_ROUTES_MERGED = "\\\\Agency\\agencydfs\\Trans\\rfairhur\\Layers\\Centerline_Synch.gdb\\CL_Maint\\CENTERLINE_ROUTES_MERGED"

# Process: Calculate Field
arcpy.CalculateField_management(CENTERLINE_ROUTES_MERGED, "EXTENT_MAXX", "!Shape.extent.XMax!", "PYTHON_9.3", "")

It appears the syntax is identically configured to yours (assuming the fc name and field name were changed for our respective databases), so perhaps the problem is with the rest of your model.  Possibly a lock is being placed on the fc that prevents the calculation from occurring or you mistyped something in the fc name.

Try capitalizing the word Shape in the calculation, since at 10.0 or later it gets picky about capitalization and in my fgdb Shape is capitalized.
Occasional Contributor
Many of the geometry calculation examples are contained in this help page.

This specific example seems to match the one you are doing, but is written as though you are doing the calculation in Desktop:

Parser:
Python

Expression:
!shape.extent.XMax!

This was built using ModelBuilder and I know it works for my data.

# Import arcpy module
import arcpy

# Local variables:
CENTERLINE_ROUTES_MERGED = "\\\\Agency\\agencydfs\\Trans\\rfairhur\\Layers\\Centerline_Synch.gdb\\CL_Maint\\CENTERLINE_ROUTES_MERGED"

# Process: Calculate Field
arcpy.CalculateField_management(CENTERLINE_ROUTES_MERGED, "EXTENT_MAXX", "!Shape.extent.XMax!", "PYTHON_9.3", "")

It appears the syntax is identically configured to yours (assuming the fc name and field name were changed for our respective databases), so perhaps the problem is with the rest of your model.  Possibly a lock is being placed on the fc that prevents the calculation from occurring or you mistyped something in the fc name.

Try capitalizing the word Shape in the calculation, since at 10.0 or later it gets picky about capitalization and in my fgdb Shape is capitalized.

Thanks,

But I bet your fc is a line feature class.  My feature class is a point.  Does XMax work for points??
MVP Honored Contributor
Thanks,

But I bet your fc is a line feature class.  My feature class is a point.  Does XMax work for points??

No.  It does not.  There is no extent for a point, only for a buffer polygon around a point.  For the X coordinate of the point itself you calculate the X of the point's Centroid.  So it is:

!Shape.Centroid.X!

Here is the help that lists the valid geometry calculations allowed for each feature class type thro...  There are several others available through the Field Calculator, like extent, but an extent means an object has at least the potential to have both height and width, which a point cannot have.
Occasional Contributor
No.  It does not.  There is no extent for a point, only for a buffer polygon around a point.  For the X coordinate of the point itself you calculate the X of the point's Centroid.  So it is:

!Shape.Centroid.X!

Here is the help that lists the valid geometry calculations allowed for each feature class type thro...  There are several others available through the Field Calculator, like extent, but an extent means an object has at least the potential to have both height and width, which a point cannot have.

Thanks for That!  Where is this documented?  I'm having a hard time finding this kind of info.
MVP Honored Contributor
Thanks for That!  Where is this documented?  I'm having a hard time finding this kind of info.

The only reason it is hard to find it is that it doesn't exist.  😉  It is found in the user forum only as far as I have seen.  Users had to do the same for VBA back when it used to work.  So be grateful for the furums.
Occasional Contributor
Thanks,

But I bet your fc is a line feature class.  My feature class is a point.  Does XMax work for points??

Amazingly, I tried it with a capital "S" in Shape (like !Shape...!) and also a lowercase "s" in shape (like !shape...!).  The interpreter doesn't seem to care if it is upper or lower case...but for me the correct answer is !Shape.Centroid.X! and !Shape.Centroid.Y! (It also works for !shape.Centroid.X! & !shape.Centroid.Y! or !shape.centroid.x! & !shape.centroid.y!...you get the picture.)  I suppose it is easier to read when it is title case, but not necessary.
Occasional Contributor
The only reason it is hard to find it is that it doesn't exist.  😉  It is found in the user forum only as far as I have seen.  Users had to do the same for VBA back when it used to work.  So be grateful for the furums.

Thanks for your help!  Bad news that it isn't documented.  That seems like a BIG oversight to me :mad:.  I am totally grateful for the forums!  Thanks again. 🙂