Codes for"select features by rectangle" don't work

313
0
01-18-2013 09:05 AM
babakkasraei
New Contributor II
I have created the following codes to select some parts of a layer by drawing a rectangle, but they don't work when I push button 1 on my tekinter GUI.


def button1Click(self, selection):
        def __init__(self):
           self.enabled = True
           self.cursor = 3
           self.shape = 'Rectangle'
        def onRectangle(self, rectangle_geometry):
           # Import adds in modules
           import pythonaddins
           # Defind the extent
           extent = rectangle_geometry
           
           # After you get your extent, to select the features in a layer named Layer intersecting the extent rectangle, do this:
           mxd = arcpy.mapping.MapDocument("CURRENT")
           df = mxd.activeDataFrame
           ext = df.extent
           # Selection from the listbox      
           index = self.listbox.curselection()               
           label = self.listbox.get(index) 

           # Local variables:
           layer = labal

           #Creates a polygon object
           a = arcpy.Array()
           a.add(ext.lowerLeft)
           a.add(ext.lowerRight)
           a.add(ext.upperRight)
           a.add(ext.upperLeft)
           a.add(ext.lowerLeft)
           thepoly = arcpy.Polygon(a)
          
    # Process: Select Layer By Location

           arcpy.SelectLayerByLocation_management(layer, "Intersect", thepoly, "", "NEW_SELECTION")



The whole codes for creating a Tekinter GUI is as follows. This script must be added to ArcMAP as a script tool and it works with any current map. It lists the layers and does some functions by choosing the buttons.


# Import modules
import sys,os,math,string,arcpy

# Import adds in modules
import pythonaddins

# Import Tkinter modules
from Tkinter import *
import tkMessageBox

# This class will create a tekinter GUI with widgets
class ScrolledList(Frame):
    def __init__(self, options, parent=None):
        Frame.__init__(self, parent)
        self.pack(expand=YES, fill=BOTH)                  
        
        self.myParent=self
        sbar = Scrollbar(self)

        # This button is for exit
        self.button5 = Button(self, text="exit",background="gray")
        self.button5.pack(side=BOTTOM, padx=10, pady=1)
        self.button5.bind("<Button-1>", self.button5Click)

        # This button will show only the selected features
        self.button3 = Button(self, text=" Show only Selected features",background="yellow")
        self.button3.pack(side = BOTTOM, padx=100, pady=1)
        self.button3.bind("<Button-1>", self.button3Click)
        
        # This button will show all features
        self.button4 = Button(self, text=" Show All Features", background="BROWN")
        self.button4.pack(side=BOTTOM, padx=100,pady=1)
        
        
        # This button will clear selection
        self.button2 = Button(self, text=" Clear selection", background="red")
        self.button2.pack(side=BOTTOM, padx=100, pady=1)
        self.button2.bind("<Button-1>", self.button2Click)
        
        # This button will pick features with drawing a rectangle on the map view 
        self.button1 = Button(self, text=" Pick Features",background="green")
        self.button1.pack(side = BOTTOM, padx=100, pady=1)
        self.button1.bind("<Button-1>", self.button1Click)

        # This listBox will show the contents of TOC and map layers
        list = Listbox(self, relief=SUNKEN)
        sbar.config(command=list.yview)                   
        list.config(yscrollcommand=sbar.set)              
        sbar.pack(side=RIGHT, fill=Y)                     
        list.pack(side=LEFT, expand=YES, fill=BOTH)

        # Read from current map
        mxd=arcpy.mapping.MapDocument("CURRENT")
        options = arcpy.mapping.ListLayers(mxd)

        # This loop will go through each item in the TOC to list them
        count = 0
        for label in options:                             
            list.insert(count, label)                      
            count += 1
        list.bind('<Double-1>', self.handleList)          
        self.listbox = list
        # This function will create a selection
    def handleList(self, event):
        
        index = self.listbox.curselection()               
        label = self.listbox.get(index)                   
        self.runCommand(label)
        

        # This function will add a label to the Tekinter GUI to show what is selected
                           
    def runCommand(self, selection):                      
        Label(text="You selected : "+selection).pack(side=BOTTOM,padx=10,pady=10)

        # This function will choose some part of a layer using a rectangle
    
    def button1Click(self, selection):
        def __init__(self):
           self.enabled = True
           self.cursor = 3
           self.shape = 'Rectangle'
        def onRectangle(self, rectangle_geometry):
           # Import adds in modules
           import pythonaddins
           # Defind the extent
           extent = rectangle_geometry
           
           # After you get your extent, to select the features in a layer named Layer intersecting the extent rectangle, do this:
           mxd = arcpy.mapping.MapDocument("CURRENT")
           df = mxd.activeDataFrame
           ext = df.extent
           # Selection from the listbox      
           index = self.listbox.curselection()               
           label = self.listbox.get(index) 

           # Local variables:
           layer = labal

           #Creates a polygon object
           a = arcpy.Array()
           a.add(ext.lowerLeft)
           a.add(ext.lowerRight)
           a.add(ext.upperRight)
           a.add(ext.upperLeft)
           a.add(ext.lowerLeft)
           thepoly = arcpy.Polygon(a)
          
    # Process: Select Layer By Location

           arcpy.SelectLayerByLocation_management(layer, "Intersect", thepoly, "", "NEW_SELECTION")


    def button2Click(self, selection):
        index = self.listbox.curselection()               
        label = self.listbox.get(index) 

        # Local variables:
        layer = label

        # Process: deSelect Layer By Location
        arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")
         # This function will close the GUI


    def button3Click(self, selection):
        # Search to see there is any selection or not
        selectcount= int(arcpy.GetCount_management(layer).getOutput(0))
    def button5Click(self, event):
        root.destroy()
        
        
root = Tk()
# Title of the tekinter

root.title('Theme Management')

# Label of the listbox
Label(text="Select and Unselect TOC features").pack(side=TOP,padx=10,pady=10)
options = map((lambda x: str(x)), range(20))
ScrolledList(options).mainloop()




I will appreciate it if somebody debug my codes please.

Best regards

Babak
Tags (2)
0 Kudos
0 Replies