How to Make Dynamic Maps with python
Many people asked me how to make a dynamic map with python. I have chosen the corona dataset of Bangladesh for this purpose. I will try to do it in short.
I am going to use four python libraries
1. Pandas is a fast, powerful, flexible, and easy to use open-source data analysis and manipulation tool.
2.Geopandas is a project to add support for geographic data to pandas objects.
3. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations.
4. PIL is a python basic Imaging Library.
5. io Module allows us to manage the file-related input and output operations.
Step: 1
Importing mentioned packages. Loading shapefile of the district of Bangladesh and the corona time series dataset. I skipped the dataset between 5/27/2020' to ‘6/8/2020’.
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import PILimport iodf=gpd.read_file(‘C:/Users/hafez/personal/GISDATA\BGD_adm/BGD_adm2.shp’)data=pd.read_csv(‘covid.csv’)
Step: 2
Then join both datasets based on district names
merged=df.join(data,on=’NAME_2',how=’right’)
Step: 3
I am creating an empty frame and lop for creating date wise plot
image_frame=[] #empty frame
for dates in merged1.columns.to_list()[2:40]: # from column to 40 [date wise ]
ax=merged1.plot(column=dates,cmap=’Reds’,figsize=(10,10),legend=True,scheme=’user_defined’,classification_kwds={‘bins’:[100,1000,2000,3000,20000]},edgecolor=’black’,linewidth=0.5)
ax.set_title(‘Total Confirmed Corona virus Cases of Bangladesh: ‘+dates,{‘fontsize’:20})
ax.text(90, 20.2, ‘Scripted with Python 3.6\nUsed package:Pandas,PIL,geopandas\nCreated by Hafez Ahmad \n’, style=’italic’,
bbox={‘facecolor’: ‘green’, ‘alpha’: 0.01, ‘pad’: 10})
ax.text(90,20, ‘\nData: https://www.iedcr.gov.bd \nTotal cases: 68,508,Deaths: 930,Recovered:14560’, style=’italic’,
bbox={‘facecolor’: ‘black’, ‘alpha’: 0.01, ‘pad’: 10})
ax.set_axis_off()
ax.get_legend().set_bbox_to_anchor((0.17,0.4))
img=ax.get_figure()
f=io.BytesIO()
img.savefig(f,format=’png’,bbox_inches=’tight’)
f.seek(0)
image_frame.append(PIL.Image.open(f)) #storing all date wise imageesimage_frame[0].save(‘dynbamnicmap.gif’,format=’GIF’,append_images=image_frame[1:],
save_all=True,duration=300,loop=1)
f.close()
The last step is making maps and gif files. Here, I used python for loop and set all other parameters like title, text for citing.
That’s it.
I like to write about the environment. If you appoint in your work, I will do it very efficiently. I am diligent in my approach to ensuring that the work I do is completed to the highest standard. I like to work closely with my clients to ensure that they receive the best possible service making your ideas a reality through an easy and efficient process resulting in repeat business and a lasting relationship with the focus of understanding your business vision.
My services include but not limited to the following:-
1: Desktop and Web Mapping and GIS such mapping of the study area, density mapping, Contour map, risk mapping, Bathymetric mapping, road mapping, flood mapping, volumetric mapping, Landslide Analysis Elevation Mapping, and choropleth map…
2: Land use and land cover change, species distribution modeling, and Simulations.
3: Geo Data analysis and visualization using ArcGIS, QGIS, GRASS, SAGA.
4: Spatial Programming in GIS using Python and R.
5: Sourcing and downloading Satellite Images, Satellite image analysis including supervised and unsupervised.
6: GIS data file conversion — Convert from Shapefile to KML, GML, CSV, GeoJSON, etc.
7: Geospatial data collection and mining.
8: GIS data API — Google Maps API, NASA API, etc.
9: dashboard with ArcGIS, python (dash), and R (shiny). I also provide screen-cast training in any of these services, so you could do it yourself.
contact email: hafezahmad100@gmail.com