Using the web as a resource

Written by Vincent Bruijn

Cleaning up some old stuff I came across a nice and simple example of how to use the internet as a resource for something artistic. When I’m teaching, I always pay attention to the possibilities of using the web as much more than just a Yellow Pages platform where you, as an artist, can show off your work with a portfolio. Just like a lot of Arduino addicts use sensoric data to visualize or moderate a Processing.org sketch or a different piece of hardware, one can use stuff from the internet as a means for a visual end.

<?php
/**
* Color scheme class for making internet-based color schemes.
* On supplying a Openweathermap.org a valid city ID this class will return a
* Hex-color value of form #ff00ff when calling the $colorcode property.
* The colorcode is based on the local forecast temperatures for three days
* and need to be supplied in Celcius
*
* @package ColorScheme
* @author Vincent Bruijn <vebruijn@gmail.com>
* @copyright (c) Vincent Bruijn 2012
* @version 2.0 September 9 2012
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License
*/
class ColorScheme
{
    /**
    * Colorcode of form #ff00ff
    * @var string
    */
    public $colorcode;

    /**
    * User defined city name
    * @var string
    */
    public $city;

    /**
     * The URL to the weather API
     * @var string
     */
    private $url = "http://openweathermap.org/data/2.0/forecast/city/";

    /**
     * The constructor needs a name of a city.
     * @param string $city Name of a city according to the Google Maps standards
     */
    public function __construct($city)
    {
        $this->city = $city;
        $this->colorcode = $this->getTempBasedString($this->city);
        $this->colorcode = "#" . $this->colorcode;

        return $this->colorcode;
    }

    protected function getTempBasedString($city, $country = null)
    {
        $cache_name = 'cache/' . $city . '-' . date('Y-m-d') . '.json';
        $jsonstr = false;

        if (file_exists($cache_name)) {
            $jsonstr = @file_get_contents($cache_name);
        } else {
            array_map('unlink', glob('cache/' . $city . '*'));
            $jsonstr = @file_get_contents ($this->url . urlencode($city));
            @file_put_contents($cache_name, $jsonstr);
        }

        if ($jsonstr === false) {
            return '808080'; // 50% gray
        }

        $data = json_decode($jsonstr, true);
        if ($data['cod'] !== '200') {
            return '808080';
        }

        $hex = '';

        for ($x = 0; $x < 3; $x++)
        {
            $value_low = $data['list'][$x]['main']['temp_min'] - 273;
            $value_high = $data['list'][$x]['main']['temp_max'] - 273;
            $value = abs(($value_low + $value_high) / 2);
            $value = $this->calculateHex($value);
            if ($value < 16) {
                $hex .= "0" . dechex($value);
            } else {
                $hex .= dechex($value);
            }
        }

        if (strlen ($hex) == 0) {
            return '808080';
        } else {
            $return = substr ($hex,0,6);
            return $return;
        }
    }

    private function calculateHex($celcius)
    {
        // -128 - 136 min and max temperature from earth in fahrenheit
        $dec = round((255 / 147) * $celcius, 0) + 124;
        return $dec;
    }
}

For this PHP class we use the API of the openweathermap.org website to retrieve up-to-date information about weather conditions of a specific location. The API returns JSON so we can easily transform it into a PHP array and read the contents.

A normative consideration was how to transform temperature values into a color. I came up with the following.

  • The coldest temperature measured on earth represents a 00 (0) value in RGB.
  • The hottest temperature ever measured on earh represents a FF (255) value in RGB.
  • The current temperature will represent red.
  • The forecast temperatures for tomorrow and the day after will respectively represent green and blue.
  • Apply some mysterious manipulations to the values to get a nice color.

When combined, a current temperature and a two day forecast will create an RGB value. This value will be used to decorate a square on the screen. For the end result, I chose 24 cities from around the world. These locations are quite evenly spread across the globe, so all kinds of weather conditions and seasonal changes are taken into account.

So what are the values of the extremes measured on earth? Well, a bit of searching on Wikipedia resulted in this:

  • The earth’s maximum temperature ever measured is 136 °F, about 58 °C in Al Aziziyah, Libya, 1922
  • The lowest temperature -128 °F, -89 °C in Vostok Station, Antarctica, 1983

All results from openweathermap.org will be mapped across these limits. The nicest part is, that all information is real time, thus the end result changes continuously. This is what I like most about it: it is never exactly the same!

By the time of creation, the Al Aziziyah measurement was the known highest. It now appears to be a Furnace Creek, Death Valley, CA, USA measurement from 1913.