Changeset 404


Ignore:
Timestamp:
Oct 15, 2004, 8:53:19 AM (20 years ago)
Author:
duncan
Message:

adding reading of ungenerated polygon files, from ?arcGIS

Location:
inundation/ga/storm_surge/pmesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pmesh/mesh.py

    r369 r404  
    33"""General 2D triangular classes for triangular mesh generation.
    44
     5   Note: A .index attribute is added to objects such as vertices and
     6   segments, often when reading and writing to files.  This information
     7   should not be used as percistant information.  It is not the 'index' of
     8   an element in a list.
     9
     10   
    511   Copyright 2003/2004
    612   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
     
    346352
    347353Segment.set_default_tag("")       
     354
    348355class Mesh:
    349356    """
     
    548555        self.userSegments.append(s)
    549556        return s
    550 
    551557   
    552558    def clearTriangulation(self):
     
    690696        pointlist=[]
    691697        pointattributelist=[]
     698       
    692699        index = 0
    693 
    694700        for vertex in self.userVertices:
    695701            vertex.index = index
     
    904910        #print "@#@#@#"
    905911       
    906         index = 0
     912        #index = 0
    907913        for point in genDict['pointlist']:
    908914            v=Vertex(point[0], point[1])
    909             v.index = index
    910             index +=1
     915            #v.index = index
     916            #index +=1
    911917            self.userVertices.append(v)
    912918
    913         index = 0
     919        #index = 0
    914920        for seg,marker in map(None,genDict['segmentlist'],genDict['segmentmarkerlist']):
    915921            segObject = Segment( self.userVertices[seg[0]],
    916922                           self.userVertices[seg[1]], marker = marker )
    917             segObject.index = index
    918             index +=1
     923            #segObject.index = index
     924            #index +=1
    919925            self.userSegments.append(segObject)
    920926
     
    929935#            index += 1
    930936       
    931         index = 0
     937        #index = 0
    932938        for point in genDict['holelist']:
    933939            h=Hole(point[0], point[1])
    934             h.index = index
    935             index +=1
     940            #h.index = index
     941            #index +=1
    936942            self.holes.append(h)
    937943
    938         index = 0
     944        #index = 0
    939945        for reg,att,maxArea in map(None,
    940946                                   genDict['regionlist'],
     
    945951                             tag = att,
    946952                             maxArea = maxArea)
    947             Object.index = index
    948             index +=1
     953            #Object.index = index
     954            #index +=1
    949955            self.regions.append(Object)
    950 
     956       
     957    def addVertsSegs(self, outlineDict):
     958        """
     959        Add out-line (user Mesh) attributes given a dictionary of the lists
     960        point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) 
     961        segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
     962        #segment marker list: [S1Marker, S2Marker, ...] (list of ints)
     963        """
     964
     965        print "@#@#@#"
     966        print outlineDict
     967        print "@#@#@#"
     968
     969        localUserVertices = []
     970        #index = 0
     971        for point in outlineDict['pointlist']:
     972            v=Vertex(point[0], point[1])
     973            #v.index = index
     974            #index +=1
     975            self.userVertices.append(v)
     976            localUserVertices.append(v)
     977           
     978        #index = 0
     979        for seg in map(None,outlineDict['segmentlist']):
     980            segObject = Segment( localUserVertices[seg[0]],
     981                           localUserVertices[seg[1]] )
     982            #segObject.index = index
     983            #index +=1
     984            self.userSegments.append(segObject)
    951985       
    952986    def TestautoSegment(self):
     
    14851519
    14861520        load_mesh.loadASCII.export_xya_file(ofile, xya_dict, title, delimiter = " ")
    1487 
     1521       
     1522def importUngenerateFile(ofile):
     1523    """
     1524    import a file, ofile, with the format
     1525    [poly]
     1526    poly format:
     1527    First line:  <# of vertices> <x centroid> <y centroid>
     1528    Following lines: <x> <y>
     1529    last line:  "END"
     1530    """
     1531    fd = open(ofile,'r')
     1532    Dict = readUngenerateFile(fd)
     1533    fd.close()
     1534    return Dict
     1535
     1536def readUngenerateFile(fd):
     1537    """
     1538    import a file, ofile, with the format
     1539    [poly]
     1540    poly format:
     1541    First line:  <# of polynomial> <x centroid> <y centroid>
     1542    Following lines: <x> <y>
     1543    last line:  "END"
     1544    """
     1545    END_DELIMITER = 'END\n'
     1546   
     1547    points = []
     1548    segments = []
     1549   
     1550    isEnd = False
     1551    line = fd.readline() #not used <# of polynomial> <x> <y>
     1552    while not isEnd:
     1553        line = fd.readline()
     1554        fragments = line.split()
     1555        vert = [float(fragments.pop(0)),float(fragments.pop(0))]
     1556        points.append(vert)
     1557        PreviousVertIndex = len(points)-1
     1558        firstVertIndex = PreviousVertIndex
     1559       
     1560        line = fd.readline() #Read the next line
     1561        while line <> END_DELIMITER:
     1562            #print "line >" + line + "<"
     1563            fragments = line.split()
     1564            vert = [float(fragments.pop(0)),float(fragments.pop(0))]
     1565            points.append(vert)
     1566            ThisVertIndex = len(points)-1
     1567            segment = [PreviousVertIndex,ThisVertIndex]
     1568            segments.append(segment)
     1569            PreviousVertIndex = ThisVertIndex
     1570            line = fd.readline() #Read the next line
     1571            i =+ 1
     1572        # Remove the last segment and the last vertex
     1573        # then add a segment from the second last vert to the 1st vert
     1574        points.pop()
     1575        segments.pop()
     1576        ThisVertIndex = len(points)-1
     1577        segments.append([ThisVertIndex, firstVertIndex])
     1578       
     1579        line = fd.readline() # read <# of polynomial> <x> <y> OR END
     1580        #print "line >>" + line + "<<"
     1581        if line == END_DELIMITER:
     1582            isEnd = True
     1583   
     1584    #print "points", points       
     1585    #print "segments", segments
     1586    ungenerated_dict = {}
     1587    ungenerated_dict['pointlist'] = points
     1588    ungenerated_dict['segmentlist'] = segments
     1589    return ungenerated_dict
    14881590
    14891591def importMeshFromFile(ofile):
     
    16951797
    16961798if __name__ == "__main__":
    1697 # THIS CAN BE DELETED
    1698     list = ["internal boundary","sea","river inlet",
    1699             "","sea","","moat","internal boundary"]
    1700     #list = ["sea","river inlet","","sea","","moat"]
    1701     list = []
    1702     print list
    1703     preset = [""]
    1704     print preset
    1705     [intlist, converter] = segment_strings2ints(list, preset)
    1706     print "intlist",intlist
    1707     print "converter",converter
    1708     newlist = segment_ints2strings(intlist, converter)
    1709     print newlist
    1710     print "***************************"
    1711     list = [(4,5,"lo"),(3,2,"mo"),(3,2,"ro")]
    1712     #list = []
    1713     print "list",list
    1714     [newlist, converter] = region_strings2ints(list)
    1715     print "newlist",newlist
    1716     trilist = []
    1717     for x in newlist:
    1718         trilist.append([x[2]])
    1719         print "trilist",trilist
    1720     oldlist = region_ints2strings(trilist, converter) 
    1721     print "oldlist",oldlist   
     1799    #from mesh import *
     1800    # THIS CAN BE DELETED
     1801    m = Mesh()
     1802    dict = importUngenerateFile("ungen_test.txt")
     1803    m.addVertsSegs(dict)
     1804    print m
  • inundation/ga/storm_surge/pmesh/pmesh.py

    r349 r404  
    100100        self.menuBar.addmenuitem('File', 'separator')
    101101        self.menuBar.addmenuitem('File', 'command',
     102                                 'Add ungenerated file from arcGIS',
     103                                 label='Add ungenerated file...',
     104                                 command=self.ImportUngenerate)
     105       
     106        self.menuBar.addmenuitem('File', 'command',
    102107                                 'Export ASCII obj',
    103                                  label='Export ASCII obj...',
     108                                 label='Export ASCII obj',
    104109                                 command=self.exportObj)
    105110       
     
    123128                                 command=self.autoSegment)
    124129        self.menuBar.addmenuitem('File', 'command', 'Normalise mesh',
    125                                  label='Normalise mesh...', command=self.normaliseMesh)
     130                                 label='Normalise mesh', command=self.normaliseMesh)
    126131        self.menuBar.addmenuitem('File', 'command', 'Normalise mesh for glutobj',
    127                                  label='Normalise mesh for glutobj...', command=self.normalise4ObjMesh)
     132                                 label='Normalise mesh for glutobj', command=self.normalise4ObjMesh)
    128133        self.menuBar.addmenuitem('File', 'separator')
    129134        self.menuBar.addmenuitem('File', 'command', 'Exit program',
     
    150155            t.cycle("DrawMode")
    151156            if key == 'pointer': #FIXME - this is specified in line 925 as well
    152                 self.curFunc     = self.drawVertex
     157                self.curFunc  = self.drawVertex
    153158                t.setInitialSunkenButton("DrawMode")
    154             self.modeClass[key]  = Mode
     159            self.modeClass[key] = Mode
    155160            # for actions that occur when the mouse goes down
    156             self.mouseDownFunc[key] =mouseDownFunc
     161            self.mouseDownFunc[key] = mouseDownFunc
    157162         
    158163    def createZooms(self):
     
    752757                                   'No triangulation to export.')
    753758
    754                
    755    
     759   
     760
     761    def ImportUngenerate(self):
     762        ofile = tkFileDialog.askopenfilename(initialdir=self.currentPath,
     763                                             filetypes=[ ("ungenerated polygon information", "txt"),
     764                                           ("All Files", "*")])
     765        if ofile == "":
     766            # The user cancelled the loading action
     767            return
     768       
     769        try:
     770            self.clearSelections()
     771            self.canvas.delete(ALL)
     772            dict = mesh.importUngenerateFile(ofile)
     773            self.mesh.addVertsSegs(dict)
     774           
     775        except SyntaxError:
     776            #this is assuming that the SyntaxError is thrown in
     777            #loadxyafile
     778            showerror('File error',
     779                      ofile + ' is not in the correct format.')
     780        except IOError:
     781            #!!! this error type can not be thrown?
     782            showerror('File error',
     783                      'file ' + ofile + ' could not be found.')
     784        except RuntimeError:
     785            showerror('File error',
     786                  'file ' + ofile + ' has an unknown file type.')
     787   
     788        self.visualiseMesh(self.mesh)
     789       
    756790    def exportASCIIsegmentoutlinefile(self):
    757791        fileType = "tsh"
Note: See TracChangeset for help on using the changeset viewer.