1

I'm new to pyqgis and trying to bring up from the previous post with a little progress from the last (implemented debugging tool pdb) but now trying to export into a .tif file so that I can check on the vertices from the csv geometry. Unfortunately I get stuck with an blank render .tif image. The render.tif filesize is showing 1.9mb, when I download and view locally, the image only has the background with white colour and no vertices.

At the same time, I checked my csv file and also looping through the features of the layer after export the csv files. They are all good...

How can I have all the features from the vector layer to be shown on the .tif image file? Anyone please help? :/

PS: I'm on ubuntu server and using PyQGIS.

Tried with render.fullExtent() and render.extent(), both return blank image.tif

Inserted sample of elevp.csv

My code:-

#!/usr/bin/python
import sys
import qgis
import PyQt4

from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *
import pdb 


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


uri = "/home/ubuntu/pyqgis/elevp.csv?crs=%s&delimiter=%s&xField=%s&yField=%s&elevField=%s" % ("EPSG:4326",";","x","y","elev")
vlayer = QgsVectorLayer(uri, "test", "delimitedtext")

QgsMapLayerRegistry.instance().addMapLayer(vlayer)

# 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.id()]
print(lst)
render.setLayerSet(lst)

# set extent
rect = QgsRectangle(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("/home/ubuntu/pyqgis/render.tif","tif")

Sample elevp.csv:-

x;y;elev
120;960;13
360;40;52
640;840;3
  • 1
    Can you please elaborate what exactly you expect the outcome to be. An image with three coloured dots? Or a georeferenced raster file with information. Additionally, the coordinates in the elevp.csv are not in EPSG:4326 as your import statement says. Maybe this question will help you to determine your desired outcome and give you a hint in which direction your script should go. – bennos Oct 1 '15 at 10:10

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.