{"id":1345,"date":"2019-11-03T15:25:43","date_gmt":"2019-11-03T14:25:43","guid":{"rendered":"https:\/\/hannes.enjoys.it\/blog\/?p=1345"},"modified":"2019-11-03T15:26:11","modified_gmt":"2019-11-03T14:26:11","slug":"30daymapchallenge-day-3-polygons-or-lego-style-brick-raster-in-qgis-using-geometry-generator-expressions","status":"publish","type":"post","link":"https:\/\/hannes.enjoys.it\/blog\/2019\/11\/30daymapchallenge-day-3-polygons-or-lego-style-brick-raster-in-qgis-using-geometry-generator-expressions\/","title":{"rendered":"#30DayMapChallenge: Day 3 \u2013 Polygons OR Lego\u00ae style brick raster in QGIS using Geometry Generator expressions"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>I showed this at <a href=\"https:\/\/qgis.ch\/de\/verein\/anwendertreffen\/anwendertreffen-olten-2019\">the annual meeting of the swiss user group in June 2019<\/a> and promptly forgot to post it for everyone to see. Let&#8217;s blame the scenery?<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"620\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/lego-swiss-1024x620.jpg\" alt=\"\" class=\"wp-image-1348\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/lego-swiss-1024x620.jpg 1024w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/lego-swiss-300x182.jpg 300w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/lego-swiss-768x465.jpg 768w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/lego-swiss-624x378.jpg 624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Have a raster.<\/p>\n\n\n\n<p><code>Processing<\/code> -&gt; <code>Create Grid<\/code>, covering it with points in a spacing of your choice. Use the same CRS as your raster (unless you want to figure out expression-based geometry transformations on your own, like in my <a href=\"https:\/\/hannes.enjoys.it\/blog\/2019\/09\/dynamic-elevation-profile-lines-as-qgis-geometry-generator\/\">elevation lines code<\/a>)<\/p>\n\n\n\n<p>Change the <code>Symbol layer type<\/code> to <code>Geometry Generator<\/code> and enter<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with_variable(\n  'radius',\n  3333,\n  buffer($geometry, @radius, 16)\n)<\/pre>\n\n\n\n<p>where radius should be a value about one third of your spacing.<\/p>\n\n\n\n<p>You should see circles!<\/p>\n\n\n\n<p>For the fill color use this expression and adjust the name of your raster layer:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with_variable(\n    'raster_layer',\n    'DHM200.xyz',\n    ramp_color('Blues',  -- change to an other named ramp here if you like\n        scale_linear(\n            raster_value(\n                @raster_layer, 1, \n                centroid($geometry)  -- back to our point\n            ),\n            raster_statistic(\n                @raster_layer, 1, \n                'min'\n            ),\n            raster_statistic(\n                @raster_layer, 1, \n                'max'\n            ),\n            0, 1  -- new scale as color ramps go 0 to 1\n        )\n    )\n)<\/pre>\n\n\n\n<p>This will get the raster value below our grid point and fit it onto a color ramp between the min and max of all the raster values.<\/p>\n\n\n\n<p>For the stroke use the same expression but wrap <code>darker(..., 150)<\/code> around it so you get a darker color.<\/p>\n\n\n\n<p>Using <code>Draw Effects<\/code> add a small <code>Drop Shadow<\/code> to your circles. I used an offset of 0.5 mm and a blur radius of 1 mm.<\/p>\n\n\n\n<p>Now add another <code>Geometry Generator<\/code> symbol layer below your existing one and use the following expression:<\/p>\n\n\n\n<p>bounds(buffer($geometry, 5000))<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with_variable(\n  'radius',\n  5000,\n  bounds(\n    buffer($geometry, @radius)\n  )\n)<\/pre>\n\n\n\n<p>with the radius being half your grid spacing.<\/p>\n\n\n\n<p>Use the same expressions for the colors as above but set the <code>darker<\/code> value to <code>200<\/code>.<\/p>\n\n\n\n<p>For some more fanciness maybe add a &#8220;QGIS&#8221; text on top of the <em>nupsies<\/em>?<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"432\" style=\"aspect-ratio: 704 \/ 432;\" width=\"704\" controls src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/lego-swiss.mp4\"><\/video><\/figure>\n\n\n\n<p>Exercise: Make it so that the result perfectly covers the raster, instead of being one grid cell off like mine.<\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/tjukanov\/status\/1187713840550744066\">Thanks Topi.<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I showed this at the annual meeting of the swiss user group in June 2019 and promptly forgot to post it for everyone to see. Let&#8217;s blame the scenery? Have a raster. Processing -&gt; Create Grid, covering it with points in a spacing of your choice. Use the same CRS as your raster (unless you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,31],"tags":[],"class_list":["post-1345","post","type-post","status-publish","format-standard","hentry","category-cartography","category-qgis"],"_links":{"self":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/1345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/comments?post=1345"}],"version-history":[{"count":5,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":1352,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/1345\/revisions\/1352"}],"wp:attachment":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/media?parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/categories?post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/tags?post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}