def createScaleBar(self): try: scalebar = QgsLayoutItemScaleBar(self.layout) scalebar.setStyle('Line Ticks Up') scalebar.setUnits(QgsUnitTypes.DistanceKilometers) scalebar.setNumberOfSegments(6) scalebar.setNumberOfSegmentsLeft(0) scalebar.setUnitsPerSegment(2) scalebar.setLinkedMap(self.map) scalebar.setUnitLabel('km') scalebar.setFont(QFont('Arial', 14)) scalebar.update() self.layout.addLayoutItem(scalebar) scalebar.attemptMove(QgsLayoutPoint(180, 190, QgsUnitTypes.LayoutMillimeters)) except: pass
def print_map(self, tav_num): self.tav_num = tav_num p = QgsProject.instance() crs = QgsCoordinateReferenceSystem() crs.createFromSrid(self.SRS) p.setCrs(crs) l = QgsLayout(p) l.initializeDefaults() page = l.pageCollection().page(0) # map - this item tells the libraries where to put the map itself. Here we create a map and stretch it over the whole paper size: x, y = 0, 0 # angolo 0, o in alto a sx if (0 <= self.width <= 297) and (0 <= self.height <= 210): width, height = 297, 210 # Formato A4 Landscape elif (0 <= self.height <= 297) and (0 <= self.width <= 210): width, height = 210, 297 # Formato A4 elif (0 <= self.width <= 420) and (0 <= self.height <= 297): width, height = 297, 420 # Formato A3 Landscape elif (0 <= self.height <= 420) and (0 <= self.width <= 297): width, height = 240, 297 # Formato A4 elif (0 <= self.width <= 1189) and (0 <= self.height <= 841): width, height = 1189, 841 # Formato A0 Landscape elif (0 <= self.height <= 1189) and (0 <= self.width <= 841): width, height = 841, 1189 # Formato A0 else: width, height = self.width * 1.2, self.height * 1.2 # self.width*10, self.height*10 da un valore alla larghezza e altezza del foglio aumentato di 5 per dare un margine size = QgsLayoutSize(width, height) page.setPageSize(size) map = QgsLayoutItemMap(l) rect = self.getMapExtentFromMapCanvas(page.pageSize().width(), page.pageSize().height(), 20.0) map.attemptSetSceneRect( QRectF(0, 0, page.pageSize().width(), page.pageSize().height())) map.setExtent(rect) map.setLayers([self.layerUS]) l.setReferenceMap(map) l.addLayoutItem(map) intestazioneLabel = QgsLayoutItemLabel(l) txt = "Tavola %s - US:%d " % (self.tav_num + 1, self.us) intestazioneLabel.setText(txt) intestazioneLabel.adjustSizeToText() intestazioneLabel.attemptMove(QgsLayoutPoint(1, 0), page=0) intestazioneLabel.setFrameEnabled(False) l.addLayoutItem(intestazioneLabel) scaleLabel = QgsLayoutItemLabel(l) txt = "Scala: " scaleLabel.setText(txt) scaleLabel.adjustSizeToText() scaleLabel.attemptMove(QgsLayoutPoint(1, 5), page=0) scaleLabel.setFrameEnabled(False) l.addLayoutItem(scaleLabel) # aggiunge la scale bar scaleBarItem = QgsLayoutItemScaleBar(l) scaleBarItem.setStyle('Numeric') # optionally modify the style scaleBarItem.setLinkedMap(map) scaleBarItem.applyDefaultSize() scaleBarItem.attemptMove(QgsLayoutPoint(10, 5), page=0) scaleBarItem.setFrameEnabled(False) l.addLayoutItem(scaleBarItem) le = QgsLayoutExporter(l) settings = QgsLayoutExporter.ImageExportSettings() settings.dpi = 100 MAPS_path = '{}{}{}'.format(self.HOME, os.sep, "pyarchinit_MAPS_folder") tav_name = "Tavola_{}_us_{}.png".format(self.tav_num + 1, self.us) filename_png = '{}{}{}'.format(MAPS_path, os.sep, tav_name) le.exportToImage(filename_png, settings) self.remove_layer()