Friday 29 November 2019

batch - How to use QGIS Atlas with multiple layers/files per coverage?


I've got 30 contour datasets that all sit over the same extent. Each contour set is in its own file. I also have 30 extent polygons for each contour set as a separate file.


Can I use Atlas to generate a series of maps - each displaying a 'pair' of datasets - ie. contours and extent for each pair over the same extent?


Everything I've read indicates that you can loop through features in a layer with Atlas, but not through multiple layers. Could I work around this by adding a 'layer display list' as an attribute of the coverage layer?


Maybe with a python script?





Update:


This is going back to a previous question from a few years ago that I asked and didn't find a solution for. QGIS has advanced a few versions since then - so I'm hoping there is either an updated solution, or someone could provide guidance to develop a script.


Advanced warning - I'm a geologist - layers of rock are how I think! :-)


I have a project area for which I want to produce a series of maps (an atlas). Each map will cover exactly the same extent, but will show properties for a different layer of rock. In this example - contours of the depth to the top of 50 different rock layers.


The desired output would be 50 maps, same extent, different layer (or layer set) displayed


To add more complexity - I'd like to add 'sets' of layers. Rock layer 1 also has unique shapefiles showing the extent polygon layer and also a label_point layer.


Current Method


I've tried merging all the contour layers into one shapefile and using this as the Atlas coverage layer. This method doesn't quite produce the desired result because:



  • I can't filter the combined layer to only show one layer (not one feature) at a time.


  • I can filter using this rule based style expression on the combined layer! (where unit is the rock layer name):


attribute($currentfeature , 'unit') = attribute(@atlas_feature, 'unit')



  • Critical issue - I get one page per individual contour (ie. one per feature - so hundreds of pages per rock layer). I can't work out how to filter so I only get one page per rock layer (or if that is possible).

  • I can't merge the different 'sets' (points, polylines, polygons). Workaround - I could merge and filter based on geometry type - same as above.

  • Merging 50 input files into one master shapefile seems a bit redundant - surely one more code loop could negate the need for this? Workaround - use the processing framework to combine input files.



Answer



Since QGIS 2.12, with the introduction of data-defined properties on the "lock layers for map" option, in the map item properties, you can do what you seek. You just need to create the right coverage layer, with the information of the extents, but also of the layers to "print".



Create the layer presets


You can "save" the current state (visible or not visible) of your layers as presets. Just create the desired combination and press the eye icon on the layers panel, and choose "add preset".


Basically, you will have to create a preset for each contour, with that layer visible (paired with other layers if you like). This will be your "set of layers".


Give your preset memorable names, or else just opt for some kind of a sequence.


Creating the coverage layer.


Now you will have to create a polygon layer with all the possible combinations between the extents polygons, and all the presets. That means you will end up with some redundancy. You will have to replicate each extent polygon for each preset. Something like this:


polygon 1, preset a polygon 1, preset b ... polygon 50, preset a polygon 50, preset b


You can do this manually with copy paste, or else create some kind of script, but I would say your best and easier way would be using Spatialite or Postgis database. Just import you polygons extents there, and create a new non-spatial table with all presets names. Then, create a view that returns all the combinations.


SELECT f.*, g.preset_name
FROM polygons_extent as f, presets as g


Making it work


Add the new layer or view to QGIS, use it as coverage layer in atlas settings, and use the preset_name attribute in data-defined properties on the "lock layers for map" option.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...