Skip to content

Instantly share code, notes, and snippets.

@maryamalqasmi
Created August 26, 2021 12:10
Show Gist options
  • Save maryamalqasmi/b61705e6cf35f0186b6f294d455a82a4 to your computer and use it in GitHub Desktop.
Save maryamalqasmi/b61705e6cf35f0186b6f294d455a82a4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "3n3ltmfnrqpi",
"outputId": "a2d4f52a-2fb7-4668-f5f0-fe862aab8eb0",
"papermill": {
"duration": 1.489571,
"end_time": "2020-09-19T06:26:57.168979",
"exception": false,
"start_time": "2020-09-19T06:26:55.679408",
"status": "completed"
},
"tags": []
},
"source": "<center>\n <img src=\"https://gitlab.com/ibm/skills-network/courses/placeholder101/-/raw/master/labs/module%201/images/IDSNlogo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n</center>\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "# **Launch Sites Locations Analysis with Folium**\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Estimated time needed: **40** minutes\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "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.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "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`.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Objectives\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "This lab contains the following tasks:\n\n* **TASK 1:** Mark all launch sites on a map\n* **TASK 2:** Mark the success/failed launches for each site on the map\n* **TASK 3:** Calculate the distances between a launch site to its proximities\n\nAfter completed the above tasks, you should be able to find some geographical patterns about launch sites.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Let's first import required Python packages for this lab:\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "!pip3 install folium\n!pip3 install wget"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "import folium\nimport wget\nimport pandas as pd"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Import folium MarkerCluster plugin\nfrom folium.plugins import MarkerCluster\n# Import folium MousePosition plugin\nfrom folium.plugins import MousePosition\n# Import folium DivIcon plugin\nfrom folium.features import DivIcon"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Task 1: Mark all launch sites on a map\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "First, let's try to add each site's location on a map using site's latitude and longitude coordinates\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "The following dataset with the name `spacex_launch_geo.csv` is an augmented dataset with latitude and longitude added for each site.\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Download and read the `spacex_launch_geo.csv`\nspacex_csv_file = wget.download('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_geo.csv')\nspacex_df=pd.read_csv(spacex_csv_file)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Now, you can take a look at what are the coordinates for each site.\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Select relevant sub-columns: `Launch Site`, `Lat(Latitude)`, `Long(Longitude)`, `class`\nspacex_df = spacex_df[['Launch Site', 'Lat', 'Long', 'class']]\nlaunch_sites_df = spacex_df.groupby(['Launch Site'], as_index=False).first()\nlaunch_sites_df = launch_sites_df[['Launch Site', 'Lat', 'Long']]\nlaunch_sites_df"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "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.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We first need to create a folium `Map` object, with an initial center location to be NASA Johnson Space Center at Houston, Texas.\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Start location is NASA Johnson Space Center\nnasa_coordinate = [29.559684888503615, -95.0830971930759]\nsite_map = folium.Map(location=nasa_coordinate, zoom_start=10)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We could use `folium.Circle` to add a highlighted circle area with a text label on a specific coordinate. For example,\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Create a blue circle at NASA Johnson Space Center's coordinate with a popup label showing its name\ncircle = folium.Circle(nasa_coordinate, radius=1000, color='#d35400', fill=True).add_child(folium.Popup('NASA Johnson Space Center'))\n# Create a blue circle at NASA Johnson Space Center's coordinate with a icon showing its name\nmarker = folium.map.Marker(\n nasa_coordinate,\n # Create an icon as a text label\n icon=DivIcon(\n icon_size=(20,20),\n icon_anchor=(0,0),\n html='<div style=\"font-size: 12; color:#d35400;\"><b>%s</b></div>' % 'NASA JSC',\n )\n )\nsite_map.add_child(circle)\nsite_map.add_child(marker)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "and you should find a small yellow circle near the city of Houston and you can zoom-in to see a larger circle.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Now, let's add a circle for each launch site in data frame `launch_sites`\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* Create and add `folium.Circle` and `folium.Marker` for each launch site on the site map\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Initial the map\nsite_map = folium.Map(location=nasa_coordinate, zoom_start=5)\n# For each launch site, add a Circle object based on its coordinate (Lat, Long) values. In addition, add Launch site name as a popup label\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "The generated map with marked launch sites should look similar to the following:\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<center>\n <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module_3/images/launch_site_markers.png\" />\n</center>\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Now, you can explore the map by zoom-in/out the marked areas\n, and try to answer the following questions:\n\n* Are all launch sites in proximity to the Equator line?\n* Are all launch sites in very close proximity to the coast?\n\nAlso please try to explain your findings.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "# Task 2: Mark the success/failed launches for each site on the map\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Next, let's try to enhance the map by adding the launch outcomes for each site, and see which sites have high success rates.\nRecall that data frame spacex_df has detailed launch records, and the `class` column indicates if this launch was successful or not\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "spacex_df.tail(10)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Next, let's create markers for all launch records.\nIf 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)`\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "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.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Let's first create a `MarkerCluster` object\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wP9PVUZ7Jfjt",
"outputId": "6a3b8164-940c-4b93-9f3d-c655c4a0c683",
"papermill": {
"duration": 0.904519,
"end_time": "2020-09-19T06:27:38.357041",
"exception": false,
"start_time": "2020-09-19T06:27:37.452522",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": "marker_cluster = MarkerCluster()\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* Create a new column in `launch_sites` dataframe called `marker_color` to store the marker colors based on the `class` value\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "\n# Apply a function to check the value of `class` column\n# If class=1, marker_color value will be green\n# If class=0, marker_color value will be red\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Function to assign color to launch outcome\ndef assign_marker_color(launch_outcome):\n if launch_outcome == 1:\n return 'green'\n else:\n return 'red'\n \nspacex_df['marker_color'] = spacex_df['class'].apply(assign_marker_color)\nspacex_df.tail(10)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* For each launch result in `spacex_df` data frame, add a `folium.Marker` to `marker_cluster`\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Add the Marker cluster to the site map\nsite_map.add_child(marker_cluster)\n\n# for each row in spacex_df data frame\n# create a Marker object with its coordinate\n# 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']"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Your updated map may look like the following screenshots:\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<center>\n <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module_3/images/launch_site_marker_cluster.png\" />\n</center>\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<center>\n <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module_3/images/launch_site_marker_cluster_zoomed.png\" />\n</center>\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "From the color-labeled markers in marker clusters, you should be able to easily identify which launch sites have relatively high success rates.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "# TASK 3: Calculate the distances between a launch site to its proximities\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Next, we need to explore and analyze the proximities of launch sites.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "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)\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Add Mouse Position to get the coordinate (Lat, Long) for a mouse over on the map\nformatter = \"function(num) {return L.Util.formatNum(num, 5);};\"\nmouse_position = MousePosition(\n position='topright',\n separator=' Long: ',\n empty_string='NaN',\n lng_first=False,\n num_digits=20,\n prefix='Lat:',\n lat_formatter=formatter,\n lng_formatter=formatter,\n)\n\nsite_map.add_child(mouse_position)\nsite_map"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "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.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "You can calculate the distance between two points on the map based on their `Lat` and `Long` values using the following method:\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "from math import sin, cos, sqrt, atan2, radians\n\ndef calculate_distance(lat1, lon1, lat2, lon2):\n # approximate radius of earth in km\n R = 6373.0\n\n lat1 = radians(lat1)\n lon1 = radians(lon1)\n lat2 = radians(lat2)\n lon2 = radians(lon2)\n\n dlon = lon2 - lon1\n dlat = lat2 - lat1\n\n a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2\n c = 2 * atan2(sqrt(a), sqrt(1 - a))\n\n distance = R * c\n return distance"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* Mark down a point on the closest railway using MousePosition and calculate the distance between the railway point to the launch site.\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# distance_railway = calculate_distance(lat1, lon1, lat2, lon2)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* After obtained its coordinate, create a `folium.Marker` to show the distance\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# create and add a folium.Marker on your selected closest raiwaly point on the map\n# show the distance to the launch site using the icon property "
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* Draw a `PolyLine` between a launch site to the selected\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Create a `folium.PolyLine` object using the railway point coordinate and launch site coordinate"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Your updated map with distance line should look like the following screenshot:\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<center>\n <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module_3/images/launch_site_marker_distance.png\" />\n</center>\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "*TODO:* Similarly, you can draw a line betwee a launch site to its closest city, coastline, highway, etc.\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Create a marker with distance to a closest city, coastline, highway, etc.\n# Draw a line between the marker to the launch site\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "After you plot distance lines to the proximities, you can answer the following questions easily:\n\n* Are launch sites in close proximity to railways?\n* Are launch sites in close proximity to highways?\n* Are launch sites in close proximity to coastline?\n* Do launch sites keep certain distance away from cities?\n\nAlso please try to explain your findings.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "# Next Steps:\n\nNow 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.\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Authors\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "[Yan Luo](https://www.linkedin.com/in/yan-luo-96288783/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDS0321ENSkillsNetwork26802033-2021-01-01)\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "### Other Contributors\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Joseph Santarcangelo\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Change Log\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n| ----------------- | ------- | ---------- | --------------------------- |\n| 2021-05-26 | 1.0 | Yan | Created the initial version |\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Copyright \u00a9 2021 IBM Corporation. All rights reserved.\n"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
},
"papermill": {
"duration": 142.196942,
"end_time": "2020-09-19T06:29:13.341578",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2020-09-19T06:26:51.144636",
"version": "2.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment