def testWidth(self): ms = QgsMapSettings() extent = QgsRectangle(100, 200, 100, 200) ms.setExtent(extent) ms.setOutputSize(QSize(400, 400)) context = QgsRenderContext.fromMapSettings(ms) context.setScaleFactor(96 / 25.4) # 96 DPI ms.setExtent(QgsRectangle(100, 150, 100, 150)) ms.setOutputDpi(ms.outputDpi() * 2) context2 = QgsRenderContext.fromMapSettings(ms) context2.setScaleFactor(300 / 25.4) s = QgsFillSymbol() s.deleteSymbolLayer(0) marker_line = QgsMarkerLineSymbolLayer(True) marker_line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex) marker = QgsSimpleMarkerSymbolLayer( QgsSimpleMarkerSymbolLayer.Triangle, 10) marker.setColor(QColor(255, 0, 0)) marker.setStrokeStyle(Qt.NoPen) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) marker_line.setSubSymbol(marker_symbol) self.assertEqual(marker_line.width(), 10) self.assertAlmostEqual(marker_line.width(context), 37.795275590551185, 3) self.assertAlmostEqual(marker_line.width(context2), 118.11023622047244, 3) marker_line.subSymbol().setSizeUnit(QgsUnitTypes.RenderPixels) self.assertAlmostEqual(marker_line.width(context), 10.0, 3) self.assertAlmostEqual(marker_line.width(context2), 10.0, 3)
def testWidth(self): ms = QgsMapSettings() extent = QgsRectangle(100, 200, 100, 200) ms.setExtent(extent) ms.setOutputSize(QSize(400, 400)) context = QgsRenderContext.fromMapSettings(ms) context.setScaleFactor(96 / 25.4) # 96 DPI ms.setExtent(QgsRectangle(100, 150, 100, 150)) ms.setOutputDpi(ms.outputDpi() * 2) context2 = QgsRenderContext.fromMapSettings(ms) context2.setScaleFactor(300 / 25.4) s = QgsFillSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.FirstVertex) simple_line = QgsSimpleLineSymbolLayer() line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) self.assertEqual(hash_line.width(), 10) self.assertAlmostEqual(hash_line.width(context), 37.795275590551185, 3) self.assertAlmostEqual(hash_line.width(context2), 118.11023622047244, 3) hash_line.setHashLengthUnit(QgsUnitTypes.RenderPixels) self.assertAlmostEqual(hash_line.width(context), 10.0, 3) self.assertAlmostEqual(hash_line.width(context2), 10.0, 3)
def testWidth(self): ms = QgsMapSettings() extent = QgsRectangle(100, 200, 100, 200) ms.setExtent(extent) ms.setOutputSize(QSize(400, 400)) context = QgsRenderContext.fromMapSettings(ms) context.setScaleFactor(96 / 25.4) # 96 DPI ms.setExtent(QgsRectangle(100, 150, 100, 150)) ms.setOutputDpi(ms.outputDpi() * 2) context2 = QgsRenderContext.fromMapSettings(ms) context2.setScaleFactor(300 / 25.4) s = QgsFillSymbol() s.deleteSymbolLayer(0) marker_line = QgsMarkerLineSymbolLayer(True) marker_line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex) marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 10) marker.setColor(QColor(255, 0, 0)) marker.setStrokeStyle(Qt.NoPen) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) marker_line.setSubSymbol(marker_symbol) self.assertEqual(marker_line.width(), 10) self.assertAlmostEqual(marker_line.width(context), 37.795275590551185, 3) self.assertAlmostEqual(marker_line.width(context2), 118.11023622047244, 3) marker_line.subSymbol().setSizeUnit(QgsUnitTypes.RenderPixels) self.assertAlmostEqual(marker_line.width(context), 10.0, 3) self.assertAlmostEqual(marker_line.width(context2), 10.0, 3)
def testSize(self): # test size and setSize ms = QgsMapSettings() extent = QgsRectangle(100, 200, 100, 200) ms.setExtent(extent) ms.setOutputSize(QSize(400, 400)) context = QgsRenderContext.fromMapSettings(ms) context.setScaleFactor(96 / 25.4) # 96 DPI ms.setExtent(QgsRectangle(100, 150, 100, 150)) ms.setOutputDpi(ms.outputDpi() * 2) context2 = QgsRenderContext.fromMapSettings(ms) context2.setScaleFactor(300 / 25.4) # create a marker symbol with a single layer markerSymbol = QgsMarkerSymbol() markerSymbol.deleteSymbolLayer(0) markerSymbol.appendSymbolLayer( QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=10)) self.assertEqual(markerSymbol.size(), 10) self.assertAlmostEqual(markerSymbol.size(context), 37.795275590551185, 3) self.assertAlmostEqual(markerSymbol.size(context2), 118.11023622047244, 3) markerSymbol.setSize(20) self.assertEqual(markerSymbol.size(), 20) self.assertEqual(markerSymbol.symbolLayer(0).size(), 20) self.assertAlmostEqual(markerSymbol.size(context), 75.59055118, 3) self.assertAlmostEqual(markerSymbol.size(context2), 236.2204724409449, 3) # add additional layers markerSymbol.appendSymbolLayer( QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=10)) markerSymbol.appendSymbolLayer( QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=30)) self.assertEqual(markerSymbol.size(), 30) self.assertAlmostEqual(markerSymbol.size(context), 113.38582677165356, 3) self.assertAlmostEqual(markerSymbol.size(context2), 354.33070866141736, 3) markerSymbol.setSize(3) self.assertEqual(markerSymbol.size(), 3) # layer sizes should maintain relative size self.assertEqual(markerSymbol.symbolLayer(0).size(), 2) self.assertEqual(markerSymbol.symbolLayer(1).size(), 1) self.assertEqual(markerSymbol.symbolLayer(2).size(), 3) # symbol layer in different size markerSymbol.symbolLayer(1).setSize(15) self.assertAlmostEqual(markerSymbol.size(context), 56.69291338582678, 3) self.assertAlmostEqual(markerSymbol.size(context2), 177.16535433070868, 3) markerSymbol.symbolLayer(1).setSizeUnit(QgsUnitTypes.RenderPixels) self.assertAlmostEqual(markerSymbol.size(context), 15, 3) self.assertAlmostEqual(markerSymbol.size(context2), 35.43307086614173, 3) markerSymbol.symbolLayer(1).setSize(45) self.assertAlmostEqual(markerSymbol.size(context), 45, 3) self.assertAlmostEqual(markerSymbol.size(context2), 45, 3)
class TestSelectiveMasking(unittest.TestCase): def setUp(self): self.checker = QgsRenderChecker() self.checker.setControlPathPrefix("selective_masking") self.report = "<h1>Python Selective Masking Tests</h1>\n" self.map_settings = QgsMapSettings() crs = QgsCoordinateReferenceSystem('epsg:4326') extent = QgsRectangle(-123.0, 22.7, -76.4, 46.9) self.map_settings.setBackgroundColor(QColor(152, 219, 249)) self.map_settings.setOutputSize(QSize(420, 280)) self.map_settings.setOutputDpi(72) self.map_settings.setFlag(QgsMapSettings.Antialiasing, True) self.map_settings.setFlag(QgsMapSettings.UseAdvancedEffects, False) self.map_settings.setDestinationCrs(crs) self.map_settings.setExtent(extent) # load a predefined QGIS project self.assertTrue(QgsProject.instance().read( os.path.join(unitTestDataPath(), "selective_masking.qgs"))) self.points_layer = QgsProject.instance().mapLayersByName('points')[0] self.lines_layer = QgsProject.instance().mapLayersByName('lines')[0] # line layer with subsymbols self.lines_layer2 = QgsProject.instance().mapLayersByName('lines2')[0] # line layer with labels self.lines_with_labels = QgsProject.instance().mapLayersByName( 'lines_with_labels')[0] self.polys_layer = QgsProject.instance().mapLayersByName('polys')[0] # polygon layer with a rule based labeling self.polys_layer2 = QgsProject.instance().mapLayersByName('polys2')[0] self.raster_layer = QgsProject.instance().mapLayersByName( 'raster_layer')[0] # try to fix the font for where labels are defined # in order to have more stable image comparison tests for layer in [ self.polys_layer, self.lines_with_labels, self.polys_layer2 ]: for provider in layer.labeling().subProviders(): settings = layer.labeling().settings(provider) font = getTestFont() font.setPointSize(32) fmt = settings.format() fmt.setFont(font) fmt.setNamedStyle('Roman') fmt.setSize(32) fmt.setSizeUnit(QgsUnitTypes.RenderPoints) settings.setFormat(fmt) if (layer.geometryType == QgsWkbTypes.PolygonGeometry): settings.placement = QgsPalLayerSettings.OverPoint layer.labeling().setSettings(settings, provider) # order layers for rendering self.map_settings.setLayers( [self.points_layer, self.lines_layer, self.polys_layer]) def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def check_renderings(self, map_settings, control_name): """Test a rendering with different configurations: - parallel rendering, no cache - sequential rendering, no cache - parallel rendering, with cache (rendered two times) - sequential rendering, with cache (rendered two times) """ for do_parallel in [False, True]: for use_cache in [False, True]: print("=== parallel", do_parallel, "cache", use_cache) tmp = getTempfilePath('png') cache = None if use_cache: cache = QgsMapRendererCache() # render a first time to fill the cache renderMapToImageWithTime(self.map_settings, parallel=do_parallel, cache=cache) img, t = renderMapToImageWithTime(self.map_settings, parallel=do_parallel, cache=cache) img.save(tmp) print("Image rendered in {}".format(tmp)) self.checker.setControlName(control_name) self.checker.setRenderedImage(tmp) suffix = ("_parallel" if do_parallel else "_sequential") + ( "_cache" if use_cache else "_nocache") res = self.checker.compareImages(control_name + suffix) self.report += self.checker.report() self.assertTrue(res) print("=== Rendering took {}s".format(float(t) / 1000.0)) def check_layout_export(self, control_name, expected_nb_raster, layers=None, dpiTarget=None): """ Generate a PDF layout export and control the output matches expected_filename """ # generate vector file layout = QgsLayout(QgsProject.instance()) page = QgsLayoutItemPage(layout) page.setPageSize(QgsLayoutSize(50, 33)) layout.pageCollection().addPage(page) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(1, 1, 48, 32)) map.setFrameEnabled(True) layout.addLayoutItem(map) map.setExtent(self.lines_layer.extent()) map.setLayers(layers if layers is not None else [self.points_layer, self.lines_layer, self.polys_layer]) settings = QgsLayoutExporter.PdfExportSettings() if dpiTarget is not None: settings.dpi = dpiTarget exporter = QgsLayoutExporter(layout) result_filename = getTempfilePath('pdf') exporter.exportToPdf(result_filename, settings) self.assertTrue(os.path.exists(result_filename)) # Generate a readable PDF file so we count raster in it result_txt = getTempfilePath("txt") subprocess.run([ "qpdf", "--qdf", "--object-streams=disable", result_filename, result_txt ]) self.assertTrue(os.path.exists(result_txt)) # expected_file = os.path.join(TEST_DATA_DIR, "control_images/selective_masking/pdf_exports/{}".format(expected_filename)) # self.assertTrue(os.path.exists(expected_file)) result = open(result_txt, 'rb') result_lines = [l.decode('iso-8859-1') for l in result.readlines()] result.close() nb_raster = len([l for l in result_lines if "/Subtype /Image" in l]) self.assertEqual(nb_raster, expected_nb_raster) # Generate an image from pdf to compare with expected control image # keep PDF DPI resolution (300) image_result_filename = getTempfilePath("png") subprocess.run([ "pdftoppm", result_filename, os.path.splitext(image_result_filename)[0], "-png", "-r", "300", "-singlefile" ]) self.checker.setControlName(control_name) self.checker.setRenderedImage(image_result_filename) res = self.checker.compareImages(control_name) self.report += self.checker.report() self.assertTrue(res) def test_save_restore_references(self): """ Test saving and restoring symbol layer references """ # simple ids mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setMasks([ QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), QgsSymbolLayerReference(self.lines_layer2.id(), QgsSymbolLayerId("some_id", [1, 3, 5, 19])), QgsSymbolLayerReference(self.polys_layer.id(), QgsSymbolLayerId("some_other_id", [4, 5])), ]) props = mask_layer.properties() mask_layer2 = QgsMaskMarkerSymbolLayer.create(props) self.assertEqual(mask_layer2.masks(), [ QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), QgsSymbolLayerReference(self.lines_layer2.id(), QgsSymbolLayerId("some_id", [1, 3, 5, 19])), QgsSymbolLayerReference(self.polys_layer.id(), QgsSymbolLayerId("some_other_id", [4, 5])), ]) # complex ids mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setMasks([ QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), QgsSymbolLayerReference( self.lines_layer2.id(), QgsSymbolLayerId("some id, #1", [1, 3, 5, 19])), QgsSymbolLayerReference( self.polys_layer.id(), QgsSymbolLayerId("some other id, like, this", [4, 5])), ]) props = mask_layer.properties() mask_layer2 = QgsMaskMarkerSymbolLayer.create(props) self.assertEqual(mask_layer2.masks(), [ QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), QgsSymbolLayerReference( self.lines_layer2.id(), QgsSymbolLayerId("some id, #1", [1, 3, 5, 19])), QgsSymbolLayerReference( self.polys_layer.id(), QgsSymbolLayerId("some other id, like, this", [4, 5])), ]) # complex ids, v2 mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setMasks([ QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("a string; with bits", 0)), QgsSymbolLayerReference( self.lines_layer2.id(), QgsSymbolLayerId("some; id, #1", [1, 3, 5, 19])), QgsSymbolLayerReference( self.polys_layer.id(), QgsSymbolLayerId("some other; id, lik;e, this", [4, 5])), ]) props = mask_layer.properties() mask_layer2 = QgsMaskMarkerSymbolLayer.create(props) self.assertEqual(mask_layer2.masks(), [ QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("a string; with bits", 0)), QgsSymbolLayerReference( self.lines_layer2.id(), QgsSymbolLayerId("some; id, #1", [1, 3, 5, 19])), QgsSymbolLayerReference( self.polys_layer.id(), QgsSymbolLayerId("some other; id, lik;e, this", [4, 5])), ]) def test_label_mask(self): # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) format = self.polys_layer.labeling().settings().format() self.assertTrue(format.mask().enabled()) self.check_renderings(self.map_settings, "label_mask") def test_multiple_label_masks_different_sets(self): # modify labeling settings of the polys layer label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_with_labels.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) format = self.polys_layer.labeling().settings().format() self.assertTrue(format.mask().enabled()) # modify labeling settings of the lines layer label_settings = self.lines_with_labels.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # polygons QgsSymbolLayerReference(self.polys_layer.id(), QgsSymbolLayerId("", 0)), ]) label_settings.setFormat(fmt) self.lines_with_labels.labeling().setSettings(label_settings) # new map settings with a line symbology that has labels self.map_settings.setLayers( [self.points_layer, self.lines_with_labels, self.polys_layer]) self.check_renderings(self.map_settings, "multiple_label_masks_different_sets") # restore map settings self.map_settings.setLayers( [self.points_layer, self.lines_layer, self.polys_layer]) def test_multiple_label_masks_same_set(self): # modify labeling settings of the polys layer label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_with_labels.id(), QgsSymbolLayerId("", 0)), ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) format = self.polys_layer.labeling().settings().format() self.assertTrue(format.mask().enabled()) # modify labeling settings of the lines layer label_settings = self.lines_with_labels.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_with_labels.id(), QgsSymbolLayerId("", 0)), ]) label_settings.setFormat(fmt) self.lines_with_labels.labeling().setSettings(label_settings) # new map settings with a line symbology that has labels self.map_settings.setLayers( [self.points_layer, self.lines_with_labels, self.polys_layer]) self.check_renderings(self.map_settings, "multiple_label_masks_same_set") # restore map settings self.map_settings.setLayers( [self.points_layer, self.lines_layer, self.polys_layer]) def test_label_mask_subsymbol(self): # new map settings with a line symbology that has sub symbols self.map_settings.setLayers( [self.points_layer, self.lines_layer2, self.polys_layer]) # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # mask only vertical segments of "roads" QgsSymbolLayerReference(self.lines_layer2.id(), QgsSymbolLayerId("", [1, 0])), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) format = self.polys_layer.labeling().settings().format() self.assertTrue(format.mask().enabled()) self.check_renderings(self.map_settings, "label_mask_subsymbol") # restore original map settings self.map_settings.setLayers( [self.points_layer, self.lines_layer, self.polys_layer]) def test_label_mask_dd(self): """ test label mask with data defined properties """ label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() fmt.mask().setEnabled(False) fmt.mask().setSize(1.0) fmt.mask().setOpacity(0.42) # mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) # overwrite with data-defined properties fmt.dataDefinedProperties().setProperty( QgsPalLayerSettings.MaskEnabled, QgsProperty.fromExpression('1')) fmt.dataDefinedProperties().setProperty( QgsPalLayerSettings.MaskBufferSize, QgsProperty.fromExpression('4.0')) fmt.dataDefinedProperties().setProperty( QgsPalLayerSettings.MaskOpacity, QgsProperty.fromExpression('100.0')) context = QgsRenderContext() fmt.updateDataDefinedProperties(context) self.assertEqual(fmt.mask().enabled(), True) self.assertEqual(fmt.mask().size(), 4.0) self.assertEqual(fmt.mask().opacity(), 1.0) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.check_renderings(self.map_settings, "label_mask") def test_label_mask_rule_labeling(self): # new map settings with a rule based labeling self.map_settings.setLayers( [self.points_layer, self.lines_layer, self.polys_layer2]) # modify labeling settings of one rule for child in self.polys_layer2.labeling().rootRule().children(): if child.description() == 'Tadam': break label_settings = child.settings() label_settings.priority = 3 fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) child.setSettings(label_settings) # modify labeling settings of another rule for child in self.polys_layer2.labeling().rootRule().children(): if child.description() != 'Tadam': break label_settings = child.settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the polygons QgsSymbolLayerReference(self.polys_layer2.id(), QgsSymbolLayerId("", 0)), ]) label_settings.setFormat(fmt) child.setSettings(label_settings) self.check_renderings(self.map_settings, "rule_label_mask") # restore map settings self.map_settings.setLayers( [self.points_layer, self.lines_layer, self.polys_layer]) def test_label_mask_symbol_levels(self): # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) format = self.polys_layer.labeling().settings().format() self.assertTrue(format.mask().enabled()) # enable symbol levels self.lines_layer.renderer().setUsingSymbolLevels(True) self.check_renderings(self.map_settings, "label_mask_symbol_levels") def test_symbol_layer_mask(self): p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "7"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '10'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) self.check_renderings(self.map_settings, "sl_mask") def test_multiple_masks_same_symbol_layer(self): """Test multiple masks that occlude the same symbol layer""" # # 1. a symbol layer mask # p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "7"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '10'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) # # 2. a label mask # # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.check_renderings(self.map_settings, "multiple_masks_same_sl") def test_multiple_masks_different_symbol_layers_same_layer(self): """Test multiple masks that occlude different symbol layers of the same layer. The UI should disallow this settings. We test here that only one mask is retained""" # # 1. a symbol layer mask # p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "7"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '10'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the yellow part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 1)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) # # 2. a label mask # # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.check_renderings(self.map_settings, "multiple_masks_different_sl") def test_multiple_masks_different_symbol_layers_same_layer2(self): """Test multiple masks that occlude different symbol layers of the same layer - 2nd possible order The UI should disallow this settings. We test here that only one mask is retained""" # # 1. a symbol layer mask # p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "7"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '10'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) # # 2. a label mask # # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the yellow part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 1)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.check_renderings(self.map_settings, "multiple_masks_different_sl2") def test_mask_symbollayer_preview(self): # # Masks should be visible in previews # p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "7"}) circle_symbol = QgsMarkerSymbol.createSimple({'size': '10'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) p.insertSymbolLayer(0, mask_layer) for control_name, render_function in [ ("as_image", lambda: p.asImage(QSize(64, 64)).save(tmp)), ("as_big_preview", lambda: p.bigSymbolPreviewImage().save(tmp)), ("sl_preview", lambda: QgsSymbolLayerUtils.symbolLayerPreviewIcon( mask_layer, QgsUnitTypes.RenderPixels, QSize(64, 64)).pixmap( QSize(64, 64)).save(tmp)) ]: tmp = getTempfilePath('png') render_function() self.checker.setControlName(control_name) self.checker.setRenderedImage(tmp) res = self.checker.compareImages(control_name, 90) self.report += self.checker.report() self.assertTrue(res) def test_mask_with_effect(self): p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "7"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '12'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the yellow part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 1)), ]) # add an outer glow effect to the mask layer blur = QgsOuterGlowEffect.create({ "enabled": "1", "blur_level": "6.445", "blur_unit": "MM", "opacity": "1", "spread": "0.6", "spread_unit": "MM", "color1": "0,0,255,255", "draw_mode": "2" }) mask_layer.setPaintEffect(blur) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) self.check_renderings(self.map_settings, "mask_with_effect") def test_label_mask_with_effect(self): # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) # add an outer glow effect to the mask blur = QgsOuterGlowEffect.create({ "enabled": "1", "blur_level": "6.445", "blur_unit": "MM", "opacity": "1", "spread": "0.6", "spread_unit": "MM", "color1": "0,0,255,255", "draw_mode": "2" }) fmt.mask().setPaintEffect(blur) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) format = self.polys_layer.labeling().settings().format() self.assertTrue(format.mask().enabled()) self.check_renderings(self.map_settings, "label_mask_with_effect") # test that force vector output has no impact on the result self.map_settings.setFlag(Qgis.MapSettingsFlag.ForceVectorOutput, True) self.check_renderings(self.map_settings, "label_mask_with_effect") def test_different_dpi_target(self): """Test with raster layer and a target dpi""" # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.map_settings.setLayers( [self.lines_layer, self.polys_layer, self.raster_layer]) self.map_settings.setDpiTarget(300) self.check_renderings(self.map_settings, "different_dpi_target") # test with high dpi screen self.map_settings.setDevicePixelRatio(2) self.check_renderings(self.map_settings, "different_dpi_target_hdpi") def test_layout_export(self): """Test mask effects in a layout export at 300 dpi""" # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.font().setPointSize(4) fmt.mask().setEnabled(True) fmt.mask().setSize(1.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.check_layout_export("layout_export", 0) def test_layout_export_w_effects(self): """Test mask effects in a layout export at 300 dpi""" # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.font().setPointSize(4) fmt.mask().setEnabled(True) fmt.mask().setSize(1.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) # add an outer glow effect to the mask blur = QgsOuterGlowEffect.create({ "enabled": "1", "blur_level": "3.445", "blur_unit": "MM", "opacity": "1", "spread": "0.06", "spread_unit": "MM", "color1": "0,0,255,255", "draw_mode": "2" }) fmt.mask().setPaintEffect(blur) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) # 4 rasters : Image and its mask for masked point and lines layer self.check_layout_export("layout_export_w_effects", 4) def test_layout_export_marker_masking(self): """Test mask effects in a layout export with a marker symbol masking""" p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "3"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '6'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) self.check_layout_export("layout_export_marker_masking", 0) def test_layout_export_marker_masking_w_effects(self): """Test mask effects in a layout export with a marker symbol masking""" p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "3"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '6'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add an outer glow effect to the mask blur = QgsOuterGlowEffect.create({ "enabled": "1", "blur_level": "3.445", "blur_unit": "MM", "opacity": "1", "spread": "0.06", "spread_unit": "MM", "color1": "0,0,255,255", "draw_mode": "2" }) # TODO try to set the mask effect on p the marker symbol -> result should be the same mask_layer.setPaintEffect(blur) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) # 2 rasters : Image and its mask for masked lines layer self.check_layout_export("layout_export_marker_masking_w_effects", 2) def test_layout_export_w_raster(self): """Test layout export with raster beneath the masked area""" # just decrease the yellow line so we see the raster on masked area self.lines_layer.renderer().symbol().symbolLayers()[1].setWidth(0.5) # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.font().setPointSize(4) fmt.mask().setEnabled(True) fmt.mask().setSize(1.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) # 1 raster : the raster layer self.check_layout_export( "layout_export_w_raster", 1, [self.lines_layer, self.polys_layer, self.raster_layer]) def test_layout_export_w_force_raster_render(self): """ Test layout export with a marker symbol masking forced to be render as raster We expect the lines to be masked and the whole output needs to be vector except the marker layer forced as raster """ p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "3"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '6'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) self.points_layer.renderer().setForceRasterRender(True) # 2 rasters : Image and its mask for the points layer self.check_layout_export("layout_export_force_raster_render", 2, [self.points_layer, self.lines_layer]) def test_layout_export_marker_masking_w_transparency(self): """Test layout export with a marker symbol masking which has an opacity lower than 1""" p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "3"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '6'}) circle_symbol.setOpacity(0.5) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) # add this mask layer to the point layer self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) # 2 rasters (mask + image) because opacity force rasterization of the masked line layers self.check_layout_export("layout_export_marker_masking_w_transparency", 2) def test_layout_export_text_masking_w_transparency(self): """Test mask effects in a layout export at 300 dpi""" # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.font().setPointSize(4) fmt.mask().setEnabled(True) fmt.mask().setSize(1.0) fmt.mask().setOpacity(0.5) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) # 4 rasters (mask+image per masked layer) because opacity force rasterization # of the masked line and point layers self.check_layout_export("layout_export_text_masking_w_transparency", 4) def test_different_dpi_target_vector(self): """Test rendering a raster layer with vector output and a target dpi Used when layout previewing """ # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.map_settings.setLayers( [self.lines_layer, self.polys_layer, self.raster_layer]) self.map_settings.setOutputDpi(81) self.map_settings.setDpiTarget(300) self.map_settings.setFlag(Qgis.MapSettingsFlag.ForceVectorOutput, True) image = QImage(self.map_settings.deviceOutputSize(), self.map_settings.outputImageFormat()) image.setDevicePixelRatio(self.map_settings.devicePixelRatio()) image.setDotsPerMeterX(int(1000 * self.map_settings.outputDpi() / 25.4)) image.setDotsPerMeterY(int(1000 * self.map_settings.outputDpi() / 25.4)) image.fill(Qt.transparent) pImg = QPainter() pImg.begin(image) job = QgsMapRendererCustomPainterJob(self.map_settings, pImg) job.start() job.waitForFinished() pImg.end() tmp = getTempfilePath('png') image.save(tmp) control_name = "different_dpi_target_vector" self.checker.setControlName(control_name) self.checker.setRenderedImage(tmp) res = self.checker.compareImages(control_name) self.report += self.checker.report() self.assertTrue(res) # Same test with high dpi self.map_settings.setDevicePixelRatio(2) image = QImage(self.map_settings.deviceOutputSize(), self.map_settings.outputImageFormat()) image.setDevicePixelRatio(self.map_settings.devicePixelRatio()) image.setDotsPerMeterX(int(1000 * self.map_settings.outputDpi() / 25.4)) image.setDotsPerMeterY(int(1000 * self.map_settings.outputDpi() / 25.4)) image.fill(Qt.transparent) pImg = QPainter() pImg.begin(image) job = QgsMapRendererCustomPainterJob(self.map_settings, pImg) job.start() job.waitForFinished() pImg.end() tmp = getTempfilePath('png') image.save(tmp) control_name = "different_dpi_target_vector_hdpi" self.checker.setControlName(control_name) self.checker.setRenderedImage(tmp) res = self.checker.compareImages(control_name) self.report += self.checker.report() self.assertTrue(res) def test_layout_export_2_sources_masking(self): """Test masking with 2 different sources""" # mask with points layer circles... p = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'size': "3"}) self.points_layer.setRenderer(QgsSingleSymbolRenderer(p)) circle_symbol = QgsMarkerSymbol.createSimple({'size': '6'}) mask_layer = QgsMaskMarkerSymbolLayer() mask_layer.setSubSymbol(circle_symbol) mask_layer.setMasks([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), ]) self.points_layer.renderer().symbol().appendSymbolLayer(mask_layer) # ...and with text label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() fmt.font().setPointSize(4) fmt.mask().setEnabled(True) fmt.mask().setSize(1.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)) ]) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) self.check_layout_export("layout_export_2_sources_masking", 0)
def processAlgorithm(self, parameters, context, feedback): feedback.setProgress(1) extent = self.parameterAsExtent(parameters, self.EXTENT, context) min_zoom = self.parameterAsInt(parameters, self.ZOOM_MIN, context) max_zoom = self.parameterAsInt(parameters, self.ZOOM_MAX, context) dpi = self.parameterAsInt(parameters, self.DPI, context) tile_format = self.formats[self.parameterAsEnum(parameters, self.TILE_FORMAT, context)] output_format = self.outputs[self.parameterAsEnum(parameters, self.OUTPUT_FORMAT, context)] if output_format == 'Directory': output_dir = self.parameterAsString(parameters, self.OUTPUT_DIRECTORY, context) if not output_dir: raise QgsProcessingException(self.tr('You need to specify output directory.')) else: # MBTiles output_file = self.parameterAsString(parameters, self.OUTPUT_FILE, context) if not output_file: raise QgsProcessingException(self.tr('You need to specify output filename.')) tile_width = 256 tile_height = 256 wgs_crs = QgsCoordinateReferenceSystem('EPSG:4326') dest_crs = QgsCoordinateReferenceSystem('EPSG:3857') project = context.project() src_to_wgs = QgsCoordinateTransform(project.crs(), wgs_crs, context.transformContext()) wgs_to_dest = QgsCoordinateTransform(wgs_crs, dest_crs, context.transformContext()) settings = QgsMapSettings() settings.setOutputImageFormat(QImage.Format_ARGB32_Premultiplied) settings.setDestinationCrs(dest_crs) settings.setLayers(self.layers) settings.setOutputDpi(dpi) if tile_format == 'PNG': settings.setBackgroundColor(QColor(Qt.transparent)) wgs_extent = src_to_wgs.transformBoundingBox(extent) wgs_extent = [wgs_extent.xMinimum(), wgs_extent.yMinimum(), wgs_extent.xMaximum(), wgs_extent.yMaximum()] metatiles_by_zoom = {} metatiles_count = 0 for zoom in range(min_zoom, max_zoom + 1): metatiles = get_metatiles(wgs_extent, zoom, 4) metatiles_by_zoom[zoom] = metatiles metatiles_count += len(metatiles) lab_buffer_px = 100 progress = 0 tile_params = { 'format': tile_format, 'quality': 75, 'width': tile_width, 'height': tile_height } if output_format == 'Directory': writer = DirectoryWriter(output_dir, tile_params) else: writer = MBTilesWriter(output_file, tile_params, wgs_extent, min_zoom, max_zoom) for zoom in range(min_zoom, max_zoom + 1): feedback.pushConsoleInfo('Generating tiles for zoom level: %s' % zoom) for i, metatile in enumerate(metatiles_by_zoom[zoom]): size = QSize(tile_width * metatile.rows(), tile_height * metatile.columns()) extent = QgsRectangle(*metatile.extent()) settings.setExtent(wgs_to_dest.transformBoundingBox(extent)) settings.setOutputSize(size) label_area = QgsRectangle(settings.extent()) lab_buffer = label_area.width() * (lab_buffer_px / size.width()) label_area.set( label_area.xMinimum() + lab_buffer, label_area.yMinimum() + lab_buffer, label_area.xMaximum() - lab_buffer, label_area.yMaximum() - lab_buffer ) settings.setLabelBoundaryGeometry(QgsGeometry.fromRect(label_area)) image = QImage(size, QImage.Format_ARGB32_Premultiplied) image.fill(Qt.transparent) dpm = settings.outputDpi() / 25.4 * 1000 image.setDotsPerMeterX(dpm) image.setDotsPerMeterY(dpm) painter = QPainter(image) job = QgsMapRendererCustomPainterJob(settings, painter) job.renderSynchronously() painter.end() # For analysing metatiles (labels, etc.) # metatile_dir = os.path.join(output_dir, str(zoom)) # os.makedirs(metatile_dir, exist_ok=True) # image.save(os.path.join(metatile_dir, 'metatile_%s.png' % i)) for r, c, tile in metatile.tiles: tile_img = image.copy(tile_width * r, tile_height * c, tile_width, tile_height) writer.writeTile(tile, tile_img) progress += 1 feedback.setProgress(100 * (progress / metatiles_count)) writer.close() results = {} if output_format == 'Directory': results['OUTPUT_DIRECTORY'] = output_dir else: # MBTiles results['OUTPUT_FILE'] = output_file return results
def processAlgorithm(self, parameters, context, feedback): feedback.setProgress(1) extent = self.parameterAsExtent(parameters, self.EXTENT, context) min_zoom = self.parameterAsInt(parameters, self.ZOOM_MIN, context) max_zoom = self.parameterAsInt(parameters, self.ZOOM_MAX, context) dpi = self.parameterAsInt(parameters, self.DPI, context) tile_format = self.formats[self.parameterAsEnum(parameters, self.TILE_FORMAT, context)] output_format = self.outputs[self.parameterAsEnum(parameters, self.OUTPUT_FORMAT, context)] if output_format == 'Directory': output_dir = self.parameterAsString(parameters, self.OUTPUT_DIRECTORY, context) if not output_dir: raise QgsProcessingException(self.tr('You need to specify output directory.')) else: # MBTiles output_file = self.parameterAsString(parameters, self.OUTPUT_FILE, context) if not output_file: raise QgsProcessingException(self.tr('You need to specify output filename.')) tile_width = 256 tile_height = 256 wgs_crs = QgsCoordinateReferenceSystem('EPSG:4326') dest_crs = QgsCoordinateReferenceSystem('EPSG:3857') project = context.project() src_to_wgs = QgsCoordinateTransform(project.crs(), wgs_crs, context.transformContext()) wgs_to_dest = QgsCoordinateTransform(wgs_crs, dest_crs, context.transformContext()) settings = QgsMapSettings() settings.setOutputImageFormat(QImage.Format_ARGB32_Premultiplied) settings.setDestinationCrs(dest_crs) settings.setLayers(self.layers) settings.setOutputDpi(dpi) wgs_extent = src_to_wgs.transformBoundingBox(extent) wgs_extent = [wgs_extent.xMinimum(), wgs_extent.yMinimum(), wgs_extent.xMaximum(), wgs_extent.yMaximum()] metatiles_by_zoom = {} metatiles_count = 0 for zoom in range(min_zoom, max_zoom + 1): metatiles = get_metatiles(wgs_extent, zoom, 4) metatiles_by_zoom[zoom] = metatiles metatiles_count += len(metatiles) lab_buffer_px = 100 progress = 0 tile_params = { 'format': tile_format, 'quality': 75, 'width': tile_width, 'height': tile_height } if output_format == 'Directory': writer = DirectoryWriter(output_dir, tile_params) else: writer = MBTilesWriter(output_file, tile_params, wgs_extent, min_zoom, max_zoom) for zoom in range(min_zoom, max_zoom + 1): feedback.pushConsoleInfo('Generating tiles for zoom level: %s' % zoom) for i, metatile in enumerate(metatiles_by_zoom[zoom]): size = QSize(tile_width * metatile.rows(), tile_height * metatile.columns()) extent = QgsRectangle(*metatile.extent()) settings.setExtent(wgs_to_dest.transformBoundingBox(extent)) settings.setOutputSize(size) label_area = QgsRectangle(settings.extent()) lab_buffer = label_area.width() * (lab_buffer_px / size.width()) label_area.set( label_area.xMinimum() + lab_buffer, label_area.yMinimum() + lab_buffer, label_area.xMaximum() - lab_buffer, label_area.yMaximum() - lab_buffer ) settings.setLabelBoundaryGeometry(QgsGeometry.fromRect(label_area)) image = QImage(size, QImage.Format_ARGB32_Premultiplied) image.fill(Qt.transparent) dpm = settings.outputDpi() / 25.4 * 1000 image.setDotsPerMeterX(dpm) image.setDotsPerMeterY(dpm) painter = QPainter(image) job = QgsMapRendererCustomPainterJob(settings, painter) job.renderSynchronously() painter.end() # For analysing metatiles (labels, etc.) # metatile_dir = os.path.join(output_dir, str(zoom)) # os.makedirs(metatile_dir, exist_ok=True) # image.save(os.path.join(metatile_dir, 'metatile_%s.png' % i)) for r, c, tile in metatile.tiles: tile_img = image.copy(tile_width * r, tile_height * c, tile_width, tile_height) writer.writeTile(tile, tile_img) progress += 1 feedback.setProgress(100 * (progress / metatiles_count)) writer.close() results = {} if output_format == 'Directory': results['OUTPUT_DIRECTORY'] = output_dir else: # MBTiles results['OUTPUT_FILE'] = output_file return results
def generate(self, writer, parameters, context, feedback): feedback.setProgress(1) extent = self.parameterAsExtent(parameters, self.EXTENT, context) self.min_zoom = self.parameterAsInt(parameters, self.ZOOM_MIN, context) self.max_zoom = self.parameterAsInt(parameters, self.ZOOM_MAX, context) dpi = self.parameterAsInt(parameters, self.DPI, context) self.tile_format = self.formats[self.parameterAsEnum(parameters, self.TILE_FORMAT, context)] tile_width = 256 tile_height = 256 wgs_crs = QgsCoordinateReferenceSystem('EPSG:4326') dest_crs = QgsCoordinateReferenceSystem('EPSG:3857') project = context.project() src_to_wgs = QgsCoordinateTransform(project.crs(), wgs_crs, context.transformContext()) wgs_to_dest = QgsCoordinateTransform(wgs_crs, dest_crs, context.transformContext()) settings = QgsMapSettings() settings.setOutputImageFormat(QImage.Format_ARGB32_Premultiplied) settings.setDestinationCrs(dest_crs) settings.setLayers(self.layers) settings.setOutputDpi(dpi) if self.tile_format == 'PNG': settings.setBackgroundColor(QColor(Qt.transparent)) self.wgs_extent = src_to_wgs.transformBoundingBox(extent) self.wgs_extent = [self.wgs_extent.xMinimum(), self.wgs_extent.yMinimum(), self.wgs_extent.xMaximum(), self.wgs_extent.yMaximum()] metatiles_by_zoom = {} metatiles_count = 0 for zoom in range(self.min_zoom, self.max_zoom + 1): metatiles = get_metatiles(self.wgs_extent, zoom, 4) metatiles_by_zoom[zoom] = metatiles metatiles_count += len(metatiles) lab_buffer_px = 100 progress = 0 tile_params = { 'format': self.tile_format, 'quality': 75, 'width': tile_width, 'height': tile_height, 'min_zoom': self.min_zoom, 'max_zoom': self.max_zoom, 'extent': self.wgs_extent, } writer.set_parameters(tile_params) for zoom in range(self.min_zoom, self.max_zoom + 1): feedback.pushConsoleInfo('Generating tiles for zoom level: %s' % zoom) for i, metatile in enumerate(metatiles_by_zoom[zoom]): if feedback.isCanceled(): break size = QSize(tile_width * metatile.rows(), tile_height * metatile.columns()) extent = QgsRectangle(*metatile.extent()) settings.setExtent(wgs_to_dest.transformBoundingBox(extent)) settings.setOutputSize(size) if hasattr(settings, 'setLabelBoundaryGeometry'): label_area = QgsRectangle(settings.extent()) lab_buffer = label_area.width() * (lab_buffer_px / size.width()) label_area.set( label_area.xMinimum() + lab_buffer, label_area.yMinimum() + lab_buffer, label_area.xMaximum() - lab_buffer, label_area.yMaximum() - lab_buffer ) settings.setLabelBoundaryGeometry(QgsGeometry.fromRect(label_area)) image = QImage(size, QImage.Format_ARGB32_Premultiplied) image.fill(Qt.transparent) dpm = settings.outputDpi() / 25.4 * 1000 image.setDotsPerMeterX(dpm) image.setDotsPerMeterY(dpm) painter = QPainter(image) job = QgsMapRendererCustomPainterJob(settings, painter) job.renderSynchronously() painter.end() # For analysing metatiles (labels, etc.) # metatile_dir = os.path.join(output_dir, str(zoom)) # os.makedirs(metatile_dir, exist_ok=True) # image.save(os.path.join(metatile_dir, 'metatile_%s.png' % i)) for r, c, tile in metatile.tiles: tile_img = image.copy(tile_width * r, tile_height * c, tile_width, tile_height) writer.write_tile(tile, tile_img) progress += 1 feedback.setProgress(100 * (progress / metatiles_count)) writer.close()
def generate(self, writer, parameters, context, feedback): feedback.setProgress(1) extent = self.parameterAsExtent(parameters, self.EXTENT, context) self.min_zoom = self.parameterAsInt(parameters, self.ZOOM_MIN, context) self.max_zoom = self.parameterAsInt(parameters, self.ZOOM_MAX, context) dpi = self.parameterAsInt(parameters, self.DPI, context) color = self.parameterAsColor(parameters, self.BACKGROUND_COLOR, context) self.tile_format = self.formats[self.parameterAsEnum( parameters, self.TILE_FORMAT, context)] quality = self.parameterAsInt(parameters, self.QUALITY, context) self.metatilesize = self.parameterAsInt(parameters, self.METATILESIZE, context) try: tile_width = self.parameterAsInt(parameters, self.TILE_WIDTH, context) tile_height = self.parameterAsInt(parameters, self.TILE_HEIGHT, context) except AttributeError: tile_width = 256 tile_height = 256 wgs_crs = QgsCoordinateReferenceSystem('EPSG:4326') dest_crs = QgsCoordinateReferenceSystem('EPSG:3857') project = context.project() src_to_wgs = QgsCoordinateTransform(project.crs(), wgs_crs, context.transformContext()) wgs_to_dest = QgsCoordinateTransform(wgs_crs, dest_crs, context.transformContext()) settings = QgsMapSettings() settings.setOutputImageFormat(QImage.Format_ARGB32_Premultiplied) settings.setDestinationCrs(dest_crs) settings.setLayers(self.layers) settings.setOutputDpi(dpi) if self.tile_format == 'PNG': settings.setBackgroundColor(color) # disable partial labels (they would be cut at the edge of tiles) labeling_engine_settings = settings.labelingEngineSettings() labeling_engine_settings.setFlag( QgsLabelingEngineSettings.UsePartialCandidates, False) settings.setLabelingEngineSettings(labeling_engine_settings) self.wgs_extent = src_to_wgs.transformBoundingBox(extent) self.wgs_extent = [ self.wgs_extent.xMinimum(), self.wgs_extent.yMinimum(), self.wgs_extent.xMaximum(), self.wgs_extent.yMaximum() ] metatiles_by_zoom = {} metatiles_count = 0 for zoom in range(self.min_zoom, self.max_zoom + 1): metatiles = get_metatiles(self.wgs_extent, zoom, self.metatilesize) metatiles_by_zoom[zoom] = metatiles metatiles_count += len(metatiles) lab_buffer_px = 100 progress = 0 tile_params = { 'format': self.tile_format, 'quality': quality, 'width': tile_width, 'height': tile_height, 'min_zoom': self.min_zoom, 'max_zoom': self.max_zoom, 'extent': self.wgs_extent, } writer.set_parameters(tile_params) for zoom in range(self.min_zoom, self.max_zoom + 1): feedback.pushConsoleInfo('Generating tiles for zoom level: %s' % zoom) for i, metatile in enumerate(metatiles_by_zoom[zoom]): if feedback.isCanceled(): break size = QSize(tile_width * metatile.rows(), tile_height * metatile.columns()) extent = QgsRectangle(*metatile.extent()) settings.setExtent(wgs_to_dest.transformBoundingBox(extent)) settings.setOutputSize(size) image = QImage(size, QImage.Format_ARGB32_Premultiplied) image.fill(color) dpm = settings.outputDpi() / 25.4 * 1000 image.setDotsPerMeterX(dpm) image.setDotsPerMeterY(dpm) painter = QPainter(image) job = QgsMapRendererCustomPainterJob(settings, painter) job.renderSynchronously() painter.end() # For analysing metatiles (labels, etc.) # metatile_dir = os.path.join(output_dir, str(zoom)) # os.makedirs(metatile_dir, exist_ok=True) # image.save(os.path.join(metatile_dir, 'metatile_%s.png' % i)) for r, c, tile in metatile.tiles: tile_img = image.copy(tile_width * r, tile_height * c, tile_width, tile_height) writer.write_tile(tile, tile_img) progress += 1 feedback.setProgress(100 * (progress / metatiles_count)) writer.close()
def renderer(self): qgis = QgsApplication([], False) qgis.setPrefixPath(self.settings.get('path'), True) qgis.setMaxThreads(1) qgis.initQgis() while True: try: fndata, srs, render_size, extended, \ target_box, result = self.queue.get() layer = QgsVectorLayer(fndata, 'layer', 'ogr') crs = QgsCoordinateReferenceSystem(srs.id) layer.setCrs(crs) settings = QgsMapSettings() settings.setLayers([layer.id()]) settings.setFlag(QgsMapSettings.DrawLabeling) settings.setFlag(QgsMapSettings.Antialiasing) settings.setCrsTransformEnabled(True) settings.setDestinationCrs(crs) settings.setMapUnits(crs.mapUnits()) settings.setOutputSize(QSize(*render_size)) settings.setExtent(QgsRectangle(*extended)) settings.setOutputImageFormat(QImage.Format_ARGB32) bgcolor = QColor.fromRgba(qRgba(255, 255, 255, 0)) settings.setBackgroundColor(bgcolor) settings.setOutputDpi(96) QgsMapLayerRegistry.instance().addMapLayer(layer) settings.setLayers([layer.id()]) # Создаем QImage руками чтобы можно было использовать # QgsMapRendererCustomPainterJob. Остальные не позволяют # обойти баг с рисованием поверх старого. img = QImage(settings.outputSize(), QImage.Format_ARGB32) # Эти костыли нужны для того, чтобы корректно рисовались # слои на прозрачном фоне, без этого получается каша. img.fill(QColor.fromRgba(qRgba(255, 255, 255, 255))) img.fill(QColor.fromRgba(qRgba(255, 255, 255, 0))) # DPI должно быть таким же как в settings, иначе ошибка. В QImage # разрешение указывается в точках на метр по каждой оси. dpm = settings.outputDpi() / 25.4 * 1000 img.setDotsPerMeterX(dpm) img.setDotsPerMeterY(dpm) painter = QPainter(img) job = QgsMapRendererCustomPainterJob(settings, painter) job.renderSynchronously() painter.end() QgsMapLayerRegistry.instance().removeAllMapLayers() # Преобразование QImage в PIL ba = QByteArray() bf = QBuffer(ba) bf.open(QIODevice.WriteOnly) img.save(bf, 'PNG') bf.close() buf = StringIO() buf.write(bf.data()) buf.seek(0) img = PIL.Image.open(buf) # Вырезаем нужный нам кусок изображения result.put(img.crop(target_box)) except Exception as e: self.logger.error(e.message) qgis.exitQgis()