Catalunya Lego map



NOTE from the future:


Creating the map

Creating this map is a matter of basic steps, and those are explained in other blogs, so I’ll just link and summarize:

  • Get some elevation data
  • Load in Qgis
  • Create a square grid
  • Run zonal statistics to get raster data into the square grid
  • Create grid centroids
  • Style 😀

The lego colour palette

There is a strict colour palette for real lego pieces. My goal was to use the ones described in

To do so, Qgis can use the fill by expression capability, and I can write a python script that translates any original cell colour, to a valid Lego colour.

I skipped the palette here, since it gets a tad long:

import math
import colorsys

def to_lego_color(v1,v2,v3, feature, parent):
    Gets the closest available lego colour according to hue_distance
    <h2>Example usage:</h2>
      <li>my_sum(r,g,b) -> (r1,g1,b1)</li>
    def hue_distance(c1, c2):
        (r1,g1,b1) = c1
        (r2,g2,b2) = c2

        hls1 = colorsys.rgb_to_hls(r1,g1,b1)
        hls2 = colorsys.rgb_to_hls(r2,g2,b2)
        return abs(hls1[0]-hls2[0])

    def find_fit_color3(r,g,b, palette):
        closest_colors = sorted(palette, key=lambda color: hue_distance(color, (r,g,b)))
        return closest_colors[0]

    LEGO_PALETTE=[here the palette]

    red,green,blue = find_fit_color3(int(v1),int(v2),int(v3), LEGO_PALETTE)

    return rgbValue

The script is based in an R,G,B input since my original dataset has this values. It can be easily changed if needed :-).

What this script does is use the HSL space to find the closest color in the palette based on Hue, nothing else.


I’ll just drop here the original tweet for reference, alongside other links:

Leave a comment

Filed under curious, gis

Leave a Reply


You are commenting using your account. Log Out /  Change )

Google photo


Twitter picture


Facebook photo


Connecting to %s
