Bug report #10887

Qgis return empty QgsComposition from standalone application

Added by Fabio Saccon over 6 years ago. Updated about 2 years ago.

Status:Closed
Priority:High
Assignee:-
Category:Map Composer/Printing
Affected QGIS version:2.10.0 Regression?:Yes
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:19255

Description

Qgis 2.4 version return empty QgsComposition from standalone application
QgsComposition empty from QgsMapRenderer()
This is the code that work with 2.2
if I use myMapRenderer=iface.mapCanvas().mapRenderer() from python console it work fine

project = "myproject.qgs"

QgsProject.instance().setFileName(project)
QgsProject.instance().read()

layertot = QgsMapLayerRegistry.instance().mapLayers()
myMapRenderer = QgsMapRenderer()
myMapRenderer.setLayerSet(layertot.keys())
mapRectangle = QgsRectangle(xmin,ymin,xmax,ymax)
myMapRenderer.setExtent(mapRectangle)

comp = QgsComposition(myMapRenderer)
comp.setPlotStyle(QgsComposition.Print)
comp.setPaperSize(180,180)
comp.setPrintResolution(150)
composerMap = QgsComposerMap(comp, 0,0,180,180)
comp.addItem(composerMap)

dpi = comp.printResolution()
dpmm = dpi / 25.4
width = int(dpmm * comp.paperWidth())
height = int(dpmm * comp.paperHeight())
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
image.fill(0)
imagePainter = QPainter(image)
sourceArea = QRectF(0, 0, comp.paperWidth(), comp.paperHeight())
targetArea = QRectF(0, 0, width, height)
comp.render(imagePainter, targetArea, sourceArea)
imagePainter.end()
image.save("c:/temp/test.png", "png")

History

#1 Updated by Nyall Dawson over 6 years ago

  • Status changed from Open to Feedback
  • Category set to Map Composer/Printing

It's an unfortunate API break in 2.4. You need to pass a QgsMapSettings object to the constructor of QgsComposition. So something like:

myMapSettings = QgsMapSettings()
comp = QgsComposition(myMapSettings)

Please confirm that this works fine for you.

#2 Updated by Fabio Saccon over 6 years ago

Also this produces an empty result

myMapRenderer = QgsMapSettings()
myMapRenderer.setLayers(layerset)

comp = QgsComposition(myMapRenderer)

#3 Updated by Giovanni Manghi over 6 years ago

Nyall Dawson wrote:

It's an unfortunate API break in 2.4.

should this be tagged as blocker (as a regression)?

#4 Updated by Nyall Dawson over 6 years ago

Fabio - can you test explicitly setting your composer map's extent? ie composerMap.setNewExtent(mapRectangle)

#5 Updated by Nyall Dawson about 6 years ago

  • Status changed from Feedback to Closed
  • Resolution set to invalid

Closed due to lack of feedback

#6 Updated by Eric Kuijt over 5 years ago

  • Target version set to Version 2.10
  • Status changed from Closed to Reopened

Qgis 2.10 version still returns empty QgsComposition from standalone application

# canvas = iface.mapCanvas() # seems to work
mapRenderer = canvas.mapRenderer()

composition = QgsComposition(mapRenderer)
composition.setPlotStyle(composition.Print)

dpi = composition.printResolution() 
dpmm = dpi / 25.4 
width = int(dpmm * composition.paperWidth()) 
height = int(dpmm * composition.paperHeight())  

# add a map to the composition 
x, y = 0, 0 
w, h = composition.paperWidth(), composition.paperHeight() 

composerMap = QgsComposerMap(composition, x, y, w, h) 

# tried this
mapRectangle = QgsRectangle(0,0,400,400)
composerMap.setNewExtent(mapRectangle)

composition.addItem(composerMap) 

# create output image and initialize it 
image = QtGui.QImage(QtCore.QSize(width, height), QtGui.QImage.Format_ARGB32) 
image.setDotsPerMeterX(dpmm * 1000) 
image.setDotsPerMeterY(dpmm * 1000) 
image.fill(0) 

# render the composition 
imagePainter = QtGui.QPainter(image) 
sourceArea = QtCore.QRectF(0, 0, composition.paperWidth(), composition.paperHeight()) 
targetArea = QtCore.QRectF(0, 0, width, height) 
composition.render(imagePainter, targetArea, sourceArea) 
imagePainter.end() 

image.save("/home/eric/%s.jpg" % province , "jpg")

I tried this to see if anything would happen, but it didn't.

mapRectangle = QgsRectangle(0,0,400,400)
composerMap.setNewExtent(mapRectangle)

#7 Updated by Giovanni Manghi about 5 years ago

  • Target version deleted (Version 2.10)

#8 Updated by Giovanni Manghi almost 4 years ago

  • Target version set to Version 2.14
  • Affected QGIS version changed from 2.4.0 to 2.10.0
  • Resolution deleted (invalid)
  • Priority changed from Normal to Severe/Regression

#9 Updated by Giovanni Manghi almost 4 years ago

  • Status changed from Reopened to Feedback

What is the status of this issue on the most recent QGIS releases?

#10 Updated by Giovanni Manghi over 3 years ago

  • Description updated (diff)
  • Status changed from Feedback to Open
  • Target version deleted (Version 2.14)

#11 Updated by Giovanni Manghi over 3 years ago

  • Regression? set to Yes

#12 Updated by Giovanni Manghi over 3 years ago

  • Priority changed from Severe/Regression to High

#13 Updated by Giovanni Manghi over 3 years ago

  • Easy fix? set to No

#14 Updated by Jürgen Fischer over 3 years ago

  • Subject changed from Qgis 2.4 version return empty QgsComposition from standalone application to Qgis return empty QgsComposition from standalone application

#15 Updated by Regis Haubourg about 2 years ago

  • Status changed from Open to Closed

Closing - QGIS version outdated and next LTR will be QGIS 3 soon. Please reopen if still concerned

Also available in: Atom PDF