Exemplo n.º 1
0
    def _create_pixmap_from_symbol(self, symbol, scale_factor, padding):
        context = QgsRenderContext()
        context.setScaleFactor(scale_factor)

        pixmap = QgsSymbolLayerUtils.symbolPreviewPixmap(
            symbol, QSize(142, 71), padding, context)
        return pixmap
Exemplo n.º 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
Exemplo n.º 3
0
    def testConvertToPainterUnitsNoMapToPixel(self):
        """
        Test converting map unit based sizes to painter units when render context has NO map to pixel set
        """
        r = QgsRenderContext()
        r.setScaleFactor(300 / 25.4)  # 300 dpi, to match above test

        # start with no min/max scale
        c = QgsMapUnitScale()

        # since we have no map scale to work with, this makes the gross assumption that map units == points. It's magic, but
        # what else can we do?
        size = r.convertToPainterUnits(10, QgsUnitTypes.RenderMapUnits, c)
        self.assertAlmostEqual(size, 41.66666, places=3)
        size = r.convertToPainterUnits(10, QgsUnitTypes.RenderMetersInMapUnits,
                                       c)
        self.assertAlmostEqual(size, 41.66666, places=3)

        # sizes should be clamped to reasonable range -- we don't want to treat 2000m map unit sizes as 10 million pixels!
        size = r.convertToPainterUnits(2000, QgsUnitTypes.RenderMapUnits, c)
        self.assertEqual(size, 100.0)
        size = r.convertToPainterUnits(2000,
                                       QgsUnitTypes.RenderMetersInMapUnits, c)
        self.assertEqual(size, 100.0)
        size = r.convertToPainterUnits(0.0002, QgsUnitTypes.RenderMapUnits, c)
        self.assertEqual(size, 10.0)
        size = r.convertToPainterUnits(0.0002,
                                       QgsUnitTypes.RenderMetersInMapUnits, c)
        self.assertEqual(size, 10.0)

        # normal units, should not be affected
        size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMillimeters, c)
        self.assertAlmostEqual(size, 23.622047, places=5)
        size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPoints, c)
        self.assertAlmostEqual(size, 8.33333333125, places=5)
        size = r.convertToPainterUnits(2, QgsUnitTypes.RenderInches, c)
        self.assertAlmostEqual(size, 600.0, places=5)
        size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPixels, c)
        self.assertAlmostEqual(size, 2.0, places=5)

        # minimum size greater than the calculated size, so size should be limited to minSizeMM
        c.minSizeMM = 5
        c.minSizeMMEnabled = True
        size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMapUnits, c)
        self.assertAlmostEqual(size, 59.0551181, places=5)

        # maximum size less than the calculated size, so size should be limited to maxSizeMM
        c.maxSizeMM = 6
        c.maxSizeMMEnabled = True
        size = r.convertToPainterUnits(26, QgsUnitTypes.RenderMapUnits, c)
        self.assertAlmostEqual(size, 70.866, places=2)
Exemplo n.º 4
0
    def identify(self, params):
        self.check_required_params(params)

        feature_collections = []

        with change_directory(self.project_root):

            crs = QgsCoordinateReferenceSystem()
            crs.createFromSrid(params.get('srs'))

            search_box = self._calcSearchBox(
                params.get('bbox'), params.get('image_size')[0],
                params.get('image_size')[1],
                params.get('click_point')[0], params.get('click_point')[1]
            )

            # initialize mapRenderer and a rendering context in order to be
            # to check if a feature will actually be rendered
            # we don't want to return features that are not visible
            img = QImage(QSize(
                settings.SUNLUMO_GFI_BUFFER*2, settings.SUNLUMO_GFI_BUFFER*2),
                QImage.Format_ARGB32_Premultiplied
            )
            dpm = 1 / 0.00028
            img.setDotsPerMeterX(dpm)
            img.setDotsPerMeterY(dpm)

            mapRenderer = QgsMapRenderer()
            mapRenderer.clearLayerCoordinateTransforms()
            mapRenderer.setOutputSize(QSize(
                settings.SUNLUMO_GFI_BUFFER*2, settings.SUNLUMO_GFI_BUFFER*2),
                img.logicalDpiX()
            )

            mapRenderer.setDestinationCrs(crs)
            mapRenderer.setProjectionsEnabled(True)
            mapUnits = crs.mapUnits()
            mapRenderer.setMapUnits(mapUnits)

            mapExtent = QgsRectangle(*search_box)
            mapRenderer.setExtent(mapExtent)

            renderContext = QgsRenderContext()
            renderContext.setExtent(mapRenderer.extent())
            renderContext.setRasterScaleFactor(1.0)
            renderContext.setMapToPixel(mapRenderer.coordinateTransform())
            renderContext.setRendererScale(mapRenderer.scale())
            renderContext.setScaleFactor(mapRenderer.outputDpi() / 25.4)
            renderContext.setPainter(None)

            qfr = QgsFeatureRequest()
            search_rectangle = QgsRectangle(*search_box)
            qfr.setFilterRect(search_rectangle)

            for q_layer in params.get('query_layers'):
                layer = self.layerRegistry.mapLayer(q_layer)

                if layer.type() == QgsMapLayer.RasterLayer:
                    # skip raster layer processing
                    continue

                # update layer fields (expressions, calculated, joined)
                layer.updateFields()

                scaleCalc = QgsScaleCalculator(
                    (img.logicalDpiX() + img.logicalDpiY()) / 2,
                    mapRenderer.destinationCrs().mapUnits()
                )
                scaleDenom = scaleCalc.calculate(mapExtent, img.width())

                # skip the layer if it's not visible at the current map scale
                if layer.hasScaleBasedVisibility():
                    if not(layer.minimumScale()
                            < scaleDenom < layer.maximumScale()):
                        continue

                # check if features actually intersect search rectangle
                intersected_features = self._intersectedFeatures(
                    layer.getFeatures(qfr), search_rectangle
                )
                # visible features generator
                visible_features = self._visibleFeatures(
                    layer, renderContext, intersected_features
                )
                layer_features = [featureToGeoJSON(
                    feature.id(), feature.geometry(),
                    self._collectAttributes(layer, feature)
                ) for feature in visible_features
                ]

                feature_collections.append(layer_features)

            return writeGeoJSON(chain(*feature_collections))