[GIS] Dealing with invalid multipolygons while transforming WKB to geometry format

postgis

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

Best Answer

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.)