babakkasraie

Select a Feature by Rectangle

Discussion created by babakkasraie on Jan 10, 2013
Latest reply on Jan 12, 2013 by babakkasraie
Hi

I have created a Python Tekinter GUI with a listbox to show the map layers and some button widgets.
I have to bind a "Select Features by Rectangle" function to the "Select Feature(button "Button 1) to be able to select some parts of a layer which is inside or intersect with rectangle. 
My Problem
If I wanted to use "Select a Layer by Location", it would be easy, but for "Select Features by Rectangle" tool I couldn't find a Python function. I may do three things.
1.       Use the Python function for it.
2.       Write codes to create a tracker rectangle on the map.
3.       Find a way to turn the tool on in ArcMAP by Python codes and use it on the map.
I tried all of these ways but I was not successful.
Now I need help please. Would you help me to write the codes for it.
Following this message you can see my script. It works with any maps. You need to add it as a tool in ArcMap to read the current map.
Best Regards
Babak Kasraei

# Import arcpy mosules
# application name: MWQM_List_Tk.py
# Date:
# created by: Babak Kasraei
# Description: This script will create a python GUI with buttons and a listbox.
# The listBox will show the layers. The buttons will choose a function.

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

# 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=100, pady=10)
        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=10)
       
       
        # This button will show all features
        self.button4 = Button(self, text=" Show All Features", background="BROWN")
        self.button4.pack(side=BOTTOM, padx=100,pady=10)
       
       
        # This button will clear selection
        self.button2 = Button(self, text=" Clear selection", background="red")
        self.button2.pack(side=BOTTOM, padx=100, pady=10)
       
       
        # 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=10)
        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):
       
        index = self.listbox.curselection()              
        label = self.listbox.get(index)

        # Local variables:
        layer = label

        # Process: Select Layer By Location
        arcpy.SelectLayerByLocation_management(layer, "", "", "", "NEW_SELECTION")


        # This function will close the GUI
    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()

Outcomes