Beispiel #1
0
    def testSetCacheImageDifferentParams(self):
        """
        Test setting cache image with different parameters
        """
        cache = QgsMapRendererCache()
        cache.updateParameters(QgsRectangle(1, 1, 3, 3), QgsMapToPixel(5))
        im = QImage(200, 200, QImage.Format_RGB32)
        cache.setCacheImage('im1', im, [])

        self.assertEqual(cache.cacheImage('im1').width(), 200)

        # if existing cached image exists with matching parameters, we don't store a new image -- old
        # one should still be retained
        im = QImage(201, 201, QImage.Format_RGB32)
        cache.setCacheImageWithParameters('im1', im, QgsRectangle(1, 1, 3, 4), QgsMapToPixel(5), [])
        self.assertEqual(cache.cacheImage('im1').width(), 200)
        cache.setCacheImageWithParameters('im1', im, QgsRectangle(1, 1, 3, 3), QgsMapToPixel(6), [])
        self.assertEqual(cache.cacheImage('im1').width(), 200)

        # replace with matching parameters
        cache.setCacheImageWithParameters('im1', im, QgsRectangle(1, 1, 3, 3), QgsMapToPixel(5), [])
        self.assertEqual(cache.cacheImage('im1').width(), 201)
        im = QImage(202, 202, QImage.Format_RGB32)
        cache.setCacheImage('im1', im, [])
        self.assertEqual(cache.cacheImage('im1').width(), 202)
Beispiel #2
0
def createTemporaryRenderContext():

    layerModel = iface.layerTreeView().layerTreeModel()
    mupp, dpi, scale = layerModel.legendMapViewData()

    if qgsDoubleNear(mupp, 0.0) or dpi == 0 or qgsDoubleNear(scale, 0.0):
        return None

    render_context = QgsRenderContext()
    render_context.setScaleFactor(dpi / 25.4)
    render_context.setRendererScale(scale)
    render_context.setMapToPixel(QgsMapToPixel(mupp))
    return render_context
Beispiel #3
0
def qgis_render_context(painter, width, height):
    mtp = QgsMapToPixel()
    # the default viewport if centered on 0, 0
    mtp.setParameters( 1,        # map units per pixel
                       width/2,  # map center in geographical units
                       height/2, # map center in geographical units
                       width,    # output width in pixels
                       height,   # output height in pixels
                       0.0       # rotation in degrees
    )
    context = QgsRenderContext()
    context.setMapToPixel(mtp)
    context.setPainter(painter)
    return context
Beispiel #4
0
    def testPainterClipPath(self):
        region = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
        region.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.ClipPainterOnly)
        region2 = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
        region2.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.NoClipping)
        region3 = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
        region3.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.ClipPainterOnly)

        rc = QgsRenderContext()

        for t in [QgsMapLayerType.VectorLayer, QgsMapLayerType.RasterLayer, QgsMapLayerType.MeshLayer, QgsMapLayerType.VectorTileLayer]:
            path, should_clip = QgsMapClippingUtils.calculatePainterClipRegion([], rc, t)
            self.assertFalse(should_clip)
            self.assertEqual(path.elementCount(), 0)

        for t in [QgsMapLayerType.VectorLayer, QgsMapLayerType.RasterLayer, QgsMapLayerType.MeshLayer, QgsMapLayerType.VectorTileLayer]:
            path, should_clip = QgsMapClippingUtils.calculatePainterClipRegion([region], rc, t)
            self.assertTrue(should_clip)
            self.assertEqual(QgsGeometry.fromQPolygonF(path.toFillPolygon()).asWkt(1), 'Polygon ((0 1, 1 1, 1 0, 0 0, 0 1))')

        # region2 is a Intersects type clipping region, should not apply for vector layers
        path, should_clip = QgsMapClippingUtils.calculatePainterClipRegion([region2], rc, QgsMapLayerType.VectorLayer)
        self.assertFalse(should_clip)
        self.assertEqual(path.elementCount(), 0)

        for t in [QgsMapLayerType.RasterLayer, QgsMapLayerType.MeshLayer, QgsMapLayerType.VectorTileLayer]:
            path, should_clip = QgsMapClippingUtils.calculatePainterClipRegion([region2], rc, t)
            self.assertTrue(should_clip)
            self.assertEqual(QgsGeometry.fromQPolygonF(path.toFillPolygon()).asWkt(1), 'Polygon ((0 1, 0.1 1, 0.1 -1, 0 -1, 0 1))')

        for t in [QgsMapLayerType.VectorLayer, QgsMapLayerType.RasterLayer, QgsMapLayerType.MeshLayer, QgsMapLayerType.VectorTileLayer]:
            path, should_clip = QgsMapClippingUtils.calculatePainterClipRegion([region, region2, region3], rc, t)
            self.assertTrue(should_clip)
            geom = QgsGeometry.fromQPolygonF(path.toFillPolygon())
            geom.normalize()
            self.assertEqual(geom.asWkt(1), 'Polygon ((0 0, 0 1, 0.1 1, 0.1 0, 0 0))')

        rc.setMapToPixel(QgsMapToPixel(5, 10, 11, 200, 150, 0))
        for t in [QgsMapLayerType.VectorLayer, QgsMapLayerType.RasterLayer, QgsMapLayerType.MeshLayer, QgsMapLayerType.VectorTileLayer]:
            path, should_clip = QgsMapClippingUtils.calculatePainterClipRegion([region, region3], rc, t)
            self.assertTrue(should_clip)
            self.assertEqual(QgsGeometry.fromQPolygonF(path.toFillPolygon()).asWkt(0), 'Polygon ((98 77, 98 77, 98 77, 98 77, 98 77))')