Пример #1
0
    def buildComposerLabel(self, key, item, page):
        '''
        Add a label to the print layout for an item and page
        '''
        cl = QgsLayoutItemLabel(self.currentComposition)

        # 1st page is a map for parcelle
        dpage = page -1
        if self.etype == 'parcelle' and self.iface:
            dpage = page

        cl.attemptMove(
            QgsLayoutPoint(
                item['position'][0],
                item['position'][1]+ (dpage) * (self.pageHeight + 10),
                QgsUnitTypes.LayoutMillimeters
            )
        )
        cl.setFixedSize(
            QgsLayoutSize(
                item['position'][2],
                item['position'][3],
                QgsUnitTypes.LayoutMillimeters
            )
        )

        cl.setVAlign(item['align'][0])
        cl.setHAlign(item['align'][1])
        content = self.getContentForGivenItem(
            key,
            item,
            page
        )

        cl.setMargin(0)
        cl.setMode(1)
        cl.setText(content)
        cl.setFrameEnabled(False)
        if 'bgcolor' in item:
            cl.setBackgroundColor(item['bgcolor'])
        if 'htmlState' in item:
            cl.setMode(item['htmlState'])
        if 'font' in item:
            cl.setFont(item['font'])

        self.currentComposition.addLayoutItem(cl)
Пример #2
0
    def buildComposerLabel(self, key, item, page):
        '''
        Add a label to the print layout for an item and page
        '''
        cl = QgsLayoutItemLabel(self.currentComposition)

        # 1st page is a map for parcelle
        dpage = page -1
        if self.etype == 'parcelle' and self.print_parcelle_page:
            dpage = page

        cl.attemptMove(
            QgsLayoutPoint(
                item['position'][0],
                item['position'][1]+ (dpage) * (self.pageHeight + 10),
                QgsUnitTypes.LayoutMillimeters
            )
        )
        cl.setFixedSize(
            QgsLayoutSize(
                item['position'][2],
                item['position'][3],
                QgsUnitTypes.LayoutMillimeters
            )
        )

        cl.setVAlign(item['align'][0])
        cl.setHAlign(item['align'][1])
        content = self.getContentForGivenItem(
            key,
            item,
            page
        )

        cl.setMargin(0)
        cl.setMode(1)
        cl.setText(content)
        cl.setFrameEnabled(False)
        if 'bgcolor' in item:
            cl.setBackgroundColor(item['bgcolor'])
        if 'htmlState' in item:
            cl.setMode(item['htmlState'])
        if 'font' in item:
            cl.setFont(item['font'])

        self.currentComposition.addLayoutItem(cl)
Пример #3
0
    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()