import arcpy import pythonaddins class ToolClassRandom(object): """Implementation for GenerateRandomPoints_addin.tool (Tool)""" def __init__(self): self.enabled = True self.cursor = 3 self.shape = 'Rectangle' # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks. def onMouseDown(self, x, y, button, shift): pass def onMouseDownMap(self, x, y, button, shift): pass def onMouseUp(self, x, y, button, shift): pass def onMouseUpMap(self, x, y, button, shift): pass def onMouseMove(self, x, y, button, shift): pass def onMouseMoveMap(self, x, y, button, shift): pass def onDblClick(self): pass def onKeyDown(self, keycode, shift): pass def onKeyUp(self, keycode, shift): pass def deactivate(self): pass def onCircle(self, circle_geometry): pass def onLine(self, line_geometry): pass def onRectangle(self, rectangle_geometry): extent = rectangle_geometry arcpy.env.workspace = "c:\ArcpyBook\Ch11" if arcpy.Exists("randompts.shp"): arcpy.Delete_management("randompts.shp") randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,"randompts","randompts.shp","POINT",rectangle_geometry) arcpy.RefreshActiveView() return randompts
Been trying to run this code to create an addin for the Generate Random Points. It gave me the error that says:
Traceback (most recent call last):
File "C:\Users\Pollock\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{A6309427-3034-2E61-07C7-2A592C101F22}\GenerateRandomPoints_addin.py", line 41, in onRectangle
return randompts
UnboundLocalError: local variable 'randompts' referenced before assignment
Solved! Go to Solution.
What do you mean "not working"?
The following works for me:
def onRectangle(self, rectangle_geometry): arcpy.env.workspace = r"IN_MEMORY" arcpy.env.overwriteOutput = True lyrname = 'randompts' randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,lyrname,'',rectangle_geometry).getOutput(0) mxd=arcpy.mapping.MapDocument('CURRENT') adf = mxd.activeDataFrame try: lyr=arcpy.mapping.ListLayers(mxd, lyrname, adf)[0] except IndexError: lyr=arcpy.MakeFeatureLayer_management(randompts, lyrname).getOutput(0) arcpy.mapping.AddLayer(adf,lyr) lyr=arcpy.mapping.ListLayers(mxd, lyrname, adf)[0] arcpy.RefreshActiveView()
The third parameter of the line:
randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,"randompts","randompts.shp","POINT",rectangle_geometry)
... refers to the "constraining_feature_class" (Optional).
Random points will be generated inside or along the features in this feature class.
This featureclass should exist, but you just deleted it...
You mean I just deleted was because of the same name ? I am still struggled with it and trying to do many different method even I looked at the CreateRandomPoints help on arcgis 10.1.
If the shapefile doesn't exist, the "randompts = arcpy.CreateRandomPoints_management" line will never get called, so when you "return randompts", the randompts variable has not been defined and you will get the UnboundLocalError.
Do this instead:
def onRectangle(self, rectangle_geometry): extent = rectangle_geometry arcpy.env.workspace = "c:\ArcpyBook\Ch11" if arcpy.Exists("randompts.shp"): arcpy.Delete_management("randompts.shp") randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,"randompts","randompts.shp","POINT",rectangle_geometry) arcpy.RefreshActiveView() return randompts
Thanks. It is still not working though I do believe the Delete management had to do with it.
What I did was drag around a boundary and nothing comes out..so I modifty to see what happens and
this only works...
so I put number sign next to 04 and 05 . See here:
def onRectangle(self, rectangle_geometry): extent = rectangle_geometry arcpy.env.workspace = r"c:\ArcpyBook\Ch11" #if arcpy.Exists('randompts.shp'): #arcpy.Delete_management('randompts.shp') randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,'randompts.shp',"",rectangle_geometry) arcpy.RefreshActiveView() #return randompts pass
This was from the book I have on page 231 from Programming ArcGIS 10.1 with Python cookbook. I know probably it has change since then. The book was published in 2013.
What do you mean "not working"?
The following works for me:
def onRectangle(self, rectangle_geometry): arcpy.env.workspace = r"IN_MEMORY" arcpy.env.overwriteOutput = True lyrname = 'randompts' randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,lyrname,'',rectangle_geometry).getOutput(0) mxd=arcpy.mapping.MapDocument('CURRENT') adf = mxd.activeDataFrame try: lyr=arcpy.mapping.ListLayers(mxd, lyrname, adf)[0] except IndexError: lyr=arcpy.MakeFeatureLayer_management(randompts, lyrname).getOutput(0) arcpy.mapping.AddLayer(adf,lyr) lyr=arcpy.mapping.ListLayers(mxd, lyrname, adf)[0] arcpy.RefreshActiveView()
That code you provide worked ! Thanks !