When it comes to 'Add polygon feature', if it is possible to make a polygon square so that user can input length(s) of the sides, then how do I do that?
4 Answers
Here's a way to do it without any digitizing or coding. I used 3.18.1:
- Start with a point layer. Below, I have a layer containing three points, located within Washington state, USA. The CRS is EPSG:2927.
- Buffer the points by one-half of the desired square side length. In my example, I want my final square polygons to be 40 miles on a side, so I set the buffer width to 20 miles.
- Run the tool Processing > Toolbox > Vector General > Bounding Boxes, where the input layer is the buffer polygon from step 2:
Voila! The resulting squares have the desired 40-mile dimension. If desired, the squares can be rotated with the Processing > Toolbox > Vector Geometry > Rotate tool.
Check "Rectangles, Ovals, Diamonds (Variable)" geoalgorithm in the QGIS's Toolbox, where user need to put equal width and height for a square, see image below
Mind that a point layer has to be created in advance. Those points will, later on, serve as square's geocentroid.
-
1
There are at least two options:
- Use
Advanced Digitizing Toolbar
, see documentation, so I will not insist on this. - Use QGIS expressions - what the following explanations will be about.
Use QGIS expressions to either create the square dynamically, but only for visualization purpose using Geometry generator
or create actual geometries with Menu Processing / Toolbox / Geometry by expression
. In both cases, you can use the same expression (see below).
Drawing a line will automatically add a square in the pre-defined size. See animation how to do it: Draw line / add symbol layer: Geometry generator: polygon / paste expression / change size:
The solution consists of two steps:
Create a line layer and draw a line. Click once for the first point: this will be the starting edge of your square. Click a second time to create the line: it's direction (not it's length) will define the orientation (azimuth, angle, rotation) of your square. You're done with the line, toggle edit mode.
Now use the following expression with
geometry generator
orgeometry by espression
as stated before: it will create a square on the right side of your line, starting on the first point you clicked, using the line for one side of the square and with a length that you can define in third line of the expression - in my case: 5000. Change this accordingly:
with_variable (
'side',
5000,
make_square(
project (
project (
start_point ($geometry),
@side,
azimuth(
start_point ($geometry),
end_point($geometry)
)
)
,
@side,
azimuth (
end_point($geometry),
start_point ($geometry)
)-radians (90))
,
start_point ($geometry)
)
)
Screenshot: see the version with geometry generator: the red dot is the starting point of the black line. The orange polygon is the square created by the expression. Note that the length of the line does not play any role, it's just for the rotation: