/posts  ·   /talks  ·   /boat  ·   /#30DayMapChallenge 2020 | 2022  ·  /About

2022 / Day 04: Green

Lets have a look at the randomness of points. For this i’ll create 256 random points in my bounding box using st_generatepoints. A variant of this function also accepts an int seed parameter which can be used to regenerate a deterministic sequence of points.

I’ll use the order of generated points to create a linestring with st_makeline so that every point will be joined with the next one using one of the PostgreSQL window functions called lead. And I’ll end up with 255 linestrings - one for each shade of green in the RGB model.

The ordering of the output with

order by oid desc

will make sure that bigger oid values (and hence brighter greens) will be drawn first and lower oids (darker greens) later on.

Exported a few of these from QGIS as pngs and converted them into a gif.

output image

with
    /* minmax defines the corners where we create a set of random points. */
    minmax as (
        select
            st_point(
                40500.000000,5993000.000000,3301
            ) as ll,
            st_point(
                1064500.000000,7017000.000000,3301
            ) as ur
    ),
    /* bounds gives the envelope for them  */
    bounds as (
        select
            st_envelope(
                st_collect(
                    array[ll, ur]
                )
            ) as geom
        from minmax
    ),
    /* pts will hold the random generated points (256 pcs) */
    pts as (
        select
            d.path[1] as oid, d.geom as geom
        from bounds
            join lateral st_generatepoints(bounds.geom, 256) as p on true
            join lateral st_dump(p) d on true
    )
select
    oid,
    /* .. and connect the dots */
    st_makeline(
        geom,
        lead(geom) over (order by oid)
    ) as geom,
    0 as red, 0 as blue, ((oid * 256) / 256)::int as green from pts
order by
    oid desc
;