In [2]:
import geopy
import pandas
from geopy.geocoders import Nominatim, GoogleV3
# versions used: geopy 1.10.0, pandas 0.16.2, python 2.7.8

io = pandas.read_csv('MCFRS_Incidents_by_Station.csv', index_col=None, header=0, sep=",")
In [3]:
geolocate_column = io['Station address']
geolocate_station_address = io['Station address'].drop_duplicates()
geocoded_station_address = geolocate_station_address
In [4]:
print(geolocate_column.size)
print(geolocate_station_address.size)
75713
36
In [5]:
geolocator = Nominatim(user_agent="my-application")
#geolocator = GoogleV3()
#uncomment the geolocator you want to use
In [6]:
station_address_df = geolocate_station_address.to_frame()

station_address_df["Coordinates"] = station_address_df["Station address"].apply(geolocator.geocode)
                                    Station address
0               12251 Georgia Ave Wheaton, MD 20902
1                 16911 Georgia Ave Olney, MD 20832
2        10620 Connecticut Ave Kensington, MD 20985
3  111 University Blvd East Silver Spring, MD 20901
4            801 Russell Ave Gaithersburg, MD 20879
In [7]:
station_address_df["Latitude"] = station_address_df["Coordinates"].apply(lambda x:x.latitude if x != None else None)
station_address_df["Longitude"] = station_address_df["Coordinates"].apply(lambda x:x.longitude if x != None else None)
station_address_df["latlong"] = station_address_df['Latitude'].map(str)+ ';' + station_address_df['Longitude'].map(str)
station_address_df
Out[7]:
Station address Coordinates Latitude Longitude latlong
0 12251 Georgia Ave Wheaton, MD 20902 (12251, Georgia Avenue, Arcola, Wheaton-Glenmo... 39.056883 -77.049810 39.0568834992534;-77.049810423994
1 16911 Georgia Ave Olney, MD 20832 (16911, Georgia Avenue, Oakdale, Olney, Montgo... 39.132605 -77.070403 39.1326053999932;-77.0704034216743
2 10620 Connecticut Ave Kensington, MD 20985 None NaN NaN nan;nan
3 111 University Blvd East Silver Spring, MD 20901 (111, University Boulevard East, Indian Spring... 39.017602 -77.006856 39.0176018333333;-77.0068563333333
4 801 Russell Ave Gaithersburg, MD 20879 (801, Russell Avenue, Metropolitan Grove, Gait... 39.155111 -77.209497 39.1551114;-77.2094970600104
5 9041 Old Georgetown Rd Bethesda, MD 20814 (9041, Old Georgetown Road, Lone Oak, Pooks Hi... 39.003869 -77.110258 39.0038686718232;-77.1102578479276
6 13900 Old Columbia Pike Burtonsville, MD 20866 (Old Columbia Pike, Valley Stream Estates, Bur... 39.111350 -76.932196 39.1113498;-76.9321964
7 12500 Veirs Mill Rd Rockville, MD 20853 (Veirs Mill Road, Croydon Park, King Farm, Roc... 39.079938 -77.138860 39.0799382;-77.1388598
8 18910 Germantown Road Germantown, MD 20874 (Kingsview Fire Station 22, 18910, Germantown ... 39.161679 -77.284039 39.16167875;-77.2840389938603
9 1945 Seminary Rd Silver Spring, MD 20910 (1945, Seminary Road, Montgomery Hills Park, M... 39.008784 -77.041436 39.0087840769231;-77.0414356923077
10 20633 Boland Farm Road Germantown, MD 20876 (Milestone Fire Station 34, 20633, Boland Farm... 39.194482 -77.244745 39.1944821;-77.2447446
11 17921 Brooke Road Sandy Spring, MD 20860 (Brooke Road, Ashton-Sandy Spring, Montgomery ... 39.161815 -77.027046 39.161815;-77.027046
12 14401 Connecticut Ave Layhill, MD 20906 None NaN NaN nan;nan
13 10617 New Hampshire Ave Silver Spring, MD 20903 (New Hampshire Avenue, Oakview, Silver Spring,... 39.013447 -76.978489 39.013447;-76.9784895
14 13216 New Hampshire Ave Silver Spring, MD 20904 (New Hampshire Avenue, Oakview, Silver Spring,... 39.013447 -76.978489 39.013447;-76.9784895
18 6600 Wisconsin Ave Bethesda, MD 20815 (6600, Wisconsin Avenue, Drummond, Montgomery ... 38.976476 -77.090408 38.976476;-77.090408
19 5920 Massachusetts Ave Bethesda, MD 20816 (Massachusetts Avenue, Westhaven, Westbard, Be... 38.957563 -77.108134 38.9575629;-77.1081339
20 21400 Laytonsville Rd Laytonsville, MD 20879 (21400, Laytonsville Road, Laytonsville, Montg... 39.206955 -77.141530 39.2069549;-77.1415297
24 26334 Ridge Rd Damascus, MD 20750 (26334, Ridge Road, Damascus Gardens, Damascus... 39.289309 -77.204965 39.2893094;-77.2049649
26 8001 Connecticut Ave Chevy Chase, MD 20815 (Chevy Chase Fire Station Company 7, 8001, Con... 38.990684 -77.076787 38.9906837;-77.0767869788552
27 12100 Darnestown Rd North Potomac, MD 20878 (12100, Darnestown Road, North Potomac, Montgo... 39.116191 -77.250554 39.11619115;-77.2505542315562
36 8110 Georgia Ave Silver Spring, MD 20910 (Silver Spring Fire Station 1, 8110, Georgia A... 38.990901 -77.027136 38.9909013;-77.0271363
41 9615 DARNESTOWN ROAD, MD 20850 (Travilah Fire Station 32, 9615, Darnestown Ro... 39.094161 -77.197114 39.0941605;-77.1971142826837
42 6700 Democracy Blvd Bethesda, MD 20814 (6700, Democracy Boulevard, Fernwood, North Be... 39.021033 -77.137650 39.0210329;-77.1376496616993
48 11430 Falls Rd. Potomac, MD 20854 (11430, Falls Road, Pine Knolls, Potomac, Mont... 39.041952 -77.192587 39.0419518549267;-77.1925871629217
53 380 Hungerford Dr Rockville, MD 20850 (Rockville Fire Station 3, 380, Hungerford Dri... 39.087608 -77.150813 39.0876083;-77.1508132
64 7201 Carroll Ave Takoma Park, MD 20912 (7201, Carroll Avenue, Carroll Manor, Takoma P... 38.976724 -77.008270 38.9767240055304;-77.0082702608061
72 7272 Muncaster Mill Rd Derwood, MD 20855 None NaN NaN nan;nan
77 121 Rollins Ave Rockville, MD 20852 (121, Rollins Avenue, Rollins Park, Rockville,... 39.058868 -77.122654 39.0588677142857;-77.1226544693878
78 20001 Crystal Rock Dr Germantown, MD 20874 (Germantown Fire Station 29, 20001, Crystal Ro... 39.185093 -77.263262 39.18509285;-77.263261994716
81 19801 Beallsville Rd Beallsville, MD 20839 (19801, Beallsville Road, Beallsville, Montgom... 39.180202 -77.411622 39.1802016;-77.4116223656649
95 9404 Falls Rd Potomac, MD 20854 (9404, Falls Road, Fawsett Farms, Potomac, Mon... 39.009380 -77.220395 39.0093799697215;-77.2203948405796
97 22610 Gateway Center Dr, Suite 400 Clarksburg,... None NaN NaN nan;nan
101 8001 River Rd Bethesda, MD 20817 (River Road, Bannockburn, Westbard, Bethesda, ... 38.981882 -77.136376 38.9818824;-77.1363759
171 25801 Frederick Rd Clarksburg, MD 20871 (Frederick Road, Clarksburg, Montgomery County... 39.242279 -77.284255 39.242279;-77.284255
308 9710 Great Seneca Highway Rockville, MD 20850 None NaN NaN nan;nan
In [8]:
station_address_df.to_csv('geocoded-output.csv')
In [9]:
station_address_revised = pandas.read_csv('geocoded-output-revised.csv')
d = dict(zip(station_address_revised['Station address'],station_address_revised['latlong']))
io['coordinates'] = io['Station address'].map(d)

io[['latitude','longitude']] = io['coordinates'].str.split(';',expand=True)

io['latitude'] = io['latitude'].astype('float64')
io['longitude'] = io['longitude'].astype('float64')
In [10]:
locations = io[['latitude', 'longitude']]
locationlist = locations.values.tolist()
len(locationlist)
Out[10]:
[39.0799382, -77.1388598]
In [126]:
#len(locationlist)
#io.to_csv('MCFRS_Incidents_by_Station_coords.csv')
In [17]:
io.groupby('Fire Station')['Monthly Total'].sum()
Out[17]:
Fire Station
Bethesda Fire Department (Station 20)                                   12842
Bethesda Fire Department (Station 26)                                   16761
Bethesda Fire Department (Station 6)                                    20786
Burtonsville Volunteer Fire Department (Station 15)                     27510
Cabin John Park Volunteer Fire Department (Station 10)                   7925
Cabin John Park Volunteer Fire Department (Station 30)                   5263
Chevy Chase Fire Department (Station 7)                                  9194
Clarksburg Fire Department (Station 35)                                  5759
Damascus Volunteer Fire Department (Station 13)                          7117
Gaithersburg-Washington Grove Volunteer Fire Department (Station 28)    13130
Gaithersburg-Washington Grove Volunteer Fire Department (Station 8)     49135
Germantown Volunteer Fire Department (Station 29)                       15333
Germantown-Milestone Fire Department (Station 34)                       13478
Glen Echo Volunteer Fire Department (Station 11)                         7679
Hillandale Volunteer Fire Department (Station 12)                       19004
Hillandale Volunteer Fire Department (Station 24)                       13003
Hyattstown Volunteer Fire Department (Station 9)                          922
Kensington Volunteer Fire Department (Station 18)                       28174
Kensington Volunteer Fire Department (Station 21)                       11027
Kensington Volunteer Fire Department (Station 25)                       45057
Kensington Volunteer Fire Department (Station 5)                        19823
Kingsview Fire Department (Station 22)                                  10293
Laytonsville District Volunteer Fire Department (Station 17)             5620
Public Safety Training Academy                                            147
Rockville Volunteer Fire Department (Station 23)                        29755
Rockville Volunteer Fire Department (Station 3)                         29865
Rockville Volunteer Fire Department (Station 31)                        11723
Rockville Volunteer Fire Department (Station 33)                        10269
Sandy Spring Volunteer Fire Department (40)                             11921
Sandy Spring Volunteer Fire Department (Station 4)                       7831
Silver Spring Volunteer Fire Department (Station 1)                     27430
Silver Spring Volunteer Fire Department (Station 16)                    17022
Silver Spring Volunteer Fire Department (Station 19)                    12810
Takoma Park Volunteer Fire Department (Station 2)                       13960
Travilah Fire Department                                                28716
Upper Montgomery County Volunteer Fire Department (Station 14)           3493
Name: Monthly Total, dtype: int64
In [27]:
import folium
MD_COORDINATES = (39.11997, -77.10751)
map = folium.Map(location=MD_COORDINATES, zoom_start=12)

#folium.Marker(locationlist[0], popup=io['Station address'][0]).add_to(map)
#len(locationlist)
for point in range(0, 500):
    folium.Marker(locationlist[point], popup=str(io['Fire Station'][point])).add_to(map) 
    #folium.Marker(locationlist[point], popup= io.groupby('Fire Station')['Monthly Total'].sum().add_to(map)
display(map)