Temperature maps
Temperature maps

Temperature maps for Germany

sample map
sample map scale

This script automatically creates temperature maps for Germany and uploads them to the commons.

This script contains three parts and runs once a day. At first it loads actual temperature information for Germany from www.weather.gov, then it creates a temperature map and finally loads it to the commons. At the right, you see an example image: It's the latest calculated map.

Function and Code

All code, I write, is semi-German, semi-English, so please excuse, if you can't read a comment or don't understand, what a variable is used for.

As said, this script runs once a day (17:00 UTC) and generates and uploads a temperature map for Germany. The script, which runs is auto.php, which only runs the three subroutines (update, create map, upload).

Database

Most of these scripts use mysql database access. They import db.inc.php, which connects to the mysql server and selects the database. In this code, all values for server, database user and so on are made anonymous with undeclared variable names.

Updating

The places are stored in a mysql table with name (not needed), latitude, longitude, last stored temperature and the URL, where to get the temperature. update.php gets the URL for every place from the database, calls the routine get_temp and stores the new temperature in the database. Additionally the temperature is printed out for debugging purposes.

The routine get_temp is in update.php, too. It uses load_website, to load the website and then uses a simple regular expression to extract the temperature. load_website is a function, which contacts the server and loads the site.

Creating

The creating of the map is the most complex thing. I'm using a trick: I don't render the whole temperature map: I let rsvg do this for me. I create a SVG with circles around every city with a gradient from the color of the temperature there to transparent.

karte.php uses db.inc.php, svg.inc.php and farben.inc.php. svg.inc.php only includes strings with the beginning and the end of the SVG file, farben.inc.php calculates the color according to a given temperature, as explained below. karte.php calculates the position on the map for every city and generates svg code for a gradient circle with the middle at the calculated coordinates. Calculating the position from the latitude/longitude I made the simple way and it works well for a small country like Germany.

After generating the SVG file, it is written to disc and converted to png using rsvg. After this, a PNG is laid about this, which shows the outlines of Germany and shows a little bit of relief. At first I put this directly in the SVG, but the rendering engine had problems with it. So now it's done with PHPs imagecopy.

Coloring

As mentioned above, karte.php uses a function from farben.inc.php to calculate the color according to a given temperature. This function is temp2farbe. I gave ten color values for ten temperatures from -20 °C to +40 °C and temp2farbe interpolates the right color for the given temperature. It uses farbinterpol to interpolate, when given two colors and a percent value. Another help function is hex2rgb, which converts hex color values to rgb values.

Uploading

At last there is the uploading to commons, which is made with upload.inc.php and the function upload_file there. The script logs in, gets the cookies, and uploads the file. Therefore some PostToHost functions are used, which are very dirty, so please don't use them ;).

Download

You can download all php files, all needed images and a sql dump packed into a zip file here.