def from_polygon(polygon, min_x=None, max_x=None, unit='um', parent=None, default_properties=None): ''' Create a shape from a :class:`shapely.geometry.Polygon`. Parameters ---------- polygon : :class:`shapely.geometry.Polygon` The initial polygon. min_x : float, optional (default: -5000.) Absolute horizontal position of the leftmost point in the environment in `unit` If None, no rescaling occurs. max_x : float, optional (default: 5000.) Absolute horizontal position of the rightmost point in the environment in `unit` If None, no rescaling occurs. unit : string (default: 'um') Unit in the metric system among 'um' (:math:`\mu m`), 'mm', 'cm', 'dm', 'm' parent : :class:`nngt.Graph` object The parent which will become a :class:`nngt.SpatialGraph`. default_properties : dict, optional (default: None) Default properties of the environment. ''' assert isinstance(polygon, Polygon), "`polygon` is not a Polygon " +\ "but a {}.".format(polygon.__class__) if _unit_support: from .units import Q_ 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 # find the scaling factor if None not in (min_x, max_x): ext = np.array(polygon.exterior.coords) leftmost = np.min(ext[:, 0]) rightmost = np.max(ext[:, 0]) scaling = (max_x - min_x) / (rightmost - leftmost) obj = scale(polygon, scaling, scaling) else: obj = Polygon(polygon) obj.__class__ = Shape obj._parent = None obj._unit = unit obj._geom_type = g_type obj._return_quantity = False obj._areas = { "default_area": Area.from_shape(obj, name="default_area", properties=default_properties) } return obj
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