-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain_menu.py
More file actions
129 lines (107 loc) · 4.59 KB
/
main_menu.py
File metadata and controls
129 lines (107 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Displaying shapefiles (point, polyline and polygons) in a GUI, capable of rendering maps based on
attribute values, and showing basic attribute statistics including the mean, the standard deviation,
the skewness, and the kurtosis
"""
from tkinter import Tk,Menu,constants,filedialog
import shp_reader
import dbfload as dbf
from main_canvas import MainCanvas
from scipy import stats
import math
class MainApp(object):
"""
The MainApp based on TKinter
Attributes
----------
root : Tk
The Tk object
dbfdata : dictionary
The current dbf data
menubar : Menu
The menu bar
attibmenu : Menu
The attribute menu
"""
def __init__(self):
self.root = Tk()
self.root.geometry("300x50")
self.root.title("Shape file Reader")
self.createMenu()
self.root.mainloop()
def createMenu(self):
"""
Creates GUI components and register events
"""
self.menubar = Menu(self.root)
self.dbfdata = None
filemenu = Menu(self.menubar, tearoff=0)
filemenu.add_command(label="Open", command=self.__openShpfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.root.quit)
self.menubar.add_cascade(label="File", menu=filemenu)
self.attibmenu = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Attibutes", menu=self.attibmenu,state='disabled')
self.root.config(menu=self.menubar)
def __openShpfile(self):
"""
Open a shapefile and read in the contents, pop up the attribute menu
with the attributes of the shapefile
"""
print("open shape file!")
directory=filedialog.askopenfilename(filetypes=[("SHAPE_FILE","*.shp")])
print(directory)
if directory == "":
return
self.shapes, self.shp_type, self.bbox = shp_reader.read_shp(directory)
#read corresponding dbf data
dbfFile = dbf.DbfLoader(directory[:-3] + "dbf")
t = dbfFile.table2list()
varNames = dbfFile.get_field_names()
variables = {}
for variable in varNames:
variables[variable] = [record[varNames.index(variable)] for record in t]
if self.dbfdata!=None:
self.attibmenu.delete(0, len(self.dbfdata)-1)
#add attributes into menu
for key in list(variables.keys()):
self.__addAttribute(key)
self.dbfdata = variables
self.menubar.entryconfig(2, state=constants.NORMAL)
def __addAttribute(self,attributeName):
"""
Add an attribute to the menu
"""
self.attibmenu.add_command(label=attributeName, command=lambda i=attributeName:self.__updateCanvas(i))
def __updateCanvas(self, attributeName):
"""
Updates the canvas and showing statistical information
"""
print(("update Canvas ", attributeName))
data_list=self.dbfdata[attributeName]
print(("attribute values: ", data_list))
try:
n, min_max, mean, var, skew, kurt = stats.describe(data_list)
print("============================")
print("attribute statistics\n")
print(("Number of units: {0:d}".format(n)))
print(("Minimum: {0:8.6f} Maximum: {1:8.6f}".format(min_max[0], min_max[1])))
print(("Mean: {0:8.6f}".format(mean)))
print(("Standard deviation: {0:8.6f}".format(math.sqrt(var))))
print(("Skew : {0:8.6f}".format(skew)))
print(("Kurtosis: {0:8.6f}".format(kurt)))
print("\n============================")
high=max(data_list)
low=min(data_list)
dif=float(high-low)
for i in range(len(data_list)):
#map colors to 0-200, 0-200, 0-200 (avoid pure white for display purpose)
index=float(data_list[i]-low)/dif*200
index=str(hex(200-int(index)).split('x')[1])
color="#"+index+index+index
self.shapes[i].color=color
except:
print("non-numeric attribute")
self.canvas=MainCanvas(self.shapes,self.bbox,self.shp_type,self.root,attributeName,data_list)
if __name__ == '__main__':
MainApp()