3

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!

Chad Cooper
12.8k4 gold badges48 silver badges87 bronze badges
asked Jul 16, 2012 at 21:02
1
  • Could you post links to your translation attempts? Commented Jul 17, 2012 at 10:28

1 Answer 1

4

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

answered Jul 17, 2012 at 10:37
1
  • 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. Commented Jul 17, 2012 at 15:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.