{"id":1377,"date":"2019-12-26T18:50:26","date_gmt":"2019-12-26T17:50:26","guid":{"rendered":"https:\/\/hannes.enjoys.it\/blog\/?p=1377"},"modified":"2019-12-26T18:50:26","modified_gmt":"2019-12-26T17:50:26","slug":"highlight-current-timeslice-in-a-qgis-atlas-layout","status":"publish","type":"post","link":"https:\/\/hannes.enjoys.it\/blog\/2019\/12\/highlight-current-timeslice-in-a-qgis-atlas-layout\/","title":{"rendered":"Highlight current timeslice in a QGIS Atlas layout"},"content":{"rendered":"\n<p>Did this for an ex-colleague some months ago and forgot to share the how-to publically. We needed a visual representation of the current time in a layout that showed both a raster map (different layer per timeslice) and a timeseries plot of an aspect of the data (this was created outside QGIS).<\/p>\n\n\n\n<p>Have lots of raster layers you want to iterate through. I have:<\/p>\n\n\n\n<p><code>.\/ECMWF_ERA_40_subset\/2019-01-01.tif<br>.\/ECMWF_ERA_40_subset\/2019-01-02.tif<br>.\/ECMWF_ERA_40_subset\/2019-01-03.tif<br>.\/ECMWF_ERA_40_subset\/2019-01-04.tif<br>.\/ECMWF_ERA_40_subset\/2019-01-05.tif<br>.\/ECMWF_ERA_40_subset\/2019-01-06.tif<br>...<\/code><\/p>\n\n\n\n<p>Create a new layer for your map extent. Draw your extent as geometry. Duplicate that geometry as many times as you have days. Alternatively you could of course have different geometries per day. Whatever you do, you need a layer with one feature per timeslice for the Atlas to iterate though. I have 30 days to visualise so I duplicated my extent 30 times.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"417\" height=\"446\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-2.png\" alt=\"\" class=\"wp-image-1378\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-2.png 417w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-2-280x300.png 280w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/figure>\n\n\n\n<p>Open the Field Calculator. Add a new field called <code>date<\/code> as <code>string<\/code> type (not as <code>date<\/code> type until some bug is fixed (sorry, did not make a note here, maybe sorting is\/was broken?)) with an expression that represents time and orders chronologically if sorted by QGIS. For example: <code>'2019-01-' || lpad(@row_number,2,0)<\/code>  (assuming your records are in the correct order if you have different geometries\u2026)<\/p>\n\n\n\n<p>Have your raster layers named the same way as the <code>date<\/code> attribute values.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"429\" height=\"428\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-3.png\" alt=\"\" class=\"wp-image-1380\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-3.png 429w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-3-300x300.png 300w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-3-150x150.png 150w\" sizes=\"auto, (max-width: 429px) 100vw, 429px\" \/><\/figure>\n\n\n\n<p>Make a new layout.<\/p>\n\n\n\n<p>For your Layout map check &#8220;Lock layers&#8221; and use <code>date<\/code> as expression for the &#8220;Lock layers&#8221; override. This will now select the appropriate raster layer, based on the attribute value &lt;-> layer name, to display for each Atlas page.<\/p>\n\n\n\n<p>Cool, if you preview the Atlas now you got a nice animation through your raster layers. Let&#8217;s do part 2:<\/p>\n\n\n\n<p>In your layout add your timeseries graph. Give it a unique ID, e. g. &#8220;plot box&#8221;. Set its width and height via new variables (until you can get those via an expression this is needed for calculations below).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"388\" height=\"578\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-4.png\" alt=\"\" class=\"wp-image-1384\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-4.png 388w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-4-201x300.png 201w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/figure>\n\n\n\n<p>Create a box to visualise the timeslice. Set its width to <code>map_get(item_variables('plot box'), 'plot_width') \/ @atlas_totalfeatures<\/code>. For the height and y use\/adjust this expression: <code>map_get(item_variables('plot box'), 'plot_height')<\/code>. For x comes the magic:<\/p>\n\n\n\n<p><code>with_variable(<br>     'days_total',<br>     day(to_date(maximum(\"date\"))-to_date(minimum(\"date\")))+1,<br>       -- number of days in timespan<br>       -- +1 because we need the number of days in total<br>       -- not the inbetween, day() to just get the number of days<br>     with_variable(<br>         'mm_per_day',<br>         map_get(item_variables('plot box'), 'plot_width') \/ @days_total,<br>         with_variable(<br>             'days',<br>             day(to_date(attribute(@atlas_feature, 'date'))-to_date(minimum(\"date\"))),<br>               -- number of days the current feature is from the first day<br>               -- to_date because BUG attribute() returns datetime for date field<br>             @mm_per_day * @days + map_get(item_variables('plot box'), 'plot_x')<br>         )<br>     )<br> )<\/code><\/p>\n\n\n\n<p>This will move the box along the x axis accordingly.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"520\" style=\"aspect-ratio: 736 \/ 520;\" width=\"736\" controls src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/highlight-time-in-atlas-graph.mp4\"><\/video><\/figure>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/highlight-time-in-atlas-graph.mkv\">Better video<\/a><a href=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/highlight-time-in-atlas-graph.mkv\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/highlight-time-in-atlas-graph.zip\">Sample Project with Data (untested)<\/a><a href=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/highlight-time-in-atlas-graph.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p>Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did this for an ex-colleague some months ago and forgot to share the how-to publically. We needed a visual representation of the current time in a layout that showed both a raster map (different layer per timeslice) and a timeseries plot of an aspect of the data (this was created outside QGIS). Have lots of [&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,29,3,31],"tags":[],"class_list":["post-1377","post","type-post","status-publish","format-standard","hentry","category-cartography","category-gis","category-guide","category-qgis"],"_links":{"self":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/1377","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=1377"}],"version-history":[{"count":6,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/1377\/revisions"}],"predecessor-version":[{"id":1389,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/1377\/revisions\/1389"}],"wp:attachment":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/media?parent=1377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/categories?post=1377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/tags?post=1377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}