<div dir="ltr"><div>related: </div><div>Random Geometry Generation with PostGIS ( by Paul Ramsey, Sep 11, 2023 )<br></div><a href="https://www.crunchydata.com/blog/random-geometry-generation-with-postgis">https://www.crunchydata.com/blog/random-geometry-generation-with-postgis</a><div><br></div><div>Maybe we could add a few new "ST_generate*" functions to PostGIS:<br><div>- ST_GenerateLine(n_vertices INTEGER, bounding_geometry GEOMETRY)<br></div><div>- ST_GeneratePolygons(n_vertices INTEGER, bounding_geometry GEOMETRY)<br></div><div><br></div></div><div>Imre</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Regina Obe <<a href="mailto:lr@pcorp.us">lr@pcorp.us</a>> ezt írta (időpont: 2023. szept. 11., H, 10:09):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-4856370385792983340"><div lang="EN-US" style="overflow-wrap: break-word;"><div class="m_-4856370385792983340WordSection1"><p class="MsoNormal">You need to do <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">CREATE EXTENSION postgis_sfcgal;<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">It’s not part of the postgis extension. If you don’t have that extension, then you can’t use this function.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">You next best bet is using ST_ConcaveHull<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><a href="https://postgis.net/docs/ST_ConcaveHull.html" target="_blank">https://postgis.net/docs/ST_ConcaveHull.html</a><u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p><div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt"><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in"><p class="MsoNormal"><b>From:</b> postgis-users <<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>> <b>On Behalf Of </b>Shaozhong SHI<br><b>Sent:</b> Monday, September 11, 2023 3:36 AM<br><b>To:</b> PostGIS Users Discussion <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br><b>Subject:</b> Re: [postgis-users] generate a geometry column of random point, line and polygon<u></u><u></u></p></div></div><p class="MsoNormal"><u></u> <u></u></p><div><div><p class="MsoNormal">Hi, Regina,<u></u><u></u></p><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal">It did not work.<u></u><u></u></p></div><div><p class="MsoNormal"><span style="font-size:9.5pt;font-family:"Source Code Pro"">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</span><u></u><u></u></p></div></div></div><p class="MsoNormal"><u></u> <u></u></p><div><div><p class="MsoNormal">On Mon, 11 Sept 2023 at 08:31, Regina Obe <<a href="mailto:lr@pcorp.us" target="_blank">lr@pcorp.us</a>> wrote:<u></u><u></u></p></div><blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in"><div><div><div><p class="MsoNormal">You are using the wrong version of ST_Collect. You want to use the aggregate form. You are using the two point form - <a href="https://postgis.net/docs/en/ST_Collect.html" target="_blank">https://postgis.net/docs/en/ST_Collect.html</a><u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">Also when you aggregate, you can’t include the gs in there.<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">Try: <u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">SELECT min(gs) , max(gs), st_optimalalphashape(st_collect(ST_MakePoint(random()*10,random()*10)) )<u></u><u></u></p><p class="MsoNormal">from generate_series(1,100) gs;<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt"><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in"><p class="MsoNormal"><b>From:</b> postgis-users <<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>> <b>On Behalf Of </b>Shaozhong SHI<br><b>Sent:</b> Monday, September 11, 2023 3:21 AM<br><b>To:</b> PostGIS Users Discussion <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br><b>Subject:</b> Re: [postgis-users] generate a geometry column of random point, line and polygon<u></u><u></u></p></div></div><p class="MsoNormal"> <u></u><u></u></p><div><div><div><div><p class="MsoNormal">I tested St_optimalphashape and it does not work.<u></u><u></u></p></div><div><p class="MsoNormal"> <u></u><u></u></p></div><div><p class="MsoNormal">select gs, st_optimalalphashape(st_collect(ST_MakePoint(random()*10,random()*10),ST_MakePoint(random()*10,random()*10))::geometry)<u></u><u></u></p></div><div><p class="MsoNormal">from generate_series(1,100) gs;<u></u><u></u></p></div><div><p class="MsoNormal"> <u></u><u></u></p></div><div><p class="MsoNormal">Example given here does not work either.<u></u><u></u></p></div><div><p class="MsoNormal"><a href="https://postgis.net/docs/en/ST_OptimalAlphaShape.html" target="_blank">ST_OptimalAlphaShape (postgis.net)</a><u></u><u></u></p></div><div><p class="MsoNormal"> <u></u><u></u></p></div><div><p class="MsoNormal">Regards,<u></u><u></u></p></div><div><p class="MsoNormal"> <u></u><u></u></p></div><div><p class="MsoNormal">David<u></u><u></u></p></div></div></div></div><p class="MsoNormal"> <u></u><u></u></p><div><div><p class="MsoNormal">On Fri, 8 Sept 2023 at 16:16, Regina Obe <<a href="mailto:lr@pcorp.us" target="_blank">lr@pcorp.us</a>> wrote:<u></u><u></u></p></div><blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt"><div><div><div><p class="MsoNormal">David,<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">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.<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">IF you want individual points, you’d combine with ST_DumpPoints.<u></u><u></u></p><p class="MsoNormal">I’ll add such an example to the docs.<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">So here is an example you can apply to a table of polygons<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">For polygons you can do something like below where p is the table name and geom is the polygon column.<u></u><u></u></p><p class="MsoNormal">This will generate 100 random points for each polygon<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">SELECT <a href="http://p.id" target="_blank">p.id</a>, dp.path[1], dp.geom <u></u><u></u></p><p class="MsoNormal">FROM p, ST_DumpPoints(ST_GeneratePoints(p.geom, 100)) AS dp;<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">The <a href="http://p.id" target="_blank">p.id</a> and path I just threw in cause I find them useful, but you could leave them out. <u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">Here is a self-contained using the example table in docs:<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">WITH p AS ( SELECT 1 AS id, ST_Buffer(<u></u><u></u></p><p class="MsoNormal"> ST_GeomFromText(<u></u><u></u></p><p class="MsoNormal"> 'LINESTRING(50 50,150 150,150 50)'),<u></u><u></u></p><p class="MsoNormal"> 10, 'endcap=round join=round') AS geom)<u></u><u></u></p><p class="MsoNormal">SELECT <a href="http://p.id" target="_blank">p.id</a>, dp.path[1], dp.geom <u></u><u></u></p><p class="MsoNormal">FROM p, ST_DumpPoints(ST_GeneratePoints(p.geom, 100)) AS dp;<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt"><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in"><p class="MsoNormal"><b>From:</b> postgis-users <<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>> <b>On Behalf Of </b>Shaozhong SHI<br><b>Sent:</b> Friday, September 8, 2023 10:54 AM<br><b>To:</b> PostGIS Users Discussion <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br><b>Subject:</b> Re: [postgis-users] generate a geometry column of random point, line and polygon<u></u><u></u></p></div></div><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">The simplicity is beautiful. Any simple script to do so? I think that will be an useful addition.<u></u><u></u></p><div><p class="MsoNormal">Regards,<u></u><u></u></p></div><div><p class="MsoNormal">David<br><br>On Friday, 8 September 2023, Regina Obe <<a href="mailto:lr@pcorp.us" target="_blank">lr@pcorp.us</a>> wrote:<u></u><u></u></p><blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt"><div><div><p class="MsoNormal">David,<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">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?<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">If you need to generate random points from a polygon:<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">Use ST_GeneratePoints: <a href="https://postgis.net/docs/en/ST_GeneratePoints.html" target="_blank">https://postgis.net/docs/en/ST_GeneratePoints.html</a><u></u><u></u></p><p class="MsoNormal">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.<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">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:<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"><a href="https://postgis.net/docs/en/ST_Buffer.html" target="_blank">https://postgis.net/docs/en/ST_Buffer.html</a><u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">SELECT ST_GeneratePoints(ST_Buffer(</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">ST_GeomFromText(</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New""> 'LINESTRING(50 50,150 150,150 50)'</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">), 0.5, 'endcap=square join=round'), 1000);</span><u></u><u></u></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New""> </span><u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">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<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">And then use something like <a href="https://postgis.net/docs/en/ST_Subdivide.html" target="_blank">https://postgis.net/docs/en/ST_Subdivide.html</a> to chop up the polygons.<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">To get a linestring out of that (It will be closed), you can take the boundary of any of the above<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal"><a href="https://postgis.net/docs/ST_Boundary.html" target="_blank">https://postgis.net/docs/ST_Boundary.html</a><u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><p class="MsoNormal">Hope that helps,<u></u><u></u></p><p class="MsoNormal">Regina<u></u><u></u></p><p class="MsoNormal"> <u></u><u></u></p><div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt"><div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in"><p class="MsoNormal"><b>From:</b> postgis-users <<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>> <b>On Behalf Of </b>Shaozhong SHI<br><b>Sent:</b> Friday, September 8, 2023 7:56 AM<br><b>To:</b> PostGIS Users Discussion <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br><b>Subject:</b> [postgis-users] generate a geometry column of random point, line and polygon<u></u><u></u></p></div></div><p class="MsoNormal"> <u></u><u></u></p><div><p class="MsoNormal">Is a simple way to do this?<u></u><u></u></p><div><p class="MsoNormal"> <u></u><u></u></p></div><div><p class="MsoNormal">Regards,<u></u><u></u></p></div><div><p class="MsoNormal"> <u></u><u></u></p></div><div><p class="MsoNormal">David<u></u><u></u></p></div></div></div></div></div></blockquote></div></div></div></div><p class="MsoNormal">_______________________________________________<br>postgis-users mailing list<br><a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br><a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a><u></u><u></u></p></div></blockquote></div></div></div></div><p class="MsoNormal">_______________________________________________<br>postgis-users mailing list<br><a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br><a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a><u></u><u></u></p></div></blockquote></div></div></div></div>_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a><br>
</div></blockquote></div>