QGIS开发之地图渲染与打印

来源:互联网 发布:flow水刀软件 编辑:程序博客网 时间:2024/06/18 07:25
  • 简单渲染
  • 使用不同CRS(坐标参考系统)渲染
  • 使用Map Composer输出

使用Map Composer输出地图

There are generally two approaches when input data should be rendered as a map: either do it quick way using QgsMapRenderer or produce more fine-tuned output by composing the map with QgsComposition class and friends.

当输入数据应该渲染为地图时,通常有两种方法:使用QgsMapRenderer或者通过使用QgsComposition类及其友元编写地图来产生更精细的输出。

Map composer is a very handy tool if you would like to do a more sophisticated output than the simple rendering shown above. Using the composer it is possible to create complex map layouts consisting of map views, labels, legend, tables and other elements that are usually present on paper maps. The layouts can be then exported to PDF, raster images or directly printed on a printer.

如果您想要做比上述简单渲染更复杂的输出,Map composer是非常方便的工具。 使用composer可以创建由地图视图,标签,图例,表格和纸质地图上通常存在的其他元素组成的复杂地图布局。 然后可以将布局导出为PDF,栅格图像或直接打印。

The composer consists of a bunch of classes. They all belong to the core library. QGIS application has a convenient GUI for placement of the elements, though it is not available in the GUI library. If you are not familiar with Qt Graphics View framework, then you are encouraged to check the documentation now, because the composer is based on it. Also check the Python documentation of the implementation of QGraphicView.

composer包含一系列的类,这些类都属于core library。QGIS应用程序有很方便的配置这些元素的GUI,不过在GUI library中并不可用。需要对Qt Graphics View框架比较熟悉。

The central class of the composer is QgsComposition which is derived from QGraphicsScene. Let us create one

mapRenderer = iface.mapCanvas().mapRenderer()c = QgsComposition(mapRenderer)c.setPlotStyle(QgsComposition.Print)

Composer的中心类是QgsComposition,它继承于QGraphicsScene。 python代码如上。

Note that the composition takes an instance of QgsMapRenderer. In the code we expect we are running within QGIS application and thus use the map renderer from map canvas. The composition uses various parameters from the map renderer, most importantly the default set of map layers and the current extent. When using composer in a standalone application, you can create your own map renderer instance the same way as shown in the section above and pass it to the composition.

composition使用我们期望运行的QGIS应用程序中QgsMapRenderer的实例,使用地图画布中的渲染器(renderer)。composer使用地图渲染器的各种参数,最重要的是地图图层的默认set和当前范围。 当在独立应用程序中使用composer时,可以按照上一节(Using Map Canvas)所示的相同方式创建自己的地图渲染器实例,并将其传递给composition。

It is possible to add various elements (map, label, …) to the composition — these elements have to be descendants of QgsComposerItem class. Currently supported items are:

可以向组合添加各种元素(地图,标签,…) - 这些元素必须是QgsComposerItem类的后代。 目前支持的项目有:

  • map — this item tells the libraries where to put the map itself. Here we create a map and stretch it over the whole paper size
x, y = 0, 0w, h = c.paperWidth(), c.paperHeight()composerMap = QgsComposerMap(c, x ,y, w, h)c.addItem(composerMap)
  • label — allows displaying labels. It is possible to modify its font, color, alignment and margin
composerLabel = QgsComposerLabel(c)composerLabel.setText("Hello world")composerLabel.adjustSizeToText()c.addItem(composerLabel)
  • legend
legend = QgsComposerLegend(c)legend.model().setLayerSet(mapRenderer.layerSet())c.addItem(legend)
  • sacle bar
item = QgsComposerScaleBar(c)item.setStyle('Numeric') # optionally modify the styleitem.setComposerMap(composerMap)item.applyDefaultSize()c.addItem(item)
  • arrow
  • picture
  • shape
  • table

By default the newly created composer items have zero position (top left corner of the page) and zero size. The position and size are always measured in millimeters

默认情况下,新创建的composer items 具有零位置(页面的左上角)和零大小。 位置和大小始终以毫米为单位。

# set label 1cm from the top and 2cm from the left of the pagecomposerLabel.setItemPosition(20, 10)# set both label's position and size (width 10cm, height 3cm)composerLabel.setItemPosition(20, 10, 100, 30)

A frame is drawn around each item by default. How to remove the frame

默认情况下,围绕每个项目绘制一个frame。 如何清除frame:

composerLabel.setFrame(False)

Besides creating the composer items by hand, QGIS has support for composer templates which are essentially compositions with all their items saved to a .qpt file (with XML syntax). Unfortunately this functionality is not yet available in the API.

除了手动创建composer tiems之外,QGIS还支持作composer模板,这些模板本质上是将所有项目保存到.qpt文件(具有XML语法)的组合。 不幸的是,该功能尚未在API中使用。

Once the composition is ready (the composer items have been created and added to the composition), we can proceed to produce a raster and/or vector output.

一旦composition准备就绪(composer items已经创建并添加到composition中),我们可以继续生成一个栅格 和/或 矢量的输出。

The default output settings for composition are page size A4 and resolution 300 DPI. You can change them if necessary. The paper size is specified in millimeters

组合的默认输出设置为页面大小A4和分辨率300 DPI。 如果需要,您可以更改它们。 纸张尺寸以毫米为单位

c.setPaperSize(width, height)c.setPrintResolution(dpi)

输出为栅格图像

The following code fragment shows how to render a composition to a raster image

以下代码片段显示了如何将composition渲染为栅格图像

dpi = c.printResolution()dpmm = dpi / 25.4width = int(dpmm * c.paperWidth())height = int(dpmm * c.paperHeight())# create output image and initialize itimage = QImage(QSize(width, height), QImage.Format_ARGB32)image.setDotsPerMeterX(dpmm * 1000)image.setDotsPerMeterY(dpmm * 1000)image.fill(0)# render the compositionimagePainter = QPainter(image)sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())targetArea = QRectF(0, 0, width, height)c.render(imagePainter, targetArea, sourceArea)imagePainter.end()image.save("out.png", "png")

输出为PDF

The following code fragment renders a composition to a PDF file

以下代码显示了如何将composition渲染为PDF文件

printer = QPrinter()printer.setOutputFormat(QPrinter.PdfFormat)printer.setOutputFileName("out.pdf")printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)printer.setFullPage(True)printer.setColorMode(QPrinter.Color)printer.setResolution(c.printResolution())pdfPainter = QPainter(printer)paperRectMM = printer.pageRect(QPrinter.Millimeter)paperRectPixel = printer.pageRect(QPrinter.DevicePixel)c.render(pdfPainter, paperRectPixel, paperRectMM)pdfPainter.end()