QGIS開發之地圖渲染與打印

使用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, 0
w, 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 style
item.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 page
composerLabel.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.4
width = int(dpmm * c.paperWidth())
height = int(dpmm * c.paperHeight())

# create output image and initialize it
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
image.fill(0)

# render the composition
imagePainter = 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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章