STEM

Creative and Fun Uses of Hashing (Part 2)

In an earlier post, I demonstrated a fun and creative use of hashing to create unique visual signatures based on any input. In this post, I’ll create add another twist to it. Specifically, I’ll show another creative use of hashing where we can input any city name and get it’s real-time weather information, and based on the city’s location and current weather conditions we will draw a fractal image that is unique for that city for that moment of time. This would create a unique “weather signature” for any city around the world at a given moment. How cool is that?

The Design

The code gets the city name from the user, then uses OpenWeatherMap.org’s API to get real-time weather data for the city. Using the API requires a key that can be obtained from the above site (It’s free to up to a certain limit, after which the developer the key holder is charged per call. For obvious reasons, I cannot share my personal API key used for the script here). The key is a 32 character long hexadecimal value that looks like a MD5 hash value.

The weather data returned is then parsed accordingly (it’s in JSON format), and specific parameters of the weather are used to determine the fractal to draw. They are as follows:
latitude: afffects the scale factor of the image (I normalize the latitude value for best effect). Wind speed affects the Fractal tree branch length (I normalize the wind speed for best effect). Temperature and humidity are both used to set the angle of the drawing via Turtle library.

As an example, the JSON load looked like this for Seattle at a particular time:

{'coord': {'lon': -122.2043, 'lat': 47.8601}, 'weather': [{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01d'}], 'base': 'stations',
'main': {'temp': 305.29, 'feels_like': 304.8, 'temp_min': 302.59, 'temp_max': 307.66, 'pressure': 1012, 'humidity': 35, 'sea_level': 1012, 'grnd_level': 1005},
'visibility': 10000, 'wind': {'speed': 1.34, 'deg': 330}, 'clouds': {'all': 0}, 'dt': 1720477386, 'sys': {'type': 2, 'id': 2002007, 'country': 'US', 'sunrise': 1720441166, 'sunset': 1720498089},
'timezone': -25200, 'id': 5803457, 'name': 'Seattle', 'cod': 200}

The API supports different units such as Imperial and Metric. By default, temperature value returned is in Kelvin scale. I used Imperials units. An example request URL takes this format:

http://api.openweathermap.org/data/2.5/weather?q=YOUR_CITY_NAME&units=imperial&appid=YOUR_API_KEY

A simplified excerpt of the fractal code looks like this:

    fractal_turtle.forward(branch_len)
    fractal_turtle.left(angle)
    draw_tree(branch_len * sf, angle, sf)
    fractal_turtle.right(angle * 2)
    draw_tree(branch_len * sf, angle, sf)
    fractal_turtle.left(angle)
    fractal_turtle.backward(branch_len)

Where branch_len, and sf contain the length of the branch in the fractal, and the scale factor respectively. The variable angle is exactly that, the angle of the Turtle as stated above.

Due to my fractal drawing function, and limited parameters affecting the fractals, some images may appear to be identical, but they actually are not! Each one at a given moment is unique as long as any of these four parameters are different: latitude of the city, temperature, humidity, wind speed. To verify that each image is definitely unique, I employ the magic of hashing and generate a hash of the drawn image. By saving the fractals to an external file, and taking the hash of each over time, we can verify that they are indeed unique! The images can be saved to a binary image file by taking the snapshot manually or programmatically using canvas.postscript() function of Turtle. For more information on the programmatic method, see my earlier post here.

Below are some outputs of the fractals drawn at different times for various cities (the city name, date of execution, and city’s weather at that moment were also drawn above the fractal on execution):

I hope this was educational and interesting. You can get the full source code from my Patreon shop here (secure transaction).

Thanks for reading!


Related:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top