lsst.afw  14.0-25-g0e8fc950e+7
polygon.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 
3 from ._polygon import Polygon
4 
5 __all__ = [] # import this module only for its side effects
6 
7 
8 def __repr__(self):
9  return "%s(%s)" % (self.__class__.__name__, [p for p in self.getVertices()])
10 
11 
12 def __reduce__(self):
13  return self.__class__, (self.getVertices(),)
14 
15 
16 def __iter__(self):
17  """Iterator over vertices"""
18  vertices = self.getVertices()
19  return iter(vertices)
20 
21 
22 def __getitem__(self, i):
23  return [pt for pt in self][i]
24 
25 
26 def __len__(self):
27  return self.getNumEdges()
28 
29 
30 def __contains__(self, point):
31  """point in polygon?"""
32  return self.contains(point)
33 
34 
35 def display(self, xy0=None, frame=1, ctype=None):
36  """Display polygon on existing frame in ds9"""
37  import lsst.afw.geom as afwGeom
38  import lsst.afw.display.ds9 as ds9
39  xy0 = afwGeom.Extent2D(0, 0) if xy0 is None else afwGeom.Extent2D(xy0)
40  with ds9.Buffering():
41  for p1, p2 in self.getEdges():
42  ds9.line((p1 - xy0, p2 - xy0), frame=frame, ctype=ctype)
43 
44 
45 def plot(self, axes=None, **kwargs):
46  """Plot polygon with matplotlib
47 
48  @param axes: Matplotlib axes to use, or None
49  @param kwargs: Additional arguments for plotting (e.g., color, line type)
50  @return Matplotlib axes
51  """
52  import numpy
53  if axes is None:
54  import matplotlib.pyplot as plt
55  plt.figure()
56  axes = plt.axes()
57  for p1, p2 in self.getEdges():
58  x = (p1.getX(), p2.getX())
59  y = (p1.getY(), p2.getY())
60  axes.plot(x, y, **kwargs)
61  vertices = self.getVertices()
62  x = numpy.array([p[0] for p in vertices])
63  y = numpy.array([p[1] for p in vertices])
64  axes.scatter(x, y, **kwargs)
65  return axes
66 
67 
68 Polygon.__repr__ = __repr__
69 Polygon.__reduce__ = __reduce__
70 Polygon.__iter__ = __iter__
71 Polygon.__getitem__ = __getitem__
72 Polygon.__len__ = __len__
73 Polygon.__contains__ = __contains__
74 Polygon.display = display
75 Polygon.plot = plot
def display(self, xy0=None, frame=1, ctype=None)
Definition: polygon.py:35
def __contains__(self, point)
Definition: polygon.py:30
def plot(self, axes=None, kwargs)
Definition: polygon.py:45