-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipMapper.py
157 lines (129 loc) · 4.07 KB
/
zipMapper.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import folium
import pandas as pd
import os
import pyodbc
import configparser
Config = configparser.ConfigParser()
Config.read(".\config.ini")
dbMode = Config.getboolean('mode', 'dbmode')
if dbMode:
dbCounties = []
dbZip = []
driver = Config.get('db', 'driver')
server = Config.get('db', 'server')
database = Config.get('db', 'database')
uid = Config.get('db', 'uid')
pwd = Config.get('db', 'pwd')
con = pyodbc.connect(driver=driver, server=server, database=database, uid=uid, pwd=pwd)
cur = con.cursor()
db_cmd = Config.get('db', 'cmd')
cur = cur.execute(db_cmd)
for r in cur:
dbCounties.append(r[1])
dbZip.append(r[2])
else:
csvZipsToDraw = Config.get('files', 'csvZipsToDraw')
countiesToDraw = Config.get('files', 'countiesToDraw')
df = pd.read_csv(csvZipsToDraw, na_values=[''])
df.set_index('index', inplace=True)
dfC = pd.read_csv(countiesToDraw, na_values=[''])
doCounty = Config.getboolean('styles', 'doCounty')
doSimple = Config.getboolean('styles', 'doSimple')
doZip = Config.getboolean('styles', 'doZip')
if doCounty:
geoDirCounty = Config.get('files', 'geoDirCounty')
if doSimple:
geoDirSimple = Config.get('files', 'geoDirSimple')
if doZip:
geoDir = Config.get('files', 'geoDir')
if not doCounty and not doSimple and not doZip:
exit("no style selected")
def parseGeoJson(directory, map, style):
global file, stateTag
files = os.listdir(directory)
if style == "county":
for file in files:
geoJson = directory + file
folium.GeoJson(
geoJson,
name='geojson',
style_function=style_functionCounty
).add_to(map)
if (style == "zip" ) or (style == "simple"):
for file in files:
geoJson = directory + file
folium.GeoJson(
geoJson,
name='geojson',
style_function=style_function
).add_to(map)
def style_function(feature):
properties = feature['properties']
featureZip= properties['ZCTA5CE10']
if dbMode:
zip = dbZip
else:
zip = df.values
if str(featureZip) in str(zip):
return {
'fillOpacity': .5,
'weight': 0.4,
'fillColor': 'red'
}
else:
return {
'fillOpacity': 0.0,
'weight': 0,
'fillColor': ''
}
def style_functionCounty(feature):
if dbMode:
counties = dbCounties
else:
counties = dfC['County'].tolist()
properties = feature['properties']
featureCounty= properties['NAME10']
stateFP= properties['STATEFP10']
def iterate():
for item in counties:
if str(checkFeature) == item:
return {
'fillOpacity': .5,
'weight': 0.4,
'fillColor': 'red'
}
else:
return {
'fillOpacity': 0.0,
'weight': 0,
'fillColor': ''
}
if stateFP == "51":
stateTag = "VA"
checkFeature = str(featureCounty) + ' ' + stateTag
style = iterate()
return style
if stateFP == "45":
stateTag = "SC"
checkFeature = str(featureCounty) + ' ' + stateTag
style = iterate()
return style
if stateFP == "37":
stateTag = "NC"
checkFeature = str(featureCounty) + ' ' + stateTag
style = iterate()
return style
def drawMap():
if doZip:
m = folium.Map(location=[35.8, -78.64], tiles='Mapbox Bright', prefer_canvas=True, zoom_start=8)
parseGeoJson(geoDir, m, 'zip')
m.save('map.html')
if doSimple:
s = folium.Map(location=[35.8, -78.64], tiles='Mapbox Bright', prefer_canvas=True, zoom_start=8)
parseGeoJson(geoDirSimple, s, 'simple')
s.save('mapSimple.html')
if doCounty:
c = folium.Map(location=[35.8, -78.64], tiles='Mapbox Bright', prefer_canvas=True, zoom_start=8)
parseGeoJson(geoDirCounty, c, 'county')
c.save('mapCounty.html')
drawMap()