POST
|
I am a beginner at Python coding and would require some help with ArcGIS Python Add ins. I have created a TOOL which is working fine. I want to modify my code so this TOOL is enable only when a condition is met (in my case when a single point is selected from a predefined layer). The TOOL become disable when selection count <> 1 I have try a few things but I cannot find the correct place where to put my code for testing of that condition. I tried in the def __init__(self): this works when the tool is loaded, but does work afterward when the selection is updated I tried to add a def onUpdate(self): to my TOOL class and modifying the config file (add onDemand="false") but that does not seem to do a difference , I am not sure if I can write such a function from this environment? Here is sample on my code: class SetDLA_TOOL(object): """Implementation for StructureEditing_V1_addin.tool (Tool)""" def __init__(self): self.enabled = True mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] # Get a count of black selected structure primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE"] rowCount1 = 0 cursor1 = None with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1: for row1 in cursor1: if row1[0] is None: rowCount1 = 0 else: rowCount1 = rowCount1 + 1 del row1, cursor1 print ("RowCount1 is " + str(rowCount1)) if rowCount1 == 1: self.enabled = True else: self.enabled = False self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks. #This works when the tool loads, but the enable/disabling of the tool is not updated afterward def onUpdate(self): #Not sure if this def is legit for a tool class? mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] # Get a count of black selected structure primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE"] rowCount1 = 0 cursor1 = None with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1: for row1 in cursor1: if row1[0] is None: rowCount1 = 0 else: rowCount1 = rowCount1 + 1 del row1, cursor1 print ("RowCount1 is " + str(rowCount1)) if rowCount1 == 1: self.enabled = True else: self.enabled = False def onMouseDownMap(self, X1, Y1, button, shift): #This is the body of the TOOL works fine but itself # Set variables mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] secSTR = arcpy.mapping.ListLayers(mapdoc,"Secondary Structure Symbol - Dip < 90 (BLACK)",dfSTR)[0] # Get a count of black selected structure primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_X","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_Y"] rowCount1 = 0 cursor1 = None with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1: for row1 in cursor1: if row1[0] is None: rowCount1 = 0 else: rowCount1 = rowCount1 + 1 if rowCount1 == 1: DLA = row1[0] X0 = row1[1] Y0 = row1[2] del row1, cursor1 secSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_DIP_LABEL_ANGLE","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_X","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_Y"] rowCount2 = 0 cursor2 = None with arcpy.da.SearchCursor(secSTR,secSTRfields) as cursor2: for row2 in cursor2: if row2[0] is None: rowCount2 = 0 else: rowCount2 = rowCount2 + 1 if rowCount2 == 1: DLA = row2[0] X0 = row2[1] Y0 = row2[2] del row2, cursor2 rowCountTotal = rowCount1 + rowCount2 print ("RowCount1 is " + str(rowCount1)) print ("RowCount2 is " + str(rowCount2)) print ("RowCountTotal is " + str(rowCountTotal)) if rowCountTotal <> 1: pythonaddins.MessageBox("You must have a single structure point selected to run this tool!","Warning") return if rowCountTotal == 1: self.enabled print("Current Dip label Angle is: " + str(DLA)) # Coordinates of the mouse click are: X1,Y1 # print("Your mouse click is at: " + str(X1) + " , " + str(Y1)) # pythonaddins.MessageBox("Your mouse clicked at " + str(x) + " , " + str(y),"My Coordinates:") # Calculate the angle between the structure symbol and the mouse click angleRADIAN = math.atan2(Y1-Y0, X1-X0) # Convert the RADIAN angle to DEGREES angle and then to GEOGRAPHIC angle angleDEGREES = math.degrees(angleRADIAN) angleGEO = round((450 - angleDEGREES )% 360) # Set the Dip label Angle to GEOGRAPHIC angle derived from the mouse click arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE", expression=angleGEO, expression_type="VB", code_block="") print("The dip label angle has been changed to: " + str(angleGEO)) arcpy.RefreshTOC() arcpy.RefreshActiveView() mapdoc.save() del angleRADIAN, angleDEGREES, angleGEO del DLA,X0,Y0 del rowCount1, rowCount2, rowCountTotal del dfSTR, primSTR, secSTR del mapdoc Thanks for your help Gen
... View more
06-25-2019
04:24 PM
|
0
|
0
|
334
|
POST
|
I am a beginner with Python coding and would required assistance with building Python Addin with TOOL class and utilising def onRectangle(self, rectangle_geometry) I have created a toolbar with Python Addin where I have added several button and tools. From the class SetINVISIBLE_TOOL(object): I want to use the def onRectangle(self, rectangle_geometry): so when the tool is used, the user must draw a rectangle to select a single point from a prescribed layer. If the selection count == 1; then the selected point will be set to invisible I have tried a few things but can not find a solution. One option the script run with no errors but nothing happen and no selection is made Second option raise an error. See below details of my code: class SetINVISIBLE_TOOL(object): """Implementation for StructureEditing_V1_addin.tool_2 (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 onRectangle(self, rectangle_geometry): """Occurs when the rectangle is drawn and the mouse button is released. The rectangle is a extent object.""" # Select points from the layer: Primary Structure Symbol (RED) which intersect with the extent rectangle mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] userExtent = rectangle_geometry #ext = dfSTR.extent blackprimSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] a = arcpy.Array() a.add(userExtent.lowerLeft) a.add(userExtent.lowerRight) a.add(userExtent.upperLeft) a.add(userExtent.upperRight) a.add(userExtent.lowerLeft) thepoly = arcpy.Polygon(a) thepolyLyr = arcpy.MakeFeatureLayer_management(thepoly) # boxAsFeature = arcpy.Polygon(arcpy.Array(rectangle_geometry)) not working # arcpy.SelectLayerByLocation_management(blackprimSTR, "Intersect", boxAsFeature, 0, "New_Selection") arcpy.SelectLayerByLocation_management(blackprimSTR,"INTERSECT",thepoly,"","NEW_SELECTION") #Option 1 tool: run but nothing happen arcpy.SelectLayerByLocation_management(blackprimSTR,"INTERSECT",thepolyLyr,"","NEW_SELECTION") #Option 2: Error is raise on this line arcpy.RefreshActiveView() # if selected point count == 1, run the rest of the script selCount = int(arcpy.GetCount_management(blackprimSTR)[0]) if selCount == 1: # Set the the select point to be invisisble i.e. calc OUTPUT_SCALE = 10 (and SECONDARY_OUTPUT_SCALE = 10 if necessary) if TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_OUTPUT_SCALE == 25: arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_OUTPUT_SCALE", expression="10", expression_type="VB", code_block="") arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.OUTPUT_SCALE", expression="10", expression_type="VB", code_block="") Your help is appreciated Gen
... View more
06-25-2019
04:18 PM
|
0
|
0
|
401
|
POST
|
I am a beginner at Python coding and would require some help with ArcGIS Python Add ins. I have created a TOOL which is working fine. I want to modify my code so this TOOL is enable only when a condition is met (in my case when a single point is selected from a predefined layer). The TOOL become disable when selection count <> 1 I have try a few things but I cannot find the correct place where to put my code for testing of that condition. I tried in the def __init__(self): this works when the tool is loaded, but does work afterward when the selection is updated I tried to add a def onUpdate(self): to my TOOL class and modifying the config file (add onDemand="false") but that does not seem to do a difference , I am not sure if I can write such a function from this environment? Here is sample on my code: class SetDLA_TOOL(object): """Implementation for StructureEditing_V1_addin.tool (Tool)""" def __init__(self): self.enabled = True mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] # Get a count of black selected structure primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE"] rowCount1 = 0 cursor1 = None with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1: for row1 in cursor1: if row1[0] is None: rowCount1 = 0 else: rowCount1 = rowCount1 + 1 del row1, cursor1 print ("RowCount1 is " + str(rowCount1)) if rowCount1 == 1: self.enabled = True else: self.enabled = False self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks. #This works when the tool loads, but the enable/disabling of the tool is not updated afterward def onUpdate(self): #Not sure if this def is legit for a tool class? mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] # Get a count of black selected structure primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE"] rowCount1 = 0 cursor1 = None with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1: for row1 in cursor1: if row1[0] is None: rowCount1 = 0 else: rowCount1 = rowCount1 + 1 del row1, cursor1 print ("RowCount1 is " + str(rowCount1)) if rowCount1 == 1: self.enabled = True else: self.enabled = False def onMouseDownMap(self, X1, Y1, button, shift): #This is the body of the TOOL works fine but itself # Set variables mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] secSTR = arcpy.mapping.ListLayers(mapdoc,"Secondary Structure Symbol - Dip < 90 (BLACK)",dfSTR)[0] # Get a count of black selected structure primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_X","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_Y"] rowCount1 = 0 cursor1 = None with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1: for row1 in cursor1: if row1[0] is None: rowCount1 = 0 else: rowCount1 = rowCount1 + 1 if rowCount1 == 1: DLA = row1[0] X0 = row1[1] Y0 = row1[2] del row1, cursor1 secSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_DIP_LABEL_ANGLE","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_X","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_Y"] rowCount2 = 0 cursor2 = None with arcpy.da.SearchCursor(secSTR,secSTRfields) as cursor2: for row2 in cursor2: if row2[0] is None: rowCount2 = 0 else: rowCount2 = rowCount2 + 1 if rowCount2 == 1: DLA = row2[0] X0 = row2[1] Y0 = row2[2] del row2, cursor2 rowCountTotal = rowCount1 + rowCount2 print ("RowCount1 is " + str(rowCount1)) print ("RowCount2 is " + str(rowCount2)) print ("RowCountTotal is " + str(rowCountTotal)) if rowCountTotal <> 1: pythonaddins.MessageBox("You must have a single structure point selected to run this tool!","Warning") return if rowCountTotal == 1: self.enabled print("Current Dip label Angle is: " + str(DLA)) # Coordinates of the mouse click are: X1,Y1 # print("Your mouse click is at: " + str(X1) + " , " + str(Y1)) # pythonaddins.MessageBox("Your mouse clicked at " + str(x) + " , " + str(y),"My Coordinates:") # Calculate the angle between the structure symbol and the mouse click angleRADIAN = math.atan2(Y1-Y0, X1-X0) # Convert the RADIAN angle to DEGREES angle and then to GEOGRAPHIC angle angleDEGREES = math.degrees(angleRADIAN) angleGEO = round((450 - angleDEGREES )% 360) # Set the Dip label Angle to GEOGRAPHIC angle derived from the mouse click arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE", expression=angleGEO, expression_type="VB", code_block="") print("The dip label angle has been changed to: " + str(angleGEO)) arcpy.RefreshTOC() arcpy.RefreshActiveView() mapdoc.save() del angleRADIAN, angleDEGREES, angleGEO del DLA,X0,Y0 del rowCount1, rowCount2, rowCountTotal del dfSTR, primSTR, secSTR del mapdoc Thnaks for your help Gen
... View more
06-24-2019
07:17 PM
|
0
|
0
|
448
|
POST
|
I am a beginner with Python coding and would required assistance with building Python Addin with TOOL class and utilising def onRectangle(self, rectangle_geometry) I have created a toolbar with Python Addin where I have added several button and tools. From the class SetINVISIBLE_TOOL(object): I want to use the def onRectangle(self, rectangle_geometry): so when the tool is used, the user must draw a rectangle to select a single point from a prescribed layer. If the selection count == 1; then the selected point will be set to invisible I have tried a few things but can not find a solution. One option the script run with no errors but nothing happen and no selection is made Second option raise an error. See below details of my code: class SetINVISIBLE_TOOL(object): """Implementation for StructureEditing_V1_addin.tool_2 (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 onRectangle(self, rectangle_geometry): """Occurs when the rectangle is drawn and the mouse button is released. The rectangle is a extent object.""" # Select points from the layer: Primary Structure Symbol (RED) which intersect with the extent rectangle mapdoc = arcpy.mapping.MapDocument("CURRENT") dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0] userExtent = rectangle_geometry #ext = dfSTR.extent blackprimSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0] a = arcpy.Array() a.add(userExtent.lowerLeft) a.add(userExtent.lowerRight) a.add(userExtent.upperLeft) a.add(userExtent.upperRight) a.add(userExtent.lowerLeft) thepoly = arcpy.Polygon(a) thepolyLyr = arcpy.MakeFeatureLayer_management(thepoly) # boxAsFeature = arcpy.Polygon(arcpy.Array(rectangle_geometry)) not working # arcpy.SelectLayerByLocation_management(blackprimSTR, "Intersect", boxAsFeature, 0, "New_Selection") arcpy.SelectLayerByLocation_management(blackprimSTR,"INTERSECT",thepoly,"","NEW_SELECTION") #Option 1 tool: run but nothing happen arcpy.SelectLayerByLocation_management(blackprimSTR,"INTERSECT",thepolyLyr,"","NEW_SELECTION") #Option 2: Error is raise on this line arcpy.RefreshActiveView() # if selected point count == 1, run the rest of the script selCount = int(arcpy.GetCount_management(blackprimSTR)[0]) if selCount == 1: # Set the the select point to be invisisble i.e. calc OUTPUT_SCALE = 10 (and SECONDARY_OUTPUT_SCALE = 10 if necessary) if TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_OUTPUT_SCALE == 25: arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_OUTPUT_SCALE", expression="10", expression_type="VB", code_block="") arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.OUTPUT_SCALE", expression="10", expression_type="VB", code_block="") Your help is appreciated Gen
... View more
06-24-2019
06:52 PM
|
0
|
0
|
512
|
Online Status |
Offline
|
Date Last Visited |
02-03-2023
03:28 AM
|