I've basically copied the script below with minor tweaks from a few other posts here on stackexchange. When I run my code and all I get for output is a TIF that's white with a legend. For the life of me I can't figure out what's wrong.

My goal is to take a shapefile (shp) and convert it to a tif with the contour lines labeled in elevation. I'm able to open the shapefile up in qgis, and overlay it on the dem file from which I made the contours. I'm even able to overlay the two in a window (canvas), using another script I wrote, but I can't for the life of me get it to save to a .tif file with the contours and labels.

Here is my code as it now stands:

from qgis.core import * from qgis.gui import * from qgis.utils import *

import PyQt4
from PyQt4.QtCore import *
from PyQt4.QtGui import *

print '\n'
#initialize qgis
app = QgsApplication([], True)
app.setPrefixPath('c:/OSGeo4W64/apps/qgis', True)

# Creat a map instance
mapinstance = QgsMapLayerRegistry.instance()

#add layer to instance
vlayer = QgsVectorLayer('c:/Data/Clairmont/quarterContour.shp', 'contour', 'ogr')
rlayer = QgsRasterLayer('c:/Data/Clairmont/smoothQuarter.tif', 'smoothQuarter')

# mapinstance.addMapLayer(vlayer)

if vlayer.isValid():
    print 'Vector layer is valid'
    print 'Vector layer is not valid'

if rlayer.isValid():
    print 'Raster layer is valid'

palyr = QgsPalLayerSettings()
palyr.enabled = True
palyr.fieldName = 'elev'
palyr.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'8', '')
palyr.placement = QgsPalLayerSettings.Line

# vlayer.setCustomProperty("labeling", "pal")
# vlayer.setCustomProperty("labeling/enabled", "true")
# vlayer.setCustomProperty("labeling/fontFamily", "Arial")
# vlayer.setCustomProperty("labeling/fontSize", "14")
# vlayer.setCustomProperty("labeling/fieldName", "ELEV")
# vlayer.setCustomProperty("labeling/placement", "2")

mapRenderer = QgsMapRenderer()
lst = [vlayer.id(), rlayer.id()]
print type(lst)

# Set Extent
# rect = rlayer.extent()
rect = vlayer.extent()
goodExtent = mapRenderer.setExtent(rect)
print 'mapExtent is valid: %s' %(goodExtent)
print 'mapRenderer.extent X:%d, Y:%d' %(mapRenderer.extent().width(), mapRenderer.extent().height())

# enum: 0 for mm, 1 for pixels
# mapRenderer.setOutputUnits(1)

print 'Output DPI: %d' %(mapRenderer.outputDpi())
print 'Output Units: %s' %(mapRenderer.outputUnits())

# print 'Output Size X:%i  Y:%i' %(mapRenderer.outputSize().width(), mapRenderer.outputSize.height())

# print 'Map Units: %s' %(mapRenderer.mapUnits())
# print 'Map units per pixel: %s' %(mapRenderer.mapUnitsPerPixel())
print 'vlayer.id(): %s' %(vlayer.id())

# mapRenderer.setLayerSet([QgsMapCanvasLayer(vlayer)])

# canvas = QgsMapCanvas()
# canvas.setCanvasColor(Qt.white)
# canvas.enableAntiAliasing(True)
# canvas.show()
# canvas.setExtent(vlayer.extent())
# canvas.setLayerSet([QgsMapCanvasLayer(vlayer)])

c = QgsComposition(mapRenderer)
# c.setPlotStyle(QgsComposition.Preview)

print 'Paperwidth: %d' %(c.paperWidth())
print 'Paperheight: %d' %(c.paperHeight())

x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c,x,y,w,h)
composerLabel = QgsComposerLabel(c)

# This seems to just be a bunch of legend stuff, but it doesn't do anything
# composerLabel.adjustSizeToText()
# c.addItem(composerLabel)
# //c.addComposerMap(composerMap)
# composerLabel.setItemPosition(20,10)
# composerLabel.setItemPosition(20,10, 100, 30)

legend = QgsComposerLegend(c)

# Set Image Sizing
print 'printResolution and dpi: %d' %(c.printResolution())
dpi = c.printResolution()
dpmm = dpi/25.4
# dpm = dpi/0.0254
width = int(dpmm * c.paperWidth())
height = int(dpmm * c.paperHeight())

print 'Width: %i   Height: %i' %(width, height)

# img = QImage(QSize(width, height), QImage.Format_RGB32)
# img = QImage(5000, 5000, QImage.Format_RGB32)
img = QImage(width, height, QImage.Format_RGB32)
img.setDotsPerMeterX(dpmm * 1000)
img.setDotsPerMeterY(dpmm * 1000)
print 'Dots per meter X: %d  Dots per meter Y: %d' %(img.dotsPerMeterX(), img.dotsPerMeterY())
imagePainter = QPainter(img)
sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())
targetArea = QRectF(0, 0, width, height)

# Render Image
c.render(imagePainter, targetArea, sourceArea)
img.save("out.tif", "tif")

# app.exec_()
# app.exitQgis()

print '\n'
print '*********************************************************************'
print '*************************Script Complete*****************************'
print '*********************************************************************'
print '\n'

All I get is a tif of the wrong size (pixelsx X pixelsy) that is entirely white with a legend in the top left corner. I know for a fact that my shapefile is fine in the sense that I'm able to make it overlay in QGIS Desktop programatically. I just can't figure out what I'm doing wrong in my script.

Another hint that my shapefile and dem raster are ok is that I've got some other code to make these appear as an overlay in a QT window and they come up just fine... including the elevation labels.

closed as unclear what you're asking by PolyGeo Jun 23 '16 at 3:34

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • It's worthwhile for you to know that the output to my console is: Vector layer is valid Raster layer is valid <type 'list'> mapExtent is valid: True mapRenderer.extent X:430, Y:320 Output DPI: 0 Output Units: 0 vlayer.id(): contour20150810081129177 Paperwidth: 297 Paperheight: 210 printResolution and dpi: 300 Width: 3507 Height: 2480 Dots per meter X: 11811 Dots per meter Y: 11811 ********************************************************************* *************************Script Complete***************************** ********************************************************************* – Marc Aug 10 '15 at 15:49
  • I should also mention that my tif is a geotiff, and my shapefile also seems to be geo-references. All this to say that I recently discovered this new property of extent() called center(). My mapRenderer.extent().center() is 486,080 3,950,623. This is to say that I may just be rendering some rectangle that doesn't include my shp file or the raster from which that shapefile was created. Hope this helps. I'm going to try and fix this myself and I'll let you know if I get this figured out. – Marc Aug 11 '15 at 0:51
  • Please use the edit button to revise your question with any additional information that can make what you are asking clearer. To me it looks like it needs heavy revision and the extraction of a code snippet that illustrates where you are stuck. – PolyGeo Jun 23 '16 at 3:33