4.2. Lesson: 创建动态打印布局

现在您已经学会了创建基本的地图布局,我们将更进一步,创建一个能够动态适应地图范围和页面属性的地图布局,例如,当您更改页面大小时。此外,创建日期将动态调整。

4.2.1. 适度 Follow Along: Creating the dynamic map canvas

  1. 加载ESRI shapefile格式的数据集 protected_areas.shpplaces.shprivers.shpwater.shp 添加到地图画布中,并调整其属性以适应您自己的方便。

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

  3. 我们想创建一个由标题和南非斯韦伦达姆附近地区的地图组成的地图布局。布局的边距应为7.5 mm,页眉高度应为36 mm。

  4. 创建名为的地图项目 main map 在画布上并转到 Layout 面板。向下滚动到 Variables 部分,并找到 Layout 一部份。这里我们设置了一些变量,您可以在整个动态打印布局中使用这些变量。转到 Layout 面板,然后向下滚动到 Variables 一节。第一个变量将定义边距。按下按钮 符号系统添加 按钮,然后键入名称 sw_layout_margin 。将该值设置为 7.5 。按下按钮 符号系统添加 按钮,然后输入名称 sw_layout_height_header 。将该值设置为 36

  5. 现在,您已经准备好通过变量自动创建地图画布的位置和大小。确保您的地图项目已选中,请转到 Item Properties 面板中,向下滚动到并打开 Position and Size 一节。单击 DataDefineExpress打开 Data defined overrideX 而从 Variables 进入,选择 @sw_layout_margin

  6. 单击 DataDefineExpress打开 Data defined overrideY ,选择 Edit... 并键入公式::

    to_real(@sw_layout_margin) + to_real(@sw_layout_height_header)
    
  7. 您可以使用的变量创建地图项的大小 WidthHeight 。单击 DataDefineExpress打开 Data defined overrideWidth 并选择 Edit ... 再来一次。填写公式::

    @layout_pagewidth - @sw_layout_margin * 2
    

    单击 DataDefineExpress打开 Data defined overrideHeight 并选择 Edit ... 。在此填写公式::

    @layout_pageheight -  @sw_layout_height_header -  @sw_layout_margin * 2
    
  8. 我们还将创建一个包含主画布地图范围坐标的网格。去 Item Properties 再次选择并选择 Grids 一节。通过单击 符号系统添加 纽扣。点击 Modify grid ... 并将 IntervalXYOffset 根据您在QGIS主画布中选择的地图比例。这个 Grid type Cross 非常适合我们的目的。

4.2.2. 适度 Follow Along: Creating the dynamic header

  1. 插入一个矩形,该矩形将包含带有 添加基本形状 Add Shape 纽扣。在 Items 面板输入名称 header

  2. 同样,请转到 Item Properties 并打开 Position and Size 一节。vbl.使用 DataDefineExpress打开 Data defined override ,请选择 sw_layout_margin 变量For X 以及为了 YWidth 应由以下表述来定义:

    @layout_pagewidth - @sw_layout_margin * 2
    

    Height 由. sw_layout_height_header 变量。

  3. 我们将插入一条水平线和两条垂直线,以使用 添加节点形状 Add Node Item 。创建一条水平线和两条垂直线并将其命名 Horizontal lineVertical line 1 Vertical line 2

    1. 对于水平线:

      1. X 添加到变量 sw_layout_margin

      2. 将表达式设置为 Y 致:

        @sw_layout_margin + 8
        
      3. 将表达式设置为 Width 致:

        @layout_pagewidth -  @sw_layout_margin * 3 - 53.5
        
    2. 对于第一条垂直线:

      1. 将表达式设置为 X 致:

        @layout_pagewidth - @sw_layout_margin * 2 - 53.5
        
      2. Y 添加到变量 sw_layout_margin

      3. 高度必须与我们创建的页眉相同,因此设置 Height 添加到变量 sw_layout_height_header

    3. 第二条垂直线放置在第一条垂直线的左侧。

      1. 将表达式设置为 X 致:

        @layout_pagewidth - @sw_layout_margin * 2 - 83.5
        
      2. Y 添加到变量 sw_layout_margin

      3. 高度应与另一条垂直线相同,因此设置 Height 添加到变量 sw_layout_height_header

    下图显示了我们的动态布局的结构。我们将在线条创建的区域中填充一些元素。

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

4.2.3. 适度 Follow Along: Creating labels for the dynamic header

  1. 您的QGIS项目的标题可以自动包括在内。标题设置在 Project Properties 。插入带有以下选项的标签 标签 Add Label 按钮,然后输入名称 project title (variable) 。在 Main PropertiesItems Properties 面板输入表达式::

    [%@project_title%]
    

    设置标签的位置。

    1. X ,请使用表达式::

      @sw_layout_margin + 3
      
    2. Y ,请使用表达式::

      @sw_layout_margin + 0.25
      
    3. Width ,请使用表达式::

      @layout_pagewidth - @sw_layout_margin *2 - 90
      
    4. 请输入 11.25Height

    在……下面 Appearance 将字体大小设置为16磅。

  2. 第二个标签将包括对您创建的地图的描述。同样,插入一个标签并将其命名 map description 。在 Main Properties 输入文本 map description 。在 Main Properties 我们还将包括:

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

    在这里,我们使用了两个 Date and Time 功能 (nowformat_date )。

    设置标签的位置。

    1. X ,请使用表达式::

      @sw_layout_margin + 3
      
    2. Y ,请使用表达式::

      @sw_layout_margin + 11.5
      
  3. 第三个标签将包括有关您的组织的信息。首先,我们将在 Variables 菜单中的 Item Properties 。转到 Layout 菜单中,单击 符号系统添加 按钮,并输入名称 o_departmento_nameo_adresso_postcode 。在第二行中输入有关您的组织的信息。我们将在 Main Properties 一节。

    在……里面 Main Properties 输入::

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

    设置标签的位置。

    1. X ,请使用表达式::

      @layout_pagewidth - @sw_layout_margin - 49.5
      
    2. Y ,请使用表达式::

      @sw_layout_margin + 15.5
      
    3. Width ,使用 49.00

    4. Height ,请使用表达式::

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

4.2.4. 适度 Follow Along: Adding pictures to the dynamic header

  1. 使用 添加图像 Add Picture 按钮将图片放在标签上方 organisation information 。在输入名称后 organisation logo 定义徽标的位置和大小:

    1. X ,请使用表达式::

      @layout_pagewidth - @sw_layout_margin - 49.5
      
    2. Y ,请使用表达式::

      @sw_layout_margin + 3.5
      
    3. Width ,使用 39.292

    4. Height ,使用 9.583

    要包括贵组织的徽标,您必须将徽标保存在主目录下,然后在下输入路径 Main Properties ► Image Source

  2. 我们的布局仍然需要一个指北针。这也将通过使用以下命令插入 北向箭头 Add North Arrow 。我们将使用默认的指北针。定义职位:

    1. X ,请使用表达式::

      @layout_pagewidth - @sw_layout_margin * 2 - 78
      
    2. Y ,请使用表达式::

      @sw_layout_margin + 9
      
    3. Width ,使用 21.027

    4. Height ,使用 21.157

4.2.5. 适度 Follow Along: Creating the scalebar of the dynamic header

  1. 要在标题中插入比例尺,请单击 比例尺 Add Scale Bar 并将其放置在指北针上方的矩形中。在……里面 MapMain Properties 选择您的 main map(Map 1) 。这意味着比例会根据您在QGIS主画布中选择的范围自动更改。选择 Style Numeric 。这意味着我们插入一个不带比例尺的简单比例尺。天平还需要一个位置和大小。

    1. X ,请使用表达式::

      @layout_pagewidth - @sw_layout_margin * 2 - 78
      
    2. Y ,请使用表达式::

      @sw_layout_margin + 1
      
    3. Width ,使用 25

    4. Height ,使用 8

    5. 请将 Reference point 在中间。

祝贺你!您已经创建了您的第一个动态地图布局。看一看布局,检查是否一切看起来都像你想要的那样!更改时,动态地图布局会自动做出反应 page properties 。例如,如果将页面大小从DIN A4更改为DIN A3,请单击 刷新 Refresh view 按钮,并对页面设计进行了调整。

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

4.2.6. What's Next?

在下一页,将给你布置一项要完成的作业。这会让你练习到目前为止学到的技巧。