Define the area class
| 13 | import pandas as pd |
| 14 | |
| 15 | class Area: |
| 16 | """Define the area class""" |
| 17 | dtype = 'area' |
| 18 | def __init__(self, body=None, unit=None): |
| 19 | self.body = body if body!=None else [] |
| 20 | self.buf, self.unit = [], unit |
| 21 | |
| 22 | def addpoint(self, p): |
| 23 | self.buf.append(p) |
| 24 | |
| 25 | def commit(self): |
| 26 | if len(self.buf)<3:return False |
| 27 | if self.buf[0]!=self.buf[-1]: |
| 28 | self.buf.append(self.buf[0]) |
| 29 | self.body.append(self.buf) |
| 30 | self.buf = [] |
| 31 | |
| 32 | def snap(self, x, y, lim): |
| 33 | cur, cid, minl = None, None, 1000 |
| 34 | for pg in self.body: |
| 35 | for i in pg: |
| 36 | d = (i[0]-x)**2+(i[1]-y)**2 |
| 37 | if d < minl:cur,cid,minl = pg,i,d |
| 38 | if minl**0.5>lim:return None |
| 39 | return cur, cur.index(cid) |
| 40 | |
| 41 | def pick(self, x, y, lim): |
| 42 | rst = self.snap(x, y, lim) |
| 43 | if rst!=None:return rst |
| 44 | for i in self.body: |
| 45 | if Polygon(i).contains(Point(x,y)): |
| 46 | return True, i |
| 47 | return None |
| 48 | |
| 49 | def draged(self, ox, oy, nx, ny, i): |
| 50 | self.update, self.infoupdate(), True |
| 51 | if i[0]==True: |
| 52 | for j in range(len(i[1])): |
| 53 | i[1][j] = (i[1][j][0]+(nx-ox), i[1][j][1]+(ny-oy)) |
| 54 | else: |
| 55 | i[0][i[1]] = (nx, ny) |
| 56 | #print('drag,',i,self.body[0][0][i]hasattr(ips.roi, 'topolygon')) |
| 57 | if i[1]==0:i[0][-1] = (nx,ny) |
| 58 | if i[1]==len(i[0])-1: |
| 59 | i[0][0] = (nx, ny) |
| 60 | |
| 61 | def report(self, title): |
| 62 | rst = [] |
| 63 | titles = ['Area', 'OX', 'OY'] |
| 64 | for pg in self.body: |
| 65 | plg = Polygon(pg) |
| 66 | area, xy = plg.area, plg.centroid |
| 67 | unit = 1 if self.unit==None else self.unit[0] |
| 68 | axy = [area*unit**2, xy.x*unit, xy.y*unit] |
| 69 | rst.append([round(i,1) for i in axy]) |
| 70 | IPy.show_table(pd.DataFrame(rst, columns=titles), title) |
| 71 | |
| 72 | def draw(self, dc, f, **key): |