コード例 #1
0
    def from_shape(cls,
                   shape,
                   height=0.,
                   name="area",
                   properties=None,
                   unit='um',
                   min_x=None,
                   max_x=None):
        '''
        Create an :class:`Area` from a :class:`Shape` object.

        Parameters
        ----------
        shape : :class:`Shape`
            Shape that should be converted to an Area.

        Returns
        -------
        :class:`Area` object.
        '''
        if _unit_support:
            from .units import Q_
            if isinstance(height, Q_):
                height = height.m_as(unit)
            if isinstance(min_x, Q_):
                min_x = min_x.m_as(unit)
            if isinstance(max_x, Q_):
                max_x = max_x.m_as(unit)

        obj = None
        g_type = None

        if isinstance(shape, MultiPolygon):
            g_type = "MultiPolygon"
        elif isinstance(shape, (Polygon, Shape, Area)):
            g_type = "Polygon"
        else:
            raise TypeError("Expected a Polygon or MultiPolygon object.")

        # find the scaling factor
        scaling = 1.
        if None not in (min_x, max_x):
            ext = np.array(shape.exterior.coords)
            leftmost = np.min(ext[:, 0])
            rightmost = np.max(ext[:, 0])
            scaling = (max_x - min_x) / (rightmost - leftmost)
            obj = scale(shape, scaling, scaling)
        else:
            if g_type == "Polygon":
                obj = Polygon(shape)
            else:
                obj = MultiPolygon(shape)

        obj.__class__ = cls
        obj._parent = None
        obj._unit = unit
        obj._geom_type = g_type
        obj.__class__ = Area
        obj._areas = None
        obj.height = height
        obj.name = name
        obj._prop = _PDict({} if properties is None else deepcopy(properties))
        obj._return_quantity = False

        return obj