1

In a standalone app I want to create a print composer with the map, a legend and a scale bar. I try with different codes but without success. I search in several foruns but I didn't find an answer.

 basename_R = fileInfo_R.baseName()
 R_color_raster = QgsRasterLayer(R_color, basename_R)  

 layerset = []
 QgsMapLayerRegistry.instance().addMapLayer(R_color_raster)
 layerset.append(R_color_raster.id())
 myMap = QgsMapSettings()

 myComposition = QgsComposition(myMap)
 myComposition.setPlotStyle(QgsComposition.Print)

 #legend
 legend = QgsComposerLegend(myComposition)
 legend.model().setLayerSet(myMap.layers())
 legend.model().setLayerSet
 myComposition.addComposerLegend(legend)
 legend.setItemPosition (25,122)
 legend.setFrameEnabled(True)
 legend.setScale(.7)   

 output_jpeg = "..../Factor.jpg"
 myImage = myComposition.printPageAsRaster(0)
 myImage.save(output_jpeg)

Legend works fine but I have no success with map composer. I try everything but nothing. Any ideas?

UPDATE

So I find this information that I think its useful for anyone that uses this composer option: Making serial maps from template using PyQGIS?

In version 2.4 and above the maprenderer is replaced by mapsettings. I try this and I am still trying but without success in compose my map. Does anyone try and get this right?

UPDATE

Ok, I find the solution to "add map". Here is the code:

fileInfo_R = QFileInfo(R_color)
 basename_R = fileInfo_R.baseName()
 path = fileInfo_R.filePath()
 R_color_raster = QgsRasterLayer(path, basename_R)  

 layerset = []
 QgsMapLayerRegistry.instance().addMapLayer(R_color_raster)
 layerset.append(R_color_raster.id())
 myMap = QgsMapSettings()

 mapRectangle = myMap.fullExtent()
 myMap.setExtent(mapRectangle)
 myMap.setOutputSize(QSize(1300,900))

 myComposition = QgsComposition(myMap)
 myComposition.setPlotStyle(QgsComposition.Print)

 x,y = 0,0
 w,h = myComposition.paperWidth(), myComposition.paperHeight()
 composerMap = QgsComposerMap(myComposition,x,y,w,h)
 composerMap.gridEnabled()
 myComposition.addComposerMap(composerMap)
 composerMap.setLayerSet(myMap.layers())

 #legend
 legend = QgsComposerLegend(myComposition)
 legend.model().setLayerSet(myMap.layers())
 legend.model().setLayerSet
 myComposition.addComposerLegend(legend)
 legend.setItemPosition (25,122)
 legend.setFrameEnabled(True)
 legend.setScale(.7)   

 item = QgsComposerScaleBar(myComposition)
 myComposition.addComposerScaleBar(item)
 item.setStyle('Numeric') 
 item.setItemPosition(0,122)
 item.update()

 output_jpeg = "..../Factor.jpg"
 myImage = myComposition.printPageAsRaster(0)
 myImage.save(output_jpeg)

Ok, but now I don't need the legend, I need to insert a picture and I try with addComposerPicture but without success. Any idea? Also I want to add the scale bar but the numeric gives me 1:1. I assume that the coordinate system isn't readed. Any ideas?

Bárbara Duarte
  • 1,451
  • 4
  • 18
  • 31

0 Answers0