I need help from someone to translate a small piece of code from VBA to Python, due to the fact that the company I am working for has upgraded from ArcGIS 9.3.1 to 10.0 and VBA is no longer supported. The code block is as follows:

static x0 as double, y0 as double  
dim pPoint as IPoint  
set pPoint = [Shape]  
x = pPoint.X   
y = pPoint.Y  
d = sqr((x-x0)^2 + (y-y0)^2)    
x0 = x   
y0= y

This is used with the calculate field tool inside model builder and as far as I can tell retrieves info about two points and calculates the difference in distance between them. I have attempted a few translations on my own with little success using !shape.firstpoint.x!, !shape.firstpoint.y!

Unfortunately my knowledge is lacking and I need some assistance. If you are able to help, that would be greatly appreciated!

  • Could you post links to your translation attempts?
    – R.K.
    Jul 17 '12 at 10:28

The following should do it (place it in the code block section):

def calc_distance(shape, x0, y0):
    point = shape.getPart(0)
    X = point.X
    Y = point.Y 
    d = math.sqrt(pow(X-x0,2) + pow(Y-y0,2)) 
    return d

The Expression should be:

calc_distance(!SHAPE!, 10000, 10000)

However, you'll need to pass the second X and Y you're comparing from in place of the two 10,000 numbers I've put into the expression. Its not clear in your VBA where you're getting those two coordinates from (they may just be 0).

  • I am using shapefiles with a few thousand points each, they are GPS data from cattle collars. I presume that it uses the FID of the points and just retrieves the coordinates in order (eg. distance between point 1 & 2, 2 & 3, 3 & 4... etc). Unfortunately I did not create the original model or write the code, so I am trying to comprehend it as best as I can myself. This code block is just a small part of a model that creates lots of fields in an attribute table and then populates them with the 'calculate field' tool. I will try your code today and see where I can get. Thanks again. Jul 17 '12 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.