Locating Maps using Python

Locating_maps_using_ipython and Jupyter Notebook

Locating Maps using Python

In this post, we will be looking to get and print some of the maps using the jupyter notebook and python. The purpose of this notebook is to have fun with the Jupyter and python - to know how powerful python is. Moreover, this post is for fun and as for a learning part.

Various map plotting has been done using basic python coding in the jupyter notebook. Follow to try, implement and practice to match the best results possible out of this notebook.

Run all the commands and coding snippets in the python jupyter notebook and get the surprisingly outputs.


Let's Go step by step to implement and Execute the Coding part👨‍💻

 Note: Run and Execute each code block in the new cell of the Jupyter notebook.

Importing Pandas and Numpy

import numpy as np  
import pandas as pd

We will be doing such plotting and generating maps using Folium. Folium is not available by default. So, we first need to install it before we start with the Map Plotting.

What is Folium?

Folium is a Python library used for visualizing geospatial data. It is easy to use and yet a powerful library. Folium is a Python wrapper for Leaflet.js which is a leading open-source JavaScript library for plotting interactive maps.

Let's install Folium

!conda install -c conda-forge folium=0.5.0 --yes #installing folium through conda library
import folium

print('Folium installed and imported!')

The following packages will be downloaded:

    package                       |            build
    altair-4.1.0                  |             py_1         614 KB  conda-forge
    branca-0.4.1                |             py_0          26 KB  conda-forge
    brotlipy-0.7.0              |py36h8c4c3a4_1000         346 KB  conda-forge
    chardet-3.0.4               |py36h9f0ad1d_1006         188 KB  conda-forge
    cryptography-3.0        |   py36h45558ae_0         640 KB  conda-forge
    folium-0.5.0                |             py_0          45 KB  conda-forge
    pandas-1.0.5                |   py36h830a2c2_0        10.1 MB  conda-forge
    pysocks-1.7.1              |   py36h9f0ad1d_1          27 KB  conda-forge
    toolz-0.10.0                 |             py_0          46 KB  conda-forge
    urllib3-1.25.10            |             py_0          92 KB  conda-forge
    vincent-0.4.4               |             py_1          28 KB  conda-forge
                                           Total:        12.1 MB


Now coming on to display a simple map.

Display simple map

# define the world map
world_map = folium.Map()

# display world map

Output for simple map
Simple Map Image

Go ahead. Try zooming in and out of the rendered map above.

All locations on a map are defined by their respective Latitude and Longitude values. So you can create a map and pass in a center of Latitude and Longitude values of [0,0].

Let's create a map centered around India and play with the zoom level to see how it affects the rendered map

Let's create a map centered around India and play with the zoom level to see how it affects the rendered map

# define the world map centered around India with a low zoom level
world_map = folium.Map(location=[20, 78], zoom_start=4)

# display world map

Output for world map-centered around India.

world map-centered around India.

You can change the various latitude and longitude to go through the different locations and countries.

You can also change to zoom level for initial visualization and then zoom in and out accordingly.

Let's create a Stamen Toner map of India with a zoom level of 4.

# create a Stamen Toner map of the world centered around India
world_map = folium.Map(location=[20, 77], zoom_start=4, tiles='Stamen Toner')

# display map

The output of Stamen Toner Map

output of Stamen Toner Map

These are high-contrast B+W (black and white) maps. They are perfect for data mashups and exploring river meanders and coastal zones.

Let's create a Stamen Terrain map of India with zoom level 4.

# create a Stamen Toner map of the world centered around India
world_map = folium.Map(location=[20, 77], zoom_start=4, tiles='Stamen Terrain')

# display map

The output of Stamen Terrain Map

output of Stamen Terrain Map

Now let us import data from an online source using the URL and display the Choropleth map

These are maps that feature hill shading and natural vegetation colors. They showcase advanced labeling and linework generalization of dual-carriageway roads.

Feel free to zoom in and find the legal roadways and rivers.

This data is the immigrants to Canada from different parts of countries.

What is a Choropleth Map ❓

A Choropleth map is a thematic map in which areas are shaded or patterned in proportion to the measurement of the statistical variable being displayed on the map, such as population density or per-capita income. The choropleth map provides an easy way to visualize how a measurement varies across a geographic area or it shows the level of variability within a region.

df_can = pd.read_excel('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx',
                     sheet_name='Canada by Citizenship',

print('Data downloaded and read into a dataframe!')

# Show the dataframe as downloaded

# print the dimensions of the dataframe

Clean up data - We will make some modifications to the original dataset to make it easier to create our visualizations. Refer to Introduction to Matplotlib and Line Plots and Area Plots, Histograms, and Bar Plots notebooks for a detailed description of this preprocessing.

# clean up the dataset to remove unnecessary columns (eg. REG) 
df_can.drop(['AREA','REG','DEV','Type','Coverage'], axis=1, inplace=True)

# let's rename the columns so that they make sense
df_can.rename(columns={'OdName':'Country', 'AreaName':'Continent','RegName':'Region'}, inplace=True)

# for sake of consistency, let's also make all column labels of type string
df_can.columns = list(map(str, df_can.columns))

# add total column
df_can['Total'] = df_can.sum(axis=1)

# years that we will be using in this lesson - useful for plotting later on
years = list(map(str, range(1980, 2014)))
print ('data dimensions:', df_can.shape)

In order to create a Choropleth map, we need a GeoJSON file that defines the areas/boundaries of the state, county, or country that we are interested in.

In our case, since we are endeavoring to create a world map, we want a GeoJSON that defines the boundaries of all world countries. For your convenience, we will be providing you with this file, so let's go ahead and download it. Let's name it world_countries.json.

# download countries geojson file
!wget --quiet https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json -O world_countries.json
print('GeoJSON file downloaded!')

Now that we have the GeoJSON file, let's create a world map, centered around [0, 0] latitude and longitude values, with an initial zoom level of 2, and using Mapbox Bright style.

world_geo = r'world_countries.json' # geojson file

# create a plain world map
world_map = folium.Map(location=[0, 0], zoom_start=2, tiles='Mapbox Bright')

# generate choropleth map using the total immigration of each country to Canada from 1980 to 2013
    columns=['Country', 'Total'],
    legend_name='Immigration to Canada'

# display map

The output of Choropleth Map

output of Choropleth Map

Thank you for viewing this notebook and article. Just give it a try to run cells of code in Jupyter Notebook. You will love 💚  to play with this amazing stuff.

You can find the same set of codes executed on Jupyter Notebook By Visiting this Repository -  Locating_maps_using_ipython

I hope you found this lab interesting and educational. Feel free to contact me if you have any questions!

If you liked the post and find it useful then please visit this Repository in Github and Drop a star ⭐

Next Post Previous Post
No Comment
Add Comment
comment url

Best Buy Products