<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _make_valid_if_necessary(shape):
 """ attempt to correct invalid shapes if necessary After simplification, even when preserving topology, invalid shapes can be returned. This appears to only occur with polygon types. As an optimization, we only check if the polygon types are valid. """

    if shape.type in ('Polygon', 'MultiPolygon') and not shape.is_valid:
        shape = shape.buffer(0)

        # return value from buffer is usually valid, but it's
        # not clear from the docs whether this is guaranteed,
        # so return None if not.
        if not shape.is_valid:
            return None

    return shape