def wkt_to_rectangle(extent):
    """Compute the rectangle from a WKT string.

    It returns None if the extent is not valid WKT rectangle.

    :param extent: The extent.
    :type extent: basestring

    :return: The rectangle or None if it is not a valid WKT rectangle.
    :rtype: QgsRectangle
    """
    geometry = QgsGeometry.fromWkt(extent)
    if not geometry.isGeosValid():
        return None

    polygon = geometry.asPolygon()[0]

    if len(polygon) != 5:
        return None

    if polygon[0] != polygon[4]:
        return None

    rectangle = QgsRectangle(
        QgsPointXY(polygon[0].x(), polygon[0].y()),
        QgsPointXY(polygon[2].x(), polygon[2].y()))

    return rectangle
def wkt_to_rectangle ( extent ) : geometry = QgsGeometry . fromWkt ( extent ) if not geometry . isGeosValid ( ) : return None polygon = geometry . asPolygon ( ) [ 0 ] if len ( polygon ) != 5 : return None if polygon [ 0 ] != polygon [ 4 ] : return None rectangle = QgsRectangle ( QgsPointXY ( polygon [ 0 ] . x ( ) , polygon [ 0 ] . y ( ) ) , QgsPointXY ( polygon [ 2 ] . x ( ) , polygon [ 2 ] . y ( ) ) ) return rectangle
Compute the rectangle from a WKT string.

    It returns None if the extent is not valid WKT rectangle.

    :param extent: The extent.
    :type extent: basestring

    :return: The rectangle or None if it is not a valid WKT rectangle.
    :rtype: QgsRectangle