Ejemplo n.º 1
0
    def testExportToSvgTextRenderFormat(self):
        l = QgsLayout(QgsProject.instance())
        l.initializeDefaults()

        # add a map and scalebar
        mapitem = QgsLayoutItemMap(l)
        mapitem.attemptSetSceneRect(QRectF(110, 120, 200, 250))
        mapitem.zoomToExtent(QgsRectangle(1, 1, 10, 10))
        mapitem.setScale(
            666
        )  # unlikely to appear in the SVG by accident... unless... oh no! RUN!
        l.addItem(mapitem)

        item1 = QgsLayoutItemScaleBar(l)
        item1.attemptSetSceneRect(QRectF(10, 20, 100, 150))
        item1.setLinkedMap(mapitem)
        item1.setStyle('Numeric')
        l.addItem(item1)

        exporter = QgsLayoutExporter(l)
        # setup settings
        settings = QgsLayoutExporter.SvgExportSettings()
        settings.dpi = 80
        settings.forceVectorOutput = False
        settings.exportMetadata = True
        settings.textRenderFormat = QgsRenderContext.TextFormatAlwaysText

        svg_file_path = os.path.join(self.basetestpath,
                                     'test_exporttosvgtextformattext.svg')
        self.assertEqual(exporter.exportToSvg(svg_file_path, settings),
                         QgsLayoutExporter.Success)
        self.assertTrue(os.path.exists(svg_file_path))

        # expect svg to contain a text object with the scale
        with open(svg_file_path, 'r') as f:
            lines = ''.join(f.readlines())
        self.assertIn('<text', lines)
        self.assertIn('>1:666<', lines)

        # force use of outlines
        os.unlink(svg_file_path)
        settings.textRenderFormat = QgsRenderContext.TextFormatAlwaysOutlines
        self.assertEqual(exporter.exportToSvg(svg_file_path, settings),
                         QgsLayoutExporter.Success)
        self.assertTrue(os.path.exists(svg_file_path))

        # expect svg NOT to contain a text object with the scale
        with open(svg_file_path, 'r') as f:
            lines = ''.join(f.readlines())
        self.assertNotIn('<text', lines)
        self.assertNotIn('>1:666<', lines)
Ejemplo n.º 2
0
    def testExportToSvgTextRenderFormat(self):
        l = QgsLayout(QgsProject.instance())
        l.initializeDefaults()

        # add a map and scalebar
        mapitem = QgsLayoutItemMap(l)
        mapitem.attemptSetSceneRect(QRectF(110, 120, 200, 250))
        mapitem.zoomToExtent(QgsRectangle(1, 1, 10, 10))
        mapitem.setScale(666)  # unlikely to appear in the SVG by accident... unless... oh no! RUN!
        l.addItem(mapitem)

        item1 = QgsLayoutItemScaleBar(l)
        item1.attemptSetSceneRect(QRectF(10, 20, 100, 150))
        item1.setLinkedMap(mapitem)
        item1.setStyle('Numeric')
        l.addItem(item1)

        exporter = QgsLayoutExporter(l)
        # setup settings
        settings = QgsLayoutExporter.SvgExportSettings()
        settings.dpi = 80
        settings.forceVectorOutput = False
        settings.exportMetadata = True
        settings.textRenderFormat = QgsRenderContext.TextFormatAlwaysText

        svg_file_path = os.path.join(self.basetestpath, 'test_exporttosvgtextformattext.svg')
        self.assertEqual(exporter.exportToSvg(svg_file_path, settings), QgsLayoutExporter.Success)
        self.assertTrue(os.path.exists(svg_file_path))

        # expect svg to contain a text object with the scale
        with open(svg_file_path, 'r') as f:
            lines = ''.join(f.readlines())
        self.assertIn('<text', lines)
        self.assertIn('>1:666<', lines)

        # force use of outlines
        os.unlink(svg_file_path)
        settings.textRenderFormat = QgsRenderContext.TextFormatAlwaysOutlines
        self.assertEqual(exporter.exportToSvg(svg_file_path, settings), QgsLayoutExporter.Success)
        self.assertTrue(os.path.exists(svg_file_path))

        # expect svg NOT to contain a text object with the scale
        with open(svg_file_path, 'r') as f:
            lines = ''.join(f.readlines())
        self.assertNotIn('<text', lines)
        self.assertNotIn('>1:666<', lines)
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
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()