3

I have a problem that ST_VoronoiPolygons started to give me GEOSVoronoiDiagram: TopologyException: Input geom 1 is invalid: Self-intersection

The function that I use it for have worked for a long time for me.

3
  • That's odd to see; ST_VoronoiPolygons essentially interprets its inputs as a MultiPoint, no matter what the actual geometry type is, so the concept of validity doesn't really apply.
    – dbaston
    Jun 21, 2018 at 12:40
  • I filed a related ticket at trac.osgeo.org/postgis/ticket/4335#ticket
    – tilt
    Feb 27, 2019 at 15:54
  • This is caused by a robustness problem in the underlying GEOS library. This has been fixed and will show up in PostGIS in the next release.
    – dr_jts
    Sep 7, 2019 at 22:16

2 Answers 2

5

The problem was connected to that the data I had was in a perfect grid. I don't know why but ST_VoronoiPolygons couldn't handle it.

So what I did was that I altered the points slightly (I use Python to populate the data so I added import random and + random.gauss(0.00000001, 0.00000001) to the x and y coordinate)

3
  • You could probably fix this more elegantly by using ST_SnapToGrid in Postgis. These sorts of errors are usually caused by spurious precision in geometries, so reducing the number of decimal places generally works. Jun 21, 2018 at 10:48
  • Well, I tried with ST_SnapToGrid, hence when I used a size "small enough" I got the same problem.
    – axel_ande
    Jun 21, 2018 at 10:59
  • Exactly. But it is much more deterministic than using random, which could still result in topology exceptions occasionally. Jun 21, 2018 at 16:04
0

Maybe there was a self-intersection... Have you tried ST_isValid and ST_isValidDetail functions? The second one could tell you where exactly this intersection is. The simplest way to deal with them is to use ST_Buffer function with radius = 0 - that's because ST_Buffer is creating brand new geometry based on origin and this geometry is always valid. With radius 0 you'll get exactly the same geometry but without self-intersections.

1
  • Is ST_MakeValid(geom) a better option to ST_Buffer(geom, 0)? Seems more intentional. Oct 18, 2021 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.