I'm trying to develop a QGIS plugin to print maps with a template.The main job of the plugin is:
Select the sheet from a combo box
Zoom to that sheet in the grid
Load the content of the canvas in the .qpt template
Save to PDF
Everything works as expected when I print the map in the current CRS (4326)But when I set the project to UTM (22032), the script fails, launching an Unknow Exception when Saving as Pdf.
I've tried reprojecting on the fly, and then printing, but it fails too.The project layers are in a postGis database, and are in EPSG:4326.
Do you think could this be the problem?
I attatch a basic copy of the script I'm using to print, that is working only when the project is EPSg:4326
def pdfSave2(self): template_path = 'route to my file/template.qpt' template_file = file(template_path) template_content = template_file.read() template_file.close() #createdocument document = QDomDocument() document.setContent(template_content) #create mapsettings object ms = self.canvas.mapSettings() # Enable on the fly CRS transformations ms.setCrsTransformEnabled(True) #composition composition = QgsComposition(ms) #set crs #myCRS = core.QgsCoordinateReferenceSystem(22032, core.QgsCoordinateReferenceSystem.EpsgCrsId) #ms.setDestinationCrs(myCRS) composition.loadFromTemplate(document, {}, False, True) #get maps map_item = composition.getComposerMapById(0) map_esquema = composition.getComposerMapById(4) map_hojas = composition.getComposerMapById(5) map_prueba = composition.getComposerMapById(10) map_item.setKeepLayerSet(False) #add canvas content map_item.setMapCanvas(self.canvas) map_esquema.setMapCanvas(self.canvas) map_hojas.setMapCanvas(self.canvas) map_prueba.setMapCanvas(self.canvas) #get selected feature from 250k grid arrayCapas = [] layers = QgsMapLayerRegistry.instance().mapLayers() for name, layer in layers.iteritems(): if '250k' in unicode(layer.name()): arrayCapas.append(layer) #get selected layer capaSelec = arrayCapas[0] #get bbox featureSelecBBox=capaSelec.boundingBoxOfSelected() #zoom canvas self.canvas.setExtent(featureSelecBBox) self.canvas.refresh() #zoom map map_item.zoomToExtent(featureSelecBBox) map_esquema.zoomToExtent(featureSelecBBox) map_hojas.zoomToExtent(featureSelecBBox) map_prueba.zoomToExtent(featureSelecBBox) map_item.setNewScale(250000, True) map_esquema.setNewScale(25000000, True) map_hojas.setNewScale(5000000, True) map_prueba.setNewScale(5000000, True) composition.refreshItems() if(composition.exportAsPDF('D:/impresion'+self.cbHojas.currentText()+'.pdf')): print self.cbHojas.currentText() else: print "algo ha fallado" print "todo ok"
أكثر...
Select the sheet from a combo box
Zoom to that sheet in the grid
Load the content of the canvas in the .qpt template
Save to PDF
Everything works as expected when I print the map in the current CRS (4326)But when I set the project to UTM (22032), the script fails, launching an Unknow Exception when Saving as Pdf.
I've tried reprojecting on the fly, and then printing, but it fails too.The project layers are in a postGis database, and are in EPSG:4326.
Do you think could this be the problem?
I attatch a basic copy of the script I'm using to print, that is working only when the project is EPSg:4326
def pdfSave2(self): template_path = 'route to my file/template.qpt' template_file = file(template_path) template_content = template_file.read() template_file.close() #createdocument document = QDomDocument() document.setContent(template_content) #create mapsettings object ms = self.canvas.mapSettings() # Enable on the fly CRS transformations ms.setCrsTransformEnabled(True) #composition composition = QgsComposition(ms) #set crs #myCRS = core.QgsCoordinateReferenceSystem(22032, core.QgsCoordinateReferenceSystem.EpsgCrsId) #ms.setDestinationCrs(myCRS) composition.loadFromTemplate(document, {}, False, True) #get maps map_item = composition.getComposerMapById(0) map_esquema = composition.getComposerMapById(4) map_hojas = composition.getComposerMapById(5) map_prueba = composition.getComposerMapById(10) map_item.setKeepLayerSet(False) #add canvas content map_item.setMapCanvas(self.canvas) map_esquema.setMapCanvas(self.canvas) map_hojas.setMapCanvas(self.canvas) map_prueba.setMapCanvas(self.canvas) #get selected feature from 250k grid arrayCapas = [] layers = QgsMapLayerRegistry.instance().mapLayers() for name, layer in layers.iteritems(): if '250k' in unicode(layer.name()): arrayCapas.append(layer) #get selected layer capaSelec = arrayCapas[0] #get bbox featureSelecBBox=capaSelec.boundingBoxOfSelected() #zoom canvas self.canvas.setExtent(featureSelecBBox) self.canvas.refresh() #zoom map map_item.zoomToExtent(featureSelecBBox) map_esquema.zoomToExtent(featureSelecBBox) map_hojas.zoomToExtent(featureSelecBBox) map_prueba.zoomToExtent(featureSelecBBox) map_item.setNewScale(250000, True) map_esquema.setNewScale(25000000, True) map_hojas.setNewScale(5000000, True) map_prueba.setNewScale(5000000, True) composition.refreshItems() if(composition.exportAsPDF('D:/impresion'+self.cbHojas.currentText()+'.pdf')): print self.cbHojas.currentText() else: print "algo ha fallado" print "todo ok"
أكثر...