Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
Could you post links to your translation attempts? – R.K. Jul 17 '12 at 10:28
up vote 4 down vote accepted

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).

share|improve this answer
Thank you very much for your help! – Jordan Walker Jul 17 '12 at 14:58
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. – Jordan Walker Jul 17 '12 at 15:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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