5

Sorry folks, I know it sounds like a lame question - and, yes I did read the manual... :-)

I'm trying to make Voronoi diagrams around points I have in a PostgreSQL table.

It should be rather easy - but I can't find a single example on using ST_VoronoiPolygons() on real data (i.e. a table of points).

All I keep seeing is examples similar to that in the docs. like this:

SELECT
    ST_VoronoiPolygons(geom) AS geom
    FROM (SELECT 'MULTIPOINT (50 30, 60 30, 100 100,10 150, 110 120)'::geometry AS geom ) AS g
;

That works fine on my computer. But I want to make Voronoi polygons around my real point, not just example-points hard-coded in the SQL.

Can anybody point me to a working example of SQL that shows the use of ST_VoronoiPolygons() with an existing PostgreSQL Point table as input.

Martin
  • 550
  • 4
  • 16
  • Can you post an example of a query you are using against one of your table? – Fabien Ancelin May 12 '17 at 16:31
  • 1
    Yes, see https://gis.stackexchange.com/questions/172198/constructing-voronoi-diagram-in-postgis/174219#174219. You might need to fiddle with the memory settings in postgresql.conf, but the functionality works well. – John Powell May 12 '17 at 16:45
  • @JohnPowellakaBarça Damn - I didn't look close enough at that page, because it was primarily about the old (<2.3) method... But you are right. Can you post it as a real answer, so I can acknowledge. I would like to post the fully functional solution, for the benefit of others. – Martin May 14 '17 at 14:54
  • @MartinHvidberg. Sure Martin, done. I spent a lot of time on getting voronoi poligonization working in Postgis. Initially I used a Python stored procedure, which worked, but was slow and had memory issues, then I had to build from source because we had old versions of everything at work. Now, thanks to DBaston, Geos developers and several others, it all works like a charm, and is super fast. – John Powell May 14 '17 at 16:56

1 Answers1

17

The trick when using a table of points is to feed all the points into ST_VoronoiPolygons by using ST_Collect, which simply combines all the points into a MultiPoint, without performing any spatial operations such as would happen with ST_Union. So, for example, with a random table of points as input, you can do:

WITH points(geom) AS 
 (SELECT 
      ST_MakePoint(random(), random()) 
   FROM generate_series(1, 100))
 SELECT 
     (ST_DUMP(ST_VoronoiPolygons(ST_Collect(geom)))).geom 
 FROM points;

which produces 100 polygons from the 100 input points, as you would expect.

See the questions and answers here for a fuller treatment of some of the issues you might encounter with ST_VoronoiPolygons.

John Powell
  • 13,649
  • 5
  • 46
  • 62