2

I have an issue with my script. I would like to show a part of map centered on selected features with a template. The script open the print composer but it don't refresh correctly the map, it's blank. I followed this tutorial. In the map element of the composer, scale is set to 0.0000000000, min X max X min Y max Y are set to 0.000. here the concerned part of my script:

                self.iface.mapCanvas().zoomToSelected()
                self.iface.mapCanvas().zoomScale(1000)

                canvas = QgsMapCanvas()
                myFile = os.path.join('C:\Users\mypath\\', 'template.qpt')
                myTemplateFile = file(myFile, 'rt')
                myTemplateContent = myTemplateFile.read()
                myTemplateFile.close()
                myDocument = QDomDocument()
                myDocument.setContent(myTemplateContent, False)
                newcomp = iface.createNewComposer()
                newcomp.composition().loadFromTemplate(myDocument)

                composition = newcomp.composition()
                map_item = composition.getComposerItemById('map')
                map_item.setMapCanvas(canvas)

                map_item.zoomToExtent(canvas.extent())

                composition.refreshItems()

My Qgis version is 2.16.3. And the Python console of Qgis don't show any error.

2

I change my code and it work now !

                self.iface.mapCanvas().zoomToSelected()
                self.iface.mapCanvas().zoomScale(1000)


                canvas = iface.mapCanvas()
                myFile = os.path.join('C:\Users\mypath\\', 'template.qpt')
                myTemplateFile = file(myFile, 'rt')
                myTemplateContent = myTemplateFile.read()
                myTemplateFile.close()
                myDocument = QDomDocument()
                myDocument.setContent(myTemplateContent, False)
                newcomp = iface.createNewComposer()
                newcomp.composition().loadFromTemplate(myDocument)

                composition = newcomp.composition()
                map_item = composition.getComposerItemById('map')
                map_item.setMapCanvas(canvas)

                map_item.zoomToExtent(canvas.extent())                    

                composition.refreshItems()

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.