[Bug] - Fix the edge cases of invalid geometries from sources

Error case in EI test runs:

  • One building from Microsoft in UKR remains invalid after parsing GeometryRule. Furthermore, the 3D geometry extruded by geometry is also at the risk of being invalid. The EI error log is:
RuleError: ['UpsertBuildingRule' failed with arguments:...
...
psycopg2.errors.InternalError: Polygon is invalid: ring 0 self intersects: ...

Checked through Microsoft_buildings and found ~20 errorneous cases after running ST_Buffer(ST_GeomFromText(ST_AsText(geometry), 4326), 0) as GeometryRule. Apparently, it fails to repair the raw geometry_wkt.

Solutions:

  1. Use the ST_MakeValid() to repair geometries. The cases that cannot be fixed by ST_Buffer() can be fixed by ST_MakeValid().
  2. Add validity check rule for geometry before upserting. Since there is no validity check function for 3D geometry in PostGIS, then the check is limited to 2D geometry. If the processed 2D geometry stays as invalid, the 3D geometry extruded from 2D will run into problems too, and the process should be cancelled. In this way, some egde cases can be fixed.
Edited by Danijel Schorlemmer