Ignore:
Timestamp:
Jan 11, 2006, 5:31:49 PM (18 years ago)
Author:
duncan
Message:

in pmesh.mesh, can now add a region specified as a polygon. (This isn't fully implemented yet)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/pmesh/mesh.py

    r2144 r2200  
    2828import load_mesh
    2929from coordinate_transforms.geo_reference import Geo_reference,DEFAULT_ZONE
     30from utilities.polygon import point_in_polygon
    3031
    3132SET_COLOUR='red'
     
    185186    Defined by a point in the hole enclosed by segments.
    186187    """
     188
    187189    HOLECORNERLENGTH = 6
     190   
    188191    def draw(self, canvas, tags, colour = 'purple',scale = 1, xoffset = 0, yoffset =0, ):
    189192        x =  scale*(self.x + xoffset)
     
    396399        Null represents no neighbor
    397400        """
    398         self.attribute = attribute
     401        self.attribute = attribute #this is a string
    399402       
    400403    def __repr__(self):
     
    501504        self.meshVertices=[]
    502505
    503         #Peters stuff
    504         # FIXME (DSG) Sets of what? 
    505506        self.setID={}
    506507        #a dictionary of names.
     
    667668        self.regions.append(h)
    668669        return h
    669    
     670
     671    #FIXME(DSG-DSG) remove EN, have a relative flag.
    670672    def addRegionEN(self, x,y):
    671673        h=Region(x-self.geo_reference.xllcorner,
     
    673675        self.regions.append(h)
    674676        return h
    675    
     677    def addRegionFromPolygon(self, polygon, tags=None,
     678                             maxArea=None, Geo_ref=None):
     679        #take into account georef
     680        #create points, segs and tags
     681        region_dict = {}
     682        region_dict['points'] = polygon
     683
     684        #Create segments
     685        #E.g. [[0,1], [1,2], [2,3], [3,0]]
     686        segments = []
     687        N = len(polygon)
     688        for i in range(N):
     689            lo = i
     690            hi = (lo + 1) % N
     691            segments.append( [lo, hi] )
     692        region_dict['segments'] = segments
     693
     694
     695        #Create tags
     696        #E.g. ['wall', 'wall', 'ocean', 'wall']
     697
     698        segment_tags = ['']*N
     699        if tags is not None:
     700            for key in tags:
     701                indices = tags[key]
     702                for i in indices:
     703                    segment_tags[i] = key
     704        region_dict['segment_tags'] = segment_tags
     705   
     706        self.addVertsSegs(region_dict) #this is assuming absolute geos
     707   
     708        #get inner point
     709        inner_point = point_in_polygon(polygon)
     710        inner = self.addRegionEN(inner_point[0], inner_point[1])
     711
     712        if maxArea is not None:
     713            inner.setMaxArea(maxArea)
     714       
    676715    def getUserVertices(self):
    677716        return self.userVertices
     
    13121351        segs2delete = self.alphaUserSegments
    13131352        #FIXME(DSG-DSG) this algorithm needs comments
    1314         #FIXME(DSG-DSG) can it be sped up?  It's slow
    13151353        new_segs = {}
    13161354        #alpha_segs = []
     
    16031641        yoffset = HEIGHT - b
    16041642        return [SCALE, xoffset, yoffset]
    1605            
    1606     def plotMeshTriangle(self,tag = 0,WIDTH = 400,HEIGHT = 400):
    1607         """
    1608         Plots all node connections.
    1609         tag = 0 (no node numbers), tag = 1 (node numbers)
    1610         """
    1611 
    1612         try:
    1613             from Tkinter import Tk, Frame, Button, Canvas, BOTTOM, Label
    1614 
    1615             [SCALE, xoffset, yoffset] = self.scaleoffset( WIDTH, HEIGHT)
    1616            
    1617             root = Tk()
    1618             frame = Frame(root)
    1619             frame.pack()
    1620             button = Button(frame, text="OK", fg="red", command=frame.quit)
    1621             button.pack(side=BOTTOM)
    1622             canvas = Canvas(frame,bg="white", width=WIDTH, height=HEIGHT)
    1623             canvas.pack()
    1624             text = Label(frame, width=20, height=10, text='triangle mesh')
    1625             text.pack()
    1626 
    1627             #print self.meshTriangles
    1628             for triangle in self.meshTriangles:
    1629                 triangle.draw(canvas,1,
    1630                               scale = SCALE,
    1631                               xoffset = xoffset,
    1632                               yoffset = yoffset )
    1633                
    1634             root.mainloop()
    1635 
    1636         except:
    1637             print "Unexpected error:", sys.exc_info()[0]
    1638             raise
    1639 
    1640             #print """
    1641             #node::plot Failed.
    1642             #Most probably, the Tkinter module is not available.
    1643             #"""
    1644 
    1645     def plotUserSegments(self,tag = 0,WIDTH = 400,HEIGHT = 400):
    1646         """
    1647         Plots all node connections.
    1648         tag = 0 (no node numbers), tag = 1 (node numbers)
    1649         """
    1650 
    1651         try:
    1652             from Tkinter import Tk, Frame, Button, Canvas, BOTTOM, Label
    1653            
    1654             [SCALE, xoffset, yoffset] = self.scaleoffset( WIDTH, HEIGHT)
    1655 
    1656             root = Tk()
    1657             frame = Frame(root)
    1658             frame.pack()
    1659             button = Button(frame, text="OK", fg="red", command=frame.quit)
    1660             button.pack(side=BOTTOM)
    1661             canvas = Canvas(frame, bg="white", width=WIDTH, height=HEIGHT)
    1662             canvas.pack()
    1663             text = Label(frame, width=20, height=10, text='user segments')
    1664             text.pack()
    1665            
    1666             for segment in self.getUserSegments():
    1667                 segment.draw(canvas,SCALE, xoffset, yoffset )
    1668                
    1669             root.mainloop()
    1670 
    1671         except:
    1672             print "Unexpected error:", sys.exc_info()[0]
    1673             raise
    1674 
    1675             #print """
    1676             #node::plot Failed.
    1677             #Most probably, the Tkinter module is not available.
    1678             #"""
    1679            
    1680 
     1643
     1644         
    16811645    def exportASCIIobj(self,ofile):
    16821646        """
Note: See TracChangeset for help on using the changeset viewer.