Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

__all__ = [] # import this module only for its side effects 

 

from lsst.utils import continueClass 

from .polygon import Polygon 

 

 

@continueClass # noqa F811 

class Polygon: 

def __repr__(self): 

return "%s(%s)" % (self.__class__.__name__, [p for p in self.getVertices()]) 

 

def __reduce__(self): 

return self.__class__, (self.getVertices(),) 

 

def __iter__(self): 

"""Iterator over vertices""" 

vertices = self.getVertices() 

return iter(vertices) 

 

def __getitem__(self, i): 

return [pt for pt in self][i] 

 

def __len__(self): 

return self.getNumEdges() 

 

def __contains__(self, point): 

"""point in polygon?""" 

return self.contains(point) 

 

def display(self, xy0=None, frame=1, ctype=None): 

"""Display polygon on existing frame in ds9""" 

import lsst.geom 

import lsst.afw.display.ds9 as ds9 

xy0 = lsst.geom.Extent2D(0, 0) if xy0 is None else lsst.geom.Extent2D(xy0) 

with ds9.Buffering(): 

for p1, p2 in self.getEdges(): 

ds9.line((p1 - xy0, p2 - xy0), frame=frame, ctype=ctype) 

 

def plot(self, axes=None, **kwargs): 

"""Plot polygon with matplotlib 

 

Parameters 

---------- 

axes : `matplotlib.axes.Axes` 

Matplotlib axes to use, or None 

kwargs : any 

Additional arguments to `matplotlib.axes.Axes.plot` 

or `matplotlib.axes.Axes.scatter`. 

 

Returns 

------- 

axes : `matplotlib.axes.Axes` 

The axes used to make the plot (same as ``axes``, if provided). 

""" 

import numpy 

if axes is None: 

import matplotlib.pyplot as plt 

plt.figure() 

axes = plt.axes() 

for p1, p2 in self.getEdges(): 

x = (p1.getX(), p2.getX()) 

y = (p1.getY(), p2.getY()) 

axes.plot(x, y, **kwargs) 

vertices = self.getVertices() 

x = numpy.array([p[0] for p in vertices]) 

y = numpy.array([p[1] for p in vertices]) 

axes.scatter(x, y, **kwargs) 

return axes