Creating a Weather Forecast App with Python using Openweathermap and MongoDB

Shayan Ali Bhatti
Analytics Vidhya
Published in
4 min readMar 14, 2020
Weather forecast picture by mohamed mahmoud hassan taken from https://www.publicdomainpictures.net/en/view-image.php?image=269235&picture=weather-forecast

Introduction

Information about weather is very important specially if you want to anticipate weather changes that can affect businesses such as street hawkers, farming, aviation, entertainment etc. Having information about weather beforehand can help people prepare for it to avoid losses.

For this purpose, it is important to have an application that can let us know about weather alerts and possibly store the weather data in a database for analytics.

In this article, I present my work on creating a weather forecast app with Python programming language using Openweathermaps API and MongoDB. This app downloads 5 day-3 hour separated weather forecast data for defined cities, in this case, Karachi, London, Mumbai and Utah. It stores the forecast data in MongoDB and displays weather alerts such as snow, rain or freezing temperatures if present in forecast for those cities. It also creates a weather map for each of the forecast and shows weather forecast on a map.

Following are the important components of this app.

OpenWeatherMap

OpenWeatherMap is an online service that provides current, historical and weather forecast data for analytics. To communicate with the weather data, a user must subscribe to the Openweathermap website at https://openweathermap.org/ and then a user can get API access key. The weather data can be downloaded simply by requesting data from server API endpoint. The data comes in JSON format.

MongoDB

MongoDB is a NoSQL database that stores data in JSON-like documents with flexible schemas. In this project, I used MongoDB for storing data. Pymongo is the library used for interfacing Mongodb with the Python code.

Folium

Folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library. It can be used to manipulate your data in Python, then visualize it on a Leaflet map via Folium. I used it to show weather markers on Map.

Procedure

In this project, I achieved the following tasks:

  1. Create account in https://openweathermap.org/api
  2. Get API access key by subscribing to OpenWeatherMap website
  3. Make a multi-threaded program to connect to API. Locations to be monitored should be placed in configuration file
  4. One thread to download 5 days/3 hour forecast
  5. One thread to download weather maps
  6. All data should be stored in database (mongodb) as seperate collections/table
  7. One thread to open the latest weather map and display the map in window (should show last image as per last time stamp)
  8. Forecast threads should print out alerts if there is rain/snow or freeezing temperatures (<2 degree Fahrenheit) in any of forecast period
  9. Display forecast/previous data from database as a graph

The following code snippet shows how to download Openweathermap API data and store it in MongoDB database and show weather alerts.

The following code snippet shows how to create a weather map with appropriate weather symbol on top of city location on map.

Results

First, we are presented with the weather alerts in the 5 day forecast. If the weather forecast for designated cities has rain, snow or freezing temperatures (<2 Fahrenheit) then we will get weather alert. It is shown in figure below:

Weather Forecast Alerts for Cities with Rain/Snow or Freezing temperatures

Following image shows the latest (5th day) London weather forecast for 19th March 5pm. If we click on the weather icon, in this case, light snow, then a Popup shows the temperature. Click on the image to have a better view.

Weather Map for Utah for 19th March, 5pm

Following image shows the plot of all the weathers for Utah present in database with temperature in Fahrenheit w.r.t date and time.

Temperature forecast plot with Temperature (F) on y-axis and Date-time on x-axis

Here is how the data in MongoDB looks like. We get temperature info, weather description, wind and timestamps separated by 3 hours since it is 5 day/3-hour separated weather forecast data. For the database, timestamp is made primary key to avoid duplicate records.

Conclusion:

In this project, I learnt to interface Mongo DB with Openweathermap API to visualize past, present and future weather forecast for analytics. I hope it will be useful to people learning about interfacing Python with Openweathermaps API and MongoDB.

A detailed explanation video of this project can be viewed on YouTube at https://www.youtube.com/watch?v=8rV9k2tVWWI&t=36s

Complete code is available on GitHub at https://github.com/shayanalibhatti/Weather_forecast_using_Python_Openweathermap_and_MongoDB

Hope you liked this project or found it helpful :)

--

--

Shayan Ali Bhatti
Analytics Vidhya

Avid observer of life and software & Machine Learning developer