David,
For this are you needing to generate random points from a line or polygon or are you trying to generate random polygons, lines, and points?
If you need to generate random points from a polygon:
Use ST_GeneratePoints: https://postgis.net/docs/en/ST_GeneratePoints.html
Note there is a option seed argument, that will give you the same exact answer if you give it the same seed, but without that the generated points will be different each time.
As I recall, ST_GeneratePoints only works with areals so won’t work with a line, however you can buffer a line very thinly to do the same. Use a flat buffer:
https://postgis.net/docs/en/ST_Buffer.html
SELECT ST_GeneratePoints(ST_Buffer(
ST_GeomFromText(
'LINESTRING(50 50,150 150,150 50)'
), 0.5, 'endcap=square join=round'), 1000);
If you want to generate random polygons, you could use ST_ConcaveHull or ST_AlphaShape around the section of a polygon you did a ST_GeneratePoints on
And then use something like https://postgis.net/docs/en/ST_Subdivide.html to chop up the polygons.
To get a linestring out of that (It will be closed), you can take the boundary of any of the above
https://postgis.net/docs/ST_Boundary.html
Hope that helps,
Regina
David,
The example query in the docs, is as simple as it gets. It’s a self-contained example you can just run, but it does return a multipoint and I realize now the docs don’t make it clear ST_GeneratePoints returns a single geometry that is a multipoint.
IF you want individual points, you’d combine with ST_DumpPoints.
I’ll add such an example to the docs.
So here is an example you can apply to a table of polygons
For polygons you can do something like below where p is the table name and geom is the polygon column.
This will generate 100 random points for each polygon
SELECT p.id, dp.path[1], dp.geom
FROM p, ST_DumpPoints(ST_GeneratePoints(p.geom, 100)) AS dp;
The p.id and path I just threw in cause I find them useful, but you could leave them out.
Here is a self-contained using the example table in docs:
WITH p AS ( SELECT 1 AS id, ST_Buffer(
ST_GeomFromText(
'LINESTRING(50 50,150 150,150 50)'),
10, 'endcap=round join=round') AS geom)
SELECT p.id, dp.path[1], dp.geom
FROM p, ST_DumpPoints(ST_GeneratePoints(p.geom, 100)) AS dp;
_______________________________________________
postgis-users mailing list
postgi...@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/postgis-users
You are using the wrong version of ST_Collect. You want to use the aggregate form. You are using the two point form - https://postgis.net/docs/en/ST_Collect.html
Also when you aggregate, you can’t include the gs in there.
Try:
SELECT min(gs) , max(gs), st_optimalalphashape(st_collect(ST_MakePoint(random()*10,random()*10)) )
from generate_series(1,100) gs;
You need to do
CREATE EXTENSION postgis_sfcgal;
It’s not part of the postgis extension. If you don’t have that extension, then you can’t use this function.
You next best bet is using ST_ConcaveHull