The launch success rate may depend on many factors such as payload mass, orbit type, and so on. It may also depend on the location and proximities of a launch site, i.e., the initial position of rocket trajectories. Finding an optimal location for building a launch site certainly involves many factors and hopefully we could discover some of the factors by analyzing the existing launch site locations.
In the previous exploratory data analysis labs, you have visualized the SpaceX launch dataset using matplotlib and seaborn and discovered some preliminary correlations between the launch site and success rates. In this lab, you will be performing more interactive visual analytics using Folium.
Objectives
This lab contains the following tasks:
TASK 1: Mark all launch sites on a map
TASK 2: Mark the success/failed launches for each site on the map
TASK 3: 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.
Let’s first import required Python packages for this lab:
!pip3 install folium==0.12.0!pip3 install wget
Requirement already satisfied: folium==0.12.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (0.12.0)
Requirement already satisfied: numpy in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from folium==0.12.0) (1.21.4)
Requirement already satisfied: jinja2>=2.9 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from folium==0.12.0) (3.0.3)
Requirement already satisfied: requests in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from folium==0.12.0) (2.26.0)
Requirement already satisfied: branca>=0.3.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from folium==0.12.0) (0.4.2)
Requirement already satisfied: MarkupSafe>=2.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jinja2>=2.9->folium==0.12.0) (2.0.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests->folium==0.12.0) (2021.10.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests->folium==0.12.0) (1.26.7)
Requirement already satisfied: idna<4,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests->folium==0.12.0) (3.1)
Requirement already satisfied: charset-normalizer~=2.0.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests->folium==0.12.0) (2.0.8)
Requirement already satisfied: wget in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (3.2)
First, let’s try to add each site’s location on a map using site’s latitude and longitude coordinates
The following dataset with the name spacex_launch_geo.csv is an augmented dataset with latitude and longitude added for each site.
# Download and read the `spacex_launch_geo.csv`spacex_csv_file = wget.download('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_geo.csv')spacex_df=pd.read_csv(spacex_csv_file)
Now, you can take a look at what are the coordinates for each site.
Above coordinates are just plain numbers that can not give you any intuitive insights about where are those launch sites. If you are very good at geography, you can interpret those numbers directly in your mind. If not, that’s fine too. Let’s visualize those locations by pinning them on a map.
We first need to create a folium Map object, with an initial center location to be NASA Johnson Space Center at Houston, Texas.
# Start location is NASA Johnson Space Centernasa_coordinate = [29.559684888503615, -95.0830971930759]site_map = folium.Map(location=nasa_coordinate, zoom_start=10)
We could use folium.Circle to add a highlighted circle area with a text label on a specific coordinate. For example,
# Create a blue circle at NASA Johnson Space Center's coordinate with a popup label showing its namecircle = folium.Circle(nasa_coordinate, radius=1000, color='#d35400', fill=True).add_child(folium.Popup('NASA Johnson Space Center'))# Create a blue circle at NASA Johnson Space Center's coordinate with a icon showing its namemarker = folium.map.Marker( nasa_coordinate,# Create an icon as a text label icon=DivIcon( icon_size=(20,20), icon_anchor=(0,0), html='<div style="font-size: 12; color:#d35400;"><b>%s</b></div>'%'NASA JSC', ) )site_map.add_child(circle)site_map.add_child(marker)
Make this Notebook Trusted to load map: File -> Trust Notebook
and you should find a small yellow circle near the city of Houston and you can zoom-in to see a larger circle.
Now, let’s add a circle for each launch site in data frame launch_sites
TODO: Create and add folium.Circle and folium.Marker for each launch site on the site map
# For each launch site, add a Circle object based on its coordinate (Lat, Long) values. In addition, add Launch site name as a popup labellaunch_site = launch_sites_df['Launch Site']# list of Lat, Long valuescoord_list =list(zip(launch_sites_df.Lat, launch_sites_df.Long))# dictionary with Lat, Long valuescoordinates =dict.fromkeys(launch_site)coordinates['CCAFS LC-40'] = coord_list[0]coordinates['CCAFS SLC-40'] = coord_list[1]coordinates['KSC LC-39A'] = coord_list[2]coordinates['VAFB SLC-4E'] = coord_list[3]# list of colorscolor_list = ['lightblue', 'lightgreen', 'black', 'orange']# list of labelslabels_list = ['CCAFS Launch Complex 40', 'CCAFS Space Launch Complex 40', 'KSC Launch Complex 39A', 'VAFB Space Launch Complex 4E']radius =1500
Make this Notebook Trusted to load map: File -> Trust Notebook
The generated map with marked launch sites should look similar to the following:
Now, you can explore the map by zoom-in/out the marked areas , and try to answer the following questions:
Are all launch sites in proximity to the Equator line?
Are all launch sites in very close proximity to the coast?
Also please try to explain your findings.
Task 2: Mark the success/failed launches for each site on the map
Next, let’s try to enhance the map by adding the launch outcomes for each site, and see which sites have high success rates. Recall that data frame spacex_df has detailed launch records, and the class column indicates if this launch was successful or not
spacex_df.tail(10)
Launch Site
Lat
Long
class
46
KSC LC-39A
28.573255
-80.646895
1
47
KSC LC-39A
28.573255
-80.646895
1
48
KSC LC-39A
28.573255
-80.646895
1
49
CCAFS SLC-40
28.563197
-80.576820
1
50
CCAFS SLC-40
28.563197
-80.576820
1
51
CCAFS SLC-40
28.563197
-80.576820
0
52
CCAFS SLC-40
28.563197
-80.576820
0
53
CCAFS SLC-40
28.563197
-80.576820
0
54
CCAFS SLC-40
28.563197
-80.576820
1
55
CCAFS SLC-40
28.563197
-80.576820
0
Next, let’s create markers for all launch records. If a launch was successful (class=1), then we use a green marker and if a launch was failed, we use a red marker (class=0)
Note that a launch only happens in one of the four launch sites, which means many launch records will have the exact same coordinate. Marker clusters can be a good way to simplify a map containing many markers having the same coordinate.
Let’s first create a MarkerCluster object
marker_cluster = MarkerCluster()
TODO: Create a new column in launch_sites dataframe called marker_color to store the marker colors based on the class value
# note:# a new column in launch_sites dataframe? or spacex_df dataframe?
# 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 redmarker_color = []for item in spacex_df['class'].values:if item ==1: marker_color.append('green')else: marker_color.append('red')
# another option is to assing the marker color list values to the dataframe column.# spacex_df['marker_color'] = marker_color# Function to assign color to launch outcomedef assign_marker_color(launch_outcome):if launch_outcome ==1:return'green'else:return'red'spacex_df['marker_color'] = spacex_df['class'].apply(assign_marker_color)spacex_df.tail(10)
Launch Site
Lat
Long
class
marker_color
46
KSC LC-39A
28.573255
-80.646895
1
green
47
KSC LC-39A
28.573255
-80.646895
1
green
48
KSC LC-39A
28.573255
-80.646895
1
green
49
CCAFS SLC-40
28.563197
-80.576820
1
green
50
CCAFS SLC-40
28.563197
-80.576820
1
green
51
CCAFS SLC-40
28.563197
-80.576820
0
red
52
CCAFS SLC-40
28.563197
-80.576820
0
red
53
CCAFS SLC-40
28.563197
-80.576820
0
red
54
CCAFS SLC-40
28.563197
-80.576820
1
green
55
CCAFS SLC-40
28.563197
-80.576820
0
red
TODO: For each launch result in spacex_df data frame, add a folium.Marker to marker_cluster
# Add marker_cluster to current site_mapsite_map.add_child(marker_cluster)# for each row in spacex_df data frame# create a Marker object with its coordinate# and customize the Marker's icon property to indicate if this launch was successed or failed, # e.g., icon=folium.Icon(color='white', icon_color=row['marker_color']for index, record in spacex_df.iterrows():# TODO: Create and add a Marker cluster to the site map marker = folium.Marker([record[1],record[2]] ,icon=folium.Icon(color='white', icon_color=record[4])) marker_cluster.add_child(marker)site_map
Make this Notebook Trusted to load map: File -> Trust Notebook
Your updated map may look like the following screenshots:
From the color-labeled markers in marker clusters, you should be able to easily identify which launch sites have relatively high success rates.
TASK 3: Calculate the distances between a launch site to its proximities
Next, we need to explore and analyze the proximities of launch sites.
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)
# Add Mouse Position to get the coordinate (Lat, Long) for a mouse over on the mapformatter ="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
Make this Notebook Trusted to load map: File -> Trust Notebook
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.
You can calculate the distance between two points on the map based on their Lat and Long values using the following method:
from math import sin, cos, sqrt, atan2, radiansdef 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 * creturn distance
TODO: Mark down a point on the closest coastline using MousePosition and calculate the distance between the coastline point and the launch site.
# find coordinate of the closet coastlinecoastline_lat =34.63462coastline_lon =-120.62439distance_coastline = calculate_distance(launch_sites_df.Lat.iloc[3], launch_sites_df.Long.iloc[3], coastline_lat, coastline_lon)distance_coastline
1.2644436254416542
TODO: After obtained its coordinate, create a folium.Marker to show the distance
# 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 # for example# 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),# )# )coastline_coordinate = [coastline_lat, coastline_lon]distance_marker = folium.Marker( coastline_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_coastline), ))
TODO: Draw a PolyLine between a launch site to the selected coastline point
# Create a `folium.PolyLine` object using the coastline coordinates and launch site coordinate# lines=folium.PolyLine(locations=coordinates, weight=1)launch_site_coordinate = [launch_sites_df.Lat.iloc[3], launch_sites_df.Long.iloc[3]]lines=folium.PolyLine(locations=(coastline_coordinate, launch_site_coordinate), weight=1)site_map.add_child(lines)site_map.add_child(distance_marker)
Make this Notebook Trusted to load map: File -> Trust Notebook
Your updated map with distance line should look like the following screenshot:
TODO: Similarly, you can draw a line betwee a launch site to its closest city, railway, highway, etc. You need to use MousePosition to find the their coordinates on the map first
A railway map symbol may look like this:
A highway map symbol may look like this:
A city map symbol may look like this:
# Create a marker with distance to a closest city, railway, highway, etc.# Draw a line between the marker to the launch siterailway_coordinates = coastline_coordinatehighway_coordinates = (34.68577, -120.59585)city_coordinates = (34.63886, -120.45788)distance_railway = distance_coastlinedistance_highway = calculate_distance(launch_sites_df.Lat.iloc[3], launch_sites_df.Long.iloc[3], highway_coordinates[0], highway_coordinates[1])distance_city = calculate_distance(launch_sites_df.Lat.iloc[3], launch_sites_df.Long.iloc[3], city_coordinates[0], city_coordinates[1])
Make this Notebook Trusted to load map: File -> Trust Notebook
After you plot distance lines to the proximities, you can answer the following questions easily:
Are launch sites in close proximity to railways?
Are launch sites in close proximity to highways?
Are launch sites in close proximity to coastline?
Do launch sites keep certain distance away from cities?
Also please try to explain your findings.
Next Steps:
Now you have discovered many interesting insights related to the launch sites’ location using folium, in a very interactive way. Next, you will need to build a dashboard using Ploty Dash on detailed launch records.