4

I want to create a template using the "Print Composer" from my layers loaded in QGIS 2.18.7 (Las Palmas). The problem is that it seems the map element is not "synchronized" with the map view. For instance, if I zoom-in on the map view, I want the map element on the template also being automatically updated with the zoom.

Another problem related to this is that I don't know how to display the selected features in the template: enter image description here

Is there any way of updating the map template automatically?

  • 1
    What are you trying to achieve? There's at least a button for updating the map window in the print composer. Select the map item -> properties -> extent -> Set to map canvas extent – Nightwatch Jul 4 '17 at 9:04
  • 1
    In terms of showing selected features in the print composer, this was asked before: How to bring feature selection into Printing Composer?. However, it seems that in the recent versions of QGIS, it no longer seems possible unless you perform a query... – Joseph Jul 4 '17 at 9:33
  • Thanks @Joseph I've already read the question you linked. With the query I can zoom-in to the selected features but what I want to achieve is to highlight them as in the map view. – ustadra Jul 4 '17 at 9:50
  • 1
    @ustadra - The only workaround I can think of at the moment is to style your layer with something like Rule-based. You could create an attribute field in your layer and only update the selected features with a specific value. Then give one colour to all features and another colour to features with the specific value. – Joseph Jul 4 '17 at 10:01
  • 1
    @Joseph Following your advice I have managed to see the selected item but without adding a new field, I have put it in response to this question – ustadra Jul 11 '17 at 8:32
8

With a little help from Python, we could create a function which refreshes your composer map each time the canvas extent (from the main window) has changed. In otherwords, whenever you pan or zoom in/out of the canvas, the composer map will also update. You can type the following code into the Python Console:

def update_composer():
    active_Composer = iface.activeComposers()
    canvas = iface.mapCanvas()
    composer = active_Composer[0]
    composition = composer.composition()
    maps = [item for item in composition.items() if item.type() == QgsComposerItem.ComposerMap and item.scene()]
    composer_map = maps[0]
    moveX = composer_map.extent().center().x()-canvas.extent().center().x()
    moveY = composer_map.extent().center().y()-canvas.extent().center().y()
    unitCon = composer_map.mapUnitsToMM()
    composer_map.moveContent(-moveX * unitCon, moveY * unitCon)
    composer_map.setNewScale(canvas.scale())

iface.mapCanvas().extentsChanged.connect(update_composer)

Credit to the author Ed B for his plugin: Sync Composer With Map of which this code was based upon (you can download/install this plugin from the menubar: Plugins > Manage and Install Plugins...).

  • @ustadra - Most welcome, glad it was helpful :) – Joseph Jul 4 '17 at 10:36
4

I try this solution:

def print_image(self):
    layers = QgsMapLayerRegistry.instance().mapLayers()
    """select layer by name"""
    for name, layer in layers.iteritems():
        if(layer.name()=="[YOUR LAYER NAME]"):

            lay=layer
    self.iface.mapCanvas().zoomToSelected( lay )
    self.iface.mapCanvas().refresh()
    path = 'C:\new folder\image.png'
    self.iface.mapCanvas().saveAsImage(path)

This makes a photo of the map view and I get the selected objects

2

I have finally used this solution:

In layer Properties->Style->Rule-based enter image description here

With $id=$atlasfeatureid I can see the items selected by the atlas

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.