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

Regina Obe lr at pcorp.us
Mon Sep 11 01:09:33 PDT 2023


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

 

 

From: postgis-users <postgis-users-bounces at lists.osgeo.org> On Behalf Of Shaozhong SHI
Sent: Monday, September 11, 2023 3:36 AM
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org>
Subject: Re: [postgis-users] generate a geometry column of random point, line and polygon

 

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

 

On Mon, 11 Sept 2023 at 08:31, Regina Obe <lr at pcorp.us <mailto:lr at pcorp.us> > wrote:

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;

 

From: postgis-users <postgis-users-bounces at lists.osgeo.org <mailto:postgis-users-bounces at lists.osgeo.org> > On Behalf Of Shaozhong SHI
Sent: Monday, September 11, 2023 3:21 AM
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org <mailto:postgis-users at lists.osgeo.org> >
Subject: Re: [postgis-users] generate a geometry column of random point, line and polygon

 

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.

ST_OptimalAlphaShape (postgis.net) <https://postgis.net/docs/en/ST_OptimalAlphaShape.html> 

 

Regards,

 

David

 

On Fri, 8 Sept 2023 at 16:16, Regina Obe <lr at pcorp.us <mailto:lr at pcorp.us> > wrote:

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 <http://p.id> , dp.path[1], dp.geom 

FROM p, ST_DumpPoints(ST_GeneratePoints(p.geom, 100)) AS dp;

 

The p.id <http://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 <http://p.id> , dp.path[1], dp.geom 

FROM p, ST_DumpPoints(ST_GeneratePoints(p.geom, 100)) AS dp;

 

 

From: postgis-users <postgis-users-bounces at lists.osgeo.org <mailto:postgis-users-bounces at lists.osgeo.org> > On Behalf Of Shaozhong SHI
Sent: Friday, September 8, 2023 10:54 AM
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org <mailto:postgis-users at lists.osgeo.org> >
Subject: Re: [postgis-users] generate a geometry column of random point, line and polygon

 

The simplicity is beautiful.  Any simple script to do so?  I think that will be an useful addition.

Regards,

David

On Friday, 8 September 2023, Regina Obe <lr at pcorp.us <mailto:lr at pcorp.us> > wrote:

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

 

From: postgis-users <postgis-users-bounces at lists.osgeo.org <mailto:postgis-users-bounces at lists.osgeo.org> > On Behalf Of Shaozhong SHI
Sent: Friday, September 8, 2023 7:56 AM
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org <mailto:postgis-users at lists.osgeo.org> >
Subject: [postgis-users] generate a geometry column of random point, line and polygon

 

Is a simple way to do this?

 

Regards,

 

David

_______________________________________________
postgis-users mailing list
postgis-users at lists.osgeo.org <mailto:postgis-users at lists.osgeo.org> 
https://lists.osgeo.org/mailman/listinfo/postgis-users

_______________________________________________
postgis-users mailing list
postgis-users at lists.osgeo.org <mailto:postgis-users at lists.osgeo.org> 
https://lists.osgeo.org/mailman/listinfo/postgis-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20230911/663b8f57/attachment.htm>


More information about the postgis-users mailing list