Select to view content in your preferred language

Common Python geometry calculations in the Field Calculator

20019
11
02-17-2011 12:59 PM
wilfredwaters
New Contributor III
Here it has Common VBA geometry calculations in the Field Calculator, provide as a number of code blocks. I have pasted these below and am wondering if someone can make a set of equivalent code blocks in Python, or can point me where someone has already made them?

Common VBA geometry calculations in the Field Calculator
Below are some code samples you can use to perform geometric calculations with VBA statements, rather than having the Calculate Geometry command do them for you.
To use the samples, open the Field Calculator, check Advanced, and type the VBA statement in the first text box. Then, type the variable name in the text box directly under the field name.

Area

Dim dblArea as double
Dim pArea as IArea
Set pArea = [shape]
dblArea = pArea.area

Perimeter

Dim dblPerimeter as double
Dim pCurve as ICurve
Set pCurve = [shape]
dblPerimeter = pCurve.Length

Length

Dim dblLength as double
Dim pCurve as ICurve
Set pCurve = [shape]
dblLength = pCurve.Length

X-coordinate of a point

Dim dblX As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblX = pPoint.X

Y-coordinate of a point

Dim dblY As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblY = pPoint.Y

X-coordinate of a polygon centroid

Dim dblX As Double
Dim pArea As IArea
Set pArea = [Shape]
dblX = pArea.Centroid.X

Y-coordinate of a polygon centroid

Dim dblY As Double
Dim pArea As IArea
Set pArea = [Shape]
dblY = pArea.Centroid.Y

To add the z-value of the start point of a polyline

Dim dblZ As Double
Dim pLine As IPolyline
Dim pPoint as IPoint
Set pLine = [Shape]
Set pPoint = pLine.FromPoint
dblZ= pPoint.Z
Tags (2)
11 Replies
NeilAyres
MVP Alum

Maybe I am misunderstanding the problem here.

But if you want to add a couple of columns containing XY's for a different coord sys than the data itself just add the columns (double of course), change the coordinate system of the data frame to your desired output. Then right click on the column, pick the XY value, and choose "use cords sys of the data frame".

Easy.

RichardFairhurst
MVP Honored Contributor

I am finding that cursors and dictionaries are now reducing my time doing field calculations by factors of nearly 16 times the speed.  As a result, I now have determined that my time will be better spent abandoning all Field and Geometry Calculator operations to write my own set of tools to replace them with an interface that configures cursors and dictionaries.

My latest script rewrite replaced 11 field calculations and one summary statistics operation with cursors and dictionary operations on 800K records and reduced the script time from 63 minutes to 4 minutes.  As a result, I simply can no longer justify the amount of my time the Field Calculator and Geometry Calculator will waste if I continue using them on any large record sets, no matter how easy they are to set up.  Now that I can design high performing tools of my own I soon will no longer ever have to fall asleep again at my computer while I tie up my ArcGIS Desktop waiting on calculations to complete.  I can't wait.