1

How can I render a vector layer into .tif file? I tried with below codes without luck.

PS: I am doing this on ubuntu server and running it via PyQGIS.

Python scripts

#!/usr/bin/python
from qgis.core import *
from qgis.utils import *
from PyQt4.QtCore import *

def main():
  qgis_prefix="/usr"    
  QgsApplication.setPrefixPath(qgis_prefix, True) 
  QgsApplication.initQgis()
  app = QgsApplication([], False)

  uri = "elevp.csv?delimiter=%s&xField=%s&yField=%s&elevField=%s" % (";","x","y","elev")
  vlayer = QgsVectorLayer(uri, "test", "delimitedtext")

  # create image
  img = QImage(QSize(800, 600), QImage.Format_ARGB32_Premultiplied)

  # set image's background color
  color = QColor(255, 255, 255)
  img.fill(color.rgb())

  # create painter
  p = QPainter()
  p.begin(img)
  p.setRenderHint(QPainter.Antialiasing)

  render = QgsMapRenderer()

  # set layer set
  lst = [vlayer.getLayerID()]
  render.setLayerSet(lst)

  # set extent
  rect = QgsRect(render.fullExtent())
  rect.scale(1.1)
  render.setExtent(rect)

  # set output size
  render.setOutputSize(img.size(), img.logicalDpiX())

  # do the rendering
  render.render(p)

  p.end()

  # save image
  img.save("render.tif","tif")


main()

I tried to capture the debug error with pdb and it show as below: -

-> /path/to/pyqgis/test.py(40)main()
-> rect = QgsRect(render.fullExtent())

Can I also ask if pdb is the correct debugging tool to use?

  • Knowing that you "tried ... without luck" is not nearly as helpful to potential answerers as an error message or symptom description. – PolyGeo Sep 28 '15 at 6:09
  • there isn't any error message given from pyqgis. How can I grab the error message? – Eric T Sep 28 '15 at 6:18
  • I'm not one of the potential answerers of this question. I recommend that you edit your question to let them know that because they may not read the comments. I also recommend these suggestions on how to frame your questions to attract potential answerers: meta.gis.stackexchange.com/a/3353/115 – PolyGeo Sep 28 '15 at 6:30
  • Thanks just tried with pdb debugging message not sure if that is the correct tool. Updated on the post and yet no clear hints given still.. – Eric T Sep 28 '15 at 8:13
  • Which is your operative system? It is an external application of QGIS or are you working at the Python Console? Where are the features of the layer? – xunilk Sep 28 '15 at 16:12

Your Answer

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

Browse other questions tagged or ask your own question.