def density_filter(a, thresh=0.3):
    """
    Only keep pixels if over thresh% of pixels in the window are > 0

    Args:
        a: the flattened form of an n x m numpy array
        thresh: filtering threshold

    Returns:
        either 0 or the pixel value


    """
    edge_length = int(len(a) ** 0.5)

    b = a.reshape((edge_length, edge_length))

    center = int((edge_length - 1) / 2)
    center_val = b[center, center]

    if center_val <= 0:  # if the value is 0 we can just move on. If it's less than 0 (should not happen in a dhm) then repair it
        return 0

    b = b > 0  # cast to Bools. If DHM is over 0, True

    density = b.sum() / edge_length**2

    if density >= thresh:
        return center_val
    else:
        return 0