1

I am beginner in python, After doing labeling to one layer i could see it both in interface and output map but when i do it for the other 2 layers it is not exported to map but can be seen in interface.

How can I modify the code?

I did this coding with pyQGIS cookbook.

from qgis.core import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import os
import glob
from datetime import datetime
from qgis.gui import *
import subprocess
shp_file = "D:\Shp\karad_contours.shp"
study_area = "D:\Shp\karad_study_area.shp"
benchmarks = "D:\Shp\karad_benchmarks.shp"
tiff_file = "D:\Autocad-Karad\karad_transparent_mosaic_group1.tif"
qgisApp = QgsApplication([], True)
qgisApp.setPrefixPath('C:\OSGeo4W64\apps\qgis', True)
qgisApp.initQgis()
tiff = QgsRasterLayer(tiff_file)    
contours = QgsVectorLayer(shp_file, 'Karad contours', 'ogr')
studyarea = QgsVectorLayer(study_area, 'Karad study area', 'ogr')
bench_marks = QgsVectorLayer(benchmarks, 'benchmarks', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(tiff)
QgsMapLayerRegistry.instance().addMapLayer(contours)
layer1 = iface.activeLayer()
layer1.setCustomProperty("labeling", "pal")
layer1.setCustomProperty("labeling/enabled", "true")
layer1.setCustomProperty("labeling/fontFamily", "Arial")
layer1.setCustomProperty("labeling/fontSize", "10") 
layer1.setCustomProperty("labeling/fieldName", "elevation")
layer1.setCustomProperty("labeling/placement", "2") 
QgsMapLayerRegistry.instance().addMapLayer(studyarea)
layer2 = iface.activeLayer()
myRenderer  = layer2.rendererV2()
layer2.setCustomProperty("labeling/isExpression", True)
mySymbol1 = QgsFillSymbolV2.createSimple({'color':'0,0,0,0','color_border':'#000000','width_border':'1'})
myRenderer.setSymbol(mySymbol1)
QgsMapLayerRegistry.instance().addMapLayer(bench_marks)
layer3 = iface.activeLayer()
layer3.setCustomProperty("labeling", "pal")
layer3.setCustomProperty("labeling/enabled", "true")
layer3.setCustomProperty("labeling/fontFamily", "Arial")
layer3.setCustomProperty("labeling/fontSize", "10") 
layer3.setCustomProperty("labeling/fieldName", "name")
layer3.setCustomProperty("labeling/placement", "2") 
iface.mapCanvas().refresh()     
canvas = QgsMapCanvas()
layers = [QgsMapCanvasLayer(contours), QgsMapCanvasLayer(tiff)]
canvas.setLayerSet(layers)
canvas.zoomToFullExtent()
e = canvas.extent()
e = contours.extent()
e = tiff.extent()
mapSettings = canvas.mapSettings()
c = QgsComposition(mapSettings)
c.setPlotStyle(QgsComposition.Print)
x, y = 10, 10
w, h = c.paperWidth() - 20, c.paperHeight() - 20
mapSettings.setOutputSize(QSize(w, h))
composerMap = QgsComposerMap(c, x, y, w, h)
composerMap.moveContent(0, 0)
composerMap.setPreviewMode(1)
composerMap.grid().setStyle(3)
composerMap.grid().setFrameStyle(3)
composerMap.grid().setIntervalX(100)
composerMap.grid().setIntervalY(100)
composerMap.setShowGridAnnotation(True)
composerMap.grid().setAnnotationPrecision(2)
composerMap.grid().setAnnotationDirection(1, 0)
composerMap.grid().setAnnotationDirection(1, 1)
composerMap.grid().setEnabled(True)
composerMap.setFrameEnabled(True)
c.addItem(composerMap)
composerLabel = QgsComposerLabel(c)
composerLabel.setText("Benchmark Reference Image")
composerLabel.setItemPosition(180, 10)
composerLabel.setFont(QFont("Times New Roman",24, QFont.Bold))
composerLabel.adjustSizeToText()
c.addItem(composerLabel)
legend = QgsComposerLegend(c)
legend.setItemPosition(10.5, 160)
legend.modelV2().index(0, 0).data()
legend.setAutoUpdateModel(False)
c.addItem(legend)
scaleBar = QgsComposerScaleBar(c)
scaleBar.setStyle('Line Ticks Up')  # optionally modify the style
scaleBar.setComposerMap(composerMap)
scaleBar.applyDefaultSize()
scaleBar.setItemPosition(221, 187)
scaleBar.setNumSegmentsLeft(0)
scaleBar.setNumSegments(2)
c.addItem(scaleBar)
northarrowIcon = QgsComposerPicture(c)
northarrowIcon.setPos(QPointF(20,20))
northarrowIcon.setSceneRect(QRectF(255, 20, 20, 20))
northarrowIcon.setPictureFile("C:\Users\Administrator\Desktop\north-arrow-orienteering-md.svg")
c.addItem(northarrowIcon)

# Save the map as png image
dpi = c.printResolution()
dpmm = dpi / 25.4
width = int(dpmm * c.paperWidth())
height = int(dpmm * c.paperHeight())
# create output image and initialize it
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
color = QColor(255, 255, 255)
image.fill(color.rgb())
# render the composition
imagePainter = QPainter(image)
sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())
targetArea = QRectF(0, 0, width, height)
c.render(imagePainter, targetArea, sourceArea)
imagePainter.end()
out_img = "D:\TCE\Output\out.png"
image.save(out_img, "png")
print out_img

# Save the map as pdf image
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("D:\TCE\Output\out.pdf")
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QPrinter.Color)
printer.setResolution(c.printResolution())
pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
c.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()
print "pdf output"
1

In the middle of your code, you have

layers = [QgsMapCanvasLayer(contours), QgsMapCanvasLayer(tiff)]
canvas.setLayerSet(layers)

You would need to expand this list with the other layers you want in the canvas.

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.