QGIS开发之地图渲染与打印

2 篇文章 0 订阅

使用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()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QGIS是一种开源的地理信息系统软件,其可以用于地图制作、空间分析、数据管理等功能。而QGIS开发桌面应用,意味着利用QGIS软件的开发工具和API来创建具有特定功能的自定义桌面应用程序。 QGIS开发桌面应用的过程可以分为以下几个步骤: 1. 界面设计:通过QGIS提供的设计工具,可以创建自定义的界面布局,包括工具栏、菜单选项、图标等,使用户可以方便地访问不同的功能。 2. 数据管理:QGIS可以通过其API提供对地理数据库的访问和管理功能,开发者可以利用这些功能来创建自定义的数据管理工具,例如数据导入、查询、编辑等操作。 3. 空间分析:QGIS提供了丰富的空间分析功能,开发者可以利用这些功能来处理地理数据,例如缓冲区分析、叠加分析、路径规划等,通过QGIS开发桌面应用,可以将这些功能集成到自定义应用中。 4. 插件开发QGIS支持插件机制,可以通过开发插件来扩展QGIS的功能。开发者可以利用QGIS的API来创建自定义插件,例如添加新的地理处理算法、导入导出数据格式等。 总的来说,QGIS开发桌面应用具有灵活性和自定义性,开发者可以根据自己的需求和项目要求来定制应用程序,将QGIS的强大功能与自己的业务逻辑结合起来,实现特定的地理信息系统应用。无论是制图、数据管理还是空间分析,QGIS都提供了丰富的工具和API支持,使开发者能够快速而灵活地开发出符合需求的桌面应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值