I'm trying to develop a function which will find the longest straight line length that can be drawn within any of the polygons in a feature class. (To keep things simple, I'm only including lines that can be drawn between any two vertices of a polygon.)
My logic so far, is (for each polygon):
- create Polyline geometries between all possible pairs of vertices of the polygon
- for each Polyline created, test if it is fully within the polygon (eg, does not cross the boundary of a concave part of the polygon)
- if it is fully within the polgyon and it's length is the largest found so far, update the maximum length variable.
Unfortunately, my arcpy line.within(poly)
always returns false, even though the lines are created from pairs of points of that polygon.
I've commented out the test of line length to ignore that for now, but the "WITHIN" message (below) is never printed and the function always returns 0
(zero).
What I'm doing wrong in this code, below?
I can confirm that the function does create over 72,000 polylines for my test polygon, and that they all have realistic looking (greater than zero) lengths. (Confirmed by using arcpy.AddMessage()
for these values.)
def PolyLength(polyFC):
maxLength = 0
with arcpy.da.SearchCursor(polyFC, ["SHAPE@"]) as cur:
for row in cur:
poly = row[0]
featureMaxLength = 0
points = []
lines = []
for part in poly:
points.extend(part)
arcpy.AddMessage("Points count: {}".format(len(points)))
for a in points:
for b in points:
if not a.equals(b):
lines.append(arcpy.Polyline(arcpy.Array([a,b])))
arcpy.AddMessage("Lines count: {}".format(len(lines)))
for line in lines:
#if line.length > featureMaxLength and line.within(poly):
if line.within(poly):
featureMaxLength = line.length
arcpy.AddMessage("WITHIN and greater length: {}".format(line.length))
else:
arcpy.AddMessage("NOT WITHIN or NOT greater length: {}".format(line.length))
if featureMaxLength > maxLength:
maxLength = featureMaxLength
return maxLength