createValidArea method

Geometry createValidArea(
  1. Geometry rawAreaGeom
)

Creates a valid area geometry from one that possibly has bad topology (i.e. self-intersections). Since buffer can handle invalid topology, but always returns valid geometry, constructing a 0-width buffer "corrects" the topology. Note this only works for area geometries, since buffer always returns areas. This also may return empty geometries, if the input has no actual area. If the input is empty or is not polygonal, this ensures that POLYGON EMPTY is returned.

@param rawAreaGeom an area geometry possibly containing self-intersections @return a valid area geometry

Implementation

Geometry createValidArea(Geometry rawAreaGeom) {
  bool isValidArea = rawAreaGeom.getDimension() == 2 && rawAreaGeom.isValid();
  // if geometry is invalid then make it valid
  if (isEnsureValidTopology && !isValidArea) {
    return rawAreaGeom.buffer(0.0);
  }
  return rawAreaGeom;
}