4.2. Lesson: Criando um Layout de Impressão Dinâmico

Now that you have learned to create a basic map layout we go a step further and create a map layout that adapts dynamically to our map extent and to the page properties, e.g. when you change the size of the page. Also, the date of creation will adapt dynamically.

4.2.1. moderado Follow Along: Criando a tela do mapa dinâmico

  1. Carregue os conjuntos de dados no formato ESRI Shapefile protected_areas.shp, places.shp, rivers.shp e water.shp na tela do mapa e adapte suas propriedades para o que for mais conveniente.

  2. After everything is rendered and symbolized to your liking, click the novoLayout New Print Layout icon in the toolbar or choose File ► New Print Layout. You will be prompted to choose a title for the new print layout.

  3. Queremos criar um layout de mapa que consiste em um cabeçalho e um mapa da região perto de Swellendam, África do Sul. O layout deve ter uma margem de 7,5 mm e o cabeçalho deve ter 36 mm de altura.

  4. Create a map item called main map on the canvas and go to the Layout panel. Scroll down to the Variables section and find the Layout part. Here we set some variables you can use all over the dynamic print layout. Go to the Layout panel and scroll down to the Variables section. The first variable will define the margin. Press the adicionarSimbologia button and type in the name sw_layout_margin. Set the value to 7.5. Press the adicionarSimbologia button again and type in the name sw_layout_height_header. Set the value to 36.

  5. Now you are ready to create the position and the size of the map canvas automatically by means of the variables. Make sure that your map item is selected, go to the Item Properties panel, scroll down to and open the Position and Size section. Click the dataDefineExpressionOn Data defined override for X and from the Variables entry, choose @sw_layout_margin.

  6. Click the dataDefineExpressionOn Data defined override for Y, choose Edit… and type in the formula:

    to_real(@sw_layout_margin) + to_real(@sw_layout_height_header)
    
  7. You can create the size of the map item by using the variables for Width and Height. Click the dataDefineExpressionOn Data defined override for Width and choose Edit … again. Fill in the formula:

    @layout_pagewidth - @sw_layout_margin * 2
    

    Click the dataDefineExpressionOn Data defined override for Height and choose Edit …. Here fill in the formula:

    @layout_pageheight -  @sw_layout_height_header -  @sw_layout_margin * 2
    
  8. We will also create a grid containing the coordinates of the main canvas map extent. Go to Item Properties again and choose the Grids section. Insert a grid by clicking the adicionarSimbologia button. Click on Modify grid … and set the Interval for X, Y and Offset according to the map scale you chose in the QGIS main canvas. The Grid type Cross is very well suited for our purposes.

4.2.2. moderado Follow Along: Criando o cabeçalho dinâmico

  1. Insira um retângulo que conterá o cabeçalho com o botão addBasicShape Adicionar Forma. No painel Items, digite o nome cabeçalho.

  2. Again, go to the Item Properties and open the Position and Size section. Using dataDefineExpressionOn Data defined override, choose the sw_layout_margin variable for X as well as for Y. Width shall be defined by the expression:

    @layout_pagewidth - @sw_layout_margin * 2
    

    and Height by the sw_layout_height_header variable.

  3. We will insert a horizontal line and two vertical lines to divide the header into different sections using the addNodesShape Add Node Item. Create a horizontal line and two vertical lines and name them Horizontal line, Vertical line 1 Vertical line 2.

    1. Para a linha horizontal:

      1. Set X to the variable sw_layout_margin

      2. Set the expression for Y to:

        @sw_layout_margin + 8
        
      3. Set the expression for Width to:

        @layout_pagewidth -  @sw_layout_margin * 3 - 53.5
        
    2. Para a primeira linha vertical:

      1. Set the expression for X to:

        @layout_pagewidth - @sw_layout_margin * 2 - 53.5
        
      2. Set Y to the variable sw_layout_margin

      3. The height must be the same as the header we created, so set Height to the variable sw_layout_height_header.

    3. A segunda linha vertical é colocada à esquerda da primeira.

      1. Set the expression for X to:

        @layout_pagewidth - @sw_layout_margin * 2 - 83.5
        
      2. Set Y to the variable sw_layout_margin

      3. The height shall be the same as the other vertical line, so set Height to the variable sw_layout_height_header.

    The figure below shows the structure of our dynamic layout. We will fill the areas created by the lines with some elements.

../../../_images/dynamic_layout_structure.png

4.2.3. moderado Follow Along: Criando rótulos para o cabeçalho dinâmico

  1. The title of your QGIS project can be included automatically. The title is set in the Project Properties. Insert a label with the rótulo Add Label button and enter the name project title (variable). In the Main Properties of the Items Properties Panel enter the expression:

    [%@project_title%]
    

    Defina a posição do rótulo.

    1. For X, use the expression:

      @sw_layout_margin + 3
      
    2. For Y, use the expression:

      @sw_layout_margin + 0.25
      
    3. For Width, use the expression:

      @layout_pagewidth - @sw_layout_margin *2 - 90
      
    4. Enter 11.25 for Height

    Under Appearance set the Font size to 16 pt.

  2. The second label will include a description of the map you created. Again, insert a label and name it map description. In the Main Properties enter the text map description. In the Main Properties we will also include:

    printed on: [%format_date(now(),'dd.MM.yyyy')%]
    

    Here we used two Date and Time functions (now and format_date).

    Defina a posição do rótulo.

    1. For X, use the expression:

      @sw_layout_margin + 3
      
    2. For Y, use the expression:

      @sw_layout_margin + 11.5
      
  3. The third label will include information about your organisation. First we will create some variables in the Variables menu of the Item Properties. Go to the Layout menu, click the adicionarSimbologia button each time and enter the names o_department, o_name , o_adress and o_postcode. In the second row enter the information about your organisation. We will use these variables in the Main Properties section.

    In Main Properties enter:

    [% @o_name %]
    [% @o_department %]
    [% @o_adress %]
    [% @o_postcode %]
    

    Defina a posição do rótulo.

    1. For X, use the expression:

      @layout_pagewidth - @sw_layout_margin - 49.5
      
    2. For Y, use the expression:

      @sw_layout_margin + 15.5
      
    3. For Width, use 49.00

    4. For Height, use the expression:

      @sw_layout_height_header - 15.5
      
../../../_images/dynamic_layout_organisation.png

4.2.4. moderado Follow Along: Adicionando imagens ao cabeçalho dinâmico

  1. Use the adicionarImagem Add Picture button to place a picture above your label organisation information. After entering the name organisation logo define the position and size of the logo:

    1. For X, use the expression:

      @layout_pagewidth - @sw_layout_margin - 49.5
      
    2. For Y, use the expression:

      @sw_layout_margin + 3.5
      
    3. For Width, use 39.292

    4. For Height, use 9.583

    To include a logo of your organisation you have to save your logo under your home directory and enter the path under Main Properties ► Image Source.

  2. Our layout still needs a north arrow. This will also be inserted by using setaNorte Add North Arrow. We will use the default north arrow. Define the position:

    1. For X, use the expression:

      @layout_pagewidth - @sw_layout_margin * 2 - 78
      
    2. For Y, use the expression:

      @sw_layout_margin + 9
      
    3. For Width, use 21.027

    4. For Height, use 21.157

4.2.5. moderado Follow Along: Criando a barra de escala do cabeçalho dinâmico

  1. To insert a scalebar in the header click on scaleBar Add Scale Bar and place it in the rectangle above the north arrow. In Map under the Main Properties choose your main map(Map 1). This means that the scale changes automatically according to the extent you choose in the QGIS main canvas. Choose the Style Numeric. This means that we insert a simple scale without a scalebar. The scale still needs a position and size.

    1. For X, use the expression:

      @layout_pagewidth - @sw_layout_margin * 2 - 78
      
    2. For Y, use the expression:

      @sw_layout_margin + 1
      
    3. For Width, use 25

    4. For Height, use 8

    5. Coloque o ponto de referência no centro.

Congratulations! You have created your first dynamic map layout. Take a look at the layout and check if everything looks the way you want it! The dynamic map layout reacts automatically when you change the page properties. For example, if you change the page size from DIN A4 to DIN A3, click the atualizar Refresh view button and the page design is adapted.

../../../_images/dynamic_layout.png

4.2.6. What’s Next?

Na próxima página, você receberá uma tarefa para concluir. Isso permitirá que você pratique as técnicas que aprendeu até agora.