We want to add only the active layers to the legend, so let’s do it this way: Now, we can go back create the legend object. I think this could have been shortcut, but now I have a list of all active map layers, stored in layersToAdd. mapLayers().values() (values from all the map layers) if those objects are in checked_layers. In this object I look at the QgsProject().instance().mapLayers() which are all layers in the QgsProject instance. I added a print statement to print which layersI am adding, which will print in the console. children() are the actual layers themselves in the layerTreeRoot(). Then look at the layerTreeRoot(), which holds all the layers in the project. In checked_layers I check the QgsProject().instance() which is the current QGIS project I’m working in. In the above code snippet, I make a list called checked_layers. #get map layer objects of checked layers by matching their names and store those in a list This includes csv tablesĬhecked_layers = #Checks layer tree objects and stores them in a list. You can alternately define the extent using the following, which sets the extent to that of the interface’s map canvas, allowing you to change it dynamically. I have created a QgsRectangle object and manually defined the extent using map coordinates from my project. The arguments (20, 20, 20, 20) seem to have no effect on the map but without calling this method, you’ll get an error. But it is necessary for the map to exist. My apologies but the map.setRect() method confuses me and to be honest, I can’t explain what it does or why its there. So in the previous code I created a map, which is a QgsLayoutItemMap object. Map.attemptResize(QgsLayoutSize(239, 178, QgsUnitTypes.LayoutMillimeters)) Map.attemptMove(QgsLayoutPoint(5, 27, QgsUnitTypes.LayoutMillimeters)) #Move & Resize map on print layout canvas #defines map extent using map coordinates #I have no idea what this does, but it is necessary Let’s start by adding a map object to the print layout. that were difficult to navigate (for me at least). Here is the inheritance diagram:Įach item has its own attributes, methods, etc. You’ll see this class is a constructor for all the QgsLayoutItems of which there are many. ![]() Here is a reference to the documentation for the QgsLayoutItem class. I am just going to address the map, legend, and labels in this example. Common things people add to the print layout include a map, legend, labels, scale bar, north arrow, etc. We need to add stuff to the print layout. Now I can create a new print layout, effectively overwriting what I had. Then I loop through each layout and if the layoutName = the name of an existing layout, remove it. This quick fix takes a look at the print layouts in the manager object, and stores it in layouts_list. Also, in testing you’ll probably run this code many times, creating an ever growing list of test print layouts which you have to clean up later. One workaround is to rename the print layout (“PrintLayout2” for example) but I find this annoying. If I run this code again, I’ll get an error message. See again that I created a print layout object with a name: layoutName. ![]() If you run the above code, look under “Project” > “Print Layouts” and you’ll see a new print layout, which is currently empty.īefore I move on, the above code works, but only ONCE. I create a layout and initialize the default settings (size, layout, etc) for the print layout. The manager object holds the layout manager (layoutManager) for the current project. I created a project, which is a reference to the current Qgs project we are working in. #initializes default settings for blank print layout canvas I’ll start by initializing a new print layout. I’m also going to dive into the documentation to show you how I referenced various class methods, attributes, etc. ![]() But hopefully this example will make your life easier Due to the near unlimited customization one print layout can have, there are a lot of small details I’ll go into and show examples for. This is an evolving document so I can’t say this file will stay exactly the same but you’ll find all the elements of this blog post in the code. To begin I’ll point you to this link which has the full code, top to bottom. I have finally figured out may of the ins and outs of the process and hopefully this will serve as a guide to save someone else a lot of effort and time. I’ve been struggling off and on for literally months trying to create and export a print layout using Python for QGIS 3.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |