Select to view content in your preferred language

Calculate X/Y Using VB or Python in a Model

1820
2
07-05-2011 09:50 AM
GabeMarcello
Emerging Contributor
This VBA script calculates x/y values from the Shape field to another field. It used to work for me in 9.3 but doesn't work anymore for 10 because of the switch to VB/Python. How would I rewrite this in VB or Python to work in 10? The 'Calculate Geometry' function is useless to me because I want to use it in a model and 'Add X/Y' creates new fields rather than calculating existing ones.

Pre-Logic Script Code:
dim dblx as double
dim dbly as double
dim pPoint as Ipoint
set pPoint = [Shape]
dblx = pPoint.x
dbly = pPoint.y


Field=
dbly
Tags (2)
0 Kudos
2 Replies
StephanieWendel
Esri Contributor
The Add XY coordinates tool pulls the XY coordinates from the shape field. This would be useful for models when you needed to add the fields and also find the X and Y values. It will save a few steps. This tool will also give the Z or M values from the shape field as well if they were enabled on the feature class when it was made. See this link for more information on the tool:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000032000000.htm

To do a field calculation with the Python parser, see this sample script.
Pre-Logic Script Code:
def find(shape):
  point = shape.getPart(0)
  return point.X


NewX =
find(!Shape!)


This code is a sample modified from the Code samples -geometry section found on this link:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000.htm
0 Kudos
GabeMarcello
Emerging Contributor
The Add XY coordinates tool pulls the XY coordinates from the shape field. This would be useful for models when you needed to add the fields and also find the X and Y values. It will save a few steps. This tool will also give the Z or M values from the shape field as well if they were enabled on the feature class when it was made. See this link for more information on the tool:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000032000000.htm

To do a field calculation with the Python parser, see this sample script.
Pre-Logic Script Code:
def find(shape):
  point = shape.getPart(0)
  return point.X


NewX =
find(!Shape!)


This code is a sample modified from the Code samples -geometry section found on this link:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000.htm


Hrm... This works great, but only on blank fields. I tried overwriting and it errors.
0 Kudos