从模板以编程方式加载composer并使用PyQGIS生成图集


10

我正在尝试构建一个插件来从文件加载打印作曲家,生成图集并导出到图像。到目前为止,我已经成功加载了模板并将其导出到图像。

我无法将图例中的任何图层(也在目录中)添加到导出的地图中,这将导致生成空白地图,并且所有字段表达式均不起作用。

    # Get layers in the legend and append, must be a cleaner way to do this?
    layers = self.iface.legendInterface().layers()
    layerStringList = []
    for layer in layers:
        layerID = layer.id()
        layerStringList.append(layerID)

    # Add layer to map render
    myMapRenderer = QgsMapRenderer()
    myMapRenderer.setLayerSet(layerStringList)
    myMapRenderer.setProjectionsEnabled(False)

    # Load template
    myComposition = QgsComposition(myMapRenderer)
    myFile = os.path.join(os.path.dirname(__file__), 'MMR_Template.qpt')
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)

    # Save image
    myImagePath = os.path.join(os.path.dirname(__file__), 'come_on.png')
    myImage = myComposition.printPageAsRaster(0)
    myImage.save(myImagePath)

这是加载的模板的摘要,应设置图集:

 <Atlas hideCoverage="false" featureFilter="reference = '61922'"    coverageLayer="desktop_search20130615160118593" fixedScale="true" composerMap="0" singleFile="false" filenamePattern="&quot;reference&quot;" enabled="true" filterFeatures="true" sortFeatures="true" sortKey="0" sortAscending="true" margin="1"/>

我也不确定将toc中的所有图层添加到QgsMapRenderer()实例的最佳方法。

谢谢

Answers:


18

如果有人感兴趣,这里就是我最终得到的代码。这将打开/关闭目录中的特定图层(从图层列表中),从文件中加载选定的作曲家模板,生成图集并导出地图。最后,将目录返回到其原始状态。

def sort_toc(self):

    # Turn on/off layers as required by search type
    legend = self.iface.legendInterface()
    layers = legend.layers()
    wanted_layers = metal_wanted
    global turn_on, turn_off, atlas_desktop
    turn_off = []
    turn_on = []
    all_layers = []
    for layer in layers:
        layername = layer.name()
        all_layers.append(layername)
        layerid = layer.id()
        if layername == "desktop_search":
            atlas_desktop = layer
        if layername in wanted_layers and legend.isLayerVisible(layer) is False:
            turn_off.append(layer)
            legend.setLayerVisible(layer, True)
        if layername not in wanted_layers and legend.isLayerVisible(layer) is True:
            turn_on.append(layer)
            legend.setLayerVisible(layer, False)
        else:
            pass

    # Checks for required layers missing from map file
    for layer in wanted_layers:
        missing = []
        if layer not in all_layers:
            missing.append(layer)
        else:
            pass
    if not missing:
        pass
    else:
        QMessageBox.warning(self.iface.mainWindow(), "Missing layers", "Required layers are missing from your map file. Details: %s" % (str(missing)))
    return atlas_desktop

def quick_export(self, ref, stype, scale):

    # Add all layers in map canvas to render
    myMapRenderer = self.iface.mapCanvas().mapRenderer()

    # Load template from file
    myComposition = QgsComposition(myMapRenderer)
    myFile = os.path.join(os.path.dirname(__file__), 'MMR_Template.qpt')
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)

    # Get map composition and define scale
    myAtlasMap = myComposition.getComposerMapById(0)
    myAtlasMap.setNewScale(int(scale))

    # Setup Atlas
    myAtlas = QgsAtlasComposition(myComposition)
    myAtlas.setCoverageLayer(atlas_desktop) # Atlas run from desktop_search
    myAtlas.setComposerMap(myAtlasMap)
    myAtlas.setFixedScale(True)
    myAtlas.fixedScale()
    myAtlas.setHideCoverage(False)
    myAtlas.setFilterFeatures(True)
    myAtlas.setFeatureFilter("reference = '%s'" % (str(ref)))
    myAtlas.setFilterFeatures(True)

    # Generate atlas
    myAtlas.beginRender()
    for i in range(0, myAtlas.numFeatures()):
        myAtlas.prepareForFeature( i )
        jobs = r"\\MSUKSERVER\BusinessMan Docs\Jobs"
        job_fol = os.path.join(jobs, str(ref))
        output_jpeg = os.path.join(job_fol, ref + "_BMS_plan.jpg")
        myImage = myComposition.printPageAsRaster(0)
        myImage.save(output_jpeg)
    myAtlas.endRender()

def return_toc(self):

    # Revert layers back to pre-script state (on/off)
    legend = self.iface.legendInterface()
    for wanted in turn_on:
        legend.setLayerVisible(wanted, True)
    for unwanted in turn_off:
        legend.setLayerVisible(unwanted, False)

您可以分享创建的各种功能的用法示例吗?这些都在QGIS Python控制台中吗?
raphael

@raphael我很早以前就构建了这个插件,但是我将尝试找出一个例子。
马特

谢谢马特(Matt)与我们分享。有可能获得这些功能的使用示例吗?非常感谢 !内森
致谢

2

也许这可以使您获得所有当前图层:

registry = QgsMapLayerRegistry.instance()
layers = registry.mapLayers().values()

嗯,您获取所有图层的方式应该可以正常工作
akbargumbira 2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.