def clip_geometry_to_srs_bounds(geometry, pyramid, multipart=False):
    """
    Clip input geometry to SRS bounds of given TilePyramid.

    If geometry passes the antimeridian, it will be split up in a multipart
    geometry and shifted to within the SRS boundaries.
    Note: geometry SRS must be the TilePyramid SRS!

    - geometry: any shapely geometry
    - pyramid: a TilePyramid object
    - multipart: return list of geometries instead of a GeometryCollection
    """
    if not geometry.is_valid:
        raise ValueError("invalid geometry given")
    pyramid_bbox = box(*pyramid.bounds)

    # Special case for global tile pyramids if geometry extends over tile
    # pyramid boundaries (such as the antimeridian).
    if pyramid.is_global and not geometry.within(pyramid_bbox):
        inside_geom = geometry.intersection(pyramid_bbox)
        outside_geom = geometry.difference(pyramid_bbox)
        # shift outside geometry so it lies within SRS bounds
        if isinstance(outside_geom, Polygon):
            outside_geom = [outside_geom]
        all_geoms = [inside_geom]
        for geom in outside_geom:
            geom_bounds = Bounds(*geom.bounds)
            if geom_bounds.left < pyramid.left:
                geom = translate(geom, xoff=2*pyramid.right)
            elif geom_bounds.right > pyramid.right:
                geom = translate(geom, xoff=-2*pyramid.right)
            all_geoms.append(geom)
        if multipart:
            return all_geoms
        else:
            return GeometryCollection(all_geoms)

    else:
        if multipart:
            return [geometry]
        else:
            return geometry
def clip_geometry_to_srs_bounds ( geometry , pyramid , multipart = False ) : if not geometry . is_valid : raise ValueError ( "invalid geometry given" ) pyramid_bbox = box ( * pyramid . bounds ) if pyramid . is_global and not geometry . within ( pyramid_bbox ) : inside_geom = geometry . intersection ( pyramid_bbox ) outside_geom = geometry . difference ( pyramid_bbox ) if isinstance ( outside_geom , Polygon ) : outside_geom = [ outside_geom ] all_geoms = [ inside_geom ] for geom in outside_geom : geom_bounds = Bounds ( * geom . bounds ) if geom_bounds . left < pyramid . left : geom = translate ( geom , xoff = 2 * pyramid . right ) elif geom_bounds . right > pyramid . right : geom = translate ( geom , xoff = - 2 * pyramid . right ) all_geoms . append ( geom ) if multipart : return all_geoms else : return GeometryCollection ( all_geoms ) else : if multipart : return [ geometry ] else : return geometry
Clip input geometry to SRS bounds of given TilePyramid.

    If geometry passes the antimeridian, it will be split up in a multipart
    geometry and shifted to within the SRS boundaries.
    Note: geometry SRS must be the TilePyramid SRS!

    - geometry: any shapely geometry
    - pyramid: a TilePyramid object
    - multipart: return list of geometries instead of a GeometryCollection