PostGISGeometry Processing

ST_GeneratePoints

What is ST_GeneratePoints?

ST_GeneratePoints is a PostGIS function that creates a MultiPoint of pseudo-random points uniformly distributed inside the input polygon. Given a polygon and a count, it returns n points whose locations are statistically uniform over the polygon's area.

SQL
1ST_GeneratePoints(geometry area, integer npoints)geometry
2ST_GeneratePoints(geometry area, integer npoints, integer seed)geometry

Supplying a seed makes the output reproducible — crucial for regression tests, shared notebooks, and deterministic pipelines.

When would you use ST_GeneratePoints?

Use ST_GeneratePoints to create test data, simulate population samples within administrative boundaries, generate seed points for Monte Carlo analyses, or synthesise realistic point clouds for demos. It is also handy for redistributing an aggregate count (e.g. census totals) as points for dot-density maps.

SQL
1SELECT district_id, ST_GeneratePoints(geom, 500, 42) AS sample_points
2FROM census_districts;

FAQs

Are the points truly uniform?

Yes — ST_GeneratePoints uses rejection sampling against the polygon's bounding box, so the resulting density is uniform with respect to the polygon's Cartesian area. For unprojected lat/lon inputs the uniformity is in degree-space, not on the sphere, which can distort density at high latitudes.

How do I make results reproducible?

Pass an integer seed. The same (area, npoints, seed) triple produces identical output, so tests and dashboards stay stable across runs. Without a seed, every call produces new points.

Does it work with MultiPolygons?

Yes. Points are distributed across all constituent polygons proportional to their areas — a MultiPolygon with two parts of sizes 1 and 3 will see roughly 25% of points in the first and 75% in the second.

Why is it slow on highly concave polygons?

The algorithm samples inside the bounding box and rejects points that fall outside the polygon. For long thin or very concave shapes, the hit rate drops and sampling takes longer. Break extremely sparse polygons into smaller parts (e.g. with a grid) before generating, or use a lower point count.