4

I have a big problem when I want to convert my WKB binary column to a geometry one using the function ST_geomFromWKB(wkb_geometry). It works only for valid multipolygon but when I take the overall data, it gives me the following error:

ERROR: Polygon must have at least four points in each ring

Moreover, I cannot use the function ST_IsValid or ST_IsValidReason because it gives me always the same error :

ERROR: Polygon must have at least four points in each ring

  • 2
    A polygon must have an area. For that at least three vertices are needed to make a triangle: A, B, and C. By definition WKB polygons must be closing, so A-B-C-A is the minimum valid polygon. I guess that you play with OpenStreetMap data that contains at least A-B-A polygons. Filter them out (or delete permanently) with WHERE ST_IsValid(wkb_geometry)=TRUE. – user30184 Jan 17 '18 at 15:33
  • anyway you can upload the data? – ziggy Jan 17 '18 at 15:40
  • what happens if you convert those polygons to lines and then back to polygons? it usually helps fix non closing rings – Elio Diaz Jan 17 '18 at 16:08
  • you can also try running st_buildarea on the geometry column, or st_makepolygon see what happens – ziggy Jan 17 '18 at 16:34
  • All these functions : st_buildarea , ST_IsValid , st_makepolygon work on geometry not on bytea (binary) column such as wkb_geometry. The problem when I use them, I have always the same problem : ERROR: Polygon must have at least four points in each ring – Mabrouk Jan 18 '18 at 9:06
2

It's not possible to load geometries that have these errors into PostGIS using WKB. If you look at the source of ST_GeomFromWKB or ST_GeomFromEWKB, you'll see that they're both hardcoded to tell the WKB parser to check that (a) each ring has at least four points, and (b) each ring is closed.

If you want to correct these geometries, it'll be easiest to first work with a tool that doesn't have these restrictions. (Though it might be fun to try and catch the parsing error and just pad in some extra well-chosen hex characters.)

  • I loaded my geometry with ogr2ogr and I fount many invalid multipolygons in my geom_wkb column. I don't know how can I filter my database because all function Is_Valid, ST_NPoints,etc work only on correct geometry. – Mabrouk Jan 19 '18 at 10:16
0

You can ignore rows with incorrect data, how to mark them I answered here: https://gis.stackexchange.com/a/321133/142211

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.