2

I am trying to render a map using the QGIS editor (eventually will generate hundreds for animation, but just trying to get one working for now). However, the rendered map is not projected (I want it to be: North_America_Albers_Equal_Area_Conic).

enter image description here

The map I want is in the QGIS map canvas following the completion of the code (figure directly below). I saved it using the "save as image" in the drop down menu.

enter image description here

and here is my code...

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

#Load project
project = QgsProject.instance()
project.read(QFileInfo('something.qgs'))

#input Layer
lyrPts =  QgsVectorLayer("this.shp", "Qflu", "ogr")

#display layer
reg = QgsMapLayerRegistry.instance()
reg.addMapLayers([lyrPts])

#Filter
lyrPts.setSubsetString('DateIndx = 500')
iface.mapCanvas().refresh()

# create image
img = QImage(QSize(8000, 6000), 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
layers = QgsMapLayerRegistry.instance().mapLayers()
lst = layers.keys()
render.setLayerSet(lst)

# Set destination CRS to match the CRS of the first layer
render.setDestinationCrs(layers.values()[0].crs())

# Enable OTF reprojection
render.setProjectionsEnabled(True)

# set extent
rect = QgsRectangle(xmin=-124, ymin=25, xmax=-69, ymax=52)
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("somewhere.png","png")
  • What's wrong with the map in the second screenshot? That's reprojected and all layers seem visible ... – underdark Aug 24 '17 at 18:51
  • Yes, that is what I want. I just exported that manually from the Qgis interface. I want to be able to do it in python because I have hundreds of figures to make. – jtam Aug 24 '17 at 18:52
  • Is the project CRS of something.qgs set to North_America_Albers_Equal_Area_Conic? If yes, why is the code changing the CRS? If no, why not? – underdark Aug 24 '17 at 19:08
  • Yes. The second image above is created by code in the interface, but the image rendered and saved by code is the first image. Not sure why code is changing CRS? Maybe I need to pass the project settings to renderer somehow? – jtam Aug 24 '17 at 19:11
  • Remove render.setDestinationCrs(layers.values()[0].crs()) – underdark Aug 24 '17 at 19:16
1

I figured out my first problem with the projection. I added these lines of code to set the renderer CRS and enable OTF reprojection:

#Set  CRS
crs = QgsCoordinateReferenceSystem(102008,QgsCoordinateReferenceSystem.EpsgCrsId)
render.setDestinationCrs(crs)

#enable OTF reprojection
render.setProjectionsEnabled(True)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.