Index

Draw a square from a center point in QGIS

Run a Python script in the QGIS console to draw a fixed-size square polygon, either from hardcoded coordinates or by clicking directly on the map.

Context

These scripts create a square polygon of a given size (in metres) around a centre point, add it as a temporary memory layer, and load it into the current QGIS project.

Run them from the QGIS Python Console: Plugins → Python Console.

Version 1 — Hardcoded coordinates

Simple version with coordinates written directly in the script.

from qgis.core import QgsGeometry, QgsPointXY, QgsFeature, QgsVectorLayer, QgsProject

# Parameters
cx, cy = -2069881.1, -7.7  # centre coordinates (in the layer's CRS units)
taille = 100                # square side length in metres
d = taille / 2

# Build the square
coins = [
    QgsPointXY(cx - d, cy - d),
    QgsPointXY(cx + d, cy - d),
    QgsPointXY(cx + d, cy + d),
    QgsPointXY(cx - d, cy + d),
    QgsPointXY(cx - d, cy - d),  # close the ring
]
geom = QgsGeometry.fromPolygonXY([coins])

# Create a temporary memory layer
layer = QgsVectorLayer("Polygon?crs=EPSG:25830", "carré_100m", "memory")

# Add the square to the layer
provider = layer.dataProvider()
feat = QgsFeature()
feat.setGeometry(geom)
provider.addFeatures([feat])
layer.updateExtents()

# Add the layer to the project
QgsProject.instance().addMapLayer(layer)
Info

Change cx, cy to your centre coordinates in the CRS of your project, and EPSG:25830 to match your project's CRS if needed.

Version 2 — Click on the map to place the centre

This version activates a map tool that waits for a click on the canvas. The square is drawn at the clicked location, then the previous tool is restored automatically.

from qgis.core import QgsGeometry, QgsPointXY, QgsFeature, QgsVectorLayer, QgsProject
from qgis.gui import QgsMapToolEmitPoint

TAILLE = 100  # square side length in metres

def draw_square(point, button):
    cx, cy = point.x(), point.y()
    d = TAILLE / 2

    coins = [
        QgsPointXY(cx - d, cy - d),
        QgsPointXY(cx + d, cy - d),
        QgsPointXY(cx + d, cy + d),
        QgsPointXY(cx - d, cy + d),
        QgsPointXY(cx - d, cy - d),
    ]
    geom = QgsGeometry.fromPolygonXY([coins])

    layer = QgsVectorLayer("Polygon?crs=EPSG:25830", "carré_100m", "memory")
    provider = layer.dataProvider()
    feat = QgsFeature()
    feat.setGeometry(geom)
    provider.addFeatures([feat])
    layer.updateExtents()
    QgsProject.instance().addMapLayer(layer)

    # Restore the default pan tool after placing the square
    iface.mapCanvas().unsetMapTool(tool)
    print(f"Square drawn at ({cx:.2f}, {cy:.2f})")

canvas = iface.mapCanvas()
tool = QgsMapToolEmitPoint(canvas)
tool.canvasClicked.connect(draw_square)
canvas.setMapTool(tool)

print("Click on the map to place the square centre...")
Tip

The script stays active until you click once. If you want to place multiple squares, remove the unsetMapTool line.

Notes

taille, ok = QInputDialog.getInt(None, "Square size", "Side length (metres):", 100, 1, 10000)
if ok:
    TAILLE = taille

Last updated: 18-05-2026 at 17:51