How Can I Calculate the Length and Width of a Polygon Using ArcPy

7596
6
05-04-2014 04:02 PM
OLANIYANOLAKUNLE
Occasional Contributor II
How Can I Calculate the Length and Width of a Polygon Using ArcPy any suggestion(s). Thanks
Tags (2)
0 Kudos
6 Replies
T__WayneWhitley
Frequent Contributor
Well, it depends on the type of polygon and orientation - do you mean a rectangular polygon?  Minimum bounding geometry is one way, see this:

http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000003q000000

The graphic row for polygon shows the various geometry methods - check out RECTANGLE_BY_WIDTH, RECTANGLE_BY_AREA, and ENVELOPE.

Also, there's the convex hull property 'getHullRectangle' --- gives you a space-delimited coordinate pair string of the vertices.  You can use the distance formula to back out the height/width if not oriented 'orthogonally' to the x/y axes.

http://resources.arcgis.com/en/help/main/10.2/index.html#//018z00000061000000

Wayne

EDIT- Python has a math module with trig functions that will help too.
...specifically related to the distance formula:  math.hypot(x,y)
https://docs.python.org/2.7/library/math.html#trigonometric-functions
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks I got it
0 Kudos
ChrisSnyder
Regular Contributor III
Here's some v10.1+ code that calculates an "aspect ratio" in regards to a poly or line FC's hull rectangle (not the same as extent rectangle BTW)... The variables "distance1" and "distance2" are length and width (depending on which one is the longer distance):

arcpy.AddField_management(indxTileSinglePartFC, "ASP_RATIO", "DOUBLE")
updateRows = arcpy.da.UpdateCursor(indxTileSinglePartFC, ["SHAPE@","ASP_RATIO"])
for updateRow in updateRows:
    shapeObj = updateRow[0]
    x1,y1,x2,y2,x3,y3,x4,y4 = [float(coord) for coord in shapeObj.hullRectangle.split(" ")]
    distance1 = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
    distance2 = math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
    if distance1 <= distance2:
        updateRow[1] = distance2 / distance1
    else:
        updateRow[1] = distance1 / distance2
    updateRows.updateRow(updateRow)
del updateRow, updateRows
0 Kudos
T__WayneWhitley
Frequent Contributor
Nice Chris!- and incidentally (if I'm not mistaken), the 'distance' lines can be shortened to use the hypot function...

In other words:
distance1 = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)


...is equivalently:
distance1 = math.hypot((x1 - x2), (y1 - y2))



Wayne
0 Kudos
ChrisSnyder
Regular Contributor III
math.hypot... Cool - I'll have to use that from now on!
0 Kudos