AnsweredAssumed Answered

QGIS Python

Question asked by pmcburney on Aug 22, 2016
Latest reply on Aug 22, 2016 by Dan_Patterson

This question pertains to QGIS. I'm trying to automate a process where I'm joining attribute data with a shapefile. I'm able to create the desired map I'm looking for but I am having issues exporting it from the QGIS interface via PDF. When I run the code it only produces my legend in the PDF format and not my map. Please refer to the code below. I appreciate any help as I am new to coding and GIS.

 

 

from qgis.core import *
from PyQt4 import QtGui
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
from qgis.core import *
from PyQt4.QtXml import QDomDocument

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

import pyodbc
import os
import sys
import socket
import csv
import processing

 

 

county_uri = 'O:/Broker Support/Cat Modeling/QGIS/Shapefiles/US Counties/UScounties.shp'
shp = QgsVectorLayer(county_uri, 'UScounties', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(shp)

csv_uri = "file:///O:/Broker Support/Cat Modeling/ArcGIS/File.csv?delimeter=,"
csv = QgsVectorLayer(csv_uri, "File", "delimitedtext")
QgsMapLayerRegistry.instance().addMapLayer(csv)

shpField='state_coun'
csvField='state_coun'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = csv.id()
joinObject.joinFieldName = csvField
joinObject.targetFieldName = shpField
joinObject.memoryCache = True
shp.addJoin(joinObject)
True

colorRamp = QgsVectorGradientColorRampV2.create({'color1':'188,255,255,255', 'color2':'0,0,128,255','stops':'0.25;176,196,222,255:0.50;70,130,180,255:0.75;65,105,225,255'})

 

renderer = QgsGraduatedSymbolRendererV2.createRenderer(shp, 'File', 6, QgsGraduatedSymbolRendererV2.Jenks, QgsSymbolV2.defaultSymbol( shp.geometryType() ), colorRamp )
shp.setRendererV2( renderer )
QgsMapLayerRegistry.instance().addMapLayer(shp)

qgis.utils.iface.activeLayer()

qgis.utils.iface.actionZoomToLayer().trigger()

def savePDF():
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x ,y, w, h)
rect = iface.mapCanvas().currentLayer().extent()
composerMap.setNewExtent(rect)
c.addItem(composerMap)


legend = QgsComposerLegend(c)
legend.model().setLayerSet(mapRenderer.layerSet())
c.addItem(legend)

 

outpath = "\\\\ba-fs-nc\\data\\Broker Support\\Cat Modeling\\QGIS\File.pdf"
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(outpath)
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()


QTimer.singleShot(3000, savePDF)

Outcomes