def bbox_filter ( wkt_string, lonmin, latmin, lonmax, latmax ):
    """
    Return a string that will select records from the geographic range
    given in WKT. If four bounding coordinates are given instead, a
    POLYGON() is constructed from them.
    """
    
    if wkt_string:
        return {'loc': wkt_string}

    elif lonmin or latmin or lonmax or latmax:
        pattern = 'POLYGON(({0} {1},{2} {1},{2} {3},{0} {3},{0} {1}))'
        return {'loc': pattern.format(lonmin, latmin, lonmax, latmax)}

    else:
        return {}