I am trying to run my program and when i get to this part of the program
I get the error that global name 'Primary_OH is not defined. Can someone look at my code and tell what i need to do to fix this.
Thanks
Solved! Go to Solution.
geodatabase = "C:\gismaps\LusData.mdb"
dataset = "ElectricData"
feature_class = "PriOHElectricLineSegment"
#####
geodatabase = r"C:\gismaps\LusData.mdb"
You need to be careful when setting variables that include a path. I like to use the r (raw text): it takes care of any of the escaped slashes for you.
You might also consider using arcpy.env.workspace; it can help clean up logical paths as well...
Javier,
I get the following
Just as an experiment to get more familiar with the MakeFeatureLayer command, do you have a simpler feature class not in a feature dataset that you can try this command on just to see exactly how the syntax should be in a simpler working script?
A general question would also be if a complex edge type feature class as shown in screenshot above is supported with MakeFeatureLayer command?
Rick
Can you share the whole code please?
i ran the program and it worked as it should. When i ran it again it said that the layer already exists:
from tkinter import *
from tkinter import messagebox
import Tkinter, arcpy
myMap = arcpy.mapping.MapDocument("C:/GISMAPS/LUS Map.mxd")
#myMap = arcpy.mapping.MapDocument("CURRENT")
geodatabase = "C:\gismaps\LusData.mdb"
dataset = "ElectricData"
feature_class = "PriOHElectricLineSegment"
window = Tk()
window.title("Feeders Selection")
window.geometry("680x500")
window.iconbitmap('C:/GISMAPS/lus.ico')
#Define the frame for the feeders to be displayed
frame = Frame(window)
frame.grid(row=1, column=1, padx=15, pady=5)
listNodes = Listbox(frame, width=10, height=10, font=("Helvetica", 10))
listNodes.pack(side="left", fill="y")
#Define the scrollbar in frame window
scrollbar = Scrollbar(frame, orient="vertical")
scrollbar.config(command=listNodes.yview)
scrollbar.pack(side="right", fill="y")
listNodes.config(yscrollcommand=scrollbar.set)
#Add the selection of all feeders
for item in [2050, 2051, 2052,
3050, 3051, 3052, 3550,
4050, 4051, 4052,
5050, 5051, 5052]:
listNodes.insert(END, item)
feeder = listNodes.get(ACTIVE)
#Define all of the checkboxes
prioh_var = IntVar()
priug_var = IntVar()
secoh_var = IntVar()
secug_var = IntVar()
fuse_var = IntVar()
switch_var = IntVar()
sp_var = IntVar()
cap_var = IntVar()
rec_var = IntVar()
pinhook_var = IntVar()
mall_var = IntVar()
peck_var = IntVar()
pri_oh = Checkbutton(window, text = "Primary Overhead", variable = prioh_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=2, y=175)
pri_ug = Checkbutton(window, text = "Primary Underground", variable = priug_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=12, y=200)
sec_oh = Checkbutton(window, text = "Secondary Overhead", variable = secoh_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=8, y=225)
sec_ug = Checkbutton(window, text = "Secondary Underground", variable = secug_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=18, y=250)
fuse = Checkbutton(window, text = "Fuse", variable = fuse_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=400, y=10)
switch = Checkbutton(window, text = "Switch", variable = switch_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=405, y=35)
servicepoint = Checkbutton(window, text = "Service Point", variable = sp_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=420, y=88)
capacitor = Checkbutton(window, text = "Capacitor", variable = cap_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=412, y=60)
recloser = Checkbutton(window, text = "Recloser", variable = rec_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=409, y=87)
pinhook = Checkbutton(window, text = "Pinhook", variable = pinhook_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=175, y=15)
mall = Checkbutton(window, text = "Mall", variable = mall_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=165, y=45)
peck = Checkbutton(window, text = "Peck", variable = peck_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=165, y=75)
#Commit all of the options
def commit():
#my_label = IntVar()
feeder = listNodes.get(ACTIVE)
print feeder
#my_label = Label(window, text=prioh_var.get()).pack()
#check to see which inputs have been selected
if prioh_var.get() == 1:
print("pri_oh = 1")
# show primary overhead feeder where feederid = feeder -ie: [FEEDERID] = "2050"
print "Primary_OH"
Primary_OH_path = r"{0}\{1}\{2}".format(geodatabase, dataset, feature_class)
Primary_OH = arcpy.MakeFeatureLayer_management(Primary_OH_path, "Primary_OH_layer2")
else:
print("pri_oh = 0")
# do not display primary overhead feeder for [FEEDERID] = "2050"
if priug_var.get() == 1:
print("pri_ug = 1")
arcpy.SelectLayerByAttribute_management(Primary_UG, "", "[FEEDERID] = '2050'")
else:
print("pri_ug = 0")
Button(window, text="commit", command=commit).place(x=300, y=350)
#Quit the program
Button(window, text="Quit", command=window.destroy).place(x=400, y=350)
window.mainloop()
Javier,
when i run the program i put in some write statements:
from tkinter import *
from tkinter import messagebox
import Tkinter, arcpy
myMap = arcpy.mapping.MapDocument("C:/GISMAPS/LUS Map.mxd")
#myMap = arcpy.mapping.MapDocument("CURRENT")
geodatabase = "C:\gismaps\LusData.mdb"
dataset = "ElectricData"
feature_class = "PriOHElectricLineSegment"
window = Tk()
window.title("Feeders Selection")
window.geometry("680x500")
window.iconbitmap('C:/GISMAPS/lus.ico')
#Define the frame for the feeders to be displayed
frame = Frame(window)
frame.grid(row=1, column=1, padx=15, pady=5)
listNodes = Listbox(frame, width=10, height=10, font=("Helvetica", 10))
listNodes.pack(side="left", fill="y")
#Define the scrollbar in frame window
scrollbar = Scrollbar(frame, orient="vertical")
scrollbar.config(command=listNodes.yview)
scrollbar.pack(side="right", fill="y")
listNodes.config(yscrollcommand=scrollbar.set)
#Add the selection of all feeders
for item in [2050, 2051, 2052,
3050, 3051, 3052, 3550,
4050, 4051, 4052,
5050, 5051, 5052]:
listNodes.insert(END, item)
feeder = listNodes.get(ACTIVE)
#Define all of the checkboxes
prioh_var = IntVar()
priug_var = IntVar()
secoh_var = IntVar()
secug_var = IntVar()
fuse_var = IntVar()
switch_var = IntVar()
sp_var = IntVar()
cap_var = IntVar()
rec_var = IntVar()
pinhook_var = IntVar()
mall_var = IntVar()
peck_var = IntVar()
pri_oh = Checkbutton(window, text = "Primary Overhead", variable = prioh_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=2, y=175)
pri_ug = Checkbutton(window, text = "Primary Underground", variable = priug_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=12, y=200)
sec_oh = Checkbutton(window, text = "Secondary Overhead", variable = secoh_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=8, y=225)
sec_ug = Checkbutton(window, text = "Secondary Underground", variable = secug_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=18, y=250)
fuse = Checkbutton(window, text = "Fuse", variable = fuse_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=400, y=10)
switch = Checkbutton(window, text = "Switch", variable = switch_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=405, y=35)
servicepoint = Checkbutton(window, text = "Service Point", variable = sp_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=420, y=88)
capacitor = Checkbutton(window, text = "Capacitor", variable = cap_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=412, y=60)
recloser = Checkbutton(window, text = "Recloser", variable = rec_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=409, y=87)
pinhook = Checkbutton(window, text = "Pinhook", variable = pinhook_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=175, y=15)
mall = Checkbutton(window, text = "Mall", variable = mall_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=165, y=45)
peck = Checkbutton(window, text = "Peck", variable = peck_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=165, y=75)
#Commit all of the options
def commit():
#my_label = IntVar()
feeder = listNodes.get(ACTIVE)
print feeder
#my_label = Label(window, text=prioh_var.get()).pack()
#check to see which inputs have been selected
if prioh_var.get() == 1:
print("pri_oh = 1")
# show primary overhead feeder where feederid = feeder -ie: [FEEDERID] = "2050"
print "Primary_OH"
Primary_OH_path = r"{0}\{1}\{2}".format(geodatabase, dataset, feature_class)
Primary_OH = arcpy.MakeFeatureLayer_management(Primary_OH_path, "Primary_OH_layer2")
print Primary_OH
else:
print("pri_oh = 0")
# do not display primary overhead feeder for [FEEDERID] = "2050"
if priug_var.get() == 1:
print("pri_ug = 1")
arcpy.SelectLayerByAttribute_management(Primary_UG, "", "[FEEDERID] = '2050'")
else:
print("pri_ug = 0")
Button(window, text="commit", command=commit).place(x=300, y=350)
#Quit the program
Button(window, text="Quit", command=window.destroy).place(x=400, y=350)
window.mainloop()
for the print statement on line 83 the output is: Primary_OH_layer2. Should it show the feeder number which is 2050? Line 73 does print the correct number
Since the program gives an error when i run it a second time because the layer has been created, how do i modify the code so that this will not happen. Do i need to change the MakeFeatureLayer_management?
make feature layer and make table view both create the respective object in memory. If you run your tool more than once without exiting out of ArcMap, you'll get an error every time. (See this write up). If you need to get rid of the feature layer or table view you have a couple of choices. See this stack exchange post. You can use arcpy.Delete_managment or try arcpy.env.overwriteOutput = True at the top of the script. The former will delete it, while the the latter will overwrite it....
Javier,
I think i have confused myself looking at the code between lines 77 and 83. What i am trying to do is change the properties for the layer "Primary OH". When line 77 is a one I want the definition query to be set to [FEEDERID] = '2050' in the layer properties.This way only the primary overhead lines with a feeder id on 2050 will show on the screen. i will need to do this for approx 8 layers (primary ug, switches, fuses, etc....). I probably am missing something but i not sure how this code will do what i want.
Rick
You can use this example in order to set a Definition Query on a map layer.
- mxd = arcpy.mapping.MapDocument(r"C:/GISMAPS/LUS Map.mxd")
- layer_name = "Primary_OH"
- primary_oh = arcpy.mapping.ListLayers(mxd, layer_name)[0]
- primary_oh.definitionQuery = "[FEEDERID] = '2050'"
Javier,
I think i am pretty close to finishing this section. When i enter the code above i had to change the Primary_OH to Primary OH since that is how it is labeled in the properties for label name. I get an invalid syntax for line 88.
2 questions and i hope i won't have to bother you anymore. What do i need to fix line 88 and also how to i make 2050 in line 89 a variable. The variable would be "feeder".
I really appreciate your help fixing all of my errors.