-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrunctionsking.py
98 lines (73 loc) · 2.65 KB
/
trunctionsking.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
#CSci 127 Teaching Staff
#October 2017
#A template for a program that finds & marks closest point.
#Modified by: ADD YOUR NAME HERE
#CSci 127 Teaching Staff
#October 2017
#A template for a program that finds & marks closest point.
#Modified by: ADD YOUR NAME HERE
import folium
import pandas as pd
def getData():
"""
Asks the user for the name of the CSV and
Returns a dataframe of the contents.
"""
csv_file = input("Enter the CSV file: ")
df = pd.read_csv(csv_file)
return df
def getColumnNames():
"""
Asks the user for the exact name of the columns that
contains the latitude and longitude and
Returns those values as a tuple.
"""
latName = input("Enter the name of the latitude column: ")
lonName = input("Enter the name of the longitude co: ")
return latName, lonName
def getLocale():
"""
Asks the user for latitude and longitude of the user's current location and
Returns those floating point numbers.
"""
lat = float(input("Enter your latude: "))
lon = float(input("Enter your longitude: "))
return lat, lon
def computeDist(x1, y1, x2, y2):
d = (x1 - x2) ** 2 + (y1 - y2) ** 2
return d
def dotAllPoints(cMap, df, latCol, lonCol):
"""
Mark all points in the latCol, lonCol with dots (little circle markers)
"""
for i, row in df.iterrows():
folium.CircleMarker(location=[row[latCol], row[lonCol]], radius=4, color='red').add_to(cMap)
def markAndFindClosest(cMap, df, latCol, lonCol, cLat, cLon):
"""
Goes through the list of points, marking each with a circle marker.
Finds the closest point using the computeDist() function and
Returns the lat and lon of closest point.
"""
df['Dist'] = df[[latCol, lonCol]].apply(lambda row: computeDist(*row, cLat, cLon), axis=1)
minRow = df[df['Dist'] == df['Dist'].min()]
folium.Marker(location=[float(minRow[latCol]), float(minRow[lonCol])],
popup="Closest").add_to(cMap)
folium.Marker(location=[cLat, cLon],
popup="You Are Here",
icon=folium.Icon(color='red')).add_to(cMap)
def writeMap(cMap):
"""
Writes the inputted map, cMap, to the file specified by the user.
"""
outF = input('Enter output file: ')
cMap.save(outfile=outF)
def main():
dataF = getData()
latColName, lonColName = getColumnNames()
lat, lon = getLocale()
cityMap = folium.Map(location=[lat, lon], tiles='cartodbpositron', zoom_start=11)
dotAllPoints(cityMap, dataF, latColName, lonColName)
markAndFindClosest(cityMap, dataF, latColName, lonColName, lat, lon)
writeMap(cityMap)
if __name__ == "__main__":
main()