def merge_envelopes(envelopes, extent_type='intersection'):
    """Return the extent of a list of envelopes.

    Return the extent of the union or intersection of a list of envelopes

    :param envelopes: list of envelopes or raster filenames (also mixed)
    :type envelopes: list, tuple, iterator
    :param extent_type: intersection or union
    :return: (xmin, xmax, ymin, ymax) in world coordinates
    """
    et = extent_type.lower()
    xmin, xmax, ymin, ymax = envelopes[0]
    if len(envelopes) > 1:
        if et == 'intersection':
            for xmin0, xmax0, ymin0, ymax0 in envelopes[1:]:
                xmin = max(xmin, xmin0)
                xmax = min(xmax, xmax0)
                ymin = max(ymin, ymin0)
                ymax = min(ymax, ymax0)
        elif et == 'union':
            for xmin0, xmax0, ymin0, ymax0 in envelopes[1:]:
                xmin = min(xmin, xmin0)
                xmax = max(xmax, xmax0)
                ymin = min(ymin, ymin0)
                ymax = max(ymax, ymax0)
        else:
            msg = 'extent_type {} unknown'.format(extent_type)
            raise TypeError(msg)
    return xmin, xmax, ymin, ymax