[GIS] Postgis: ST_Buffer breaks because of no forward edges

postgispostgresql

I try to buffer a huge road network using ST_Buffer. After 8h of processing I get an error message:

ERROR: GEOSBuffer: TopologyException: No forward edges found in buffer subgraph

Here is my query (adapted from: memory issue when trying to buffer/union large dataset using postgis):

CREATE TABLE Nebenstrassen050 ("geom" geometry(multipolygon,3857), gid serial primary key);
CREATE SEQUENCE bseq;

WITH ordered AS (
  SELECT wkb_geometry AS geom
  FROM Nebenstrassen
  ORDER BY ST_GeoHash(st_transform(wkb_geometry,4326))
),
grouped AS (
  SELECT nextval('bseq') / 100000 AS id, ST_Buffer(ST_Collect(ST_MakeValid(geom)),50) AS geom
  FROM ordered
  GROUP BY id
),
groupedfinal AS (
  SELECT (ST_Dump(ST_Union(geom))).geom AS geom
  FROM grouped
)
INSERT INTO Nebenstrassen050 SELECT * FROM groupedfinal;

I tried to implement ST_MakeValid to avoid this error but it doesn’t show any effect. I’ working on a Win7 64bit machine with Postgres 9.3 and PostGIS 2.1. Any Ideas?

Best Answer

One of the collections is somehow confusing the buffer routine. So, first create a table of collections, then bisect the table (test the first half and the last half to find what half the bad collection is in, then test the first and last half of that half, etc, etc) to find the degenerate collection.

Simply changing the size of your grouping (change 100000 to 10000 perhaps) might help, but that's a guess. Simple testing of the buffer function doesn't seem to indicate that zero-length lines or empty lines cause errors, so probably it's just a particular combination of geometry in one of the collections. You could try running a very light ST_SnapToGrid to move the vertices around a little, which might resolve the error.

Sorry no definitive solution, it's a topology failure in the overlay code, which generally is a coordinate precision issue, and just shaking things up a bit can make (apparently nonsensical) differences.