Convert VBA to Python field calculator script

5310
6
Jump to solution
10-15-2014 06:29 PM
SiyangTeo
Occasional Contributor

Hi, I have been using this VBA field calculator script in ArcGIS 9.3.1 to shift points spatially according to the X & Y fields from a table join:

 

Dim pPoint As IPoint

Set pPoint = [Shape]

pPoint.X =

pPoint.Y =

__esri_field_calculator_splitter__

pPoint

 

However, it does not work in after I upgraded to ArcGIS 10.2.2. Is there anyone who is able to convert this VBA script to Python?

 

Thank you in advance.

 

Regards,

Siyang

0 Kudos
1 Solution

Accepted Solutions
SiyangTeo
Occasional Contributor

I found a Python script that does the work, however, it can only shift when the XY fields are within the shapefiles, but does not work when reading from fields of a table join.

Anyone knows how to rectify this?

def pointMove(shape,x_value,y_value):

   point = shape.getPart(0)

   point.X = x_value

   point.Y = y_value

   return point

__esri_field_calculator_splitter__

pointMove( !Shape!, !X!, !Y! )

View solution in original post

0 Kudos
6 Replies
SiyangTeo
Occasional Contributor

I found a Python script that does the work, however, it can only shift when the XY fields are within the shapefiles, but does not work when reading from fields of a table join.

Anyone knows how to rectify this?

def pointMove(shape,x_value,y_value):

   point = shape.getPart(0)

   point.X = x_value

   point.Y = y_value

   return point

__esri_field_calculator_splitter__

pointMove( !Shape!, !X!, !Y! )

0 Kudos
curtvprice
MVP Esteemed Contributor

> it does not work in after I upgraded to ArcGIS 10.2.2.

You must have updated from 9.3. The field Calculator no longer supports VBA, it now only supports VBScript and Python. You must use Python to access geometry as of ArcGIS 10.0. There are shortcuts that work with the Python syntax (i.e. "SHAPE@" for the shape field). See the examples in the help‌.

> does not work when reading from fields of a table join.

If the X and Y were joined fields, you'd need to uses prefixes, see below. Is that not working?

If anyone was wondering, the text snippet we've been sharing here is a .cal file such as what you can write from the right click field / calculate field dialog box access from a table view in ArcMap. The "__esri_field_calculator_splitter__" line separates the code block from the Calculate Field expression.

Kind of an 8.x-ish interface ... but it is still supported.

def pointMove(shape,x_value,y_value):

   point = shape.getPart(0)

   point.X = x_value

   point.Y = y_value

   return point

__esri_field_calculator_splitter__

pointMove( !SHAPE@!, !jtable.X!, !jtable.Y! )

Posting Code blocks in the new GeoNet

SiyangTeo
Occasional Contributor

Hi,

Thanks for the reply.

Can I clarify; let's say my joined field names for X & Y are Temp.X_Point & Temp.Y_Point respectively (Temp being the joined layer/table name). Should the resulting expression be:

pointMove( !SHAPE@!, !jtable.Temp.X_Point!, !jtable.Temp.Y.Point! )

OR

pointMove( !SHAPE@!, !jtable.X_Point!, !jtable.Y.Point! )

I tried both but the field calculator was unable to execute.

Thanks!

0 Kudos
curtvprice
MVP Esteemed Contributor

jtable was my example table name:

pointMove( !SHAPE@!, !Temp.X_Point!, !Temp.Y_Point! )

0 Kudos
SiyangTeo
Occasional Contributor

Hi Price,

I have changed the join table field names.

However, there was an error message at the geoprocessing results window, saying "Invalid field SHAPE@".

After changing the shape field to the actual field name, another error appeared, saying "invalid syntax (<string>, line 1)".

Wonder how to rectify from here... would you know?

Thanks for spending time with this issue btw.

0 Kudos
curtvprice
MVP Esteemed Contributor

This thread continues (several months later) here:

Move point features based on XY values