Catalunya Lego map

I stumbled upon a lego map in twitter and I thought it was quite fun and I should do something similar:

And here’s the result. One with NaturalEarth data colours for height, and another one with a set of official lego colours 😀

..
NOTE from the future:
Since everybody in twitter was to keen to remind me, First the map in EPSG:25831, then in 4236 😀

..

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 brickset.com

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

@qgsfunction(args='auto', group='Custom')
def to_lego_color(v1,v2,v3, feature, parent):
    """
    Gets the closest available lego colour according to hue_distance
    <h2>Example usage:</h2>
    <ul>
      <li>my_sum(r,g,b) -> (r1,g1,b1)</li>
    </ul>
    """
    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)

    rgbValue = str(red) + "," + str(green) + "," + str(blue)
    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.

References

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

Leave a comment

Filed under curious, gis

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.