Python Add-In Q: Polygons

302
1
Jump to solution
05-07-2013 07:14 AM
JamesCrandall
MVP Frequent Contributor
I need to replicate the Select By Polygon tool (minus the selecting step).  I simply need to be able to draw a temporary polygon to 'do stuff' with.  What am I missing here? --- I see a way to set self.shape to Line, Circle or Rectanlge but there is no Polygon.

I can set self.shape to "Line" and then create an array of points in the onLine def, but this is not what the SelectByPolygon tool is doing.

Please help with this seemingly simple process. 

Thanks!
j
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
I'm not completely satisfied with this solution, but it accomplishes what I need --- return the area, acreage for the shape that is drawn in the map display. 

1.  I Need to add some spatial reference checks on the DataFrame.
2.  The <self.shape = "Line"> is not quite doing what I want (interactively draw a polygon). 
3.  This is drawing a line, I'd rather it draw a polygon (as you click each vertex, the polygon begins to take shape, just like in the SelectByPolygon tool).  As it stands, I can tell that the user will be confused on how to close the polygon (really it should only take 4 clicks to create a simple rectangle/polygon, but because it is a line the user will want to "close" the polygon and click that 5th vertex).

How about interactively creaing a polygon graphic element?  I don't see how this can be converted to such a thing.

Anyway -- this returns the correct acreage when the DataFrame's coordinate system properties are correctly set:

 class ToolClass_AcresByClicks(object):          """Implementation for EOCTools_addin.tool (Tool)"""     def __init__(self):         self.enabled = True         self.shape = "Line"       def onLine(self, line_geometry):         array = arcpy.Array()         part = line_geometry.getPart(0)         for pt in part:            array.add(pt)                     t_polygon = arcpy.Polygon(array)         parea = t_polygon.area         p_acreage = parea / 43560         print p_acreage 

View solution in original post

0 Kudos
1 Reply
JamesCrandall
MVP Frequent Contributor
I'm not completely satisfied with this solution, but it accomplishes what I need --- return the area, acreage for the shape that is drawn in the map display. 

1.  I Need to add some spatial reference checks on the DataFrame.
2.  The <self.shape = "Line"> is not quite doing what I want (interactively draw a polygon). 
3.  This is drawing a line, I'd rather it draw a polygon (as you click each vertex, the polygon begins to take shape, just like in the SelectByPolygon tool).  As it stands, I can tell that the user will be confused on how to close the polygon (really it should only take 4 clicks to create a simple rectangle/polygon, but because it is a line the user will want to "close" the polygon and click that 5th vertex).

How about interactively creaing a polygon graphic element?  I don't see how this can be converted to such a thing.

Anyway -- this returns the correct acreage when the DataFrame's coordinate system properties are correctly set:

 class ToolClass_AcresByClicks(object):          """Implementation for EOCTools_addin.tool (Tool)"""     def __init__(self):         self.enabled = True         self.shape = "Line"       def onLine(self, line_geometry):         array = arcpy.Array()         part = line_geometry.getPart(0)         for pt in part:            array.add(pt)                     t_polygon = arcpy.Polygon(array)         parea = t_polygon.area         p_acreage = parea / 43560         print p_acreage 
0 Kudos