I need to generate maps (saved in PNG, JPG or PDF) from raster files (for each file - one map) which meets following criteria; * each map has exactly the same color pallette, so that it will be easy to compare them, * I wish to add any label (for example source file name) at the top of each map, * (optionally) I wish to include legend (color pallette) * (I will be in heaven) if there will be coordinates in map included.
It is quite easy to save GeoTIFF as PNG (gdal_translate -of PNG), but I can't find a solution to set mentioned options. Is it possible to generate such maps from Python/GDAL? How? Should I call from Python QGIS map composer or there's another solution?
Edit
I started to write my Python script to resolve my issue. I assume that:
- I have opened a Composer View where I have all elements already added
- For each raster file, I need to:
- remove old raster file
- add new raster file
- (if needed) refresh map in composer view. Legend and all other elements should probably leave the same
- export as PDF/PNG/any other graphics format.
from PyQt4.QtGui import QImage, QPainter from PyQt4.Qt import QSize from PyQt4.QtCore import QRectF
def StringToRaster(raster):
# This function loads raster from provided path http://gis.stackexchange.com/questions/144058/how-to-load-a-raster-layer-using-pyqgis
if isinstance(raster,basestring):
fileInfo = QFileInfo(raster)
baseName = fileInfo.baseName()
path = fileInfo.filePath()
if (baseName and path):
raster = QgsRasterLayer(path, baseName)
if not raster.isValid():
print "Layer failed to load!"
return
else:
print "Unable to read basename and file path - Your string is probably invalid"
return
return raster
def GenerateRasterMaps(rasterList,rasterStyle):
# I wish to use exactly the same composer view - only raster file should be changed
myComposer=iface.activeComposers()[0]
for rasterFile in rasterList:
if (newRaster):
QgsMapLayerRegistry.instance().removeMapLayer(newRaster.id())
newRaster=StringToRaster(rasterFile)
newRaster.loadNamedStyle(rasterStyle)
QgsMapLayerRegistry.instance().addMapLayer(newRaster)
# Now I should only refresh current composer view and export map as a PDF. Unfortunately, after few hours on reading (and testing): http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/composer.html and QGIS API, I still have no effect. Why it is so hard to export current Composer View to PDF/PNG. Why it is not simply one command?)