Here is a set returning function ST_CreateFishnet
that creates a 2D grid of polygon geometries:
CREATE OR REPLACE FUNCTION ST_CreateFishnet(
nrow integer, ncol integer,
xsize float8, ysize float8,
x0 float8 DEFAULT 0, y0 float8 DEFAULT 0,
OUT "row" integer, OUT col integer,
OUT geom geometry)
RETURNS SETOF record AS
$$
SELECT i + 1 AS row, j + 1 AS col, ST_Translate(cell, j * $3 + $5, i * $4 + $6) AS geom
FROM generate_series(0, $1 - 1) AS i,
generate_series(0, $2 - 1) AS j,
(
SELECT ('POLYGON((0 0, 0 '||$4||', '||$3||' '||$4||', '||$3||' 0,0 0))')::geometry AS cell
) AS foo;
$$ LANGUAGE sql IMMUTABLE STRICT;
where nrow
and ncol
are the number of rows and columns, xsize
and ysize
are the lengths of the cell size, and optional x0
and y0
are coordinates for the bottom-left corner.
The result is row
and col
numbers, starting from 1 at the bottom-left corner, and geom
rectangular polygons for each cell. So for example:
SELECT *
FROM ST_CreateFishnet(4, 6, 10, 10) AS cells;
row | col | geom
-----+-----+--------------------------------
1 | 1 | 0103000000010000000500000000...
2 | 1 | 0103000000010000000500000000...
3 | 1 | 0103000000010000000500000000...
4 | 1 | 0103000000010000000500000000...
1 | 2 | 0103000000010000000500000000...
2 | 2 | 0103000000010000000500000000...
...
3 | 6 | 0103000000010000000500000000...
4 | 6 | 0103000000010000000500000000...
(24 rows)
Or to make a single geometry collection for the full grid:
SELECT ST_Collect(cells.geom)
FROM ST_CreateFishnet(4, 6, 10, 10) AS cells;
You can add the x0
/ y0
origin offsets (these defaulted to zero).
What QGIS is referring to in terms of the layer boundary is actually the envelope or bounding box of the geometry.
I've documented a solution which is probably the closest you will get to automating this is (short of filing a bug / feature request).
The process works based on the ratio between area of the two objects: the feature geometry and it's bounding box).
Check the following steps below:
Select the layer you want to work on.
Select the feature you wish to work on. Make sure nothing else is selected.
Run the following snippet within the Python Console.
layer = qgis.utils.iface.activeLayer()
features = layer.selectedFeatures()
feature = features[0]
geom = feature.geometry()
env_rect = geom.boundingBox()
env_geom = QgsGeometry.fromRect(env_rect)
env_geom.area() / geom.area()
Be sure to push enter as the last line will not automatically return.
Run the Regular Points
tool again, but enter the number of points that you want inside the box multiplied by the output given by your script.
In my sample below, the result I received from the output was a value of ~2. I requested 20 dots to be output by the process where I actually only wanted 10. I was given 8 in return, which is fairly close. Depending on the actual shape you're using the results will vary.
If you were so inclined, you could automate (recursively) the Regular Points process modifying the variables until you arrived at your desired number.
Best Answer
Create the grid layer and then clip it with the polygon? Vector -> Geoprocessing Tools -> Clip. It would probably be best to create your grid with lines, rather than polygons. N.