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.
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.
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)
Change cx, cy to your centre coordinates in the CRS of your project, and EPSG:25830 to match your project's CRS if needed.
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...")
The script stays active until you click once. If you want to place multiple squares, remove the unsetMapTool line.
EPSG:25830 (UTM zone 30N). Change this to match your project's CRS — coordinates must be in that CRS's units (metres for projected, degrees for geographic).Export → Save Features As.taille, ok = QInputDialog.getInt(None, "Square size", "Side length (metres):", 100, 1, 10000)
if ok:
TAILLE = tailleLast updated: 18-05-2026 at 17:51