ocrd_cis/ocropy/segment.py
def polygon_for_parent(polygon, parent):
    """Clip polygon to parent polygon range.
    
    (Should be moved to ocrd_utils.coordinates_for_segment.)
    """
    childp = Polygon(polygon)
    if isinstance(parent, PageType):
        if parent.get_Border():
            parentp = Polygon(polygon_from_points(parent.get_Border().get_Coords().points))
        else:
            parentp = Polygon([[0,0], [0,parent.get_imageHeight()],
                               [parent.get_imageWidth(),parent.get_imageHeight()],
                               [parent.get_imageWidth(),0]])
    else:
        parentp = Polygon(polygon_from_points(parent.get_Coords().points))
    # check if clipping is necessary
    if childp.within(parentp):
        return polygon
    # ensure input coords have valid paths (without self-intersection)
    # (this can happen when shapes valid in floating point are rounded)
    childp = make_valid(childp)
    parentp = make_valid(parentp)
    # clip to parent
    interp = childp.intersection(parentp)
    # post-process
    if interp.is_empty or interp.area == 0.0:
        # FIXME: we need a better strategy against this
        raise Exception("intersection of would-be segment with parent is empty")
    if interp.type == 'GeometryCollection':
        # heterogeneous result: filter zero-area shapes (LineString, Point)
        interp = unary_union([geom for geom in interp.geoms if geom.area > 0])
    if interp.type == 'MultiPolygon':
        # homogeneous result: construct convex hull to connect
        # FIXME: construct concave hull / alpha shape
        interp = interp.convex_hull
    if interp.minimum_clearance < 1.0:
        # follow-up calculations will necessarily be integer;
        # so anticipate rounding here and then ensure validity
        interp = asPolygon(np.round(interp.exterior.coords))
        interp = make_valid(interp)
    return interp.exterior.coords[:-1]
Clip polygon to parent polygon range.
    
    (Should be moved to ocrd_utils.coordinates_for_segment.)
    
Clip polygon to parent polygon range.
Clip polygon to parent polygon range .
def polygon_for_parent(polygon, parent):
    childp = Polygon(polygon)
    if isinstance(parent, PageType):
        if parent.get_Border():
            parentp = Polygon(polygon_from_points(parent.get_Border().get_Coords().points))
        else:
            parentp = Polygon([[0,0], [0,parent.get_imageHeight()],
                               [parent.get_imageWidth(),parent.get_imageHeight()],
                               [parent.get_imageWidth(),0]])
    else:
        parentp = Polygon(polygon_from_points(parent.get_Coords().points))
    if childp.within(parentp):
        return polygon
    childp = make_valid(childp)
    parentp = make_valid(parentp)
    interp = childp.intersection(parentp)
    if interp.is_empty or interp.area == 0.0:
        raise Exception("intersection of would-be segment with parent is empty")
    if interp.type == 'GeometryCollection':
        interp = unary_union([geom for geom in interp.geoms if geom.area > 0])
    if interp.type == 'MultiPolygon':
        interp = interp.convex_hull
    if interp.minimum_clearance < 1.0:
        interp = asPolygon(np.round(interp.exterior.coords))
        interp = make_valid(interp)
    return interp.exterior.coords[:-1]
def polygon_for_parent ( polygon , parent ) : childp = Polygon ( polygon ) if isinstance ( parent , PageType ) : if parent . get_Border ( ) : parentp = Polygon ( polygon_from_points ( parent . get_Border ( ) . get_Coords ( ) . points ) ) else : parentp = Polygon ( [ [ 0 , 0 ] , [ 0 , parent . get_imageHeight ( ) ] , [ parent . get_imageWidth ( ) , parent . get_imageHeight ( ) ] , [ parent . get_imageWidth ( ) , 0 ] ] ) else : parentp = Polygon ( polygon_from_points ( parent . get_Coords ( ) . points ) ) if childp . within ( parentp ) : return polygon childp = make_valid ( childp ) parentp = make_valid ( parentp ) interp = childp . intersection ( parentp ) if interp . is_empty or interp . area == 0.0 : raise Exception ( "intersection of would-be segment with parent is empty" ) if interp . type == 'GeometryCollection' : interp = unary_union ( [ geom for geom in interp . geoms if geom . area > 0 ] ) if interp . type == 'MultiPolygon' : interp = interp . convex_hull if interp . minimum_clearance < 1.0 : interp = asPolygon ( np . round ( interp . exterior . coords ) ) interp = make_valid ( interp ) return interp . exterior . coords [ : - 1 ]
Clip polygon to parent polygon range.
Clip polygon to parent polygon range .
"""Clip polygon to parent polygon range.
    
    (Should be moved to ocrd_utils.coordinates_for_segment.)
    """ # check if clipping is necessary # ensure input coords have valid paths (without self-intersection) # (this can happen when shapes valid in floating point are rounded) # clip to parent # post-process # FIXME: we need a better strategy against this # heterogeneous result: filter zero-area shapes (LineString, Point) # homogeneous result: construct convex hull to connect # FIXME: construct concave hull / alpha shape # follow-up calculations will necessarily be integer; # so anticipate rounding here and then ensure validity
polygon
parent