import json


def check_geometry_size(footprint):
    """
    Excessive large geometries are problematic of AWS SQS (max size 256kb) and cause 
    performance issues becuase they are stored in plain text in the JSON
    blob.

    This func reads the geojson and applies a simple heuristic to reduce the
    footprint size through simplification. With each iteration, the geometry
    is simplified by 0.01 degrees.
    
    Parameters
    ----------
    footprint : obj
                A shapely Polygon or MultiPolygon

    Returns
    -------
    geojson : dict
              A geojson representation of the geometry
    """
    geojson = footprint.__geo_interface__
    as_str = json.dumps(geojson)
    geomsize = len(as_str.encode('utf-8'))
    n_iterations = 0
    while geomsize > 125000:
        footprint = footprint.simplify(0.01)
        geojson = footprint.__geo_interface__
        as_str = json.dumps(geojson)
        geomsize = len(as_str.encode('utf-8'))
        n_iterations += 1
    return geojson