Skip to content

Commit

Permalink
implement QgsPaintEngineHack.fixEngineFlags http://qgis.org/api/2.0/q…
Browse files Browse the repository at this point in the history
…gspaintenginehack_8cpp_source.html to get correct styles with PDF export.
  • Loading branch information
wilhelmberg committed Mar 25, 2014
1 parent bef911e commit d8d0823
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VoGISRaumplanungPlot/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[general]
name=VoGIS Raumplanung
description=Create Plots from VoGIS Data.
version=0.1.2
version=0.1.3
qgisMinimumVersion=2.0
qgisMaximumVersion=2.99
author=BergWerk GIS
Expand Down
2 changes: 1 addition & 1 deletion VoGISRaumplanungPlot/resources_rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Resource object code
#
# Created: Di. Mär 11 11:12:25 2014
# Created: Di. Mär 25 11:50:27 2014
# by: The Resource Compiler for PyQt (Qt v4.8.4)
#
# WARNING! All changes made in this file will be lost!
Expand Down
2 changes: 1 addition & 1 deletion VoGISRaumplanungPlot/ui_vogisraumplanungplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'ui_vogisraumplanungplot.ui'
#
# Created: Tue Mar 11 11:12:25 2014
# Created: Tue Mar 25 11:50:27 2014
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost!
Expand Down
2 changes: 1 addition & 1 deletion VoGISRaumplanungPlot/ui_vogisraumplanungplotsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'ui_vogisraumplanungplotsettings.ui'
#
# Created: Tue Mar 11 11:12:25 2014
# Created: Tue Mar 25 11:50:27 2014
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost!
Expand Down
13 changes: 13 additions & 0 deletions VoGISRaumplanungPlot/vrpcomposer/vrpprintcomposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from qgis.core import QgsFeatureRequest
from qgis.core import QgsMapLayerRegistry
from qgis.core import QgsMessageLog
from qgis.core import QgsPaintEngineHack
from qgis.core import QgsRasterLayer
from qgis.core import QgsVectorLayer
from qgis.gui import QgsMessageBar
Expand Down Expand Up @@ -65,6 +66,7 @@ def __init__(
self.ortho_lyr = None
self.themen = themen
self.composition = None
self.composermap = None
self.comp_textinfo = None
self.template_qpt = templateqpt
self.pdf_map = pdfmap
Expand Down Expand Up @@ -138,7 +140,13 @@ def export_all_features(self):
return u'Kein Kartenfenster im Layout vorhanden!'
compmap = compmaps[0]
else:
if len(composition.composerMapItems()) < 1:
return u'Kein Kartenfenster im Layout vorhanden!'
compmap = composition.composerMapItems()[0]

self.composermap = compmap
#self.composermap.setPreviewMode(QgsComposerMap.Render)
#self.composermap.setPreviewMode(QgsComposerMap.Rectangle)
#taken from QgsComposerMap::setNewAtlasFeatureExtent (not yet available in QGIS 2.0)
#http://www.qgis.org/api/qgscomposermap_8cpp_source.html#l00610
old_ratio = compmap.rect().width() / compmap.rect().height()
Expand Down Expand Up @@ -183,6 +191,7 @@ def export_all_features(self):
pdf_painter = QPainter(printer)
paper_rect_pixel = printer.pageRect(QPrinter.DevicePixel)
paper_rect_mm = printer.pageRect(QPrinter.Millimeter)
QgsPaintEngineHack.fixEngineFlags(printer.paintEngine())
#DKM only
if len(self.themen) < 1:
composition.render(pdf_painter, paper_rect_pixel, paper_rect_mm)
Expand Down Expand Up @@ -415,6 +424,10 @@ def __update_composer_items(self, oberthema, subthema=None, labels=None, gnrflae
txt = txt.replace('[TODAY]', strftime("%d.%m.%Y"))
txt = txt.replace('[DATE]', self.settings.dkm_stand())
lbl[0].setText(txt)
#self.composermap.updateItem()
#self.composermap.updateCachedImage()
self.composermap.mapRenderer().updateFullExtent ()


def __get_items(self, typ, composition=None):
if composition is None:
Expand Down
67 changes: 67 additions & 0 deletions dev-scripts/QgsCompositionTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from PyQt4.QtCore import QFile
from PyQt4.QtXml import QDomDocument
from PyQt4.QtCore import QIODevice
from PyQt4.QtCore import QSizeF
from PyQt4.QtGui import QPrinter
from PyQt4.QtGui import QPainter
import os

pdf_out = '/home/bergw/_TEMP/out.pdf'
if os.path.isfile(pdf_out):
os.remove(pdf_out)
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
#print dir(c)
#c.setAtlasMode(QgsComposition.AtlasMode.ExportAtlas)

xml_file = QFile('/home/bergw/VoGIS-Raumplanung-Daten/A4_hoch_template.qpt')
if xml_file.open(QIODevice.ReadOnly) is False:
throwError

doc = QDomDocument('mydoc')
doc.setContent(xml_file)
if c.loadFromTemplate(doc) is False:
throwError

cov_lyr = iface.activeLayer()
print 'cov_lyr', cov_lyr.name()
ac = c.atlasComposition()
ac.setEnabled(True)
ac.setSingleFile(True)
ac.setCoverageLayer(cov_lyr)
#ac.setFeatureFilter('length("GNR") > 7 AND left("GNR",3)=\'120\'')
ac.setFeatureFilter('"GNR" = \'905\'')
ac.setHideCoverage(False)

print 'beginRender, feats:', ac.numFeatures()
ac.beginRender()
for i in range(0, ac.numFeatures()):
print 'prepareForFeature', i
ac.prepareForFeature(i)
img = c.printPageAsRaster(0)
tmp_file = '/home/bergw/_TEMP/' + str(i) + '.jpg'
if os.path.isfile(tmp_file):
os.remove(tmp_file)
img.save(tmp_file)
ac.endRender()
print 'render finished'


#printer = QPrinter()
#printer.setOutputFormat(QPrinter.PdfFormat)
#printer.setOutputFileName(pdf_out)
#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)
#TODO: checken ob File vorhanden?
#LOESCHEN!!!
#c.render(pdfPainter, paperRectPixel, paperRectMM)
#pdfPainter.end()

print 'FERTIG'
63 changes: 63 additions & 0 deletions dev-scripts/QgsCompositionTest2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from PyQt4.QtCore import QFile
from PyQt4.QtXml import QDomDocument
from PyQt4.QtCore import QIODevice
from PyQt4.QtCore import QSizeF
from PyQt4.QtGui import QPrinter
from PyQt4.QtGui import QPainter
import os
import subprocess

pdf_out = '/home/bergw/_TEMP/out.pdf'
if os.path.isfile(pdf_out):
os.remove(pdf_out)
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)

xml_file = QFile('/home/bergw/VoGIS-Raumplanung-Daten/A4_hoch_template.qpt')
if xml_file.open(QIODevice.ReadOnly) is False:
throwError

doc = QDomDocument('mydoc')
doc.setContent(xml_file)
if c.loadFromTemplate(doc) is False:
throwError

if QGis.QGIS_VERSION_INT > 20200:
#compmaps = self.__get_items(QgsComposerMap)
#compmap = compmaps[0]
pass
else:
compmap = c.composerMapItems()[0]

c.setPlotStyle(QgsComposition.Print)
compmap.setPreviewMode(QgsComposerMap.Render)
#compmap.updateItem()
#emit compmap.itemChanged();
#compmap.extentChanged();
#compmap.toggleAtlasPreview()
#compmap.setNewScale(compmap.scale()+1)
#c.setPrintResolution(150)
#print c.printResolution()
c.setPrintAsRaster(False)

printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(pdf_out)
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)

QgsPaintEngineHack.fixEngineFlags(printer.paintEngine())

#c.renderPage(pdfPainter, 0)
c.doPrint(printer,pdfPainter)

pdfPainter.end()
subprocess.call(('xdg-open', pdf_out))

print 'FERTIG'
67 changes: 67 additions & 0 deletions dev-scripts/QgsCompositionTest3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from PyQt4.QtCore import QFile
from PyQt4.QtXml import QDomDocument
from PyQt4.QtCore import QIODevice
from PyQt4.QtCore import QSizeF
from PyQt4.QtGui import QPrinter
from PyQt4.QtGui import QPainter
import os

pdf_out = '/home/bergw/_TEMP/out.pdf'
if os.path.isfile(pdf_out):
os.remove(pdf_out)
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
#print dir(c)
#c.setAtlasMode(QgsComposition.AtlasMode.ExportAtlas)

xml_file = QFile('/home/bergw/VoGIS-Raumplanung-Daten/A4_hoch_template.qpt')
if xml_file.open(QIODevice.ReadOnly) is False:
throwError

doc = QDomDocument('mydoc')
doc.setContent(xml_file)
if c.loadFromTemplate(doc) is False:
throwError

cov_lyr = iface.activeLayer()
print 'cov_lyr', cov_lyr.name()
ac = c.atlasComposition()
ac.setEnabled(True)
ac.setSingleFile(True)
ac.setCoverageLayer(cov_lyr)
#ac.setFeatureFilter('length("GNR") > 7 AND left("GNR",3)=\'120\'')
ac.setFeatureFilter('"GNR" = \'905\'')
ac.setHideCoverage(False)

print 'beginRender, feats:', ac.numFeatures()
ac.beginRender()
for i in range(0, ac.numFeatures()):
print 'prepareForFeature', i
ac.prepareForFeature(i)
img = c.printPageAsRaster(0)
tmp_file = '/home/bergw/_TEMP/' + str(i) + '.jpg'
if os.path.isfile(tmp_file):
os.remove(tmp_file)
img.save(tmp_file)
ac.endRender()
print 'render finished'


#printer = QPrinter()
#printer.setOutputFormat(QPrinter.PdfFormat)
#printer.setOutputFileName(pdf_out)
#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)
#TODO: checken ob File vorhanden?
#LOESCHEN!!!
#c.render(pdfPainter, paperRectPixel, paperRectMM)
#pdfPainter.end()

print 'FERTIG'
11 changes: 11 additions & 0 deletions dev-scripts/QgsFeatureRequest-TEST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
lyr = iface.activeLayer()
#r = QgsFeatureRequest()
#print dir(r)
#r.setFilterExpression(u'PGEM_NAME = \'Dornbirn\'')
gst = {}
for f in lyr.getFeatures():
if f['PGEM_NAME'] == 'Dornbirn':
gnr = f['GNR']
if not gnr in gst:
gst[gnr] = f
print len(gst), 'Grundstuecke'
13 changes: 13 additions & 0 deletions dev-scripts/test-multithreading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import multiprocessing as mp
import sys, os

from multiprocessing import Process

print("I'm in main module")
# OSGeo4W does not bundle python in exec_prefix for python
path = os.path.abspath(os.path.join(sys.exec_prefix, '../../bin/pythonw.exe'))
mp.set_executable(path)
print("Setting executable path to {:s}".format(path))
sys.argv = [ None ] # '../tst.py' __file__
mgr = mp.Manager()
print("I'm past Manager()")
Binary file modified vogisraumplanungplot.zip
Binary file not shown.

0 comments on commit d8d0823

Please sign in to comment.