How to access element in geoprocessing value object?

1758
9
01-23-2017 08:30 PM
TieshengWu
Occasional Contributor

HI all

I am puzzled by a simple problem.  I don't know how to access a point object's x and y coordinate from a  arctoolbox in  python script.

It can be accessed  this way:

pnt=arcpy.GetParameterAsText(0).split()    # c1 is a point object in arctoolbox interface
x=float(pnt[0])
y=float(pnt[1])

It's not ideal because the code will fail when the x and y are inputed in different decimal seperator, say ',' format.

So I use:

pnt=arcpy.GetParamete(0)

But what's the method to get pnt.X and pnt.Y?

0 Kudos
9 Replies
FC_Basson
MVP Regular Contributor

Have a look at this help description: http://pro.arcgis.com/en/pro-app/arcpy/classes/point.htm

Then to read the geometry:

point = arcpy.GetParameter(0) 
ptGeometry = arcpy.PointGeometry(point)
x = ptGeometry.X
y = ptGeometry.Y‍‍‍‍‍‍‍‍
0 Kudos
TieshengWu
Occasional Contributor

Thank you Basson. Line 2 return: RuntimeError: Object: CreateObject cannot create geometry from inputs.

arcpy.AddMessage(type(point)) show:  <type 'geoprocessing value object'> 

0 Kudos
FC_Basson
MVP Regular Contributor

It leaves the question of how your point object is defined?  Is it a string?  Please provide more information on the tool.

0 Kudos
TieshengWu
Occasional Contributor

0 Kudos
FC_Basson
MVP Regular Contributor
point = arcpy.GetParameter(0)
coords = str(point).split(' ')
x = coords[0]
y = coords[1]
arcpy.AddMessage(x)
arcpy.AddMessage(y)
0 Kudos
TieshengWu
Occasional Contributor

The x and y will be used as float, so decimal seperator problem still exists. 

0 Kudos
FC_Basson
MVP Regular Contributor

Your decimals should be in point notation (12.3456) and not comma notation (12,3456), then the Python code works.  

0 Kudos
TieshengWu
Occasional Contributor

Than you Basson, but I am tring to write a tool not only for myself.

0 Kudos
FC_Basson
MVP Regular Contributor

So your issue is with the regional settings of the OS for decimal number notation.  You can detect what format the user's OS is showing floats with 

locale.localeconv()['decimal_point']