forked from mishnit/housing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
executable file
·96 lines (79 loc) · 2.03 KB
/
map.py
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
from pylab import *
import MySQLdb
import random
# Constants.
MAX_CIRCLE_SIZE = 100
MIN_CIRCLE_SIZE = 5
def randomColor():
result = '%x' % random.randint(0, 16777215)
if len(result) == 5:
return '#0'+result
elif len(result) == 4:
return '#00'+result
return '#'+result
def normalize(value,maxValue,minValue,maxTarget,minTarget):
'''
Normalizes the given values with respect to the target.
'''
return float(maxTarget)*(float(value)-float(minValue))/(float(maxValue) - float(minValue)) + float(minTarget)
def plotData(minValue,maxValue,connection,query):
# Query for the data.
cursor = connection.cursor()
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
# Generate the plot.
for result in results:
(x,y,value) = result
color = randomColor()
size = normalize(value,maxValue,minValue,MAX_CIRCLE_SIZE,MIN_CIRCLE_SIZE)
scatter(x,y, c=color, s=size, alpha=0.3, edgecolors=None)
def getLimits(connection):
'''
Returns the max, min of the list price.
'''
# Create a cursor.
cursor = connection.cursor()
# Query for the data.
query = '''\
select max(list),
min(list)
from houses
where list is not null
and latitude is not null
and longitude is not null
'''
cursor.execute(query)
(maxValue, minValue) = cursor.fetchone();
# Close the cursor.
cursor.close()
# Return the values.
return (maxValue, minValue)
def main():
# Connect to the database.
connection = MySQLdb.connect (host = "localhost",
user = "root",
passwd = "80017001",
db = "housing")
# Get the limits of the list price.
maxValue, minValue = getLimits(connection)
# Load the data from the database.
for year in range(1995,2012):
query = '''
select latitude,
longitude,
list
from houses
where list is not null
and latitude is not null
and longitude is not null
and listdate between MAKEDATE(%s,1) and MAKEDATE(%s,1)
''' % (year, year+1)
plotData(minValue,maxValue,connection,query)
# Plot the data.
grid(True)
show()
# Close the connection.
connection.close ()
if __name__ == '__main__':
main()