-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.6 SpaceX_Interactive Visual Analytics and Dashboards.py
330 lines (212 loc) · 8.73 KB
/
1.6 SpaceX_Interactive Visual Analytics and Dashboards.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
# coding: utf-8
# ## Objective
# This lab contains the following tasks:
#
# * Mark all launch sites on a map
# * Mark the success/failed launches for each site on the map
# * Calculate the distances between a launch site to its proximities
#
# After completed the above tasks, you should be able to find some geographical patterns about launch sites.
#
# In[1]:
import folium
import pandas as pd
import requests
# In[2]:
from folium.plugins import MarkerCluster
from folium.plugins import MousePosition
from folium.features import DivIcon
# ### 1. Mark all launch sites on a map
# In[3]:
url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_geo.csv'
df = pd.read_csv(url,index_col=False)
df.head()
# In[4]:
df.columns
# In[5]:
geo_data = df[['Launch Site', 'Lat','Long','class']]
geo_data.head()
# In[6]:
geo = geo_data.groupby(['Launch Site'], as_index=False).first()
#The first() method returns the first n rows, based on the specified value
ge = geo[['Launch Site', 'Lat','Long']]
ge.head()
# We first need to create a folium `Map` object, with an initial center location to be NASA Johnson Space Center at Houston, Texas.
# In[7]:
# Start location is NASA Johnson Space Center
nasa_coordinate = [29.559684888503615, -95.0830971930759]
site_map = folium.Map(location=nasa_coordinate, zoom_start=10)
site_map
# In[9]:
# Initial the map
site_map = folium.Map(location=nasa_coordinate, zoom_start=5)
# We could use `folium.Circle` to add a highlighted each launch site with a text label on a specific coordinate.
# In[10]:
for index,site in ge.iterrows():
location = [site['Lat'],site['Long']]
print(site['Launch Site'])
print(location)
# In[11]:
for index,site in ge.iterrows():
location = [site['Lat'],site['Long']]
circle = folium.Circle(location, radius=1000, color='#d35400', fill= True).add_child(folium.Popup(site['Launch Site']))
marker = folium.map.Marker(location,
icon = DivIcon(
icon_size=(20,20),
icon_anchor=(0,0),
html='<div style="font-size: 12; color:#d35400;"><b>%s</b></div>' % site['Launch Site'],
)
)
site_map.add_child(circle)
site_map.add_child(marker)
site_map
# ![image.png](attachment:image.png)
# * All launch sites in proximity to the Equator line
# * All launch sites in very close proximity to the coast
# ### 2. Mark the success/failed launches for each site on the map
# In[12]:
df.tail()
# In[13]:
# Create a MarkerCluster object
marker_cluster = MarkerCluster()
# In[14]:
# Apply a function to check the value of `class` column
# If class=1, marker_color value will be green
# If class=0, marker_color value will be red
marker_color =[]
for i in df['class']:
if i == 1:
i = 'green'
else:
i='red'
marker_color.append(i)
print(marker_color)
# In[15]:
# add color to df
df['marker_color'] = marker_color
df.tail(5)
# In[16]:
site_map.add_child(marker_cluster)
# Add marker_cluster to current site_map:folium.map.Marker(loc,icon).add_to(map)
for index, record in df.iterrows():
location = [record['Lat'],record['Long']]
marker=folium.map.Marker(location,
icon=folium.Icon(color='white', icon_color=record['marker_color'])
)
marker_cluster.add_child(marker)
site_map
# ![image.png](attachment:image.png)
# ### 3.Calculate the distances between a launch site to its proximities
# Let's first add a `MousePosition` on the map to get coordinate for a mouse over a point on the map. As such, while you are exploring the map, you can easily find the coordinates of any points of interests (such as railway)
# In[17]:
# Add Mouse Position to get the coordinate (Lat, Long) for a mouse over on the map
formatter = "function(num) {return L.Util.formatNum(num, 5);};"
mouse_position = MousePosition(
position='topright',
separator=' Long: ',
empty_string='NaN',
lng_first=False,
num_digits=20,
prefix='Lat:',
lat_formatter=formatter,
lng_formatter=formatter,
)
site_map.add_child(mouse_position)
site_map
# ![image.png](attachment:image.png)
# Now zoom in to a launch site and explore its proximity to see if you can easily find any railway, highway, coastline, etc. Move your mouse to these points and mark down their coordinates (shown on the top-left) in order to the distance to the launch site.
#
# In[25]:
from math import sin, cos, sqrt, atan2, radians
def calculate_distance(lat1, lon1, lat2, lon2):
# approximate radius of earth in km
R = 6373.0
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return distance
# In[26]:
ge
# In[27]:
# find coordinate of from KSC LC-39A to the closet coastline
launch_site2_lat = 28.573255
launch_site2_lon = -80.646895
coastline_lat = 28.58109
coastline_lon = -80.61355
distance_coastline2 = calculate_distance(launch_site2_lat, launch_site2_lon, coastline_lat, coastline_lon)
distance_coastline2
# In[24]:
# Create and add a folium.Marker on your selected closest coastline point on the map
# Display the distance between coastline point and launch site using the icon property
coordinate = [coastline_lat,coastline_lon]
distance_marker = folium.Marker(
coordinate,
icon=DivIcon(icon_size=(20,20),icon_anchor=(0,0),html='<div style="font-size: 12; color:#d35400;"><b>%s</b></div>' % "{:10.2f} KM".format(distance_coastline2),
))
site_map.add_child(distance_marker)
# Create a `folium.PolyLine` object using the coastline coordinates and launch site coordinate
lines=folium.PolyLine(locations=[[launch_site2_lat,launch_site2_lon],[coastline_lat,coastline_lon]], weight=1)
site_map.add_child(lines)
site_map
# ![image.png](attachment:image.png)
# In[31]:
# Create a marker with distance of from KSC LC-39A to a closest city
city_lat = 28.52806
city_lon = -80.65615
distance_city2 = calculate_distance(launch_site2_lat, launch_site2_lon, city_lat, city_lon)
coordinate = [city_lat, city_lon]
distance_marker = folium.Marker(
coordinate,
icon=DivIcon(icon_size=(20,20),icon_anchor=(0,0),html='<div style="font-size: 12; color:#d35400;"><b>%s</b></div>' % "{:10.2f} KM".format(distance_city2),
))
site_map.add_child(distance_marker)
# Create a `folium.PolyLine` object using the coastline coordinates and launch site coordinate
lines=folium.PolyLine(locations=[[launch_site2_lat,launch_site2_lon],[city_lat,city_lon]], weight=1)
site_map.add_child(lines)
site_map
# ![image.png](attachment:image.png)
# In[32]:
# Create a marker with distance of from CCAFS SLC-40 to a closest railway
launch_site1_lat = 28.563197
launch_site1_lon = -80.576820
railway_lat = 28.57246
railway_lon = -80.58507
distance_railway1 = calculate_distance(launch_site1_lat, launch_site1_lon, railway_lat, railway_lon)
coordinate = [railway_lat, railway_lon]
distance_marker = folium.Marker(
coordinate,
icon=DivIcon(icon_size=(20,20),icon_anchor=(0,0),html='<div style="font-size: 12; color:#d35400;"><b>%s</b></div>' % "{:10.2f} KM".format(distance_railway1),
))
site_map.add_child(distance_marker)
# Create a `folium.PolyLine` object using the coastline coordinates and launch site coordinate
lines=folium.PolyLine(locations=[[launch_site1_lat,launch_site1_lon],coordinate], weight=1)
site_map.add_child(lines)
site_map
# ![image.png](attachment:image.png)
# In[33]:
# Create a marker with distance of from CCAFS SLC-40 to a closest highway
highway_lat = 28.54779
highway_lon = -80.56825
distance_highway1 = calculate_distance(launch_site1_lat, launch_site1_lon, highway_lat, highway_lon)
coordinate = [highway_lat, highway_lon]
distance_marker = folium.Marker(
coordinate,
icon=DivIcon(icon_size=(20,20),icon_anchor=(0,0),html='<div style="font-size: 12; color:#d35400;"><b>%s</b></div>' % "{:10.2f} KM".format(distance_highway1),
))
site_map.add_child(distance_marker)
# Create a `folium.PolyLine` object using the coastline coordinates and launch site coordinate
lines=folium.PolyLine(locations=[[launch_site1_lat,launch_site1_lon],coordinate], weight=1)
site_map.add_child(lines)
site_map
# ![image-2.png](attachment:image-2.png)
# * Are launch sites in close proximity to railways? yes
# * Are launch sites in close proximity to highways? yes
# * Are launch sites in close proximity to coastline? yes
# * Do launch sites keep certain distance away from cities? yes