[postgis-users] generate a geometry column of random point, line and polygon

45 views
Skip to first unread message

Shaozhong SHI

unread,
Sep 8, 2023, 7:56:13 AM9/8/23
to PostGIS Users Discussion
Is a simple way to do this?

Regards,

David

Regina Obe

unread,
Sep 8, 2023, 10:19:43 AM9/8/23
to PostGIS Users Discussion

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

Shaozhong SHI

unread,
Sep 8, 2023, 10:54:34 AM9/8/23
to PostGIS Users Discussion
The simplicity is beautiful.  Any simple script to do so?  I think that will be an useful addition.
Regards,
David

Regina Obe

unread,
Sep 8, 2023, 11:16:00 AM9/8/23
to PostGIS Users Discussion

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;

Shaozhong SHI

unread,
Sep 11, 2023, 3:21:03 AM9/11/23
to PostGIS Users Discussion
I tested St_optimalphashape and it does not work.

select gs, st_optimalalphashape(st_collect(ST_MakePoint(random()*10,random()*10),ST_MakePoint(random()*10,random()*10))::geometry)
from generate_series(1,100) gs;

Example given here does not work either.

Regards,

David

_______________________________________________
postgis-users mailing list
postgi...@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/postgis-users

Regina Obe

unread,
Sep 11, 2023, 3:30:48 AM9/11/23
to PostGIS Users Discussion

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;

Shaozhong SHI

unread,
Sep 11, 2023, 3:36:10 AM9/11/23
to PostGIS Users Discussion
Hi, Regina,

It did not work.
ERROR: function st_optimalalphashape(geometry) does not exist LINE 1: SELECT min(gs) , max(gs), st_optimalalphashape(st_collect(S... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SQL state: 42883 Character: 28

Regina Obe

unread,
Sep 11, 2023, 4:09:35 AM9/11/23
to PostGIS Users Discussion

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

 

https://postgis.net/docs/ST_ConcaveHull.html

Imre Samu

unread,
Sep 12, 2023, 8:17:38 PM9/12/23
to PostGIS Users Discussion
related: 
Random Geometry Generation with PostGIS ( by Paul Ramsey, Sep 11, 2023 )
https://www.crunchydata.com/blog/random-geometry-generation-with-postgis

Maybe we could add a few new "ST_generate*" functions to PostGIS:
- ST_GenerateLine(n_vertices INTEGER, bounding_geometry GEOMETRY)
- ST_GeneratePolygons(n_vertices INTEGER, bounding_geometry GEOMETRY)

Imre
Reply all
Reply to author
Forward
0 new messages