Skip to content
Snippets Groups Projects
mapcanvas.py 40.8 KiB
Newer Older
  • Learn to ignore specific revisions
  •     def spatialCenter(self)->SpatialPoint:
            """
            Returns the map center as SpatialPoint (QgsPointXY + CRS)
            :return: SpatialPoint
            """
            return SpatialPoint.fromMapCanvasCenter(self)
    
        def spatialExtentHint(self)->SpatialExtent:
            """
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
            Returns a hint for a SpatialExtent, derived from the first raster layer
            :return: SpatialExtent
    
            crs = self.crs()
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
    
            layers = self.layers()
            if len(layers) > 0:
                e = self.fullExtent()
                ext = SpatialExtent(crs, e)
            else:
                ext = SpatialExtent.world()
    
    
    
    class CanvasBoundingBoxItem(QgsGeometryRubberBand):
    
        def __init__(self, mapCanvas):
            assert isinstance(mapCanvas, QgsMapCanvas)
            super(CanvasBoundingBoxItem, self).__init__(mapCanvas)
    
            self.canvas = mapCanvas
            self.mCanvasExtents = dict()
            self.mShow = True
            self.mShowTitles = True
            self.setIconType(QgsGeometryRubberBand.ICON_NONE)
    
        def connectCanvas(self, canvas):
            assert isinstance(canvas, QgsMapCanvas)
            assert canvas != self.canvas
            if canvas not in self.mCanvasExtents.keys():
                self.mCanvasExtents[canvas] = None
                canvas.extentsChanged.connect(lambda : self.onExtentsChanged(canvas))
                canvas.destroyed.connect(lambda : self.disconnectCanvas(canvas))
                self.onExtentsChanged(canvas)
    
        def disconnectCanvas(self, canvas):
                self.mCanvasExtents.pop(canvas)
    
        def onExtentsChanged(self, canvas):
            assert isinstance(canvas, QgsMapCanvas)
    
            ext = SpatialExtent.fromMapCanvas(canvas)
            ext = ext.toCrs(self.canvas.mapSettings().destinationCrs())
    
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
            geom = QgsPolygon()
    
            assert geom.fromWkt(ext.asWktPolygon())
    
            self.mCanvasExtents[canvas] = (ext, geom)
            self.refreshExtents()
    
        def refreshExtents(self):
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
            multi = QgsPolygon()
    
            if self.mShow:
                for canvas, t in self.mCanvasExtents.items():
                    ext, geom = t
                    multi.addGeometry(geom.clone())
            self.setGeometry(multi)
    
        def paint(self, painter, QStyleOptionGraphicsItem=None, QWidget_widget=None):
            super(CanvasBoundingBoxItem, self).paint(painter)
    
            if self.mShowTitles and self.mShow:
                painter.setPen(Qt.blue);
                painter.setFont(QFont("Arial", 30))
    
                for canvas, t in self.mCanvasExtents.items():
                    ext, geom = t
                    ULpx = self.toCanvasCoordinates(ext.center())
                    txt = canvas.windowTitle()
                    painter.drawLine(0, 0, 200, 200);
                    painter.drawText(ULpx,  txt)
    
    
        def setShow(self, b):
            assert isinstance(b, bool)
            self.mShow = b
    
        def setShowTitles(self, b):
            assert isinstance(b, bool)
            self.mShowTitles = b