Custom Colormaps in QGIS

QGIS Colormap view
QGIS (version 1.8 and before) lacks built in predefined colormaps . The only default possibility to create a colormap is to classify by equal intervals and with red-blue colors. While it is still possible to manually edit automatically created colormap, I found it completely unhandy. Firstly, because most of my raster data I would like to represent with certain step within chosen amplitude, secondly, I would like to represent the raster in different color scale and thirdly - I would like to use more color scales than one.

Interface of my Colormapping tool. Two color
scales used here - from black to white and from
white to green.
When someone creates the colormap for chosen raster, it is possible to save that custom colormap as a plain text file. That gives an opportunity to prepare such a text file outside of QGIS, no matter where and how. As my working environment is Windows and I know some Visual Basic  stuff, I wrote a little helper program, where I am able to set start value, count of the categories, step of the value and combine or put together multiple colormap scales. 

Important notice: the first and default color scale can be obtained pushing button "Classify". For other scales click right mouse button on a chosen category and menu will appear.

The program is written in Visual Basic 2010 and is dependent of Microsoft .NET 4 framework. It will not be able to run while that framework has not been installed.
Short overview about program usage can be seen in following video.

ColorMapper for QGIS - download


Array to string in python

Converting chosen dimension from pseudo 2D  array (list within a list) to string in Python can be done written at least in 4 lines. "for" loop together with "string" functionality to join or "implode" an array element.

Code can be seen here- http://pastebin.com/y1kQ2kpe


Find median or any statistic parameter in loop by array key

Suppose you have multiple lists with the same length and key:

1          35           33
2          40           20
3          23           23
12        60           70

and you would like to calculate medians by the key -
result should look like:

1          34
2          30
3          23
12        65
If you have raw tabular data, of course, this can be done fast in any spreadsheet. but if your results are intermediate results generated in large script, then it would be good if they have a structure and in such case they can be put together and passed to function.

What was before?
Previously in my script I invented superduper basic simple data structure for my data - long term monthly mean pairs. The month number is a key and it has its corresponding calculated value. It is kinda pseudo 2D array, where each element is a small array containing respective key (month number) and value, altogether 12 elements.

Why again?
So, what happens if you run such long term monthly mean function in loop (each time changing something in previous calculations to get different long term monthly mean values, of course, otherwise there would have no meaning of it..)? You get multiple arrays with similar structure.
What can be done with such arrays? Well.. different things, of course, any statistics.. in my case, 50th percentile, called median (or vice versa). The next thing is practical - how to pass all these arrays to the specific function? Wrap them together.

What is wrapping here? Superduper basic simple thing, actually - I created an "outer" array, I call it wrapper array, which I am appending with "long term monthly mean" pseudo 2D array and such an "outer array"
 is what I passed to the function.

What function does? Function takes apart such an "outer array" and reconfigures it by keys allowing to make a median calculation over the created reconfigured lists. Reconfigured array consists of number of keys of lists - list count is equal to key count - and each list consists of all values corresponding to the key. Then statistics can be done over each list and the result can passed to the function return (I prefer to return array with the similar structure like "long term monthly mean" structure - with pairs consisting of key and corresponding statistical value).

The full sample script can be seen here.


another "long term monthly mean" script

I always try to simplify and optimize things when I write my scripts. When I firstly started to write some scripts which iterated over a dates, I didnt realize the importance of splitted datetime objects by its parts and I wrote these scripts complicated with different inner conditions and so on. This one is very basic and simple one - it takes apart the "datetime" and stores the value accordingly by the month in a 2D temporary array, where in the one dimension there are 12 months and in the another dimension - appended values. Then in another loop the values are averaged within each month and result is given as 2D array as a month-averaged value pair.

The function can be seen here - http://pastebin.com/YZEtkKh6