In [1]:
from colour import Color
import numpy as np

Color ramp generator for rasql

Deriving images from data not in pixel form directly needs the application of a color ramp. The next function generates a color ramp between two colors, interpolating linearly.

Planned for the future: quantile ramps from distributions.


In [2]:
def generateRamp2(col1, col2, steps, rangeMin, rangeMax):
    ramp = Color(col1).range_to(Color(col2), steps)
    rgbRamp = list(
        map(lambda c: list(
            map(lambda ve: int(ve * 255), c.rgb)), ramp)
    )
    linInt =  list(map(lambda x: str("%.2f" % round(x, 2)), np.linspace(rangeMin, rangeMax)))
    values = list(zip(linInt, rgbRamp))
    
    res_strings = list(map(lambda val: 
                    " ".join(
                        ("case {0} > c.1".format(val[0]),
                         "return {{red: {0}; green: {1}; blue: {2}}}".format(val[1][0], val[1][1], val[1][2]))
                    ),
                    values)
               )
    
    head_strings = 'image>>for c in ( iri_test ) return encode( switch case c.1 = -9999 return {red: 0; green: 0; blue: 0}'
    tail_strings = 'default return {red: 1; green: 1; blue: 1}  , "png")'
    return (head_strings + " ".join(res_strings) + tail_strings).encode('unicode-escape').decode()
        
generateRamp2("red", "blue", 100, -5, 5)


Out[2]:
'image>>for c in ( iri_test ) return encode( switch case c.1 = -9999 return {red: 0; green: 0; blue: 0}case -5.00 > c.1 return {red: 255; green: 0; blue: 0} case -4.80 > c.1 return {red: 255; green: 10; blue: 0} case -4.59 > c.1 return {red: 255; green: 20; blue: 0} case -4.39 > c.1 return {red: 255; green: 30; blue: 0} case -4.18 > c.1 return {red: 255; green: 41; blue: 0} case -3.98 > c.1 return {red: 255; green: 51; blue: 0} case -3.78 > c.1 return {red: 255; green: 61; blue: 0} case -3.57 > c.1 return {red: 255; green: 72; blue: 0} case -3.37 > c.1 return {red: 255; green: 82; blue: 0} case -3.16 > c.1 return {red: 255; green: 92; blue: 0} case -2.96 > c.1 return {red: 255; green: 103; blue: 0} case -2.76 > c.1 return {red: 255; green: 113; blue: 0} case -2.55 > c.1 return {red: 255; green: 123; blue: 0} case -2.35 > c.1 return {red: 255; green: 133; blue: 0} case -2.14 > c.1 return {red: 255; green: 144; blue: 0} case -1.94 > c.1 return {red: 255; green: 154; blue: 0} case -1.73 > c.1 return {red: 255; green: 164; blue: 0} case -1.53 > c.1 return {red: 255; green: 175; blue: 0} case -1.33 > c.1 return {red: 255; green: 185; blue: 0} case -1.12 > c.1 return {red: 255; green: 195; blue: 0} case -0.92 > c.1 return {red: 255; green: 206; blue: 0} case -0.71 > c.1 return {red: 255; green: 216; blue: 0} case -0.51 > c.1 return {red: 255; green: 226; blue: 0} case -0.31 > c.1 return {red: 255; green: 236; blue: 0} case -0.10 > c.1 return {red: 255; green: 247; blue: 0} case 0.10 > c.1 return {red: 252; green: 255; blue: 0} case 0.31 > c.1 return {red: 242; green: 255; blue: 0} case 0.51 > c.1 return {red: 231; green: 255; blue: 0} case 0.71 > c.1 return {red: 221; green: 255; blue: 0} case 0.92 > c.1 return {red: 211; green: 255; blue: 0} case 1.12 > c.1 return {red: 200; green: 255; blue: 0} case 1.33 > c.1 return {red: 190; green: 255; blue: 0} case 1.53 > c.1 return {red: 180; green: 255; blue: 0} case 1.73 > c.1 return {red: 169; green: 255; blue: 0} case 1.94 > c.1 return {red: 159; green: 255; blue: 0} case 2.14 > c.1 return {red: 149; green: 255; blue: 0} case 2.35 > c.1 return {red: 139; green: 255; blue: 0} case 2.55 > c.1 return {red: 128; green: 255; blue: 0} case 2.76 > c.1 return {red: 118; green: 255; blue: 0} case 2.96 > c.1 return {red: 108; green: 255; blue: 0} case 3.16 > c.1 return {red: 97; green: 255; blue: 0} case 3.37 > c.1 return {red: 87; green: 255; blue: 0} case 3.57 > c.1 return {red: 77; green: 255; blue: 0} case 3.78 > c.1 return {red: 66; green: 255; blue: 0} case 3.98 > c.1 return {red: 56; green: 255; blue: 0} case 4.18 > c.1 return {red: 46; green: 255; blue: 0} case 4.39 > c.1 return {red: 36; green: 255; blue: 0} case 4.59 > c.1 return {red: 25; green: 255; blue: 0} case 4.80 > c.1 return {red: 15; green: 255; blue: 0} case 5.00 > c.1 return {red: 5; green: 255; blue: 0}default return {red: 1; green: 1; blue: 1}  , "png")'

In [ ]:
http://example.org/rasdaman/ows?service=WMS&version=1.3.0&request=InsertStyle
&name=FireMarkup
&layer=dessert_area
&abstract=This style marks the areas where fires are in progress with the color red
&rasqlTransformFragment=case $Iterator when ($Iterator.c1) < 0 then {255, 0, 0} else {0, 255, 0} end

In [ ]: